function otvori(nesto){
	var obj = nesto;
	var state = document.getElementById(obj).style.display;
	if(state == "block"){
		document.getElementById(obj).style.display="none";
	} else {
		document.getElementById(obj).style.display="block";
	}
	return;
}

function zatvori(nesto){
	var obj = nesto;
	document.getElementById(obj).style.display="none";
	return;
}
function otvorijednom(nesto) {
	var obj = nesto;
	document.getElementById(obj).style.display="block";
	return;
}


function ajaxRequest(){
	var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
	if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		for (var i=0; i<activexmodes.length; i++){
			try{
				return new ActiveXObject(activexmodes[i]);
			}
			catch(e){
				alert("Vaš browser ne podržava AJAX.");
			}
		}
	}
	else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return new XMLHttpRequest();
	else
		return false;
}
function explode( delimiter, string, limit ) {
	var emptyArray = { 0: '' };
	if ( arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined' ) {
		return null;
	}

	if ( delimiter === '' || delimiter === false || delimiter === null ) {
		return false;
	}

   if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' ) {
	   return emptyArray;
   }

   if ( delimiter === true ) {
	   delimiter = '1';
   }
   
   if (!limit) {
	   return string.toString().split(delimiter.toString());
   }
   else {
	   var splitted = string.toString().split(delimiter.toString());
	   var partA = splitted.splice(0, limit - 1);
	   var partB = splitted.join(delimiter.toString());
	   partA.push(partB);
	   return partA;
   }
}

function AttachOnClick(id) {
	var tabla = document.getElementById('tabla-slicice'+id);
	var images = tabla.getElementsByTagName('img');
	for (i=0;i<images.length;i++) {
		images[i].onmouseover = function() {
			this.style.opacity = "1";
			this.style.filter = "alpha(opacity=100)";
			this.style.cursor = "pointer";
		};
		images[i].onmouseout = function() {
			this.style.opacity = "0.5";
			this.style.filter = "alpha(opacity=50)";
		}
		images[i].onclick = function() {
			var source = this.src;
			var caption = this.alt;
			document.getElementById('caption').innerHTML = caption;
			source = source.replace(/50x50/, "300x300");
			document.getElementById('oglas-slike'+id).style.backgroundImage = 'url('+source+')';// 47px 74px no-repeat';
		}
	}
}

function GetCity(zip,where) {
	if (zip != '') {
		var mypostrequest=new ajaxRequest();
		mypostrequest.onreadystatechange=function(){
			if (mypostrequest.readyState==4){
				if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
					document.getElementById(where).value = mypostrequest.responseText;
				}
				else{
					alert("Greška prilikom obrade zahteva.");
				}
			}
		}
		var parameters="load=true&zip="+zip;
		mypostrequest.open("POST", "js/zip.php", true);
		mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		mypostrequest.send(parameters);
	}
	else {
		return;
	}
}

function base64_encode(data) {	        
	var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
 
	if (!data) {
		return data;
	}
	
	do {
		o1 = data.charCodeAt(i++);
		o2 = data.charCodeAt(i++);
		o3 = data.charCodeAt(i++);
 
		bits = o1<<16 | o2<<8 | o3;
 
		h1 = bits>>18 & 0x3f;
		h2 = bits>>12 & 0x3f;
		h3 = bits>>6 & 0x3f;
		h4 = bits & 0x3f;
 
		tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
	} while (i < data.length);
	
	enc = tmp_arr.join('');
	
	switch (data.length % 3) {
		case 1:
			enc = enc.slice(0, -2) + '==';
		break;
		case 2:
			enc = enc.slice(0, -1) + '=';
		break;
	}

	return enc;
}
