var xmlhttpMain


function Login(username, password){
if(!username || !password){
document.getElementById("error").innerHTML = "Please enter username and password";
    return;

}
swapView("loading");
xmlhttpMain=GetXmlHttpObject();
if (xmlhttpMain==null)
  {
  alert ("Your browser does not support AJAX! Please enable AJAX");
  return;
  }
var url="/PHP/main.php";
url=url+"?action=validateLogin";
url=url+"&username=";
url=url+username;
url=url+"&password=";
url=url+password;
xmlhttpMain.onreadystatechange=userDataChanged;
xmlhttpMain.open("GET",url,true);
xmlhttpMain.send(null);

}

function load(){
checkcookie();
}



function checkcookie(){
xmlhttpMain=GetXmlHttpObject();
if (xmlhttpMain==null)
  {
  alert ("Your browser does not support AJAX! Please enable AJAX");
  return;
  }
var url="/PHP/main.php";
url=url+"?action=checkcookie";
xmlhttpMain.onreadystatechange=userDataChanged;
xmlhttpMain.open("GET",url,true);
xmlhttpMain.send(null);
}



function userDataChanged()
{
	if (xmlhttpMain.overrideMimeType)
		xmlhttpMain.overrideMimeType('text/xml');
	
	if (xmlhttpMain.readyState==4)
	{
		var xmlDoc=xmlhttpMain.responseXML;
		var bla = xmlhttpMain;
		if (xmlDoc.getElementsByTagName("response")[0].getAttribute("type") == "success"){
			if(xmlDoc.getElementsByTagName("response")[0].getAttribute("subtype") == "login"){
				 document.getElementById("username").innerHTML =  xmlDoc.getElementsByTagName("response")[0].getAttribute("username");
			    	swapView("welcome");
			}
		}else if(xmlDoc.getElementsByTagName("response")[0].getAttribute("type") == "error"){
			swapView("login");
			if(xmlDoc.getElementsByTagName("response")[0].getAttribute("subtype") == "badCookie"){
					//there was no phone specified
					document.getElementById("error").innerHTML = "";
			}else if(xmlDoc.getElementsByTagName("response")[0].getAttribute("subtype") == "badCombo"){
				document.getElementById("error").innerHTML = "Invalid username/password<br>please try again";
			}
		}
	}
}

function swapView(view){
var loading = document.getElementById("loading");
var login = document.getElementById("login");
var welcome = document.getElementById("welcome");
switch (view){
    case "login":
         welcome.style.display = "none";
         loading.style.display = "none";
         login.style.display = "block";
         break;
    case "welcome":
        welcome.style.display = "block";
         loading.style.display = "none";
         login.style.display = "none";
         break;
    case "loading":
            welcome.style.display = "none";
         loading.style.display = "block";
         login.style.display = "none";
         break;
    }

}



function GetXmlHttpObject()
{
	var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
	 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
	  for (var i=0; i<activexmodes.length; i++){
	   try{
	    return new ActiveXObject(activexmodes[i])
	   }
	   catch(e){
	    //suppress error
	   }
	  }
	 }
	 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	  return new XMLHttpRequest()
	 else
	  return false
}