/*==== 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'){                                                             
        $('#header-user-info').css('display','none');
        $('#header-user-info-logged-in').css('display','inline');                            
        $('#header-user-info-logged-in').text('Welcome, ' + getSession('FirstName') + ' ' + getSession('LastName') + '!');
    }                    
    else{                            
        $('#header-user-info-logged-in').css('display','none');
        $('#header-user-info').css('display','inline');
    }
}

/*==== 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');                                        
    }
}

/*=== chat link for storeid 3509 (store_code=MF7979)*/
function showChatLink(){
        var path = window.location.pathname;
        if (getSession('StoreId') == '3509'){ //&& ( path == '/' || path == '/default.aspx' || path == '/laminate.aspx' )
            $('#chat-link').css('display', 'inline'); 
        }else{
            $('#chat-link').css('display', 'none'); 
        }
    }

/*==== 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');                    
            $('#your-location').css('display', 'none');
            $('#zipcode').text(getSession('Zip'));            
        }else if (ziptype == 'u'){            
            $('#zipcode').text(getSession('Zip'));       // zip code  
            $('#your-location').css('display', 'inline');                      
        }        
    }else{
        $('#enter-zip-code').css('display','inline');
        $('#change-zip-code').css('display','none');
    }
}

/*==== displays "enter zip" form and hides "change zip" ====*/
function displayEnterZipForm(ZipCodeTextBox){
    $('#change-zip-code').css('display','none');
    $('#enter-zip-code').css('display','inline');
    $('#zip-code').css('display','none');
    $("#" + ZipCodeTextBox).focus();            
}

/*==== disabling form ====*/
function DisableZipCodeForm(oTextBox){                        
    var oZipCode = $('#'+oTextBox)[0];           
    if (oZipCode && oZipCode.value != ''){                            
        disableOverlayPostalCodeForm();
        disableHeaderPostalCodeForm();                            
    }                        
}

/*==== 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);
    }
}

/*==== top navigation menu links ===*/
function topMenuLinks(){
//    if (getSession('DefaultGeography') == '1'){
//        searchLinksBlocked();
//    }else{
        searchLinks();
//    }
}

/*==== 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    
    $('#SaleCenterLink').attr('href',linkSaleCenter); // sale center                
}

/*==== assigns navigate url to search meny links using zip blocking ====*/
function searchLinksBlocked(){
    $('#CarpetSearchLink').attr('href',"javascript:setSession('curURL','" + linkCarpet + "');showModalZipOverlay();"); // carpet
    $('#HardwoodSearchLink').attr('href',"javascript:setSession('curURL','" + linkHardwood + "');showModalZipOverlay();"); // hardwood
    $('#LaminateSearchLink').attr('href',"javascript:setSession('curURL','" + linkLaminate + "');showModalZipOverlay();"); // laminate
    $('#VinylSearchLink').attr('href',"javascript:setSession('curURL','" + linkVinyl + "');showModalZipOverlay();"); // vinyl
    $('#TileSearchLink').attr('href',"javascript:setSession('curURL','" + linkTile + "');showModalZipOverlay();"); // tile    
    $('#SaleCenterLink').attr('href',"javascript:setSession('curURL','" + linkSaleCenter + "');showModalZipOverlay();"); // sale center
}

/*==== free text search ====*/ 
function Search(){        
    if ($('#SearchTextBox') && $('#SearchTextBox').val() != '' && $('#SearchTextBox').val() != 'Search product'){        
        if ($('#SearchTextBox').length < 60 ){            
            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 ====*/

