function toHex(d)
{
  H=d.toString(16).toUpperCase()
  if(H.length % 2==1){H="0"+H}
  return H
}

function toHexString(Str)
{
  var hStr=""
  for(i=0;i<=Str.length-1;i++)
  {
    hStr+=toHex(Str.charCodeAt(i))
  }
  return hStr
}

function fromHexString(Str)
{
  var frmh=""
  if(Str.length % 2==1){Str="0"+Str}
  for(i=0;i<=Str.length-1;i+=2)
  {
    frmh+=String.fromCharCode(parseInt(Str.substring(i,i+2),16))
  }
  return frmh
}

function cyphr(InpString,kstr,d)
{
  var key=new Array(InpString.length)
  var keystr=kstr
  var zkey="m"
  while(keystr.length<InpString.length){keystr+=kstr}
  for(c=0; c<=InpString.length-1; c++)
  {
    key[c]=keystr.charCodeAt(c)-zkey.charCodeAt(0)
  }
  var cyphered=""
  var tempChar
  for(c=0; c<=InpString.length-1; c++)
  {
    tempChar=InpString.charCodeAt(c)+d*key[c]
    tempChar=tempChar & 255   // DON'T USE % 256 as treats negatives incorrectly
    cyphered+=String.fromCharCode(tempChar)
  }
  return cyphered
}

function cypher(InpString,kstr)
{return cyphr(InpString,kstr,-1)}

function uncypher(InpString,kstr)
{return cyphr(InpString,kstr,1)}

function cypherURI(InpString,kstr)
{return encodeURIComponent(cypher(InpString,kstr))}

function uncypherURI(InpString,kstr)
{return uncypher(decodeURIComponent(InpString),kstr)}

function cypherEsc(InpString,kstr)
{return escape(cypher(InpString,kstr))}

function uncypherEsc(InpString,kstr)
{return uncypher(unescape(InpString),kstr)}

function cypherHex(InpString,kstr)
{return toHexString(cypher(InpString,kstr))}

function uncypherHex(InpString,kstr)
{return uncypher(fromHexString(InpString),kstr)}

function oneWay(InpString)
{
  var c=cypher(InpString.substring(1,InpString.length),InpString)
  var cm=""
  var chck=0
  for (var i=0; i<InpString.length; i++)
  {
//alert(toHex(InpString.charCodeAt(i)))
    chck+=InpString.charCodeAt(i)
    chck&=255
  }
//alert("chck: "+toHex(chck))
  for (var i=0; i<c.length; i++)
  {
    cm+=String.fromCharCode((c.charCodeAt(i)+chck)&255)
  }
  return cm
}

function oneWayHex(InpString)
{return toHexString(oneWay(InpString))}



// -----------------------------------------------

function charRemoveNotify(teststring,dl,notify)
{
 if(dl.length>1){dl=dl.substring(0,1)}
// * is used as a delimiting char in the cust data cookie.
 dlimit=teststring.indexOf(dl,0)
 while(dlimit!=-1)
 {
   if(notify==true){alert(dl+" is an invalid character")}
   teststring=teststring.substring(0,dlimit)+teststring.substring(dlimit+1,teststring.length)
   dlimit=teststring.indexOf(dl,0)
 } 
 return teststring
}

function charRemove(teststring,dl)
{
 return charRemoveNotify(teststring,dl,true)
}

function charReplace(test, out, replace) {
  if(out==replace) return test;
  pos=test.indexOf(out);
  while(pos>-1) {
    test=test.substring(0,pos) + replace + test.substring(pos+out.length,test.length);
    pos=test.indexOf(out,pos+replace.length);
  }
  return test;
}

function plural(n)
{
  var s=""
  if(n!=1) {s="s"}
  return s
}

function reloadPage()
{
  document.execCommand("Refresh")
}

function trim(str)
{
  if(str.length==0) {return str}
  while(str.charAt(0)==" ") {str=str.substring(1,str.length)}
  while(str.charAt(str.length-1)==" ") {str=str.substring(0,str.length-1)}
  return str
}

function getSearchAsArray()
{
  var results=new Array();
  var input = unescape(parent.location.search);
  if (input)
  {
    if (input.substring(0, 1) == '?') {input = input.substring(1)}
    var srchArray=input;
    var tempArray=srchArray.split("=");
    results[tempArray[0]]=srchArray.substring(tempArray[0].length+1,srchArray.length)
  }
  return results;
}

function checkFrameset()
{
  if(parent==window)
  {
    // use replace to keep current page out of history
    location.replace("index.htm?content="+escape(location.href))
  }
}

function kCheck()
{
  if(parent.dom.length==19) {keyword=oneWay(parent.dom)}
}

function setBackground()
{
  if(parent.TitleFrame.tradecust==true){document.body.background="aidaTrade.jpg"}
  else{document.body.background="aidaMO.jpg"}
}

function orderdatestring()
{
  var orderdate=new Date
  var ordDate=orderdate.getDate()
  var ordMonth=orderdate.getMonth()+1
  var ordYear=orderdate.getYear()
  var ordHours=orderdate.getHours()
  var ordMinutes=orderdate.getMinutes()
  var ordSeconds=orderdate.getSeconds()
  return ordDate+"/"+ordMonth+"/"+ordYear+" "+ordHours+":"+ordMinutes+":"+ordSeconds
}

function currency(v)
{
  var sgn=""
  if(v<0) {v=-v;sgn=" -"}
  pounds=Math.round(v-.5)
  pence=Math.round(v*100-pounds*100)
  if (pence==100){pounds=pounds+1;pence=0}
  if (pence<10){var z="0"}
  if (pence>=10){var z=""}
  res=""+sgn+pounds+"."+z+pence
  return res
}

function tradeCust()
{return parent.TitleFrame.tradecust}

function stkLinesCount()
{return parent.TitleFrame.stocknum}

function stkForSale()
{return parent.TitleFrame.stockcount}

function stkItem(i)
{return parent.TitleFrame.stitem[i]}

function stkLine(partNum)  // returns line number of this part number, -1 if not exist
{
  if(parent.TitleFrame.stPart[partNum]>=0){return parent.TitleFrame.stPart[partNum]}
  else{return -1}
}

function stkDesc(i)
{return parent.TitleFrame.stdesc[i]}

function stkCat(i)
{return parent.TitleFrame.sttype[i]}

function stkRRP(i)
{
  if(parent.TitleFrame.stprice[i]){return parent.TitleFrame.stprice[i]}
  else{return 0}
}

function stkTrdPrice(i)
{
  if(parent.TitleFrame.tradecust==true)
  {
    if(parent.TitleFrame.stTradePr[i]){return parent.TitleFrame.stTradePr[i]}
    else{return 0}
  }
  else
    {return stkRRP(i)}
}

function stkRetUnits(i)
{return parent.TitleFrame.stNumKits[i]}

function stkTrdMult(i)
{return parent.TitleFrame.stmult[i]}

function catTotCount()
{return parent.TitleFrame.cat}

function catName(ct)   // returns cat name of this number or "" if not exist
{
  if(parent.TitleFrame.catStockLine[ct]){return parent.TitleFrame.sttype[parent.TitleFrame.catStockLine[ct][0]]}
  else{return ""}
}

function catNum(thiscat)  // returns cat number of this name, -1 if not exist
{
  if(parent.TitleFrame.cName[thiscat]>=0){return parent.TitleFrame.cName[thiscat]}
  else{return -1}
}

function catComment(i)
{return parent.TitleFrame.comment[i]}

function catCommentTrade(i)
{return parent.TitleFrame.commentTrade[i]}

function catLinesCount(i)
{return parent.TitleFrame.catLineCount[i]}

function catForSale(i)
{return parent.TitleFrame.catcount[i]}

function catFindStock(ct,ln)
{return parent.TitleFrame.catStockLine[ct][ln]}

function tradeLogin()
{
  if(tradeCust()!=true){parent.ContentFrame.location="TradeEntry1.htm"}
  else
  {
     notTrade()
     switchTrol()
     parent.ContentFrame.location="home1.htm"
  }
}

function notTrade()
{
  parent.TitleFrame.tradecust=false
  parent.TitleFrame.setTradeCookie()
}

function switchTrol()
{
  if(parent.TitleFrame.altTrolCount>0)
  {
    //alert("retrieve "+parent.TitleFrame.altTrolCount+"; "+parent.TitleFrame.altTrItem.toString()+", "+parent.TitleFrame.altTrQuant.toString())
    parent.TitleFrame.trolcount=parent.TitleFrame.altTrolCount
    for(i=0; i<parent.TitleFrame.altTrolCount; i++)
    {
      parent.TitleFrame.tritem[i]=parent.TitleFrame.altTrItem[i]
      parent.TitleFrame.trquant[i]=parseInt(parent.TitleFrame.altTrQuant[i])
    }
    parent.TitleFrame.altTrolCount=0
  }
  else
  {
    parent.TitleFrame.altTrolCount=parent.TitleFrame.trolcount
    for(i=0; i<parent.TitleFrame.altTrolCount; i++)
    {
      parent.TitleFrame.altTrItem[i]=parent.TitleFrame.tritem[i]
      parent.TitleFrame.altTrQuant[i]=parseInt(parent.TitleFrame.trquant[i])
    }
    //alert("store "+parent.TitleFrame.altTrolCount+"; "+parent.TitleFrame.altTrItem.toString()+", "+parent.TitleFrame.altTrQuant.toString())
  }
  parent.TitleFrame.trsort(false)
}

function incVAT(prexV)
{return Math.round(100*(1+parent.TitleFrame.vatrate)*prexV-0.4999999)/100}

function exVAT(princV)
{return Math.round(100*princV/(1+parent.TitleFrame.vatrate)+0.4999999)/100}

function additem(i)  // calls tradd with line i
{                    // adding quantBox pieces
  document.products.elements["quantBox"+i].value=parent.TitleFrame.tradd(i,parseInt(document.products.elements["quantBox"+i].value))
}   

function picturetype(i)
{
  if((stkCat(i).indexOf("Retail Pack")!=-1)||(stkCat(i).indexOf("POS Display Box")!=-1))
    {return ".jpg"}
  else
    {return ".gif"}
}

function incQuant(c)
{
  var box=parseInt(document.products.elements["quantBox"+c].value)
  if(tradeCust()==true){box=box+stkTrdMult(c)}
  else{box=box+1}
  if(box<0){box=0}
  if(tradeCust()==true){box=tradeMult(c,box)}
//  getElementById("parent.ContentFrame.products.quantBox"+c).value=box
  document.products.elements["quantBox"+c].value=box
}

function decQuant(c)
{
  var box=parseInt(document.products.elements["quantBox"+c].value)
  if(tradeCust()==true){box=box-stkTrdMult(c)}
  else{box=box-1}
  if(box<0){box=0}
  if(tradeCust()==true){box=tradeMult(c,box)}
  document.products.elements["quantBox"+c].value=box
}

function tradeMult(i,q,notify)  // return <mult> adjusted q for item i
{
  var retValue
  if(tradeCust()==true) {
    if(stkTrdMult(i)==0) {retValue=0}
    else {retValue=stkTrdMult(i)*Math.round(0.49+q/stkTrdMult(i))}
  }
  else {retValue=q}
  
  if(notify==true && retValue!=q && retValue!=0) {alert("This item is sold in order multiples of "+stkTrdMult(i))}
//  if(retValue==0) {alert("This item is not available individually")}

  return retValue
}

function next()
{
  do {
    parent.TitleFrame.currentcat++
    if(parent.TitleFrame.currentcat>parent.TitleFrame.cat-1)
      {parent.TitleFrame.currentcat=0}
  } while(tradeCust()!=true && stkRetUnits(catFindStock(parent.TitleFrame.currentcat,0))!=1)
}

function prev()
{
  do {
    parent.TitleFrame.currentcat--
    if(parent.TitleFrame.currentcat<0)
      {parent.TitleFrame.currentcat=parent.TitleFrame.cat-1}
  } while(tradeCust()!=true && stkRetUnits(catFindStock(parent.TitleFrame.currentcat,0))!=1)
}
