/* JavaScript Document: global.js

	Change Log:

	Date		Programmer			Project	Issue	Subtask	Description
	----		----------			-------	-----	-------	----------------
	11/21/2006	Adrian J. Moreno	8		4189	4338	updated popUp() to set window to always be resizable
	11/21/2006	Adrian J. Moreno	8		4189	4338	added checkEnter() to stop form submit via Enter key
	11/28/2006	Angie Ng			8		4329			add dropDownList() to show drop down menu
	01/26/2007	Adrian J. Moreno	8		0075	5773	added cancelAction()
	02/08/2007	Adrian J. Moreno	8		5643	5924	imported roller() from startLayout.cfm
	03/30/2007	Adrian J. Moreno	8		5643	6277	replaced dropDownList() with Son of Suckerfish Menus
	04/25/2007	Niv  	  Lev		10		2495			Forgot your password cookie domain issue
	07/16/2007	Kelly Johnson		8		6801			Added bussonIndexOn function to force navlist button highlight
	04/10/2008	Jim Ames			9		7803			Modified checkPolicyFormat() policy.length >=13, policy.substring(6, 16) and pNbr.length = 10
*/
function roller(navID, state, imageID)
{
	if (state == 'over') 
	{
		document.getElementById(navID).style.backgroundColor='#369';
	}
	else if (state == 'out')
	{
		if (_buttonIndex != navID) { document.getElementById(navID).style.backgroundColor='#39C'; }
	}
	document.getElementById('themeImage').src = document.getElementById(imageID).src;
}
/*
@function 	= 	checkPolicyFormat()
@purpose 	= 	Check if entered policy number is correctly formatted
@parameters = 	policy - policy number
@returns	= 	boolean
@usage		=	checkPolicyFormat(policy)
*/
function checkPolicyFormat(policy)
{	
	/* if the length of the entered policy number is 7, then it MUST be a numeric value.  */
	if (policy.length == 7) { if (isNaN(policy)) { return false; } } 
	else if (policy.length >= 13 ) /* if the length of the entered policy number is 13,  */
	{ 
		pChr = policy.substring(0, 6); pNbr = policy.substring(6, 16);
		if (pChr.length == 6) { /* The first 6 characters are alphabetic */ if (pChr.search(/[0-9]/) != -1) { return false; } }
		if (pNbr.length = 10) { /* Check to see if the pNbr has an Alpha character */ if (pNbr.search(/[a-z]|[A-Z]/) != -1) { return false; } }
	} 
	else { return false; }
	return true;
}
/*
@function 	= 	clearLabels()
@purpose 	= 	remove className from <label>
@parameters = 	formID: form's ID value
@returns	= 	void
@usage		=	clearLabels("someFormID")
*/
function clearLabels(formID) { var l = document.getElementById(formID).getElementsByTagName("label"); for (x = 0; x < l.length; x++) { if (l[x].getAttribute("id")) { errorLabel(l[x].getAttribute("id"), 0); } } }
/*
@function 	= 	validateSplitDate()
@purpose 	= 	validate a given month/day/year
@parameters = 	day: 1 - 31; month: 1 - 12; year: year
@returns	= 	boolean
@usage		=	validateSplitDate(x, y, z)
*/
function validateSplitDate(month, day, year)
{	var x = 0;var y = 0;var z = 0;
	if ((month.length > 2) || (day.length > 2) || (year.length != 4)){return false;}
	
	if (month.length == 2){if (month.charAt(0) == "0"){x = parseInt(month.charAt(1));}else{x = parseInt(month);}}
	else{x = parseInt(month);}
	
	if (day.length == 2){if (day.charAt(0) == "0"){y = parseInt(day.charAt(1));}else{y = parseInt(day);}}
	else {y = parseInt(day);}
	
	z = parseInt(year);
	
	if (isNaN(x) || isNaN(y) || isNaN(z)) { return false; }     
	if (x != 0 && y != 0 && z != 0){
		switch(x){
			case 2:if (z == Math.round(z / 4) * 4){if(y > 29){return false;}}else{if (y > 28){return false;}} break;
			case 4 : case 6 : case 9 : case 11 : if(y > 30) {return false;} break;
			default:if ((x <= 1) || (x <= 12)){if (y <= 1 || y <= 31){return true;}else{return false;}}else{return false;}break;
		}
		return true;
	}
	else 
	{ 
		return false; 
	}
}

function validateLoginForm()
{
	f = document.getElementById("loginForm"); err = "";
	url = document.URL; urlParts = url.split("/");
	domain = urlParts[2]; controlFolder = urlParts[3];
	if (f.username.value == "") { err += "You must enter a user name.\n"; f.username.focus(); }
	if (f.password.value == "") { err += "You must enter a password.\n"; f.password.focus(); }
	if (err == "") { if (f.rememberuser) { if (f.rememberuser.checked) { saveCookie(); } } f.action = "https://" + domain + "/" + controlFolder + "/public/login.fcc"; f.submit; return true; } else { alert(err); return false; } 
}
function validateBridgeLoginForm(webraterFolder,state,qid)
{
	f = document.getElementById("loginForm"); err = "";
	url = document.URL; urlParts = url.split("/");

	theLeft = url.indexOf("=") + 1;
	theRight = url.lastIndexOf("&");
	var qid = url.substring(theLeft, theRight);

	domain = urlParts[2]; controlFolder = urlParts[3];
	if (f.username.value == "") { err += "You must enter a user name.\n"; f.username.focus(); }
	if (f.password.value == "") { err += "You must enter a password.\n"; f.password.focus(); }
	if (err == "") { if (f.rememberuser) { if (f.rememberuser.checked) { saveCookie(); } } f.action = "https://" + domain + "/" + webraterFolder + "/BridgeLogin/login.fcc"; f.submit; return true; } else { alert(err); return false; } 
}
function loginFocus()
{
	f = document.getElementById("loginForm"); 
	if (location.href.indexOf("message=pw") == -1) { f.username.focus(); }
	else
	{
		var uname = document.cookie;
		if (uname!="")
		{ 
			var index = uname.indexOf("uname="); if (index == -1) { return null; } index = uname.indexOf("=", index) + 1; 
			var endstr = uname.indexOf(";", index); if (endstr == -1) { endstr = uname.length; } uname =  unescape(uname.substring(index, endstr)); 	
			f.username.value = uname; f.password.focus();
		}
	}		
}
function getDomain()
{
	var domainName=document.domain;
	if ((domainName.indexOf(".unitrininc.com")) > 0)
	{ 
		domainName = ".unitrininc.com"; 
	}
	if ((domainName.indexOf(".unitrinspecialty.com")) > 0)
	{ 
		domainName = ".unitrinspecialty.com"; 
	}
	return domainName;
}
function delCookie()
{
	var urlStr = location.href;
	if (urlStr.indexOf("remember") > 0) { var the_date = new Date("December 31, 2002"); var the_cookie_date = the_date.toGMTString(); document.cookie = "drowssap=" + null + "resu=" + null + ";expires=" + the_cookie_date + ";path=/; domain=" + getDomain(); }
}
function loadCookie()
{
	var pw = document.cookie; f = document.getElementById("loginForm");
	if (pw!="")
	{
		function getPass() { var index = pw.indexOf("drowssap="); if (index == -1) { return null; } index = pw.indexOf("=", index) + 1; var endstr = pw.indexOf("resu", index); if (endstr == -1) { endstr = pw.length; } return unescape(pw.substring(index, endstr)); }
		function getId() { var index = pw.indexOf("resu="); if (index == -1) { return null; } index = pw.indexOf("=", index) + 1; var endstr = pw.indexOf(";", index); if (endstr == -1) { endstr = pw.length; } return unescape(pw.substring(index, endstr)); }
		var yourID = getId (); var yourPW = getPass (); var yourCK = getPass () + ";" + getId (); 
		if (yourID!=null && yourPW != null) { f.password.value = yourPW; f.username.value = yourID; }
	}
}
function saveCookie()
{
	f = document.getElementById("loginForm");
	if (f.rememberuser)
	{
		if (f.rememberuser.checked)
		{
			var password; var username; password = f.password.value; username = f.username.value; var the_date = new Date("December 31, 2012"); var the_cookie_date = the_date.toGMTString();
			document.cookie = "drowssap=" + password + "resu=" + username + ";expires=" + the_cookie_date + ";path=/; domain=" + getDomain();
		}
	}
}
/*
@function 	= 	popUp()
@purpose 	= 	create a pop up window with parameters
@parameters = 	url: relative/full URL; 
@parameters = 	windoName: name of the window you're creating; 
@parameters = 	menubar, toolbar, location, directories, status, scrollbars: 0/1; 
@parameters = 	width, height: n > 0
@returns	= 	void
@usage		=	popUp('foo.cfm', 'foo', 0, 0, 0, 0, 0, 1, 400, 200); 
*/
function popUp(url, windowName, menubar, toolbar, location, directories, status, scrollbars, width, height)
{
	var showPage = url; var windowArgs = new Array(); var windowArgsList = "";
	if (menubar == 1) { windowArgs[windowArgs.length] = "menubar=yes"; }
	if (toolbar == 1) { windowArgs[windowArgs.length] = "toolbar=yes"; }
	if (location == 1) { windowArgs[windowArgs.length] = "location=yes"; }
	if (directories == 1) { windowArgs[windowArgs.length] = "directories=yes"; }
	if (status == 1) { windowArgs[windowArgs.length] = "status=yes"; }
	if (scrollbars == 1) { windowArgs[windowArgs.length] = "scrollbars=yes"; }
	if (width > 0) { windowArgs[windowArgs.length] = "width=" + width; }
	if (height > 0) { windowArgs[windowArgs.length] = "height=" + height; }
	windowArgs[windowArgs.length] = "resizable=yes";	// For accessibility reasons, window should always be resizeable
	for (x = 0; x < windowArgs.length; x++) { windowArgsList = windowArgsList + windowArgs[x]; if (x != windowArgs.length) { windowArgsList = windowArgsList + ","; } }
	pop = window.open(showPage, windowName, windowArgsList); pop.focus();
}
/*
@purpose 	= 	This function removes the ability to submit a form with the Enter key.
@return 	=	true or false
@author 	=	Jennifer Madden
@url 		=	http://www.jennifermadden.com/162/examples/stringEnterKeyDetector.html
@version 	=	1, 2004-09-15
*/
function checkEnter(e) 
{	
	var characterCode;
	if (e && e.which) { e = e; characterCode = e.which; } 
	else { if (window.event) { e = event; characterCode = e.keyCode; } }
	if (characterCode == 13) { return false; } else { return true; }
}

/*
@function 	= 	abstractOnLoad()
@purpose 	= 	This method is an abstract function
@returns	= 	void
@usage		=	abstractOnLoad(); 
*/
function abstractOnLoad() { if( typeof(onLoadCall) != "undefined" ) { onLoadCall();	} }
/*
@function 	= 	sfHover
@purpose 	= 	cross-browser drop-down menus
@author		= 	HTMLDog.com: http://www.htmldog.com/articles/suckerfish/dropdowns/
@returns	= 	void
@usage		=	not called directly
*/
sfHover = function() {
	var sfEls = document.getElementById("navlist").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
/*
@function	= cancelAction()
@purpose	= This method will redirect the user away from an application when they click a Cancel button
@parameters	= url: relative/full URL
@parameters	= msg: optional - Message to display in the Confirm box.
*/
function cancelAction(url, msg)
{
	var cancelMsg = "Do you really want to Cancel?";
	if (msg) { cancelMsg = msg; }
	if (confirm(cancelMsg)) { location.replace(url); }
}

/*
@function 	= validateSearch()
@purpose	= To check if search searchField is Blank.
@parameters = obj: string value of the field to check
*/
function validateSearch(obj)
{
	var inputField = document.getElementById(obj);
	if (inputField.value != "" && inputField.value != 'Enter search text here.')
	{
		return true;	
	}
	inputField.value = "Enter search text here.";
	inputField.style.color = '#ccc';
	return false;
	
}
/* JKJ - W008-06801 */
function buttonIndexOn(button)
{
	button.className = "on";
}
