var iWebkit;

if (!iWebkit) {

    iWebkit = window.onload = function() {

        iWebkit.checkboxHeight = "25";
        iWebkit.radioHeight = "25";
        iWebkit.autolistNumVisible = 10;

        function url() {
            var a = document.getElementsByTagName("a");
            for (var i = 0; i < a.length; i++) {
                if (a[i].className.match("noeffect")) {
                }
                else {
                    a[i].onclick = function() {
                        window.location = this.getAttribute("href");
                        return false;
                    };
                }
            }
        }

        function hideURLbar() {
            window.scrollTo(0, 0.9);
        }

        iWebkit.popup = function() {
            window.scrollTo(0, 9999);
            var o_popup = document.getElementById(arguments[0]);
            var o_frame = o_popup.getElementsByClassName('confirm_screen')[0];
            o_frame.className = 'confirm_screenopen';
            var b = document.getElementById("cover");
            b.className = "cover";
            b.style.height = document.height + "px";
        };

        iWebkit.closepopup = function() {
            var o_parent = arguments[0].currentTarget;
            var b_found = false;
            do {
                o_parent = o_parent.parentNode;
                if (o_parent.tagName.toLowerCase() == 'div' && o_parent.className.toLowerCase() == 'popup') {
                    b_found = true;
                    break;
                }
            } while (o_parent.parentNode);

            if (b_found === false) {
                return false;
            }

            var o_frameclose = o_parent.getElementsByClassName('confirm_screenopen')[0];
            o_frameclose.className = 'confirm_screen';
            var b = document.getElementById("cover");
            b.className = "nocover";
            b.style.height = 0;
        };

        function initAutoLists() {

            var ul = document.getElementsByTagName('ul');
            for (var i = 0; i < ul.length; i++) {
                var list = ul[i];
                if (list.getAttribute('class').search(/(autolist)/) === -1) {
                    continue;
                }
                var items = list.getElementsByTagName('li');
                if (items.length <= iWebkit.autolistNumVisible) {
                    continue;
                }
                list.numitems = items.length;
                list.visibleitems = 0;
                var button = list.getElementsByClassName('autolisttext')[0];
                button.onclick = function(event) {
                    var list = this.parentNode;
                    list.showItems(list.visibleitems + iWebkit.autolistNumVisible);
                    return false;
                };
                list.showItems = function(numItems) {
                    var items = this.getElementsByTagName('li');
                    var count = 0;
                    for (var i = 0; i < items.length; i++) {
                        items[i].className = items[i].className.replace(/hidden/g, '');
                        if (i >= numItems) {
                            items[i].className = items[i].className + ' hidden';
                        }
                        else {
                            count += 1;
                        }
                    }
                    this.visibleitems = count;
                    button.className = button.className.replace(/hidden/g, '');
                    if (count >= (items.length - 1)) {
                        button.className = button.className + ' hidden';
                    }
                };
                list.showItems(iWebkit.autolistNumVisible);
            }
        }

        iWebkit.init = function() {
            url();
            hideURLbar();
            ////initAutoLists();
            var inputs = document.getElementsByTagName("input"), span = [], textnode, option, active;
            for (var a = 0; a < inputs.length; a++) {
                if (inputs[a].type === "checkbox" || inputs[a].type === "radio") {
                    span[a] = document.createElement("span");
                    span[a].className = inputs[a].type;
                    if (inputs[a].checked) {
                        if (inputs[a].type === "checkbox") {
                            var position = "0 -" + (iWebkit.checkboxHeight * 2) + "px";
                            span[a].style.backgroundPosition = position;
                        } else {
                            position = "0 -" + (iWebkit.radioHeight * 2) + "px";
                            span[a].style.backgroundPosition = position;
                        }
                    }
                    inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                    inputs[a].onchange = iWebkit.clear;
                    span[a].onmouseup = iWebkit.check;
                    document.onmouseup = iWebkit.clear;
                }
            }

        };

        iWebkit.pushed = function() {
            var element = this.nextSibling;
            if (element.checked && element.type === "checkbox") {
                this.style.backgroundPosition = "0 -" + iWebkit.checkboxHeight * 3 + "px";
            } else {
                if (element.checked && element.type === "radio") {
                    this.style.backgroundPosition = "0 -" + iWebkit.radioHeight * 3 + "px";
                } else {
                    if (!element.checked && element.type === "checkbox") {
                        this.style.backgroundPosition = "0 -" + iWebkit.checkboxHeight + "px";
                    } else {
                        this.style.backgroundPosition = "0 -" + iWebkit.radioHeight + "px";
                    }
                }
            }
        };

        iWebkit.check = function() {
            var element = this.nextSibling;
            if (element.checked && element.type === "checkbox") {
                this.style.backgroundPosition = "0 0";
                element.checked = false;
            } else {
                if (element.type === "checkbox") {
                    this.style.backgroundPosition = "0 -" + iWebkit.checkboxHeight * 2 + "px";
                } else {
                    this.style.backgroundPosition = "0 -" + iWebkit.radioHeight * 2 + "px";
                    var group = this.nextSibling.name;
                    var inputs = document.getElementsByTagName("input");
                    for (var a = 0; a < inputs.length; a++) {
                        if (inputs[a].name === group && inputs[a] !== this.nextSibling) {
                            inputs[a].previousSibling.style.backgroundPosition = "0 0";
                        }
                    }
                }
                element.checked = true;
            }
        };

        iWebkit.clear = function() {
            var inputs = document.getElementsByTagName("input");
            for (var b = 0; b < inputs.length; b++) {
                if (inputs[b].type === "checkbox" && inputs[b].checked) {
                    inputs[b].previousSibling.style.backgroundPosition = "0 -" + iWebkit.checkboxHeight * 2 + "px";
                } else {
                    if (inputs[b].type === "checkbox") {
                        inputs[b].previousSibling.style.backgroundPosition = "0 0";
                    }
                    else {
                        if (inputs[b].type === "radio" && inputs[b].checked) {
                            inputs[b].previousSibling.style.backgroundPosition = "0 -" + iWebkit.radioHeight * 2 + "px";
                        } else {
                            if (inputs[b].type === "radio") {
                                inputs[b].previousSibling.style.backgroundPosition = "0 0";
                            }
                        }
                    }
                }
            }
        };

        iWebkit.choose = function() {
            var option = this.getElementsByTagName("option");
            for (var d = 0; d < option.length; d++) {
                if (option[d].selected) {
                    document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
                }
            }
        };

        iWebkit.init();
    };
}


//////calendar js functions
var ctrlId;
function openCalander(txtId) {
    ////window.open("popupCal.html?ctrlId=" + txtId, "calPopup", "status=1,menubar=0,resizable=0,width=350,height=300");

    var ctntDiv = document.getElementById('editContent');
    var popDiv = document.getElementById('popupContent');
    ctntDiv.style.display = 'none';
    popDiv.style.display = 'block';
    ctrlId = txtId;
}

function closeCalendar() {
    ////window.open("popupCal.html?ctrlId=" + txtId, "calPopup", "status=1,menubar=0,resizable=0,width=350,height=300");

    var ctntDiv = document.getElementById('editContent');
    var popDiv = document.getElementById('popupContent');
    ctntDiv.style.display = 'block';
    popDiv.style.display = 'none';
}

function closeCalendar(dt) {
    ////window.open("popupCal.html?ctrlId=" + txtId, "calPopup", "status=1,menubar=0,resizable=0,width=350,height=300");

    var ctntDiv = document.getElementById('editContent');
    var popDiv = document.getElementById('popupContent');
    ctntDiv.style.display = 'block';
    popDiv.style.display = 'none';
    if (dt != null) {
        var ctrl = document.getElementById(ctrlId);
        ctrl.value = dt;
    }
}

////////////////

function fillEndDate(startId, endId) {
    document.getElementById(endId).value = document.getElementById(startId).value;
}////
function fillEndTime(startTimeId, endTimeId, startAMPMId, endAMPMId) {
    var startTimeDDL = document.getElementById(startTimeId);
    var endTimeDDL = document.getElementById(endTimeId);
    var startAMPMDDL = document.getElementById(startAMPMId);
    var endAMPMDDL = document.getElementById(endAMPMId);
    if (startTimeDDL.length - startTimeDDL.selectedIndex == 3) {
        if (startAMPMDDL.selectedIndex == 0)
            endAMPMDDL.selectedIndex = 1;
        else
            endAMPMDDL.selectedIndex = 0;
        endTimeDDL.selectedIndex = startTimeDDL.selectedIndex + 1;
    }
    else {
        endAMPMDDL.selectedIndex = startAMPMDDL.selectedIndex;
    }
    if (startTimeDDL.length - startTimeDDL.selectedIndex == 1)
        endTimeDDL.selectedIndex = 0;
    else
        endTimeDDL.selectedIndex = startTimeDDL.selectedIndex + 1;
}

function fillEndAMPM(startId, endId) {
    var startDDL = document.getElementById(startId);
    var endDDL = document.getElementById(endId);
    endDDL.selectedIndex = startDDL.selectedIndex;
}
