	
	
	/*---------------------------------------------------------------------------------------
		utheinvestigations.com - primary.js - 03-03-07
	---------------------------------------------------------------------------------------*/

	window.onload = function(){
	
		//Firefox
		if(navigator.userAgent.indexOf("Firefox") != -1){
			
			//Global Variables
			username = document.getElementById("username");
			password = document.getElementById("password");
			loginBtn = document.getElementById("loginBtn");
			
			input = [username, password];
		
			//Event Listeners
			loginBtn.addEventListener("click", loginBtnAction, false);
			username.addEventListener("focus", loginFieldAction, false);
			password.addEventListener("focus", loginFieldAction, false);
			username.addEventListener("keypress", loginFieldAction, false);
			password.addEventListener("keypress", loginFieldAction, false);
			
			//Initialize
			username.value = "";
			password.value = "";
			username.focus();
		
		}
		
		//CSS Fix
		if(navigator.userAgent.indexOf("Opera") != -1){
			
			//Opera 7 - 8.5 and Netscape 7 CSS Bug Fix
			var array = new Array();
				array[0] = ["Opera/7", 15];
				array[1] = ["Opera 7", 18];
				array[2] = ["Opera 8", 20];
				array[3] = ["Netscape/7", 20];
			
			for(var i=0; i<array.length; i++){
				
				if(navigator.userAgent.indexOf(array[i][0]) != -1){
					
					document.getElementById("username").style.height = array[i][1] + "px";
					document.getElementById("password").style.height = array[i][1] + "px";	
					
				}
				
			}
			
		}
		
		//Event Handlers
		document.getElementById("printBtn").onclick = function(){
		
			parent.frames["printFrame"].printMe();
		
		};
		
		document.getElementById("mail").onclick = function(){
			
			if(navigator.userAgent.indexOf("Opera") != -1){
				
				alert("We're sorry, this feature is not available in Opera.  Try using Firefox instead!");	
				
			}else{
				
				username.value = "";
				password.value = "";
				username.style.borderColor = "#f1f0ed";
				password.style.borderColor = "#f1f0ed";
				document.getElementById("email-parent").style.display = "block";
				document.getElementById("email-bg").style.display = "block";
				document.getElementById("send-to").value = "info@utheinvestigations.com";
				document.getElementById("subject").focus();
				
				var pageHeight = document.getElementById("parent").offsetHeight;
				document.getElementById("disable-bg").style.display = "block";
				document.getElementById("disable-bg").style.height = pageHeight + "px";
				
			}
			
		};
		
		document.getElementById("close-btn").onclick = function(){
			
			document.getElementById("disable-bg").style.display = "none";
			
			document.getElementById("email-parent").style.display = "none";
			document.getElementById("email-bg").style.display = "none";
			document.getElementById("subject").value = "";
			document.getElementById("message").value = "";
			username.style.color = "#000";
			password.style.color = "#000";
			username.focus();
			
		};
		
		document.getElementById("send-to").onfocus = emailFieldEvent;
		document.getElementById("subject").onfocus = emailFieldEvent;
		document.getElementById("message").onfocus = emailFieldEvent;
		
		document.getElementById("send-to").onblur = emailFieldEvent;
		document.getElementById("subject").onblur = emailFieldEvent;
		document.getElementById("message").onblur = emailFieldEvent;
		
		document.getElementById("send-btn").onclick = sendBtnEvent;
			
	}
		
	/*---------------------------------------------------------------------------------------
		loginBtnAction*/
		
	function loginBtnAction(){
	
	
		if( !validate() ){
			
			invalid();
			
		}else{
		
			request( encodeQueryString() );
		
		}
			
	}
	
	/*---------------------------------------------------------------------------------------
		encodeQueryString*/
		
	function encodeQueryString(){
	
		var headerString = "";
				
		headerString += username.getAttribute("id") + "=" + encodeURIComponent(username.value);
		headerString += "&";
		headerString += password.getAttribute("id") + "=" + encodeURIComponent(password.value);
		
		return headerString;
	
	}
	
	/*---------------------------------------------------------------------------------------
		validate*/
		
	function validate(){
				
		for(var i=0; i<input.length; i++){
		
			if(input[i].value == "" || input[i].value == "INVALID ENTRY"){
			
				return false;
			
			}
		
		}
		return true;
	
	}
	
	/*---------------------------------------------------------------------------------------
		invalid*/
		
	function invalid(){
		
		for(var i=0; i<input.length; i++){
		
			if(input[i].value == "" || input[i].value == "INVALID ENTRY"){
			
				input[i].style.borderColor = "#cd0b0b";
				input[i].style.color = "#cd0b0b";
				input[i].value = "INVALID ENTRY";
				
				if(input[i].type == "password"){
				
					input[i].type = "text";
				
				}
			
			}
		
		}
	
	}
	
	/*---------------------------------------------------------------------------------------
		loginFieldAction*/
		
	function loginFieldAction(){
	
		if(arguments[0].type == "focus"){
		
			loginFieldFocus(this);
			
		}else if(arguments[0].type == "keypress"){
			
			if(arguments[0].keyCode == 13){
			
				loginBtnAction();	
				
			}else if(this.value == "INVALID ENTRY"){
				
				loginFieldFocus(this);
				
			}
			
		}
	
	}
	
	/*---------------------------------------------------------------------------------------
		loginFieldFocus*/
		
	function loginFieldFocus(){
		
		for(var i=0; i<input.length; i++){
		
			if(input[i].value == "INVALID ENTRY"){
			
				input[i].value = "";
				input[i].style.borderColor = "#f1f0ed";
				input[i].style.color = "#000000";
				
			}
		
		}
		if(input[1].type == "text"){
				
			input[1].type = "password";
				
		}
				
	}
		
	/*---------------------------------------------------------------------------------------
		request*/

	function request(){
		
		var request = new XMLHttpRequest();
		
		request.open("get", "request.php?c=admin&f=login&" + arguments[0], true);
		
		request.onreadystatechange = function(){
	
			if(request.readyState == 4){
				
				response(request.responseXML);
				
			}
	
		}
		request.send(null); 
		
	}
	
	/*---------------------------------------------------------------------------------------
		response*/
		
	function response(){
		
		switch(arguments[0].getElementsByTagName("id")[0].firstChild.nodeValue){
		
			case "1":
		
				username.style.borderColor = "#cd0b0b";
				username.style.color = "#cd0b0b";
				username.value = "INVALID ENTRY";
				break;
				
			case "2":
		
				if(password.type == "password"){
				
					password.type = "text";
				
				}
				password.style.borderColor = "#cd0b0b";
				password.style.color = "#cd0b0b";
				password.value = "INVALID ENTRY";
				break;
			
			case "3":
			
				location.href = arguments[0].getElementsByTagName("url")[0].firstChild.nodeValue;
								
		}
		
	}
	
	