popup = 
{
 load : function(code)
 {	
  	var cookieValue = readCookie("postalCode");
 	if(!cookieValue || code==0)
	{
		popup.init(code);
	}
 },
 init : function(code) 
 {
	YAHOO.namespace("popup");
	
	if (!YAHOO.popup.zipcode) 
	{
		// Initialize the temporary Panel to display while waiting for external content to load

		YAHOO.popup.zipcode = 
				  new YAHOO.widget.Panel("zipcode",  
															 { width: "250px"
															 	,height:"280px"
																,fixedcenter: true
																,close: false
																,draggable: false
																,zindex:99
																,modal: true
																,visible: false
															 } 
												);
		YAHOO.popup.zipcode.setBody(getZipCodePopupHTML());
		YAHOO.popup.zipcode.render(document.body);
	}

	YAHOO.popup.zipcode.body.className = "";
	
	var cookieValue = readCookie("postalCode");
		if(cookieValue)
			document.getElementById("txtZipCode").value = cookieValue;
		else
			document.getElementById("txtZipCode").value ="";

	YAHOO.popup.zipcode.show();
	
	if(code==0)
	{
		if(window.ActiveXObject)
			document.getElementById("lblErrormessage").innerText="Please enter a valid zipcode";
		else
			document.getElementById("lblErrormessage").textContent="Please enter a valid zipcode";
		eraseCookie("postalCode");
		document.getElementById("txtZipCode").value="";
	}
 },
 popupClose: function()
 {
 	YAHOO.popup.zipcode.hide();
 }
} 


function getZipCodePopupHTML()
{
return  '<div id="geoLocatorBackgrd" class="geoLocatorBackgrd">\n'
		+ '	<form name="zipForm" id="zipForm" onsubmit="javascript:validateZipCode();return false;" method="post" style="z-index:99">\n'
		+ '		<div id="geoLocatorHeader" class="geoLocatorHeader">\n'
		+ '			<div id="geoLocatorHeaderText" class="geoLocatorHeaderText">\n'
		+ '				Choose Your City\n'
		+ '			</div>\n'
		+ '		</div>\n'
		+ '		<div id="geoLocatorRedLine" class="geoLocatorRedLine"></div>\n'
		+ '		<div id="geoLocatorText" class="geoLocatorText">\n'
		+ '			Enter your zip code to view offers in your area.\n'
		+ '		</div>\n'
		+ '		<div id="lblErrormessage" class="geoLocatorErrText"></div>\n'
		+ '		<div id="geoLocatorZipCode" class="geoLocatorZipCode">Zip Code</div>\n'
		+ '		<div id="geoLocatorZipCodeTxtbox" class="geoLocatorZipCodeTxtbox">\n'
		+ '			<input id="txtZipCode" maxLength="5" style="height:13px;width:80px" type="text"/>\n'
		+ '		</div>\n'
		+ '		<div id="geoLocatorSubmit" class="geoLocatorSubmit">\n'
		+ '			<input type="image" src="'+getWebRoot()+'/images/GeoLocator/SubmitButton.jpg"/ title="Submit">\n'
		+ '		</div>\n'
		+ '		<div id="geoLocatorDottedLine" class="geoLocatorDottedLine"></div>\n'
		+ '		<div id="geoLocatorCloseButton" class="geoLocatorCloseButton">\n'
		+ '			<a href="#" onclick="popup.popupClose();return false;"><img src="'+getWebRoot()+'/images/GeoLocator/CloseButton.jpg" style="border:0px" title="Close"/></a>\n'
		+ '		</div>\n'
		+ '	</form>\n'
		+ '</div>\n'
}

function validateZipCode()
{
	var zipFrmCtrl=document.getElementById("txtZipCode");
	if(zipFrmCtrl==null) return;

	if (/^\d{5}$/.test(zipFrmCtrl.value))
	{
		if(zipFrmCtrl.value=="00000")
		{
			if(window.ActiveXObject)
				document.getElementById("lblErrormessage").innerText="Please enter your valid five-digit zip code.";
			else
				document.getElementById("lblErrormessage").textContent="Please enter your valid five-digit zip code.";
			eraseCookie("postalCode");
			document.getElementById("txtZipCode").value="";
		}
		else
		{
			if(window.ActiveXObject)
				document.getElementById("lblErrormessage").innerText="";
			else
				document.getElementById("lblErrormessage").textContent="";

			createCookie("postalCode",zipFrmCtrl.value,30); // Creating cookie.
			parent.popup.popupClose();
			document.location.reload();
		}
	}
	else
	{
		if(window.ActiveXObject)
			document.getElementById("lblErrormessage").innerText="Please enter your five-digit zip code.";
		else
			document.getElementById("lblErrormessage").textContent="Please enter your five-digit zip code.";
		eraseCookie("postalCode");
		document.getElementById("txtZipCode").value="";
	}
}

function createCookie(name,value,days) 
{
	var expires = "; expires="; 
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires += date.toGMTString();
	}
	
  var cookie_string = name+"="+value;
  cookie_string += expires;
  cookie_string += ";path=/";
  cookie_string += "; domain=" + escape (document.domain);
  document.cookie = cookie_string;
}

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);
}

 function getall(x)
 {
 	var s="";
 	var keys = new Array();
 	var count=0;
 	if (x==null)
 		s+="NULL Data\n";
 
 	for(var i in x)
 		if (i!="innerHTML" && i!="outerHTML")
 			keys[count++]=i;
 
 	keys.sort();
 
 	for(i in keys)
 		s += keys[i] + " = "+ x[keys[i]] + "\n";
 
 	alert(s);
 }

