﻿function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function showControlRadChk(show_value, hide_value, control_Name, show_ID) {

    var controlArray = document.getElementsByName(control_Name);
    var show_control = document.getElementById(show_ID);

    for (i = 0; i < controlArray.length; i++) {
        if (controlArray[i].checked && controlArray[i].value == show_value) {
            show_control.style.display = "block";
        }
        else if (controlArray[i].checked && controlArray[i].value == hide_value) {
            show_control.style.display = "none";
        }
    }
}


function checkRadChk(controlID) {

    var args_str = false;

    var chkListModules = document.getElementById(controlID);
    var elements = chkListModules.getElementsByTagName('input');

    for (i = 0; i < elements.length; i++) {
        if (elements[i].checked) {
            args_str = true;
        }
    }

    return args_str;
}


function showControl(show_ID) {
    var show_control = document.getElementById(show_ID);
    show_control.style.display = "block";
}

function hideControl(show_ID) {
    var show_control = document.getElementById(show_ID);
    show_control.style.display = "none";
}

function enable_validation(valid_id) {
    var control = document.getElementById(valid_id)
    if (control) {
        ValidatorEnable(control, true);
        control.isvalid = true;
        ValidatorUpdateDisplay(control);
    }
}
function disable_validation(valid_id) {
    var control = document.getElementById(valid_id)
    if (control) {
        ValidatorEnable(control, false);
        control.isvalid = false;
        ValidatorUpdateDisplay(control);
    }
}
function showControlRadChk2(show_value, control_Name, show_ID, valid_str) {
    var controlArray = document.getElementsByName(control_Name);
    var show_control = document.getElementById(show_ID);
    var valid_str_val = '';
    if (control_Name.indexOf('dd_list') != 0) {
        for (i = 0; i < controlArray.length; i++) {
            //alert(controlArray[i].selected);
            if (controlArray[i].value == show_value) {
                show_control.style.display = "block";


                var fieldset = document.getElementById(show_ID);
                var elements = fieldset.getElementsByTagName('input');
                for (x = 0; x < elements.length; x++) {
                    if (elements[x].id.indexOf(valid_str) && elements[x].type == "hidden") {
                        valid_str_val = valid_str_val + ',' + elements[x].value
                    }
                }

                if (valid_str_val.indexOf(',') != -1) {
                    var valid_items = valid_str_val.split(',');
                    for (var num = 0; num < valid_items.length; num++) {
                        if (valid_items[num] != '') { enable_validation(valid_items[num]); }
                    }
                }
                else {
                    enable_validation(valid_str_val);
                }

            }
            else if (controlArray[i].value != show_value) {
                show_control.style.display = "none";

                var fieldset = document.getElementById(show_ID);
                var elements = fieldset.getElementsByTagName('input');
                for (x = 0; x < elements.length; x++) {
                    if (elements[x].id.indexOf(valid_str) && elements[x].type == "hidden") {
                        valid_str_val = valid_str_val + ',' + elements[x].value
                    }
                }

                if (valid_str_val.indexOf(',') != -1) {
                    var valid_items = valid_str_val.split(',');
                    for (var num = 0; num < valid_items.length; num++) {
                        if (valid_items[num] != '') { disable_validation(valid_items[num]); }
                    }
                }
                else {
                    disable_validation(valid_str_val);
                }

            }
        }

    }
    else {
        for (i = 0; i < controlArray.length; i++) {
            if (controlArray[i].checked && controlArray[i].value == show_value) {
                show_control.style.display = "block";


                var fieldset = document.getElementById(show_ID);
                var elements = fieldset.getElementsByTagName('input');
                for (x = 0; x < elements.length; x++) {
                    if (elements[x].id.indexOf(valid_str) && elements[x].type == "hidden") {
                        valid_str_val = valid_str_val + ',' + elements[x].value
                    }
                }

                if (valid_str_val.indexOf(',') != -1) {
                    var valid_items = valid_str_val.split(',');
                    for (var num = 0; num < valid_items.length; num++) {
                        if (valid_items[num] != '') { enable_validation(valid_items[num]); }
                    }
                }
                else {
                    enable_validation(valid_str_val);
                }

            }
            else if (controlArray[i].checked && controlArray[i].value != show_value) {
                show_control.style.display = "none";

                var fieldset = document.getElementById(show_ID);
                var elements = fieldset.getElementsByTagName('input');
                for (x = 0; x < elements.length; x++) {
                    if (elements[x].id.indexOf(valid_str) && elements[x].type == "hidden") {
                        valid_str_val = valid_str_val + ',' + elements[x].value
                    }
                }

                if (valid_str_val.indexOf(',') != -1) {
                    var valid_items = valid_str_val.split(',');
                    for (var num = 0; num < valid_items.length; num++) {
                        if (valid_items[num] != '') { disable_validation(valid_items[num]); }
                    }
                }
                else {
                    disable_validation(valid_str_val);
                }

            }
        }
    }
}

function show_imageDD(dd_value, a_id, img_id, img_w, img_h, wm, bg, folder) {
    if (dd_value.indexOf(",") != -1) {
        var split_dd = dd_value.split(",")
        for (i = 0; i < split_dd.length; i++) {
            var split_str = split_dd[i].split("^")[3]
            if (split_str != undefined && split_str != '' && split_str != 'Image URL') {
                var a_tag = document.getElementById(a_id);
                var img_tag = document.getElementById(img_id);
                img_tag.src = "Request.Image?img=uploads/" + folder + "/" + split_str + "&width=" + img_w + "&height=" + img_h + "&bg=" + bg + "&watermark=" + wm
                a_tag.href = "Request.Image?img=uploads/" + folder + "/" + split_str + "&watermark=" + wm
            }
        }
    }
    else {
        var split_str = dd_value.split("^")[3]
        //alert(dd_value + ' -' + split_str);
        if (split_str != undefined && split_str != '' && split_str != 'Image URL') {
            var a_tag = document.getElementById(a_id);
            var img_tag = document.getElementById(img_id);
            img_tag.src = "Request.Image?img=uploads/" + folder + "/" + split_str + "&width=" + img_w + "&height=" + img_h + "&bg=" + bg + "&watermark=" + wm
            a_tag.href = "Request.Image?img=uploads/" + folder + "/" + split_str + "&watermark=" + wm
        }
    }
}
