/// <reference path="jquery-1.3.2.min-vsdoc.js" />

var formIsValid = false;
var overrideValidation = false;

$("document").ready(function() {

    //redirect to secure version of page if we are not there.
    if (document.location.protocol != "https:") {
        var x = "https://casb.worldsecuresystems.com" + document.location.pathname;
        window.location.replace(x);

    }


    //wire up go back even on payment form
    $("#goBack").click(function() {
        $("#paymentInformation").css("display", "none");
        $("#baseForm").show("fast");
        return false;
    });

    //wire up goto payment button on base form
    $("#baseFormButton").click(function() {

        DoFormValidation("#baseForm");


        if (formIsValid) {
            $("#baseForm").css("display", "none");
            $("#paymentInformation").show("fast");
            var amount = CalculateFormAmount();
            $("#Amount").val(amount);
        }
        //keeps button from submitting form
        return false;
    });


    $("#catwebformbutton").click(function() {
        DoFormValidation("#paymentInformation");
        if (!formIsValid)
            return false;
    });

});

//Ability to Add more validation routines later on.
function DoFormValidation(formSectionId) {

    var globalWhy = "";

    var requriedFieldwhy = DoRequiredFieldValidation(formSectionId);

    globalWhy += requriedFieldwhy;

    if (globalWhy == "" || overrideValidation)
        formIsValid = true;
    else {
        formIsValid = false;
        alert(globalWhy);
    }
}

function DoRequiredFieldValidation(formSectionId) {

    var whyRequired = "";

    //All fields with .formRequired class must have value    
    $(formSectionId + " .formRequired").each(function(args) {

        //add filed not valid css to missing required fields, and set validtion message.  
        //Message comes from validation attribute on element.
        if ($(this).val() == "") {
            whyRequired += $(this).attr("validation") + "\n";
            $(this).addClass("fieldNotValid");
            $(this).bind("change", null, function() {
                $(this).removeClass("fieldNotValid");
            });
        }

        //remove class if later on field becomes valid
        else {
            $(this).removeClass("fieldNotValid");
        }

    });
    return whyRequired;

}

function show(num) {

    //credit card
    if (num == 0) {
        $("#ccDetails").css("display", "block");
        $("#poNumber").css("display", "none");
        overrideValidation = false;
        //set value to cc method
        $("#PaymentMethodType_1").val(1);
    }
    //check
    if (num == 1) {
        $("#ccDetails").css("display", "none");
        $("#poNumber").css("display", "none");
        overrideValidation = true;
        //set value to cod method
        $("#PaymentMethodType_1").val(3);
    }
    //po
    if (num == 2) {
        $("#ccDetails").css("display", "none");
        $("#poNumber").css("display", "block");
        overrideValidation = true;
        //set value to cod method
        $("#PaymentMethodType_1").val(3);
    }
}
