/*==== Adds event to window.onload without overwriting currently assigned onload functions.====*/
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){        
        window.onload = func;        
    } else {
        window.onload = function(){
            oldonload();
            func();            
        }
    }
}

/*==== 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 for English Version====*/
function SubmitSignUpForDeals(e){    
    if (isEnterKey(e)){            
      SignUpForDeals();
      return false;
    }    
}

/*==== this function allows the "enter" key to Submit SignUpForDeals in footer for French Version====*/
function SubmitTestFr(e){    
    if (isEnterKey(e)){            
      TestFr();
      return false;
    }    
}

/*==== show header welcome message ====*/
function showWelcome(){     
    if (getSession('IsRegistered') == '1' && getSession('FirstName') != '' && getSession('LastName')){
        $('#header-main-welcome').css('display', 'inline');
        $('#header-welcome-message').text('Welcome, ' + escape(getSession('FirstName')) + ' ' + escape(getSession('LastName')));
    }else{
        $('#header-main-welcome').css('display', 'none');
    }
}

/*==== displays "change zip" and hides "enter zip" form ====*/
function displayChangeZipForm(){         
    var ziptype = getSession('ZipType');    
    if (ziptype !='i'){
        $('#enter-zip-code').css('display','none');
        $('#change-zip-code').css('display', 'inline');
        if (ziptype == 'u'){             
            $('#zipcode').text(getSession('Zip'));
            $('#pcikup-only-location').css('display', 'none');
            $('#header-main-store-info').css('display', 'inline');            
            $('#localized-for-zip').css('display','none');
            $('#your-postal-code').css('display','inline');
        }else if (ziptype == 'v'){
            $('#zipcode').text(getSession('VirtualZIP'));
            $('#pcikup-only-location').css('display', 'inline');
            $('#header-main-store-info').css('display', 'inline');            
            $('#localized-for-zip').css('display','none');
            $('#your-postal-code').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(){      
    if (getSession('ZipType') == 'i'){
        showModalZipOverlay();
    }else{
        $('#change-zip-code').css('display', 'none');
        $('#enter-zip-code').css('display', 'inline');
    }
}

/*==== disabling form ====*/
function DisableZipCodeForm(oTextBox){ 
alert("DisableZipCodeForm(oTextBox)");
/*                       
    var oZipCode = $j('#'+oTextBox)[0];           
    if (oZipCode && oZipCode.value != ''){                            
        disableOverlayPostalCodeForm();
        disableHeaderPostalCodeForm();
        $j("#HiddenPostalCode").attr('value', oZipCode.value);        
        $j('#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);
        $('#master-page-footer-main-page-bottom-form').css('display', 'none');
    }else{
        $('#LeftLink').attr('href', linkRegistration);
        $('#LeftLink').text(linkRegistrationText);
        $('#RightLink').attr('href', linkLogin);
        $('#RightLink').text(linkLoginText);
        $('#master-page-footer-main-page-bottom-form').css('display', 'inline-block');
    }
}

/*==== free text search ====*/ 
function Search(){ 
    var s = $(".textbox-search-en");
    var f = $(".textbox-search-fr");    
    if (s.length == 0){
        if ( f.val() != 'undefined' && f.val() != '' && f.val() != 'Recherche par mot clé ou sku'){
            window.location.href = linkSearchResult + '?SearchFreeText=' + escape(f.val());
        }
    }
    
    if (f.length == 0){
        if (s.val() != 'undefined' && s.val() != '' && s.val() != 'Search by keyword or SKU'){
            window.location.href = linkSearchResult + '?SearchFreeText=' + escape(s.val());
        }
    }
}  

/*==== Sign Up For Deals Alerts in a footer form for English Version ====*/ 
function SignUpForDeals(){ 
 
    var s = $(".SignUpFormTextBox");
    var f = $(".SignUpFormTextBoxFr");
    
    if (s.length == 0){
        if ( f.val() != 'undefined' && f.val() != '' && f.val() != "Entrez dans l'adresse de votre courrier électronique ici"){
            window.location.href = linkSignUpForDeals + '?EmailText=' + f.val();
        }
    }
    
    if (f.length == 0){
        if (s.val() != 'undefined' && s.val() != '' && s.val() != 'Enter your email address here'){
            window.location.href = linkSignUpForDeals + '?EmailText=' + s.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(){
    alert("disableHeaderPostalCodeForm()");
//    $j('#header-zip-code-loading-overlay').css('visibility','visible');               
}

/*==== disables postal code form in the header ====*/
function disableOverlayPostalCodeForm(){
    alert("disableOverlayPostalCodeForm()");
//    $j('#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(){  
alert("setPostalCode()");
/*  
    var f = $j('#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 ====*/

/*==== Gets language and apply CSS changes ====*/
function ajustCSSforLanguage(){
    if (ClientLanguageVar == 'fr-CA'){
        $('#header-main-links').width(350);
        $('#header-main-links').css('marginLeft', '-20px');
    }
    else if (ClientLanguageVar == 'en-US'){
        $('#header-main-links').width(280);
    }
}
