//------------------------------------------------------------------------------
// declareation of global variables
//------------------------------------------------------------------------------
var partserver;
var root;
var iNaviTop = 320; //  navigation position (top) in pixels

//------------------------------------------------------------------------------
// initialize data objects
//------------------------------------------------------------------------------

function getKeyValuePairs(sRowValues) {
  aKeyValues = sRowValues.split("@@");
  aKeyValuePairs = new Array();
  
  for (i = 0; i < aKeyValues.length; i++) {
    aValues = aKeyValues[i].split("=");
    aKeyValuePairs[i] = new cO(aValues[0], aValues[1]);
  }
  
  return aKeyValuePairs;
}


function pO(tabarr,prjpath,prjname,prjpreview,normnumber)
{
  this.tabArray=tabarr;
  this.prjPath=prjpath;
  this.prjName=prjname;
  this.prjPreview=prjpreview;
  this.normNumber=normnumber;
}

// function rowObject(arr, rowNr){
function rO(sKeyValuePairs,rowNr,idNr)
{
  this.rowValues=sKeyValuePairs;
  this.rowId=rowNr;
  this.idNr = idNr;
}

// function colObject(name, value) {
function cO(name,value)
{
  this.varName=name;
  this.varValue=value;
}

function foundObject(prjpath,prjname,prjpreview,normnumber,colarr,rowid,idNr,sortvalue)
{
  this.prjPath=prjpath;
  this.prjName=prjname;
  this.prjPreview=prjpreview;
  this.normNumber=normnumber;
  this.colArr=colarr;
  this.rowId=rowid;
  this.sortValue=sortvalue;
  this.idNr = idNr
}

//------------------------------------------------------------------------------
// create the link to open an item within PARTdataManager
//------------------------------------------------------------------------------
function openProjectLink(tableRow, index)
{
// create the open project link
  var tdProject=document.createElement('td');
  
  var aProject=document.createElement('a');
  var hrefProject=document.createAttribute('href');
  if(partserver==0)
  {
    hrefProject.nodeValue='javascript:openProject('+index+')';
  } else {
    hrefProject.nodeValue='javascript:parent.setOrderFrame('+index+')';
  }    
  aProject.setAttributeNode(hrefProject);
//   }

// create the open Project image
  var imgProject=document.createElement('img');
  var image=prjFoundArrayTemp[index].prjPath;
  
  var regExp=new RegExp(prjFoundArrayTemp[index].prjName+'$');
  image=image.replace(regExp,prjFoundArrayTemp[index].prjPreview);
  partserver=document.partserverCheck.partserver.value;
  root=document.partserverCheck.root.value;

  if(partserver==0)
  {
    image=image.replace(/zimmer/,'');
  }
  else
  {
    image=image.replace(/zimmer/,root);
  }
  
  imgProject.src=image;
  var name=prjFoundArrayTemp[index].prjPreview;
  name=name.replace(/\..+/,'');
  name=name+'Small'+index;
  imgProject.name=name;
  imgProject.id=index;
  imgProject.width='40';
  imgProject.height='40';
  imgProject.style.display='block'; // make it a block element (prevents alignment on the base line)
  imgProject.border='0';
  imgProject.onmouseover=function(){Tip('<img src='+image+'>');}
  
  aProject.appendChild(imgProject);
  tdProject.appendChild(aProject);
    
  tableRow.appendChild(tdProject);
}

//------------------------------------------------------------------------------
// open an item within PARTdataManager
//------------------------------------------------------------------------------
function openProject(index)
{
  document.openProject.action='/cgi-bin/assistantredirector.exe';
  document.openProject.project.value='$CADENAS_DATA/23d-libs/'+prjFoundArrayTemp[index].prjPath;
  document.openProject.rowId.value=prjFoundArrayTemp[index].rowId;
  document.openProject.submit();
}

//------------------------------------------------------------------------------
// Get the number of Pages
//------------------------------------------------------------------------------
function pageCounter(clampsFoundArray)
{
  var tenthes=clampsFoundArray.length%ROWSSHOWN;
  if(tenthes>0)
  {
    var pages=(clampsFoundArray.length/ROWSSHOWN)-(tenthes/ROWSSHOWN)+1;
  }
  else
  {
    var pages=clampsFoundArray.length/ROWSSHOWN;
  }
    
  var currentPage=(prjStart / ROWSSHOWN)+1;
  var amount=document.createTextNode(page+currentPage+of+pages+' ');
  
  var div=document.createElement('div');
  div.style.position='absolute';
  div.style.left='340px';
  div.style.top = iNaviTop + 'px';
  
  div.appendChild(amount);
  
  document.getElementById('showResult').appendChild(div);
}

//------------------------------------------------------------------------------
// Get back to search window
//------------------------------------------------------------------------------
function backToSearch(div) {

// create the back to search link
  var getBack=document.createElement('a');
  getBack.style.textDecoration = 'none';
  
  var hrefGetBack=document.createAttribute('href');
  hrefGetBack.nodeValue="javascript:showSearch('"+div+"')";
  getBack.setAttributeNode(hrefGetBack);
  
  var id=document.createAttribute('id');
  id.nodeValue='showSearch';
  getBack.setAttributeNode(id);
  
  getBack.style.position='absolute';
  getBack.style.top = iNaviTop + 'px';
  
//   getBack.onmouseover=function(){Tip(backToSearchText);}
  
  document.getElementById('showResult').appendChild(getBack);
  
// create the back to search image
  var getBackImg=document.createElement('img');
  
  var getBackImgSrc=document.createAttribute('src');
  getBackImgSrc.nodeValue=root + '/guide/images/backArrow.gif';
  getBackImg.setAttributeNode(getBackImgSrc);
  
  getBackImg.width='26';
  getBackImg.height='17';
  
  deleteBorder(getBackImg);
  
  document.getElementById('showSearch').appendChild(getBackImg);
  
  var getBackText = document.createTextNode(' ' + backToSearchText);
  document.getElementById('showSearch').appendChild(getBackText);
}

// These variables belong to function setOrderFrame, but are also accessed by
// the functions showFoundClamps and showFoundShocks in clampFunctions.js and
// shockFunctions.js!

var sStaticOldBgColor,iStaticOldIndex;

function deselectAll()
{
  // This function is called by clampAllSearch and shockSearch in order
  // to reset the selection of a table row.
  iStaticOldIndex=-1;
  sStaticOldBgColor='';
}

function selectGuiRow(index)
{
  // This function selects one row at the GUI.
  var sOldElementName=('tableRow'+iStaticOldIndex+''); // The element name of the previously selected row
  var sElementName=('tableRow'+index+''); // The element name of the row to be selected
  var oOldSelectedRow=document.getElementById(sOldElementName); // The previously selected row
  var oSelectedRow=document.getElementById(sElementName); // The row to be selected
  try
  {
    oOldSelectedRow.style.backgroundColor=sStaticOldBgColor; // Restore previously selected row to original color
  } catch(e) {
    oOldSelectedRow=-1; // No row was previously selected
  }
  sStaticOldBgColor=oSelectedRow.style.backgroundColor; // Store the original color of the row to be selected
  oSelectedRow.style.backgroundColor='#ffffcc'; // Highlight the row to be selected
  // The row which has just been selected will be the row to be
  // unselected the next time this function is called
  iStaticOldIndex=index;
}

//------------------------------------------------------------------------------
// set the order data according to the selected item (Partserver only)
//------------------------------------------------------------------------------
function setOrderFrame(index) {
  selectGuiRow(index); // Select the row at the GUI
  
  var orderNumber;
  // set the order number
  for(i=0;i<prjFoundArrayTemp[index].colArr.length;i++) {
    if(prjFoundArrayTemp[index].colArr[i].varName=='BESTELLNUM'||prjFoundArrayTemp[index].colArr[i].varName=='BESTNUM')
    {
      orderNumber=prjFoundArrayTemp[index].colArr[i].varValue;
      document.orderForm.orderNumber.value=orderNumber;
//       document.orderForm.submit();
      break;
    }
  }
  
  // remove old header
  var div=document.getElementById('caption01');
  if(div.hasChildNodes())
  {
    while(div.childNodes.length>=1)
    {
      div.removeChild(div.firstChild);
    }
  }
  
  div.style.textAlign = 'center';
  div.style.color = '#ffffff';
  div.style.fontSize = 2;
  div.style.fontWeight = 'bold';
  
  // change orderForm header
  var header=document.createTextNode(orderNumber + ' ');
  
  
  var prjName = prjFoundArrayTemp[index].prjName;

  var instInstruct,iProjectNr,sProjectPath,i,oProjectArray,temp;
  var sProductDescription;
  iRowNr=prjFoundArrayTemp[index].rowId;
  sProjectPath=prjFoundArrayTemp[index].prjPath;
  try // get the pdf path
  {
    temp=pdf2Projects.length; // test if you can access clamps pdf array
    instInstruct=pdf2Projects[sProjectPath][iRowNr]; // a clamp instruction
    sProductDescription=pdf3Projects[sProjectPath][iRowNr]; // the product description
  } catch(e) { // if accessing clamps pdf array raises an error
    try
    {
      temp=anweisProjects.length; // test if you can access shock pdf array
      instInstruct=anweisProjects[sProjectPath][iRowNr]; // it's a shock instruction
      sProductDescription=''; // there is no description for shocks
    } catch(e) { // and if this fails
      instInstruct=''; // no instruction
    }
  }
  if((typeof(instInstruct)=='undefined')||(instInstruct=='undefined'))
  {
    instInstruct='';
  }
  if((typeof(sProductDescription)=='undefined')||(sProductDescription=='undefined'))
  {
    sProductDescription='';
  }
  var pdfPath=root+prjFoundArrayTemp[index].prjPath.replace(/^zimmer/,'').replace(prjFoundArrayTemp[index].prjName,'');
  if(instInstruct!='') // if there is an instruction
  {
    document.getElementById('pdfMontage').href=pdfPath+instInstruct; // link
  } else { // otherwise
    document.getElementById('pdfMontage').href='#no_pdf'; // dummy link
  }
  if(sProductDescription!='') // if there is a description
  {
    try // shocks do not have the element 'pdfProduktbeschreibung'
    {
      document.getElementById('pdfProduktbeschreibung').href=pdfPath+sProductDescription; // link
    } catch(e) {
    }
  } else { // otherwise
    try
    {
      document.getElementById('pdfProduktbeschreibung').href='#no_pdf'; // dummy link
    } catch(e) {
    }
  }


  var prjPath=prjFoundArrayTemp[index].prjPath;
  
  
  document.getElementById('caption01').appendChild(header);
  
  // set the rest of the orderForm values
  document.orderForm.instInstruct.value=instInstruct;
  document.orderForm.partIndex.value=index;
  document.orderForm.prjPath.value=prjPath;
  document.orderForm.rowId.value=prjFoundArrayTemp[index].idNr;
  document.orderForm.submit();
  
  // set the data for the 3d Preview
  prjPath=prjPath.replace(/^zimmer/, '');
  
  
  document.preview3d.prjPath.value=root+prjPath.replace(/.prj$/,'.zjv');
  document.preview3d.submit();

  // create the link for the 2d Drawing  
  prjPath=prjPath.replace(prjFoundArrayTemp[index].prjName,'');
  
  var prjName=prjFoundArrayTemp[index].prjName;
  prjName=prjName.replace(/.prj$/,'');
  
  document.getElementById('2dPreviewBig').name=orderNumber;
  document.getElementById('2dPreviewSmall').src=root+prjPath+'m_'+prjName+'_400_400_v0.jpg';
  
  try {
    document.getElementById("theResults").style.visibility = "hidden";
  } catch(e) {}

  // set all three order elements visible
  document.getElementById('orderFrame').style.visibility='visible';
  document.getElementById('2dPreviewSmall').style.visibility='visible';
  document.getElementById('3dPreview').style.visibility='visible';
  
  document.getElementById('theTable').border=1; // IE7 workaround
  document.getElementById('theView').style.visibility='visible';

}

//------------------------------------------------------------------------------
// adds an item to the basket
//------------------------------------------------------------------------------
function toBasket(index, getNB, getParams)
{
  var partfile=prjFoundArrayTemp[index].prjPath;
  partfile=partfile.replace(/zimmer/,root);
  opener.parent.warenkorb_add2(getNB,'assistant',partfile,getParams);
}

//------------------------------------------------------------------------------
// shows the item in 3d preview
//------------------------------------------------------------------------------
function preview(index)
{
}

//------------------------------------------------------------------------------
// delete the image border
//------------------------------------------------------------------------------
function deleteBorder(image)
{
    var border=document.createAttribute('border');
    border.nodeValue='0';
    image.setAttributeNode(border);
}

//------------------------------------------------------------------------------
// remove the old table
//------------------------------------------------------------------------------
function removeTable()
{
  var div=document.getElementById('showResult');
  if(div.hasChildNodes())
  {
    while(div.childNodes.length>=1)
    {
      div.removeChild(div.firstChild);
    }
  }
}


//------------------------------------------------------------------------------
// design functions for html
//------------------------------------------------------------------------------
function clamps()
{
  document.selection.script.value='$CADENAS_DATA/23d-libs/zimmer/guide/clamps.vbb';
  document.selection.submit();
}

function shocks()
{
  document.selection.script.value='$CADENAS_DATA/23d-libs/zimmer/guide/shocks.vbb';
  document.selection.div.value='search';
  document.selection.submit();
}

function home()
{
  document.selection.script.value='$CADENAS_DATA/23d-libs/zimmer/guide/start.vbb';
  document.selection.root.value=document.selection.originalRoot.value;
  document.selection.submit();
}

var bStaticDonthide;

function printpreview()
{
  var oWindow=window.open((document.getElementById('htmlpath').value+'imgprint.htm'),print,"left=50,top=50,width=500,height=500,location=no,menubar=no,toolbar=no,scrollbars=no,status=no");
}

function showpreviewordernumber()
{
  var pi,sOrderNumber,sAdapterplatte,pnumberdiv; // variable declaration
  for(pi=0;pi<prjFoundArrayTemp[iStaticOldIndex].colArr.length;pi++)
  {
    if(prjFoundArrayTemp[iStaticOldIndex].colArr[pi].varName=='BESTELLNUM'||prjFoundArrayTemp[iStaticOldIndex].colArr[pi].varName=='BESTNUM')
    {
      sOrderNumber=prjFoundArrayTemp[iStaticOldIndex].colArr[pi].varValue;
    }
    if(prjFoundArrayTemp[iStaticOldIndex].colArr[pi].varName=='ADAPTERPLA')
    {
      sAdapterplatte=prjFoundArrayTemp[iStaticOldIndex].colArr[pi].varValue;
    }
  }
  pnumberdiv=document.getElementById('measurepartnumber');
  if(pnumberdiv.hasChildNodes()) // if it has child nodes
  {
    while(pnumberdiv.childNodes.length>=1) // while it has child nodes
    {
      pnumberdiv.removeChild(pnumberdiv.firstChild); // remove child node
    }
  }
  var pheader=document.createTextNode(sOrderNumber+''); // create text node
  var pbr=document.createElement('br'); // create element
  pnumberdiv.appendChild(pheader); // append child
  if(sAdapterplatte) // if sAdapterplatte is not undefined
  {
    var padapterplatte=document.createTextNode(sAdapterplatte+''); // create text node
    pnumberdiv.appendChild(pbr); // append child
    pnumberdiv.appendChild(padapterplatte); // append child
  }
}

function click2dpreview()
{
  // Someone has clicked the 2d preview
  // Display measure vars and drawing
  document.getElementById('measurepic').src=document.getElementById('2dPreviewSmall').src; // load image
  var iVars,j,sProject,iRow,sVarName,sValue,oBr,oTextNode,sTextNode=''; // variable declaration
  var bIsEmpty; // variable declaration
  sProject=prjFoundArrayTemp[iStaticOldIndex].prjPath; // retrieve currently selected project
  document.getElementById('3dPreview').style.visibility='hidden'; // fade out 3d preview
  document.getElementById('measureframe').style.visibility='visible'; // set measure frame visible
  document.getElementById('measurebg').style.visibility='visible'; // set measure bg visible
  bIsEmpty=0; // element is not empty unless proven otherwise
  var measureVars = document.getElementById('measurevars');
  while(!bIsEmpty) // as long as it's not empty
  {
    try
    {
      // remove element
//       document.getElementById('measurevars').removeChild(document.getElementById('measurevars').lastChild);
      measureVars.removeChild(measureVars.lastChild);
    } catch(e) {
      bIsEmpty=1; // no more elements
    }
  }
  try // will throw an error when project was not defined within 'measure.xml'
  {
    for(iVars = 0; iVars < projectvars[sProject].length; iVars++) // step through the vars
    {
      sVarName = projectvars[sProject][iVars]; // get the var name
      var col = prjFoundArrayTemp[iStaticOldIndex].colArr;
      for(j = 0; j < col.length; j++) // step through the columns
      {
        if(col[j].varName.toLowerCase() == sVarName.toLowerCase()) // if var names match
        {
          sValue = col[j].varValue; // retrieve var value of given var
          oBr = document.createElement('br'); // create line break
          measureVars.appendChild(document.createTextNode(sVarName + ': ' + sValue)); // append child
          measureVars.appendChild(oBr); // append child
        }
      } 
    }
  } catch(e) {
  }
  showpreviewordernumber();
}

function hide2dpreview()
{
  if(!bStaticDonthide)
  {
    document.getElementById('3dPreview').style.visibility='visible';
    document.getElementById('measureframe').style.visibility='hidden';
    document.getElementById('measurebg').style.visibility='hidden';
  }
  bStaticDonthide=false;
}

function donthide2dpreview()
{
  bStaticDonthide=true; // Don't hide
}
