 /******** cookie util *********/
function getCookie(name, key){             
    var val = '';                        
    $.each($.cookie(name).split('&'), function(){                                
        var subCookie = this.split('=');                     
        if (subCookie[0].toLowerCase() == key.toLowerCase()){
            val = subCookie[1];
            return;
        }                                                     
    });    
    return val;
}

/*==== this function allows the "enter" key to submit search ====*/
function SubmitSearch(e){    
    if (isEnterKey(e)){            
      Search();
      return false;
    }    
}

/*==== show header welcome message ====*/
function showWelcome(){
    if (getCookie('SessionInfo', 'IsRegistered') == '1'){                                                             
        $('#header-user-info').css('display','none');
        $('#header-user-info-logged-in').css('display','inline');                            
        $('#header-user-info-logged-in').text('Welcome, ' + getCookie('UserInfo', 'FirstName') + ' ' + getCookie('UserInfo', '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');                                        
    }
}

/*==== displays "change zip" and hides "enter zip" form ====*/
function displayChangeZipForm(){    
    $('#change-zip-code').css('display','inline');
    $('#enter-zip-code').css('display','none');
    $('#zip-code').css('display','none');
}

/*==== displays "enter zip" form and hides "change zip" ====*/
function displayEnterZipForm(){    
    $('#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();                            
    }                        
}

/*==== 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(getCookie('SessionInfo', 'IsSignedOn') == '1'){
        $('#RegistrationAccountLink').attr('href',linkAccount);
        $('#RegistrationAccountLink').text('Account Page');
        $('#LoginLogoutLink').attr('href',linkLogout);
        $('#LoginLogoutLink').text('Logout');
    }else{
        $('#RegistrationAccountLink').attr('href',linkRegistration);
        $('#RegistrationAccountLink').text('Register');
        $('#LoginLogoutLink').attr('href',linkLogin);
        $('#LoginLogoutLink').text('Login');
    }
}

/*==== 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 (document.getElementById("SearchTextBox") != null || document.getElementById("SearchTextBox") != "undefined"){
        var searchTextBox = document.getElementById("SearchTextBox").value;
        if (searchTextBox != "" && searchTextBox.length < 50 ){            
            window.location.href = linkSearchResult + '?SearchFreeText=' + escape(searchTextBox);
        }        
    }    
}   

/*==== 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 ====*/
