﻿var FormHelper = {};
FormHelper.DisposeToolkitExtender = function(id) {
    var component = Sys.Application.findComponent(id);
    if (component != null && component != 'undefined') {
        component.dispose();
    }
};
FormHelper.datePickerOnClientShowing = function(id) {
    var component = Sys.Application.findComponent(id);
    if (component != null && component != 'undefined') {
        component._todaysDate = new Date(new Date().getFullYear() - 18, 1, 1, 12);
        component._today.style["display"] = "none";
    }
}
FormHelper.datePickerOnClientShown = function(id) {
    var component = Sys.Application.findComponent(id);
    if (component != null && component != 'undefined') {
        component._switchMode("years", false);
    }
}
FormHelper.parseDateInput = function(textBoxId, dateValueId) {
    var textBox = $get(textBoxId);
    textBox.value = textBox.value.replace(/(\d+)[\/\.\s-](\d+)[\/\.\s-](\d+)/, '$3/$2/$1');
    var twoLetterYear = textBox.value.match(/^\d\d(?=\/)/);
    textBox.value = textBox.value.replace(/^(\d\d\/)/, '20$1')
    var d = new Date(textBox.value);

    if (d == 'NaN') { return; }

    if (twoLetterYear) {
        if (d.getFullYear() > 2030) /* Assume dates 00-30 means 2000-2030 and assume 31-99 means 1931-1999 */
        {
            d.setYear(d.getYear() - 100);
        }
    }

    $get(dateValueId).value = d.format('yyyy-MM-dd');

    var b = Sys.UI.Behavior.getBehaviorByName(textBox, 'CalendarBehavior');
    if (b != null) {
        b.set_selectedDate(d);
    }
};
FormHelper.showDialog = function(dialogId, invokeId, causesValidation) {
    var validationResult = true;
    if (causesValidation && typeof(Page_ClientValidate) == 'function') {
        validationResult = Page_ClientValidate();
    }

    if (validationResult) {
        $get(dialogId).buttonId = invokeId;
        $.blockUI({ message: $('#' + dialogId) });
    }
};
FormHelper.purchaseFormValidation = function(oSource, args) {
    var valid = true;

    if ($get(FormHelper.purchaseFormValidation.QuantityId).value == ''
        && $get(FormHelper.purchaseFormValidation.VoucherId).value == '') {
        valid = false;
    }

    args.IsValid = valid;
}
FormHelper.questionValidate = function(oSrouce, args) {
    var valid = true;

    for (var ctr = 0; ctr < FormHelper.questionContainerList.length; ctr++) {
        valid = valid && FormHelper.questionValidateRadios(FormHelper.questionContainerList[ctr]);
    }
    args.IsValid = valid;
};
FormHelper.questionValidateRadios = function(containerId) {
    var radios = $("#" + containerId + " input");
    for (var ctr = 0; ctr < radios.length; ctr++) {
        if (radios[ctr].checked) {
            return true;
        }
    }
    return false;
}
FormHelper.questionRegister = function(containterId) {
    FormHelper.questionContainerList[FormHelper.questionContainerList.length] = containterId;
};
FormHelper.questionContainerList = new Array();
FormHelper.hideFormMessagesOnValidation = function(oSrouce, args) {
    $('.success').attr("style", "display:none;");
    $('.fail').attr("style", "display:none;");
    args.IsValid = true;
};
FormHelper.beenSubmitted = false;
FormHelper.preventDoubleSubmit = function() {
    if (FormHelper.beenSubmitted)
    { return false; }
    else 
    {
        FormHelper.beenSubmitted = true;
        return true; 
    }
};