﻿var mbSetFocus = true;
var bThisFormValidate = true;
var oErrorMsg = {
    "txtFirstName": "First Name must be specified",
    "fuCV_Require": "A CV must be supplied",
    "fuCV_Invalid": "We can only upload Word .doc, .docx, .pdf or .rtf documents",
    "txtLastName": "Last Name must be specified",
    "txtPreferredName": "Preferred Name must be specified",
    "lstCountryID": "Please specify a Country",
    "txtEmail_Require": "Please specify an Email Address",
    "txtEmail_Invalid": "Invalid Email Address",
    "txtSecondEmail_Require": "Please specify an Alternative Email Address",
    "txtSecondEmail_Invalid": "Invalid Email Address",
    "txtPrimaryPassportCountryID": "Please specify your Primary Passport's country",
    "ccb_ProfessionalRegistration": "Please specify Professional Registration countries",
    "lstPrimaryMedicalDegreeCountryID": "Please specify your Primary Medical Degree Country",
    "txtGender": "Please specify a Gender",
    "txtGraduationYear_Require": "Please specify your Graduation Year",
    "txtGraduationYear_Invalid": "Invalid Graduation Year",
    "txtGraduationYear_OutsideRange": "Graduation Year must not be more than 60 years ago",
    "chkProfessionalApproval": "Since you have Full GMC registration, please remove the months value",
    "txtProfessionalApprovalMonths": "Please enter the number of Months",
    "lstDesiredCountry1ID": "Please specify a Desired Country",
    "lstEmploymentTypeID": "Please specify a Employment Type",
    "txtPassword": "Please specify your Password",
    "txtRetypePassword_Require": "Please retype your Password",
    "txtRetypePassword_Invalid": "The two passwords do not match",
    "txtCity": "Please specify the City",
    "txtPostcode": "Please specify the Postcode",
    "lstPositionCategoryID": "Please specify a Discipline (category)",
    "lstDesiredCountry1ID": "Please specify your Desired Country",
    "txtDesiredLocation1": "Please specify your Desired Location",
    "lstEmploymentTypeID": "Please specify a Employment Type",
    "txtDateAvailable": "Please specify your Available Date",
    "txtSpecialty": "Please specify your Specialty",
    "lstWhereHearID": "Where you heard about Geneva Health must be specified",
    "txtOtherInfo": "Please enter some Comments/Other Information text",
    "txtDateAvailable_Invalid": "Invalid date format",
    "txtDateAvailable_Require": "Please select an Available Date",
    "txtSuburb": "Please specify the Suburb",
    "txtdecHoursPerWeek_Require": "Please specify your desired Hours Per Week",
    "txtdecHoursPerWeek_Invalid":"Invalid number format",
    "txtPreviousExperience": "Please specify any Previous Experience or enter 'none'",
    "lstPositionCategoryID_UKOnly": "Please specify a Profession (category)",
    "txtNationality": "Please specify your Nationality",
    "txtEligibleToWork_UK": "Please specify whether you are eligible to work in the UK",
    "txtGmc": "Please specify whether you have full GMC Registration",
    "txtDriversLicence": "Please specify whether or not you have a full Driver's License",
    "txtEligibleToWork_NZ": "Please specify whether you are eligible to work in NZ", 
    "txtWorkPermitRequired": "Please specify whether you require a UK work permit"
    };

    function RequiredValidate(sEleName, sErrorName, sError, bLastValidatorsResult) {
    if (bLastValidatorsResult) {
            oEle = document.getElementById(sEleName);
            resetValidationError(sErrorName);
            if (trim(oEle.value) == '') {
                setValidationError(oEle, sErrorName, sError);
                return false;
            }
        return true;
    }
    else {
        return bLastValidatorsResult;
    }
}

function trim(stringToTrim) {
    return stringToTrim.replace(/\s+/g, "");
}
function LovItemSelected(sEleName, sErrorName, sError, bLastValidatorsResult) {
    if (bLastValidatorsResult) {
            oEle = document.getElementById(sEleName);
            resetValidationError(sErrorName);
            if (oEle.options[oEle.selectedIndex].value == '' | oEle.options[oEle.selectedIndex].value == '0') {
                setValidationError(oEle, sErrorName, sError);
                return false;
            }
        return true;
    }
    else {
        return bLastValidatorsResult;
    }
}
function RangeValidate(sEleName, sErrorName, sError, nMin, nMax, bLastValidatorsResult) {
    if (bLastValidatorsResult) {
        oEle = document.getElementById(sEleName);
        resetValidationError(sErrorName);
        if (oEle.value < nMin || oEle.value > nMax) {
            setValidationError(oEle, sErrorName, "Value outside valid range (" + nMin + " to " + nMax + ")");
            return false;
        }
        return true;
    }
    else {
        return bLastValidatorsResult;
    }
}
function RegularExpressionValidate(sEleName, sErrorName, oReg, sError, bLastValidatorsResult) {
    if (bLastValidatorsResult) {
            oEle = document.getElementById(sEleName);
            resetValidationError(sErrorName);
            var sValidateValue = oEle.value;
            if (oReg.test(sValidateValue) == false) {
                setValidationError(oEle, sErrorName, sError);
                return false;
            }
        return true;
    }
    else {
        return bLastValidatorsResult;
    }
}
var gblDateError = "";

function DateValidate(sEleName, sErrorName, sError, dtMin, dtMax, bLastValidatorResult) {
    if (bLastValidatorResult) {
        var oErrorEle = $get(sErrorName + "_Error");
        if ($get(sEleName).value != "") {
            if (isValidDate($get(sEleName), dtMin, dtMax)) {
                oErrorEle.style.display = "none";
                return true;
            }
            else {
                oErrorEle.innerHTML = gblDateError;
                oErrorEle.style.display = "block";
                return false;
            }
        }
        else {
            oErrorEle.style.display = "none";        
            return true;
        }
    }
    else {
        return bLastValidatorsResult;
    }  
}
function setValidationError(oEle, sElementNiceName, sErrorMessage) {

    var oErrorEle = document.getElementById(sElementNiceName + '_Error');
    oErrorEle.innerHTML = sErrorMessage;
    oErrorEle.style.display = "block";
    if (mbSetFocus == true) {
        oEle.focus();
        mbSetFocus = false;
    }
}

function resetValidationError(sElementNiceName) {
    var oErrorEle = document.getElementById(sElementNiceName + '_Error');
    oErrorEle.style.display = "none";
}
var dtCh= "-";

function isValidDate(oDate, dtMin, dtMax) {
    var dtStr = oDate.value;
    var sMonth = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
    var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)

	var strDay=dtStr.substring(0,pos1)
	var strMonth = dtStr.substring(pos1 + 1, pos2)
	var nMonthNmbr = 0
	var strYear=dtStr.substring(pos2+1)

	strYr=strYear

	if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)

    var bMonthValid = false;
	for (var i = 0; i <= 11; i++) {
	    if (sMonth.substring(i * 3, (i * 3) + 3) == strMonth.toUpperCase()) {
	        bMonthValid = true;
	        nMonthNmbr = i + 1
	    }
	}
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1 || dtStr.length!= 11){
		gblDateError = "Date format must be dd-mmm-yyyy"
		return false
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (nMonthNmbr == 2 && day > daysInFebruary(year)) || day > daysInMonth[nMonthNmbr]) {
		gblDateError = "Invalid day for the month"
		return false
	}
	if (!bMonthValid){
	    gblDateError = "Invalid Month"
	    return false
	}
	if (strYear.length != 4 || year == 0 ) {
		gblDateError = "Invalid year"
		return false
    }
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(strDay + strYear, dtCh))==false){
		gblDateError = "Invalid date"
		return false
	}
	if (nMonthNmbr.length = 1) {
	    sMth = "0" + nMonthNmbr 
	} else {
	    sMth = nMonthNmbr
	}
	var myDate = new Date(strYear + "/" + sMth + "/" + strDay)
	var myMinDate = new Date(dtMin.getFullYear() + "/" + dtMin.getMonth() + "/" + dtMin.getDate())
	var myMaxDate = new Date(dtMax.getFullYear() + "/" + dtMax.getMonth() + "/" + dtMax.getDate())

	if (myDate < myMinDate || myDate > myMaxDate) {
        gblDateError = "Date outside valid range (" + ConvertDate(dtMin) + " to " + ConvertDate(dtMax) + ")"
        return false
    }
	return true
}
function ConvertDate(myDate) {
    var sMonth = "JanFebMarAprMayJunJulAugSepOctNovDec";
    var nMonthNmbr = myDate.getMonth()

    return myDate.getDate() + "-" + sMonth.substring(nMonthNmbr * 3, (nMonthNmbr * 3) + 3) + "-" + myDate.getFullYear()
}
function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}