	// Reference to signinForm
	var signinForm;
	
	// Page to be directed to upon successful login
	var landingPage;
	
	// Called when the page is loaded
	function init()
	{
		signinForm = document.signinForm;
		signinForm.username.focus();
		// Retrieve credentials from cookie if they're persisted
		retrieveCredentials();
	}

	
	
	// Authenticate user using Flash proxy
	function authenticateUser(username, password, callBack)
	{
		// Make sure that a username and password has been entered
		// TODO: Make stronger algorithm
		if(!validate(username, password))
		{
			document.getElementById("message").innerHTML = "Please specify a username and password";
			return;
		}
		
		// Handle persisting username/password
		persistCredentials(username, password);
		
		if(callBack == undefined)
			callBack = "callBack";
		
		// Flash proxy for Web Service
		var proxy = document.proxy;
		proxy.SetVariable("username", username);
		proxy.SetVariable("password", password);
		proxy.SetVariable("callBack", callBack);
		proxy.SetVariable("watch", 1);
	}
	// TODO: Create dummy form to handle submission instead of hardcoding signinForm
	function callBack(result)
	{
		// Error logging in
		if(isNaN(result))
		{
			document.getElementById("message").innerHTML = result;
		}
		// Successful login
		else
		{
			signinForm.sessionID.value = result;
			signinForm.action = (landingPage == undefined) ? "index.jsp" : landingPage + window.location.search;
			signinForm.username.value = "";
			signinForm.password.value = "";
			signinForm.submit();
		}
	}
	
	// Simple validation to check for the existence of username and password
	function validate(username, password)
	{
		if(username.length >= 1 && password.length >=1)
			return true;
		else
			return false;
	}
	
	/**
	 * XOR Encryption algorithm
	 * http://javascript.internet.com/passwords/xor-encryption4.html
	 */
	function encryptString(str, pwd)
	{
	
		if(pwd == null || pwd.length <= 0)
		{
		    return;
		}
		
		var prand = "";
			
		for(var i=0; i<pwd.length; i++)
		{
		    prand += pwd.charCodeAt(i).toString();
		}
			
		var sPos = Math.floor(prand.length / 5);
		var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
		var incr = Math.ceil(pwd.length / 2);
		var modu = Math.pow(2, 31) - 1;
		var salt = Math.round(Math.random() * 1000000000) % 100000000;
		  
		prand += salt;
		  
		while(prand.length > 10)
		{
		    prand = (Number(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
		}
			
		prand = (mult * prand + incr) % modu;
		var enc_chr = "";
		var enc_str = "";
		  
		for(var i=0; i<str.length; i++)
		{
		    enc_chr = Number(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
				
			if(enc_chr < 16)
			{
			    enc_str += "0" + enc_chr.toString(16);
			}
			else
			{
				enc_str += enc_chr.toString(16);
			}	
		    	prand = (mult * prand + incr) % modu;
		  	}
			
		  	salt = salt.toString(16);
		  
		  	while(salt.length < 8)salt = "0" + salt;
		  		enc_str += salt;
		  	
		  	return enc_str;
		}
	
	/**
	 * XOR String Decrypter
	 */	
	function decryptString(str, pwd)
	{
		var prand = "";
	  
		for(var i=0; i<pwd.length; i++)
		{
			prand += pwd.charCodeAt(i).toString();
	  	}
	  
	  	var sPos = Math.floor(prand.length / 5);
	  	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
	  	var incr = Math.round(pwd.length / 2);
	  	var modu = Math.pow(2, 31) - 1;
	  	var salt = parseInt(str.substring(str.length - 8, str.length), 16);
	  	str = str.substring(0, str.length - 8);
	  	prand += salt;
	  
	  	while(prand.length > 10)
		{
	    	prand = (Number(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
	  	}
	  
	  	prand = (mult * prand + incr) % modu;
		var enc_chr = "";
	  	var enc_str = "";
	  	
		for(var i=0; i<str.length; i+=2)
		{
	    	enc_chr = Number(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
	    	enc_str += String.fromCharCode(enc_chr);
	    	prand = (mult * prand + incr) % modu;
	  	}
	  
	  	return enc_str;
	}
	
	var key = "SensorLogic07082005";
	
	// Function to set all cookie parameters
	function setCookie(name, value, expires, path, domain, secure)
	{
		var curCookie = name + "=" + escape(value) + 
		((expires) ? "; expires=" + expires.toGMTString() : "") + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") + 
		((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
	
	// Function to retrieve the cookie
	function getCookie(name)
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		
		if (begin == -1)
		{
			begin = dc.indexOf(prefix);

			if (begin != 0) return null;
		}
		else
		    begin += 2;
		
			var end = document.cookie.indexOf(";", begin);

			if (end == -1)
			end = dc.length;
		
			return unescape(dc.substring(begin + prefix.length, end));
	}
	
	// Persist the username and password
	function persistCredentials(username, password)
	{
		var expires = new Date();
		// Cookie expires in 7 days
		expires.setTime(expires.getTime() + 7 * 24 * 60 * 60 * 1000);
		
		// Persist the username
		var username = encryptString(username, key);
		setCookie("username", username, expires);
		
		// If checkbox is selected then persist password
		if(signinForm.password_checkbox.checked)
		{
			var password = encryptString(password, key);
			setCookie("password", password, expires);
		}
		// Don't persist the password
		else
		{
			setCookie("password", "", expires);
		}
	}
	
	// Retrieve persisted credentials
	function retrieveCredentials()
	{
		var username = getCookie("username");
		var password = getCookie("password");
		
		// Populate the username field
		if(username != null)
			signinForm.username.value = decryptString(username, key);
		
		// If the password exists populate the field and check the box
		if(password != null && password != '')
		{
			signinForm.password.value = decryptString(password, key);
			signinForm.remember_password.checked = true;
		}
		
	}
	
	function getLocalTimezone()
	{
		var d = new Date();
		var timeZone = "";
	
		if(navigator.appVersion.indexOf("MSIE") > -1)
		{
			timeZone = d.toString().split(" ")[4];
		} else {
			timeZone = d.toString().split("(")[1];
			timeZone = timeZone.split(")")[0];
		}
		return timeZone;
	}
	
	function setTimeZone()
	{
		document.StandardApp.SetVariable("localTimezone", getLocalTimezone());
	}
	
	function setQueryString()
	{
		if(window.location.search.length>1)
			window.document.StandardApp.SetVariable("queryString", window.location.search);
	}
	
	
	
	