var the_field='the field';
var must_contain_email='must contain a valid e-mail address.';
var must_be_filled='must be filled.';
var must_contain_number='must contain a number.';
var must_contain_number_between='must contain a number between';
var and_string='and';
var must_be_greather_or_equal_than='must be greater or equal than';
var errors_in_form='While checking your form, the following errors appear:';
var myfocus;

function setFocus() {
 if(!myfocus) myfocus=window;
  myfocus.focus();
}

window.onLoad=setFocus();

var enterForm;
function enter_key_trap(e)
{
    var keyPressed;

    if (document.layers) {
        keyPressed = String.fromCharCode(e.which);
    } else if (document.all) {
        keyPressed = String.fromCharCode(window.event.keyCode);
    } else if (document.getElementById) {
        keyPressed = String.fromCharCode(e.keyCode);
    }

    if ((keyPressed == "\r" || keyPressed == "\n")) {
        enterForm.submit();
    }
}
if(enterForm) {
if (window.document.captureEvents != null) {
    window.document.captureEvents(Event.KEYPRESS);
    window.document.onkeypress = enter_key_trap;
}
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

var focused=false;

function fieldFocus(myval) {
 if(!focused) {
    focused=true;
    myfocus=myval;
    myval.select();
    setFocus();
 }
}

function validateForm() {
  var i,p,q,pt,nm,test,name,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) {
    name=args[i+1];test=args[i+2]; val=findObj(args[i]);myval=val;
    if (val) { nm=name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) {
      	p=val.indexOf('@');pt=val.indexOf('.');
        if (pt<1 || p<1 || p==(val.length-1)) {
          errors+='- '+the_field+' \''+nm+'\' '+must_contain_email+'\n';
          fieldFocus(myval);
        }
      } 
      else if (test!='R') {
        if (isNaN(val)) { 
          errors+='- '+the_field+' \''+nm+'\' '+must_contain_number+'\n';
          fieldFocus(myval);
        }
        if (test.indexOf('inRange') != -1) {
          p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<parseInt(min,10) || parseInt(max,10)<val){
            errors+='- '+the_field+' \''+nm+'\' '+must_contain_number_between+' '+min+' '+and_string+' '+max+'.\n';
            fieldFocus(myval);
          }
        }
        if (test.indexOf('isSup') != -1) {
          p=test.indexOf(':');
          min=test.substring(6);
          if (val<parseInt(min,10)){
            errors+='- '+the_field+' \''+nm+'\' '+must_be_greather_or_equal_than+' '+min+'.\n';
            fieldFocus(myval);
          }
        }
      }
      } 
      else if (test.charAt(0) == 'R') {
        errors += '- '+the_field+' \''+nm+'\' '+must_be_filled+'\n';
        fieldFocus(myval);
      }
    } 	
  } 
if (errors) alert(errors_in_form+'\n'+errors);
focused=false;
document.returnValue = (errors == '');
}

function setPort(val,portInput) {
 if(val=="pop3") portInput.value='110';
 else portInput.value='143';
}