function LinkCheckBoxOptionOthers(OptionControlNamePrefix, TextInputID, OptionOthersIndex) {
    var oName = OptionControlNamePrefix + '$' + OptionOthersIndex;
    $("input[name='" + oName + "']").click(function() {
        if (oName === $(this).attr('name')) {
            if ($(this).is(':checked')) {
                $('#' + TextInputID).removeAttr('disabled');
                $('#' + TextInputID).focus();
            } else {
                $('#' + TextInputID).attr('disabled', true);
            }
        }
    });
}

function LinkRadioBoxOptionOthers(OptionControlNamePrefix, TextInputID, others) {
    $("input[name^='" + OptionControlNamePrefix + "']").click(function() {
        if (others === $(this).val()) {
            $('#' + TextInputID).removeAttr('disabled');
            $('#' + TextInputID).focus();
        } else {
            $('#' + TextInputID).attr('disabled', true);
        }
    });
}
function show_popup(type, titleContent, contentURL) {
    switch (type) {
        case 1:
            var displayTitle = $("#" + titleContent).html();
            tb_show(displayTitle, "#TB_inline?height=307&width=644&inlineId=" + contentURL);
            break;
        case 2:
            tb_show(titleContent, contentURL + "?TB_iframe=true&height=307&width=644");
            break;
        case 3: //Email Signup
            tb_show(titleContent, contentURL + "?TB_iframe=true&height=150&width=300");
            break;
        case 4: //Email Signup
            tb_show(titleContent, contentURL + "?TB_iframe=true&height=430&width=650");
            break;
        case 5:
            var displayTitle = $("#" + titleContent).html();
            tb_show(displayTitle, "#TB_inline?height=150&width=450&inlineId=" + contentURL);
            break;
    }
}
String.prototype.endsWith = function(pattern) {
    var d = this.length - pattern.length;
    return d >= 0 && this.lastIndexOf(pattern) === d;
};

function Generate_dropdown(pContentSource, pTargetDiv, pLabelID) {
    var displayContent;

    //CatSubMenuUL_238Courts, CatSubMenuItem_238Courts
    displayContent = "<select onchange='dropdown_redirect(this.value)'>";
    displayContent += "<option value='" + $("a#" + pContentSource.replace('UL', 'Item')).attr('href') + "'>select one</option>"
    var href = unescape(document.location.href);
    //alert('href=' + unescape(href));
    var c = 0;
    $("#" + pContentSource + " li").each(function(i) {
        var childID = this.id;
        var childLink = $("#" + childID + " a").attr('href');
        var childContent = $("#" + childID + " a").html();

        var path = childLink.substr(35, childLink.length - 48);
        //alert('path=' + path);
        displayContent += "<option value='" + childLink + "'";
        if (href.indexOf(path) != -1) {
            displayContent += " selected ";
        }

        displayContent += ">" + childContent + "</option>";
        c++;
    });

    displayContent += "</select>";
    if (c > 0) {
        $("#" + pTargetDiv).html(displayContent);
        if (pLabelID != '') {
            $("#" + pLabelID).show();
        }
    } else {
        $("#" + pTargetDiv).hide();
        if (pLabelID != '') {
            $("#" + pLabelID).hide();
        }
    }
}

function dropdown_redirect(pURL) {
    //var domain = "http://www.ecourts.sg";
    //var redirectURL = domain + pURL;
    if (pURL == null || '' == pURL) {
        return;
    }
    document.location = pURL;
}

function RetrievePostalAddress(p, b, s, d, u) {
    try {
        var v = $.trim($('#' + p).val());
        if (v.length < 6) {
            return;
        }
        if (!v.match(/\d{6}/)) {
            AlertInvalidPostalCode(p);
            return false;
        }
        $.getJSON("/AJAX/x.aspx", { p: v }, function(json) {
            if (!json.valid) {
                AlertInvalidPostalCode(p);
                return false;
            }
            $('#' + b).val(json.block);
            $('#' + s).val(json.street);
            $('#' + d).val(json.building);

            $('#' + u).focus();
        });
    } catch (err) {
    }
    return false;
}

function CheckPostalCodeInput(pcID, bID) {
    var pc = $.trim($('#' + pcID).val());
    if (pc.length == 6 && /[0-9]{6}/.test(pc)) {
        $('#' + bID).click();
    }
    return true;
}

function AlertInvalidPostalCode(p) {
    alert("You have entered an invalid postal code.");
    $('#' + p).focus();
}

function CheckDeclaraction(ctl, args) {
    try {
        var chkbox = document.getElementById(source.controltovalidate);
        args.IsValid = chkbox.checked;
    } catch (err) {
    }
    args.IsValid = false;
}

function ToggleSection(handleID, secionID) {
    if ($('#' + secionID).css('display') == 'block') {
        $('#' + secionID).hide('slow');
        $('#' + handleID).attr('src', '/Static/Images/cat-rightarrow.gif');
    } else {
        $('#' + secionID).show('slow');
        $('#' + handleID).attr('src', '/Static/Images/cat-downarrow.gif');
    }
}    