/*************************************************************
 *				SHOW AND HAND GLOBAL MENU FUNCTIONS
*************************************************************/
//global menu values
var MENU_TIMER_ID;			//timer flag for menu display
var CURRENT_MENU_ID;			//current menu display
var CURRENT_TAB_ID;			//get current tab name

function showTopNavMenu(el, img) {
	//alert("Firing showTopNavMenu!");
	window.clearTimeout(MENU_TIMER_ID);
	if(CURRENT_MENU_ID && CURRENT_MENU_ID != el) hideTopNavMenuAction( CURRENT_MENU_ID, CURRENT_TAB_ID );		//clear current menu id if the exist
	CURRENT_MENU_ID = el;																				//set new current menu id
	CURRENT_TAB_ID = img;																				//set new current tab id
	rollOverTab(el, img)																					//highlight tab
	showNavMenu(el);																						//display menu
}

//sets timer for hiding of the menu
function hideTopNavMenu(el, img){
	//alert("Firing hideTopNavMenu!");
	MENU_TIMER_ID = window.setTimeout("hideTopNavMenuAction('" + el + "', '" + img + "')", 500);
}	

function hideTopNavMenuAction(el, img) {
	rollOffTab(el, img)
	hideNavMenu(el);
}

//nav rollover function
function rollOverTab(el, img){
	$(el + "nav").style.backgroundImage = 'url(/images/nav/' + img + '_on.gif)';
}

//nav rollover function
function rollOffTab(el, img){
		$(el + "nav").style.backgroundImage = 'url(/images/nav/' + img + '_off.gif)';
}

function showNavMenu(el) {
	var pos = new Array();
	pos = getMenuPosition($(el + 'nav'));
	var elLeft = pos[0] + 1;
	var elTop= pos[1] + 26;
	$(el + 'ddmenu').style.left = elLeft + 'px';
	$(el + 'ddmenu').style.top = elTop + 'px';
	$(el + 'ddmenu').style.display = 'block';		
}

function hideNavMenu(el) {
	$(el + 'ddmenu').style.left = 0;
	$(el + 'ddmenu').style.top = 0;
	$(el + 'ddmenu').style.display = 'none';			
}

//get menu element position
function getMenuPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function highlightDropdown(el, state) {
	if(state == 'over') {
		$(el).className = 'highlightOver';	
	} else {
		$(el).className = 'highlightOff';
	}
}

function isDivEvent(e){
	if(!e) var e = window.event;
	var eventTarget = (window.event) ? e.srcElement : e.target;
	return (eventTarget.nodeName == 'DIV') ? true : false;
}

function preloadNavImages(){
}

/*************************************************************
 *				VALIDATION FUNCTIONS
*************************************************************/
function validateZipcode(frm, fld) {
	var zip = eval('document.' + frm + '.' + fld + '.value');	//get zip code value	
	//check if it is a 5-digit number returning true or false
	var regex = /^\d{5}$/i;
	if(regex.exec(zip) == null) {
		alert("Please enter a valid 5 digit zip code and try again.");
		eval('document.' + frm + '.' + fld + '.focus()');
		return false;
	} else {
		return true;
	}
}


/*************************************************************
 *				UTILITIY FUNCTIONS
*************************************************************/
//get object handle function from Prototype 1.4
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

//cross platform event registrar
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

//set form focus
function setFocus(el){
	$(el).focus();
}

//simple navigation function
function goTo(url){
	window.location.href = url;
}

//simple navigation function with new window
function goToExternal(url, winName){
	newwindow = window.open(url, winName, 'width=700,height=500,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,menubar=yes');
	if (window.focus) {newwindow.focus()}
	return false;	
}

//simple navigation function with new window
function openPopupWindow(url, winName, h, w){
	newwindow = window.open(url, winName, 'width=' + w + ',height=' + h + ',resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no');
	if (window.focus) {newwindow.focus()}
}

//function to highlight current page's navigaiton links
function highlightGlobalNav() {
	var windowURL = unescape(window.location.href);
	var getquote = /\/startquote.asp/i; 
	var findagent = /\/agents\//i; 
	var insuranceprods = /\/products\//i; 
	var aboutus = /\/about\//i; 
	
	if(windowURL.match(getquote) != null) {
		$('getquote').className = 'current';
		$('findagent').className = 'rightofcurrent';
	}
	else if(windowURL.match(findagent) != null) {
		$('findagent').className = 'current';
		$('insuranceprods').className = 'rightofcurrent';
	}
	else if(windowURL.match(insuranceprods) != null) {
		$('insuranceprods').className = 'current';
		$('aboutus').className = 'rightofcurrent';
	}
	else if(windowURL.match(aboutus) != null) {
		$('aboutus').className = 'current';
	}
}

//function to highlight current page's navigaiton links
function highlightLeftNav() {
	var i, linklist, nextGlobalLink;	
	var windowURL = unescape(window.location.href);
	for(i=0; i<document.links.length; i++){
		if(document.links[i].href.toLowerCase() == windowURL.toLowerCase()){
			//if it is a left hand nav element
			if(document.links[i].parentNode.parentNode.className == 'leftNav' && document.links[i].parentNode.className == 'navItem'){
				document.links[i].parentNode.className = 'navItem current';
			} 
		}
	}
}

function getDOMAttribute(obj){
	for( var x = 0; x < obj.attributes.length; x++ ) {
		if( obj.attributes[x].nodeName.toLowerCase() == 'class' ) {
			window.alert( 'The value of the \'title\' attribute is: ' +
				theParagraph.attributes[x].nodeValue );
		}
	}	
}

//submit form to ensure validation is triggered on scripted submit
function submitForm(frm, vscript, fld) {	
	var bValidate = eval(vscript + "('" + frm + "', '" + fld +  "')");	//returns true of the form is valid
	if(bValidate){
		eval("document." + frm + ".submit()");
	}
}


/*************************************************************
 *				ACCOUNT MANAGEMENT FUNCTIONS
*************************************************************/
function validatePolicy(frm) 
{
	var strPolicy = eval("document." + frm + ".policyno.value");
	//eval("document." + frm + ".action = ''");
	if (Trim(strPolicy) == "")
	{
		alert("POLICY NUMBER IS BLANK\n---------------------------------------------\n" +	
				"Please enter a policy number and try again.\n");
		eval("document." + frm + ".policyno.focus()");
		return false;
	}
	else
	{	
	       if (strPolicy.length < 10 || strPolicy.length > 13 || isAlphaNumeric(Trim(strPolicy)) == false) {
		   	alert("INVALID POLICY NUMBER\n---------------------------------------------\n" +	
					"Valid policy numbers are between 10 and 13 characters in length.\n\n" +
					"Please make sure your policy number is the correct length and \n" + 
					"does not contain any spaces or dashes (-).\n");
				eval("document." + frm + ".policyno.focus()");
	       	return false;
	       }
	       return true;
	}	
}

function changeLocation(){
	document.policy_lookup.policyno.value='';
	document.policy_lookup.action="https://www.secureinsforms.com/MyPolicy/Login/MainMenu.asp?target=AcctInfo";		
	document.policy_lookup.submit();
}


function isAlphaNumeric(strValue) {
    var j;
    var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    var strChar;
  
    for (j=0; j < strValue.length; j++)
    {
        strChar = strValue.charAt(j);
 
        if (chars.indexOf(strChar) == -1)
        {
	        return false;
   	    }
    }    
    return true;
     
  }  
 
//Trimming functions
function LTrim(str){
	if (str==null){return null;}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
	}

function RTrim(str){
	if (str==null){return null;}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
	}

function Trim(str){return LTrim(RTrim(str));}

