/*-------------------------------------------------------------------*/
/* ---== Universal Forms UltraCheck : © Dracon.Team?  --- v3.2 ==--- */
/*-------------------------------------------------------------------*/
/* Syntax:
/* Field properties for error alert better description: alt="the Destination Country"
/* Submitting button gets disabled and it's title changed to the 3rd array member value
/* For checking e-mails validity use word 'mail' anywhere in the fieldname, e.g: 'email' or 'Mail3'
/* For checking numbers validity use word 'num' anywhere in the fieldname, e.g: 'num3' or 'AmountNum'
/* For checking any 2 fields confirmation match use any names in pairs as the 4th array member
/* For NOT checking a field use the <DisabledChar> as the first letter of its name
/* onClick="UltraCheck(this.form.name,this,'[button text]','[confirm1],[confirm2]')"
/*-------------------------------------------------------------------*/

var DisabledChar = '_';
var EmptyField = false;
var BadPass = false;
var xPass = new Array();
var xPassCount = 0;
var MultiBox = false;

function UltraCheck(FormName,Button,Sending) {
  Pass = (arguments[3]) ? arguments[3] : '';
  FieldsNum = window.document.forms[FormName].elements.length
  for (i=0; i<FieldsNum; i++) {
    if (window.document.forms[FormName].elements[i].name != null && window.document.forms[FormName].elements[i].name.charAt(0) != DisabledChar) {
      /* CheckBox & Radio */
      if (window.document.forms[FormName].elements[i].type == 'checkbox' || window.document.forms[FormName].elements[i].type == 'radio') {
        /* Related CheckBox Test */
        for (x=0; x<FieldsNum; x++) {    
          if (window.document.forms[FormName].elements[x].name == window.document.forms[FormName].elements[i].name) {
            if (window.document.forms[FormName].elements[x].checked == true) {
              MultiBox = true; break;        
            }
          }
          else MultiBox = false;
        }
        if (window.document.forms[FormName].elements[i].checked == false && !MultiBox) {
          /* If alt exists, use it as error return */
          ReturnMsg = (window.document.forms[FormName].elements[i].alt) ? 'alt' : 'name';
          alert('Please tick ' + window.document.forms[FormName].elements[i][ReturnMsg]);
          window.document.forms[FormName].elements[i].focus();
          EmptyField = true; break;
        }
      }
      /* Other Input Types */
      if (!window.document.forms[FormName].elements[i].value) { 
        /* If alt exists, use it as error return */
        ReturnMsg = (window.document.forms[FormName].elements[i].alt) ? 'alt' : ((window.document.forms[FormName].elements[i].title) ? 'title' : 'name');
        /* Select or Enter */
        ActMsg = (window.document.forms[FormName].elements[i].type.toLowerCase().indexOf('select') != -1) ? 'select' : 'enter';
        alert('Please ' + ActMsg + ' ' + window.document.forms[FormName].elements[i][ReturnMsg]);
        window.document.forms[FormName].elements[i].focus();
        EmptyField = true; break;
      }
      else if (window.document.forms[FormName].elements[i].name.toLowerCase().indexOf('mail') != -1) {
        if (EmailCheck(window.document.forms[FormName].elements[i].value) == 0) {
          alert('Please enter valid e-mail address');
          window.document.forms[FormName].elements[i].focus();
          EmptyField = true; break;
        }
      }
      else if (window.document.forms[FormName].elements[i].name.toLowerCase().indexOf('num') != -1) {
        if (window.document.forms[FormName].elements[i].value * 0 != 0) {
          alert('Sorry, this is not a valid number');
          window.document.forms[FormName].elements[i].focus();
          EmptyField = true; break;
        }
      }      
    }
  }
  if (!EmptyField) { 
    if (Pass != '') {
      Pass = Pass.split(',');
      for (i=0; i<Pass.length; i = i + 2) {
        if (window.document.forms[FormName][Pass[i]].value != window.document.forms[FormName][Pass[i+1]].value) {
        alert(window.document.forms[FormName][Pass[i]].name + ' confirmation does not match');
        window.document.forms[FormName][Pass[i+1]].focus();
        window.document.forms[FormName][Pass[i+1]].select();
        BadPass = true;
        }
      }
    }
    if (!BadPass) { 
      Button.blur();
      Button.disabled = true;
      Button.value = Sending;
      window.document.forms[FormName].submit();
    }
  }
  EmptyField = false;
  BadPass = false;
  MultiBox = false;
}

function EmailCheck(emailStr) {
  if (emailStr=='') {
    return 1 // Blank is OK, it's checked elsewhere .. 
  }
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  var matchArray=emailStr.match(emailPat)
  if (matchArray==null) {
    return 0
  }
  var user=matchArray[1]
  var domain=matchArray[2]

  // See if "user" is valid
  if (user.match(userPat)==null) {
    return 0
  }

  /* if the e-mail address is at an IP address (as opposed to a symbolic
    host name) make sure the IP address is valid. */
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null)  {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255)  {
        return 0
      }
    }
    return 1
  }

  // Domain is symbolic name
  if (parseInt(navigator.appVersion.charAt(0))==5 && navigator.appName == "Netscape") {
    var strdomainPat=/^[^\s\(\)<>@,;:\\\"\.\[\]]+(\.[^\s\(\)<>@,;:\\\"\.\[\]]+[^\s\(\)<>@,;:\\\"\.\[\]]+)*$/
    var strdomainArray=domain.match(strdomainPat);
    if (strdomainArray==null) {
      return 0//"Please enter a valid email address.\nFor example: john@company.com"
    }
  }
  else {
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
      return 0//"Please enter a valid email address.\nFor example: john@company.com"
    }
  }

  /* Now we need to break up the domain to get a count of how many atoms
     it consists of. */
  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length

  if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
   // The address must end in a three-letter domain, or two letter country.
   return 0
  }

  // Make sure there's a host name preceding the domain.
  if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return 0
  }

  // If we've gotten this far, everything's valid!
  return 1;
}

