/* VSETKY JAVASCRIPTY SPOJENE S SUBWEBSITES */

function modalInfoVisits()
{
    var dialog_buttons = {};
    dialog_buttons[Ajax.JS_subwebsite_modal_send] = function(){ajax_sendMessageInfoVisits();}
    dialog_buttons[Ajax.JS_subwebsite_modal_cancel] = function(){$(this).dialog('close');
}

    $("#modalInfoVisits").dialog({
        modal:true,
        draggable: false,
        resizable: false,
        minHeight: 0,
        width: 400,
        height: 'auto',
        title: Ajax.Master_Emails,
        buttons: dialog_buttons
    });
} 


function ajax_sendMessageInfoVisits() {
    $('.ui-dialog-buttonpane button').attr('disabled', 'disabled');
    var posli1 = $("#posli1").val();
    var posli2 = $("#posli2").val();
    var posli3 = $("#posli3").val();
    var posli4 = $("#posli4").val();
    var posli5 = $("#posli5").val();
    $.getJSON("unifile.php?fti=ajax_emails", {
        posli1:posli1,
        posli2:posli2,
        posli3:posli3,
        posli4:posli4,
        posli5:posli5
    }, function(json) {
        callback_sendMessageInfoVisits(json);
    });
}

function callback_sendMessageInfoVisits(json) {
    if(json == null || json == "undefined") {
        alert("Error sending visitors message");
        return;
    }

    if(json.emailSend == true) {
        $("<div class='notice_ok_noborder_min'>" + json.emailSendText +"</div>").appendTo("#displaymodalInfoVisits");
        $("#displaymodalInfoVisits > div").delay(4000).fadeOut(500, function() {$(this).remove();});

        for(i=1; i<=5; i++) {
            $("#posli"+i).val("");
            $("#posli"+i).css({"color" : "black","font-weight" : "normal"});
        }  
    }
    else{
        var badEmails = "";
        var numberEmails = "";
        if(json.wrongEmails != null){
            for(i=1; i<=5; i++) {
                $("#posli"+i).css({"color" : "black","font-weight" : "normal"});
            }
            for (var i=0; i<json.wrongEmails.length;i++)
            {
                badEmails += '' + json.wrongEmails[i];
                numberEmails = '' + json.wrongEmails[i];
                $("#posli"+numberEmails).css({"color" : "red","font-weight" : "bold"});

                if(i != json.wrongEmails.length - 1)
                   badEmails += ', ';              
            }
        
            $("<div class='notice_warning_noborder_min'>" + Ajax.JS_tooltip_invalidemail +" (" + badEmails +") </div>").appendTo("#displaymodalInfoVisits");
            $("#displaymodalInfoVisits > div").delay(4000).fadeOut(500, function() {$(this).remove();});
        }

        if(json.emailNotSendText != null && json.emailNotSendText != "undefined") {
            $("<div class='notice_warning_noborder_min'>" + json.emailNotSendText +"</div>").appendTo("#displaymodalInfoVisits");
            $("#displaymodalInfoVisits > div").delay(4000).fadeOut(500, function() {$(this).remove();});
        }

         if(json.notAnyEmails != null && json.notAnyEmails != "undefined") {
            $("<div class='notice_warning_noborder_min'>" + json.notAnyEmails +"</div>").appendTo("#displaymodalInfoVisits");
            $("#displaymodalInfoVisits > div").delay(4000).fadeOut(500, function() {$(this).remove();});
         }
    }

    $("#displaymodalInfoVisits").fadeIn(600);
    $('.ui-dialog-buttonpane button').removeAttr('disabled');  
}


function modalInfoNotification()
{
    var dialog_buttons = {};
    dialog_buttons[Ajax.JS_subwebsite_modal_send] = function(){ajax_sendMessageInfoNotifications();}
    dialog_buttons[Ajax.JS_subwebsite_modal_cancel] = function(){$(this).dialog('close');}

    $("#modalInfoNotification").dialog({
        modal:true,
        draggable: false,
        resizable: false,
        minHeight: 0,
        width: 400,
        height: 'auto',
        title: Ajax.Master_Register,
        buttons: dialog_buttons
    });
}

function ajax_sendMessageInfoNotifications() {
    $('.ui-dialog-buttonpane button').attr('disabled', 'disabled');
    var email = $("#emailNotification").val();
    var unscribe = 0;
    if($('#unscribe').attr('checked'))
        unscribe = 1;
    else
       unscribe = 0;

    $.getJSON("unifile.php?fti=ajax_visitorsemails", {email:email, unscribe:unscribe}, function(json) {callback_sendMessageInfoNotifications(json);});
}

function callback_sendMessageInfoNotifications(json) {
    if(json == null || json == "undefined") {
        alert("Error sending Notification message");
        return;
    }

    if(json.emailSend == true) {
        $("<div class='notice_ok_noborder_min'>" + json.emailSendText +"</div>").appendTo("#displaymodalInfoNotification");
        $("#displaymodalInfoNotification > div").delay(4000).fadeOut(500, function() {$(this).remove();});
    }
    else{
        $("<div class='notice_warning_noborder_min'>" + json.emailSendText +"</div>").appendTo("#displaymodalInfoNotification");
        $("#displaymodalInfoNotification > div").delay(4000).fadeOut(500, function() {$(this).remove();});
    }

    $("#displaymodalInfoNotification").fadeIn(600);
    $('.ui-dialog-buttonpane button').removeAttr('disabled'); 
}












/* AJAXY PRE SUBWEBSITES */

var dif = 0;
var diff = 0;

/*******************************/
/** AJAX na vytvorenie albumu **/
/*******************************/
function ajax_createAlbum(input, userid) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var albumName = urlencode(input.value);
    if(albumName == '') {
        input.focus();
        alert(Ajax.JS_Ajax_album_notempty);
        return false;
    }
    var intoalopts = document.getElementById('selImageAlbum');
    for(var i=0; i < intoalopts.options.length; i++) {
        var re = new RegExp("^\\s*("+intoalopts.options[i].text+")?\\s*$", "i");
        if(albumName.match(re)) {
            input.focus();
            alert(Ajax.JS_Ajax_album_exists);
            return false;
        }
    }
    if(!albumName.match(/^[\w\-_ \'\.(),#!?&%]+$/)) {
        alert(Ajax.JS_Ajax_album_incorrect + " " + albumName );
        input.focus();
        return false;
    }
    var url = "unifile.php?fti=ajax_createalbum&aname="+albumName+"&uid="+userid+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=createAlbum_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function createAlbum_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        //alert(xmlHttp.responseText);
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(Ajax.JS_Ajax_album_notcreated);
        } else {
            var opts = xmlHttp.responseText.split(";");
            var intosel = document.getElementById('selImageAlbum');
            intosel.options.length = 0;
            for(str in opts) {
                var opt = opts[str].split(":");
                var o = new Option(opt[1], opt[0]);
                intosel.options[intosel.options.length] = o;
                if(opt[1] == document.getElementById('galeryalbumname').value)
                    intosel.selectedIndex = intosel.options.length -1 ;
            }
            //document.getElementById('selImageAlbum').innerHTML = xmlHttp.responseText;
            alert(Ajax.JS_Ajax_album_created);
        }
        document.getElementById('galeryalbumname').value = "";
    }
}


/***************************************/
/** AJAX na vytvorenie custom webpage **/
/***************************************/
function createCustomWebpage(wpname, userid) {
    if(!wpname.match(/\S+/) || wpname.length > 40) {
        document.getElementById('customwebpage').focus();
        alert(Ajax.JS_Ajax_page_empty);
        return false;
    }
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_createwebpage&wpname="+urlencode(wpname)+"&uid="+userid+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=createWebpage_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function createWebpage_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            document.getElementById("customwebpage").value="";
            var wpid = xmlHttp.responseText.substr(0, xmlHttp.responseText.indexOf("|"));
            document.getElementById("webpageorder").innerHTML = xmlHttp.responseText.substr(xmlHttp.responseText.indexOf("|")+1);
            if(confirm(Ajax.JS_Ajax_page_editpage ))
                document.location = "index.php?co=editpage&editpage="+wpid;
        }
    }
}


/*************************************/
/** AJAX na zmazanie custom webpage **/
/*************************************/
function deleteCustomWebpage(wpid, zid) {
    if(!confirm(Ajax.JS_Ajax_page_delete))
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletewebpage&wpid="+wpid+"&zid="+zid+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=deleteWebpage_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function deleteWebpage_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            document.getElementById("webpageorder").innerHTML = xmlHttp.responseText;
        }
    }
}


/***************************************/
/** AJAX na presunutie custom webpage **/
/***************************************/
function moveCustomWebpage(wpid, zid, smer) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_movewebpage&wpid="+wpid+"&zid="+zid+"&smer="+smer+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=moveWebpage_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function moveWebpage_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            document.getElementById("webpageorder").innerHTML = xmlHttp.responseText;
        }
    }
}

/***************************************/
/** AJAX na vytvorenie custom style **/
/***************************************/
function createCustomStyle(csname, userid) {
    if(!csname.match(/\S+/) || csname.length > 40) {
        document.getElementById('customstyle').focus();
        alert(Ajax.JS_Ajax_style_incorrect);
        return false;
    }
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_createcustomstyle&csname="+urlencode(csname)+"&uid="+userid+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=createCustomstyle_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function createCustomstyle_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            document.getElementById("customstyle").value="";
            var csid = xmlHttp.responseText.substr(0, xmlHttp.responseText.indexOf("|"));
            document.getElementById("customstyles").innerHTML = xmlHttp.responseText.substr(xmlHttp.responseText.indexOf("|")+1);
            if(confirm(Ajax.JS_Ajax_style_edit))
                document.location = "index.php?co=editstyle&csid="+csid;
        }
    }
}

/***********************************/
/** AJAX na zmazanie custom style **/
/***********************************/
var customStyleDeleteID = null;
function deleteCustomStyle(csid, zid) {
    if(!confirm(Ajax.JS_Ajax_style_delete))
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletecustomstyle&csid="+csid+"&zid="+zid+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=deleteCustomStyle_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    customStyleDeleteID = csid;
}

function deleteCustomStyle_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            document.getElementById("customstyles").innerHTML = xmlHttp.responseText;
            var styleSelect = document.getElementById("ownstyle");
            for(var i =0; i< styleSelect.options.length; i++)
                if(styleSelect.options[i].value == "unifile.php?fti=customstyle-user2|"+customStyleDeleteID) {
                    styleSelect.options[i] = null;
                    break;
                }
            customStyleDeleteID = null;
        }
    }
}



/***************************************/
/** AJAX na poslanie mailov visitorom **/
/***************************************/
function ajax_sendemailsvisitors() {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var section = $id("updatesection").value;
    var url="unifile.php?fti=ajax_sendemailvisitor&section="+section+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=sendemailsvisitors_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function sendemailsvisitors_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));	// ak sa nieco posralo, tak vypiseme error, inak nic
        } else {
            alert(xmlHttp.responseText)
        }
    }
}

/***************************************/
/** AJAX na zmazanie baneru **/
/***************************************/
function ajax_deleteBannerImage(filename, style_id, property) {
    if(filename=="")
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletebannerimage&f="+filename+"&csid="+style_id+"&p="+property;
    dif++;
    xmlHttp.onreadystatechange=deleteBannerImage_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function deleteBannerImage_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));	// ak sa nieco posralo, tak vypiseme error, inak nic
        } else {
            var data = xmlHttp.responseText.split("|");
            for(var i=0; i < document.getElementById("selex"+data[1]).options.length; i++) {
                if(document.getElementById("selex"+data[1]).options[i].value == data[0]) {
                    document.getElementById("selex"+data[1]).options[i] = null;
                    document.getElementById("selex"+data[1]).selectedIndex = 0;
                    document.getElementById("preview"+data[1]).style.display = "none";
                    break;
                }
            }
        }
    }
}

/***********************************/
/** AJAX generovanie ajax objektu **/
/***********************************/
function GetXmlHttpObject(handler) { 
    var objXMLHttp=null;
    if (window.XMLHttpRequest) {
        objXMLHttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}

/***************************************/
/** AJAX na zmazanie pesnicky z disku **/
/***************************************/
function deleteSongFromDisk(song) {
    if(!confirm(Ajax.JS_Ajax_song_delete))
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletesong&song="+urlencode(song)+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=deleteSongFromDisk_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function deleteSongFromDisk_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            var mp3box = document.getElementById("music");
            removeMp3FromList();
        }
    }
}


/********************************************/
/** AJAX na zmazanie fallingobject z disku **/
/********************************************/
// FALLING OBJECT
function deleteFallingObject(uid) {
    var foselect = document.getElementById('fobjects');
    if(foselect == "undefined" || foselect == null)
        return;
    if(!confirm(Ajax.JS_Ajax_falling_delete))
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletefalling&uid="+uid+"&fo="+urlencode(foselect.value)+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=deleteFallingObject_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function deleteFallingObject_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            var foselect = document.getElementById('fobjects');
            if(foselect == "undefined" || foselect == null)
                return;
            var usedValue = document.getElementById("selectedFalling").value;
            var children = foselect.childNodes[2].childNodes;
            for(var i=0; i<children.length; i++) {
                var child = children[i];
                if(child.selected)
                    foselect.childNodes[2].removeChild(child);
            }
            foselect.value = usedValue;
            generateimage(foselect.options[foselect.selectedIndex].id);
        }
    }
}

/***********************************************/
/** AJAX na zmazanie background image z disku **/
/***********************************************/
// Background image
function deleteBackgroundImage(uid) {
    var biselect = document.getElementById('setbackground');
    if(biselect == "undefined" || biselect == null)
        return;
    if(!confirm(Ajax.JS_Ajax_back_delete))
        return;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="unifile.php?fti=ajax_deletebackground&uid="+uid+"&bi="+urlencode(biselect.value)+"&test="+dif;
    dif++;
    xmlHttp.onreadystatechange=deleteBackgroundImage_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function deleteBackgroundImage_state() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        if(xmlHttp.responseText.indexOf("ERROR")>-1) {
            alert(xmlHttp.responseText.substr(6));
        } else {
            var biselect = document.getElementById('setbackground');
            if(biselect == "undefined" || biselect == null)
                return;
            var usedValue = document.getElementById("selectedBackground").value;
            var children = biselect.childNodes;
            for(var i=0; i<children.length; i++) {
                var child = children[i];
                if(child.selected)
                    biselect.removeChild(child);
            }
            biselect.value = usedValue;
        }
    }
}


