// Authentication Form and JS Handling for Secure Client Area and Content Management System
// Copyright 2009 Taupo Solutions Ltd
var logonControls = {
	
	display : function()
	// container function to call display of logon form
	{
		// track:
		pageTracker._trackPageview('/logonCalled');
		var winContent = "<h1>Taupo Solutions Secure Client Area</h1>";
		winContent += '<p class="introP">Please log in with the username and password you were supplied with:</p>';
		winContent += '<div id="logonMessage" style="display: none; -webkit-border-radius: 5px; -moz-border-radius: 5px"><img src="activitySquare.gif" alt="authenticating..." width="70" height="10">';
		winContent += '<p>Authenticating, please wait...</p></div>';
		
		winContent += '<div class="icon"></div>';
		winContent += '<form id="logonForm">';
		winContent += '<p><label title="Please enter your username." for="username">username: </label>';
					
		winContent += '<input title="Please enter your username." autocomplete="OFF" class="text" id="username"></p>';
		winContent += '<p><label title="Please enter your password." for="password">password: </label>';
					
		winContent += '<input type="password" autocomplete="OFF" class="text" id="password" value="" name="Password">';
		winContent += '<input type="button" value="logon" id="slogin" name="slogin" class="lButton" onclick="logonControls.validateLogon();">';
		winContent += '<input type="button" value="cancel" id="cancel" name="cancel" class="lButton" onclick="windowControls.hideWindow();"></p>';
		winContent += '</form>';
		winContent += '<div id="baseMessages">';
		winContent += '<p id="failMessage" style="display: none">Unable to verify your username or password &mdash;</p>';
		winContent += '<p>Unauthorised access is forbidden. If you have lost your username or password, or have not yet been given one, please contact Taupo for assistance on: 0118 9242 831.</p>';
		winContent += '</div>';
		
		
		
		windowControls.showWindow({modal: true,height:250,width:470,winContent: winContent});
		
		
	},
	
	validateLogon : function()
	// from onclick on login botton
	// send data to back end via ajax for authentication. Backend will redirect appropriatly or return fail...
	{
		// dim controls
		$('logonForm').fade({ 
        duration: 0.5, 
        to: 0.2 
      	});
		$('logonMessage').appear({afterFinish:function(){logonControls.activateLogon();}});
		pageTracker._trackPageview('/logonButtonClicked');
		
	},
	
	activateLogon : function()
	// ajax validate
	{
		var url="authorise.php";
		var saveStringCollect = "";
		
		saveStringCollect = "&uname=" + escape($('username').value);
		saveStringCollect += "&pword=" + escape($('password').value);
		
		new Ajax.Request(url, {
			  method: 'post',
			  postBody:  saveStringCollect,
			  
			  onSuccess: function(transport) {
				var responseArray = transport.responseText;
				if (responseArray == "FAIL")
				{
					//INVALID LOGON -- FAIL
					logonControls.logonCallbackFail();
				}
				else
				{
					//a  successful logon is handled by the back end php with a header redirect
					// no action necessary here
				}
				
			  }	
			});
		
	},
	
	logonCallbackFail : function()
	// if backend supplies fail, report to user
	{
		
		Effect.BlindDown('failMessage');
		$('failMessage').pulsate({duration: 5});
		$('username').value='';
		$('password').value='';
		$('logonForm').appear({ 
        delay: 1.5
      	});
		$('logonMessage').fade({delay: 1.5});

		
	}
	
	
	
	
	
}

var windowControls = {
	
	showWindow : function(args)
	// display modalty
	{
		var bodyWidth = document.viewport.getWidth();
		var bodyHeight = document.viewport.getHeight();
		var scrollOffset = document.viewport.getScrollOffsets();
		//scrollOffset = [0,0];
		args.posX = Math.round(bodyWidth/2 - (parseInt(args.width)/2)) + scrollOffset[0];
	
		// if window height is too low, reduce:
		
		args.posY = Math.round(bodyHeight/2 - (parseInt(args.height)/2)) + scrollOffset[1];
				
		$('modal').setOpacity(0.65);
			
			if ($('modal').getStyle('position') != "fixed"){
				$('modal').setStyle({height: bodyHeight + 'px'});
			}
		// position window:
		$('popupWindow').setStyle({left: args.posX + 'px',top: args.posY + 'px'});
	 	
	 	// style window:
		$('popupWindow').className = args.type;
		
		// size window:
	 	$('popupWindow').setStyle({width: args.width + 'px',height: args.height + 'px',display: 'none', visibility: 'visible'});
	 
		$('popupWindow').update(args.winContent);
		
		new Effect.Appear('popupWindow', 
   		{ 
      		duration: 0.4,
			afterFinish:function(element)
			{
				if(args.focusField){
					$(args.focusField).focus();
				}
			}
		});
		
		if (args.modal)
		// only allow drag if not modal
		{
			// if modal, display the background layer with opacity to 'lock' page...
			$('modal').style.visibility = "visible";
		}
		
		
	},
	
	hideWindow : function()
	// hide midality
	{
		$('modal').style.visibility = "hidden";
		
		new Effect.Fade('popupWindow', 
   		 { 
      		duration: 0.3,
      		afterFinish: function(){$('popupWindow').update('');}
    	 });
		
		
	}
	
	
	
}