var up = 1;
var original_height = new Array();

var xmlHttp_Login = false;

var dropdown = new Array();
var original_html = "";

function Init(){
	original_height[0] = document.getElementById("sign_in_dropdown").offsetHeight;
	document.getElementById("sign_in_dropdown").style.height = "0px";
	document.getElementById("sign_in_dropdown").style.display = "none";
	original_height[1] = "372px";
	original_height[2] = 0;
}

if(document.images){
	//preload two gosquared images
	var gosquaredhover = new Image(63,25);
	gosquaredhover.src = "images/gosquaredhover.png";
	var gosquared = new Image(63,25); 
	gosquared.src = "images/gosquared.png";
}

function showMainMenu(){
	if(document.images){
		document.getElementById("goSquaredImage").src = gosquaredhover.src;
		clearTimeout(dropdown[1]);
		show("main_dropdown",1);
	}
}

function hideMainMenu(){
	if(document.images){
		document.getElementById("goSquaredImage").src = gosquared.src;
		clearTimeout(dropdown[1]);
		hide("main_dropdown",1)
	}
}

function drop(id,heightid){
	if(document.getElementById){
		clearTimeout(dropdown[heightid]);
		if(up==1){
			up = 0;
			document.getElementById(id).style.display = "block";
			show(id,heightid);	
		}else{
			up = 1;
			hide(id,heightid);
		}
	}
}

function show(id,heightid){
	if(document.getElementById){
		var object = document.getElementById(id);
		var height = parseInt(object.offsetHeight);
		var orig_height = parseInt(original_height[heightid]);
		
		if(height<orig_height/10*9){
			height = height+orig_height/10;
			object.style.height = height+"px";
			dropdown[heightid] = setTimeout("show('"+id+"','"+heightid+"');",10);
		}else{
			object.style.height = orig_height+"px";
		}
	}
}

function hide(id,heightid){
	if(document.getElementById){
		var object = document.getElementById(id);
		var height = parseInt(object.offsetHeight);
		var orig_height = parseInt(original_height[heightid]);
				
		if(height>orig_height/10){
			dropdown[heightid] = setTimeout("hide('"+id+"','"+heightid+"');",10);
			height = height-orig_height/10;
			object.style.height = height+"px";
		}else{
			object.style.height = "0px";
			document.getElementById(id).style.display = "none";
		}
	}
}

function submitLogin(){
	if(document.getElementById){
	
		document.getElementById("login_errormessage").innerHTML = "";
		document.getElementById("username_error_image").style.display = "none";
		document.getElementById("password_error_image").style.display = "none";
		
		var go=1;
		
		var userObject = document.getElementById("username");
		if(userObject.value!=''){
			username = userObject.value;
		}else{
			loginError(0);
			go = 0;
		}
		
		var passObject = document.getElementById("password");
		if(passObject.value!=''){
			password = passObject.value;
		}else{
			loginError(2);
			go = 0;
		}
		
		if(go==1){
		
			//create login ajax object
			if(window.XMLHttpRequest){
				try{
					var xmlHttp_Login = new XMLHttpRequest();
					if(xmlHttp_Login.overrideMimeType){
						xmlHttp_Login.overrideMimeType('text/XML');
					}
				}
				catch(e){
				}
			}else if(window.ActiveXObject){
				try{
					xmlHttp_Login = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e){
					try{
						xmlHttp_Login = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
					}
				}
			}
			
			if(!xmlHttp_Login){
				alert("ARRGH! Browser Error! Please try visiting our non-javascript version of the site.");
				return false;
			}

			//open connection
			xmlHttp_Login.open("POST","login.php",true);
			//set return function
			xmlHttp_Login.onreadystatechange = function() {
				stateChanged_Login(xmlHttp_Login);
			};
			//set headers
			xmlHttp_Login.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//send form
			xmlHttp_Login.send("username="+escape(username)+"&password="+escape(password));
			
		}
	}
}


function stateChanged_Login(XMLHttpLogin){
	if(XMLHttpLogin.readyState == 0){
	}
	if(XMLHttpLogin.readyState > 0 && XMLHttpLogin.readyState < 4){
		original_html = document.getElementById("sign_in_dropdown").innerHTML
		document.getElementById("sign_in_dropdown").innerHTML = "<di"+"v class=\"margin\"><obje"+"ct classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"20px\" height=\"20px\"><par"+"am name=\"allowScriptAccess\" value=\"sameDomain\" /><par"+"am name=\"movie\" value=\"http://www.gosquared.com/images/loading.swf\" /><par"+"am name=\"quality\" value=\"high\" /><e"+"mbed src=\"http://www.gosquared.com/images/loading.swf\" quality=\"high\" width=\"20px\" height=\"20px\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></ob"+"ject><sp"+"an>Logging In...</s"+"pan></d"+"iv>";
		document.getElementById("sign_in_dropdown").style.height = "auto";
		original_height = document.getElementById("sign_in_dropdown").offsetHeight;
		document.getElementById("sign_in_dropdown").style.height = original_height+"px";
	}
	if(XMLHttpLogin.readyState == 4){
		if(XMLHttpLogin.status == 200){
			parseXML_Login(XMLHttpLogin.responseXML);
		}else{
			alert("Login Error. Sorry but I've given up.");
		}
	}
}

function parseXML_Login(loginXML){
	try{
		var error = loginXML.getElementsByTagName("error");
		alert(error[0].firstChild.nodeValue);
	}catch(e){
		var login = loginXML.getElementsByTagName("test");
		alert(login[0].firstChild.nodeValue);
	}
}

function loginError(code){
	if(document.getElementById){
		switch(code){
			case 0:
				document.getElementById("username_error_image").style.display = "inline";
				document.getElementById("login_errormessage").innerHTML += "No Username Submitted.<b"+"r />";
				break;
			case 1:
				document.getElementById("username_error_image").style.display = "inline";
				document.getElementById("login_errormessage").innerHTML += "Unrecognised Username.<b"+"r />";
				break;
			case 2:
				document.getElementById("password_error_image").style.display = "inline";
				document.getElementById("login_errormessage").innerHTML += "No Password Submitted.<b"+"r />";
				break;
			case 3:
				document.getElementById("password_error_image").style.display = "inline";
				document.getElementById("login_errormessage").innerHTML += "Incorrect Password.<b"+"r />";
				break;
		}
		document.getElementById("sign_in_dropdown").style.height = "auto";
		original_height(0) = document.getElementById("sign_in_dropdown").offsetHeight;
		document.getElementById("sign_in_dropdown").style.height = original_height[0]+"px";
	}

}
