
// Validate.js
// Client-side validation for subscribe.asp (and others)
// 09.05.2007 Modified BZM


function ValidateEmailAddress( fld )
{
    //== Validate the email address
	var str = fld.value;
	var at= '@';
	var dot='.';
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at)==-1) return false;
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
    if (str.indexOf(at,(lat+1))!=-1) return false;
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
    if (str.indexOf(dot,(lat+2))==-1) return false;
    if (str.indexOf(" ")!=-1) return false;
	 
    return true;
	 
} // ValidateEmailAddress


function ValidateForm()
{
    var f = window.document.Subscribe_Form;
    
	if( Trim(f.First_Name.value) == '')  {
		alert('Please enter your first name.');
		f.First_Name.focus();
	    return false;
	}
	if( Trim(f.Last_Name.value) == '')  {
		alert('Please enter your last name.');
		f.Last_Name.focus();
	    return false;
	}
	if( Trim(f.Company.value) == '')  {
		alert('Please enter your company name.');
		f.Company.focus();
	    return false;
	}
	if( Trim(f.Address.value) == '')  {
		alert('Please enter your address.');
		f.Address.focus();
	    return false;
	}
	if( Trim(f.Email.value) == '')  {
		alert('Please enter your email address.');
		f.Email.focus();
	    return false;
	}
	else{
	
	    //== Validate the email address
		if( !ValidateEmailAddress( f.Email ) ) {
		    alert('Please enter a valid email address.');
		    f.Email.focus();
		    return false;
		}
    }
	return true;
}


//================================================================
// ValidateForm2
//================================================================
// Same as ValidateForm(), but also checks that one or more areas is selected...
// Used by subscribe.asp
function ValidateForm2()
{
    // Run the common validation (above)
    if( !ValidateForm() ) return false;
    
    // Additional validation for area selection
    // 01.11.2007  Firefox-compatible version (BZM)
    try {
        var CheckboxesFound = false;
        var AreasSelected = false;
        var n = document.getElementsByTagName("input");
        for( i = 0; i < n.length; i++ ) {
            if( n[i].name == "chkarea" ) {
                CheckboxesFound = true;
                if( n[i].checked ) {
                    AreasSelected = true;
                    break;
                }
            }
        }
        if( !CheckboxesFound ) return true;     // No checkboxes here.
        if( AreasSelected ) return true;        // At least one was checked.
        
        alert( 'Please select at least one area to subscribe to.' );
        return false;
    }
    catch(e) {
        alert(e);
        return false;   // error!
    }

//    REMOVED 01.11.2007 - Not Firefox compatible (BZM)
//    var areas = document.getElementsByName('chkArea');
//    if( areas ) {
//        alert( 'Found chkArea - ' + areas.length );
//        var AreaSelected = false;
//        for( i = 0; i < areas.length; i++ ) {
//            if( areas[i].checked ) {
//                AreaSelected = true;
//                break;
//            }
//        }
//        if( AreaSelected )
//            return true;
//        else {
//            alert( 'Please select at least one area to subscribe to' );
//            areas[0].focus();
//            return false;
//        }
//    }
//    else {
//        //alert( 'Missing chkArea' );
//        // No area checkboxes on this form!
//        return true;
//    }

}



//================================================================
// ValidateForm3
//================================================================
// Similar to ValidateForm(), but tailored for alumni-register.asp
function ValidateForm3()
{
    var f = window.document.Form1;
    
	if( Trim(f.First_Name.value) =='')  {
		alert('Please enter your first name.');
		f.First_Name.focus();
	    return false;
	}
	if( Trim(f.Last_Name.value) =='')  {
		alert('Please enter your last name.');
		f.Last_Name.focus();
	    return false;
	}

    //== Validate the email address
	if( Trim(f.Email.value) =='')  {
		alert('Please enter your email address.');
		f.Email.focus();
	    return false;
	}
	if( !ValidateEmailAddress( f.Email ) ) {
	    alert('Please enter a valid email address.');
	    f.Email.focus();
	    return false;
	}

	return true;
}



//================================================================
// ValidateForm4
//================================================================
// Similar to ValidateForm(), but tailored for application-form2.asp (vacancies)
function ValidateForm4()
{
    var f = window.document.Form1;
    
	if(Trim(f.Surname.value) =='')  {
		alert('Please enter your last name.');
		f.Surname.focus();
	    return false;
	}
	
    //== Validate the email address
	if(Trim(f.EmailAddress.value) =='')  {
		alert('Please enter your email address.');
		f.EmailAddress.focus();
	    return false;
	}
	if( !ValidateEmailAddress( f.EmailAddress ) ) {
	    alert('Please enter a valid email address.');
	    f.EmailAddress.focus();
	    return false;
	}

    //----------------------------------------------------------------------- WORKING IN NZ
    if( f.OkToWorkInNZ[0].checked ) {
        if( Trim(f.ResidencyStatus.value) == '' ) {
            alert( 'Please enter your residency status.' );
            f.ResidencyStatus.focus();
            return false;
        }
        if( Trim(f.WorkPermit.value) == '' ) {
            alert( 'Please enter your work permit details.' );
            f.WorkPermit.focus();
            return false;
        }
    }
    
    //----------------------------------------------------------------------- HEALTH
	if( !( f.Accidents[0].checked || f.Accidents[1].checked ) ) {
	    alert('Please complete the question about accidents.');
	    f.Accidents[0].focus();
	    return false;
	}

	if( !( f.Sickness[0].checked || f.Sickness[1].checked ) ) {
	    alert('Please complete the question about sickness.');
	    f.Sickness[0].focus();
	    return false;
	}

	if( !( f.OOS[0].checked || f.OOS[1].checked ) ) {
	    alert('Please complete the question about OOS.');
	    f.OOS[0].focus();
	    return false;
	}

	if( !( f.Medical[0].checked || f.Medical[1].checked ) ) {
	    alert('Please complete the question about medical conditions.');
	    f.Medical[0].focus();
	    return false;
	}

	if( ( f.Accidents[1].checked || f.Sickness[1].checked || f.OOS[1].checked || f.Medical[1].checked ) && ( Trim(f.MedicalDetails.value) == '' ) ) {
	    alert('Please provide details.');
	    f.MedicalDetails.focus();
	    return false;
	}

    //----------------------------------------------------------------------- CRIMINAL
	if( !( f.Criminal[0].checked || f.Criminal[1].checked ) ) {
	    alert('Please complete the question about criminal convictions.');
	    f.Criminal[0].focus();
	    return false;
	}

	if( f.Criminal[1].checked && ( Trim(f.CriminalDetails.value) == '' ) ) {
	    alert('Please provide details about your criminal convictions.');
	    f.CriminalDetails.focus();
	    return false;
	}

	if(f.Agreed.checked == false ) {
	    alert('Please confirm that you accept the personal declaration.');
	    f.Agreed.focus();
	    return false;
	}

    //alert('All validation ok');
	//return false; // for debugging only
	
	return true;
	
}


//================================================================
// ValidateForm5
//================================================================
// Used on careers/application-form.asp, to ensure at least on
// job is selected.
function ValidateForm5()
{
    var selection = document.getElementsByName("VacancyId");
    if( selection ) {
        for( i = 0; i < selection.length; i++ ) {
            if( selection[i].checked ){
                return true;    // at least one is selected
            }
        }
    }
    var exSelection = document.getElementsByName("exOfInterest");
    if (selection) {
        for (i = 0; i < exSelection.length; i++) {
            if (exSelection[i].checked) {
                return true;    // at least one is selected
            }
        }
    }
    
    alert( 'Please select the job(s) you would like to apply for.' );
    return false;
    
} // ValidateForm5


//================================================================
// ValidateFormPPSA
//================================================================
// Similar to ValidateForm(), but tailored for PPSA form in resource 36
function ValidateFormPPSA()
{
    // Use the common code from above.
    if( !ValidateForm() ) return false;

    // If it's ok so far, check a question was entered too
    var f = window.document.Subscribe_Form;
        
	if( Trim(f.Question.value) =='')  {
		alert('Please enter your question.');
		f.Question.focus();
	    return false;
	}
	
	return true;
    
} // ValidateFormPPSA


// This code will be executed by older browsers and prevent errors
function setArea() {
    // blank function to fool older browsers
}

function setArea() { // This function takes the 'area' value from the URL and checks the appropriate checkbox.
  // Get the QueryString
  var locString = window.location.toString();
  locString = locString.toLowerCase();
  
  // Get the value of the 'area' field
  var areaIndex = locString.indexOf("area=");
  
  if (areaIndex != -1) {
    var areaValue = locString.substring(areaIndex+5, locString.length);
    
    // Set checkboxes based on area value  
    try {
      eval("document.subscribe." + areaValue + ".checked = true");
    }
    catch (e) {
      // Error handler
    }  
  }
  // Uncomment below to debug areaValue
  // document.Subscribe_Form.First_Name.value = areaValue;
}


function Trim(S)
{
    return S.replace(/^\s+|\s+$/g, '');
}
