//*****************************************
//    STAGE 1 validation
//*****************************************

function ValidateStage1() {
    
    //clear all error highlights
    $('#form_step1 > *').removeClass("highlight_error");
    $("#errorbox").html("").css('display', 'none');
    
    // init vars
    var response = 0;
    var errorlist = new Array();

    // check for valid start date
    if (valid_required($("input[name='start_date']").val())== false) {
        errorlist.push("Please enter a valid start date");
        $("input[name='start_date']").addClass("highlight_error");
    }
    
    if ($('#terms_yes').attr('checked')== false) {
      errorlist.push("Please accept our Terms and Conditions");
      $("input[name='terms']").addClass("highlight_error");
    }

    if (errorlist.length > 0) {
        // add content to error box
        $("#errorbox").append("<ul>").css('display', 'block');
        window.scroll(0, 250);
        for(var i=0;i<errorlist.length;i++) {
            $("#errorbox ul").append("<li>"+errorlist[i]+"</li>");
        }
        return false;
    } else {
        // when it has passed all
        return true;
    }
}


//*****************************************
//    STAGE 2A functions
//*****************************************

function ValidateStage2A(deliverycheck) {
    
    //clear all error highlights
    $('#form_step2 > *').removeClass("highlight_error");
    $("#errorbox").html("");
    
    // init vars
    var response = 0;
    var errorlist = new Array();

    if (valid_required($("input[name='firstname']").val())== false)                                                     { errorlist.push("Please enter a First Name "); $("input[name='firstname']").addClass("highlight_error"); } 
    if (valid_required($("input[name='surname']").val())== false)                                                       { errorlist.push("Please enter a Surname "); $("input[name='surname']").addClass("highlight_error"); } 
    if (valid_required($("input[name='address1']").val())== false)                                                      { errorlist.push("Please enter a Address Line1"); $("input[name='address1']").addClass("highlight_error"); } 
    if (valid_required($("input[name='town']").val())== false)                                                          { errorlist.push("Please enter a Town"); $("input[name='town']").addClass("highlight_error"); } 
    if (valid_required($("input[name='county']").val())== false)                                                        { errorlist.push("Please enter a County"); $("input[name='county']").addClass("highlight_error"); } 
    if (valid_postcode($("input[name='postcode']").val())== false)                                                      { errorlist.push("Please enter a valid Postcode"); $("input[name='postcode']").addClass("highlight_error"); } 
    if (deliverycheck == false)                                                                                         { errorlist.push("Your postcode does not match the region you specified. Use a valid postcode, or update your region on <a href=\"/booking/mealplan/stage1\">Stage 1</a>"); $("input[name='postcode']").addClass("highlight_error"); }
    if (valid_required($("input[name='phone']").val())== false)                                                         { errorlist.push("Please enter a valid Phone "); $("input[name='phone']").addClass("highlight_error"); } 
    if (valid_required($("input[name='email']").val())== false || valid_email($("input[name='email']").val())== false)  { errorlist.push("Please enter a valid Email Address "); $("input[name='email']").addClass("highlight_error"); } 
    if ($("input[name='email']").val() != $("input[name='confirm_email']").val())                                       { errorlist.push("Please confirm your Email Address "); $("input[name='confirm_email']").addClass("highlight_error"); } 
    if (valid_required($("input[name='password']").val())== false)                                                      { errorlist.push("Please enter a valid Password"); $("input[name='password']").addClass("highlight_error"); } 
    if ($("input[name='confirm_password']").val() != $("input[name='password']").val())                                 { errorlist.push("Please confirm your Password"); $("input[name='confirm_password']").addClass("highlight_error"); } 
    if (valid_required($("input[name='captcha']").val())== false)                                                       { errorlist.push("Please enter the letters shown in the box "); $("input[name='captcha']").addClass("highlight_error"); } 

    if (errorlist.length > 0) {
        // add content to error box
        $("#errorbox").append("<ul>");
        window.scroll(0, 250);
        for(var i=0;i<errorlist.length;i++) {
            $("#errorbox ul").append("<li>"+errorlist[i]+"</li>");
        }
        return false;
    } else {
        // when it has passed all
        return true;
    }
}

//*****************************************
//    STAGE 2B functions
//*****************************************

function ValidateStage2B() {
    
    //clear all error highlights
    $('#form_step2 > *').removeClass("highlight_error");
    $("#errorbox").html("");
    
    // init vars
    var response = 0;
    var errorlist = new Array();

    if (valid_required($("input[name='email']").val())== false || valid_email($("input[name='email']").val())== false)  { errorlist.push("Please enter a valid Email Address "); $("input[name='email']").addClass("highlight_error"); } 
    if (valid_required($("input[name='password']").val())== false)                                                      { errorlist.push("Please enter a valid Password"); $("input[name='password']").addClass("highlight_error"); } 


    if (errorlist.length > 0) {
        // add content to error box
        $("#errorbox").append("<ul>");
        for(var i=0;i<errorlist.length;i++) {
            $("#errorbox ul").append("<li>"+errorlist[i]+"</li>");
        }
        return false;
    } else {
        // when it has passed all
        return true;
    }
}


//*****************************************
//    STAGE 2C functions
//*****************************************

function ValidateStage2C(deliverycheck) {
    
    //clear all error highlights
    $('#form_step2 > *').removeClass("highlight_error");
    $("#errorbox").html("");
    
    // init vars
    var response = 0;
    var errorlist = new Array();

    if (valid_required($("input[name='firstname']").val())== false)       { errorlist.push("Please enter a First Name "); $("input[name='firstname']").addClass("highlight_error"); } 
    if (valid_required($("input[name='surname']").val())== false)         { errorlist.push("Please enter a Surname "); $("input[name='surname']").addClass("highlight_error"); } 
    if (valid_required($("input[name='address1']").val())== false)        { errorlist.push("Please enter a Address Line1"); $("input[name='address1']").addClass("highlight_error"); } 
    if (valid_required($("input[name='town']").val())== false)            { errorlist.push("Please enter a Town"); $("input[name='town']").addClass("highlight_error"); } 
    if (valid_required($("input[name='county']").val())== false)          { errorlist.push("Please enter a County"); $("input[name='county']").addClass("highlight_error"); } 
    if (valid_postcode($("input[name='postcode']").val())== false)        { errorlist.push("Please enter a valid Postcode"); $("input[name='postcode']").addClass("highlight_error"); } 
    if (deliverycheck == false)                                                                                         { errorlist.push("Your postcode does not match the region you specified. Use a valid postcode, or update your region on <a href=\"/booking/mealplan/stage1\">Stage 1</a>"); $("input[name='postcode']").addClass("highlight_error"); }
    if (valid_required($("input[name='phone']").val())== false)           { errorlist.push("Please enter a valid Phone "); $("input[name='phone']").addClass("highlight_error"); } 
    if (valid_required($("input[name='captcha']").val())== false)         { errorlist.push("Please enter the letters shown in the box "); $("input[name='captcha']").addClass("highlight_error"); } 


    if (errorlist.length > 0) {
        // add content to error box
        $("#errorbox").append("<ul>");
        for(var i=0;i<errorlist.length;i++) {
            $("#errorbox ul").append("<li>"+errorlist[i]+"</li>");
        }
        return false;
    } else {
        // when it has passed all
        return true;
    }
}


//*****************************************
//    STAGE 3 functions
//*****************************************

function LoadStage3(curr_stage) {

    $('#submit_button').bind('click',function(){
        $('#form_step3').submit();
        return false;
    });

}

//*****************************************
//    Validation functions
//*****************************************


function valid_email(data) {
    var regex = /^[\w\-_]+(\.[\w\-_]+)*@[\w\-_]+(\.[\w\-_]+)+$/i;
    return regex.test(data);
}

function valid_telephone(data) {
    var regex = /^[\+]{0,2}\d{0,2}\s?\(?[\d\s]{1,5}\)?\s?[\d\s]{5,13}$/i;
    return regex.test(data);
}

function valid_postcode(data) {
    var regex = /[A-Z]{1,2}[0-9]{1,2}\s?[0-9][A-Z]{2}/i;
    return regex.test(data);
}

function valid_date(data) {
    var regex = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/i;
    return regex.test(data);
}

function valid_required(data) {
    if (data.length<=0) {
        return false;
    } else {
        return true;
    }
}

