<!--
// Java script validation module - first copied from 'author unknown' and enhanced by Marc Lucas
// and Paul Anderson to include extra validation rules. If you want to know how it works, get a manual.
//
// Rules available are as follows....
// R    -   Field is required (this parameter can be set on the front most others eg RIsNum)
// r    -   Is required although a single 0 can be input to override this.
// 
// isDate   - the date format should be dd-mm-yyyy
// isDtPast - must be valid date and may not be in the future.
// n1,n2,n3 - one of the three fields must be present - must always be 3 parameters passed in with n1,n2,n3 as types.
// s1 to s5 - one of the entries must be selected (not set to -Select One-)
// isDato   - The date format should be correct or set to string dd-mm-yyyy
// isTime   - must be valid time as HH:MM
// isEmail  - must be a fairly valid Email address
// isPhone  - basicallu allows numerics with a single space in only.
// isPostcode - work in progress.
// isSelect - stops anyone not selecting an item from a drop down where the first line is 'select one' etc.
// isPassword1 & isPassword2 - must both be sent as items - will compare and error if both not the same
// isNum    - allows only numeric and decimal points
// inRange  - check for a range of values to be supplied. 'inRange00000066:00000088' checks for the value being GT 66 and LE 88
// isFilestem - check to make sure no chars are set to "/[\\\/*\?:"><|]/"
//
// Amendment History............
// 15-12-2003 Amended Marc Lucas - new options - r, n1,n2,n3 and if date past! - mostly working
//            field(s).
// 16-12/2003 Amended paul Anderson (to get Marc Lucas's last amendment working)
//
// 07-01-2004 Amended by Paul Anderson to include filestem checking.
// 06-02-2004 Marc Lucas - add little 'p' as mandatory field or allow 'NA' as input
// 12-03-2004 Amended by Paul Anderson (strengthen the date validations

function MM_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=MM_findObj(n,d.layers[i].document);
  if (!x && d.getElementById)
      x=d.getElementById(n);
  return x;
}
function MM_validateForm() {
    var vax,vax2,i,i2,i3,i4,p,q,nm,test,num,min,max,ddint,mmint,mmint1,mmint2,yyyyint,errors='',args=MM_validateForm.arguments
    for (i=0; i < (args.length - 2); i+=3)
        {
          test=args[i+2]
          val=MM_findObj(args[i])
          nm=val.id  
          vax=val.value
          if ((vax) != "") { 
              while (vax.substr(0,1) == ' ') {
                     vax = vax.substr(1,vax.length-1);
                   };

//              alert(' starting  ');

              if (test.indexOf('isDate') != -1) 
                 { 
                  if ((vax.substr(2,1) != '-')||(vax.substr(5,1) != '-')||
                      (isNaN(vax.substr(0,2)))||(parseInt(vax.substr(0,2),10) < 1)||
                      (isNaN(vax.substr(3,2)))||(parseInt(vax.substr(3,2),10) < 1)|| 
                      (isNaN(vax.substr(6,4)))||(parseInt(vax.substr(6,4),10) < 1)||
                      (vax.length != 10)||
                      (vax.substr(6,1) == ' ')||
                      (vax.substr(7,1) == ' ')||                      
                      (vax.substr(8,1) == ' ')||                      
                      (vax.substr(9,1) == ' ')||
                      (vax.substr(0,1) == ' ')||
                      (vax.substr(1,1) == ' ')||
                      (vax.substr(3,1) == ' ')||
                      (vax.substr(4,1) == ' ')||
                      (vax.substr(6,2) < '19'))         
                       {
                        val.select();
                        val.focus();
                        errors += '- '+nm+' must be "dd-mm-yyyy" format\n';
                       }
                  else {
                        ddint   = parseInt(vax.substr(0,2),10);
                        mmint   = parseInt(vax.substr(3,2),10);
                        mmint1  = mmint - 1;
                        mmint2  = mmint1 * 2;
                        yyyyint = parseInt(vax.substr(6,4),10);
                        maxdd   = '312831303130313130313031';
                        if ((yyyyint % 4) == 0 )
                           {
                            maxdd = '312931303130313130313031';
                           };
                        if ((mmint < 1)||(mmint > 12)||(ddint > (parseInt(maxdd.substr(mmint2,2),10))))
                            {
                             val.select();
                             val.focus();
                             errors += '- '+nm+' must be "dd-mm-yyyy" format please\n';
                            };
                       };
                 };

              if (test.indexOf('isDtPast') != -1) 
                  { 
                   if (vax != '0') {
                      if ((vax.substr(2,1) != '-')||(vax.substr(5,1) != '-')||
                         (isNaN(vax.substr(0,2)))||(parseInt(vax.substr(0,2),10) < 1)||
                         (isNaN(vax.substr(3,2)))||(parseInt(vax.substr(3,2),10) < 1)|| 
                         (isNaN(vax.substr(6,4)))||(parseInt(vax.substr(6,4),10) < 1)||
                         (vax.length != 10)||
                         (vax.substr(0,1) == ' ')||
                         (vax.substr(1,1) == ' ')||
                         (vax.substr(3,1) == ' ')||
                         (vax.substr(4,1) == ' ')||                              
                         (vax.substr(6,1) == ' ')||
                         (vax.substr(7,1) == ' ')||                   
                         (vax.substr(8,1) == ' ')||                   
                         (vax.substr(9,1) == ' ')||
                         (vax.substr(6,2) < '19'))      
                          {
                           val.select();
                           val.focus();
                           errors += '- '+nm+' must be "dd-mm-yyyy" format and in the past\n';
                          }
                      else {
                           ddint   = parseInt(vax.substr(0,2),10);
                           mmint   = parseInt(vax.substr(3,2),10);
                           mmint1  = mmint - 1;
                           mmint2  = mmint1 * 2;
                           yyyyint = parseInt(vax.substr(6,4),10);
                           maxdd   = '312831303130313130313031';
                           if ((yyyyint % 4) == 0 ) {
                               maxdd = '312931303130313130313031';
                           };
                           if ((mmint < 1)||(mmint > 12)||(ddint > (parseInt(maxdd.substr(mmint2,2),10)))) {
                                val.select();
                                val.focus();
                                errors += '- '+nm+' must be "dd-mm-yyyy" format please\n';
                           }
                           else { tt = new Date;
                                  now = new Date;
                                  now.getDate();
                                  tt.setDate(ddint);
                                  tt.setMonth(mmint1);
                                  tt.setYear(yyyyint);
                                  if (!(tt < now)) {
                                       val.select();
                                       val.focus();
                                       errors += '- Date must be in the past\n';
                                  };
                           };
                       };
                    };
              };

              if ((test.indexOf('isDato') != -1) && (vax.substr(0,10) != 'dd-mm-yyyy'))
                 { 
                  if ((vax.substr(2,1) != '-')||(vax.substr(5,1) != '-')||
                      (isNaN(vax.substr(0,2)))||(parseInt(vax.substr(0,2),10) < 1)||
                      (isNaN(vax.substr(3,2)))||(parseInt(vax.substr(3,2),10) < 1)|| 
                      (isNaN(vax.substr(6,4)))||(parseInt(vax.substr(6,4),10) < 1)||
                      (vax.length != 10)||
                      (vax.substr(6,1) == ' ')||
                      (vax.substr(7,1) == ' ')||                      
                      (vax.substr(8,1) == ' ')||                      
                      (vax.substr(9,1) == ' ')||
                      (vax.substr(0,1) == ' ')||
                      (vax.substr(1,1) == ' ')||
                      (vax.substr(3,1) == ' ')||
                      (vax.substr(4,1) == ' ')||
                      (vax.substr(6,2) < '19'))         
                       {
                        val.select();
                        val.focus();
                        errors += '- '+nm+' must be "dd-mm-yyyy" format.\n';
                       }
                  else {
                        ddint   = parseInt(vax.substr(0,2),10);
                        mmint   = parseInt(vax.substr(3,2),10);
                        mmint1  = mmint - 1;
                        mmint2  = mmint1 * 2;
                        yyyyint = parseInt(vax.substr(6,4),10);
                        maxdd   = '312831303130313130313031';
                        if ((yyyyint % 4) == 0 )
                           {
                            maxdd = '312931303130313130313031';
                           };
                        if ((mmint < 1)||(mmint > 12)||(ddint > (parseInt(maxdd.substr(mmint2,2),10))))
                            {
                             val.select();
                             val.focus();
                             errors += '- '+nm+' must be "dd-mm-yyyy" format please\n';
                            };
                  };
                 };

              if (test.indexOf('isTime')!=-1) {
                  if ((vax.length != 5)||(vax.substr(2,1) != ':')|| 
                      (isNaN(vax.substr(0,2)))||(parseInt(vax.substr(0,2),10) < 0)||(parseInt(vax.substr(0,2),10) > 23)||
                      (isNaN(vax.substr(3,2)))||(parseInt(vax.substr(3,2),10) < 0)||(parseInt(vax.substr(3,2),10) > 59)) {
                       val.select();
                       val.focus();
                       errors+='- '+nm+' must be "hh:mm" format\n';
                  };
              };

              if (test.indexOf('isEmail') != -1) {
                  vax = val.value;
                  p = vax.indexOf('@')     
                  if ( p < 1 || p == (vax.length-1)) {
                      val.select();
                      val.focus();
                      errors+='- '+nm+' format is invalid\n';
                  }
              };

              if (test.indexOf('isFilestem') != -1) {
                  vax = val.value;
                  vok = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789,.£%\'()-_&';
                  vokrm = 0;
                  vokch = '.';
                  for (i3=0; i3 < vax.length; i3+=1) {
                       vokr = 0;
                       for (i4=0; i4 < vok.length; i4+=1) {
                            if (vax.substr(i3,1) == vok.substr(i4,1)) {
                                vokr = 1;
                            };
                       };
                       // keep a copy of the last invalid character for the display
                       if (vokr == 0) {
                         vokch = vax.substr(i3,1);
                       };
                       if (vokr == 0) {
                           vokrm = 1;
                       };      
                  };
                  if (vokrm == 1) {
                      val.select();
                      val.focus();
                      errors+='- '+nm+' please do not use ' + vokch + ' characters \n';
                  };
              };


              if (test.indexOf('isPhone') != -1) {
                   vax2 = '';
                   i4 = 0;
                   for (i3=0; i3 < vax.length; i3+=1) {
                      if ( vax.substr(i3,1) != ' ' && vax.substr(i3,1) != '(' && vax.substr(i3,1) != ')' && vax.substr(i3,1) != '+')
                            { vax2 += vax.substr(i3,1);
                              i4 += 1; 
                       };
                   };
                   if (isNaN(vax2)) {
                        val.select();
                        val.focus();
                        errors+='- '+nm+' must be valid and may include + and ( ) \n';
                   };
              };

              if (test.indexOf('isPostcode') != -1) {
//              Need some code in here one day - check for space are postcodes always 3_3 digits?
              };


              if (test.indexOf('isSelect') != -1) {
                vsel = val.value;
                if ((vsel == 'select one') || (vsel == '-Select One-')) {
                    errors+='- '+nm+' must be selected\n';
                };
              };


              if (test.indexOf('isPassword1') != -1) {
//              just save the thing for the next check
                pass1 = val.value;
              };

              if (test.indexOf('isPassword2') != -1) {
//              check it against the one we saved last time 
                pass2 = val.value;
                if (pass1 != pass2) {
                    val.select();
                    val.focus();
                    errors+='- Passwords do not match\n';
                }
              };

              if (test.indexOf('isEmail1') != -1) {
//              just save the thing for the next check
                pass1 = val.value;
              };


              if (test.indexOf('isEmail2') != -1) {
//              check it against the one we saved last time 
                pass2 = val.value;
                if (pass1 != pass2) {
                    val.select();
                    val.focus();
                    errors+='- Email addresses do not match\n';
                }
              };
              if (test.indexOf('isNum') != -1) {
                  num = parseFloat(vax);
                  if (isNaN(vax)) {
                      val.select();
                      val.focus();
                      errors+='- '+nm+' must be numeric\n'
                  }
              };
              if (test.indexOf('inRange') != -1) {
                  p=test.indexOf(':');
                  min=test.substr(8,p);
                  max=test.substr(p+1);
                  if (isNaN(vax)) {
                      val.select();
                      val.focus();
                      errors+='- '+nm+' must be numeric\n'
                  };
                  if ((parseFloat(vax) < parseFloat(min))||(parseFloat(vax) > parseFloat(max))) {
                      val.select();
                      val.focus();
                      errors+='- '+nm+' must be in range '+min+'\n'
                  }; 
              };
            }       
        else 
            {

              if (test.indexOf('n1') != -1) {
//              just save the thing for the next check
                req1 = val.value;
              };

              if (test.indexOf('n2') != -1) {
//              just save the thing for the next check
                req2 = val.value;
              };

              if (test.indexOf('n3') != -1) {
//              check to see if n1,n2 and n3 all all not present
                req3 = val.value;
                if ((req1 == "") && (req2 == "") && (req3 == ""))  {
                    val.select();
                    val.focus();
                    errors+=' - Order Lines - at least one field must be entered\n';
                }
              };


            if (test.charAt(0) == 'p') {
                errors += '- '+nm+' is required\n .... enter NA if not known\n';
            };

            if (test.charAt(0) == 'r') {
                errors += '- '+nm+' is required\n .... enter zero if not known\n';
            };
            
            if (test.charAt(0) == 'R') {
                errors += '- '+nm+' is required\n';
            };   
        }
    } 
    

    var ind, i, theForm;
    theForm = document.forms[0];
    for(i = 0; i < theForm.length; i++){
      if(theForm.elements[i].type == 'checkbox' && theForm.elements[i].name == 'tac_check'){
        ind = i;
        break;
      }
    }
    if (ind) {
        if (!theForm.elements[ind].checked){
          errors += '- You must check the Terms and Conditions box to continue\n';  
        }
    }
    
    if (errors) {
        alert(' The following error(s) have been detected:\n\n' + errors);
    } 
    document.MM_returnValue = (errors == '')
}

function ValCert(){
var ind, i, theForm;
theForm = document.forms[0];
for(i = 0; i < theForm.length; i++){
  if(theForm.elements[i].type == 'checkbox'){
    ind = i;
    break;
  }
}

if (!theForm.elements[ind].checked){
  alert('You must check the Declaration box to continue');
  return false;
}
return true;
}

//-->
