function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = "";
}

function setJrk(jrk) {
  if (document.getElementById('xJrk')) {
    var el=document.getElementById('xJrk');
    el.value=jrk;
  }
  else {
    var el=document.createElement("input");
    el.type="hidden";
    el.name="xJrk";
    el.id="xJrk";
    el.value=jrk;
    document.ordform.appendChild(el);
  }
}

function delRow(jrk) {
  setJrk(jrk);
  var el2=document.createElement("input");
  el2.type="hidden";
  el2.name="cmd";
  el2.value="Kustuta rida";
  document.ordform.appendChild(el2);
  document.ordform.submit();
}

function delFiRow(jrk) {
  setJrk(jrk);
  var el2=document.createElement("input");
  el2.type="hidden";
  el2.name="cmd";
  el2.value="Kustuta FI rida";
  document.ordform.appendChild(el2);
  document.ordform.submit();
}

function chooseAse(jrk,kood,kogus) {
  document.ordform.kood.value=kood;
  document.ordform.kogus.value=kogus;
  setJrk(jrk);
  var el2=document.createElement("input");
  el2.type="hidden";
  el2.name="cmd";
  el2.value="Vali asendus";
  document.ordform.appendChild(el2);
  document.ordform.submit();  
}

function chooseVaba(kood) {
 document.ordform.kood.value=kood;
 // document.ordform.kogus.value=kogus;
 var el2=document.createElement("input");
 el2.type="hidden";
 el2.name="cmd";
 el2.value="Vali vaba";
 document.ordform.appendChild(el2);
 document.ordform.submit();  
}

function chooseOEM(srcnr,partnr,suppl) {
 document.ordform.kood.value=partnr;
 var el=document.createElement("input");
 el.type="hidden";
 el.name="cmd";   
 el.value="Vali oem";
 document.ordform.appendChild(el);
 var el=document.createElement("input");
 el.type="hidden";
 el.name="srcnr"; 
 el.value=srcnr;  
 document.ordform.appendChild(el);
 var el=document.createElement("input");
 el.type="hidden";
 el.name="suppl"; 
 el.value=suppl;  
 document.ordform.appendChild(el);
 document.ordform.submit();  
}

function ch2inp(x,jrk,kogus,kood) {
  if (window.chKogus==0) {
    if (x.innerHTML.length<40) {
      if (x.innerHTML.search('#0468bb')!=-1) {
        var el=document.createElement("input");
        el.type="hidden";
        el.name="chkFI";
        el.value=jrk;
        document.ordform.appendChild(el);
      }
      window.chKogus=1;
      x.innerHTML='<input type="text" id="'+jrk+'" name="chkK" value="'+kogus+'" size="2" maxlength="4"><input type="submit" name="cmd" value="mk">';
      var el=document.createElement("input");
      el.type="hidden";
      el.name="chkR";
      el.value=jrk;
      document.ordform.appendChild(el);
      document.ordform.kood.value=kood;
      obj=document.getElementById(jrk);
      obj.focus();
    }
  }
}

function saveState(x,k) {
  if(x=='K') {
    if(k)
        document.ordform.ohkm.checked=false;
  }
  if(x=='Z') {
    if(k)
        document.ordform.ztk.checked=false;
  }
  if(x=='H') {
    if(k)
        document.ordform.showhh.checked=false;
  }
  if(x=='T') {
    if(k)
        document.ordform.tellkinn.checked=false;
  }
  var el=document.createElement("input");
  el.type="hidden";
  el.name="cmd";
  el.value="Salvesta "+x+"state";
  document.ordform.appendChild(el);
  document.ordform.submit();
}

function saveEmail() {
  var el=document.createElement("input");
  el.type="hidden";
  el.name="cmd";
  el.value="SalvestaEmail";
  document.ordform.appendChild(el);
  document.ordform.submit();
}

function vabaotsPop(jrk,kood,kogus) {
  showPop();
  document.ordform.kogus.value=kogus;
  setJrk(jrk);
  url="fsearch.py?wskood="+kood+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<iframe name="PIS" id="PIS" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
}

function oemotsPop(kood) {
  showPop();
  url="oemsearch.py?wskood="+kood+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<iframe name="PIS" id="PIS" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
}

function queryPop(jrk,inf,nimi,firma) {
  showPop();
  setJrk(jrk);
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<span><u>Päring tehasetoote tellimiseks.</u><br><br><b>NB! Palun lisage oma e-maili aadress allpool.</b><br><br> \
  <form name="ask" action="/cgi-bin/ask.cgi" target="_blank" method="POST" enctype="multipart/form-data"> \
  <input type="hidden" name="user" value="'+firma+'"> \
  <input type="hidden" name="nimi" value="'+nimi+'"> \
  <textarea name="asktext" cols="60" rows="7">Soovin tellida tehase tellimusena '+inf+'\r\n\r\nPalun tagasisidet emailile: __email__</textarea> \
  <input type="submit" name="cmd" value=" Send " onClick="parent.cancelPop();"> \
  </form></span>';
}

function vabaotsNS(kood) {
  url="fsearch.py?wskood="+kood+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<iframe name="PIS" id="PIS" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
}

function oemotsNS(kood) {
  url="oemsearch.py?wskood="+kood+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<iframe name="PIS" id="PIS" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
}

function sobivPop(kood) {
  url="usedIn.py?c="+kood+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<iframe id="popSisuIframe" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
  showPop();
}

function orderPop(cookie,trans,mrk) {
  url="index.py?cmd=sendNOW&ck="+cookie+"&trans="+trans+"&mrk="+mrk+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<span id="wait"><center><b>Palun oodake, tellimus edastatakse...</b><br><br><img src="img/loading.gif"></center></span><br><br><iframe id="popSisuIframe" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
  showPop();
}

function orderPop2(cookie,trans,mrk) {
  url="index.py?cmd=sendNOW2&ck="+cookie+"&trans="+trans+"&mrk="+mrk+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<span id="wait"><center><b>Palun oodake, tellimus edastatakse...</b><br><br><img src="img/loading.gif"></center></span><br><br><iframe id="popSisuIframe" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
  showPop();
}

function orderPop3(cookie,trans,mrk) {
  url="index.py?cmd=sendNOW3&ck="+cookie+"&trans="+trans+"&mrk="+mrk+"&rnd="+Math.random();
  var node = document.getElementById('popup_sisu');
  node.innerHTML='<span id="wait"><center><b>Palun oodake, tellimus edastatakse...</b><br><br><img src="img/loading.gif"></center></span><br><br><iframe id="popSisuIframe" frameborder="no" width="700" height="400" src="'+url+'"></iframe>';
  showPop();
}

function hideWait() {
  document.getElementById('wait').innerHTML='';
  //document.getElementById('wait').style.visibility = 'hidden';
}

if(navigator.appName == "Microsoft Internet Explorer"){
  var canSee = 'block'
  } else {
  var canSee = 'table-row';
}

function cancelPop() {
  document.ordform.kogus.value=1;
  hidePop();
}

function hidePop() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideshow').style.visibility = 'hidden'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.hideshow.visibility = 'hidden'; 
} 
else { // IE 4 
document.all.hideshow.style.visibility = 'hidden'; 
} 
} 
}

function showPop() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideshow').style.visibility = 'visible'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.hideshow.visibility = 'visible'; 
} 
else { // IE 4 
document.all.hideshow.style.visibility = 'visible'; 
} 
} 
} 

function toggleLayer(fieldname) {
  var xx =  document.getElementsByName(fieldname);
  for (var i=0; i<xx.length; i++) { 
    if(xx[i].style.display== 'none')
      xx[i].style.display=canSee;
    else
      xx[i].style.display="none";
  }
}
function toggleAll() {
  var see = 'none';
  if (window.shAse==1) {
    window.shAse=0;
    document.ordform.showAse.checked=false;
    }
  else {
    window.shAse=1;
    see = canSee;
    }
  var xx =  document.getElementsByTagName('TR');
  for (var i=0; i<xx.length; i++) 
   if (xx[i].id.substring(0,3)=='ase')
      xx[i].style.display=see;
}  
function showAll() {
  var xx =  document.getElementsByTagName('TR');
  for (var i=0; i<xx.length; i++) 
   if (xx[i].id.substring(0,3)=='ase')
      xx[i].style.display=canSee;
}  

function Countdown(what,when) {
  this.when = when;
  this.what = what;

  function setElement(id, value) {
    window.document.getElementById(id).innerHTML = value;
  }

  function AddNull(num) {
	return ((num >= 0)&&(num < 10))?"0"+num:num+"";
  }

  function countdown() {
    var seconds_left  = this.when;
    this.when-=1;

    if(seconds_left <= 1800) {
      window.document.getElementById("timer"+this.what).style.color='red';
    }
    if(seconds_left <= 0) {
      clearTimeout(countdown.timer);
      window.document.getElementById(this.what).innerHTML='';
      window.document.getElementById(this.what+"t").innerHTML='';
      return;
    }

    seconds  = Math.floor(seconds_left / 1) % 60;
    minutes  = Math.floor(seconds_left / 60) % 60;
    hours    = Math.floor(seconds_left / 3600) % 24;

    var setTime = AddNull(hours) + ":" + AddNull(minutes) + ":" + AddNull(seconds);
    window.document.getElementById("timer"+this.what).innerHTML = setTime;
    countdown.timer = setTimeout(countdown, 1000);
  }

  function start() {
    this.timer = setTimeout(countdown, 3000);
  }

  start(when);
}

function s_h(wh,sh) {
  if (sh) {
    document.getElementById(wh).style.visibility='visible';
    }
  else
    document.getElementById(wh).style.visibility='hidden';
}

function toggleID(id) {
  var xx =  document.getElementById(id);
  if (xx) {
    if (xx.style.display==canSee)
      xx.style.display="none";
    else
      xx.style.display=canSee;
  }
}

