﻿$(document).ready(function() {
    if (!$.browser.msie) {
        $("#ZipLeft").css("padding-top", "12px");
        $("#ZipRight").css("padding-top", "12px");
    }

    $("#firstname").bind("blur", function() {
        if ($(this).val().length < 2) {
            $("#RequiredFieldValidator1").css("visibility", "visible");
            documentOK = false;
        }
    });
    $("#LastName").bind("blur", function() {
        if ($(this).val().length < 2) {
            $("#RequiredFieldValidator2").css("visibility", "visible");
            documentOK = false;
        }
    });
    $("#Address1").bind("blur", function() {
        if ($(this).val().length < 5) {
            $("#RequiredFieldValidator3").css("visibility", "visible");
            documentOK = false;
        }
    });
    $("#City").bind("blur", function() {
        if ($(this).val().length < 2) {
            $("#RequiredFieldValidator4").css("visibility", "visible");
            documentOK = false;
        }
    });
    $("#ZipCode").bind("blur", function() {
        if ($(this).val().length < 5) {
            $("#RequiredFieldValidator5").css("visibility", "visible").html("You must enter a Zip Code")
            documentOK = false;
        }
    });
    $("#ZipCode").bind("keypress", function(e) {
        return NumbersOnly(e, this)
    });
    $("#Email").bind("blur", function() {
        if (!echeck($(this).val())) {
            $("#RequiredFieldValidator6").css("visibility", "visible")
            documentOK = false;
        }
    });
    $("#btn_order_now").bind("click", function() {
        return ValidateForm();
    });

});

function ValidateForm() {
    var documentOK = true;

    if ($("#firstname").val().length < 2) {
        $("#RequiredFieldValidator1").css("visibility", "visible");
        documentOK = false;
    }
    if ($("#LastName").val().length < 2) {
        $("#RequiredFieldValidator2").css("visibility", "visible");
        documentOK = false;
    }
    if ($("#Address1").val().length < 5) {
        $("#RequiredFieldValidator3").css("visibility", "visible");
        documentOK = false;
    }
    if ($("#City").val().length < 2) {
        $("#RequiredFieldValidator4").css("visibility", "visible");
        documentOK = false;
    }
    if ($("#ZipCode").val().length < 5) {
        $("#RequiredFieldValidator5").css("visibility", "visible").html("You must enter a Zip Code")
        documentOK = false;
    }
    if (!echeck($("#Email").val())) {
        $("#RequiredFieldValidator6").css("visibility", "visible")
        documentOK = false;
    }
    return documentOK;
}
function echeck(str) {
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1) {
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (str.indexOf(" ") != -1) {
        return false;
    }
    return true;
}

function NumbersOnly(e, me) {
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        $("#RequiredFieldValidator5").html("This field only allows numerical input").css("visibility", "visible");
        return false;
    }
}
