﻿/// <reference path="~/pzMedia/js/jquery-1.3.2-vsdoc.js" />

var errorMessage = "";

function doValidateUserDetailsOne(firstName, lastName, dobDay, dobMonth, dobYear, email, emailConfirm, telephone, mobile, address1, address2, address3, town, county, country, postcode, disabled, disability, err) {
    var hasErrors = false;
    hasRequiredError = false;
    hasEmailError = false;
    hasRequiredDOBError = false;
    hasCompareError = false;
    errorMessage = "";
    $('#' + err).html('');

    if (!doValidateField(firstName, err))
        hasErrors = true;
    if (!doValidateField(lastName, err))
        hasErrors = true;
    if (!doValidateField(address1, err))
        hasErrors = true;
    if (!doValidateField(town, err))
        hasErrors = true;
    if (!doValidateEmail(email, err))
        hasErrors = true;
    if (!doValidateEmail(emailConfirm, err))
        hasErrors = true;
    if (!doValidateDOBDropDown(dobDay, err))
        hasErrors = true;
    if (!doValidateDOBDropDown(dobMonth, err))
        hasErrors = true;
    if (!doValidateDOBDropDown(dobYear, err))
        hasErrors = true;
    if (!doCompareField(email, emailConfirm, err))
        hasErrors = true;

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    } else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        hasEmailError = false;
        hasRequiredDOBError = false;
        hasCompareError = false;
        return true;
    }
}

function doValidateUserDetailsTwo(salary, noticePeriod, err) {
    var hasErrors = false;
    hasRequiredError = false;
    errorMessage = "";
    $('#' + err).html('');

    if (!doValidateField(salary, err))
        hasErrors = true;
    if (!doValidateField(noticePeriod, err))
        hasErrors = true;

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    } else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        return true;
    }
}

function doValidateEmploymentHistory(title, description, location, startDate, endDate, position, reason, salary, notes, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    hasDateError = false;
    errorMessage = "";
    $('#' + err).html('');

    if (checkFormUsed([title, description, location, startDate, endDate, position, reason, salary, notes], err)) {
        if (!doValidateField(title, err))
            hasErrors = true;
        if (!doValidateField(description, err))
            hasErrors = true;
        if (!doValidateField(location, err))
            hasErrors = true;
        if (!doValidateField(startDate, err))
            hasErrors = true;
        else
            if (!doValidateDate(startDate, err))
                hasErrors = true;
        if (!doValidateField(position, err))
            hasErrors = true;
        if (!doValidateField(salary, err))
            hasErrors = true;
        if (!doValidateDate(endDate, err))
            hasErrors = true;

        $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    }

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else if (hasErrors == false && parseInt($('#' + hfCount).val()) == 0) {
        $('#' + err).html('Please complete at least one entry to proceed');
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        hasDateError = false;
        return true;
    } 
}

function doValidateGapHistory(startDate, endDate, reason, notes, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    hasDateError = false;
    errorMessage = "";
    $('#' + err).html('');

    if (checkFormUsed([startDate, endDate, reason, notes], err)) {
        if (!doValidateField(startDate, err))
            hasErrors = true;
        else
            if (!doValidateDate(startDate, err))
                hasErrors = true;
        if (!doValidateField(reason, err))
            hasErrors = true;
        if (!doValidateDate(endDate, err))
            hasErrors = true;

        $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    }

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        hasDateError = false;
        return true;
    }
}

function doValidateInstitutionHistory(title, startDate, endDate, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    hasDateError = false;
    errorMessage = "";
    $('#' + err).html('');

    if (checkFormUsed([title, startDate, endDate], err)) {
        if (!doValidateField(title, err))
            hasErrors = true;
        if (!doValidateField(startDate, err))
            hasErrors = true;
        else
            if (!doValidateDate(startDate, err))
                hasErrors = true;
        if (!doValidateDate(endDate, err))
            hasErrors = true;

        $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    }

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else if (hasErrors == false && parseInt($('#' + hfCount).val()) == 0) {
        $('#' + err).html('Please complete at least one entry to proceed');
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        hasDateError = false;
        return true;
    }
}

function doValidateQualificationHistory(title, subject, grade, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    $('#' + err).html('');

    if (checkFormUsed([title, subject, grade], err)) {
        if (!doValidateField(title, err))
            hasErrors = true;
        if (!doValidateField(subject, err))
            hasErrors = true;
        if (!doValidateField(grade, err))
            hasErrors = true;

        $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    }

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else if (hasErrors == false && parseInt($('#' + hfCount).val()) == 0) {
        $('#' + err).html('Please complete at least one entry to proceed');
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        return true;
    }
}

function doValidateOtherHistory(title, notes, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    $('#' + err).html('');

    if (checkFormUsed([title, notes], err)) {
        if (!doValidateField(title, err))
            hasErrors = true;

        $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    }

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        return true;
    }
}

function doValidateReference(name, position, organisation, organisationAddress, telephone, hfCount, err) {
    var hasErrors = false;
    hasRequiredError = false;
    $('#' + err).html('');

    //if (checkFormUsed([name, position, organisation, organisationAddress, telephone], err)) {
    if (!doValidateField(name, err))
        hasErrors = true;
    if (!doValidateField(position, err))
        hasErrors = true;
    if (!doValidateField(organisation, err))
        hasErrors = true;
    if (!doValidateField(organisationAddress, err))
        hasErrors = true;
    if (!doValidateField(telephone, err))
        hasErrors = true;

    $('#' + hfCount).val(parseInt($('#' + hfCount).val()) + 1);
    //}

    if (hasErrors == true) {
        $('#' + err).html(errorMessage);
        $('#' + err).addClass("col3bloc_excl");
        return false;
    }
    else {
        $('#' + err).html('');
        $('#' + err).removeClass("col3bloc_excl");
        hasRequiredError = false;
        return true;
    }
}

// check if the form is partially filled
function checkFormUsed(arrFields, err) {
    var isUsed = false;
    var arrLength = arrFields.length;
    for (i = 0; i < arrLength; i++) {
        if ($('#' + arrFields[i]).val() != "")
            isUsed = true;
        else
            $('#' + getOutputField(arrFields[i])).removeClass("error");

    }
    return isUsed;
}

var hasRequiredError = false;
// validate the field
function doValidateField(field, err) {

    var output = getOutputField(field);
    if ($('#' + field).val() == "") {
        $('#' + output).addClass("error");
        if (!hasRequiredError) {
            if (errorMessage.length > 0) {
                errorMessage += '<br />';
            }
            errorMessage += 'Please fill in all required fields';
            hasRequiredError = true;
        }
        return false;
    } else {
        $('#' + output).removeClass("error");
        return true;
    }
}

var hasEmailError = false;
// validate the email address
function doValidateEmail(email, err) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var output = getOutputField(email);
    if ($('#' + email).val() != "") {
        if (!emailPattern.test($('#' + email).val())) {
            $('#' + output).addClass("error");
            if (!hasEmailError) {
                if (errorMessage.length > 0) {
                    errorMessage += '<br />';
                }
                errorMessage += 'Please enter a valid email address';
                hasEmailError = true;
            }
            return false;
        } else {
            $('#' + output).removeClass("error");
            return true;
        }
    } else {
        $('#' + output).removeClass("error");
        return true;
    }
}

var hasDateError = false;
// validate the date (dd/mm/yyyy)
function doValidateDate(date, err) {
    var datePattern = /^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$/;
    var output = getOutputField(date);
    if ($('#' + date).val() != "") {
        if (!datePattern.test($('#' + date).val())) {
            $('#' + output).addClass("error");
            if (!hasDateError) {
                if (errorMessage.length > 0) {
                    errorMessage += '<br />';
                }
                errorMessage += 'Please enter a valid date';
                hasDateError = true;
            }
            return false;
        } else {
            $('#' + output).removeClass("error");
            return true;
        }
    } else {
        $('#' + output).removeClass("error");
        return true;
    }
}

var hasRequiredDropDownError = false;
// validate drowdown list
function doValidateDropDown(ddl, err) {
    var output = getOutputField(ddl);
    if ($("#" + ddl + " option:selected").val() == '') {
        $('#' + output).addClass("error");
        if (!hasRequiredDropDownError) {
            if (errorMessage.length > 0) {
                errorMessage += '<br />';
            }
            errorMessage += 'Please select an item from the dropdown list';
            hasRequiredDropDownError = true;
        }         
        return false;
    } else {
        $('#' + output).removeClass("error");
        return true;
    }
}

var hasRequiredDOBError = false;
// validate date of birth drowdown lists
function doValidateDOBDropDown(ddl, err) {
    var output = getDOBOutputField(ddl);
    if ($("#" + ddl + " option:selected").val() == '') {
        $('#' + output).addClass("error");
        if (!hasRequiredDOBError) {
            if (errorMessage.length > 0) {
                errorMessage += '<br />';
            }
            errorMessage += 'Please enter your date of birth via the dropdown lists';
            hasRequiredDOBError = true;
        }
        return false;
    } else {
        $('#' + output).removeClass("error");
        return true;
    }
}

var hasCompareError = false;
// compare the field
function doCompareField(field1, field2, err) {
    var output1 = getOutputField(field1);
    var output2 = getOutputField(field2);
    if ($('#' + field1).val() == "" && $('#' + field2).val() == "") {
        $('#' + output1).removeClass("error");
        $('#' + output2).removeClass("error");
        return true;
    } else {
        if ($('#' + field1).val() != $('#' + field2).val()) {
            $('#' + output1).addClass("error");
            $('#' + output2).addClass("error");
            if (!hasCompareError) {
                if (errorMessage.length > 0) {
                    errorMessage += '<br />';
                }
                errorMessage += 'Fields do not match';
                hasCompareError = true;
            }
            return false;
        } else {
            $('#' + output1).removeClass("error");
            $('#' + output2).removeClass("error");
            return true;
        }
    }
}


// Gets the li tag name for a given field - assumes the li has the same name as the field but with li as the first 2 characters
function getOutputField(field) {
    var arrName = field.split("_");
    var name = arrName[arrName.length - 1];
    var nameLength = name.length;
    var liName = 'li' + name.substring(2, nameLength);
    return field.replace(name, liName);
}
function getDOBOutputField(field) {
    var arrName = field.split("_");
    var name = arrName[arrName.length - 1];
    var liName = 'liDateOfBirth';
    return field.replace(name, liName);
}

// UK Postcode RegEx - ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$

