validityKeys=new Array();
validityDescriptions=new Array();
var scrollTable=false;

function moveFig(s){
	strObj="document.all."+s+"_0_2"
	tdObj=eval(strObj)
	body=window.document.body
    moveDrawObj(tdObj.offsetTop+minPosDrawY)
    body.scrollTop=tdObj.offsetTop+minPosDrawY

}

function resizeHeaders() {
return false;
// Resize the Header Fields;
  if (scrollTable==true){
  var dataTable = eval("document.all."+dataTableName)
  var headerTable = eval("document.all."+headerTableName)
  var headerRow=eval("document.all."+headerRowName)



  //var Totalcells = 1* headerRow.cells.length - 1;
  var Totalcells = 1* dataTable.rows(0).cells.length - 1;
  for (i=0; i < Totalcells; i++) {

    var headCell = headerTable.rows(0).cells(i);
    var dataCell = dataTable.rows(0).cells(i);

  //	  var headCell = document.all.tblHeader.rows(0).cells(i);
  //	  var dataCell = document.all.tblData.rows(0).cells(i);

    var padW_lft = Math.round(eval(headCell).currentStyle.paddingLeft.replace("px",""));
    var padW_rgt = Math.round(eval(headCell).currentStyle.paddingRight.replace("px",""));
    var brdW_lft = Math.round(eval(headCell).currentStyle.borderLeftWidth.replace("px",""));
    var brdW_rgt = Math.round(eval(headCell).currentStyle.borderRightWidth.replace("px",""));

    //var offAmt = padW_lft + padW_rgt + brdW_lft + brdW_rgt;
    var offAmt = padW_lft + padW_rgt
    w=dataCell.offsetWidth - offAmt
    headCell.style.width = w;

  }

  headerRow.style.visibility = "hidden"
  headerRow.style.position   = "absolute"

  // Resize the First one (overflow has a 1px border around it, so we compensate the offset)
  headerTable.rows(0).cells(0).style.width = Math.round( headerTable.rows(0).cells(0).currentStyle.width.replace("px","")) + 1;
  }
}

function moveDraw(){
   body=window.document.body
   posBody=body.scrollTop
   moveDrawObj(posBody)
}

function moveDrawObj(posBody){
  obj=document.all.divDraw
  if (posBody > minPosDrawY && posBody < maxPosDrawY ) {
    obj.style.posTop=posBody - minPosDrawY
  } else if(posBody < minPosDrawY){
    obj.style.posTop = 0
   } else {
      //obj.style.posTop = posBody-maxPosDrawY
	  //obj.style.posTop=posBody - (minPosDrawY-60)
	  //alert('3=>'+obj.style.posTop);
   }
}

function DL_GetElementTop(eElement)
{

    var nTopPos = eElement.offsetTop;            // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element
    while (eParElement != null)
    {                                            // move up through element hierarchy
      nTopPos += eParElement.offsetTop;        // appending top offset of each parent
      eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nTopPos;                              // return the number calculated
}

function trim(inputString) {
  if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }

   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }

   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function onLoadCheck(){
  scrollDiv();
  checkOrderButton();
  resetCheckBoxOrderItem();
  resizeHeaders();
  if (document.all.divDraw){
    //minPosDrawY=DL_GetElementTop(document.all.divDraw);
    minPosDrawY=DL_GetElementTop(document.all.dataDrawTable);
    maxPosDrawY=DL_GetElementTop(document.all.maxscrolling)-document.all.divDraw.offsetHeight;
    document.body.onscroll=moveDraw;
  }
}

function resetCheckBoxOrderItem(){
  listPart=document.all.partValue
  if (listPart){
    if (listPart.length>1){
      for (i=0;i<listPart.length;i++){
        listPart[i].checked=false
      }
      }else{
        listPart.checked=false
      }
  }
}

function checkOrderButton(){
  listPart=document.all.partValue
  if (!listPart && document.all.addItemButton){
    document.all.addItemButton.disabled=true
  }
}

function scrollDiv(){
  if (document.all.scrollTable) {
    y=(document.all.scrollTable.scrollWidth/totFig)*nFig;
      if (y>widthDiv){
        document.all.scrollTable.scrollLeft=y-widthDiv;
      }
  }
}

function adjustIFrameSize (iframeWindow,frameName,divName) {
   docBody = iframeWindow.document.body;
   iframeElement = document.getElementById(frameName);
   priceSostDiv = iframeWindow.document.getElementById(divName);
   if (priceSostDiv){
     iframeElement.height = priceSostDiv.clientHeight
   }
}
var openWin=true

function clickActionExt(typeTarget,url,openWinFlag) {
  if (openWinFlag==1 || openWin){
    clickAction(typeTarget,url)
  }
  openWin=true
}

function clickAction(typeTarget,url) {
  generalAttrib="toolbar=0,menubar=0,location=0,directories=0,status=0,top=25,left=(screen.availWidth-650)";
	if (typeTarget == 'TARGET_IFRAME' ){
          url='navi?'+url
	  iframeElement=document.frames[0];
	  iframeElement.location=url
	}else if (typeTarget == 'TARGET_NEW_WINDOW' ){
          url='navi?'+url
          p=window.open(url,typeTarget,"width=657px,height=570px,scrollbars=0,resizable=1"+ generalAttrib );
          p.focus();none();
	}else if (typeTarget == 'HELP_WINDOW' ){
          p=window.open(url,typeTarget,"width=790px,height=530px,scrollbars=1,resizable=1,"+ generalAttrib );
          p.focus();none();
	}else if (typeTarget == 'MS_WINDOW' ){
          p=window.open(url,typeTarget,"width=720px,height=530px,scrollbars=1,resizable=0,"+ generalAttrib );
          p.focus();none();
	}else if (typeTarget == 'POP_UP_FABRICS' ){
          url='navi?'+url
          p=window.open(url,typeTarget,"width=430px,height=350px,scrollbars=0,resizable=0,"+ generalAttrib );
          p.focus();none();
	}else if (typeTarget == 'TARGET_DETAIL_WIN' ){
          url='navi?'+url
          p=window.open(url,typeTarget,"width=640px,height=300px,scrollbars=1,resizable=1"+ generalAttrib );
          p.focus();none();
	}else if (typeTarget == 'top' ){
          url='navi?'+url
          if (window.opener) {
            window.opener.location.href=url
            window.close();
          } else {
            top.location.href=url
          }
	}else if (typeTarget == '_parent' ){
          url='navi?'+url
          if (window.opener) {
            window.opener.location.href=url
            window.close();
          } else {
           parent.location.href=url
		}
	}else{
	    url='navi?'+url
	    location.href=url
	}
}

function changeDisplayMode(obj,statusObj) {
  if (obj.style.display=="none"){
    statusObj.src="imagesnavi/collapse.gif"
    obj.style.display="block";
  } else {
    statusObj.src="imagesnavi/expand.gif"
    obj.style.display="none";
  }
  if (parent.adjustIFrameSize) parent.adjustIFrameSize(window,'priceSostFrame','priceSost')
}

function changeM(objList,window_id) {
    //make = objList.options[objList.selectedIndex].value.split(',');
    if (objList.selectedIndex > -1) {
      make = objList.options[objList.selectedIndex].value.split('&');
    var key;
    var language;
    var url="";

    for (i=0; i<make.length; i++) {
      if (!(make[i].indexOf("WINDOW") > -1)) {
        url += make[i]+"&";
      }
    }
    url+="WINDOW_ID="+window_id;
    location.href=url;
    }else{return false;}
}

function goLocation(url) {
    location.href=url;
}

function setTitle(obj,titleStr){
    obj.title=titleStr;
}


function none() {;}

function pointOnMouseOver(id) {
    idPointCell = document.getElementById(id+"_2")
}

/*
  Dispatch fuction for vertical menu selected
  Input parameter:
  int typeCall:
      bit mask of type of call
      1st -> call single change color.
      2nd -> call sencond change color
      3rd -> show detail layer
      4th -> move detail layer
   String idItem
      name of cell to change color
   String detail
      detail to show in layer
   int posXLayer
      new X position to move the detail layer
      if posXLayer == -1 no move
   int posYLayer
      new Y position to move the detail layer
      if posYLayer == -1 no move
*/


function selVertMenu(typeCall,varLayer,voiceObj,pointObj,flagPointOff, imgSrc) {
    var n=typeCall
    var i=0;
    while (n > 0) {
      i++;
      if(n & 1) {
        switch (i){
          //case 1:
          case 2:
            changeBgColorRow(voiceObj,colorRowItemSel,'hand')
            if (voiceObj) {
              if (!(varLayer==null) && !(varLayer=='[object]')) setTitle(voiceObj,varLayer);
            }
            break;

          case 3:
            if (pointObj){
              if (pointObj.length>1){
                for (_i=0;_i<pointObj.length;_i++) {
                  if (flagPointOff=='true')
                    pointObj[_i].src = imgSrc
                  //pointObj[_i].src = imgMirino.src
                  else
                    pointObj[_i].src = imgPallinoRosso.src
                }
              }else{
                if (flagPointOff=='true')
                  pointObj.src = imgSrc
                  //pointObj.src = imgMirino.src
                else
                  pointObj.src = imgPallinoRosso.src
              }
            }
            break;

          case 4:
            if (flagPointOff=='true')
              //pointObj.children(0).src = imgMirino.src
              pointObj.children(0).src = imgSrc
            else
              pointObj.children(0).src = imgPallinoRosso.src
            break;

          case 5:
            if (pointObj)
              if (!(varLayer==null) && !(varLayer=='[object]')) {
                setTitle(pointObj,varLayer);
              }
          break;

          case 6:
            changeBgColorRowStyle(voiceObj,colorRowItemSel,'hand')
            break;
          case 7:
            selectRowVertMenu(voiceObj,colorRowItemSel,'hand')
            break;
        }
      }
      n = n >> 1
    }
}

function unSelVertMenu(typeCall,obj,pointObj,flagPointOff) {
  var n=typeCall
  var i=0;
  while (n > 0) {
  i++;
    if(n & 1) {
      switch (i){
      //case 1:

      case 2:
        //changeBgColorCell(obj,i,oldBgColor[i],'default')
        restoreBgColorRow(obj,'default')
        break;

      case 3:
        if (pointObj){
          if (pointObj.length>1){
            for (_i=0;_i<pointObj.length;_i++) {
              if (flagPointOff=='true')
                pointObj[_i].src = imgPallinoTrans.src
              else
                pointObj[_i].src = imgPallinoBianco.src
            }
          }else{
              if (flagPointOff=='true')
                pointObj.src = imgPallinoTrans.src
              else
                pointObj.src = imgPallinoBianco.src
          }
        }
        break;

      case 4:
        if (flagPointOff=='true')
          pointObj.children(0).src = imgPallinoTrans.src
        else
          pointObj.children(0).src = imgPallinoBianco.src
        break;

      case 5:
        break;

      case 6:
        restoreBgColorRowStyle(obj,'default')
        //changeBgColorRowStyle(voiceObj,colorRowItemSel,'hand')
        break;

      }
    }
  n = n >> 1
  }
}


function changeBgColorRowStyle(vObj,bgColor,typeCursor) {
  row = vObj.parentElement;
  if (row){
    for (i=0;i<row.cells.length;i++) {
      cell=row.cells[i]
      cssClassStyle= cell.currentStyle
      cssStyle= cell.style
      oldBgColor[i]= "" + cssClassStyle.backgroundColor
      cssStyle.backgroundColor=bgColor
    }
  }
  if (vObj.style)
    vObj.style.cursor= typeCursor;
}

function restoreBgColorRowStyle(vObj,typeCursor) {
  row = vObj.parentElement;
  if ((row) && (row != oldRowSelected)){
    for (i=0;i<row.cells.length;i++) {
      cell=row.cells[i]
      cssStyle= cell.style
      cssStyle.backgroundColor=oldBgColor[i]
    }
  }
  if (vObj.style)
  vObj.style.cursor= typeCursor;
}

function selectRowVertMenu(vObj,bgColor,typeCursor) {
  if (oldRowSelected!=null) {
    //ripristino colori vecchia riga
    for (i=0;i<oldRowSelected.cells.length;i++) {
      cell=oldRowSelected.cells[i]
      cssStyle= cell.style
      cssStyle.backgroundColor=oldBgColorCellSelected[i]
    }
    cell.innerHTML="";
  }
  row = vObj.parentElement;
  oldRowSelected=row;
  if (row){
    cssClass=null;
    for (i=0;i<row.cells.length;i++) {
      cell=row.cells[i]
      cssStyle= cell.style
      oldBgColorCellSelected[i]= "" +oldBgColor[i]
      cssStyle.backgroundColor=bgColor
  }
  cell.innerHTML=">>";
  }
  if (vObj.style)
      vObj.style.cursor= typeCursor;
}

function changeBgColorCell(vObj,bgColor,typeCursor) {
  if (vObj){
    oldCellSelected=vObj
    vObj.bgColor=bgColor
      if (vObj.style)
        vObj.style.cursor= typeCursor;
  }
}

function chBgColorTableRow(row,bgColor){
  var i=0;
  for (var i=0;i<row.cells.length;i++) {
    cell=row.cells[i]
    oldBgColor[i]= "" + cell.bgColor
    cell.bgColor=bgColor
  }
}

function restoreBgColorTableRow(row){
  if (row){
    for (var i=0;i<row.cells.length;i++) {
      row.cells[i].bgColor=oldBgColor[i]
    }
  }
}

function changeBgColorRow(vObj,bgColor,typeCursor,idElement) {
  if (vObj.length>0) {
    totRow = vObj.length
    for (i=0;i<totRow;i++){
      row=vObj[i].parentElement;
      chBgColorTableRow(row,bgColor);
    }
  }else{
    chBgColorTableRow(vObj.parentElement,bgColor);
  }
  if (vObj.style)
    vObj.style.cursor= typeCursor;
}

function changeBgColorRowAdv(vObj,bgColor,typeCursor,strId) {
  coll = eval ("document.all." + strId);
  for (i=0;i<coll.length;i++) {
    cell=coll[i]
    oldBgColor[i]= "" + cell.bgColor
    cell.bgColor=bgColor
  }
  if (vObj.style)
    vObj.style.cursor= typeCursor;
}

function restoreBgColorRowAdv(vObj,typeCursor,strId) {
  coll = eval ("document.all." + strId)
  if (coll){
    for (i=0;i<coll.length;i++) {
      coll[i].bgColor=oldBgColor[i]
    }
  }
  if (vObj.style)
    vObj.style.cursor= typeCursor;
}

function restoreBgColorRow(vObj,typeCursor) {
  if (vObj.length>0) {
    totRow = vObj.length;
    for (i=0;i<totRow;i++){
      restoreBgColorTableRow(vObj[i].parentElement);
    }
  }else{
    restoreBgColorTableRow(vObj.parentElement);
  }
  if (vObj.style)
    vObj.style.cursor= typeCursor;
}

function xxselRow (cell,bgColor) {
  row = cell.parentElement;
  fChange=false;
  if (cell.name!="cellSelected"){
    if (rowSel){
      if (rowSel!=row) {
        fChange=true;
      }
    }else{
      fChange=true;
    }
  }

  if (fChange){
    table = row.parentElement
    for (nRow=1;nRow<table.rows.length;nRow++) {
      currentRow=table.rows[nRow];
      for (i=0;i<currentRow.cells.length;i++) {
        currentRow.cells[i].bgColor = oldBgColor[i]
      }
    }
    for (i=0;i<row.cells.length;i++) {
      oldBgColor[i]=row.cells[i].bgColor;
      row.cells[i].bgColor=bgColor;
    }
  }
  rowSel=row
}


function selRow (cell,bgColor) {
  row = cell.parentElement;
  if (oldRow!=null) {
    //ripristino colori vecchia riga
    for (i=0;i<oldRow.cells.length;i++) {
      oldRow.cells[i].bgColor = oldBgColor[i]
    }
  }

  oldRow=row;

  for (i=0;i<row.cells.length;i++) {
    oldBgColor[i]=row.cells[i].bgColor;
    row.cells[i].bgColor=bgColor;
  }
}



function fillLayer(lay,content){
    lay.innerHTML=content
}

//define universal reference to "bottomMenu"
//var crossobj=document.all? document.all.bottomMenu : document.getElementById? document.getElementById("bottomMenu") : document.bottomMenu


function positionit(){
  var dsoctop=document.all? document.body.scrollTop : pageYOffset

  //define universal browser window height
  var window_height=document.all? document.body.clientHeight : window.innerHeight

  //if the user is using IE 4+ or NS6+
  if (document.all || document.getElementById){
    //crossobj.style.top=dsoctop+parseInt(window_height)-20
    //var crossobj=document.all? document.all.bottomMenu : document.getElementById? document.getElementById("bottomMenu") : document.bottomMenu
    document.all.bottomMenu.style.top=dsoctop+parseInt(window_height)-25
  }
}


function submitForm(){
  var check=false;
  for (i=0; i<document.forms[0].DRV.length;i++){
    if  (document.forms[0].DRV[i].checked){
      document.forms[0].DRIVE.value = document.forms[0].DRV[i].value;
      check=true;
    }
  }
  if (!check)
    document.forms[0].DRIVE.value = "0";
  document.forms[0].KEY.value = "GROUP";

  var colorObj = document.forms[0].COLOUR;
  if (colorObj.selectedIndex>0 &&
      colorObj.options[colorObj.selectedIndex].value!='HEAD_COL' &&
      colorObj.options[colorObj.selectedIndex].value!='HEAD_INT' ){
      var colComb = colorObj.options[colorObj.selectedIndex].colsOfComb;
      document.forms[0].SELECTED_COLOR_COMB.value = colComb;
  }else{
	document.forms[0].SELECTED_COLOR_COMB.value =""
  }
  document.forms[0].submit();
}


function reloadFormPage(field,insertModelLabel){
  var fldChs = window.document.forms[0].CHASSIS_NO;
  var fldMod = window.document.forms[0].MOD_COD;
  if (field == fldChs && fldChs.value.length != 17) {
    if (fldMod.value == "") {
      alert(insertModelLabel);
      return false;
    }
  }
  if (field.value != "")  {
    document.forms[0].MODIFIED_FIELD.value = field.name;
  }

  var check=false;

  for (i=0; i<document.forms[0].DRV.length;i++){
    if  (document.forms[0].DRV[i].checked){
      document.forms[0].DRIVE.value = document.forms[0].DRV[i].value;
      check=true;
    }
  }
  if (!check)
    document.forms[0].DRIVE.value = "0";

  document.forms[0].KEY.value = "FORM";
  document.forms[0].submit();
}

function loadIFrame(objIFrame){
  this.src=location.href
}

function showAltText(){
  lay=document.getElementById("formDiv");
  //lay.innerHTML = text
  lay.style.visibility='visible'
}

function hideAltText(){
  lay=document.getElementById("formDiv")
  lay.style.visibility='hidden';
}

function checkFields(){
  var totEl = document.forms[0].elements.length;
  for (i=0; i<totEl; i++)
    alert("KEY : "+document.forms[0].elements[i].name+" ; Value : "+document.forms[0].elements[i].value);
}


function checkSelected(obj,xmlValue,type){
// type possible values :
//  0 - applies to option list
//  1 - applies to checkbox
//  2 - applies to text field

    //Check option list
    if (type == 0){
       //if (obj.value == xmlValue) obj.selected = true;
       for (i=1; i<obj.options.length; i++) {
          if (obj.options[i].value == xmlValue) obj.options[i].selected = true;
       }
    }
    //check checkboxes
    else if (type == 1){
       for (i=0; i<obj.length; i++) {
          if (obj[i].value == xmlValue) obj[i].checked = true;
       }
    }
    //check input text field
    else if (type == 2){
          if (xmlValue.length != 0) obj.value = xmlValue;
    }
    else {

	}
}

function clearForm(form){
    var formLength = document.forms[0].FRM_ELMLGT.value;
    //for (i=3; i<=formLength; i++) {
	for (i=0; i<=formLength; i++) {
      form.elements[i].value="";
    }
    form.KEY.value = "FORM";
    form.submit();
}

function showtip(current,text){
    thetitle=text.split('&nbsp;')
      if (thetitle.length>1){
        thetitles=''
      for (i=0;i<thetitle.length;i++) thetitles+=thetitle[i]
      current.title=thetitles
    }else current.title=text
}

function hidetip(){
  if (document.layers) document.tooltip.visibility="hidden"
}

function selDataPart (cell,bgColor) {
    if (oldCellPartSel!=null) {
        oldCellPartSel.bgColor=oldCellPartSel_bgColor
    }

    oldBgColor[0]=colorRowItemSel;
    oldCellPartSel=cell
    oldCellPartSel_bgColor=bgColor
    cell.bgColor=colorRowItemSel
}



function getSubSevenStyle(i,totItem,totVal,detLength) {
  //var div = obj.getElementById("varDiv");
  var det = 0;
  var out = "<div style='overflow:auto;background-color:ffffff;vertical-align:middle;";
  var endTag = "'>";
  var defaultHeight = "height:25px;";
  var height;

  if (totVal >= 5 && totVal <=10) height="height:60px;"
  else if (totVal > 10) height="height:80px;"
  else if (totVal < 3 && detLength > 130) height="height:40px;"
  else if ((totVal > 3 && totVal < 10) && detLength < 140) height="height:40px;"
  else height = defaultHeight;

  document.writeln(out+height+endTag);

}

function showDiv(divId,forceShow) {
    if (forceShow==1 || flagShow==1) {
        flagShow=1
        divId.style.visibility='inherit'
    }
}



function addReplacedItems(url, additem, message) {
  listPart=document.all.ItemSelection;
  listQuantities=document.all.Quantities;
  listSelPart ="";
    if  (listPart.length > 1){
      for (i=0;i<listPart.length;i++) {
        if (listPart[i].checked){
          temp = listPart[i].value.indexOf(";");
          listSelPart +=listPart[i].value.substring(temp)+";"+
                        listQuantities[i].value+";|";
        }
      }
    }else{
        if (listPart.checked) {
          temp = listPart.value.indexOf(";");
          listSelPart +=listPart.value.substring(temp)+";"+listQuantities.value+";|";
        }
    }

    urlToGo='navi?'+url+additem+listSelPart;
    if (additem=='&ITEMSADD=' && listSelPart==''){
      alert(message);
    }else{
      window.location.href=urlToGo;
    }
}

function selectItemGroup(checkElement) {
    listPart=document.all.ItemSelection;
	if  (listPart.length > 1){
		for (i=0;i<listPart.length;i++) {
			temp = listPart[i].value.indexOf(";");
            preparedStr = listPart[i].value.substring(0,temp);
            if (preparedStr==checkElement.value){
			    listPart[i].checked = checkElement.checked;
            }
		}
	}else{
			temp = listPart.value.indexOf(";");
            preparedStr = listPart.value.substring(0,temp);
            if (preparedStr==checkElement.value){
			    listPart.checked = checkElement.checked;
            }
	}
}


function changeButtonBgColor(vObj,bgColor,typeCursor) {
    if (vObj.style)
        vObj.style.cursor= typeCursor;
        vObj.style.backgroundColor=bgColor;
}

function confirmDeletion(vObjType, message, id) {
  var agree = confirm(message);
  if (agree){
    //var urlToGo = "navi?"+url+"&ITEMSRMV="+id
    var urlToGo = "navi"+document.location.search+"&ITEMSRMV="+id
    window.location.href = urlToGo;
    //return true;
  }
  else
    return false;

}


function _saveShoppingCartValues(){
//If there's only one qty field
  if (typeof (document.all.qty.length) == 'undefined'){
    SB_Qty_values = document.all.qty.value;
  } else {
//Otherwise collect all qty field values
    for (i=0; i<document.all.qty.length; i++){
      SB_Qty_values[i] = document.all.qty[i].value;
    }
  }
//Collect the additional notes value
    SB_notes_value = document.all.addit_notes.value;
}

function updateQuantity(vObj){
  if (typeof (document.all.qty.length) == 'undefined'){
    //There's only one item in the cart
    SB_Qty_data = vObj.id+";"+document.all.qty.value+"|";
  }
  else{
    for (i=0; i<document.all.qty.length; i++){
      if (SB_Qty_values[i] != document.all.qty[i].value){
        SB_Qty_data[i] = vObj.id+";"+document.all.qty[i].value+"|";
      }
    }
  }
  SB_flag_qtyChanged=true;
}


function saveShoppingCartValues(){
  var attributes = new Array();
  if (typeof (document.all.qty.length) == 'undefined'){
    attributes[0] = document.all.qty.value;
      if (!(typeof (document.all.price) == 'undefined'))
        attributes[1] = document.all.price.value;
      else
        attributes[1] = "";
      SB_default_values.put(document.all.qty.id,attributes);
  } else {
      for (i=0; i<document.all.qty.length; i++){
        attributes = new Array();
        attributes[0] = document.all.qty[i].value;
          if (!(typeof (document.all.price) == 'undefined')){
            if (document.all.price[i]){
              attributes[1] = document.all.price[i].value;
            }
            else
              attributes[1] = "";
          }
          else
            attributes[1] = "";
        SB_default_values.put(document.all.qty[i].id,attributes);
      }
    }

//Collect the additional notes value
    SB_notes_value = document.all.addit_notes.value;
}

function updateShoppingCartValues(vObj){
  var currId = vObj.id;
  var currName = vObj.name;
  var currAttr = SB_default_values.get(currId);
  var newAttr = new Array();

  if ((currName) && currName=='qty'){
    newAttr[0] = vObj.value;
    newAttr[1] = currAttr[1];
  } else {
    newAttr[0] = currAttr[0];
    newAttr[1] = vObj.value;
  }
  SB_default_values.put(vObj.id,newAttr);
  SB_flag_qtyChanged=true;
}


function emptyShoppingBasket(msg){

var conf = confirm(msg);
  if (conf){
    var url = "navi"+window.location.search+"&ITEMSCLR=1";
    window.location.href = url;
  }
}

function collectCartIds(pos,value){
  SB_Cart_ids[pos]=value;
}

function setUpdAlertMsg(msg){
  SB_upd_qty_alert = msg;
}

function openReportPage(url){
  window.open(url);
}

function addNotes(message, alertmsg, obj) {
 if (obj.value == "" || typeof(obj.value) == 'undefined'){
    alert(alertmsg);
    return false;
 }
  var agree = confirm(message);
  if (agree){

    var urlToGo = "navi"+document.location.search+"&ADDNOTES="+obj.value;
    window.location.href = urlToGo;
  }
  else
    return false;
}

function updateNotes(vObj){

    if (vObj.value == ""){
        vObj.value = " ";
    }
    SB_notes_value = vObj.value;
    SB_flag_NotesFieldChanged=true;
}


function xupdateShoppingBasket(){
  if (!SB_flag_qtyChanged && !SB_flag_NotesFieldChanged) {
    alert(SB_upd_qty_alert);
    return false;
  }
  else {
    var url = "navi"+window.location.search
    var updateList = "";
    if (SB_flag_qtyChanged){
        for (i=0; i<SB_Qty_data.length; i++){
          if (!(typeof (document.all.qty.length) == 'undefined')){
            if (!(typeof (SB_Qty_data[i]) == 'undefined')){
                updateList+=SB_Qty_data[i];
            }
          }
          else
            //There's only a single item ...
            updateList+=SB_Qty_data;
        }
        url = url+"&ITEMSUPD="+updateList;
    }
    if (SB_flag_NotesFieldChanged){
		if (SB_notes_value.length>100) SB_notes_value=SB_notes_value.substring(0,100)
        url = url + "&ADDNOTES="+encodeURIComponentNew(SB_notes_value);
    }
    SB_flag_UpdateMade = true;
    SB_flag_qtyChanged = false;
    SB_flag_NotesFieldChanged = false;
    window.location.href = url;
  }
}


function updateShoppingBasket(){
  if (!SB_flag_qtyChanged && !SB_flag_NotesFieldChanged) {
    alert(SB_upd_qty_alert);
    return false;
  }
  else {
    var url = "navi"+window.location.search
    var updateList = "";
    var keys = SB_default_values.keys();
    if (SB_flag_qtyChanged){
        for (i=0; i<keys.length; i++){
          if (!(typeof (document.all.qty.length) == 'undefined')){
            currId = keys[i];
            currAttribs = SB_default_values.get(currId);
            if (!(typeof (currAttribs[0]) == 'undefined')){
              currUpdateStr = currId+";"+currAttribs[0]+";"+currAttribs[1]+"|";
              updateList+=currUpdateStr;
            }
          }
          else{
            if (keys.length>1){
            //should be only one item ... why did hash collect strange items??
              currId = keys[i];
              currAttribs = SB_default_values.get(currId);
                if (!(typeof (currAttribs[0]) == 'undefined')){
                  currUpdateStr = currId+";"+currAttribs[0]+";"+currAttribs[1]+"|";
                  updateList+=currUpdateStr;
                }
            } else {
              //There's only a single item ...
              currId = keys[0];
              currAttribs = SB_default_values.get(currId);
              if (!(typeof (currAttribs[0]) == 'undefined')){
                currUpdateStr = currId+";"+currAttribs[0]+";"+currAttribs[1]+"|";
                updateList+=currUpdateStr;
              }
            }
          }
        }
        url = url+"&ITEMSUPD="+updateList;
    }
    if (SB_flag_NotesFieldChanged){
		if (SB_notes_value.length>100) SB_notes_value=SB_notes_value.substring(0,100)
        url = url + "&ADDNOTES="+encodeURIComponentNew(SB_notes_value);
    }
    SB_flag_UpdateMade = true;
    SB_flag_qtyChanged = false;
    SB_flag_NotesFieldChanged = false;
    window.location.href = url;
  }
}


function checkSBChanges(alert_msg){
    if ((SB_flag_qtyChanged || SB_flag_NotesFieldChanged) && !SB_flag_UpdateMade){
        alert(alert_msg);
        return false;
    }
    else return true;
}


function doOpenReportPage(msg,url){
   if (checkSBChanges(msg)){
       openReportPage(url)
   }
}

function doExportData(msg,url){
   if (checkSBChanges(msg)){
       window.location.href=url
   }
}
var flagShow=0;
var flagCartIdCount=0;

var oldCellPartSel=null;
var oldCellPartSel_bgColor="";

var oldRow=null
var oldRowSelected=null


var imgPallinoRosso = new Image();
imgPallinoRosso.src="imagesnavi/viewfinder_on.gif"
var imgPallinoBianco = new Image();
imgPallinoBianco.src="imagesnavi/viewfinder_off.gif"
var imgMirino = new Image();
imgMirino.src="imagesnavi/viewfinder_over.gif"
var imgPallinoTrans = new Image();
imgPallinoTrans.src="imagesnavi/trpoint.gif"
//var colorRowItemSel='D6D6D6'
var colorRowItemSel='FFEFC6'
//var colorRowItemSel='red'
var oldCellSelected;

var SB_Conf_Message_EN = 'Are you sure you want to proceed with the deletion?'
var SB_Conf_Message_IT = 'Sei sicuro di voler precedere nell\'eliminazione ?'
var SB_Qty_values = new Array();
var SB_Qty_data = new Array();
var SB_Cart_ids = new Array();
var SB_flag_qtyChanged = false;
var SB_upd_qty_alert="";
var SB_notes_value = new String("");
var SB_flag_NotesFieldChanged = false;
var SB_flag_UpdateMade = false;
var SB_Price_values = new Array();
var SB_default_values = new kthashtable();

var oldClass  = new Array(3);
var oldBgColor  = new Array(3);
var oldBgColorCellSelected  = new Array();
var rowSel = null
//part used for handling debug window opened for editing labels
var dcTime=250;    // doubleclick time
var dcDelay=100;   // no clicks after doubleclick
var dcAt=0;        // time of doubleclick
var savTO=null;    // handle of click setTimeOut

function handleDebug() {
   switch (event.type) {
    case "dblclick":
      if (event.ctrlKey)

          doDoubleClick(event.type);
      break;
    default:
  }
}
function doDoubleClick(which) {
  var d = new Date();
  dcAt = d.getTime();
  if (savTO != null) {
    clearTimeout( savTO );          // Clear pending Click
    savTO = null;
  }
  if (document.all.labelForm){
     document.all.labelForm.submit()
  }
}

document.ondblclick=handleDebug;
////////  end of part ///////////////////////////////////////////////

// This function parses "TITLE" tag of <TD> which contains validities and descriptions.
// parsing tag used is " - " which should be changed if i .java are changed!!!!!
// it fills 2 arrays with values splitted.
// Whole this thing is used to show popup onclick on <TD> containing validity keys!
function parseValidity(tag){
    var splittag=" - ";
    var arrout=tag.title.split('\n')
    var i;
    for(i=0;i<arrout.length;i++){
      if(arrout[i].length){
        var arrin=arrout[i].split(splittag);
        validityKeys[i]=arrin[0];
        validityDescriptions[i]=arrin[1];
      }
    }
}
function recalcolateDIV(){}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function replaceAll(st, ch, replCh){
if (typeof(st) =='undefined') return "";
else {
    while(st.indexOf(ch)!=-1){
        st = st.replace(ch,replCh);
    }
}
return st;
}




function mvsObject(descr,value,vmkCodM,vmkCodV){
	this.text=descr;
	this.value=value;
	this.MV=vmkCodM+"."+("0"+vmkCodV).substr(vmkCodV.length-1);
	this.vmkCodM=vmkCodM;
	this.vmkCodV=vmkCodV;
}

//Sort function for mvs in version form
// parameter : sort_field   0) sort by version code
//                          1) sort by engine type (after | )
//

function sortMVS(sort_field){
	var mvs = new Array();
    var j = 0;
	for (i=0; i<document.all.versionForm.MVS.options.length;i++){
		if (document.all.versionForm.MVS[i].text != "") {
          mvs[j++] = new mvsObject(	document.all.versionForm.MVS[i].text,
        						    document.all.versionForm.MVS[i].value,
								    document.all.versionForm.MVS[i].vmkCodM,
								    document.all.versionForm.MVS[i].vmkCodV
                                  );
		}
	}
	if (mvs.length!=0){
    	if (sort_field == '0'){
        	//sorting array of values
        	mvs.sort(mvsCmpCode);
        	//replacing old combo ...
            document.all.versionForm.MVS.options[0] = new Option("", "");
        	for (j=0; j<mvs.length; j++){
                i=j+1;
				document.all.versionForm.MVS.options[i] = new Option(mvs[j].text, mvs[j].value);
				document.all.versionForm.MVS.options[i].vmkCodM = mvs[j].vmkCodM;
				document.all.versionForm.MVS.options[i].vmkCodV = mvs[j].vmkCodV;
         	}
    	} else if (sort_field == '1'){
      		mvs.sort(mvsCmpDescr);
            document.all.versionForm.MVS.options[0] = new Option("", "");
         	for (j=0; j<mvs.length; j++){
               i=j+1;
               document.all.versionForm.MVS.options[i] = new Option(mvs[j].text, mvs[j].value);
               document.all.versionForm.MVS.options[i].vmkCodM = mvs[j].vmkCodM;
               document.all.versionForm.MVS.options[i].vmkCodV = mvs[j].vmkCodV;
         	}
    	}
	}
}


function mvsCmpDescr(a, b) {
    var aVal = parseFloat(a.MV);
    var bVal = parseFloat(b.MV);
	return aVal - bVal;
}

function mvsCmpCode(a, b) {
    if ( a.value < b.value ) return -1;
    if ( a.value > b.value ) return 1;
    return 0;
}


//function common to accessories detail pop-up
function adjustSize(){
	var resultDiv 			= document.getElementById("searchResult");
	var resultRow 			= document.getElementById("resultRow");
	var imgsDiv 			= document.getElementById("scrollTable");
	var detailsDiv 			= document.getElementById("part_details");
	var scrollMapAndDetailRow	= document.getElementById("scrollMapAndDetailRow");
	var buttonRow			= document.getElementById("buttonRow");

    if (detailsDiv.clientHeight >145)
        imgsDiv.style.height = "303px";
    else
        imgsDiv.style.height = "149px";

    if(scrollMapAndDetailRow!=null)
        scrollMapAndDetailRow.style.height = detailsDiv.clientHeight+"px";
    if(resultDiv!=null)
        resultDiv.style.height = resultRow.clientHeight+40+"px";
    if(buttonRow!=null)
        buttonRow.style.height = "30px";

}


function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function completeStringWith(line, completeWith, count) {
  var len = count - line.length;
  for (i = 0; i < len; i++) {
    line = completeWith + line;
  }
  return line;
}
