var interest_index = 0;
var state_index = 0;
$(document).ready(function() {
    $(function() { $('#submit').bind('click', ValidateForm); });
    $(function() { $('#ddlInterest').bind('change', InterestChange); });
    $(function() { $('#states1').bind('change', StatesChange); });
});
function ValidateForm(e) {
    var valid = true;
    valid = (checkRequired('#txtBname', "Name of Business", '#valBName')==false) ? false : valid;
    valid = (checkRequired('#txtCname', "Contact Name",  '#valCName')==false) ? false : valid;
    valid = (checkRequired('#txtCity', "City", '#valCity') == false) ? false : valid;
    valid = (checkRequired('#states1', "State", '#valState') == false) ? false : valid;

    valid = (checkRequired('#txtPhone', "Phone", '#valPhone') == false) ? false : valid;
    valid = (checkRequired('#txtEmail', "Email", '#valEmail') == false) ? false : valid;

    valid = (checkRequired('#states2', "State", '#valPState') == false) ? false : valid;
    valid = (checkRequired('#ddlInterest', "Service", '#valInterest') == false) ? false : valid;

    if (interest_index == 1) {
        valid = (checkRequiredGroup("box_set1", "Select at least one!", '#valbox_set1') == false) ? false : valid;
        valid = (checkRequiredGroup("box_set2", "Select at least one!", '#valbox_set2') == false) ? false : valid;
        valid = (checkRequiredGroup("box_set3", "Select at least one!", '#valbox_set3') == false) ? false : valid;
    }
    if (interest_index == 2) {
        valid = (checkRequiredGroup("box_set4", "Select at least one!", '#valbox_set4') == false) ? false : valid;
        valid = (checkRequiredGroup("box_set5", "Select at least one!", '#valbox_set5') == false) ? false : valid;
        valid = (checkRequiredGroup("box_set6", "Select at least one!", '#valbox_set6') == false) ? false : valid;
    }
    if (state_index == 1) {
        valid = (checkRequired('#txtCountry', "Country", '#valCountry') == false) ? false : valid;
    }

    //valid = true;
    return valid;
}
function toggleParentCheckboxes(current, form) {
    var checked = ($("#" + form + " :checkbox[name='" + current.name + "']").length == $("#" + form + " :checkbox[name='" + current.name + "']:checked").length);
    $("#" + form + " :checkbox[name='" + current.name.replace("[]", "") + "']").attr("checked", checked);
}
function InterestChange(e) {
    $('#tbl1').hide();
    $('#tbl2').hide();
    $('#tbl3').hide();
    interest_index = 0;
    
    var value = $('#ddlInterest').val();
    if ($.trim(value).length > 0) {
        $('#tbl3').show();
        interest_index = 3;
        if (value == "Design/Build Services") {
            $('#tbl1').show();
            interest_index = 1;
        }
        if (value == "Design/Engineering") {
            $('#tbl2').show();
            interest_index = 2;
        }
    }
    
}
function updateTips(t, ctl) {
    $(ctl).text(t);
}
function checkRequired(o, n, ctl) {
    var value = $(o).val();
    var ret = $.trim(value).length > 0;
    var msg = "";
    if (ret == false)
        msg = " " + n + " is required!";
    updateTips(msg, ctl);
    return ret;
}

function checkRequiredGroup(o, n, ctl) {
    var group_checked = $("input[id=" + o + "]:checked").length;
    var ret = group_checked > 0;
    var msg = "";
    if (ret == false)
        msg =  n ;
    updateTips(msg, ctl);
    return ret;
}
function CheckBoxChange(checkbox) {
    var name = "box_set4";
    var value = false;
    value = checkbox.checked;
    //alert(value);
    $("INPUT[id=" + name + "]").attr('checked', value);
}
function StatesChange(e) {
    $('#tbl0').hide();
    state_index = 0;

    var value = $('#states1').val();
    if ($.trim(value).length > 0) {        
        if (value == "Other") {
            $('#tbl0').show();
            state_index = 1;
        }
        
    }

}
