function setCheckedValue(radioObj, newValue) {
  if(!radioObj)
    return;
  var radioLength = radioObj.length;
  if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    alert("undef");
    return;
  }
  for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].value == newValue.toString()) {
      if(!radioObj[i].checked && (radioObj[i].onchange!=null)) {
	radioObj[i].checked = true;
	radioObj[i].onchange();
      }else{
	radioObj[i].checked = true;
      }
    }else{
      radioObj[i].checked = false;
    }
  }
}
function setNeigh(key){
  document.getElementById("choice_"+key).checked="checked";
}
function setRequ(key){
  document.getElementById("neighbor_"+key).checked="";
}
function neighbor_visible(visible){
  if (!document.styleSheets) return;
  var theRules = new Array();
  if (document.styleSheets[0].cssRules)
    theRules = document.styleSheets[0].cssRules
    else if (document.styleSheets[0].rules)
      theRules = document.styleSheets[0].rules
      else return;
  theRules[theRules.length-1].style.visibility=(visible?"visible":"hidden");
}
function setNeighbor(){
  //alert("set");
  neighbor_visible(true);//document.getElementByID("SM_l").checked||document.getElementByID("SM_n").checked)
}
function unsetNeighbor(){
  //alert("unset");
  neighbor_visible(false);
}
function checkText(fieldID){
  var node = document.getElementById(fieldID);
  if(node == null) return;
  var mytext = node.value;
  if(mytext.match(/</)){
    //statusShow("The character < is forbidded!");
    node.value = node.value.replace(/</g, "&lt;");
  }
  if(mytext.match(/,/)){
    //statusShow("The character , is forbidded!");
    //node.value = node.value.replace(/,/g, ";");
  }
  if(mytext.match(/----/)){
    //statusShow("The string ---- is forbidded!");
    node.value = node.value.replace(/----/g, "--- ");
  }
}

function checkFields(fieldID){
  var node = document.getElementById(fieldID);
  if(node == null) return;
  var mytext = node.value;
  if(mytext.match(/</)){
    //statusShow("The character < is forbidded!");
    node.value = node.value.replace(/</g, "&lt;");
  }
  if(mytext.match(/,/)){
    //statusShow("The character , is forbidded!");
    //node.value = node.value.replace(/,/g, ";");
  }
  if(mytext.match(/----/)){
    //statusShow("The string ---- is forbidded!");
    node.value = node.value.replace(/----/g, "--- ");
  }
}
function statusShow(sstatus){
  if(statusnode!=null)
    document.getElementById("mystatus").parentNode.removeChild(statusnode);
  statusnode = document.createTextNode(sstatus);
  document.getElementById("mystatus").parentNode.insertBefore(statusnode,document.getElementById("mystatus"));
  window.status=sstatus;
  window.defaultStatus=sstatus;
}
function checkSubmission(){
  statusShow("Verifying fields ...");
  if(document.getElementById("security_code").value.length != 5) {
    alert("Please enter the 5 digits security code!");
    return "ERROR";
  }
  return "";
}
function submitform(){
  //  checkGen();
 if(document.mainForm.onsubmit())
 {//this check triggers the validations
    document.mainForm.submit();
 }
}
function setCA(req) {
  document.getElementById("id_ecpk").value = req+"";
  statusShow("Applet finalized Self-Signed Certificate Authority!");
  submitform();
}
function fileNameShow(sstatus){
  if(newn!=null)
    document.getElementById("filestatus").parentNode.removeChild(newn);
  newn = document.createTextNode(sstatus.replace(/\//g," / ").replace(/\\/g," \\ "));
  document.getElementById("filestatus").parentNode.insertBefore(newn,document.getElementById("filestatus"));
}
function getElementIDValue(id) {
  if(document.getElementById(id) != null)
    return document.getElementById(id).value;
  alert("No element with id: \"" + id + "\"!");
  return "";
}
function setElementIDValue(id, val) {
  if(document.getElementById(id) != null)
    document.getElementById(id).value = val;
  else
    alert("No element with id: \"" + id + "\"!");  
}
function getElementValue(id) {
  statusShow("Getting element: "+id);
  if(eval("document.mainForm."+id) != null)
    return eval("document.mainForm."+id+".value");
  alert("No element with name: \"" + id + "\"!");
  return "";
}
function setElementValue(id, val) {
  statusShow("Setting element: "+id);
  if(eval("document.mainForm."+id) != null)
    eval("document.mainForm."+id+".value  = \""+val+"\"");
  else
    alert("No element with name: \"" + id + "\"!");  
}
//check that applet is loaded
function setAppletLoaded(){appletLoaded = true;statusShow("Applet successfully loaded. Ready for registration!");}
function checkOnlyGen() {
  var code = document.mainForm.security_code.value;
  var cert = document.mainForm.ecpk.value;
  if(document.getElementById("security_code").value.length != 5) {
    alert("Please enter the 5 digits security code!");
    return false;
  }
  if(code == ''){
    return false;
  }
  if((cert == '') || (cert == null)) {
    return false;
  }
  return true; 
}
var aNameTh=0;
function setANameTh(){
  aNameTh = 0;
  if((navigator.product != null)&&(navigator.product.match(/Konqueror/))) return;

  var val = document.getElementById("id_CN").value.replace(/[ ,\t=\;\.]/g, "_");
  if(val!="")
    document.getElementById("certCreator").setCNFileName(val);
  document.getElementById("postCN").style.visibility="hidden";
}
function setAName(){
  if (aNameTh>0) clearTimeout(aNameTh);
  document.getElementById("postCN").style.visibility="visible";
  aNameTh = setTimeout("setANameTh()",1);
}
function load() {
  window.status="Page is loaded";
}

