﻿//--- Validation Functions --//

//Validation error message array
var Errors = new Array()
Errors[0] = "&bull; Please choose a title.";
Errors[1] = "&bull; Please enter your first name.";
Errors[2] = "&bull; Please enter your surname.";
Errors[3] = "&bull; Please enter a contact email.";
Errors[4] = "&bull; Email address is invalid. Please check.";
Errors[5] = "&bull; Email addresses do not match.";
Errors[6] = "&bull; Please enter a contact number.";
Errors[7] = "&bull; Your contact number must be numeric.";
Errors[8] = "&bull; Please tell us how you heard about us.";


//All validation comes through here were the error message visual response is set
function MainValidation(div, errorNumber, pass) {    
    //If control is valid empty the appropriate div
    if (pass) {
        {
            $get(div).innerHTML = '';
            $get(div).style.display = 'none';
        }
    }
    //If there is an error show the appropriate error message
    else {
        $get(div).innerHTML = Errors[errorNumber];
        $get(div).style.display = 'block';
    }
    //If we have any error messages to display ensure container div is displayed otherwise hide it
    var errorNumber = CountErrors();
    if (errorNumber > 0) {        
        $get('errorTitle').innerHTML = errorNumber > 1 ? 'Some fields on this page need checking' : 'A field on this page needs checking';
        $get('divError').style.display = 'block';
        window.scroll(0, 0);
    }
    else
        $get('divError').style.display = 'none';

    return pass;
}

//Validation method which sets the appropriate image response
function ImageValidation(img, pass) {
    
    //If control is valid hide the error image
    if (pass) {
        $get(img).style.display = 'none';
    }
    //If there is an error display image
    else {
        $get(img).style.display = 'inline';
    }

    return pass;
}

//Most validation comes through here were the visual response is set
function Validation(img, div, errorNumber, pass) {
    ImageValidation(img, pass);
    return MainValidation(div, errorNumber, pass);
}

//Checks if we have any error messages to display
function CountErrors() {
    var invalid = 0;
    //Iterate thru all the error divs and set valid = false if any of them are not empty
    var ErrorDivs = $get('divErrors').getElementsByTagName('div');
    for (var i = 0; i < ErrorDivs.length; i++) {
        if (ErrorDivs[i].innerHTML != '')
            invalid++;
    }
    return (invalid);
}

//Checks whether the watermark field is empty
function CheckWaterMark(tbw) {
    var watermark = $find(tbw);
    if (watermark != null)
        return watermark.get_Text().toString().trim() != '';
    else
        return false;
}

//Compare whether the values in two watermarks are the same
function CompareWatermarks(tbw1, tbw2)
{
    var watermark1 = $find(tbw1);
    var watermark2 = $find(tbw2);
    return watermark1.get_Text().toString().trim() == watermark2.get_Text().toString().trim();
}

//Checks whether the email is populated with an invalid value
function CheckEmail(tbw) {
    var watermark = $find(tbw);
    if (watermark != null) {
        if (watermark.get_Text().toString().trim() == '')
            return true;
        else {
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            return filter.test(watermark.get_Text().toString());
        }
    }
    else
        return true;
}

//Checks whether an input value is numeric
function CheckNumeric(tbw) {
    return !isNaN($find(tbw).get_Text().toString().trim());
}


//--- Control Validation Functions --//

//Validate dropdown lists have been selected
function CheckDropDownList(ddl) {
    return $get(ddl).selectedIndex != 0;
}

//Validate title has been chosen
function TitleValidation() {
    return Validation('imgTitle', 'divErrorTitle', 0, CheckDropDownList(ddlTitle));
}

//Validate name field
function NameValidation() {
    return Validation('imgName', 'divErrorName', 1, CheckWaterMark('tbwName'));
}

//Validate name field
function SurnameValidation() {
    return Validation('imgSurname', 'divErrorSurname', 2, CheckWaterMark('tbwSurname'));
}

//Validate email field
function EmailValidation() {
    if (Validation('imgEmail', 'divErrorEmail', 3, CheckWaterMark('tbwEmail')))
        return Validation('imgEmail', 'divErrorEmail', 4, CheckEmail('tbwEmail'));
    else
        return false;
}

//Validate email confirm
function EmailConfirmValidation() {
    if (Validation('imgEmail', 'divErrorEmail', 3, CheckWaterMark('tbwEmail')) && Validation('imgEmail', 'divErrorEmail', 4, CheckEmail('tbwEmail'))){
        return Validation('imgEmailConfirm', 'divErrorEmailConfirm', 5, CompareWatermarks('tbwEmail', 'tbwConfirmEmail'));
    }
    else {
        Validation('imgEmailConfirm', 'divErrorEmailConfirm', 5, true);
        return false;
    }     
}

//Validate phone numbers
function TelephoneValidation() {
    if (Validation('imgPhone', 'divErrorPhone', 6, CheckWaterMark('tbwPhone')))
        return Validation('imgPhone', 'divErrorPhone', 7, CheckNumeric('tbwPhone'));
    else
        return false;
}
//Validate phone numbers
function ReferrerValidation() {
    
    return Validation(imgReferrer, 'divReferrer', 8, CheckDropDownList(ddlReferrer));    
}

//Validate all controls on the page
function ValidateAll() {
    TitleValidation()
    NameValidation();
    SurnameValidation();
    EmailValidation();
    EmailConfirmValidation();
    TelephoneValidation();
    ReferrerValidation();
    return CountErrors() == 0;
}
