/**
 * Javascript to support the usedvehiclesFilter.ftl page.
 * This should consists of the common functionality 
 * of usedVehiclesFilter.ftl and usedVehiclesDetails.ftl
 * @author Rohit Gayal (c) Versata 2008
 */
/*
 * This sets the Omniture Variables 
 * for the POI Popups
 * And then fire the Omniture Event
 */
function setOmnitureTag(make, model, year)
{
  s.pageName= "dc: photo popup"; 
  s.eVar11= "dc: photo popup";
  s.prop11= "dc: photo popup";  
  s.prop12 = s.eVar12 = year;
  s.prop16 = s.eVar16 = make.toLowerCase() + " " + model.toLowerCase();
  fireOmnitureEvent();
}
/**
* 
* This method is for the invocation of the pre-Owned details page magnifier
*/
 var magnifierFlag = true;
 var thumbNailFlag = true;
function invokeMagnifierOmnitureTag(make, model, year, id)
{
  var magnifierId = "CPEnlarge";
  if( id == magnifierId && magnifierFlag == true){
    setOmnitureTag(make, model, year);
    magnifierFlag = false;
  }
}
/**
* 
* This method is for the invocation of the pre-Owned details page thumbnails
*/
function invokeThumbNailOmnitureTag(make, model, year, id, index)
{
  var thumbNailId = "feature_"+index;
  if(id == thumbNailId && thumbNailFlag == true){
    setOmnitureTag(make, model, year);
    thumbNailFlag = false;
  }
}

/**
 * Set the key to replace
 */
function changeValue(key,value)
{
  switch(key){
    case 'Year': modelYear = value; break;
    case 'Make': make = value; break;
    case 'Model': model = value; break;
    case 'Pkg': modelPkg = value; break;
    case 'Style': bodyStyle = value; break;
  }
}

/**
 * this function is clear ' '
 */
function Trim(sInputString)
{
  var sTmpStr = ' ';
  var i = -1;
  while(sTmpStr == ' ')
  {
    ++i;
    sTmpStr = sInputString.substr(i,1);
  }
  sInputString = sInputString.substring(i);
  sTmpStr = ' ';
  i = sInputString.length;
  while(sTmpStr == ' ')
  {
    --i;
    sTmpStr = sInputString.substr(i,1);
  }
  sInputString = sInputString.substring(0,i+1);
  var sTmpStrs = sInputString.split(' ');
  var str="";
  if(sTmpStrs.length>1)
  {
    for(var i =0;i<sTmpStrs.length;i++)
    {
      if(sTmpStrs[i].length>0){
        str=str+sTmpStrs[i]+' ';
      } 
    }
    sInputString = str.substring(0, str.length-1);
  }
  return sInputString;
}

/*
 * Adding Trim back to POI Search Results Page
 * Using EquivalentVehicleParameter to generate the Used-Vehicle-Name.
 * @parameter key : define which part of the name to replaced
 * @parameter arrays : the equivalent-Vehicle-Parameters
 * @parameter dataValue : the part of the name to be replaced
 * @parameter descValue : MNFBodyDesc = FullModelDisplayName
 */
function generateVehicleNames(key,arrays,dataValue,descValue)
{
  for (var i = 0;i<arrays.length;i++)
  {
    var exp;
    exp = arrays[i].replace(/\*/g,"\\.*").replace(/\?/g,"\\.?");
    if(exp == arrays[i]){
      var strArr = arrays[i].split(",");
      var strs="";
      for(var j=0;j<strArr.length;j++){
        strs= strs+'\\b'+strArr[j]+'\\b|';     
      }
      strs = strs.substring(0, strs.length-1);
      var strsReg=new RegExp(strs,"i");
      var tempdata = strsReg.exec(dataValue);
      var tempdesc = strsReg.exec(descValue);
      if(tempdata!==null && tempdesc!==null){
        dataValue = dataValue.replace(tempdata,tempdesc);
        descValue = descValue.replace(tempdesc,"");
        mnfBodyDesc = descValue;
        changeValue(key,dataValue);
      }else {
        for(var z=0;z<strArr.length;z++){
          strs= strs+strArr[z]+'|';     
        }
        strs = strs.substring(0, strs.length-1);
        strsReg=new RegExp(strs,"i");
        tempdata = strsReg.exec(dataValue);
        tempdesc = strsReg.exec(descValue);
        if(tempdata!==null && tempdesc!==null) {
          dataValue = dataValue.replace(tempdata,tempdesc);
          descValue = descValue.replace(tempdesc,"");
          mnfBodyDesc = descValue;
          changeValue(key,dataValue);
        }
      }
    }
    else{
      exp = exp.replace(/\,/g,"\\b|");
      var descarr;
      var dataarr;
      var temp;
      descarr = descValue.split(" ");
      var strsReg=new RegExp(exp);
      for (var j=0;j<descarr.length;j++)
      {
        temp = strsReg.exec(descarr[j]);
        if(temp===null){ continue;}
        else{
          temp = descarr[j];
          dataarr=dataValue.split(" ");
          for (var k=0;k<dataarr.length;k++)
          {
            tempdata = strsReg.exec(dataarr[k]);
            if(tempdata===null){ continue; }
            else{
              tempdata=dataarr[k];
              dataValue = dataValue.replace(tempdata,temp);
              descValue = descValue.replace(temp,"");
              mnfBodyDesc = descValue;
              changeValue(key,dataValue);
              break;
            }
          }
          break;
        }
    }
  }
 }

  if(dataValue==descValue){
    descValue = descValue.replace(dataValue,"");
    mnfBodyDesc = Trim(descValue);
  }else {
    var descarr = descValue.split(" ");
    var datascarr = dataValue.split(" ");
    for (var x=0;x<descarr.length;x++)
    {
      for(var y=0;y<datascarr.length;y++)
      {
        var descarrs = descarr[x].split("-");
        for(var z=0;z<descarrs.length;z++)
        {
          if(descarrs[z]==datascarr[y]) {
            descValue = descValue.replace('-'+datascarr[y],"");
            mnfBodyDesc = Trim(descValue);
          }
        }
        if(descarr[x]==datascarr[y]){
          descValue = descValue.replace(datascarr[y],"");
          mnfBodyDesc = Trim(descValue);
        }
      }
    }
  }
}
