/*==== this function allows the "enter" key to submit search ====*/
function SubmitSearch(e){    
    if (isEnterKey(e)){            
      Search();
      return false;
    }    
}

/*==== this function allows the "enter" key to Submit SignUpForDeals in footer ====*/
function SubmitSignUpForDeals(e){    
    if (isEnterKey(e)){            
      SignUpForDeals();
      return false;
    }    
}

/*==== show header welcome message ====*/
function showWelcome(){    
    if (getSession('IsRegistered') == '1' && getSession('FirstName') != '' && getSession('LastName') != ''){
        $('#header-welcome-message').css('display','inline');                            
        $('#header-welcome-message').text('Welcome, ' + escape(getSession('FirstName')) + ' ' + escape(getSession('LastName')) + '!');
    }   
}

/*==== checks is query string has "r" and "pc" parameters after postal has been submitted and raises an error (zipoverlay) in case of error ====*/
function checkPostalCode(){
    if (getQueryStringItem("r") == 'failure' && getQueryStringItem("pc") != ''){
        window.setTimeout("showModalZipOverlay()", 60);                    
        $('#ZipBlocking_ErrorMessage').css('visibility','visible');                                        
    }
}

/*==== displays "change zip" and hides "enter zip" form ====*/
function displayChangeZipForm(){ 
    var ziptype = getSession('ZipType');        
    if (ziptype !='' && getSession('DefaultGeography') == '0'){    
        $('#enter-zip-code').css('display','none');             
        $('#change-zip-code').css('display','inline');
        if (ziptype == 'i'){    
            $('#localized-for-zip').css('display', 'inline');        
            if (getSession('VirtualZip') != ""){
                $('#pcikup-only-location').css('display', 'inline');
                $('#zipcode').text(getSession('VirtualZip'));       // virtual zip code
                $('#deliverable-showroom').css('display','none');     // deliverable showroom        
            }else{
                $('#zipcode').text(getSession('Zip')); 
                $('#deliverable-showroom').css('display','inline');     // deliverable showroom                    
            }
        }else if (ziptype == 'u'){
            $('#shopping-in-zip').css('display', 'inline');    
            $('#zipcode').text(getSession('Zip'));       // zip code
            $('#pcikup-only-location').css('display','none');       // 'pickup only' location is not visible
            $('#deliverable-showroom').css('display','inline');     // deliverable showroom        
        }else if (ziptype == 'v'){
            $('#shopping-in-zip').css('display', 'inline');
            $('#zipcode').text(getSession('VirtualZip'));// zip code  
            $('#pcikup-only-location').css('display','inline');     // pickup only locations
            $('#deliverable-showroom').css('display','none');       // deliverable showroom is hidden  
        }     
    }else{
        $('#enter-zip-code').css('display','inline');
        $('#change-zip-code').css('display','none');
    }
}

/*==== displays "enter zip" form and hides "change zip" ====*/
function displayEnterZipForm(){
    if (getSession('ZipType') == 'i'){
        showModalZipOverlay();
    }else{
        $('#change-zip-code').css('display','none');
        $('#enter-zip-code').css('display','inline');
        $('#zip-code').css('display','none');
    }
}

/*==== disabling form ====*/
function DisableZipCodeForm(oTextBox){                        
    var oZipCode = $('#'+oTextBox)[0];           
    if (oZipCode && oZipCode.value != ''){                            
        disableOverlayPostalCodeForm();
        disableHeaderPostalCodeForm();
        $("#HiddenPostalCode").attr('value', oZipCode.value);        
        $('#aspnetForm').submit();                            
    }                        
}

/*==== this function allows the "enter" key to submit postal code ====*/
function SubmitPostalCode(e, sender){
    if (isEnterKey(e)){             
      DisableZipCodeForm(sender);
      return false;
    }    
}

/*==== returns "true" if a keypress was "enter" key ====*/
function isEnterKey(e){
   var key;        
    if(window.event){
       key = window.event.keyCode; // IE
    }else{
       key = e.which; //Firefox      
    }        
    if(key == 13){                  
      return true;
    }else{
      return false
    }
}

/*==== customer links ====*/            
function showCustomerLinks(){
    if(getSession('IsSignedOn') == '1'){
        $('#LeftLink').attr('href',linkAccount);
        $('#LeftLink').text(linkAccountText);
        $('#RightLink').attr('href',linkLogout);
        $('#RightLink').text(linkLogoutText);
    }else{
        $('#LeftLink').attr('href',linkRegistration);
        $('#LeftLink').text(linkRegistrationText);
        $('#RightLink').attr('href',linkLogin);
        $('#RightLink').text(linkLoginText);
    }
}

/*==== select appropriet search menu ====*/
function selectSearchMenu(){
    var categoryid = queryString("categoryid");                
    var arearugs = queryString("eventid");
    if(categoryid != 'false'){        
        switch(categoryid)
        {
            case '32169':   // carpet
                $('#CarpetSearchLink').removeClass("header-product-link");
                $('#CarpetSearchLink').removeClass("float-left");
                $('#CarpetSearchLink').addClass("header-product-link-selected");
                break;
            case '32173':   // hardwood
                $('#HardwoodSearchLink').removeClass("header-product-link");
                $('#HardwoodSearchLink').removeClass("float-left");
                $('#HardwoodSearchLink').addClass("header-product-link-selected");
                break;                        
            case '32172':   // laminate
                $('#LaminateSearchLink').removeClass("header-product-link");
                $('#LaminateSearchLink').removeClass("float-left");
                $('#LaminateSearchLink').addClass("header-product-link-selected");
                break;
            case '44421':   // vinyl
                 $('#VinylSearchLink').removeClass("header-product-link");
                $('#VinylSearchLink').removeClass("float-left");
                $('#VinylSearchLink').addClass("header-product-link-selected");
                break;
            case '32170':   // tile
                $('#TileSearchLink').removeClass("header-product-link");
                $('#TileSearchLink').removeClass("float-left");
                $('#TileSearchLink').addClass("header-product-link-selected");
                break;
            case '34040':        // area rugs
                $('#RugsSearchLink').removeClass("header-product-link");
                $('#RugsSearchLink').removeClass("float-left");
                $('#RugsSearchLink').addClass("header-product-link-selected");
                break;
        }
    }else if (arearugs != 'false' && arearugs == '211'){
        // select area rugs if it's a custom page                          
        $('#RugsSearchLink').addClass("header-product-link-selected");
    }else{                                         
        switch(window.location.toString().toLowerCase())
        {
            case linkCarpet:
                $('#CarpetSearchLink').removeClass("header-product-link");
                $('#CarpetSearchLink').removeClass("float-left");
                $('#CarpetSearchLink').addClass("header-product-link-selected");
                break;
            case linkHardwood:                
                $('#HardwoodSearchLink').removeClass("header-product-link");
                $('#HardwoodSearchLink').removeClass("float-left");
                $('#HardwoodSearchLink').addClass("header-product-link-selected");
                break;
            case linkLaminate:
                $('#LaminateSearchLink').removeClass("header-product-link");
                $('#LaminateSearchLink').removeClass("float-left");
                $('#LaminateSearchLink').addClass("header-product-link-selected");
                break;
            case linkVinyl:
                $('#VinylSearchLink').removeClass("header-product-link");
                $('#VinylSearchLink').removeClass("float-left");
                $('#VinylSearchLink').addClass("header-product-link-selected");
                break;
            case linkTile:
                $('#TileSearchLink').removeClass("header-product-link");
                $('#TileSearchLink').removeClass("float-left");
                $('#TileSearchLink').addClass("header-product-link-selected");
            case linkRugs:
                $('#RugsSearchLink').removeClass("header-product-link");
                $('#RugsSearchLink').removeClass("float-left");
                $('#RugsSearchLink').addClass("header-product-link-selected");
                break;                        
        }               
    }
}

/*==== assigns navigate url to search menu links ====*/
function searchLinks(){                
    $('#CarpetSearchLink').attr('href',linkCarpet); // carpet
    $('#HardwoodSearchLink').attr('href',linkHardwood); // hardwood
    $('#LaminateSearchLink').attr('href',linkLaminate); // laminate
    $('#VinylSearchLink').attr('href',linkVinyl); // vinyl
    $('#TileSearchLink').attr('href',linkTile); // tile
    $('#RugsSearchLink').attr('href',linkRugs); // area rugs
    $('#SaleCenterLink').attr('href',linkSaleCenter); // sale center                
    selectSearchMenu();
}

/*==== assigns navigate url to search meny links using zip blocking ====*/
function searchLinksBlocked(){
    $('#CarpetSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkCarpet + "');showModalZipOverlay();"); // carpet
    $('#HardwoodSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkHardwood + "');showModalZipOverlay();"); // hardwood
    $('#LaminateSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkLaminate + "');showModalZipOverlay();"); // laminate
    $('#VinylSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkVinyl + "');showModalZipOverlay();"); // vinyl
    $('#TileSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkTile + "');showModalZipOverlay();"); // tile
    $('#RugsSearchLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkRugs + "');showModalZipOverlay();"); // area rugs
    $('#SaleCenterLink').attr('href',"javascript:set_session_cookie('curUrl','" + linkSaleCenter + "');showModalZipOverlay();"); // sale center
    selectSearchMenu();
}

/*==== free text search ====*/ 
function Search(){            
    if ($('#SearchTextBox') && $('#SearchTextBox').val() != '' && $('#SearchTextBox').val() != 'Search by keyword or SKU'){
        window.location.href = linkSearchResult + '?SearchFreeText=' + escape($('#SearchTextBox').val());
    }    
}   

/*==== Sign Up For Deals Alerts in a footer form ====*/ 
function SignUpForDeals(){            
    if ($('#SignUpFormTextBox') && $('#SignUpFormTextBox').val() != '' && $('#SignUpFormTextBox').val() != 'Search by keyword or SKU'){
        window.location.href = linkSignUpForDeals + '?EmailText=' + escape($('#SignUpFormTextBox').val());
    }    
}  

/*==== submit postal code from the header if "enter" key is pressed ====*/
function keyHeaderPostalCode(e){              
    if (isEnterKey(e)){
        disableHeaderPostalCodeForm();
        setPostalCode();
        return false;                    
    }
}   

/*==== submit postal code from zip overlay if "enter" key is pressed ====*/
function keyOverlayPostalCode(e){              
    if (isEnterKey(e)){
        disableOverlayPostalCodeForm();
        setPostalCode();
        return false;                    
    }
}   

/*==== disables postal code form in the header ====*/
function disableHeaderPostalCodeForm(){
    $('#header-zip-code-loading-overlay').css('visibility','visible');               
}

/*==== disables postal code form in the header ====*/
function disableOverlayPostalCodeForm(){
    $('#zip-code-loading-overlay').css('visibility','visible');               
}

/*==== sets postal code submitted from the header form ====*/
function setHeaderPostalCode(){
    disableHeaderPostalCodeForm();
    setPostalCode();
}

/*==== sets postal code submitted from the zip overlay form ====*/
function setOverlayPostalCode(){
    disableOverlayPostalCodeForm();    
    setPostalCode();
}
         
/*==== submits postal code to "linkSetPostalCode" url ====*/
function setPostalCode(){    
    var f = $('#aspnetForm')[0]; 
    if (f){
        f.action = linkSetPostalCode;
        f.submit();
    }
}

/*==== start: query string ====*/
function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }            
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
        for(var j=0; j < this.keyValuePairs.length; j++) {
            if(this.keyValuePairs[j].split("=")[0].toLowerCase() == s.toLowerCase())
            return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key){                
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}

function getQueryStringItem(key){
    var s = queryString(key);  
    if(s == 'false') {
        return '';
    }else{
        return s;
    }
}         
/*==== end: query string ====*/



/*************** besing: inherited from leons - should be depricated *******************************/
var wPopUpWnd = null;
var wFeaturesUpWnd = null;

function jsNewWindow(jsTarget, jsWidth, jsHeight)
{	
	var strRndm = Math.random() * 10000;
	
	if (jsWidth == null)
	{
		jsWidth = 600;
	}
	if (jsHeight == null)
	{
		jsHeight = 400;
	}
	
	
	if (jsTarget.search(/\?/g) == -1)
	{
		jsTarget = jsTarget +  "?dft=" + strRndm;
	}
	else
	{
		if (jsTarget.lastIndexOf("#") != -1){
			if (jsTarget.lastIndexOf("/") == -1){
				var label = jsTarget.substring(jsTarget.lastIndexOf("#"), jsTarget.length);
				jsTarget = label;
			}
			else{
				var url = jsTarget.substring(0, jsTarget.lastIndexOf("?"));
				var params = jsTarget.substring(jsTarget.lastIndexOf("?"), jsTarget.lastIndexOf("#"));
				var label = jsTarget.substring(jsTarget.lastIndexOf("#"), jsTarget.length);
				jsTarget = url + params + label;
			}
		}
		else{
			jsTarget = jsTarget +  "&dft=" + strRndm;
		}	
	}
	window.open(jsTarget ,"","width=" + jsWidth + ",height=" + jsHeight + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");	

}

function jsShowPopUp(jsTarget, wndName, jsWidth, jsHeight)
{
	var strRndm = Math.random() * 10000
	if (jsTarget.search(/\?/g) == -1)
		{
		jsTarget = jsTarget +  "?dft=" + strRndm;
		}
	else
		{
		jsTarget = jsTarget +  "&dft=" + strRndm;
		}
		
	if (wndName == null)
	{
		wndName = "popUpWnd";
	}
	if (jsWidth == null)
	{
		jsWidth = 445;
	}
	if (jsHeight == null)
	{
		jsHeight = 600;
	}

	if (wPopUpWnd == null) {
		wPopUpWnd = window.open(jsTarget ,wndName,"width=" + jsWidth + ",height=" + jsHeight + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
		wPopUpWnd.focus();
	} 
	else 
	{
		if (wPopUpWnd.closed) {
			wPopUpWnd = null;
			wPopUpWnd = window.open(jsTarget ,wndName,"width=" + jsWidth + ",height=" + jsHeight + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
			wPopUpWnd.focus();
		} 
		else 
		{
			wPopUpWnd.navigate(jsTarget );//??
			wPopUpWnd.focus();
		}
	}
}
/*************** besing: inherited from leons - should be depricated ******************************/
