var currURL = unescape(window.location.href);
var currREF = unescape(document.referrer); //Added 12-15-05 
var thisDay = new Date();
var fullYear = thisDay.getFullYear();

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 getQueryStringParam(pName)
{
// This code will pull any parameter value from the URL/QueryString
// Returns the Parameter Value if Found
// Returns Empty String if Param not Found or pName is ""
	var queryString = location.search;
	var startPos = 0;
	var stopPos = 0;
	var tempPos = 0;
	var pValue = "";
	
	if (pName) 
	{				
		if(queryString.indexOf(pName) != -1)
		{
			//Parameter Found - Get Parameter Value
			startPos = queryString.indexOf(pName) + pName.length + 1;
			stopPos = ((tempPos = (queryString.indexOf("&", startPos))) == -1) ? queryString.length : tempPos;
			pValue = queryString.substring(startPos, stopPos);
			pValue = unescape(pValue);
		}
	}
	return pValue;	
}

//var xmlDoc; //assumed to exist
//Used for populating without invoking event 
function loadXmlDocument(xmlFile, cbFun)
{
   if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
   {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
      xmlDoc.load(xmlFile);
      cbFun(xmlDoc);
      return true;
   }
   else if (document.implementation!='undefined' && document.implementation.createDocument!='undefined')
   {
      xmlDoc= document.implementation.createDocument("", "", null);
      xmlDoc.load(xmlFile);
      xmlDoc.onload = function()
      {
         cbFun(xmlDoc);
      }
      return true;
   }
   else
   {
      return false;
   }
}


var cookieStr=getCookie('attPersistantLocalization');
//window.alert("att cookie: "+cookieStr);
// goToCityState cookie if "yes" overrides the personlaizaiton cookie
// and causes the city/state page to appear
//this cookie is set to "yes" if the user clicks "change" on one of the marketing pages.
var goToCityState=getCookie('goToCityState');
var xmlDoc = "";
stateAbbrev=new Array();
stateAbbrev['Alabama']="AL";
stateAbbrev['Florida']="FL";
stateAbbrev['Georgia']="GA";
stateAbbrev['Kentucky']="KY";
stateAbbrev['Louisiana']="LA";
stateAbbrev['Mississippi']="MS";
stateAbbrev['North Carolina']="NC";
stateAbbrev['South Carolina']="SC";
stateAbbrev['Tennessee']="TN";
//cookie stuff
var expires = "";
var path ="/";

if (currURL.indexOf('iocteamsite.bls.com') > -1) {
var BSdomain = ".bls.com";
} else if (currURL.indexOf('localhost') > -1) {
var BSdomain = "";
} 
else if (currURL.indexOf('ourbellsouth.com') > -1) {
var BSdomain = ".ourbellsouth.com";
}
else if (currURL.indexOf('90.106.3.109') > -1) {
var BSdomain = "90.106.3.109";
} else {
var BSdomain = ".bellsouth.com";
}
var secure = "";
var cookiestuff = ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((BSdomain) ? "; domain=" + BSdomain : "") +
  ((secure) ? "; secure" : "");
if (cookieStr!=null)
{
    if ((goToCityState==null) || (goToCityState=="") || (goToCityState=="no")) 
    {
	var cltState="";
	var cltCity="";
	var cltNode="";
	//window.alert("clt cookie: "+cookieStr);
	re=/state=(.+?)(\||$)/;
	if (re.test(cookieStr))
	{
	    cltState=RegExp.$1;
	    if ((cltState!=null)&&(cltState!=""))
	    {
		if ((stateAbbrev[cltState]!=null)&&(stateAbbrev[cltState]!=""))
		{
		   cltState=stateAbbrev[cltState];
		}
		else
		{
		    cltState="";
		}
	    } 
	}//test        
	if ((cltState!=null)&&(cltState!=""))
	{
	   re=/city=(.+?)(\||$)/;        
	   if (re.test(cookieStr))
	   {
		cltCity=RegExp.$1; 
	   }       
	    //name kludges
	   //window.alert("cltState: "+cltState+"  cltCity: "+cltCity);
	   //get nodenumber
	   loadXmlDocument("/consumer/xml/" + cltState + ".xml", getCLTData);
	} //city test
    }//goToCityState
    else
    {
	//goToCityState==yes citystate cookie was wiped out when we set thias cookie and refreshed page in city_state var in popProducts.js
	//set to "no" for future visits
	setChangeLocale('no');
    }
}//cookies    	
function getCLTData()
{
    var hit=false;
    citiesList = xmlDoc.getElementsByTagName("city");
    for (var x=0;x<citiesList.length;x++)
    {
	var tCity = citiesList[x].getElementsByTagName("cityname")[0].firstChild.nodeValue;
	//window.alert(tCity+":"+cltCity);
	if (tCity==cltCity)
	{
	    hit=true;
	    cltNode=citiesList[x].getElementsByTagName("nodenumber")[0].firstChild.nodeValue;
	    setCityState(cltNode, cltState, cltCity)
	    //window.alert(tCity+":"+cltState+":"+cltNode);
	    break;
	}
    }
    if (!hit)
    {
	    cltNode=citiesList.length-1;
	    setCityState(cltNode, cltState, 'Other')
	    //window.alert("Other:"+cltState+":"+cltNode);
    }
}
function setCityState(citystate_node, citystate_state, citystate_city)
{
    var cltCityState_string = "";
    cltCityState_string = cltCityState_string + "xxx" + citystate_node + "xxx" + citystate_state + "xxx" + citystate_city;
    document.cookie = "citystate=" + escape(cltCityState_string) + cookiestuff;
}
function setChangeLocale(tType)
{
    //type is 'yes' or 'no'
    document.cookie = "goToCityState=" + escape(tType) + cookiestuff;
}

function showIt(theElement) { document.getElementById(theElement).style.display = 'block'; }
function hideIt(theElement) { document.getElementById(theElement).style.display = 'none'; }
function writeIt(theElement,theCopy) { document.getElementById(theElement).innerHTML = theCopy; }

function loadMyScript(url)
{
   var theScript = document.createElement("script");
   theScript.src = url;
   theScript.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(theScript);
}
