var intLastID = 0;
var intLastCID = 0;

//Function to change page from option list
function linkURL(URL) {
	
	if (URL.options[URL.selectedIndex].value != "") self.location.href = URL.options[URL.selectedIndex].value;	
	return true;
}

//Function to open pop up window
function winOpener(theURL, winName, scrollbars, resizable, width, height) {
	
	winFeatures = 'left=' + (screen.availWidth-10-width)/2 + ',top=' + (screen.availHeight-30-height)/2 + ',scrollbars=' + scrollbars + ',resizable=' + resizable + ',width=' + width + ',height=' + height + ',toolbar=0,location=0,status=1,menubar=0'
  	window.open(theURL, winName, winFeatures);
}

//sitesearch
function SiteSearch(){ 
	document.S.submit();
}

//XML
var strOtherXML = '';
var intID = 0;
var intTimeOut;
var strReply;
var callInProgress = false;
function showXml(strReplyID, strMode, intID, strString){ 
	if(callInProgress){
		if(strOtherXML!='')
			strOtherXML += "|";
		strOtherXML += "showXml('" + strReplyID + "', '" + strMode + "', " + intID + ", '" + strString + "');";
		return false;
	}
	strReply = strReplyID;
	document.getElementById(strReply).innerHTML='<div style="padding:10px;text-align:center"><img src="img/cm/loading.gif"></div>';
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		return true;
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",'ajax?M=' + strMode + '&ID=' + intID + '&str=' + strString, true);
	xmlHttp.send(null);
	intTimeOut = window.setTimeout(function(){
		if(callInProgress){
			xmlHttp.abort();
			callInProgress = false;
			document.getElementById(strReply).innerHTML='Time-out na AJAX pozivu.';
		}
	}, 10000);
	return false;
}

function sendXml(strReplyID, strMode, intID, strString){ 
	if(callInProgress){
		if(strOtherXML!='')
			strOtherXML += "|";
		strOtherXML += "sendXml('" + strReplyID + "', '" + strMode + "', " + intID + ", '" + strString + "');";
		return false;
	}
	strReply = strReplyID;
	document.getElementById(strReply).innerHTML='<div style="padding:10px;text-align:center"><img src="img/cm/loading.gif"></div>';
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		return true;
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",'ajax?M=' + strMode + '&ID=' + intID + '&str=edit', true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", strString.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strString);
	return false;
}

function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		document.getElementById(strReply).innerHTML=xmlHttp.responseText;
		window.clearTimeout(intTimeOut);
		callInProgress = false;
		nextRequest();
 	}
	else
		callInProgress = true;
}

function nextRequest(){ 
	if(strOtherXML!=''){ 
		var strNextFunction = '';

		arrayOtherXML = strOtherXML.split("|");
		strOtherXML = '';

		for(var intCount = 0; intCount < arrayOtherXML.length; intCount++){
			if(strNextFunction==''){
				strNextFunction = arrayOtherXML[intCount];
			}
			else{
				if(strOtherXML=='')
					strOtherXML = arrayOtherXML[intCount];
				else
					strOtherXML = strOtherXML + '|' + arrayOtherXML[intCount];
			}
		} 
		window.setTimeout(strNextFunction, 1);
 	}
}


function GetXmlHttpObject(){
	var xmlHttp = null;
	try{ xmlHttp = new ActiveXObject("MSXML3.XMLHTTP") }catch(e){
		try{ xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0") }catch(e){
			try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") }catch(e){
				try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") }catch(e){
					try{ xmlHttp = new XMLHttpRequest() }catch(e){
						return null;
					}
				}
			}
		}
	}
	return xmlHttp;
}

//email protection decoding...
var letters = 'NVpr/ :OPRSL39._5XYZQM>FGHnoJklDEst="<mabcde-@TUK4fghiuv678xIWwyz012ABCj';
var split = letters.split("");
var num = '';
var countEM = '';
var encrypted = '';
var sendCode = '';
function deCodeEmial(it, token){
	var bcounEM = '0';
	var chars = it.split("");
	while(bcounEM < it.length){
		countEM = '0';
		while(countEM < letters.length){
			if(split[countEM] == chars[bcounEM]){
				if(countEM == "0"){countEM = "";}
				if(eval(countEM-token) < 0){num = eval(letters.length-(token-countEM));encrypted += split[num];}
				else{num = eval(countEM-token);encrypted += split[num];}
			}countEM++;
		}bcounEM++;
	}
	sendCode = encrypted;
	encrypted = '';
	return sendCode;
}


// Function to add the code for bold italic and underline, to the message
function AddMessageCode(code, promptText, elementID) {
	var insertCode;
	if (code != "") {
		insertCode = prompt(promptText + "\n<" + code + ">xxx</" + code + ">", "");
			if ((insertCode != null) && (insertCode != "")){
				document.getElementById(elementID).value += "<" + code + ">" + insertCode + "</"+ code + ">";
			}
	}		
	document.getElementById(elementID).focus();
}

//show game details...
function Game(strGame){
	if(top.location.href != 'http://www.udruga-ping.hr/' && top.location.href != 'http://www.udruga-ping.hr/index')
		self.location.href = 'index?game' + strGame;
	else{
		document.getElementById("Title").innerHTML="Game server";
		showXml('ping', 'm_game', 0, strGame);
	}
}

//check
function check(strName){
	var blnSetOK;
	blnSetOK = false
	if(document.getElementById(strName).value.length!=0)
		blnSetOK = true;
	else
		if(document.getElementById(strName).options)
			if(document.getElementById(strName).options[document.getElementById(strName).selectedIndex].value.length!=0)
				blnSetOK = true;
	if(blnSetOK==true)
		document.getElementById('I' + strName).src = 'img/cm/OK.png';
	else
		document.getElementById('I' + strName).src = 'img/cm/X.png';
}
//checkForm
function checkForm(strNames, blnAlert){
	arayNames = strNames.split(",");
	for(var intCount = 0; intCount < arayNames.length; intCount++){
		if(blnAlert==1){
			if(document.getElementById(arayNames[intCount]).value.length==0){
				alert('Jedno ili vise obaveznih polja je prazno.');
				document.getElementById(arayNames[intCount]).focus();
				return false;
			}
		}
		else
			check(arayNames[intCount]);
	}
	return true;
}

//page scrool
function pageScroll(intDestination){
	var intDifrence = 0;
	var intUserTop = 0;
	if(typeof(window.pageYOffset) == 'number')
		intUserTop = window.pageYOffset;
	else if(document.body && document.body.scrollTop)
		intUserTop = document.body.scrollTop;
	else if(document.documentElement && document.documentElement.scrollTop)
		intUserTop = document.documentElement.scrollTop;
	//calculate offset and go up
	intDifrence = intUserTop - intDestination;
	if(intDifrence > 5){
		window.scrollBy(0,-(intDifrence / 2));
		setTimeout('pageScroll(' + intDestination + ');',30);
	}
}

//short functions for xml
function sNews(ID){ 
	showXml('newsID' + ID, 'N', ID, '');
}
function hNews(ID){ 
	document.getElementById('newsID' + ID).innerHTML="";
}

//show search user..
function showHint(strUsername, intAPID){
	if(strUsername.length >= 2)
		showXml('SHusername', 'SHun', intAPID, encodeURI(strUsername));
	else
		document.getElementById('SHusername').innerHTML = '';
}

function comSave(strReplyID, strMode, intID){
	var arrayFormV = document.getElementById("frmCom" + intID);
	var strFormV = '';
	for (var intCount=0; intCount < arrayFormV.length; intCount++){
		if(strFormV != '')
			strFormV += '&';
		strFormV += arrayFormV.elements[intCount].name + '=' + arrayFormV.elements[intCount].value;
	}
	return sendXml(strReplyID, strMode, intID, strFormV);
}

function comDel(strReplyID, strMode, intID){
	if(confirm('Sigurno žetite izbrisati ovaj upis?'))
		return showXml(strReplyID, strMode, intID, 'del');
	else
		return false;
}

