﻿// JScript validation for seniors signup
// PC 10/03/08, 09/01/09, 15/7/09

    // global variable to hold date values, which get set inside function isValidDate and checked down below
    yy = 0;

  function isEmail(s) {
	var pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (pattern.test(s)) return true;
	else return false;
  }

  // all-encomapssing date validation adapated by PC from an example somewhere on t'interweb.
  // expects DMY with two-digits format, dd.mm.yy, as specifically specified.
  // this is required by the "CAT" database system in use at the receiving end (in OZ).
  function isValidDate(dateStr, format) {
      var reg1 = /^\d{2}(\-|\/|\.)\d{2}\1\d{2}$/

      // If it doesn't conform to the right format (with a 2 digit year) then fail
      if ((reg1.test(dateStr) == false)) { return false; }
      var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
      // Check to see if the 3 parts end up making a valid date
      if (format.substring(0, 1) == "M") { var mm = parts[0]; }
      else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
      if (format.substring(0, 1) == "D") { var dd = parts[0]; }
      else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
      if (format.substring(0, 1) == "Y") {yy = parts[0]; }
      else if (format.substring(1, 2) == "Y") {yy = parts[1]; } else {yy = parts[2]; }
      if (parseFloat(yy) <= 10) { yy = (parseFloat(yy) + 2000).toString(); }
      if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
      var dt = new Date(parseFloat(yy), parseFloat(mm) - 1, parseFloat(dd), 0, 0, 0, 0);
      if (parseFloat(dd) != dt.getDate()) { return false; }
      if (parseFloat(mm) - 1 != dt.getMonth()) { return false; }
      return true;
  }
    
    // Radio Button Validation
    // copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
    // you may copy this function but please keep the copyright notice with it. now do the boogaloo.
    function valButton(btn) {
        var cnt = -1;
        for (var i=btn.length-1; i > -1; i--) {
            if (btn[i].checked) {cnt = i; i = -1;}
        }
        if (cnt > -1) return btn[cnt].value;
        else return null;
    }
    
     // fn to check form is filled in correctly

    function validateForm() {

        var btn = '';
        var frmTheForm = document.forms[0];
                
         if (frmTheForm.Surname.value == '') {
            alert('Please enter your family name');
            frmTheForm.Surname.focus();
            return false;
        }
        
        if (frmTheForm.Givenname.value == '') {
            alert('Please enter your first/given name');
            frmTheForm.Givenname.focus();
            return false;
        }
        
        btn = valButton(frmTheForm.Gender);
        if (btn == null) { 
            alert('Please select your gender'); 
            frmTheForm.Gender[0].focus();    
            return false 
        }
                
        if (frmTheForm.Country.selectedIndex <= 0) {
            alert('Please enter your nationality');
            frmTheForm.Country.focus();
            return false;
        }
        
        var theDate = frmTheForm.dateofbirth.value;
        if (!isValidDate(theDate, 'DMY')) { 
            alert("Please enter a valid date of birth.\nThe date MUST be in the format dd.mm.yy"); 
            frmTheForm.dateofbirth.focus();
            return false;
        }
        
         if (frmTheForm.add1.value == '') {
            alert('Please enter your address line 1');
            frmTheForm.add1.focus();
            return false;
        }
        
         if (frmTheForm.town.value == '') {
            alert('Please enter your country');
            frmTheForm.town.focus();
            return false;
        }
        
        if (frmTheForm.pcode.value == '') {
            alert('Please enter a valid postcode. If you dont have a postcode, please enter NONE');
            frmTheForm.pcode.focus();
            return false;
        }
        
        if (frmTheForm.phone.value == '') {
            alert('Please enter your phone number');
            frmTheForm.phone.focus();
            return false;
        }
        
        if(!isEmail(frmTheForm.Email.value))
	    {
		    alert('Please enter a valid email address');
            frmTheForm.Email.focus();
            return false;
	    }
    
	    // event details checking - aqmended as per 2009 requirements
        if (frmTheForm.E1.selectedIndex <= 0) {
            alert('Please enter your choice of first event');
            frmTheForm.E1.focus();
            return false;
            }
            
       // check male players have only selected male games etc
       var intFirstSelection = frmTheForm.E1.selectedIndex;
       var intSecondSelection = frmTheForm.E2.selectedIndex;
       btn = valButton(frmTheForm.Gender);
       
       switch(btn) {
            case 'M':
                if (frmTheForm.E1.options[intFirstSelection].value.indexOf("WOMEN") != -1) {
                    alert('Male players can only enter mens events');
                    frmTheForm.E1.focus();
                    return false;
                    }
                else if (intSecondSelection >0) {
                    if (frmTheForm.E2.options[intSecondSelection].value.indexOf("WOMEN") != -1) {
                        alert('Male players can only enter mens events.');
                        frmTheForm.E2.focus();
                        return false;
                        }
                    }  
                break;
                
             case 'F':
                if (frmTheForm.E1.options[intFirstSelection].value.indexOf("WOMEN") == -1) {
                    alert('Female players can only enter womens events');
                    frmTheForm.E1.focus();
                    return false;
                    }
                else if (intSecondSelection >0) {
                    if (frmTheForm.E2.options[intSecondSelection].value.indexOf("WOMEN") == -1 ) {
                        alert('Female players can only enter womens events.');
                        frmTheForm.E2.focus();
                        return false;
                        }
                   }
                break; 
            } // end switch               
                
                
        // make sure they haven't selected the same event twice
        if (intFirstSelection == intSecondSelection) {
            alert('Event selections should be different');
            frmTheForm.E2.focus();
            return false;
        }

        // make sure they have not entered two events of the same type
        // this relies on the distribution of the signles and doubles in the list of options
        if (intSecondSelection != 0) {
            if (intFirstSelection <= 10 && intSecondSelection <= 10) {
                alert('You cannot enter two singles events');
                frmTheForm.E2.focus();
                return false;
            }            
        }
        
        if (intSecondSelection != 0) {
            if (intFirstSelection >= 11 && intSecondSelection >= 11) {
                alert('You cannot enter two doubles events');
                frmTheForm.E2.focus();
                return false;
            }
        }

        // fab new featurette - check they are age eligible for their chosen events, using the global variable yy set above and some A1 kludgery to get the age category they've chosen
        // we subjtract 1 from the age to allow for year rollovers, and since the events are grouped in fives that's OK
        var dteNow = new Date();
        var intYear = dteNow.getFullYear();
        var intUserAge = intYear - yy;
//       alert(intYear + ' - ' + yy + '=' + intUserAge);
        // there should def be a first entry chosen, and this should be the age part of the value string
        var intPos1 = (frmTheForm.E1.options[intFirstSelection].value.indexOf("ENS ")) + 4;
        if (intPos1 > 0) {
            var intAgeReq1 = parseInt(frmTheForm.E1.options[intFirstSelection].value.substr(intPos1, 2));
            if (intUserAge < intAgeReq1 - 1) {
                alert('You must be ' + intAgeReq1 + ' years old by the end of the event year to enter this event');
                frmTheForm.E1.focus();
                return false;
            }
        }
        // and there might be a second entry too
        var intPos2 = (frmTheForm.E2.options[intSecondSelection].value.indexOf("ENS ")) + 4;
        if (intPos2 > 0) {
            var intAgeReq2 = parseInt(frmTheForm.E2.options[intSecondSelection].value.substr(intPos2, 2));
            if (intUserAge < intAgeReq2 - 1) {
                alert('You must be ' + intAgeReq2 + ' years old by the end of the event year to enter this event');
                frmTheForm.E2.focus();
                return false;
            }
        }

        // moving right along with the swing of things...
        btn = valButton(frmTheForm.TeamNomination);
        if (btn == null) { 
            alert('Please select YES, NO or TBC\nregarding your participation in the World Team Championships'); 
            frmTheForm.TeamNomination[0].focus();
            return false;
        }
        
        // cehck they have entered "i agree" in the release box
        if (frmTheForm.waiver.value.toLowerCase() != 'i agree') {
            alert('You must enter I AGREE in the Player Release box before continuing');
            frmTheForm.waiver.focus();
            return false;
        }
        
        // get here when validation completes OK - just need to set the correct target page depending on the choice of payment method
        // this alternative option is currently coyyented out but could be reinstated if IG decides she doesnt want to manually reconcile all worldpay payments
        // sone extra coding would then be nece3ssary to generate the confirmatory email in worldpay callback rather than beforehand (as at present)
        
        /*
        var strPaymentMethod = valButton(frmTheForm.Paymentmethod);
        switch(strPaymentMethod) {
            case 'visa':
                //frmTheForm.action = "tokengen.asp";                        <<< an alternative ending
                frmTheForm.action = "worldchampionships/formprocessor.asp";
                break;
                
            case 'visapost':
                frmTheForm.action = "worldchampionships/formprocessor.asp";
                break;                
        
            case 'cheque':
                frmTheForm.action = "worldchampionships/formprocessor.asp";
                break;            
        }
        */
       
       frmTheForm.action = "worldchampionships/formprocessor.asp";
       return true;

    }
