// JavaScript Document
var dif = 0;
var diff = 0;
/********************************/
/** AJAX na zmenu podkategorii **/
/********************************/
var albumAct = -1;
function getAlbumPict(albid, userid, selAlbum) {
	saved = true;
	if(albid==-1 || userid == null)
		return;
	saveImageOrder()
	
	if(document.getElementById('imageIdList') != null)
		saveAlbumOrder(albid,userid,document.getElementById('imageIdList').value);
	/* POD FF TO NEFUNGOVALO, lebo stale zobrazovalo hlasku, lebo to nevoslo do funkcie saveAlbumOrder_state z nejakej priciny
	if(!saveAlbumOrder(albid,userid,document.getElementById('imageIdList').value)) {
		if(!confirm("You have to save your changes in photo order before switching to other album,\notherwise your changes will be lost.\n\nDo you want to switch anyway?")) {
			if(albumAct!=-1)
				selAlbum.selectedIndex = albumAct;
			return;
		}
	}
	*/
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	//setTimeout( window.location.href ="unifile.php?fti=ajax_getalbum&aid="+albid+"&uid="+userid,0);
	
	var url="unifile.php?fti=ajax_getalbum&aid="+albid+"&uid="+userid+"&test="+diff;
	diff++;
	xmlHttp.onreadystatechange=getAlbumPict_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getAlbumPict_state() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById("alb_order").innerHTML = xmlHttp.responseText;
		initGallery();
	}
}


/************************************/
/** AJAX na zmenu poradia obrazkov **/
/************************************/
function saveAlbumOrder(albid, userid, alborder) {
	if(albid==-1 || userid == null)
		return;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	

	var url="unifile.php?fti=ajax_savealbumorder&aid="+albid+"&uid="+userid+"&albord="+alborder+"&test="+dif;
	dif++;
	saved = false;
	xmlHttp.onreadystatechange=saveAlbumOrder_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	return saved; // toto by sa malo zmenit na true vo funkcii saveAlbumOrder_state(), ak prebehol ajax uspesne
}

var saved = true;
function saveAlbumOrder_state() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		if(xmlHttp.responseText.indexOf("OK")>-1)
			saved = true;
		else saved = false;
	} else saved = false;
}


/*******************************/
/** AJAX na vytvorenie albumu **/
/*******************************/
function ajax_createAlbum(input, userid) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var albumName = input.value;
	if(albumName == '') {
		input.focus();
		alert("Album name can not be empty.");
		return false;
	}
	var intoalopts = document.getElementById('selImageAlbum').options;
	for(var op in intoalopts) {
		var re = new RegExp("^\s*("+intoalopts[op]+")?\s*$", "i");
		if(albumName.match(re)) {
			input.focus();
			alert("Album name '"+albumName+"' already exists, please choose different name.");
			return false;
		}
	}
	if(!albumName.match(/^[\w\-_ \'.(),#!?&]+$/)) {
		alert("Incorrect album name, please change it.");
		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('Album has not been created, please try to change it\'s name.');
		} 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("Album has been successfully 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("Page name can not be empty or longer then 40 characters!");
		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("Would you like to edit new page now?"))
        document.location = "index.php?co=editpage&editpage="+wpid;
		}
	}
}


/*************************************/
/** AJAX na zmazanie custom webpage **/
/*************************************/
function deleteCustomWebpage(wpid, zid) {
	if(!confirm("Are you sure you want to delete this Page (It cannot be restored after removed)?"))
		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("Custom style's name can not be empty or longer then 40 characters!");
		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("Would you like to edit new style?"))
				document.location = "index.php?co=editstyle&csid="+csid;
		}
	}
}

/***********************************/
/** AJAX na zmazanie custom style **/
/***********************************/
var customStyleDeleteID = null;
function deleteCustomStyle(csid, zid) {
	if(!confirm("Are you sure you want to delete this Style (It cannot be restored after removed)?"))
		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 poslanie mailu po kliknuti na create na register_inside **/
/**************************************************/
function ajax_sendemailregister() {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var odkaz = document.getElementById('odkaz').value;
	var url="inside/ajax_sendemailregister.php?odkaz="+odkaz+"&test="+dif;
	dif++;
	xmlHttp.onreadystatechange=sendemailregister_state; // funkcia ktora urci, ze co sa ma robit s vysledkom vratenym
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function sendemailregister_state() {
}
/***************************************/
/** 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("Are you sure you want to delete this Song from website (It cannot be restored after removed)?"))
		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("Are you sure you want to delete this falling object from website (It cannot be restored after removed)?"))
		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("Are you sure you want to delete this background image from website (It cannot be restored after removed)?"))
		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;
		}
	}
}
