﻿
function validate(field) {
    var valid = "0123456789-() "
    var ok = "yes";
    var temp;
    for (var i = 0; i < field.value.length; i++) {
        temp = "" + field.value.substring(i, i + 1);
        if (valid.indexOf(temp) == "-1") ok = "no";
    }
    if (ok == "no") {
        return 1;
    }
    if (!(exists(field.value))) {
        return 1;
    }
}



function isLongerThan11(field) {
    if (field.substr(0, 7) == 'http://') {
        if (field.length < 11) {
            return 0;
        }
    }
    else {
        if (field.length < 4) {
            return 0;
        }
    }
    return 1;
}

function isChecked(field) {
    if (field.checked) {
        return 1;
    }
    return 0;
}

function multiSelectCheck(field) {
    var selectCount = 0;

    for (var i = 0; i < field.length; i++) {
        if (field.options[i].selected == true) {
            selectCount++;
        }
    }

    if (selectCount > 6) {
        return 0;
    }
    return 1;
}


//////////////////////////////////////////////////////////////////////
// validateForm() function:
// Validates input of entire form.
//////////////////////////////////////////////////////////////////////


function validateAppForm() {

    var fixThis = "";

    if (!(isLongerThan11(document.frmApp.u.value))) {
        fixThis += "Please enter the full URL (eg. http://www.uk250.co.uk).\n"
    }

    if (!(exists(document.frmApp.c.value))) {

        fixThis += "Please enter a contact name.\n";
    }

    if ((validate(document.frmApp.tel))) {

        fixThis += "Please enter a valid telephone number.\n";
    }


    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.frmApp.submit();
    }
}

function validateAppForm1() {

    var fixThis = "";


    if (!(exists(document.signup.name.value))) {

        fixThis += "Please enter your name.\n";
    }

    if (!(exists(document.signup.address1.value))) {

        fixThis += "Please enter your postal address.\n";
    }


    if (!(exists(document.signup.postcode.value))) {

        fixThis += "Please enter your postcode.\n";
    }

    if (!(exists(document.signup.email.value))) {

        fixThis += "Please enter your email address.\n";
    }

    if (!(isChecked(document.signup.tandc))) {

        fixThis += "You must accept the terms and conditions to subscribe. If you don't wish to continue then please follow the home link or press the back button on your browser.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.signup.submit();
    }
}

function validateTopUp() {

    var fixThis = "";



    if (document.topup.tandc.value == "") {

        fixThis += "You must accept the terms and conditions. If you don't wish to continue then please follow the home link or press the back button on your browser.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.topup.submit();
    }
}


function validateLogonForm() {

    var fixThis = "";

    if (!(exists(document.logon.email.value))) {
        fixThis += "Please enter your email address.\n"
    }

    if (!(exists(document.logon.password.value))) {

        fixThis += "Please enter your password.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.logon.submit();
    }
}

function validateUpload() {

    var fixThis = "";

    if (!(exists(document.upload.refno.value))) {
        fixThis += "Please input a reference number.\n"
    }

    if (!(exists(document.upload.title.value))) {
        fixThis += "Please input a job title.\n"
    }

    if (!(exists(document.upload.description.value))) {
        fixThis += "Please enter a job description.\n"
    }

    if (!(ischecked(document.upload.uk))) {
        fixThis += "You must be an EU resident or have a valid work permit to apply for this job.\n"
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.upload.submit();
    }
}

function validatePasswdForm() {

    var fixThis = "";

    if (!(exists(document.changepass.password.value))) {
        fixThis += "Please enter your old password.\n"
    }

    if (!(exists(document.changepass.newpassword.value))) {
        fixThis += "Please enter your new password.\n"
    }

    if (!(exists(document.changepass.confirmpassword.value))) {
        fixThis += "Please confirm your new password.\n"
    }

    if (!(document.changepass.newpassword.value == document.changepass.confirmpassword.value)) {
        fixThis += "The passwords you entered did not match.\n"
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.changepass.submit();
    }
}

function validateApply() {

    var fixThis = "";


    if (!(exists(document.apply.name.value))) {

        fixThis += "Please enter your name.\n";
    }


    if (!(exists(document.apply.email.value))) {

        fixThis += "Please enter your email address.\n";
    }

    if (!(exists(document.apply.file.value))) {

        fixThis += "You must attach your CV.\n";
    }

    if (!(isChecked(document.apply.permit))) {

        fixThis += "Please confirm that you are able to work in the UK.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.apply.submit();
    }
}

function validateListAgency() {

    var fixThis = "";

    if (!(exists(document.onlineapp.company.value))) {

        fixThis += "Please enter your Company name.\n";
    }

    if (!(exists(document.onlineapp.contact.value))) {

        fixThis += "Please enter your name.\n";
    }


    if (!(exists(document.onlineapp.email.value))) {

        fixThis += "Please enter your email address.\n";
    }


    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.onlineapp.submit();
    }
}






function validateFeedBack() {

    var fixThis = "";

    if (!(exists(document.feedback.name.value))) {

        fixThis += "Please enter your name.\n";
    }

    if (!(exists(document.feedback.email.value))) {

        fixThis += "Please enter your email address.\n";
    }


    if (!(exists(document.feedback.comments.value))) {

        fixThis += "Please enter your comments.\n";
    }


    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.feedback.submit();
    }
}

function validateSalesTarget() {

    var fixThis = "";

    if (!(exists(document.apply.name.value))) {

        fixThis += "Please enter your name.\n";
    }

    if (!(exists(document.apply.address.value))) {

        fixThis += "Please enter your email address.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.apply.submit();
    }
}

function validateH2Office() {

    var fixThis = "";

    if (!(exists(document.apply.name.value))) {

        fixThis += "Please enter your name.\n";
    }

    if (!(exists(document.apply.address.value))) {

        fixThis += "Please enter your email address.\n";
    }

    if (!(isChecked(document.apply.chkUKResident))) {

        fixThis += "Please confirm that you are a resident in the UK or other EU country OR I have a valid UK work permit.\n";
    }

    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.apply.submit();
    }
}

function validateDataCapture() {

    var fixThis = "";

    if (!(exists(document.apply.name.value))) {

        fixThis += "Please enter your name.\n";
    }

    if (!(exists(document.apply.address.value))) {

        fixThis += "Please enter your email address.\n";
    }


    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.apply.submit();
    }
}

function validateCVebook() {

    var fixThis = "";

    if (!(isChecked(document.frmCVbook.accept))) {

        fixThis += "You must accept the terms & conditions to order.\n";
    }



    if (fixThis != "") {
        alert(fixThis);

    } else {

        document.frmCVbook.submit();
    }
}



function ValidateElectricianForm(formdata) {


    var fixThis = "";


    if (!(exists(formdata.title.value))) {
        fixThis += "Please select a Title.\n"
    }

    if (!(exists(formdata.firstname.value))) {
        fixThis += "Please input your First Name.\n"
    }

    if (!(exists(formdata.lastname.value))) {
        fixThis += "Please input your Surname.\n"
    }

    if (!(exists(formdata.address.value))) {
        fixThis += "Please input your Address.\n";

    }
    if ((!(exists(formdata.address2.value))) && (formdata.address.value != "")) {
        fixThis += "Please input your Address2.\n"
    }

    if (!(exists(formdata.city.value))) {
        fixThis += "Please input your City or Town.\n"
    }

    if (!(exists(formdata.postcode.value))) {
        fixThis += "Please input your Postcode.\n"
    }

    if (!(exists(formdata.phone.value))) {
        fixThis += "Please input your Phone Number.\n"
    }

    if (!(ChkEmail(formdata.email.value)) || (formdata.email.value == "")) {
        fixThis += "Please enter a valid email address.\n";
    }

    if (fixThis != "") {
        alert(fixThis);
        return false;
    } else {
        return true;
    }
}



function ChkEmail(str) {
    var pattern = /^[\w-_.]*[\w-_.]@[\w].+[\w]+[\w]$/i;
    return pattern.test(str);
}
