$(document).ready(function() {
    $("a.fancybox").fancybox({overlayOpacity: 0.8, overlayColor: "#000"});
    prepairTextEditors();
    inputChangeHandler();
    prepairCaptcha();
    prepairConfirmAnchorTags();
    prepairConfirmSubmitInputs();
    getContinue();
});
function getBaseUrl() {
    var base = null;
    if (document.getElementsByTagName) {
        var arr = document.getElementsByTagName("base");
        if (arr.length)
            base = arr[0].href;
    }
    return base;
}
function getContinue() {
    var request = null;
    if (window.ActiveXObject) {
        try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) { request = new ActiveXObject("Microsoft.XMLHTTP"); }
    }
    else if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    }
    if (request != null) {
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                var response = request.responseText;
                var anchors = document.getElementsByTagName("a");
                for (var i = 0; i < anchors.length; i++) {
                    if (anchors[i].className.indexOf("anchorCloseBtn") != -1) {
                        var href = anchors[i].href;
                        href = (href.indexOf("?") == 0) ? href.substr(0, href.indexOf("=")) : "";
                        anchors[i].href = href + response;
                    }
                }
            }
        }
        var string = getBaseUrl() + "ext/windowname.php?cmd=get&wid=" + getWindowName();
        request.open("GET", string, true);
        request.send(null);
    }
}
function getWindowName() {
    if (self.window.name == "")
        self.window.name = Math.floor(Math.random() * 10000);
    return self.window.name;
}
function inputChangeHandler() {
    var forms = document.getElementsByTagName("form");
    for (var i = 0; i < forms.length; i++) {
        var inputs = forms[i].getElementsByTagName("input");
        for (var j = 0; j < inputs.length; j++) {
            if (inputs[j].type == "text" && inputs[j].className.indexOf("change") != -1) {
                inputs[j].onfocus = function() {
                    if (this.value == this.defaultValue) this.value = "";
                }
                inputs[j].onblur = function() {
                    if (this.value == "") this.value = this.defaultValue;
                }
            }
            else if (inputs[j].type == "password" && inputs[j].className.indexOf("change") != -1) {
                replaceInput(inputs[j], "text", inputs[j].value, inputs[j].value, false);
            }
        }
    }
    function replaceInput(element, type, value, title, focus) {
        var newElement = document.createElement("input");
        newElement.setAttribute("id", element.id);
        newElement.setAttribute("type", type);
        newElement.setAttribute("name", element.name);
        newElement.setAttribute("value", value);
        newElement.setAttribute("class", element.className);
        newElement.setAttribute("title", title);
        element.parentNode.replaceChild(newElement, element);

        if (focus) {
            setTimeout(function() {newElement.focus();}, 1000);
        }
        newElement.onfocus = function() {
            if (this.value == this.title) replaceInput(this, "password", "", this.title, true);
        }
        newElement.onblur = function() {
            if (this.value == "") replaceInput(this, "text", this.title, this.title, false);
        }
    }
}
function prepairCaptcha() {
    var captcha = document.getElementById("captchaAnchor");
    if (captcha != null) {
        captcha.href = "javascript:void(0)";
        captcha.onclick = function() {
            var img = document.getElementById("captchaImage");
            img.src = img.src + "#";
        }
    }
}
function prepairConfirmAnchorTags() {
    var arr = document.getElementsByTagName("a");
    var hrefs = new Array();
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].className.indexOf("confirm") != -1) {
            hrefs[i] = arr[i].href;
            arr[i].href = "javascript:void(0)";
            arr[i].onclick = function() {
                var i = 0;
                while (i < arr.length && arr[i] != this)
                    i++;
                if (window.confirm("Biztosan végrehajtja a műveletet?")) location.href = hrefs[i];
            }
        }
    }
}
function prepairConfirmSubmitInputs() {
    var forms = document.getElementsByTagName("form");
    for (var i = 0; i < forms.length; i++) {
        var inputs = forms[i].getElementsByTagName("input");
        for (var j = 0; j < inputs.length; j++) {
            if (inputs[j].type == "submit" && inputs[j].className.indexOf("confirm") != -1) {
                inputs[j].type = "button";
                inputs[j].onclick = function() {
                    if (window.confirm("Biztosan végrehajtja a műveletet?")) this.form.submit();
                }
            }
        }
    }
}
function prepairTextEditors() {
    var textareas = document.getElementsByTagName("textarea");
    for (var i = 0; i < textareas.length; i++) {
        if (textareas[i].className.indexOf("WYSIWYG") != -1) {
            var editor = new FCKeditor(textareas[i].name);
            editor.BasePath = "js/fckeditor/";
            editor.Height = "500px";
            editor.ReplaceTextarea();
        }
    }
}
function setBackUrl(string) {
    var request = null;
    if (window.ActiveXObject) {
        try {request = new ActiveXObject("Msxml2.XMLHTTP");}
        catch (e) {request = new ActiveXObject("Microsoft.XMLHTTP");}
    }
    else if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    }
    if (request != null) {
        string = getBaseUrl() + "ext/windowname.php?cmd=set&wid=" + getWindowName() + "&loc=" + string;
        request.open("GET", string, true);
        request.send(null);
    }
}
function stringSubtract(str1, str2) {
    var pos = str1.indexOf(str2);
    if (pos == -1)
        return str1;
    return (str1.substr(0, pos) + str1.substr(pos + str2.length));
}
