
function AjaxObjectGen(){
  if(window.XMLHttpRequest){
    return new XMLHttpRequest();
  }else if(window.ActiveXObject){
    var msxmls=new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',  'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP');
    for (var i=0; i<msxmls.length; i++){
      try{
        return new ActiveXObject(msxmls[i]);
      }catch(e){
      }
    }
  }
  throw new Error("Could not instantiate XMLHttpRequest");
}  

var cp1251=[0x82,  0x201A,0x84,  0x201E,0x85,  0x2026, 0x86 , 0x2020,0x87,  0x2021,0x88 , 0x20AC,0x91 , 0x2018,0x92,  0x2019, 0x93,  0x201C,0x94,  0x201D, 0x95,  0x2022, 0x96,  0x2013, 0x97,  0x2014, 0x98,  0x0020, 0x99,  0x2122];
function encodeCl1251(str){
  var out="";
  for(i=0; i<str.length; i++){
    if(str.charCodeAt(i)<=127) out=out+str.charAt(i);
    else{
      for(j=1; j<cp1251.length; j+=2){
        if(str.charCodeAt(i)==cp1251[j]){
          out=out+String.fromCharCode(cp1251[j-1]);
        }
      }
    }
  }
  return escape(out);
}


function getTagValue(node,tag){
  var result="";
  if(node){
    if(node.getElementsByTagName(tag)){
      var first_tag=node.getElementsByTagName(tag)[0];    
      if(first_tag){
        var x=first_tag.childNodes;  
        for(var i=0; i<x.length; i++) result=result+x[i].nodeValue;     
      }
    }
  }  
  return result;
}

function el(id){ return document.getElementById(id);}


function FormObject(ID){
  this.FormID=ID;
  this.FormName="";
  this.ProcessURL="";
  this.EmailID="";
  this.OnSubmit=null; 
  this.getID=new AjaxObjectGen();
  this.submitForm=new AjaxObjectGen();  
  var instance=this;
  this.getID.open('GET', "proxy.iml?PROXYURL=http://www.imlcentral.com/utilities/EmailTo/form.iml&FormID="+this.FormID+"&"+Date(), true);
  this.getID.onreadystatechange=function(){
    if(instance.getID.readyState==4){
        var rxml=instance.getID.responseXML;
        instance.FormName=getTagValue(rxml,'formname');
        instance.ProcessURL=getTagValue(rxml,'processurl');
        instance.EmailID=getTagValue(rxml,'emailid');
        instance.OnSubmit=document.forms[instance.FormName].onsubmit;
        document.forms[instance.FormName].onsubmit=function(){return instance.sendForm();};
    }
  }
  this.getID.send(null);  
}

FormObject.prototype.sendForm=function(){
 var sendNow=1;
 if(this.OnSubmit!=null){
    try{
      sendNow=this.OnSubmit();
    }catch(error){}
 }
 if(sendNow==1){
  var instance=this;
  var parameters;
  var submitURL="proxy.iml";
  if(this.ProcessURL.indexOf("http")==0){
     parameters="PROXYURL="+this.ProcessURL+"&EmailID="+this.EmailID;
  }else{
    submitURL=this.ProcessURL;
    parameters="EmailID="+this.EmailID;
  }
  var x=document.forms[instance.FormName].getElementsByTagName("*");
  for(var i=0; i<x.length; i++){  
    var name=""; var val="";
    if(x[i].nodeName=='INPUT'){
      if(x[i].type=="checkbox"||x[i].type=="radio"){
        if(x[i].checked) name=x[i].name; val=x[i].value;
      }else  name=x[i].name; val=x[i].value;
    }
    if(x[i].nodeName=='SELECT'){
       name=x[i].name;        
       if(x[i].selectedIndex>=0) val=x[i].options[x[i].selectedIndex].value;
    }
    if(x[i].nodeName=='TEXTAREA'){
       name=x[i].name; val=x[i].value;
    }
    if(name!=""){
      if(parameters=="") parameters=name+"="+encodeCl1251(val);
      else parameters=parameters+"&"+name+"="+encodeCl1251(val);
    }
  }
  this.submitForm.open('POST', submitURL, true);
  this.submitForm.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
  this.submitForm.setRequestHeader("Content-length", parameters.length);
  this.submitForm.setRequestHeader("Connection", "close");
  this.submitForm.onreadystatechange=function(){
    if(instance.submitForm.readyState==4){
      document.forms[instance.FormName].submit();
    }
  }
  this.submitForm.send(parameters);
 }
 return false;
}



