var tHeight = 0;
var model = "";
var trim = ""; 

function completeAdvantageTable()
{
  var t = document.getElementById("tAdvantage");
  var oRows = t.getElementsByTagName("tr");
  var oCols = 0;
  var dif = 0;
  
  t.deleteRow(1);       /* Empty row */
  t.deleteRow(0);       /* This row contains only 'GMC Advantage' title */
  t.rows[0].cells[1].innerHTML = "GMC Advantage";
  for (var i=0; i < oRows.length; i++)
  {
    oCols = oRows[i].getElementsByTagName("td");
    if (oCols.length < 6)
    {
       dif = 6 - oCols.length;
       for (var j=0; j < dif; j++)
       {
          var cell = document.createElement("td");
          var cellText = document.createTextNode(" ");
		  var cellDiv = document.createElement("div");
		  cellDiv.appendChild(cellText);
          /*cell.appendChild(cellText);*/
          cell.className = "resultColumn resultsWidth";
		  cell.appendChild(cellDiv);
          oRows[i].appendChild(cell);
       }
    }
  }
}


function setTableRowBkgColor(ptable)
{
  var t= document.getElementById(ptable);
  if (t) 
  {
    var oRows = t.getElementsByTagName("tr");
    var i=0;
    for (i=0; i < oRows.length; i++)
    {
      if ((i % 2) == 0)    // if it is an even row...
      {
         if(navigator.appName == "Microsoft Internet Explorer"){
			 oRows[i].style.height= "10px";
		 }
		 else{
			 oRows[i].style.height= "36px";
		 }
         tHeight += 36;
      }
      else                 // odd row ...
      {
         tHeight += 32;
		 if(navigator.appName == "Microsoft Internet Explorer"){
			 oRows[i].style.height= "10px";
		 }
		 else{
			 oRows[i].style.height= "32px";
		 }
         oRows[i].style.background= "#A8ABB2";                 /* Gray */
         var nCols = oRows[i].cells.length;
         if (nCols >= 6)
            oRows[i].cells[2].style.background= "#CFD0D4";     /* Lightgray */
         else
            oRows[i].cells[3].style.background= "#CFD0D4";     /* Lightgray */
      }
    }
    i= i-1;
    oRows[i].cells[3].className= "resultColumn resultsWidth";
  }
}


/* This function sets an image as the background of an element */
function setImage(pelement, pImage)
{
  var elem = document.getElementById(pelement);
  elem.style.background= pImage;
}


// This function toggles the subtabs
function toggleSubTab(myTabId){
  var realId = myTabId.substring(myTabId.indexOf('_')+1, myTabId.length-2);
  var realDataId = "data_" + realId;
  var disableDataId = "";
  var myCurrentSubTab = "";
  var myBottomTopSubTab = "";
  var bkg_Image = "";
  var ddata;
  var sep1;
  var sep2;

  for (x = 0; x < subTabNames.length; x++){
    myCurrentSubTab = subTabNames[x].replace("&amp;", "&");
    
    /* Top images */
    myBottomTopSubTab = myCurrentSubTab + "_t";
    document.getElementById(myBottomTopSubTab).className = "subTabNoSelected";
    bkg_Image= myCurrentSubTab.substring(myCurrentSubTab.indexOf('_')+1, myCurrentSubTab.length);
    if (bkg_Image.indexOf(" ") > -1)
       bkg_Image= bkg_Image.substring(0, bkg_Image.indexOf(" "));
    setImage(myBottomTopSubTab, "url(/images/bp/comparator/btn_" + bkg_Image.toLowerCase() + "_off.gif)");
    
    /* Bottom images */
    myBottomTopSubTab = myCurrentSubTab + "_b";   
    document.getElementById(myBottomTopSubTab).className = "subTabNoSelected";
    setImage(myBottomTopSubTab, "url(/images/bp/comparator/btn_" + bkg_Image.toLowerCase() + "_off.gif)");
    
    disableDataId = "data_" + myCurrentSubTab.substring(myCurrentSubTab.indexOf('_')+1, myCurrentSubTab.length);
    document.getElementById(disableDataId).style.display = 'none';
  }
  /* Setting selected tab on */
  myBottomTopSubTab = "subTab_" + realId + "_t";
  document.getElementById(myBottomTopSubTab).className = "subTabSelected";
  if (realId.indexOf(" ") > -1) {
     bkg_Image= "url(/images/bp/comparator/btn_" + realId.substring(0, realId.indexOf(" ")).toLowerCase() + "_on.gif)";
  }
  else {
     bkg_Image= "url(/images/bp/comparator/btn_" + realId.toLowerCase() + "_on.gif)";
  }
  setImage(myBottomTopSubTab, bkg_Image);
  
  myBottomTopSubTab = "subTab_" + realId + "_b";
  document.getElementById(myBottomTopSubTab).className = "subTabSelected";
  setImage(myBottomTopSubTab, bkg_Image);
  
  document.getElementById(realDataId).style.display = 'block';
  setTableRowBkgColor('t' + realId);
  /* Pendiente de revisar separadores 
  ddata = document.getElementById('t' + realId).clientHeight;
  ddata -= 20;
  sep1 = document.getElementById("separator1").style.Height = 
  sep1.style.height = this.tHeight+"px"; */
}


// This function toggles the main tabs
function toggleMainTab(domEl)
{
  var content = domEl.id + "Content";
  var contentTemp = "";
  
  for (var i = 0; i < tabNames.length; i++)
  {
    document.getElementById(tabNames[i]).className = (domEl.id == tabNames[i]) ? "tabSelected" : "tabNoSelected";
    document.getElementById("mainTab1").style.background= (domEl.id == "mainTab1") ? "url(/images/bp/comparator/btn_features_specs_on.gif) no-repeat" : "url(/images/bp/comparator/btn_features_specs_off.gif) no-repeat";
    document.getElementById("mainTab2").style.background= (domEl.id == "mainTab2") ? "url(/images/bp/comparator/btn_photo_comparison_on.gif) no-repeat" : "url(/images/bp/comparator/btn_photo_comparison_off.gif) no-repeat";
    contentTemp = tabNames[i] + "Content";
    document.getElementById(contentTemp).style.display = (content == contentTemp) ? 'block' : 'none';
  }
}



var myColumnIndex = 0;

//This function manages the Add/Replace Actions
function addReplace(myYear, myMake, myModel, myStyle){
  
  if (myColumnIndex < 2){
    alert('You must select the column to add/replace.')
  }
  else{
    fillActionForms(myYear, myMake, myModel, myStyle);
  }
}

//This functions tell us if the column selected is empty (we are adding) or it already contains a vehicle (we are replacing)
function isAdd(){
  return retrieveTrim() == "NO_TRIM";
}

//Returns the trim of the car for the selected column in the dropdown, if theres no trim, it returns a string "NO_TRIM"
function retrieveTrim(){
  var myTrimId = "trim_" + (myColumnIndex-1);
  var myTrim = document.getElementById(myTrimId).innerHTML.replace(/^\s+|\s+$/g,""); // This removes spaces (trim)
  
  return myTrim;
}

//This function calls the right action depending on if we are adding or replacing a vehicle
function fillActionForms(myYear, myMake, myModel, myStyle){
  if (isAdd()){
    document.hiddenAddForm.cmpModelYear.value = myYear;
    document.hiddenAddForm.cmpMakeName.value = myMake;
    document.hiddenAddForm.cmpModelID.value = myModel;
    document.hiddenAddForm.cmpModelTrimID.value = myStyle;
    document.hiddenAddForm.divisionCode.value = document.comparatorForm.divisionCode.value;
    
    document.hiddenAddForm.submit();
  }
  else{
    document.hiddenReplaceForm.cmpModelYear.value = myYear;
    document.hiddenReplaceForm.cmpMakeName.value = myMake;
    document.hiddenReplaceForm.cmpModelID.value = myModel;
    document.hiddenReplaceForm.cmpModelTrimID.value = myStyle;
    document.hiddenReplaceForm.divisionCode.value = document.comparatorForm.divisionCode.value;
    
    document.hiddenReplaceForm.trimID.value = retrieveTrim();
    
    document.hiddenReplaceForm.submit();
  }
}

function slide(elementId1, elementId2, show){
  if (show){
    if (!window.ActiveXObject) new Effect.Fade(elementId1, { duration: 0.3, from: 1, to: 0, queue: 'end'});
    new Effect.Appear(elementId2, { duration: 0.3, from: 0, to: 1, queue: 'front'});
  }else{
    if (!window.ActiveXObject) new Effect.Appear(elementId1, { duration: 0.3, from: 0, to: 1, queue: 'end'});
    new Effect.Fade(elementId2, { duration: 0.3, from: 1, to: 0, queue: 'front'});
  }
}

// Returns the specific trim given a full trim string 
function returnVehicleTrim(trimString, year, make, model){
  /*var myStringArray = trimString.split(" ");
  var i = 0;*/
  var myReturnString = trimString;
  
  /*for (i = 3; i < myStringArray.length; i++){
    myReturnString += " " + myStringArray[i];
  }*/
  
  var myYear = new RegExp(year, 'i');
  var myMake = new RegExp(make, 'i');
  var MyModel = new RegExp(model, 'i');       
  
  myReturnString = myReturnString.replace(myYear, "");
  myReturnString = myReturnString.replace(myMake, "");
  myReturnString = myReturnString.replace(MyModel, ""); 
  
  myReturnString = myReturnString.replace( /^\s+|\s+$/g,"");
  
  return myReturnString;
}

// Manages the Omniture Clicks
function clickTrack(oArgs){
  for (var q in oArgs){
    if (window[q] != undefined) {
      window[q] = oArgs[q];
    }
  }
  
  s_gs(s_account);
}


function manageSubTabsClickTrack(subTabHeader, brandId, selectedModel){
  getModelTrim(selectedModel);
  if (subTabHeader.toLowerCase().indexOf("advantage") >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'ADVANTAGE', s_prop15: 'FEATURES | ADVANTAGE', s_prop16: 'COMPARISON | FEATURES | ADVANTAGE', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | ADVANTAGE', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("powertrain")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'POWERTRAIN', s_prop15: 'FEATURES | POWERTRAIN', s_prop16: 'COMPARISON | FEATURES | POWERTRAIN', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | POWERTRAIN', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("dimensions")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'DIMENSIONS', s_prop15: 'FEATURES | DIMENSIONS', s_prop16: 'COMPARISON | FEATURES | DIMENSIONS', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | DIMENSIONS', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("exterior")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'EXTERIOR', s_prop15: 'FEATURES | EXTERIOR', s_prop16: 'COMPARISON | FEATURES | EXTERIOR', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | EXTERIOR', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("interior")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'INTERIOR', s_prop15: 'FEATURES | INTERIOR', s_prop16: 'COMPARISON | FEATURES | INTERIOR', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | INTERIOR', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("safety")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'SAFETY', s_prop15: 'FEATURES | SAFETY', s_prop16: 'COMPARISON | FEATURES | SAFETY', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | SAFETY', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("fuel efficiency")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'FUEL EFFICIENCY', s_prop15: 'FEATURES | FUEL EFFICIENCY', s_prop16: 'COMPARISON | FEATURES | FUEL EFFICIENCY', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | FUEL EFFICIENCY', s_prop25: brandId  });
  }
  else if (subTabHeader.toLowerCase().indexOf("warranty")  >= 0){
    clickTrack({s_pageName: brandId + ' | DIVISIONAL | COMPARISON | FEATURES',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARISON', s_prop3: 'DIVISIONAL | COMPARISON', s_prop4: model + ' | ' + trim, s_prop11: 'FEATURES', s_prop12: 'COMPARISON | FEATURES', s_prop13: 'DIVISIONAL | COMPARISON | FEATURES', s_prop14: 'WARRANTY', s_prop15: 'FEATURES | WARRANTY', s_prop16: 'COMPARISON | FEATURES | WARRANTY', s_prop17: 'DIVISIONAL | COMPARISON | FEATURES | WARRANTY', s_prop25: brandId  });
  }
}

function managePhotosTabClickTrack(id, photoName, brandId){
  //id: 0 = main tab, 1 = exterior element, 2 = interior element
  
  if (id == 0){
    clickTrack({s_pageName: brandId + '| DIVISIONAL | COMPARE | PHOTO',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARE', s_prop3: 'DIVISIONAL | COMPARE', s_prop11: 'PHOTO', s_prop12: 'DIVISIONAL | PHOTO', s_prop13: 'DIVISIONAL | COMPARE | PHOTO', s_prop14: 'INDEX', s_prop15: 'PHOTO | INDEX', s_prop16: 'COMPARE | PHOTO | INDEX', s_prop17: 'DIVISIONAL | COMPARE | PHOTO | INDEX', s_prop25: brandId  });
  }
  else if (id == 1){
    clickTrack({s_pageName: brandId + '| DIVISIONAL | COMPARE | PHOTO',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARE', s_prop3: 'DIVISIONAL | COMPARE', s_prop11: 'PHOTO', s_prop12: 'DIVISIONAL | PHOTO', s_prop13: 'DIVISIONAL | COMPARE | PHOTO', s_prop14: 'EXTERIOR | ' + photoName, s_prop15: 'PHOTO | EXTERIOR | ' + photoName, s_prop16: 'COMPARE | PHOTO | EXTERIOR | ' + photoName, s_prop17: 'DIVISIONAL | COMPARE | PHOTO | EXTERIOR | ' + photoName, s_prop25: brandId  });
  }
  else if (id == 2){
    clickTrack({s_pageName: brandId + '| DIVISIONAL | COMPARE | PHOTO',s_prop1: 'DIVISIONAL', s_prop2: 'COMPARE', s_prop3: 'DIVISIONAL | COMPARE', s_prop11: 'PHOTO', s_prop12: 'DIVISIONAL | PHOTO', s_prop13: 'DIVISIONAL | COMPARE | PHOTO', s_prop14: 'INTERIOR | ' + photoName, s_prop15: 'PHOTO | INTERIOR | ' + photoName, s_prop16: 'COMPARE | PHOTO | INTERIOR | ' + photoName, s_prop17: 'DIVISIONAL | COMPARE | PHOTO | INTERIOR | ' + photoName, s_prop25: brandId  });
  }
}


function manageLinksClickTrack(id, brandId){
  clickTrack2({s_linkName: brandId + '| COMPARISON | QUICK TOOLS | '+ id ,s_prop5: brandId + '| COMPARISON | QUICK TOOLS | '+ id });
}

// Finds the first element inside a DIV and returns its path treating it as an image element
function getFirstElementPathAsImage(myDiv){
  var returnString = "";
  var i = 0;
  var theEnd = false;
  while (i < myDiv.childNodes.length && !theEnd){
    if (myDiv.childNodes[i].nodeType == 1){
      returnString = myDiv.childNodes[i].src;
      theEnd = true;
    }
    i++;
  }
  return returnString;
}


 function imgResize(elem) {
  var winWidth=0;
  var winHeight=0;
  var imgWidth=843
  var imgHeight=623
  var newWidth=0;
  var newHeight=0;
  var bgImage=document.getElementById(elem);
  if(window.innerWidth) {
   winWidth =window.innerWidth;
   winHeight=window.innerHeight;
  } else {
   winWidth=document.body.offsetWidth;
   winHeight=document.body.offsetHeight;
  }
  if(winHeight>imgHeight) {
   newHeight=winHeight;
   newWidth=winHeight*(imgWidth/imgHeight);
  }
  if(newWidth<winWidth) {
   newWidth=winWidth;
   newHeight=newWidth*(imgHeight/imgWidth);
  }
  bgImage.style.width =newWidth;
  bgImage.style.height=newHeight;
 }
 
function clickTrack2(oArg) 
{ 
   if (typeof(oArg.s_pageName) == "string") { s_pageName = oArg.s_pageName; }
   if (typeof(oArg.s_lnk) == "boolean") {s_lnk = oArg.s_lnk;} else {s_lnk = true;}
   if (typeof(oArg.s_linkName) == "string") { s_linkName = oArg.s_linkName; }
   if (typeof(oArg.s_linkType) == "string") { s_linkType = oArg.s_linkType; } else { s_linkType="o"; }                                                            
   if (typeof(oArg.s_prop1) == "string") { s_prop1 = oArg.s_prop1; }                                                           
   if (typeof(oArg.s_prop2) == "string") { s_prop2 = oArg.s_prop2; }                                                           
   if (typeof(oArg.s_prop3) == "string") { s_prop3 = oArg.s_prop3; } 
   if (typeof(oArg.s_prop4) == "string") { s_prop4 = oArg.s_prop4; } 
   if (typeof(oArg.s_prop5) == "string") { s_prop5 = oArg.s_prop5; } 
   if (typeof(oArg.s_prop11) == "string") { s_prop11 = oArg.s_prop11; }
   if (typeof(oArg.s_prop12) == "string") { s_prop12 = oArg.s_prop12; }
   if (typeof(oArg.s_prop13) == "string") { s_prop13 = oArg.s_prop13; }
   if (typeof(oArg.s_prop14) == "string") { s_prop14 = oArg.s_prop14; }
   if (typeof(oArg.s_prop15) == "string") { s_prop15 = oArg.s_prop15; }
   if (typeof(oArg.s_prop16) == "string") { s_prop16 = oArg.s_prop16; }
   if (typeof(oArg.s_prop17) == "string") { s_prop17 = oArg.s_prop17; }
   if (typeof(oArg.s_prop18) == "string") { s_prop18 = oArg.s_prop18; }
   if (typeof(oArg.s_prop19) == "string") { s_prop19 = oArg.s_prop19; }                                                           
   if (typeof(oArg.s_prop20) == "string") { s_prop20 = oArg.s_prop20; }
   if (typeof(oArg.s_prop21) == "string") { s_prop21 = oArg.s_prop21; }  
   if (typeof(oArg.s_prop24) == "string") { s_prop24 = oArg.s_prop24; }
   if (typeof(oArg.s_prop23) == "string") { s_prop23 = oArg.s_prop23; }
   if (typeof(oArg.s_prop25) == "string") { s_prop25 = oArg.s_prop25; }   
   if (typeof(oArg.s_prop28) == "string") { s_prop28 = oArg.s_prop28; }   
   if (typeof(oArg.s_prop30) == "string") { s_prop30 = oArg.s_prop30; }   

   //alert(s_pageName+"~"+s_linkName+"~"+s_linkType+"~"+s_prop1+"~"+s_prop2+"~"+s_prop3+"~"+s_prop19+"~"+s_prop20+"~");
   //alert(s_account);
   s_lnk=true;
   s_gs(s_account);
   s_pageName = "";
   s_prop1 = "";
   s_prop2 = "";
   s_prop3 = "";
   s_prop4 = "";
   s_prop5 = "";
   s_prop11 = "";
   s_prop12 = "";
   s_prop13 = "";
   s_prop14 = "";
   s_prop15 = "";
   s_prop16 = "";
   s_prop17 = "";
   s_prop19 = "";
   s_prop20 = "";
   s_prop21 = "";
   s_prop25 = "";
   s_prop24 = "";
   s_prop23 = "";
   s_prop28 = "";
   s_prop30 = "";
}

function getModelTrim(selectedModel){
	var arrayModel = new Array;
	arrModel = selectedModel.split(" ");
	model = arrModel[0].toUpperCase();
	if(arrModel.length > 1){
		trim = arrModel[1].toUpperCase();
	} else {
		trim = arrModel[0].toUpperCase();
	}
}