/* Path for this file: /content/dam/nissan/codepack/nissan/components/pages/layout/mainlayout.js/jcr:content/renditions/original */
// these were pulled from main JS file into mainlayout.js
/*
jQuery(document).ready(function() {
    if(jQuery("#frameBg").size()>0){
        //jQuery("#frameBg").height(jQuery("#frameContent").height()+20);
    }
    jQuery("#frameContent").click(function(){
        //jQuery("#frameContent").html(jQuery("#frameContent").html()+"<br /><br />");
        //resizeMe(jQuery("#frameContent").height());
    });    
});
function resizeMe(hgt){
    if(jQuery("#frameBg").size()>0){
        jQuery("#frameBg").height(hgt+20);
        if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){
            //jQuery("#pageBg").height(jQuery("#pageBg").height()+20);
        }
    }
}
*/

jQuery(document).ready(function() {
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) === 6) jQuery('#frameBg + #frameContent').addClass('frameContentHasBg');
 
    if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){         
            jQuery("#pageBg").css("margin-bottom","50px");
        }
    
});


//Note: This file contains unmodified, legacy site code.
function populateDrop(drpIn,drpOut){

    with (drpOut) {
        for(i=options.length-1;i>0;i--) {
            options[i] = null; // null out in reverse order (bug fix)
        }
        opt = 1;
        for(i=0;i<model2year.length;i++){
            
            //alert(drpIn[drpIn.selectedIndex].value+" = "+model2year[i][0]+" = "+
              //(drpIn[drpIn.selectedIndex].value==model2year[i][0]));
            if(drpIn[drpIn.selectedIndex].value==model2year[i][0]) {
                options[0] = new Option("Select Year", "");
                options[opt] = new Option(model2year[i][1], model2year[i][1]);
                opt++;
            } else if (drpIn.selectedIndex==0) {//if no model is selected, no years are displayed
                options[0] = new Option("Select a Model First", "");
                //options[0] = new Option("------", "");
            }
        }
        selectedIndex = 0;
    }
}

function validateForm()
{
  var error = '';

  if(jQuery('select[name=model]').attr('selectedIndex') < 1)
    error = 'Please select a model<br>';

  if(jQuery('select[name=year]').attr('selectedIndex') < 1)
    error += 'Please select a year<br>';

  if(error.length == 0)
  {
    var year = null;
    var hiddenName = '';

    // if all hidden inputs are left in, the url will be too
    // long for some browsers. (5927 chars @ time of writing)
    hiddenName = jQuery('select[name=model]').val() + '_path';
    year = jQuery('select[name=year]').val();
    jQuery('input:hidden').not('input[name='+hiddenName+'][value*='+year+']').remove();

    jQuery('form[name=techPubsForm]').submit();
  }
  else
    jQuery('#errorDiv').html(error);
}
jQuery(function(){
    alphabetizeSelect('select:first',true);
});
function alphabetizeSelect(whichOne,doNotTouchFirstElement){
    doNotTouchFirst=!!doNotTouchFirstElement;
    var sel_j = jQuery(whichOne);
    var sel;
    if(!sel_j.length){
        return;
    }else{
        sel=sel_j[0];
    }

    var _names=[];
    sel_j.find('option').each(function(i){
        if(i==0 && doNotTouchFirstElement){return}
        _names.push([this.text,this.value,this.selected]);
    });
    _names.sort(function(a,b){
        
        a1 = (''+a).toUpperCase();
        b1 = (''+b).toUpperCase();
        aComma = a1.indexOf(',');
        bComma = b1.indexOf(',');
        a1= a1.substring(0,aComma);
        b1= b1.substring(0,bComma)
        if(a1 < b1) return -1;
        if(a1 > b1) return 1;
        return 0;
    });
    
    sel.options.length=doNotTouchFirstElement?1:0;
    jQuery(_names).each(function(i){
        var newOption = sel.options[sel.options.length]=new Option(this[0],this[1]);
        if(this[2]){
            newOption.selected=true;
        }
    });
}

//firing custom crmEvents
jQuery(document).ready(function() {
    if(window.location.search.length > 0){
        var queryParams = window.location.search.substr(1).split('&');
        var final = {};
        
        for (i=0; i<queryParams.length; i++)
        {
            var temp = queryParams[i].split('=');
            final[temp[0]] = temp[1];
        }   
        
        if (final["model"] != undefined && final["year"]!= undefined)
        {
            //call: coreEvent2(MDL,MY)
            crmEvent2(final["model"], final["year"]);
        }
        else
        {
            crmEvent1();
        }
    }
    else
    {
        crmEvent1();
    }

    jQuery(".tIconLink > a").click(function(){      
        var docFullPath = jQuery(this).attr("href");
        var doc = docFullPath.substr(docFullPath.lastIndexOf("/")+1).split(".");
        //call: coreEvent3(docExt,DocName,MDL,MY)
        crmEvent3(doc[1],doc[0], final["model"], final["year"]);
    });
});


