//*********************************************************************************************
// Function that logs user in and check to see if donate page has been filled out
//*********************************************************************************************
//Temporary setting of memberID
memberID = "0";
function subMemLogin()
{
    
    if(memberID == 0)
    {
// Call Validation routine
	    loginVal = logValidation();
	    if(loginVal)
	    {
	
// Add login information if validation is good	
// Call XML load routine and put data in document to send to backend	// Create XML doc
//  loadDocument is routine from General js to create XML doc
//  setXMLValue is routine from General js to insert info into the XML doc
//  sendDoc is routine from General js to send the document to backend
//  getXMLValue is routine from General js to get info from XML Doc
	        loadDocument();
	        setXmlValue('functionName', 'chkLogin');
	        setXmlValue('userName', document.getElementById("uName").value);
	        setXmlValue('pWord', document.getElementById("pWord").value);	        
	        //alert(xmlDoc.xml);
	        //alert(xmlDoc.xml);
	        xmlDoc = sendDoc();
	        //alert(xmlDoc.xml);
        // Check to if entered login/password is correct		
	        if(getXmlValue("message") == "Failure")
	        {
		        alert("Invalid username or password! Please try again, or see your administrator for assistance.");
	        }
// Keep memberID for possible future use proceed if login is good
	        if(getXmlValue("curMemberId") > "0")
	        {
		        memberID = getXmlValue("curMemberId");
		        //alert(memberID);
		        alert(getXmlValue("message"));
		        document.getElementById("logOut").style.display = "inline";
		        document.getElementById("updateReg").style.display = "block";
		        document.getElementById("pWord").value = "";		        
		        if(getXmlValue("curMemberId") == "1")
		        {
		            location='manager.htm';
		        }
	        }
	        if(getXmlValue("message") == "Server Error")
	        {
		        alert("Server error. Try again later or contact the site administrator.");
	        }
	    }
	}
	else
	{
	
	    alert("You are already logged in"); 
	} 
}
//*********************************************************************************************
// Login Validation of username and password
//*********************************************************************************************
function logValidation()
{
// Valdate user name
	if(document.getElementById("uName").value == "")
	{
	
		alert("Please Enter a Username");
		document.getElementById("uName").select();
		return false;	
	}
// Validate password
	if(document.getElementById("pWord").value == "" || document.getElementById("pWord").value.length < 4)
	{
		alert("Please Enter a Password at Least 4 Characters");	
		document.getElementById("pWord").select();
		return false;
	}
	return true;
}
//////////////////////////////////////
// Logoff 
///////////////////////////////////////
function logOff()
{
			loadDocument();
			setXmlValue('functionName', 'logOff');
			//alert(xmlDoc.xml);
			xmlDoc = sendDoc();
			//alert(xmlDoc.xml);
			alert(getXmlValue("message"));
		    document.getElementById("logOut").style.display = "none";
		    memberID = 0;
}
//////////////////////////////////////
// Handle forgotton password 
///////////////////////////////////////
function getPword()
{
//load XML doc
  loadDocument();
//  loadDocument is routine from General js to create XML doc
//  setXMLValue is routine from General js to insert info into the XML doc
//  sendDoc is routine from General js to send the document to backend
//  getXMLValue is routine from General js to get info from XML Doc
//Call function to validate user input  
  val = memForgotValidate();
//Continue if validation is good  
	if (val == true)
	{
		setXmlValue("functionName", "getPword");
		setXmlValue("message", "test Message");
		setXmlValue("userName", txtUName.value);
		setXmlValue("pWord", pWord.value);
		setXmlValue("eMail", txtEmail.value);
		//alert(xmlDoc.xml);
		xmlDoc = sendDoc();
		//alert(xmlDoc.xml);
		alert(getXmlValue("message"));
	}
}
//*********************************************************************************************
// Login Validation of username and password
//*********************************************************************************************
function memForgotValidate()
{
// Login must be at least 2 characters and alphabetic 
	sLoginString = "^([a-z0-9]((-|_)[a-z0-9])*){2,}\\s*$"; 
// email must be a with @ and another word with a . and two or three characters
	sEmailString = "^(\\w+(\\.\\w+)*){2,}@(\\w+(\\.\\w+)*){2,}\\.\\w{2,3}\\s*$";
// Password must be at least 6 characters
	sPassString = "^[A-Za-z0-9!@#$%^&*()_]{6,10}\\s*$";
// Valdate user name
	if (!validateField("txtUName", sLoginString) )
	{
		alert("Invalid UserName. You must enter a username.");
		document.getElementById("txtUName").select();
		return false;
	}
	if (!validateField("txtEmail", sEmailString) )
	{
		alert("Invalid Email You must enter a valid email");
		document.getElementById("txtEmail").select();
		return false;
	}
	if (!validateField("pWord", sPassString) )
	{
		alert("Invalid Password. You must enter a password.\nPassword must be 6 to 10 characters of letters and digits.");
		document.getElementById("pWord").select();
		return false;
	}
	if (!validateField("rePass", sPassString) ){
		alert("Invalid Password. You must enter a password.\nPassword must be 6 to 10 characters of letters and digits.");
		document.getElementById("rePass").select();
		return false;
	}
	firstPass = document.getElementById("pWord").value;
	secondPass = document.getElementById("rePass").value;
	if(firstPass != secondPass)
	{
		alert("The password you entered must match exactly the one you reentered. Please reenter your password!");
		document.getElementById("pWord").value = "";
		document.getElementById("rePass").value = "";
		document.getElementById("pWord").select();
		return false;
	}
	return true;
}