var url;
var request;
var domainName = "";

function setDomainName(temp)	{	domainName = temp;	}

function getURL()
{	url = "http://" + domainName + "/lib/functions.login.php?stuff=" + new Date().getTime() + "&site_id=" + site_id + "&client_id=" + client_id;	}

function createRequest()
{
	try
	{	request = new XMLHttpRequest();	}
	catch(trymicrosoft)
	{
		try
		{	request = new ActiveXObject("Msxm12.XMLHTTP");	}
		catch(othermicrosoft)
		{
			try
			{	request = new ActiveXObject("Microsoft.XMLHTTP");	}
			catch(fail)
			{	request = null;	}
		}
	}

	if(request == null)
	{	alert("Error creating XMLHttpRequest!");	}
}

/*------------------------Start: Login------------------------------*/
function processLogin()
{
	createRequest();
	getURL();
		
	var loginEmail = document.getElementById("login_email").value;
	if(loginEmail == "")
	{
	   document.getElementById("login_message").innerHTML = "<font color='#cc0000'><b>Please enter in an Email Address.</b></font>";
	   document.getElementById("login_email").focus();
	   highlight_error("login_email");
	   return false;
	}
	var loginURL = url + "&function=processLogin&email=" + loginEmail;
		
	request.open("GET", loginURL, true);
	request.onreadystatechange = updateLogin;
	request.send(null);
}

function updateLogin()
{
    if(request.readyState == 4)
	{
        var loginData = request.responseText;
        loginData = loginData.split("|");
        
        if(loginData[0] == 0)
        {
            //Error in looking up client. Check Email address or Register for an account.
            if(loginData[1] == "r")
            {   var messageDiv = "register_message";    }
            else if(loginData[1] == "l")
            {   var messageDiv = "login_message";   }
            
             document.getElementById(messageDiv).innerHTML = "<font color='#cc0000'><b>" + loginData[2] + "</b></font>";
        }
        else if(loginData[0] == 1)
        {
            //Login was Successful.
            if(from == "save")
            {
           	window.location="save_tour.php?site_id=" + site_id + "&tour_id=" + trip_id + "&type=" + trip_type;
            }
            else if(loginData[1] == "r" || loginData[1] == "l")
            {
                document.getElementById("content").innerHTML = loginData[2];
                client_id = loginData[3];
            }
            else
            {
                location.reload();
            }
        }
        
        
	}
}
/*------------------------End: Login------------------------------*/


/*------------------------Start: Register------------------------------*/
//All data back from the AJAX call can be processed by the same updateLogin function.
function processRegister()
{
	createRequest();
	getURL();
		
	var clientEmail = document.getElementById("client_email").value;
	if(clientEmail == "")
	{
	   document.getElementById("register_message").innerHTML = "<font color='#cc0000'><b>Please enter in an Email Address.</b></font>";
	   document.getElementById("client_email").focus();
	   highlight_error("client_email");
	   return false;
	}
		
    var clientName = document.getElementById("client_name").value;
    if(clientName == "") { clientName = "NIA"; }
    
	var clientCity = document.getElementById("client_city").value;
	if(clientCity == "") { clientCity = "NIA"; }
	
	var clientState = document.getElementById("client_state").value;
	if(clientState == "") { clientState = "NIA"; }
	
	var clientZip = document.getElementById("client_zip").value;
	if(clientZip == "") { clientZip = 0; }
	else if(isNaN(clientZip) == true)
	{
	   document.getElementById("register_message").innerHTML = "<font color='#cc0000'><b>Please use only Numbers for Zip Code.</b></font>";
	   document.getElementById("client_zip").focus();
	   highlight_error("client_zip");
	   return false;
	}
	
	var loginURL = url + "&function=processRegister&email=" + clientEmail + "&name=" + clientName;
	loginURL += "&city=" + clientCity + "&state=" + clientState + "&zip=" + clientZip;
		
	request.open("GET", loginURL, true);
	request.onreadystatechange = updateLogin;
	request.send(null);
}
/*------------------------End: Register ------------------------------*/

/*------------------------Start: Logout ------------------------------*/
function processLogout()
{
    createRequest();
	getURL();
	var loginURL = url + "&function=processLogout";
	
	request.open("GET", loginURL, true);
	request.onreadystatechange = updateLogin;
	request.send(null);
}
/*------------------------End: Logout ------------------------------*/

function highlight_error(divLocation)
{
	document.getElementById(divLocation).style.color = "yellow";
	document.getElementById(divLocation).style.fontWeight = "bold";
	document.getElementById(divLocation).style.backgroundColor = "#cc0000";
}

function remove_highlight_error(divLocation)
{
	document.getElementById(divLocation).style.color = "";
	document.getElementById(divLocation).style.fontWeight = "";
	document.getElementById(divLocation).style.backgroundColor = "";
}
