/*	@Copyright (c) 2002 by Deecal International, Ltd. All Rights Reserved.*/

var childwin;

/* Takes the user to the first step in the login */
function LoginStepOne()
{
	parent.document.iframe1.location = '/dcal/AuthenticationServlet?action=loginstep1';
}

/* Takes the user to their Home Page */
function home()
{
	winlocation = '/dcal/HomePageServlet?action=HomePage';
	
	//Cross Browser Support
	//At this point, the popup window named 'newWin' already exists.
	//The window.open call doesn't open a new window, it just loads a page into the existing window.
	//However, reloading the page in this manner causes the window.opener property to get set to null. (FF, Safari & Chrome, not IE)
	//Instead, we should reload the page by changing the window.location property.
	//childwin = window.open(winlocation,'newWin','toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,height=712, width=1015,left=0,top=0');
	parent.location = winlocation;
	childwin = parent;
}
	
/*  This checks that the user has entered a username */
function Check()
{
	if(document.Iframe1Form.Login.value == '')
	{
		document.Iframe1Form.displayMessage.value = parent.Login1;
				displayMessage(parent.LoginHTML1);
		document.Iframe1Form.Login.focus();
		return false;
	}
	else
		return true;
}
	
	
/* This checks that the user has entered extra information */
function CheckExtra(option, labelKey)
{
	// 1:MotherMaidenName 2:PhoneNumberLast4Digits 3:DefaultCardLast4Digits 
	if(document.Iframe1Form.ExtraInfo.value == ''){
		popupOk("SecurityQuestionError_" + labelKey);
		return false;
	}
	
	if(option == 2){
		if(document.Iframe1Form.ExtraInfo.value.length != 4){
			document.Iframe1Form.displayMessage.value = Iframe1Form.phoneErrorMsg.value;
			displayMessage(parent.LoginHTML1);
			document.Iframe1Form.ExtraInfo.focus();
			return false;
		}     
	}
	else if(option == 3){
		if(document.Iframe1Form.ExtraInfo.value.length != 4){
			 document.Iframe1Form.displayMessage.value = parent.Login4;
			 displayMessage(parent.LoginHTML1);
			 document.Iframe1Form.ExtraInfo.focus();
			 return false;
		}     
	}
	return true;
}
	
	
/* Function to Submit the form and Launch new window */
//	function submitForm(extraInfo)
//	{
	    //URL = '/dcal/AuthenticationServlet';
//	    action = document.Iframe1Form.action.value;

//	    winlocation = URL + "?action=" + action + "&ExtraInfo=" + extraInfo;
//	    window.open(winlocation,'newWin','toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,height=712, width=1015,left=0,top=0');
	    //parent.close();
//	}
	
	
/* Function to validate the passwords */
function validatePwd(option) 
{
	var invalid = " "; // Invalid character is a space
	var pw1 = "";
	var pw2 = "";
	var minLength = 8; // Minimum length
	pw1 = document.Iframe1Form.Password.value;
	// The option determines if both passwords should be validated. 
	// If option is 1, then both must be validated
	if(option == 1)
	{
		//Ensure new password is a valid one
		if(!verifyPassword(document.Iframe1Form.Password, document.Iframe1Form.ConfirmedPassword,true,true)){
			document.Iframe1Form.Password.focus();
			return false;            	
		}
		else            
		 return true;
	} 
	else // Otherwise just validate the password
	{            
		// check for a value in the password field.
		if (pw1 == '') {
			document.Iframe1Form.displayMessage.value = parent.Login10;
			displayMessage(parent.LoginHTML1);            	
			document.Iframe1Form.Password.focus();
			return false;
		}
		else 
			return true;
			//submitPwdForm(target, pw1, pw2);
	}
}

/*Function to ensure password is not all numbers*/
function NotAllNumbers(PWord) {
	valid='0123456789';
	len=parseInt(PWord.length);
	var temp;
	var temp2 = '';
	var AllNumbers=1; //Flag (0):All numeric (1):Not all numeric
	for (i = 0;  i < len;  i++) {
		temp = PWord.charAt(i);
		if (valid.indexOf(temp)==-1) { //A Character in the Password is not numeric
			AllNumbers = 1;
			return AllNumbers;}
		else
			AllNumbers = 0;
	}	
	return AllNumbers;
}


/* Function to Submit the form and Launch new window */
//	function submitPwdForm(target, pw1, pw2)
//	{
	    //URL = '/dcal/AuthenticationServlet';
//	    action = document.Iframe1Form.action.value;

//        if(target == 'newWin')
//        {
//	        winlocation = URL + "?action=" + action + "&Password=" + pw1 + "&ConfirmedPassword=" + pw2;	    
//	        window.open(winlocation,'newWin','toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,height=712, width=1015,left=0,top=0');
	        //parent.close();
//	    }
//	    else
//	    {
//	        location = URL + "?action=" + action + "&Password=" + pw1 + "&ConfirmedPassword=" + pw2;
//	    }
//	}
	
	
/* Reset both password fields */
function passwordReset()
{
	document.Iframe1Form.Password.value = "";
	document.Iframe1Form.ConfirmedPassword.value = "";        
}
    
/* Change language and resubmit */
function ChangeLang(list, css) 
{  
	var indexUrl = '/dcal/LandingServlet';
	var lang = list.options[list.selectedIndex].value; 

	if (lang == "None") 
	{
		return;
	}
	
	//parent.location=indexUrl+"?language="+lang + "&custom="+css;            
	parent.location = indexUrl
		+ '?action='         + 'landingpage'
		+ '&languagechange=' + 'true'
		+ '&language='       + lang
		+ '&custom='         + css;
}

/* Set the focus for the login functions */
function setfocus(aForm)
{
	if(aForm.elements != null && aForm.elements[0]!=null) 
	{
		var i;
		var max = aForm.length;
		
		for( i = 0; i < max; i++ ) 
		{
			if(aForm.elements[i].type != "hidden" && aForm.elements[i].type != "image" 
				&& aForm.elements[i].type != "select-one" && aForm.elements[i].type != "select"
				&& !aForm.elements[i].disabled && !aForm.elements[i].readOnly) 
			{
				aForm.elements[i].focus();
				aForm.elements[i].select();
				break;
			}
		}
	}
}   

/* Set the CSS as per css suplied in url */
function setCSS()
{
	var paramArray = new Array();
	var parameters = "";
	var paramname = "";
	var paramvalue = "";
	var param = "";
	var start = 0;
	var count = 0;
	var end = 0;
	
	urlquery = document.URL;
	index = urlquery.indexOf("?");

	if (index > -1) 
	{
		parameters = urlquery.substring((index+1),urlquery.length);
		for(i=0; i<parameters.length; i++)
		{	    
			charAt = parameters.substring(i, (i+1));
			if(charAt == "&")
			{
				var end = i;
				paramArray[count] = parameters.substring(start, end);
				var start = i+1;
				count++;
			}
		}
		paramArray[count] = parameters.substring(start, parameters.length);
		for(x=0; x<paramArray.length; x++)
		{
			param = paramArray[x];
			paramindex = param.indexOf("=")
			if (paramindex > -1) 
			{
				paramname = param.substring(0,paramindex);
				if(paramname.toUpperCase() == "CUSTOM")
				{
					paramvalue = param.substring((paramindex+1), param.length);
				}
			}
		}
	}
	document.writeln("<link rel='STYLESHEET' type='text/css' href='/Resources/css/common" + paramvalue + ".css'>");
}
    

/* Confirm re login from the login page */
function confirmReLogIn() {
	childwin.close();
	parent.location.reload();
	parent.window.focus();
	return true; 	
}

//Confirm re login
function closeLogIn() {
	parent.window.close();
	return true;
}

/* Confirm exit from the login page */
function confirmClose() {
	var agree=confirm(logout);
	if (agree) {
		childwin.close();
		parent.window.close();
		return true; 
	} 
	else {
		return false; 
	}
}
 /* Added so we don't need to include common.js
Prevent the user opening a new browser window from
the application window by using CTRL + N
*/
function keyPressed(evt) {
	if ((78 == evt.keyCode) && (evt.ctrlKey)) { //Disable CTRL + N to open a new window	
		return false;
	}
	if ((87 == evt.keyCode) && (evt.ctrlKey)) { //Disable CTRL + W to close the window
		return false;     
	}     
	if (122 == evt.keyCode) { //Disable F11 shortcut key
		evt.keyCode = 0;
		evt.returnValue = false;
	}     
	//if (8 == evt.keyCode) { //Disable keyboard back button.
		//return false;
	//}
}

/*Force Only numbers - called on 'onkeyup'
If a user types in a character other than a digit, a alert is displayed and the character is deleted.
Parameter : The field that's input you want to check
*/
var vNumeric = parent.Numeric;
function onlyNumbers(field) {
	valid='0123456789,.';
	len=parseInt(field.value.length);
	var temp;
	var temp2 = '';
	for (i = 0;  i < len;  i++) {
		temp = field.value.charAt(i);
		if (valid.indexOf(temp)==-1) {
			document.Iframe1Form.displayMessage.value = parent.Numeric;
			displayMessage(parent.LoginHTML1); // Error String read from ErrorStrings.js
			field.value = temp2;
			return;
		}
		temp2 = temp2 + temp;
	}
}

function displayMessage(URL) {
	newwindow=window.showModalDialog(URL, this, 'dialogWidth:340px;dialogHeight:222px;dialogTop:center;dialogLeft:center;resizable:no;help:no;status:no;');	
}
	
function getCSS(win) {	
	if(document.MainForm){
		//Called from Main Application pages
		win.write("<link rel='stylesheet' href='" + document.MainForm.cssFile.value + "'/>");
	}
	else {
		//Called from Self Registration screen
		win.write("<link rel='stylesheet' href='" + document.Iframe1Form.cssFile.value + "'/>");
	}
}

function CheckU()
{
	GPword = document.FUsername.GPword.value;
	GCardNo = document.FUsername.GCardNo.value;
	GEaddr = document.FUsername.GEaddr.value;
	/*  This checks that the user has entered a password */
	if(GPword == '')
	{		
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo1
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FUsername.GPword.focus();
		return false;
	}
	if(GCardNo != '')
	{
		valid='0123456789';
		len=parseInt(GCardNo.length);
		var temp;
		var temp2 = '';
		for (i = 0;  i < len;  i++) {
			temp = GCardNo.charAt(i);
			if (valid.indexOf(temp)==-1) {
				dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo3;
				dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
				document.FUsername.GCardNo.value = temp2;
				document.FUsername.GCardNo.focus();
				return false;
			}
			temp2 = temp2 + temp;
		}
		if (len < 16)
		{
			var zeros = 16 - len;
			for ( i = 0; i < zeros ; i++ ) {
			  temp2 = "0".concat(temp2);
			}
			GCardNo = temp2;
		}
	}
	if(GEaddr == '')
	{		
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo4;
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FUsername.GEaddr.focus();
		return false;
	}
  
	if (GEaddr.indexOf("@")== -1) {
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo5;
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FUsername.GEaddr.focus();
		return false;
	}
	displayMessage('/dcal/AuthenticationServlet?action=forgotUser&GPword='+GPword+'&GCardNo='+GCardNo+'&GEaddr='+GEaddr);
	window.close();
	return true;
}

function CheckP()
{
	GUName = document.FPword.GUName.value;
	GCardNo = document.FPword.GCardNo.value;
	GEaddr = document.FPword.GEaddr.value;
	/*  This checks that the user has entered a password */
	if(GUName == '')
	{		
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo2
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FPword.GUName.focus();
		return false;
	}
	if(GCardNo != '')
	{		
		valid='0123456789';
		len=parseInt(GCardNo.length);
		var temp;
		var temp2 = '';
		for (i = 0;  i < len;  i++) {
			temp = GCardNo.charAt(i);
			if (valid.indexOf(temp)==-1) {
				dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo3;
				dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
				document.FPword.GCardNo.value = temp2;
				document.FPword.GCardNo.focus();
				return false;
			}
			temp2 = temp2 + temp;
		}
		if (len < 16)
		{		
			var zeros = 16 - len;
			for ( i = 0; i < zeros ; i++ ) {
			  temp2 = "0".concat(temp2);
			}
			GCardNo = temp2;
		}

	}
	if(GEaddr == '')
	{		
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo4;
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FPword.GEaddr.focus();
		return false;
	}
  
	if (GEaddr.indexOf("@")== -1) {
		dialogArguments.document.Iframe1Form.displayMessage.value= dialogArguments.parent.ReqInfo5;
		dialogArguments.displayMessage(dialogArguments.parent.LoginHTML1);
		document.FUsername.GEaddr.focus();
		return false;
	}
	displayMessage('/dcal/AuthenticationServlet?action=forgotPword&GUName='+GUName+'&GCardNo='+GCardNo+'&GEaddr='+GEaddr);
	window.close();
	return true;
}

/*  This checks that the Card Applicant has entered the 2 mandatory fields */
function CheckCard()
{
	if(document.CardForm.CompName.value == '')
	{
		document.CardForm.displayMessage.value = ReqGuestInfo1;
		displayMessage(parent.CardLoginHTML1);
		document.CardForm.CompName.focus();
		return false;
	}
	if(document.CardForm.CompToken.value == '')
	{		
		document.CardForm.displayMessage.value = ReqGuestInfo2;
		displayMessage(parent.CardLoginHTML1);
		document.CardForm.CompToken.focus();
		return false;
	}
	if(document.CardForm.VerifyID.value != '') 
	{		
		if (document.CardForm.VerifyID.value.length < 20)
		{
			document.CardForm.displayMessage.value = ReqGuestInfo3;
			displayMessage(parent.CardLoginHTML1);
			document.CardForm.VerifyID.focus();
			return false;
		}
	}
	if(document.CardForm.appId.value != '')
	{		
		if(document.CardForm.VerifyID.value == '')
		{
			document.CardForm.displayMessage.value = ReqGuestInfo4;
			displayMessage(parent.CardLoginHTML1);
			document.CardForm.VerifyID.focus();
			return false;
		}
		else
		{	
			CApply = document.CardForm.appId.value;
			valid='0123456789';
			len=parseInt(CApply.length);
			var temp;
			var temp2 = '';
			for (i = 0;  i < len;  i++) {
				temp = CApply.charAt(i);
				if (valid.indexOf(temp)==-1) {
					document.CardForm.displayMessage.value= ReqGuestInfo5;
					displayMessage(parent.CardLoginHTML1);
					document.CardForm.appId.value = temp2;
					document.CardForm.appId.focus();
					return false;
				}
				temp2 = temp2 + temp;
			}
		}
	}

	document.CardForm.submit();
	// window.close();    	
	return true;
}

function getCardCSS(win) {	
	win.write("<link rel='stylesheet' href='" + document.CardForm.cssFile.value + "'/>");
}
function popupscreen(url)
{
	newwindow=window.showModalDialog(url, window, 'dialogWidth:340px;dialogHeight:265px;dialogTop:centre;dialogLeft:centre;resizable:no;help:no;status:no;');
}
