// Author: Al Sierra
// Version: 6.30.2008
// Description: AJAX for login screen
var login = {
	_dataURL: 'scripts/php/client.php',
	_docRef: false,
	_divShutter: false,
	_divWindow: false,
	_cmd: 'auth',
	_id: false,
	_shutter_h:false,
	_shutter_w:false,
	
	Init: function(inDocumentRef,loc)
	{
		this._docRef = inDocumentRef;
		this._dataURL = loc+this._dataURL;
		// find our shutter DIV
		this._divShutter = $('shutter_l');
		if(!this._divShutter) {
			// shutter not found, create
			this._divShutter = this._docRef.createElement('div');
			this._divShutter.id = 'shutter_l';
			this._divShutter.style.display = 'none';
			this._divShutter.style.background = '#000000';
			this._divShutter.style.top = '0px';
			this._divShutter.style.left = '0px';
			this._divShutter.style.position = 'absolute';
			login._getPageDimensions();
			this._divShutter.style.height = login._shutter_h+"px";
			this._divShutter.style.width = login._shutter_w+"px";
			//this._divShutter.style.height = '100%';
			//this._divShutter.style.width = '100%';
			this._docRef.body.appendChild(this._divShutter);
		}
		// find our window DIV
		this._divWindow = $('window_l');
		if(!this._divWindow) {
			// popup DIV not found, create
			this._divWindow = this._docRef.createElement('div');
			this._divWindow.id = 'window_l';
			this._divWindow.style.display = 'none';
			this._docRef.body.appendChild(this._divWindow);
			// load the dispay HTML if not already loaded
			new Ajax.Updater('window_l', loc+'scripts/php/get_screen.php?s=login', {evalScripts: true});
		}
	},
	DisplayLogin: function() 
	{	
	 	$('username').value="";
	 	$('password').value="";
	 	$('email').value="";
		this._divWindow.show();
		new Effect.Appear(login._divWindow,{duration: .1, to:1.0 });
		this._divWindow.style.zIndex = 9000;		
		this._divShutter.style.zIndex = 8000;
		new Effect.Appear(login._divShutter,{duration: 0, from: 0.0, to:0.45 });
		$('username').focus();
	},
	Submit: function() 
	{
		// make sure user selected a folder
		var emsg='';
		if (!this._ValidateString($('username').value) || !this._ValidateString($('password').value)) {
			emsg+="Invalid values were inserted.\n";
		}
		if (emsg!='') {
			alert(emsg);
			return false;
		}
		var msg_data = {};
		msg_data.user = $('username').value;
		msg_data.pass = $('password').value;
		// fire an AJAX update
		var ajaxtags = new Ajax.Request(this._dataURL+'?cmd='+this._cmd,{
			parameters: $H(msg_data).toQueryString(),
			asynchronous: false
		});
		if (ajaxtags.transport.statusText != "OK") {
			alert("A problem was encountered when trying to contact the server.\nPlease try again.");
			return false;
		}
		if (ajaxtags.transport.responseText.length > 0 ) {
			try {
				var JSON = eval("("+ajaxtags.transport.responseText+")");
				if (undefined!==JSON.ehandler) {
					//alert('The following error has occured:\n'+JSON.emsg);
					new Effect.Shake(login._divWindow, {fps:12, delay:1});
					$('password').value="";
					return false;
				}
			} catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
 		}
		$('username').value="";
	 	$('password').value="";
		//this._GoTo('/scripts/php/login_redirect.php');
		
		//alert('success');
		//reload current page
		location.reload(true);

		new Effect.DropOut(login._divWindow, {queue: 'end'}); 
		new Effect.Parallel([ 
			new Effect.Fade(login._divWindow), 
			new Effect.Fade(login._divShutter)
		], {duration:0.8,queue: 'end',afterFinish: window.open('/scripts/php/login_redirect.php','')});
	},
	
	//_GoTo: function(url) {
		//this._url = url;
		//window.location.href=this._url;
	//}
	
	SubmitForget: function() 
	{
		// make sure user selected a folder
		var emsg='';
		if (!this._ValidateString($('email').value)) {
			emsg+="Invalid value was inserted.\n";
		}
		if (emsg!='') {
			alert(emsg);
			return false;
		}
		var msg_data = {};
		msg_data.email = $('email').value;
		// fire an AJAX update
		var ajaxtags = new Ajax.Request(this._dataURL+'?cmd=reset',{
			parameters: $H(msg_data).toQueryString(),
			asynchronous: false
		});
		if (ajaxtags.transport.statusText != "OK") {
			alert("A problem was encountered when trying to contact the server.\nPlease try again.");
			return false;
		}
		if (ajaxtags.transport.responseText.length > 0 ) {
			try {
				var JSON = eval("("+ajaxtags.transport.responseText+")");
				if (undefined!==JSON.ehandler) {
					$('retrieve_error').style.display = 'block';
					$('email').value="";
					return false;
				}
			} catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
 		}
		$('email').value="";
		$('retrieve_success').style.display = 'block';
	},
	ForgotUsePass: function() 
	{
		$("login_box").style.display = 'none';
		$("login_retrieve").style.display = 'block';
	 
	},
	Cancel: function() 
	{
		new Effect.Appear(login._divWindow,{duration: .5, to:0.0 });
		new Effect.Appear(login._divShutter,{duration: 0, to:0.0, afterFinish: login._CancelHide })
	},
	_CancelHide: function() 
	{
		login._divWindow.hide();
		login._divShutter.hide();
		//reset form back to orginal state
		$("login_box").style.display = 'block';
		$("login_retrieve").style.display = 'none';
		$('retrieve_success').style.display = 'none';
		$('retrieve_error').style.display = 'none';
	},
	_getPageDimensions : function() 
	{
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		if(yScroll < windowHeight){
			login._shutter_h = windowHeight;
		} else { 
			login._shutter_h = yScroll;
		}

		if(xScroll < windowWidth){	
			login._shutter_w = windowWidth;
		} else {
			login._shutter_w = xScroll;
		}
	},
	_ValidateString: function (s){
        if(s==undefined || s==null || s.length==0) {
            return false;
        }
        
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(s)) {
            return false;
        }
     
        //Check for html tags
        reHtmlTags = new RegExp(/<(.|\n)/);
        if (reHtmlTags.test(s)) {
            return false;
        }
        return true;
    }
}