// JavaScript Document

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AJAX Master Callback Function
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function DoCallback(data, url, what)
{
	
	   if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processReqChange;
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.send(data);
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req = new ActiveXObject('Microsoft.XMLHTTP')
			if (req) {
				req.onreadystatechange = processReqChange;
				req.open('POST', url, true);
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				req.send(data);
        }
    }
}
	
function processReqChange() {
    // only if req shows 'loaded'
    if (req.readyState == 4) {
        // only if 'OK'
        if (req.status == 200) {
            eval(what);
        } else {
            alert('There was a problem retrieving data: ' +
                req.responseText);
        }
    }
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Login User with AJAX
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function loginUser()
{
	url = "php/login.php";
	what ="LoginStatus(req.responseText)";
	var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
   	DoCallback("username="+username+"&password="+password,"../php/login.php","LoginStatus(req.responseText)");
}

function LoginStatus(Status)
            {
                if(Status == 1)
				{
                    alert("Welcome!");
					updateAll();
					document.getElementById('loginContainer').style.display = "none";
				}
                else
				{
                    alert("Bad username or password!");
				}
            }
			
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Refresh All DIV states
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function updateAll()
{
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var loginVars = "username="+username+"&password="+password;
	reqZenPhoto(loginVars);
	reqUpdateContacts(loginVars);
}

function reqZenPhoto(loginVars)  
{
	url = "galleryUpdate.php";
	what = "updateZenphoto(req.responseText)";
	DoCallback(loginVars, url, what);
}

function updateZenphoto(updateInfo) 
{
	document.getElementById('zenphoto').innerHTML = updateInfo;
}
function reqUpdateContacts(loginVars)
{
	url = '../php/contacts.php';
	what = 'updateContacts(req.responseText)';
	DoCallback(loginVars, url, what);
}
function updateContacts(updateInfo)
{
	document.getElementById('contact').innerHTML = updateInfo;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Contacts Add Update and Delete buttons for Contacts Admin
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function addContact(id, action)
{
	firstName = document.getElementById('firstName').value;
	lastName = document.getElementById('lastName').value;
	maidenName = document.getElementById('maidenName').value;
	street = document.getElementById('street').value;
	city = document.getElementById('city').value;
	state = document.getElementById('state').value;
	zipCode = document.getElementById('zipCode').value;
	homePhone = document.getElementById('homePhone').value;
	cellPhone = document.getElementById('cellPhone').value;
	email = document.getElementById('email').value;
	info = ("action="+action+"&id="+id+"&FirstName="+firstName+"&LastName="+lastName+"&MaidenName="+maidenName+"&Street="+street+"&City="+city+"&State="+state+"&ZipCode="+zipCode+"&HomePhone="+homePhone+"&CellPhone="+cellPhone+"&Email="+email);
	
	if (action == 'Delete')
	{
		var answer = confirm('Are you sure you want to delete '+firstName+' '+lastName+'?');
		if(answer)
		{
			refreshForm(id, info);
		}
	} else if(action !== 'Delete')
	{
		refreshForm(id, info);
	}
	
}

function refreshForm(id, info)
{
	data = "&id="+id;
	what = "replaceInfo(req.responseText); refreshNames(data)";
	DoCallback(info, 'php/infoUpdate.php', what);
}

function refreshNames(data)
{
	what = "updateNames(req.responseText)";
	DoCallback(data, 'php/nameUpdate.php', what);

}

// Contacts Add Update and Delete buttons
function selectName(value)
{
	info = "id="+value;
	what = "replaceInfo(req.responseText)";
	DoCallback(info, 'php/infoUpdate.php', what);
}

function replaceInfo(updatedPage)
{
	document.getElementById('changeForm').innerHTML = updatedPage;
}

function updateNames(updatedNames)
{	
	document.getElementById('nameSelect').innerHTML = updatedNames;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Zenphoto Stuff
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function updatePage (stuff)  {
		url = "galleryUpdate.php";
		what = "updateZenphoto(req.responseText)";
		DoCallback(stuff, url, what);
	 }
	 function updateZenphoto(updateInfo) {
	 	document.getElementById('zenphoto').innerHTML = updateInfo;
	 }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Contact Info Admin Stuff
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function selectName(value)
{
	info = "id="+value;
	what = "replaceInfo(req.responseText)";
	DoCallback(info, 'php/infoUpdate.php', what);
}
