///////////////////////////////////////////////////////////////////////////////////
//
//	Login validation javascript functions
//
//		7/8/2004 by Frank M. DeProspo
//
//		This is a compilation of the password validation
//		functions that are contained in all of our applications.
//		I removed them from the asp and put them into this
//		include file for convenience.
//
//
///////////////////////////////////////////////////////////////////////////////////


function isLetter(aChar)
{
	return ((aChar >= 'a') && (aChar <= 'z')) ||
		((aChar >= 'A') && (aChar <= 'Z'));
}


function isDigit(aChar)
{
	return ((aChar >= '0') && (aChar <= '9'));
}


function isNumeric(number)
{
	number = number + "";
	return ((number.length > 0) && isComposedOfChars("0123456789", number));
}


function isAlphanumeric(alphaNumString)
{
	alphaNumString = alphaNumString + "";
	return ((alphaNumString.length > 0) &&
		isComposedOfChars("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", alphaNumString));
}

function isAlphanumericOrUnderscore(alphaNumString)
{
	alphaNumString = alphaNumString + "";
	return ((alphaNumString.length > 0) &&
		isComposedOfChars("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_", alphaNumString));
}

function hasText(text)
{
	var newText = strip(text + "", " ");
	//return (newText != "");
	//FIX:
	if((newText != "") && (newText != null))
		return(true);
	else
		return(false);
}


function remove(string, rChar)
{
	var retStr = "";

	for(var i = 0; i < string.length; i++)
	{
		var ch = string.charAt(i);
		if (ch != rChar)
		{
			  retStr = retStr + ch;
		}
	}
	return retStr;
}


// Return the index into inString of the first character in inString
// that is not found in okayChars.
function indexOfFirstNotIn(okayChars, inString)
{
	var i;
	for (i=0; i < inString.length; i++)
	{
		var charm = inString.charAt(i);
		if (okayChars.indexOf(charm) == -1)
		{
		  return i;
		}
	}
	return -1;
}

// Return the index into inString of the last character in inString
// that is not found in okayChars.
function indexOfLastNotIn(okayChars, inString)
{
	var i;
	for (i = inString.length - 1; i >= 0; i--)
	{
		var charm=inString.charAt(i);
		if (okayChars.indexOf(charm) == -1)   // return this char
		{
		  return i;
		}
	}
	return -1;
}


function strip(string, sChar)
{
	if ((string == null) || (string == ""))
	{
		return "";
	}

	var startIndex = indexOfFirstNotIn(sChar, string);
	var endIndex = indexOfLastNotIn(sChar, string);

	// Confirm that we have at least one non-stripped character.
	if (startIndex == -1)
	{
		return "";
	}

	return string.substring(startIndex, endIndex + 1);
}


function substringWithChars(validChars, inString)
{
	var tempString = "";
	var i;
	for (i=0; i < inString.length; i++)
	{
		var ch = inString.charAt(i);
		if (validChars.indexOf(ch) != -1)   // this char is valid, save it
		{
			  tempString = tempString + ch;
		}
	}
	return tempString;
}

function isComposedOfChars(validChars, inString)
{
	return (indexOfFirstNotIn(validChars, inString) == -1);
}

function hasCommonLengthFourSubstrings(inString1, inString2)
{
	var string1 = strip(inString1," ");
	var string2 = strip(inString2," ");
	var numberOfSubstrings = string1.length - 3;
	var i;
	for (i = 0; i < numberOfSubstrings; i++)
	{
		var nextSubstring = string1.substring(i, i+4);
		if (string2.indexOf(nextSubstring) != -1)
		{
			return true;
		}
	}
	return false;
}


function autoSub() 
{
	checkFormInputNSubmit();
	doSub();
}


function subFormData()
{
	document.cookie =  "autoPostCheck=1240";
	document.form1.submit();
}


function closePopWin()
{    
	// close pop-up window if it is open
	//do not close if early IE

	if (navigator.appName != "Microsoft Internet Explorer" || parseInt(navigator.appVersion)>=4) 
	{
		if(new_win != null) 
		{
			if(!new_win.closed) 
			{
				new_win.close();
			}
		}
	}
}

function menuWindow(loc) 
{
	closePopWin();
	new_win = window.open(loc, "NewWindow", "location=1,toolbar=1,menubar=1,scrollbars=1,status=1,width=515,height=480,directories=1,resizable=1,top=0,left=0");
}



function windowOpener2(e) 
{
	errormsg = e;
	if (errormsg!="")
	{
		alert(e);
	}
	else
	{
		document.form1.New_Password.focus();
	}

}

function launchCenter(url, name, height, width) 
{
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) 
	{
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}
	srt = str += ", menubar=no, scrollbars=no, status=0, location=0, directories=0, resizable=1";
	return window.open(url, name, str);
}


function checkForCookies()
{
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000 * 24);
	document.cookie = "cookieEnabledPersist=true; expires=" + expire.toGMTString();
	document.cookie = "cookieEnabledSession=true";
	var cookieString = ""+document.cookie;
	if (cookieString.indexOf("cookieEnabledPersist")==-1 || cookieString.indexOf("cookieEnabledSession")==-1)
	{
		document.location.href = "ValidateLogin.asp";
	}
}


function windowOpener() 
{
	checkForCookies();
	closePopWin();
	if (errm!="") 
	{
		new_win = launchCenter("", "NewWindow", 400, 340);
		//new_win = window.open("","NewWindow", "menubar=no,scrollbars=no,status=0,width=340,height=400,location=0,directories=0,resizable=1");
		doc = new_win.document;
		window.setTimeout("writeWindow(new_win,errm);", 100);
		//if (new_win.focus) { new_win.focus(); }
	}
	else if (blockbrow=="true") 
	{
		new_win = launchCenter("", "NewWindow", 500, 340);
		//new_win = window.open("","NewWindow", "menubar=no,scrollbars=no,status=0,width=340,height=500,location=0,directories=0,resizable=1");
		doc = new_win.document;
		window.setTimeout("writeWindowBlockBrow(new_win);", 100);
		//if (new_win.focus) { new_win.focus(); }
	}
	else 
	{
		document.form1.New_Password.focus();
	}
}

function writeWindowBlockBrow(win) 
{
	doc.clear();
	doc.write('<HTML><HEAD><TITLE>Please Note</TITLE></HEAD>');
	doc.write('<BODY bgcolor=#ffffff link=#0000cc vlink=#ff0000 alink=#cecece>');
	doc.write('<IMG SRC=media\images\AppLogo.gif width=198 height= 80 ALT=\"iCAST\">');
	doc.write('<CENTER>');
	doc.write('<FONT color=\"#ff0000\" size=2 face=\"Arial\">');
	doc.write('</FONT></CENTER>');
	doc.write(' <BR>We have determined that you are using Netscape 6 as your browser. iCAST does not currently support this browser. <BR><BR>iCAST currently supports the following browsers for IBM compatible users: <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Netscape Navigator 4.0x <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Microsoft Internet Explorer 4.0x <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Microsoft Internet Explorer 5.0x <BR><BR>iCAST supports the following browsers for Macintosh users: <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Netscape Navigator 4.0x <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Microsoft Internet Explorer 4.0x<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Microsoft Internet Explorer 5.0x<BR><BR><BR>');
	doc.write('<P><CENTER>');
	doc.write('<a test123=\"\" href=\'javascript:window.close()\'>\n');
	doc.write('<IMG SRC=/images/ok.gif width=63 height=20 border=0 ALT=OK></A>');
	doc.write('<CENTER></BODY></HTML>');
	doc.close();
}


var times = 0;
var errm = "";
var errormsg = "";
var blockbrow = "";
var doc;
var new_win = null;
var nw = null;

ERROR_MESSAGE_ALREADY_SUBMITTED = "<LI>You have already submitted a request.  Please wait for your request to be processed.";
ERROR_MESSAGE_INVALID_ID_PASSWORD = "<LI>We are unable to verify the information that you have entered.  Please be certain that you have entered your RaCF and Password correctly.<BR><BR>For more information about IDs or Passwords, please use the ID and Password links on the Sign In screen.";
ERROR_MESSAGE_EMPTY_FIELDS = "<LI>Please complete all of the required fields before continuing.";

MSG0003=" The Password must be 3-15 numbers and/or letters, case sensitive. Please re-enter the Password.";
MSG0003_short="The Password you entered is too short.";
MSG0003_large="The Password you entered is too large.";
MSG0003_spl_char="The Password you entered contains one or more special character(s) (~`!@#$%^&*()-+|&#92;&#34;',.?/:;<>[]{}=_), which is not allowed.";
MSG0003_space="The Password you entered contains one or more space character(s), which is not allowed.";
MSG0003_same="The Password you entered is the same as the current password.  Please enter a new password.";
MSG0003_notsame="The New and Confirm Passwords do not match.  Please enter the same password in both New and Confirm.";

function openErrorWindow(error_message)
{
	windowOpener2(error_message);
}

function checkFormInputNSubmit()
{
	// No Id and Pass
	// ID - No Pass
	// Pass - No ID
	// Either one Misssing

	// Incorrectly formatted ID
	// Incorrectly formatted Pass

	document.form1.Access_ID.value = document.form1.Access_ID.value;

	accessID = strip(document.form1.Access_ID.value, " ");
	Password = strip(document.form1.New_Password.value, " ");
	
	error_message = checkPassword(Password);

	if ( error_message != "empty" )
	{
		openErrorWindow(error_message);
		return;
	}

	document.form1.submit();
}


function checkPassword(Password)
{
	if ( document.form1.Confirm_Password.value != Password )
	{
		return MSG0003_notsame + MSG0003;
	}

	if ( document.form1.New_Password.value.indexOf(' ')  > -1 )
	{
		return MSG0003_space + MSG0003;
	}

	if ( document.form1.Current_Password.value == Password )
	{
		return MSG0003_same + MSG0003;
	}

	if (Password.length < 4 )
	{
		return MSG0003_short + MSG0003;
	}

	if (Password.length > 15 )
	{
		return MSG0003_large + MSG0003;
	}

	if (!isAlphanumeric(Password))
	{
		return MSG0003_spl_char + MSG0003;
	}
	return "empty";

}

function gatingUnload()
{
}

function doSub() 
{
	checkFormInputNSubmit();
	return true;
}


function loginHelp()
{
}

function resetPassword()
{
    document.resetForm.Access_ID.value = document.form1.Access_ID.value;
    document.resetForm.reason.value = "reset_request_model";
	document.resetForm.submit();
}


function doReset() {
     document.form1.Access_ID.value = document.form1.Access_ID.value;
     if (times==0) {
       if (document.form1.Access_ID.value.length == 0)
       {
          windowOpener2(ERROR_MESSAGE_INVALID_ACCESS_ID_RESET);
          return false;
       }
       if (!isValidAccessID(document.form1.Access_ID)) 
       {
          windowOpener2(ERROR_MESSAGE_INVALID_ID_PASSWORD);
          return false;
       }
       document.form1.reset.value = "yes";
       subFormData();
       return false;
     }
     else {
       windowOpener2(ERROR_MESSAGE_ALREADY_SUBMITTED);
       return false;
     }
}



