var configuratorZipCodeCookie = "ZipCode";
var backToCookie = "backTo";
var zipcodeParamName="zp";
// SR 15704
var raqZipCodeCookie = "ZipCode";
// END OF SR 15704

function getModelBodyTrim() {
	var qs = new Querystring();
	var model = qs.get("mo");
	var trim = qs.get("tr");
	var bs = qs.get("bs");
	return "mo="+model+"&tr="+trim+"&bs="+bs;
}

function zipCodeHandler() {

  // new zipcode change 	
  if(isCookieEnabled())	
   {
	// window.location
    
	// if zip not set, redirect w/ backTo URL 
	var cookie = readCookie(configuratorZipCodeCookie);
	
	if (cookie == null ) {
		// redirect to handler page
		var baseUrl = location.pathname;
		var baseQS = location.search;
		var origUrl = baseUrl + baseQS;
		var newUrl = baseUrl + "?service=external/EnterZipCode&";
		newUrl += getModelBodyTrim() + "&backTo=" + escape(origUrl.replace("SummaryMQError","Summary").replace("&mqe=true",""));
        
		/** modified by kartik for SR 14776**/
		location.href = newUrl;
        /** end of modification SR 14776**/
	}
	// otherwise, do nothing
   }
   
   // if cookies are disabled then check the zipcode parameter value 
   else
   {
      var qs = new Querystring();	  
	  var zpParamValue = qs.get(zipcodeParamName);
	  var pageName =  "";
	  
	  var pageNameParam = qs.get("service");
	  
	  //alert("zipcodeHandler cookies disabled pageNameParam:"+pageNameParam+"  zpParamValue: " + zpParamValue);
	  
	  if((zpParamValue==null || zpParamValue=="") && (pageNameParam != null))
       {
	     // redirect to handler page
		var baseUrl = location.pathname;
		var baseQS = location.search;
		var origUrl = baseUrl + baseQS;
		var newUrl = baseUrl + "?service=external/EnterZipCode&";			
		newUrl += getModelBodyTrim() + "&backTo=" + escape(origUrl.replace("SummaryMQError","Summary").replace("&mqe=true",""));				
		location.href = newUrl;
	   }	   
   }
}

function disabledLinkItem() {
	return false;
}


function isCookieEnabled()
{
 	var cookieName = 'testCookie1-' + (new Date().getTime()); 
    createCookie(cookieName,"cookieValue",0); 
    var cookiesEnable = document.cookie.indexOf(cookieName) != -1;
	eraseCookie(cookieName);
	
	return cookiesEnable;
}


// cookie check 06/04/2009 MY
function checkCookieEnabled(){

	var cookieName = 'testCookie-' + (new Date().getTime()); 
    createCookie(cookieName,"cookieValue",0);
    var cookiesEnabled = document.cookie.indexOf(cookieName) != -1;
	eraseCookie(cookieName);
   
	if (cookiesEnabled) {		 
		createCookie(configuratorZipCodeCookie,document.getElementById("zipcode").value,365);
		document.Form0.submit();            
	}else {
		// new zipcode change 
		var qs = new Querystring();	
		var backToUrl = qs.get("backTo");
		//alert("backToUrl: " + backToUrl);
		document.Form0.backTo.value=backToUrl;
		document.Form0.submit();
		// alert("Your Cookie Setting is disabled, Please Enable.");			
	}
}


function checkZipCode( zipCodeInput ) {
	var validZip = /^[0-9]{5}$/;
	var buildImg = $("buildYourNissanImage");
	
	
	
	  
			  if( validZip.test(zipCodeInput.value) && zipCodeInput.value!='00000') {
				$("buildYourNissanItem").onclick = null;
				// $("requestAQuoteLinkItem").onclick = null;
		
				buildImg.src = buildImg.src.replace(/_off.gif/g,'.gif');
				buildImg.src = buildImg.src.replace(/_off_es.gif/g,'_es.gif');
				$("buildYourNissanAnchor").className = "";
				
				try {
					setOverSrc(buildImg);
					$("searchInventoryLinkItem").onclick = null;
					$("searchInventoryAnchor").className = "glArrow";
				} catch (error) {}
		
				var qs = new Querystring();
				
				if (qs && qs.get("backTo")) {
					createCookie("backTo", qs.get("backTo"));
				}
				
				$("backTo").value = readCookie("backTo");
		
				return true;
			}
			else {
				$("buildYourNissanItem").onclick = disabledLinkItem;
				// $("requestAQuoteLinkItem").onclick = disabledLinkItem;
		
				if (buildImg.src.indexOf('_on')!=-1) {
					buildImg.src = buildImg.src.replace(/(_on\.gif|_on_es\.gif)/ig,'.gif');
				}
				if (buildImg.src.indexOf('_off')==-1) {
					if (buildImg.src.indexOf('_es') > 0) buildImg.src = buildImg.src.replace(/_es.gif/g,'_off_es.gif');
					else buildImg.src = buildImg.src.replace(/.gif/g,'_off.gif');
				}
				$("buildYourNissanAnchor").className = "disabled";
				// $("requestAQuoteAnchor").className = "disabled";
				
				try {
					buildImg.onmouseover = null;
					buildImg.onmouseout = null;
		
					$("searchInventoryLinkItem").onclick = disabledLinkItem;
					$("searchInventoryAnchor").className = "disabled";
				} catch (error) {}
		
				return false;
			}
		
	  
	  
	

}	

function createCookie(name,value,days)
{
	//alert(name+" cookie created");

	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);
}

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

var zipcode = readCookie(configuratorZipCodeCookie);

function appendZip( elem ) {

	if( elem.href.indexOf( "&zipcode=" ) >= 0 ) return;
    
   if( !isCookieEnabled() )
   {      
      var qs = new Querystring();
	  if(qs.get(zipcodeParamName)!=null)
	   zipcode = qs.get(zipcodeParamName);
   }
	
   elem.href += "&zipcode=" + zipcode;
   
}

function appendPageName( elem ) {

	if( elem.href.indexOf( "&pp=" ) >= 0 ) return;

	elem.href += "&pp=" + currentPageName;
}


function appendStateToLinks() {

 
	if( zipcode ) 
	{
	
		if ( $("tab5") ) 		appendZip( $("tab5") );
		
				
		if ( $("raqlink") ) 	
		{
			appendZip( $("raqlink") );
			appendPageName($("raqlink"));
		}
		if ( $("summarylink1") ) appendZip( $("summarylink1") );
		if ( $("summarylink2") ) appendZip( $("summarylink2") );
	}

}