//**********************************************
//Globalne funkcije
//**********************************************
var timer1 = null;

function replaceText(el, txt) {
	while (el.childNodes.length > 0) {
		el.removeChild(el.firstChild);
	}
	el.innerHTML = parseScript(txt);
//	el.appendChild(document.createTextNode(txt));
}

function parseScript(_source)
{
		var source = _source;
		var scripts = new Array();
 
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
 
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
 
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		// Return the cleaned source
		return source;
}


function showProgress()
{
	var el = document.getElementById("progress");
	el.style.visibility = 'visible';
}

function hideProgress()
{
	var el = document.getElementById("progress");
	el.style.visibility = 'hidden';
}

function showInfo(text, interval)
{
	var el = document.getElementById("info");
	replaceText(el, text);
	el.style.visibility = 'visible';
	if (interval > 0) {
		clearInterval(timer1);
		timer1 = setInterval(hideInfo, interval);
	}
}

function hideInfo()
{
	var el = document.getElementById("info");
	el.style.visibility = 'hidden';
}



//**********************************************
// AJAX za navigaciju na stranici
//**********************************************
var req = null;
var lastQuery = null;
var lastContnent = null;
var currentContent = null;
var lastID = null;
var timer2 = null;
var url = null;

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
				req = null;
		}
	}
}
else
{
	try {
		req = new XMLHttpRequest();
	}
	catch (error) {
		req = null;
	}
}

function ajaxRequest(obj,id,query) {
	obj.blur()
	var menuItem = document.getElementById(id)
	
	$('.menuitemselected').attr("class", "menuitem");
	if (menuItem != null)
		menuItem.setAttribute("class", "menuitemselected")
	lastQuery = query
	
	showProgress()
	
	if (lastID == 'video')
		musicStop();

	lastID=id;
	
	switch (id)
	{
		case 'video':
			musicStop();
			document.title = "The making of"
			break;
		case 'home':
			document.title = "Welcome - Sentaler - Bojana Sentaler"
			break
/*		case 'collection':
			replaceText(document.getElementById("content"), "")
			document.getElementById("collection_div").style.visibility = 'visible'
			document.getElementById("collection_div").style.position = "static"
			document.title = "Collection"
			hideProgress()
			return*/ // EXIT 
		default:
			document.title = (id.substr(0, 1).toUpperCase() + id.substr(1)).replace('\_',' ').replace('\_',' ')
			break
	}
	var loadingContent = null;
	
	if (query != null)
		url = "content/"+id+".php"+query
	else {
		//window.location.hash = "http:/www.sentaler.com/?loc=" + id;
		loadingContent = $('#content_'+id);
		if (loadingContent.length == 1) {
			if (loadingContent.attr('loaded') === 'true') {
				hideProgress();
				ShowContent(loadingContent);
				return;
			}
		}
		else {
			loadingContent = $('#content_bonus');
			loadingContent.empty();
		}
		url = "content/"+id+".php"
	}
	
	currentContent = loadingContent;
	
	req.abort();
	req.open("get", url, true);
	req.onreadystatechange = 
		function() {
			hideProgress()
			if (req.readyState == 4) {
				if (req.status == 200) {
					if (loadingContent != null && loadingContent == currentContent) {						$('#content_bonus').empty();
						loadingContent.empty();
						loadingContent.append(req.responseText);
						loadingContent.attr('loaded','true');
						ShowContent(loadingContent);
					}
		//			document.getElementById("collection_div").style.visibility = 'collapse'
		//			document.getElementById("collection_div").style.position = "absolute";
		//			el = document.getElementById("content");
		//			replaceText(el, req.responseText);
				}
				else {
					//alert("Greska: " + req.status);
				}
			}
		}
	req.send(null);	
}

function ShowContent(content) {
	if (lastContnent != null)
		lastContnent.hide();
	content.show();
	lastContnent = content;
}

//**********************************************
// AJAX za slanje forme
//**********************************************
var http_request = false;
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }

  if (!http_request) {
	 showInfo('Cannot complete the request', 5000);
	 return false;
  }

  showProgress();
  showInfo("Sending message...", 0);
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  hideProgress()
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		showInfo(http_request.responseText, 5000);
		obj = document.forms[0].reset();
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function get(url) {
	obj = document.forms[0];
  	var poststr = "";
  	for (var n=1; n < obj.elements.length; n++) {
		if (obj.elements[n].type != 'radio' || obj.elements[n].checked)
	    	poststr += "&" + obj.elements[n].name + "=" + encodeURI(obj.elements[n].value.replace(/&/g,"(amp)"));
	}
	poststr = obj.elements[0].name + "=" + encodeURI(obj.elements[0].value.replace(/&/g,"(amp)")) + poststr;

  	makePOSTRequest(url, poststr);
}

function getContact() {
	get('contact.php');
}
 
function getWheretobuy() {
	if (document.forms[0].elements['name'].value == "" || document.forms[0].elements['city'].value == "")
		alert("Please fill all required fields.");
	else if (!(document.forms[0].elements['email'].value != "" || document.forms[0].elements['phone'].value != ""))
		alert("Please fill email or phone fields.");
	else
		get('wheretobuy.php');
}

