
//test cookie
var expDays = 90; // number of days the cookie should last

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;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('cjCookie')
if(count == null) {
SetCookie('cjCookie','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('cjCookie')
SetCookie('cjCookie',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
	var count = GetCookie('cjCookie');
	if (count == null){		//first time visitor
		if (getURLParam('PID') != ""){		//if PID exists
			count = getURLParam('PID');		//set as "cookie"
			SetCookie('cjCookie', count, exp);
		}
		else{
			//count = "";  //if no PID exists
			//SetCookie('cjCookie', count, exp);  //set as "cookie" w/ no PID value
		}
	}
	else{					//return visitor, check PID
		if (!getURLParam('PID')){  
			//if no PID, redirect to page and use their cookie from before to continue tracking
			window.location = "http://www.comcastspecial.com/index.html?PID=" + GetCookie('cjCookie');
		}
		else{
			//PID exists, update cookie to show new PID
			count = getURLParam('PID');		//set as "cookie"
			SetCookie('cjCookie', count, exp);	
		}
	}
}

//tracking pages
 
  function hdtv(){
 	var ht = window.open("http://www.pricecheater.com/index.php?PID=" + getURLParam('PID'), "_blank");
 }
  function gaming(){
 	var game = window.open("http://www.yourgamingexperience.com/index.html?PID=" + getURLParam('PID'), "_blank");
 }
  function acer(){
 	var ht = window.open("http://www.acerspecial.com/index.php?PID=" + getURLParam('PID'), "_blank");
 }
  function moving(){
 	var mo = window.open("http://comcastspecial.com/moving/index.php?PID=" + getURLParam('PID'), "_blank");
 }
 
 //old linkage function (new at bottom)
 function linkageOld(thelink, windowopen, specialprop){
	   if(specialprop == "true"){
		   var tl = window.open(thelink + "?PID=" + getURLParam('PID'), windowopen, 'top=10,left=20,scrollbars=yes,width=575,height=480,menubar=no,toolbar=no,status=no,location=no;resizable=yes;');
	   }
	   else{
		   if(windowopen == null){
			var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), '_self');
			exit = false;
		   }
		   else{
			var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), windowopen); 
		   }
	   }
 }

//two bookmarking functions
function favoris() {
	if( navigator.appName != 'Microsoft Internet Explorer' ){
		window.sidebar.addPanel(document.title,"http://www.comcastspecial.com/index.html?PID=" + getURLParam('PID'),"");
	}
	else{
		window.external.AddFavorite("http://www.comcastspecial.com/index.html?PID=" + getURLParam('PID'),document.title);
	}
}

function writeLink(){
    var ua=navigator.userAgent.toLowerCase();
    var isKonq=(ua.indexOf('konqueror')!=-1);
    var isSafari=(ua.indexOf('webkit')!=-1);
    var isMac=(ua.indexOf('mac')!=-1);
    var buttonStr=isMac?'Command/Cmd':'CTRL';

    if(window.external && (!document.createTextNode ||
      (typeof(window.external.AddFavorite)=='unknown'))) {
		document.write("<a href='javascript:void(favoris());'>Bookmark Us</a>"); //IE6+
    } else if(navigator.userAgent.indexOf("Firefox")!=-1) {
      document.write("<a href='javascript:void(favoris());'>Bookmark Us</a>") // Firefox
    } else if(window.opera) {
      document.write("<a href='http://www.comcastspecial.com' rel='sidebar' title='Comcast High Speed Internet'>Bookmark Us</a>") // Opera 7+, this loses tracking tho
    } else{
/*		if(window.home || isSafari) { // Firefox, Netscape, Safari, iCab
      document.write("You need to return to the home page and press "+buttonStr+" + D to bookmark our site.")
    } else if(!window.print || isMac) { // IE5/Mac and Safari 1.0
      document.write("You need to press Command/Cmd + D to bookmark our site.")    
    } else {
      document.write("In order to bookmark this site you need to do so manually through your browser.")*/
    }
}
 
 //radio buttons
/*function checkradioHome()
{
	document.form1.rbhome.checked=true
	document.form1.rbbusiness.checked=false
	document.getElementById('hidethis').style.display = '';
	document.getElementById('hidethis2').style.display = '';
	document.getElementById('hidethis3').style.display = '';
	document.getElementById('require').style.display = '';
	document.getElementById('contactstuff').style.display = 'none';
	document.getElementById('contactstuff2').style.display = 'none';
	document.getElementById('resiEm').style.display = '';
	document.getElementById('bizEm').style.display = 'none';
	document.form1.email.style.fontWeight = 'bold';
}

function checkradioBusiness()
{
	document.form1.rbhome.checked=false
	document.form1.rbbusiness.checked=true
	document.getElementById('hidethis').style.display = 'none';
	document.getElementById('hidethis2').style.display = 'none';
	document.getElementById('hidethis3').style.display = 'none';
	document.getElementById('require').style.display = 'none';
	document.getElementById('contactstuff').style.display = '';
	document.getElementById('contactstuff2').style.display = '';
	document.getElementById('resiEm').style.display = 'none';
	document.getElementById('bizEm').style.display = '';
	document.form1.email.style.fontWeight = '';
}*/

//PID stuffies
function getURLParam( name ){

  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function staticLoadScript(url)
{
   document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
}

function SetPartnerID(str)
{
	var partner_id
    if ( getURLParam('id') != '' ){
	    partner_id = getURLParam('id');
    }
    else{
	    partner_id = str;
    }
    if ( getURLParam('PID') != '' ){
	    if (partner_id.indexOf("-") > -1) {
	        partner_id = partner_id + ':' + getURLParam('PID')
        }
        else
        {
	        partner_id = partner_id + '-' + getURLParam('PID')
        }
    }
    return partner_id
}

function checkVal1(newPID){
	var pidSplit = newPID.split(':');

	return pidSplit[1];
}

function checkVal2(newPID){
	var pidSplit = newPID.split(':');

	return pidSplit[0];
}

function checkVal3(newPID){
	var pidSplit = newPID.split(':');

	return pidSplit[2];
}

function checkVal4(newPID){
	var pidSplit = newPID.split(':');

	return pidSplit[3];
}

//Get Referring URL Domain
function GetRefDomain()
{  
  var referrer = GetReferringPage();
  if (!referrer) {
    return false;
  }
  
  var dashLoc = referrer.indexOf('.');                    //grab the engine from the URL
  var dashLoc2;
  var front;                                                    
  var rest;
  var refDir;
  var len = referrer.length;
  if (dashLoc > 0){
	front = referrer.substr(0, dashLoc);
	rest = referrer.substr(dashLoc + 1, len);
	dashLoc2 = rest.indexOf('.');
	if (dashLoc2 > 0){
		refDir = rest.substr(0, dashLoc2);
	}
  }                                                      //end of said grabbing
  
  /*var queryPrefix = DeterminPreFix(referrer);
  alert("referrer is: " + referrer + "\nqueryPrefix is: " + queryPrefix);
  if (!queryPrefix)
  {
  	return false;
  }
  
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  
  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }
  
  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);

  queryString = queryString.replace(/(%22)(.*?)(%22)/gi, function(x,y,z) {return z.replace(/\+/gi," ")});
  queryString = queryString.replace(/(\")(.*?)(\")/gi, function(x,y,z) {return z.replace(/\+/gi," ")});
  
  queryString = queryString.replace(/%20/gi, ", ");
  queryString = queryString.replace(/\+/gi, ", ");

  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  
  queryString = queryString.replace(/%28/gi, "");
  queryString = queryString.replace(/\(/gi, "");
  
  queryString = queryString.replace(/%29/gi, "");
  queryString = queryString.replace(/\)/gi, "");*/
  
  return refDir;
}

/*function DeterminPreFix(referrerString)
{
      
  if (!referrerString) {
  	return false;
  }
  // used by google, Live (msn), Ask (google), altavista (yahoo), iwon (google)
  else if (referrerString.indexOf("q=") >= 0) 
  {
  	return "q=";
  }
  // used by yahoo,
  else if (referrerString.indexOf("p=") >= 0) 
  {
  	return "p=";
  }
  // used by AOL (google), Netscape (google), Lycos (google)
  else if (referrerString.indexOf("query=") >= 0) 
  {
  	return "query=";
  }
  // dmoz (google)
  else if (referrerString.indexOf("search=") >= 0) 
  {
  	return "search=";
  }
  else
  {
  	return false;
  }
}*/


function DeterminReferringDomain()
{
  var referrerString = GetReferringPage();

  if (!referrerString) {
    return false;
  }
  
  var strSplit = referrerString.split("/");
  
  if (strSplit[2].length == 0) 
  {
  	return "Unknown";
  }
  else
  {
  	return strSplit[2];
  }
  
}

function GetReferringPage()
{
  var referrerString = document.referrer.toLowerCase();

  // Test Values:
  //var referrerString = "http://www.complaints.com/directory/2006/march/21/23.htm";

  return referrerString;
}

//for Badal designed split test
function openQual(){

	document.getElementById('str_overlay').style.display = 'block';
	document.getElementById('msg').style.display = 'block';
	document.getElementById('flash').style.display = 'none';
	
}

function closeQual(){

	document.getElementById('str_overlay').style.display = 'none';
	document.getElementById('msg').style.display = 'none';
	document.getElementById('flash').style.display = '';
	
}

//get subdirectory - tracking for SEO city/state
function getSub(){

	var winLoc = window.location.pathname;
	var subFolder = winLoc.substring(0, winLoc.lastIndexOf('/'));

	if(subFolder == "/comcast"){			//if subdirecty exists
		if(getURLParam('PID') != ''){		//has PID already, don't want to overwrite
			return "None";
		}
		else{								//with no PID
			//alert(window.location.pathname);
			if(window.location.pathname == "/comcast/" || window.location.pathname == "/comcast/index.html"){
				return "citystateindex";
			}
			else{
				var ourCityState = window.location.pathname;
				var stripOut = ourCityState.substring(9, ourCityState.indexOf('.'));
				return stripOut;
			}			
		}	
	}
	else{
		return "None";	
	}
	
}

//get search engine - tracking for SEO city/state
var sEngine2;  //global to allow use in linkage function
function getEngine(){
	if(GetSearchTerms()){
	  var TermsExist2 = GetSearchTerms();
	  TermsExist2 = TermsExist2.replaceAll(", ", "_");
  
	  var dashLoc2 = TermsExist2.indexOf(':');                                                                             
	  var sTerm2;
	  var len2 = TermsExist2.length;
	  if (dashLoc2 > 0){
		  sEngine2 = TermsExist2.substr(0, dashLoc2);
		  sTerm2= TermsExist2.substr(dashLoc2 + 1, len2);
	  }
	  
	  return sEngine2;
	}
	else{
		return "None";	
	}
}

//adjusted linkage function to allow creation/passing of SEO city/state
function linkage(thelink, windowopen, specialprop){
	 if(specialprop == "true"){
		 var tl = window.open(thelink + "?PID=" + getURLParam('PID'), windowopen, 'top=10,left=20,scrollbars=yes,width=650,height=480,menubar=no,toolbar=no,status=no,location=no;resizable=yes;');
	 }
	 else{
		 if(windowopen == null){
			if(getURLParam('PID') != ''){ 	//PID exists, pass away!
				if(getURLParam('adused') != ''){
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID') + "&adused=" + getURLParam('adused'), '_self');
				}
				else if(getURLParam('AdID') != ''){
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID') + "&adused=" + getURLParam('AdID'), '_self');
				}
				else{
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), '_self');
				}
				//alert("pid exists!");
		 		exit = false;
			}
		 	else{
				var winLoc = window.location.pathname;
				var subFolder = winLoc.substring(0, winLoc.lastIndexOf('/'));
				
				if(subFolder == "/comcast"){	//no pid and the city/state subfolder exists
					//alert("in subfolder");
					//alert(sEngine2);
					if(!sEngine2){
						var t2 = window.open(thelink + "?PID=", '_self');
		 				exit = false;
					}
					else{
						var t2 = window.open(thelink + "?PID=" + sEngine2 + ":" + getSub() +"::natural", '_self');
						exit = false;
					}
				}
				else{	//no pid and city/state subfolder doesn't exist
					//alert("no subfolder");
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), '_self');
		 			exit = false;
				}
			}
		 }
		 else{
			if(getURLParam('PID') != ''){	//PID exists, pass away!
				if(getURLParam('adused') != ''){
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID') + "&adused=" + getURLParam('adused'), windowopen);
				}
				else if(getURLParam('AdID') != ''){
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID') + "&adused=" + getURLParam('AdID'), windowopen);
				}
				else{
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), windowopen);
				} 
		 		exit = false;
			}
		 	else{
				var winLoc = window.location.pathname;
				var subFolder = winLoc.substring(0, winLoc.lastIndexOf('/'));
				
				if(subFolder == "/comcast"){	//no pid and the city/state subfolder exists
					if(!sEngine2){
						var t2 = window.open(thelink + "?PID=", '_self');
		 				exit = false;
					}
					else{
						var t2 = window.open(thelink + "?PID=" + sEngine2 + ":" + getSub() +"::natural", windowopen);
						exit = false;
					}
				}
				else{	//no pid and city/state subfolder doesn't exist
					var t2 = window.open(thelink + "?PID=" + getURLParam('PID'), windowopen); 
		 			exit = false;
				}
			}
		 }
	 }
}

//for new system
function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
	//create UserGUID in lead database
	$(document).ready(function(){
		$.post("http://www.comcastspecial.com/buyflow/proxy2.php?proxy_url=http://www.bandwidthnational.com/cw/api/lead_api_create.php", {}, function(text){
			
			/*if(text.xml) {
				text=(text.xml);   // XML to String  for IE
			}else{
				text=(new XMLSerializer().serializeToString(text));  // XML to String for Firefox,etc browsers
			}*/
			//alert(text);
			createCookie("UserGUID", text, 1); 	
			uGUID = readCookie("UserGUID");
			document.IndexForm.userId.value = text;					
		});
	});
   //return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function createCookie(name,value,days) {
	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=/";
}

function readCookie(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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*var uGUID = readCookie("UserGUID");
if (uGUID == null){
	guid();
}
else{
	
	//use GUID to return previous lead info, if exists	
	$(document).ready(function(){
		$.post("http://www.comcastspecial.com/buyflow/proxy2.php?proxy_url=http://www.bandwidthnational.com/cw/api/lead_api_fetch.php", {UserID: uGUID}, function(text){
			var splitText = text.split(";");
			
			if(splitText.length != 0){
				document.IndexForm.txtHomeStreetAddress1.value = splitText[1];
				document.IndexForm.txtHomeStreetAddress2.value = splitText[2];
				if(splitText[3] == 0){
					
				}
				else{
					document.IndexForm.txtHomeZip.value = splitText[3];
				}
				document.IndexForm.txtHomeEmailId.value = splitText[4];
				if(splitText[5] != 0){
					document.IndexForm.txtHomePhone1.value = splitText[5];
					document.IndexForm.txtHomePhone2.value = splitText[6];
					document.IndexForm.txtHomePhone3.value = splitText[7];
				}
				//add phone...split phone fields up
			}
			else{
				//alert("Empty array.");	
			}
			
			document.IndexForm.userId.value = uGUID;
		});
	});
}*/