﻿///<Reference path="jquery.min.js"/>
///<Reference path="jquery.validate.js"/>

$.validator.setDefaults({
    debug: false
});

//$.validator.setDefaults({
//    submitHandler: funSubmitForm
//});

$().ready(funDocReady);

function funDocReady() {

    /*2010-09-30 Cancel because Estimated budget is removed
    $.validator.addMethod("checkBudgetBelow", function(value) {
    var val = $("#txtBudgetBelow").val();
    if (val == "") {
    return true;
    }
    var num = parseFloat(val);
        
    if (isNaN(num)) {
    return false;
    }
    if (num <= 0) {
    return false;
    }
    return true;
    }, "");

    function checkBudgetBetwwen() {
    var val1 = $("#txtBudgetStart").val();
    var val2 = $("#txtBudgetEnd").val();
    var num1 = parseFloat(val1);
    var num2 = parseFloat(val2);
    if (val1 == val2) {
    return true;
    }
    if (isNaN(num1) || num1 <= 0) {
    return false;
    }
    if (isNaN(num2) || num2 <= 0) {
    return false;
    }
    if (num1 > num2) {
    $("#lblBudgetStart").text("The lower budget value cannot be greater than the upper one.");
    return false;
    }
    return true;
    }

    $.validator.addMethod("checkBudgetStart", function(value) {
    var val1 = $("#txtBudgetStart").val();
    var val2 = $("#txtBudgetEnd").val();
    var num1 = parseFloat(val1);
    var num2 = parseFloat(val2);

        if (val1 == "" && val2 == "") {
    $("#lblBudgetEnd").text("");
    $("#lblBudgetStart").text("");
    return true;
    }
        
    if (val2 != "" && val1 == "") {
    $("#lblBudgetStart").text("The lower end of budget is required");
    return false;
    }

        if (isNaN(num1) || num1 <= 0) {
    $("#lblBudgetStart").text("Invalid input for the lower end of budget");
    return false;
    }
    if (val2 == "") {
    $("#lblBudgetEnd").text("The upper end of budget is required");
    return true;
    }

        if (isNaN(num2) || num2 <= 0) {
    $("#lblBudgetEnd").text("Invalid input for the upper end of budget");
    return true;
    }
    if (num1 > num2) {
    $("#lblBudgetStart").text("The lower budget value cannot be greater than the upper one");
    return false;
    }
    return true;
    }, "");

    $.validator.addMethod("checkBudgetEnd", function(value) {
    var val1 = $("#txtBudgetStart").val();
    var val2 = $("#txtBudgetEnd").val();
    var num1 = parseFloat(val1);
    var num2 = parseFloat(val2);

        if (val1 == "" && val2 == "") {
    $("#lblBudgetEnd").text("");
    $("#lblBudgetStart").text("");
    return true;
    }

        if (val1 != "" && val2 == "") {
    $("#lblBudgetEnd").text("The upper end of budget is required");
    return false;
    }

        if (isNaN(num2) || num2 <= 0) {
    $("#lblBudgetEnd").text("Invalid input for the upper end of budget");
    return false;
    }
    if (val1 == "") {
    $("#lblBudgetStart").text("The lower end of budget is required");
    return true;
    }

        if (isNaN(num1) || num1 <= 0) {
    $("#lblBudgetStart").text("Invalid input for the lower end of budget.");
    return true;
    }
    if (num2 < num1) {
    $("#lblBudgetEnd").text("The upper budget value cannot be smaller than the lower one");
    return false;
    }
    return true;
    }, "");   */     
    
    $("#frmMain").validate({
        rules: {
            txtName: {
                required: true,
                minlength: 3
            },
            /*
            2009-04-08, Canceled by Rock due to Connie request
            txtTitle: {
            required: true,
            minlength: 3
            },
            txtAddress: {
            required: true              
            },
            txtCompany: {
            required: true,
            minlength: 3
            }
            ,*/
            txtEmail: {
                required: true,
                email: true
            },
            txtPhone: {
                minlength: 5
            },
            /*sltDate: "checkYear"
            ,
            txtBudgetBelow: "checkBudgetBelow",
            txtBudgetStart: "checkBudgetStart",
            txtBudgetEnd: "checkBudgetEnd",  */
            services: {
                required: true
            }

        },
        messages: {
            txtName: {
                required: "Name is required",
                minlength: "At least 3 characters"
            },
            /*
            2009-04-08, Canceled by Rock due to Connie request
            txtTitle: {
            required: "Title is required",
            minlength: "At least 3 characters"
            },
            txtAddress: {
            required: "Country is required"                
            }
            ,
            txtCompany: {
            required: "Company is required",
            minlength: "At least 3 characters"
            },*/
            txtEmail: {
                required: "Email is required",
                email: "Email is invalid"
            },
            txtPhone: {
                minlength: "At least 5 characters"
            },
    
            services: {
            required: "At least 1 service"
        }

    }
});

    $("#frmMain").submit(funSubmitForm);    
    $("#imgSubmit").click(funSubmitForm);    
    $("#imgCancel").click(function() { window.location.href = "http://axisoft.net/ags/home.html"; });
    $("form img").css("cursor", "pointer");
//    InitYearMonth();
    InitVisiblity();

//    $("#trBudget input[type=radio]").click(funChangeBudget);
    $("#trNature input[type=radio]").click(funChangeNature);
    //TODO:$("#

//    $("#sltYear").change(funCheckMonth);
    $("#chkPerfTest").click(function() { funToggleChild("chkPerfTest") });
    $("#chkSecTest").click(function() { funToggleChild("chkSecTest") });
    $("#chkNoUser").click(function() { funCheckOtherTextBox("chkNoUser", "txtNoUser"); });    
    $("#chkOtherCode").click(function() { funCheckOtherTextBox("chkOtherCode", "txtOtherCode"); });
    $("#chkOtherDoc").click(function() { funCheckOtherTextBox("chkOtherDoc", "txtOtherDoc"); });
    $("#chkOtherTest").click(function() { funCheckOtherTextBox("chkOtherTest", "txtOtherTest"); });
    $("#chkOther").click(function() { funCheckOtherTextBox("chkOther", "txtOtherService"); });
    
};

/*function funCheckMonth() {
    var y = $("#sltYear").val();
    $("#sltMonth").empty();
    var item = "<option value=''></option>";
    $("#sltMonth").append(item);
    
    if (y == "") {        
        return;
    }
    var sltMonth = document.getElementById("sltMonth");
    var idx = 1;
    var dat = new Date();

    if (parseInt(y) == dat.getFullYear()) {
        idx = dat.getMonth() + 1;
    }  
    
    for (var x = idx; x < 13; x++) {
       var month = x.toString();
       item = "<option value='" + month + "'>" + month + "</option>";

        $("#sltMonth").append(item);
    }
   
} */

function funToggleChild(parentName) {
    var chk = $("#" + parentName).attr("checked");
    //alert(chk);
    if (chk) {
        $("#divST input[parent=" + parentName + "]").attr("disabled", "");
        //$("#divST input[parent=" + parentName + "][type=text]").css("background-color", "white");
    }
    else {
        $("#divST input[parent=" + parentName + "]").attr("disabled", "disabled");
        $("#divST input[parent=" + parentName + "][type=text]").css("background-color", "#DDDDDD");
        $("#divST input[parent=" + parentName + "]").attr("checked", "");
        $("#divST input[parent=" + parentName + "]").val("");
        
    }
}

/*function funChangeBudget() {
    $("#trBudget input[type=text]").val("");
    var texts = $("#trBudget input[type=text]");
    texts.css("background-color", "#DDDDDD");
    texts.attr("disabled", "disabled");
    $("#trBudget input[type=text]").valid();
    
    if ($("#rdoBudget1").attr("checked")) {      
        //do nothing
    }
    if ($("#rdoBudget2").attr("checked")) {
        $("#trBudget input[type=text]").val("");
        $("#txtBudgetBelow").css("background-color", "white");
        $("#txtBudgetBelow").attr("disabled", "");
    }
    if ($("#rdoBudget3").attr("checked")) {
        $("#trBudget input[type=text]").val("");
        $("#txtBudgetStart").css("background-color", "white");
        $("#txtBudgetStart").attr("disabled", "");
        $("#txtBudgetEnd").css("background-color", "white");
        $("#txtBudgetEnd").attr("disabled", "");
    }
}*/

function funChangeNature() {
    $("#txtOtherNature").css("background-color", "#DDDDDD");
    $("#txtOtherNature").attr("disabled", "disabled");
    $("#txtOtherNature").val("");
    if ($("#rdoNature4").attr("checked")) {
        $("#txtOtherNature").css("background-color", "white");
        $("#txtOtherNature").attr("disabled", "");
    }
}

function funCheckVisible(section, vis) {   
    var selectedValues ="";

    $("#tdService input[type=checkbox]").each(function() {
    if ($(this).attr("checked") && $(this).attr("id") !="chkOther") {
            selectedValues += $(this).attr("value");
        }
    });  
    
    if (!vis) {
        $("#" + section).addClass("visible");
    }
    else {
        $("#" + section).removeClass("visible");
    }
}

function funCheckOtherTextBox(otherCHKID, otherTxtID) {
    var chk = $("#" + otherCHKID).attr("checked");
    if (chk) {
        $("#" + otherTxtID).attr("disabled", "");
        $("#" + otherTxtID).css("background-color", "white");
    }
    else {
        $("#" + otherTxtID).attr("disabled", "disabled");
        $("#" + otherTxtID).css("background-color", "#DDDDDD");
        $("#" + otherTxtID).val("");
    }
}

//function InitYearMonth() {
//    var sltYear = document.getElementById("sltYear");
//    for (var x = 0; x < 11; x++) {
//        var y = new Date().getFullYear();
//        var year = (y + x).toString();
//        var item = "<option value='" + year + "'>" + year +"</option>";        
//        
//        $("#sltYear").append(item);
//    }   
//}

function InitVisiblity() {
    $("div[id*=div]").addClass("visible");
    funCheckOtherTextBox("chkOther", "txtOtherService");
    funCheckOtherTextBox("rdoNature4", "txtOtherNature");
    funCheckOtherTextBox("chkNoUser", "txtNoUser");
    
    $("#imgLoading").addClass("visible");
    var texts = $("#trBudget input[type=text]");
    texts.css("background-color", "#DDDDDD");
    texts.attr("disabled", "disabled");
    $("#txtOtherNature").css("background-color", "#DDDDDD");
    $("#txtOtherNature").attr("disabled", "disabled");
    $("#divST input[parent]").attr("disabled", "disabled");
    $("#divST input[type=text]").attr("disabled", "disabled");
    $("#divST input[type=text]").css("background-color", "#DDDDDD");
}

//function funCheckDeadline() {
//    var ret = false;
//    var dat = new Date();
//    var y = $("#sltYear").val();
//    var m = $("#sltMonth").val();
//    if (y == "" && m == "") {
//        ret = true;
//    }
//    else if (y == "") {
//        ret = false;
//    }
//    else if (y < dat.getFullYear()) {
//        ret = false;
//    }
//    else if (y == dat.getFullYear() && m < (dat.getMonth()+1)) {
//        ret = false;
//    }
//    else {
//        ret = true;
//    }
//   
//    if (ret) {
//        $("#lblDate").css("display", "none");
//    }
//    else {
//        $("#lblDate").css("display", "inline");
//    }
//    //alert(ret);
//}

function funSubmitForm() {
    var valid = $("#frmMain").valid();
    if (!valid) {
        alert("Form is incomplete or incorrect, please check and try again.");
        return false;
    }
    //$("#debug").empty();
    //alert("Ajax start");

    //collect data inputted by user
    var name = $("#txtName").val();
    var title = $("#txtTitle").val();
    var company = $("#txtCompany").val();
    var address = $("#txtAddress").val();
    var email = $("#txtEmail").val();
    var phone = $("#txtPhone").val();
    var selectedServices = "";

    $("#tdService input[type=checkbox]").each(function() {
        if ($(this).attr("checked")) {
            selectedServices += $(this).attr("value");
            if ($(this).attr("id") == "chkOther") {
                selectedServices += "(";
                selectedServices += $("#txtOtherService").val();
                selectedServices += ")";
            }
            selectedServices += "<br>";

        }
    });    
    
    var projectDesc = $("#txtProjectDesc").val();
    var mon = $("#sltMonth").val();
    if (mon!="" && parseInt(mon) < 10) {
        mon="0"+mon;
    }

   /* var projectDeadline = $("#sltYear").val() + "/" + mon;
    var budget = "";
    if ($("#rdoBudget1").attr("checked")) {
        budget = "Undecided";
    }
    if ($("#rdoBudget2").attr("checked")) {
        budget = "Below: " + $("#txtBudgetBelow").val();
    }
    if ($("#rdoBudget3").attr("checked")) {
        budget = "Between: " + $("#txtBudgetStart").val() + " to " + $("#txtBudgetEnd").val();
    }*/

    var pbDS = "";
    if ($("#rdoPBBS").attr("checked")) {
        pbDS = "B/S";
    }
    if ($("#rdoPBCS").attr("checked")) {
        pbDS = "C/S";
    }
    
    var pcDS = $("#txtPCDS").val();
    var boVer = $("#txtBOver").val();
    var etl = "";
    if ($("#rdoBOETL1").attr("checked")) {
        etl = "Yes";
    }
    if ($("#rdoBOETL2").attr("checked")) {
        etl = "No";
    }
    if ($("#rdoBOETL3").attr("checked")) {
        etl = "Undecided";
    }
    
    var mtl = "";
    if ($("#rdoBOMTL1").attr("checked")) {
        mtl = "Yes";
    }
    if ($("#rdoBOMTL2").attr("checked")) {
        mtl = "No";
    }
    if ($("#rdoBOMTL3").attr("checked")) {
        mtl = "Undecided";
    }

    var dnVer = "";
    if ($("#rdoDNVer1").attr("checked")) {
        dnVer = "R5";
    }
    if ($("#rdoDNVer2").attr("checked")) {
        dnVer = "R6";
    }
    if ($("#rdoDNVer3").attr("checked")) {
        dnVer = "R6.5";
    }
    if ($("#rdoDNVer4").attr("checked")) {
        dnVer = "R7";
    }
    if ($("#rdoDNVer5").attr("checked")) {
        dnVer = "R8";
    }

    var number = $("#sltNumber").val();
    var user = "";
    if ($("#rdoUser1").attr("checked")) {
        user = "Yes";
    }
    if ($("#rdoUser2").attr("checked")) {
        user = "No";
    }
    if ($("#rdoUser3").attr("checked")) {
        user = "Undecided";
    }
    var nature = "";
    if ($("#rdoNature1").attr("checked")) {
        nature = "Document Management System";
    }
    if ($("#rdoNature2").attr("checked")) {
        nature = "Workflow";
    }
    if ($("#rdoNature3").attr("checked")) {
        nature = "Imaging";
    }
    if ($("#rdoNature4").attr("checked")) {
        nature = "Others, please specify (" + $("#txtOtherNature").val() + ")";
    }

    var pbVis = !$("#divPB").hasClass("visible");
    var pcVis = !$("#divPC").hasClass("visible");
    var boVis = !$("#divBO").hasClass("visible");
    var ldVis = !$("#divLD").hasClass("visible");
    var stVis = !$("#divST").hasClass("visible");
    var testType = "";
    if ($("#chkFunTest").attr("checked")) {
        testType = $("#chkFunTest").val();
        testType += "<br>";
    }

    if ($("#chkLoadTest").attr("checked")) {
        if (testType == "") {
            testType = $("#chkLoadTest").val();
        }
        else {           
            testType += $("#chkLoadTest").val();
        }
        testType += "<br>";
    }

    if ($("#chkStrTest").attr("checked")) {
        if (testType == "") {
            testType = $("#chkStrTest").val();
        }
        else {
            testType += $("#chkStrTest").val();
        }
        testType += "<br>";
    }


    if ($("#chkPerfTest").attr("checked")) {
        if (testType == "") {
            testType = $("#chkPerfTest").val();
        }
        else {            
            testType += $("#chkPerfTest").val();
        }
        testType += "<br>";
        $("#trTestType [parent=chkPerfTest]").each(function() {
            if ($(this).attr("checked")) {
                testType += "&nbsp;&nbsp;&nbsp;";
                testType += $(this).val();
                if ($(this).attr("id") == "chkNoUser") {
                    testType += "&nbsp;&nbsp;&nbsp;";
                    testType += "(";
                    testType += $("#txtNoUser").val();
                    testType += ")";
                }
                testType += "<br>";
            }
        });

    }

    if ($("#chkSecTest").attr("checked")) {
        if (testType == "") {
            testType = $("#chkSecTest").val();
        }
        else {            
            testType += $("#chkSecTest").val();
        }
        testType += "<br>";

        $("#trTestType [parent=chkSecTest]").each(function() {
            if ($(this).attr("checked")) {
                testType += "&nbsp;&nbsp;&nbsp;";
                testType += $(this).val();
                testType += "<br>";
            }
        });
    }
  
    var codeReview = "";
    $("#trCodeReview input[type!=text]").each(function() {
        if ($(this).attr("checked")) {         
            codeReview += $(this).val();            
            if ($(this).attr("id") == "chkOtherCode") {
                codeReview += "(";
                codeReview += $("#txtOtherCode").val();
                codeReview += ")";
            }
            codeReview += "<br>";
        }
    });

    var doc = "";
    $("#trDoc input[type!=text]").each(function() {
        if ($(this).attr("checked")) {
            doc += $(this).val();            
            if ($(this).attr("id") == "chkOtherDoc") {
                doc += "(";
                doc += $("#txtOtherDoc").val();
                doc += ")";
            }
            doc += "<br>";
        }
    });
    
    var otherTest = "";
    $("#trOhterTest input[type!=text]").each(function() {
        if ($(this).attr("checked")) {
            otherTest += $(this).val();           
            if ($(this).attr("id") == "chkOtherTest") {
                otherTest += "(";
                otherTest += $("#txtOtherTest").val();
                otherTest += ")";
            }
            otherTest += "<br>";
        }
    });
    
    
    $("#imgLoading").removeClass("visible");
    $.ajax({ url: 'SendEmail.aspx',
        type: 'POST',
        data: { Name: name, Title: title, Company: company, Address: address, Email: email, Phone: phone,
            Services: selectedServices, ProjectDesc: projectDesc,// ProjectDeadline: projectDeadline, Budget: budget, 
            PBDS: pbDS, PCDS: pcDS, BOVer: boVer, ETL: etl, MTL: mtl,
            DNVer: dnVer, Number: number, User: user, Nature: nature,
            PBVis: pbVis, PCVis: pcVis, BOVis: boVis, LDVis: ldVis, STVis: stVis,
            TestType: testType, CodeReview: codeReview, OtherTest: otherTest, Doc: doc
        },
        dataType: 'html',
        timeout: 8000,
        error: function() { alert('Error when sending your request. Please check and try again.'); $("#imgLoading").addClass("visible"); },
        success: function(result) { /*alert("Thank you."); return;*/ window.location.replace("thanks.html"); }
    });
}

