// JavaScript Document

var setcookiepage = "http://www.brighthouse.com/setcookie.asp"
/**
 * This section contains all of the Cookie Handling functions
 *
 */


/*This part if for the increase remember cookie */


/**
 * This function creates a cookie on the user’s machine for a given number of days.
 * @param {string} name The name for the cookie
 * @param {string} value The value for the cookie
 * @param {int} days The number of days the cookie is valid
 * @member CookieNS
 */
 
function createCookie(name,value,days)
{	
	value = value + "," + urlPath;
	try{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	}
	catch(e){alert(e);}
}

/**
 * This function creates a regular cookie
 * @param (string) name The name of the cookie
 * @param (int) value The value of the cookie
 * @param (int) days The number of days cookie is valid
 */
 function createSelectionCookie(name, value, days)
 {
   try{
   if(days)
   {
     var date = new Date();
	 date.setTime(date.getTime()+(days*24*60*60*1000));
	 var expires = "; expires="+date.toGMTString();
   } else {
     var expires = "";
   }
   
   document.cookie = name+"="+value+expires;
   }
   catch(e){alert(e);}
 }

/**
 * This function reads a cookie on the user’s machine.
 * @param {string} name The name for the cookie
 * @returns The value of the cookie
 * @type String
 * @member CookieNS
 */

function readCookie1(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		  return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
 * This function erases the cookie of the given name on the user’s machine.
 * @param {string} name The name of the cookie
 * @see createCookie Sends function settings which will create a cookie in the past therefore deleting it.
 * @member CookieNS
 */

function eraseCookie(name)
{     //alert(name);
	createCookie(name,"",-1);
}

/**
 * This function calls a Bright House page and submits the value of the Zip Code finder.
 * If text box is left blank or empty then it redirects to "Locations page" - "/areas_we_serve/default.aspx"
 * else it checks to see if the zip code is in the proper format is entered and does not submit
 * the form until the requirement is met.
 * @param {object} f The form Object
 * @see setfocusZipHP is the event handler for this function
 * @member CookieNS
 */
 /**  Need rewrite for multiple divisions espanol **/
function setcookieHP(f){    
    var ZipCodeReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    var strZip = f.txtzipcode.value; //*** Trim() spaces to setup check for redirect("Locations page") when blank ***

    strZip = strZip.replace(/^\s+|\s+$/, ''); //*** Trim() spaces to setup check for redirect("Locations page") when blank ***

    if(strZip == "" || !ZipCodeReg.test(f.txtzipcode.value))
    {
        if(strZip == "")
        {
            f.action="/areas_we_serve/default.aspx"; //*** redirect("Locations page") when blank ***
        }
        else
        {
		    if(division[0] === "espanol") // division espanol
		    {
			    alert("Por favor coloque su código postal");
		    }
		    else
		    {
			    alert("Please enter a valid Zip Code");
		    }
        }
                
        return;
    }
	
	
	
    //if (f.chkRemember.checked){
        
        f.action="";
        f.action = setcookiepage + '?QueryString='+ f.txtzipcode.value + '&Remember=true';          
        f.submit(); 
        return; 
  }
    /*else{        
        f.action="";
        f.action = setcookiepage + '?QueryString='+ f.txtzipcode.value + '&Remember=false';           
        f.submit(); 
        return; 
        }*/


/**
 * This function reads a cookie on the user’s machine.
 * @param {string} name The name for the cookie
 * @returns The value of the cookie
 * @type String
 * @member CookieNS
 */

    function readCookie(name) {
		if(document.cookie == '') { // there's no cookie, so go no further			
			return false; 
		} else { // there is a cookie
			var firstChar, lastChar;
			var theBigCookie = document.cookie;
			firstChar = theBigCookie.indexOf("=");	// find the start of 'name'			
			var NN2Hack = firstChar + name.length;
			if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
				firstChar += name.length + 1; // skip 'name' and '='
				lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
				if(lastChar == -1) lastChar = theBigCookie.length;
				return unescape(theBigCookie.substring(firstChar, lastChar));
			} else { // If there was no cookie of that name, return false.
				return false;
			}
		}	
	}

/**
 * This function gets a cookie on the user’s machine.
 * @param {string} name The name for the cookie
 * @returns The value of the cookie
 * @type String
 * @member CookieNS
 */

	function GetCookie (name) {  
            var arg = name + "=";  
            var alen = arg.length;  
            var clen = document.cookie.length;  
            var i = 0;  
            while (i < clen) {    
            var j = i + alen;                
            if (document.cookie.substring(i, j) == arg)      
            	return getCookieVal(j);    
            i = document.cookie.indexOf(" ", i) + 1;    
            if (i == 0) break;   
            }  
            return null;
            }

	//checks the cookie and sends the user to his default subsite
	/**
 	 * This function gets the subsite cookie on the user’s machine.
     * This is used for redirects for the subsidiary. Redirects the page
	 * to the subsidiary.
 	 * @member CookieNS
	 */
	
	function checkCookie() {
	    var cook = readCookie('subsite');			    
		if (cook == null || cook == false) {
						;
		} else {
			document.location="http://"+cook;
			}
	}

	/**
	 * This function calls a Bright House page and submits the value of the Zip Code finder.
	 * It also checks to see if the zip code is in the proper format and does not submit
	 * the form until the requirement is met.
	 * @param {object} f The form Object
	 * @see setcookieHP is a similar function
	 * @member CookieNS
	 */
	 /**  Need rewrite for multiple divisions espanol **/
	function setcookieCB(f)
	{
	   var ZipCodeReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		if(!ZipCodeReg.test(f.zipCode.value))
		{
			if(division[0] === "espanol") // division espanol
			{
				alert("Por favor coloque su c"+ unescape('%f3') +"digo postal");
			}
			else
			{
				alert("Please enter a valid Zip Code");
			}
		  
			return;
		}else{
			f.action=setcookiepage + '?QueryString='+ f.zipCode.value + '&Remember=True';   
			f.submit();   
	   }
	   
	}
