var CNparts = new Array();
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function buildCN() {
  var k=1;
  var valCN="";
  for(;k<CNparts.length;k++) {
    //alert("Get: "+CNparts[k]);
    if(CNparts[k]!=null) valCN =  valCN + " " + document.getElementById(CNparts[k]).value;
  }
  if(k==1) return;
  document.getElementById("id_CN").value = valCN.trim();
  if(document.getElementById("id_CN").onblur != null) document.getElementById("id_CN").onblur();
}

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 string ---- is forbidded!");
    node.value = node.value.replace(/----/g, "--- ");
  }
}

function checkSubmission(){
  statusShow("Verifying fields ...");
  if(document.getElementById("security_code").value.length != 5) {
    alert("Please enter the 5 digits security code!");
    return "ERROR";
  }
  if(document.getElementById("id_CN").value.length <1) {
    alert("Please type a name for the owner of the certificate!");
    return "ERROR";
  }
  return "";
}
function submitform(){
  //  checkGen();
  if(document.mainForm.onsubmit()) {
    //this check triggers the validations
    var filename="Error retrieving name";
    if(newn!=null) fileName=newn.nodeValue;
    alert(fileWarning(fileName));
    document.mainForm.submit();
 }
}
function setCertReq(req) {
  document.getElementById("id_ecpk").value = req+"";
  statusShow("Applet finalized Certificate Request!");
  submitform();
}
function statusShowInit(sstatus){
  document.getElementById("upstatus").innerHTML=sstatus.replace("......","<BR>");
  statusShow(sstatus);
}
function statusShow(sstatus){
  if(document.getElementById("mystatus")==null) return;
  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 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;
  if(document.getElementById("upstatustable") != null)
    document.getElementById("upstatustable").style.visibility="hidden";
  if(document.getElementById("upstatus") != null)
    document.getElementById("upstatus").innerHTML="";
  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(document.getElementById("id_CN").value.length <1) {
    alert("Please enter a name for the owner of the certificate!");
    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, "_");
  //document.getElementById("postCN").innerHTML="Wait a moment! I'm working...";
  //alert("a");
  if(val!="")
    document.getElementById("certCreator").setCNFileName(val);
  document.getElementById("postCN").style.visibility="hidden";
  //document.getElementById("postCN").innerHTML="";
}
function setAName(){
  if (aNameTh>0) clearTimeout(aNameTh);
  document.getElementById("postCN").style.visibility="visible";
  aNameTh = setTimeout("setANameTh()",1);
}
function load() {
  //window.status="Page is loaded";
if(!appletLoaded)
  fileNameShow("A proposed name for the file with secret keys will be shown here by the applet!");
 else setAppletLoaded();
}
window.onload=load;

