// JavaScript Document
var rootFolder = "./utils/";

var MAX_CALLS = 100;
var fCalls = new Array(MAX_CALLS);
var lastIndex = -1;
var position = -1;



function runApp() {	
	var w = screen.availWidth;
	var h = screen.availHeight;
	
	var url = "app.php";
	window.open(url,"Flash","menubar=no,width="+w+",height="+h+",scrollbars=yes,toolbar=no");
}


function storeFunctionCalle( calledFunction ) {
	
	
	if(lastIndex == MAX_CALLS-1) {
		for(var i=0; i<MAX_CALLS-1; i++) {
			fCalls[i] = fCalls[i+1];	
		}
		lastIndex--;
	}
	
	if(position < lastIndex) {
		fCalls[++position] = calledFunction;
		lastIndex = position;
		
		//position++;
		//lastIndex = position;
		//fCalls[lastIndex] = calledFunction;	
	} else {
		fCalls[++lastIndex] = calledFunction;
		position = lastIndex;
		//lastIndex++;
	}
	document.getElementById("direction-next").className = "directionIdle";
	document.getElementById("direction-back").className = "";
		
}


function extendText(id) {
	
	var divId = "machine"+id;
	var morelessId = "moreless"+id;
	var newtabId = "newtab"+id;
	
	var params = rootFolder+"menu_process.php?machid="+id+"&short=0";	
	params += "&sid="+Math.random();  //cache control
	
	
	
	makeHttpGet(divId,params);
	//document.getElementById("moreless"+id).innerHTML = "<a href=\"javascript:shortenText("+id+")\">Less</a>";
	document.getElementById(divId).className = "machineTextDesc";
	document.getElementById(morelessId).innerHTML = "Less";
	document.getElementById(morelessId).href = "javascript:shortenText("+id+")";
	document.getElementById(newtabId).innerHTML = "Open in new tab";
	
}

function shortenText(id) {
	
	var divId = "machine"+id;
	var morelessId = "moreless"+id;
	var newtabId = "newtab"+id;
	
	var params = rootFolder+"menu_process.php?machid="+id+"&short=1";	
	params += "&sid="+Math.random();  //cache control
	
	makeHttpGet(divId,params);
	//document.getElementById("moreless"+id).innerHTML = "<a href=\"javascript:extendText("+id+")\">More</a>";
	document.getElementById(divId).className = "machineTextPref";
	document.getElementById(morelessId).innerHTML = "More";
	document.getElementById(morelessId).href = "javascript:extendText("+id+")";
	document.getElementById(newtabId).innerHTML = "More in new tab";
	
}

function makePost(params,action,objId) {
	
	var xmlHttpPost=GetXmlHttpObject();
	if (xmlHttpPost==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	xmlHttpPost.onreadystatechange=function() {
		if (xmlHttpPost.readyState==4) { 
			document.getElementById(objId).innerHTML=xmlHttpPost.responseText;
		}
	}
	
    xmlHttpPost.open('POST',action,true);
	
    xmlHttpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttpPost.setRequestHeader("Content-length", params.length);
    xmlHttpPost.setRequestHeader("Connection", "close");
	
	xmlHttpPost.send(params);
	
}


function sendUs() {
	
	var sname = document.getElementById('senderName').value;
	var semail = document.getElementById('senderEmail').value;
	var stext = escape(document.getElementById('senderText').value);
	
	var params = "name="+sname+"&email="+semail+"&text="+stext;
	params += "&sid="+Math.random();
	
	var action = rootFolder+'sendus_process.php';
	makePost(params,action,'sendus');
	
	
}




function showSimplePage(id, notsave) {
	
	/*
	if(!notsave) {
		storeFunctionCalle( function() {
			showSimplePage(id, true);
		} ); 
	} */
	
		
	var params = rootFolder+"menu_process.php?pageid="+id;	
	params += "&sid="+Math.random();  //cache control
	
	makeHttpGet("textBody",params);
	
	initMenu1();
	initMenu2();
	initMenu3();
	
}


function showOver(id) {
	if(document.getElementById(id).className != "menu1Selected")
		document.getElementById(id).className = "menu1Over";
	/*
	if(id.indexOf("_selected") < 0)
		document.getElementById(id).id = id+"_over";*/
}
function showOff(id) {
	if(document.getElementById(id).className != "menu1Selected")
		document.getElementById(id).className = "";
	/*
	if(id.indexOf("_selected") < 0)
		document.getElementById(id).id = orgId;*/
}

function showOverDir(id) {
	if(document.getElementById(id).className != "directionIdle")
		document.getElementById(id).className = "directionOver";
}
function showOffDir(id) {
	if(document.getElementById(id).className != "directionIdle")
		document.getElementById(id).className = "";
}

function selectM3(id) {
	if(document.getElementById(id).className != "menu3LinkSelected") {
		document.getElementById(id).className = 'menu3LinkOver';	
		
		var obj = document.getElementById(id);
		for(i=0; i<obj.childNodes.length; i++) {
			obj.childNodes[i].className = 'innerMenu3ElmOver';
		}
	}
}

function unselectM3(id) {
	if(document.getElementById(id).className != "menu3LinkSelected") {
		document.getElementById(id).className = 'menu3Link';
		
		var obj = document.getElementById(id);
		for(i=0; i<obj.childNodes.length; i++) {
			obj.childNodes[i].className = 'innerMenu3Elm';
		}
	}
}
/*   2nd  */

function selectM3_2(id) {
	if(document.getElementById(id).className != "menu3LinkSelected2") {
		document.getElementById(id).className = 'menu3LinkOver2';	
		
		var obj = document.getElementById(id);
		for(i=0; i<obj.childNodes.length; i++) {
			obj.childNodes[i].className = 'innerMenu3ElmOver2';
		}
	}
}

function unselectM3_2(id) {
	if(document.getElementById(id).className != "menu3LinkSelected2") {
		document.getElementById(id).className = 'menu3Link2';
		
		var obj = document.getElementById(id);
		for(i=0; i<obj.childNodes.length; i++) {
			obj.childNodes[i].className = 'innerMenu3Elm2';
		}
	}
}

function showNotice() {
	document.getElementById('homeNotice').style.visibility = "visible";
}

function hideNotice() {
	document.getElementById('homeNotice').style.visibility = "hidden";
}


function showFlashWindow(path) {
	if(path.length == 0)
		return;
		
	var url = "flashview.php?address="+path;
		
	window.open(url,"Flash","menubar=no,width=500,height=400,toolbar=no");
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

function makeHttpGet(objId, params) {
	
	var xhttp=GetXmlHttpObject();
	
	if (xhttp==null)
  	{
  		alert ("Error: Your browser does not support AJAX!");
  		return;
  	} 

	xhttp.onreadystatechange=function() {
		if (xhttp.readyState==4) { 
			document.getElementById(objId).innerHTML=xhttp.responseText;
		}
	}
	xhttp.open("GET",params,true);
	xhttp.send(null);
}

function initMenu1() {
	var obj = document.getElementById('menu1_content');
	
	var j=0;
	for(i=0; i<obj.childNodes.length; i++) {
		var ix = obj.childNodes[i].id;

		if(ix != null && ix != "") {
			j++;

			var newId = 'menuMain_Elm'+(j);
			document.getElementById(ix).id = newId;
		}
	}
}

function initMenu2() {
	
	var params = rootFolder+"menu_process.php?init=2";
	makeHttpGet('menu2_content', params);
	
	document.getElementById('picField').innerHTML = "";
	document.getElementById('picField').style.visibility = "hidden";
	
}

function initMenu3() {
	
	//document.getElementById('lMenuTitle').innerHTML = "<a class=\"bymanuf\" href=\"javascript:loadManufacturers()\">By Manufacturers</a>";
	
	var params =  rootFolder+"menu_process.php?init=3";
	makeHttpGet('menu3_content', params);
	
	document.getElementById('picField').innerHTML = "";
	document.getElementById('picField').style.visibility = "hidden";
	
}

function loadManufacturers() {
	document.getElementById('lMenuTitle').innerHTML = "By Manufacturers";
	initMenu1();
	initMenu2();
	
	var params =  rootFolder+"menu_process.php?init=4";
	makeHttpGet('menu3_content', params);

}



function selectParent(menuId, thisDivId, type, orgDivId) {
	//document.getElementById('log').innerHTML = "selectParents: ";
	if(thisDivId != null && type != null) {
			
		var obj = document.getElementById(menuId);
		
		//document.getElementById('log').innerHTML = "";
		var j=0;
		for(i=0; i<obj.childNodes.length; i++) {
			
			var ix = obj.childNodes[i].id;
			var cname = obj.childNodes[i].className;
			if(ix != null && ix != "") {
				
				if(type == 1) {  //menu1 change of div's id
					document.getElementById(ix).className = "";
					
					/*j++;
					if(ix != thisDivId) {
						var newId = 'menuMain_Elm'+(j);
						document.getElementById(ix).id = newId;
					}*/
				}
				else if(type == 2) {  //menu2 change of anchor's class
					if(document.getElementById(ix).className != 'menu2DeadLink')
						document.getElementById(ix).className = 'menu2Link';
				}
				else if(type == 3) {  //menu3 change of div's class
					if(document.getElementById(ix).className != 'menu3DeadLink') {
						//var object = obj.childNodes[i];
						//document.getElementById('log').innerHTML += i+": "+obj.childNodes[i]+": "+obj.childNodes[i].className+": "+obj.childNodes[i].innerHTML+"<br>";
						document.getElementById(ix).className = 'menu3Link';
						var object = document.getElementById(ix);
						object.childNodes[0].className = 'innerMenu3Elm';
					}
				}
			}
		}
		
		
		if(type == 1) { // && orgDivId != null) {
			document.getElementById(thisDivId).className = "menu1Selected";
			//document.getElementById(thisDivId).id = thisDivId+"_selected";		
			//document.getElementById(thisDivId).id = orgDivId+"_selected";		
		}
		else if(type == 2) {
			document.getElementById(thisDivId).className = 'menu2SelectedLink';
		}
		else if(type == 3) {
			document.getElementById(thisDivId).className = 'menu3LinkSelected';
			var object = document.getElementById(thisDivId);
			//object.childNodes[0].className = 'innerMenu3ElmSelected';
			object.childNodes[0].className = 'innerMenu3ElmOver';
		}
	}
}

function showMachinesByManuf(thisDivId, id, notsave) {
	
	if(!notsave) {
		storeFunctionCalle( function() {
			showMachinesByManuf(thisDivId, id, true);
		} ); 
	} 
	
	if(id == null)
		return;
	
	var parentMenuId = document.getElementById(thisDivId).parentNode.id;
	selectParent(parentMenuId, thisDivId, 3);	
	
	params =  rootFolder+"menu_process.php?manfid="+id;
	params += "&sid="+Math.random();  //cache control
	
	makeHttpGet('textBody', params);
	
}

function showMachines(thisDivId, id1, id2, id3, notsave) {
	
	if(!notsave) {
		storeFunctionCalle( function() {
			showMachines(thisDivId, id1, id2, id3, true);
		} ); 
	} 
	
	
	
	//if(id1 == null || id2 == null || id3 == null)
	//	return;
		
	if(id2 == null || id3 == null)
		return;
	
	var parentMenuId = document.getElementById(thisDivId).parentNode.id;	
	selectParent(parentMenuId, thisDivId, 3);
	
	var idlist = null;
	if(id1 != null) {
		idlist = "id1="+id1+"&id2="+id2+"&id3="+id3;
	}
	else 
		idlist = "id2="+id2+"&id3="+$id3;
	
	document.getElementById('picField').style.visibility = "visible";
	
	var params = rootFolder+"menu_process.php?"+idlist+"&pic=1";
	params += "&sid="+Math.random();  //cache control
	makeHttpGet('picField', params);
	
	params =  rootFolder+"menu_process.php?"+idlist;
	params += "&sid="+Math.random();  //cache control
	
	makeHttpGet('textBody', params);
	
}


function showMachinesFor2nd(thisDivId, id1, id2, notsave) {
	
	/*
	if(!notsave) {
		storeFunctionCalle( function() {
			showMachinesFor2nd(thisDivId, id1, id2, true);
		} ); 
	} */
		
	if(id1 == null || id2 == null) 
		return;
	
	//var parentMenuId = document.getElementById(thisDivId).parentNode.id;	
	//selectParent(parentMenuId, thisDivId, 3);
	
	var idlist = "id1="+id1+"&id2="+id2+"&machinesonly=1";  //NOVO - LOCIMO DA HOCEMO PRIKAZATI machine v TEXTu in NE polninit 3 menija
	
	//document.getElementById('picField').style.visibility = "visible";
	
	params =  rootFolder+"menu_process.php?"+idlist;
	params += "&sid="+Math.random();  //cache control
	
	makeHttpGet('textBody', params); //, function() { return "ALO"; });
	
}


function showMenu3(thisDivId, id1, id2, notsave) {
	
	if(!notsave) {
		storeFunctionCalle( function() {
			showMenu3(thisDivId, id1, id2, true);
		} ); 
	} 
	
	if(id1 == null || id2 == null)
		return;

	var parentMenuId = document.getElementById(thisDivId).parentNode.id;
	selectParent(parentMenuId, thisDivId, 2);
	
	var params =  rootFolder+"menu_process.php?id1="+id1+"&id2="+id2;
	params += "&sid="+Math.random();  //cache control
	
	document.getElementById('picField').innerHTML = "";
	document.getElementById('picField').style.visibility = "hidden";
	
	makeHttpGet('menu3_content', params);
	
	showMachinesFor2nd(thisDivId, id1, id2);
	
	//document.getElementById("textBody").innerHTML = "<p class='notice2'>Select from <b>By Material</b> menu to view machines by material.</p>";
	
	
}

function showMenu2(thisDivId, orgDivId, id1, notsave) {
	
	if(!notsave) {
		storeFunctionCalle( function() {
			showMenu2(thisDivId, orgDivId, id1, true);
			//showMenu2(thisDivId, id1, true);
		} ); 
	} 
	
	if(id1 == null)
		return;
	
	
	var elm = document.getElementById(thisDivId);
	
	var parentMenuId = elm.parentNode.id;
	//alert(parentMenuId);
	//selectParent(parentMenuId, thisDivId, 1, orgDivId);
	//selectParent(parentMenuId, thisDivId, 1);
	selectParent(parentMenuId, orgDivId, 1);
	
	var params =  rootFolder+"menu_process.php?id1="+id1;
	params += "&sid="+Math.random(); //cache control
	
	makeHttpGet('menu2_content', params);
	
	initMenu3();
	document.getElementById("textBody").innerHTML = "<p class='notice2'>Select from 2nd menu to see types of material in <b>By Material</b> menu.</p>";
	
	
}

function goNext() {
	if(position < lastIndex) {
		
		position++;
		var functionCalle = fCalls[position];
		functionCalle();	
		document.getElementById("direction-back").className = "";
	} else {
		document.getElementById("direction-next").className = "directionIdle";	
		document.getElementById("direction-back").className = "";
	}
}

function goBack() {
	if(position > 0) {
		
		position--;
		var functionCalle = fCalls[position];
		functionCalle();
		document.getElementById("direction-next").className = "";
	} else {
		document.getElementById("direction-back").className = "directionIdle";	
		document.getElementById("direction-next").className = "";
	}
}

