// whitespace characters
var whitespace = " \t\n\r";	
var defaultEmptyOK = false ;	


// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

	
// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

	

//Function Name 	: setBKGImage(paStrBgImageURL,paStrImageId,psCategoryId)
//Purpose	    	: Sets Background Images
//Input Parameters	: paStrBgImageURL,paStrImageId,psCategoryId
//Output Parameters : None
	
function setBKGImage(paStrBgImageURL,paStrImageId,psCategoryId)
{
	
	var iDisplayImageCount, iLifestyleCounterOld, iLifestyleCounterCurrent ;
	var strCookieValRetrieved, strCookieValSaved ;
	strCookieValRetrieved = "" ;
	strCookieValSaved  =  "" ;
	iLifestyleCounterOld = 0 ;
	iLifestyleCounterCurrent = 0 ;
	iDisplayImageCount = 0 ;
	iTotalLifeStyleImages = paStrImageId.length ;	
	//var expiryDate = new Date("March 12, 2099 00:00:00")
	var expiryDate
	strCookieValRetrieved = getCookie('LifestyleCookie') ;	
	iLifestyleCounterOld  = parseInt(getImageCounterForCategory(strCookieValRetrieved,psCategoryId)) ;
	if (iLifestyleCounterOld == 0 )
	{
		iLifestyleCounterCurrent = Math.round(Math.random()*(paStrBgImageURL.length-1));
		if (iLifestyleCounterOld == 0 )
		{
			iLifestyleCounterCurrent = 1 ;
		}		
		
	} else 	 {
		iLifestyleCounterCurrent = iLifestyleCounterOld + 1 ;
		if (iLifestyleCounterCurrent >= iTotalLifeStyleImages + 1)
		{
			iLifestyleCounterCurrent = 1 ;
		}		
	}		
	
	if (document.body)
	{
		if (paStrBgImageURL.length > 0)
		{
			document.body.background = paStrBgImageURL[iLifestyleCounterCurrent-1] ;
			document.body.style.backgroundRepeat = 'no-repeat' ;				
			strCookieValSaved = FormCookieValue(strCookieValRetrieved,psCategoryId, iLifestyleCounterCurrent) ;
			setCookie('LifestyleCookie',strCookieValSaved,expiryDate,'','',false);
		}
	}
	
}	



//Function Name 	: setCookie(name, value, expires, path, domain, secure)
//Purpose	    	: Sets Cookie
//Input Parameters	: name, value, expires, path, domain, secure
// 					name - name of the cookie
// 					value - value of the cookie
// 					[expires] - expiration date of the cookie (defaults to end of current session)
// 					[path] - path for which the cookie is valid (defaults to path of calling document)
// 					[domain] - domain for which the cookie is valid (defaults to domain of calling document)
// 					[secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// 					an argument defaults when it is assigned null as a placeholder
// 					a null placeholder is not required for trailing omitted arguments
//Output Parameters : None

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie ;
}


//Function Name 	: getCookie(name)
//Purpose	    	: Gets Cookie Name
//Input Parameters	: name - name of the desired cookie
//Output Parameters : return string containing value of specified cookie or null if cookie does not exist

function getCookie(name) {
  var dc = document.cookie ;
  var prefix = name + "=" ;
  var begin = dc.indexOf("; " + prefix);
  
  if (begin == -1) 
  {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else 
  {
  	begin += 2 ;
  }
  var end = document.cookie.indexOf(";", begin) ;
  
  if (end == -1) end = dc.length ;
  return unescape(dc.substring(begin + prefix.length, end)) ;
  
}





//Function Name 	: deleteCookie(name, path, domain)
//Purpose	    	: Deletes Cookie
//Input Parameters	: name, path, domain
//		 			  name - name of the cookie
// 					  [path] - path of the cookie (must be same as path used to create cookie)
// 					  [domain] - domain of the cookie (must be same as domain used to create cookie)
// 					  * path and domain default if assigned null or omitted if no explicit argument proceeds
//Output Parameters : None
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


//Function Name 	: getImageCounterForCategory(strCookieVal,strCat)
//Purpose	    	: 
//Input Parameters	: strCookieVal,strCat
//Output Parameters 	: None
function getImageCounterForCategory(strCookieVal,strCat)
{
	var aFirstSplit = new Array();
	var aSecondSplit = new Array();
	var iLifestyleCounter = 0  ;
		
	if (strCookieVal != "" && strCookieVal != null ) 
	{
		aFirstSplit = strCookieVal.split("~") ;
		
		for (var i = 0; i < aFirstSplit.length; i++)
		{
			aSecondSplit = strCookieVal.split("|") ;
			
			if  ( aSecondSplit[0] == strCat )
			{
				iLifestyleCounter = aSecondSplit[1] ;				
				break ;
			}	
		}	
	} 
	return iLifestyleCounter ;
}



//Function Name 	: FormCookieValue(strCookieVal,strCat,iLifestyleCounter)
//Purpose	    	: 
//Input Parameters	: strCookieVal,strCat,iLifestyleCounter
//Output Parameters 	: None

function FormCookieValue(strCookieVal,strCat,iLifestyleCounter)
{
	var aFirstSplit = new Array();
	var aSecondSplit = new Array();
	var aFirstJoin = new Array();
	var strFinalString = "" ;
	var catExist = "";
	var strCurrString = "" ;
	var strFirstString  = "" ;
	
	if (strCookieVal != "" && strCookieVal != null ) 
	{
		aFirstSplit = strCookieVal.split("~") ;
		
		for (var i = 0; i < aFirstSplit.length-1; i++)						
		{
			aSecondSplit = aFirstSplit[i].split("|") ;

			if (aSecondSplit[0] == strCat ) 
			{
				catExist = "true" ;
				aSecondSplit[1] = iLifestyleCounter ;
			}		
			
			aFirstJoin[i] = aSecondSplit.join("|");	
			
			if (i = 0)
			{
				strFinalString = aSecondSplit.join("|");	
			} else
			{
				if (strFinalString == "" )
				{
					strFinalString = aSecondSplit.join("|");				
				} else
				{
					strFinalString = strFinalString + "~" + aSecondSplit.join("|");								
				}
			}			
		}
	}
	
	
	if ( catExist != "true" && strCat != "" )
	{			
		strCurrString = strCat + "|" + iLifestyleCounter ;
		if (strFinalString == "" )
		{
			strFinalString =  strCurrString ;
		} else
		{
			strFinalString = strFinalString + "~" + strCurrString ;			
		}		
	}
	return strFinalString;
}



//Function Name 	: AdjustWnd
//Purpose	    	: To adjust the window according to size specified
//Input Parameters	: nWndWidth, nWndheight
//Output Parameters : Resize itself to appropriate size
function AdjustWnd(nWndWidth, nWndheight)
{
	var W;
	var H;
	var vHeight;
	var vWidht;
	var NS4;
	var vIE;
	vIE = document.all;
	NS4 = document.layers;
	W = nWndWidth ;
	H = nWndheight ;

	if (vIE)
	{
		self.resizeTo(W,H+300);
		vWidth = W + (W - document.body.clientWidth) ;
		vHeight = H + (H - (document.body.clientHeight-300)) ;
	}
	else
	{
		if(NS4)
		{
			self.resizeTo(W,H);
			vWidth = W;
			if(H<=100)
			{
				vHeight = H;
			}
			else
			{
				vHeight = H;
			}
		}
		else
		{
			self.resizeTo(W,H+300);
			vWidth = W + (window.outerWidth - window.innerWidth)  ;
			if(H<=100)
			{
				vHeight = H + (window.outerHeight - window.innerHeight) ;
			}
			else
			{
				vHeight = H + (window.outerHeight - window.innerHeight)+5 ;
			}
		}

	}
	if(H<=100)
	{
		self.resizeTo(vWidth+30,vHeight+30);
	}
	else{
		self.resizeTo(vWidth+30,vHeight+30);
	}
	focus();
}


function fnSubmit()
{
	if (window.document.frmChangePassword.txtUserName.value == "")
	{	alert("Please enter your member name.");
		return false;
	} else if (window.document.frmChangePassword.txtOldPassword.value == "")
	{	alert("Please enter your old password.");
		return false;
	} else if ( window.document.frmChangePassword.txtNewPassword.value == "")
	{	alert("New Password cannot be blank.");
		return false;
	} else if ( window.document.frmChangePassword.txtRePassword.value == "")
	{	alert("please re-enter new password.");
		return false;
	}

return true;
}

function RedirectTo(sURL) {
var URL = sURL
window.document.frmRegistrationExtended.action = URL;
window.document.frmRegistrationExtended.submit();
}


function SetMode(mode)
{
if (mode == "EDIT")
	window.document.frmLogin.hdnMode.value ="Edit";
else 
	window.document.frmLogin.hdnMode.value ="Login";
return false
}



function LoadWnd(sUrl)
{
	var sNewWnd;

	sNewWnd = sUrl ;
	
	if (sNewWnd != "")
	{		
		//Open the link url in the new window
		window.open(sNewWnd, "new",'toolbar=yes');
		
		//Load the original URL in the current window
		window.history.back(1);
	}	
}	



//Function Name 	: onSearchClick_a
//Purpose	    	: On Click of search posts to search list page
//Input Parameters	: None
//Output Parameters : None	
function onSearchClick_a(jForm,jField) 
{		
	var validStatus;
	validStatus = true;
	if (isWhitespace(eval('window.document.'+jForm+'.'+jField).value)) 	
	{
		    alert("Please enter Keyword");
			validStatus = false ;
	} 	
	if  (validStatus == false) 	return false ;												
}


//Function Name 	: onSearchClick_b
//Purpose	    	: On Click of search posts to search list page
//Input Parameters	: None
//Output Parameters : None	
function onSearchClick_b(jForm,jField) 
{		
	var validStatus;
	validStatus = true;
	window.document.frm.hSubmit.value = "Search" ;	
	if (isWhitespace(eval('window.document.'+jForm+'.'+jField).value)) 	
	{
		    alert("Please enter Keyword");
			validStatus = false ;
	} 
	if  (validStatus == false) 	return false ;												
}


//Function Name 	: onSearchClick_c
//Purpose	    	: On Click of search posts to search list page
//Input Parameters	: None
//Output Parameters : None	
function onSearchClick_c() 
{		
	var validStatus;
	validStatus = true;
	var valContentTypeSelected ;
	var valMenuCatSelected ;
	
	valContentTypeSelected = window.document.frmSrch.selectContentType.value ;
	valMenuCatSelected	   = window.document.frmSrch.selectSearchArea.value ;


	
	if  (valContentTypeSelected == -1 || valContentTypeSelected == -2 || valMenuCatSelected == -1  || valMenuCatSelected == -2 )
	{
		 if (isWhitespace(window.document.frmSrch.textKeyword.value)) 	
		{
		    alert("Please enter Keyword");
			validStatus = false ;
		} 
	}	
	if  (validStatus == false) 	return false ;
	var lPos = window.document.frmSrch.action.indexOf('q=');
	if (lPos >0 )
	{
		window.document.frmSrch.action = window.document.frmSrch.action.substring(0,lPos-1);
	}
	
	window.document.frmSrch.action = window.document.frmSrch.action + '?q='+escape(window.document.frmSrch.textKeyword.value);
}



//Function Name 	: doSubmit
//Purpose	    	: posts form
//Input Parameters	: None
//Output Parameters : None
function doSubmit(url) {	
	window.document.frmSrch.action = url;
	window.document.frmSrch.method = "post" ;
	window.document.frmSrch.submit();
}



function submitfrm(url)
{
	window.document.frm.action=url;		
	window.document.frm.method="post";
	window.document.frm.submit();
}


//   open new window -  Specific to Glossary Content Type
function openDefinationTermWindow(strLocation,nWidth,nHeight) {
	window.open(strLocation,"new","width=nWidth,height=nHeight,toolbar=yes,status=no,menubar=0,scrollbars=yes,resizable=yes");
}	


//   open new window
function openNewWindow(strLocation) {
	window.open(strLocation,"new","width=300,height=400,toolbar=yes,status=no,menubar=0,scrollbars=yes,resizable=yes");
}	

//   open new window with var from a Special Menu
function openSpMenu(url,wd,ht,tl,st,mn,sc,re) {
var strurl, strwd, strht, strtl, strst, strmn, strsc, strre, strparams;
strparams="";
strtl="yes";
strst="yes";
strmn="yes";
strsc="yes";
strre="yes";

	if(url!=""){strurl=url;}
	if(wd!="" && isFinite(wd)){
			strwd=wd;
			strparams=strparams+"width="+strwd;
	}
	if(ht!="" && isFinite(ht)){
			strht=ht;
			if(strparams!=""){strparams=strparams+",";}
			strparams=strparams+"height="+strht;			
	}
	if(tl!="" && tl!=null && (tl=="no"||tl=="yes")){strtl=tl;}
		if(strparams!=""){strparams=strparams+",";}
		strparams=strparams+"toolbar="+strtl;
	if(st!="" && st!=null &&(st=="no"||st=="yes")){strst=st;}
		if(strparams!=""){strparams=strparams+",";}
		strparams=strparams+"status="+strst;
	if(mn!="" && mn!=null &&(mn=="no"||mn=="yes")){strmn=mn;}
		if(strparams!=""){strparams=strparams+",";}
		strparams=strparams+"menubar="+strmn;
	if(sc!="" && sc!=null &&(sc=="no"||sc=="yes")){strsc=sc;}
		if(strparams!=""){strparams=strparams+",";}
	 	strparams=strparams+"scrollbars="+strsc;
	if(re!="" && re!=null &&(re=="no"||re=="yes")){strre=re;} 
		if(strparams!=""){strparams=strparams+",";}
		strparams=strparams+"resizable="+strre;

if(strurl!=""){
	window.open(strurl,"new",strparams);
	}
}

//Function Name 	: PopulateMenuCategories(selectedindex)
//Purpose	    	: Dynamically creates dropdown
//Input Parameters	: selectedindex
//Output Parameters 	: None

function PopulateMenuCategories(selectedindex)
{
	var ddConType ;
	var ddMenuCat ;
	var j , k;
	k = 2 ;
	ddConType = document.frmSrch.selectContentType ;
	ddMenuCat = document.frmSrch.selectSearchArea ;
	
	if ( selectedindex != null )
	{
		sSelectedContentCategory = ddConType.options[ddConType.selectedIndex].value ;
	}
	else
 	{
		sSelectedContentCategory = sSelectedContentCategoryIdTmp ;
	}	

		
	if (sSelectedMenuCategoryId  == "" || sSelectedMenuCategoryId == null) 
	{
		sSelectedMenuCategoryId = sSelectedMenuCategoryIdTmp ;
	}

	for (var i = 0; i < ddMenuCat.length; i++) 
	{	
		if ( ddMenuCat != null ) 
		{
			ddMenuCat.options[i].text = ""  ;
			ddMenuCat.options[i].value = "" ;
		}
	}

	ddMenuCat.options.length = 0 ;
	
	if ( ddMenuCat != null ) 
	{
		ddMenuCat.options[ddMenuCat.options.length] = new Option('Search All Areas' ,'-1');
	}

	if ( sSelectedContentCategory != "" )
	{
		for (var i = 0; i < aConTypeMenuCategory.length; i++) 
		{	
			if (aConTypeMenuCategory[i][0] == sSelectedContentCategory )
			{
		       ddMenuCat.options.length++;  
		       ddMenuCat.options[ddMenuCat.options.length-1].text  = aConTypeMenuCategory[i][2] ;
		       ddMenuCat.options[ddMenuCat.options.length-1].value = aConTypeMenuCategory[i][1];

			   if ( sSelectedMenuCategoryId != null )
				{
					if ( aConTypeMenuCategory[i][1] == sSelectedMenuCategoryId )
					{
						ddMenuCat.options[ddMenuCat.options.length-1].selected = true ;
					}
				}				
				k = k + 1;				
			}
		}		 	  
	}
}


//Function Name 	: validateServiceLocator()
//Purpose	    	: check for valid data on servicer locator screen.
//Input Parameters	: none
//Output Parameters 	: True/False
function validateServiceLocator()
{
	var productCategory;
	var zipcode;
	var temp ="0123456789";
	productCategory = document.forms[0].elements["category"];
	zipcode = document.forms[0].elements["zipcode"].value;
	if( !productCategory[0].checked && !productCategory[1].checked 
		&& !productCategory[2].checked && !productCategory[3].checked
		&& !productCategory[4].checked && !productCategory[5].checked)
	{
		alert("Please select a product category");
		return false;
	}
	// check if zip code is null 
	if(isWhitespace (zipcode))
	{
		alert("Please enter a zipcode to search.");
		return false;
	} else if(zipcode.length < 5)
	{
		alert("Please enter a valid zipcode to search.");
		return false;
	}
	for (i=0; i < zipcode.length; i++)
	{
		if (temp.indexOf(zipcode.charAt(i)) == -1)
		{
			alert("Please enter a valid zipcode to search.");
			return false;
		}
	}
	return true;
}