var objTemp;

function addPerm(){
	// create the url
	var strURL = "user.php?action=addPermission";
	
	// get the form
	var objForm = document.getElementById("addPermForm");
	
	if(objForm == null){
		alert("Unable to find the form.");
	}
	
	// append the values
	strURL += "&permName=" + objForm.perm.value;
	strURL += "&permDetails=" + objForm.permDetails.value;
	
	// get an xmlhttp object
	xmlHttp = GetXmlHttpObject();
	
	// change the onstatechange response
	xmlHttp.onreadystatechange = addPermStateChange;
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
}

function addPermStateChange(){
	if(xmlHttp.readyState==4){
		alert(xmlHttp.responseText);
	}
}

function addUser(){
	if(xmlHttp.readyState==4){
		alert(xmlHttp.responseText);
	}	
}

function checkUserForm(){
	// get the form
	var objForm = document.getElementById("userform");
	
	var bSuccess = true;
	var strErrors = "";
	
	// check the login id
	if(objForm.loginid.value.length == 0){
		strErrors += "A User Name is required.\n";
	}
	
	// check the real name
	if(objForm.realName.value.length == 0){
		strErrors += "The real name is required.\n";
	}

	// check for any errors
	if(strErrors.length > 0){
		bSuccess = false;
		alert(strErrors);
		return false;
	}
	
	// use Ajax to create the account and report the results
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure an object was returned
	if(xmlHttp == null){
		alert("Error creating Ajax interface");
		return false;
	}
	
	// set the response handler
	xmlHttp.onreadystatechange=addUser;
	
	// build the query
	var strURL = "user.php?action=addUser&loginid=" + objForm.loginid.value;
	strURL += "&password=" + objForm.password.value;
	strURL += "&realname=" + objForm.realName.value;
	strURL += "&type=" + objForm.userType.value;
	
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
	return false;
}

function deleteUser(objRow, strUsername){
	// check to make sure the user should be deleted
	if(! confirm("Are you sure you want to delete the user " + strUsername)){
		return false;
	}
	
	// get an XmlHttp object
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure an object was returned
	if(xmlHttp == null){
		return true;
	}
	
	objTemp = objRow;
	
	// create the query
	var strURL = "user.php?action=deleteUser&username=" + strUsername;
	
	// set up the xmlHttp object and run it
	xmlHttp.onreadystatechange = deleteUserStateChange;
	xmlHttp.open("GET",strURL,true);
	xmlHttp.send(null);
	
	return false;
}

function deleteUserStateChange(){
	if(xmlHttp.readyState==4){
		document.getElementById('editUser').deleteRow(objTemp.parentNode.parentNode.rowIndex);
	}
}

function disableUser(strUsername){
	// get an XmlHttp Object
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure an object was returned
	if(xmlHttp == null){
		alert("AJAX not supported.");
		return true;
	}

	// set the ready state change callback
	xmlHttp.onreadystatechange = disableUserStateChange;
	
	// create the query
	var strURL = "user.php?action=disableUser&username=" + strUsername;
	
	// get the object to update 
	objTemp = document.getElementById("enable"+strUsername);
	
	// perform the query
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
	return false;
}

function disableUserStateChange(){
	// check to see if the right state was sent
	if(xmlHttp.readyState == 4){
		objTemp.innerHTML = xmlHttp.responseText;
	}
}

function grantAccess(strElement, intType, intPermID){
	// get an xmlHttp object
	xmlHttp = GetXmlHttpObject();
	
	objTemp = document.getElementById(strElement);
	
	// check to make sure an object was returned
	if(xmlHttp==null){
		objTemp.innerHTML = "AJAX not supported";
		return false;
	}
	
	// change the state change handler
	xmlHttp.onreadystatechange = accessChangeStateChange;
	
	// create the url
	var strURL = "user.php?action=grantAccess&type=" + intType + "&perm=" + intPermID;
	
	// send the query
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
	return false;
}

function removeAccess(strElement, intType, intPermID){
	// get an xmlHttp object
	xmlHttp = GetXmlHttpObject();
	
	objTemp = document.getElementById(strElement);
	
	// check to make sure an object was returned
	if(xmlHttp==null){
		objTemp.innerHTML = "AJAX not supported";
		return false;
	}
	
	// change the state change handler
	xmlHttp.onreadystatechange = accessChangeStateChange;
	
	// create the url
	var strURL = "user.php?action=removeAccess&type=" + intType + "&perm=" + intPermID;
	
	// send the query
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
	return false;
}

function accessChangeStateChange(){
	if(xmlHttp.readyState == 4){
		objTemp.innerHTML = xmlHttp.responseText;
	}
}

function enableUser(strUsername){
	// get an XmlHttp Object
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure an object was returned
	if(xmlHttp == null){
		alert("AJAX not supported.");
		return true;
	}

	// set the ready state change callback
	xmlHttp.onreadystatechange = disableUserStateChange;
	
	// create the query
	var strURL = "user.php?action=enableUser&username=" + strUsername;
	
	// get the object to update 
	objTemp = document.getElementById("enable"+strUsername);
	
	// perform the query
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
	
	return false;
}

function enableUserStateChange(){
	// check to see if the right state was sent
	if(xmlHttp.readyState == 4){
		objTemp.innerHTML = xmlHttp.responseText;
	}
}

function selectOption(strValue){
	// get a xmlHttp object
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure an object was returned
	if(xmlHttp == null){
		document.getElementById("userOptionDisplay").innerHTML="AJAX not supported.";
		return;
	}
		
	// update the interface so the user knows what is happening
	document.getElementById("userOptionDisplay").innerHTML="Loading..";
	
	// set the response handler
	xmlHttp.onreadystatechange=selectOptionStateChange;
	
	// build the query
	var strURL = "user.php?action=" + strValue;
	
	// perform the query
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
}

function selectOptionStateChange(){
	if(xmlHttp.readyState==4){
		document.getElementById("userOptionDisplay").innerHTML=xmlHttp.responseText;
	}	
}

function selectType(intID){
	// create an xmlHttp object
	xmlHttp = GetXmlHttpObject();
	
	// check to make sure the xmlhttp object was initialized
	if(xmlHttp == null){
		document.getElementById("displayTypesDisplay").innerHTML="AJAX not supported.";
		return;
	}
	
	// set the handler
	xmlHttp.onreadystatechange=selectTypeStateChange;
	
	var strURL = "user.php?action=displayType&typeID=" + intID;
	
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);	
}

function selectTypeStateChange(){
	if(xmlHttp.readyState==4){
		document.getElementById("displayTypesDisplay").innerHTML=xmlHttp.responseText;
	}
}