function gid(id)
{
	return document.getElementById(id);
}

// We create the HTTP Object
var ajax = new Array(20);
var i = 0;

function getHTTPObject()
{
	if (window.XMLHttpRequest) 
	{
   		http_request = new XMLHttpRequest();
 	} 
 	else if (window.ActiveXObject) 
 	{
    	try
    	{
		    http_request = new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) 
    	{
      		http_request = new ActiveXObject("Microsoft.XMLHTTP");
    	}
 	}				
	return http_request;
}

function handleHttpResponse()
{
	switch (ajax[i].readyState)
	{
		case 4 : 	gid("innerdiv").innerHTML="<div id=\"loading\"><img src=\"./graphics/spinner.gif\" width=\"16\" height=\"16\" alt=\"spinner\" />&nbsp; Zpracovávám...</div>" + ajax[i].responseText;
					break;
		case 0,1,2,3: gid("innerdiv").innerHTML= "<img src=\"../graphics/spinner.gif\" width=\"16\" height=\"16\" alt=\"spinner\" />&nbsp; Zpracovávám...";
	}
}

// The server-side scripts
var sendUrl = "includes/contact-form.php?"; 			

function ContactFormHttpResponse()
{
	switch (ajax[i].readyState)
	{
		case 1 :  	gid("contact-form").style.visibility = "visible";
					gid("contact-form").innerHTML = "<img src=\"../graphics/spinner.gif\" width=\"16\" height=\"16\" />&nbsp;&nbsp; zpracovávám, čekejte, prosím...";
					break;
		case 4		 :	gid("contact-form").style.visibility = "visible"
						gid("contact-form").innerHTML=ajax[i].responseText;
					break;
	}
}
			
function sendForm()
{
	form = gid("contactForm");
	i++;
	ajax[i] = getHTTPObject();
	ajax[i].open("GET", sendUrl + "contact_name=" + encodeURI(form.contact_name.value) + "&contact_phone=" + escape(form.contact_phone.value) + "&contact_email=" + form.contact_email.value + "&contact_company=" + form.contact_company.value + "&contact_note=" + encodeURI(form.contact_note.value) + "&send=1", true);
	ajax[i].onreadystatechange = ContactFormHttpResponse;
	ajax[i].send(null);
	return false;
}
var getUrl = "includes/contact-form.php?";

function getForm()
{
	form = gid("contactForm");
	i++;
	ajax[i] = getHTTPObject();
    ajax[i].open("GET", getUrl, true);
	ajax[i].onreadystatechange = ContactFormHttpResponse;
   	ajax[i].send(null);
  	return false;
}	        

function Loading()
{
		gid("loading").style.display= "block";
}

//AJAX funkce pro zpracovani objednavkoveho formulare
function GetOrderForm()
{
	gid("innerdiv").innerHTML = "<div id=\"loading\"><img src=\"./graphics/spinner.gif\" width=\"16\" height=\"16\" alt=\"spinner\" />&nbsp; Zpracovávám...</div>";
	load_script = "./includes/order-form.php?";
	
	i++; ajax[i] = getHTTPObject();		
	ajax[i].open("GET", load_script, true);
	ajax[i].onreadystatechange = handleHttpResponse;
	ajax[i].send(null);	
}

//AJAX funkce pro zpracovani objednavkoveho formulare
function ProccessOrder()
{
	Loading();
	load_script = "./includes/order-form.php?";
	setTimeout(function () { 
	i++; ajax[i] = getHTTPObject();		
	ajax[i].open("GET", load_script + "item_name=" + encodeURI(gid('itemName').value) + "&item_id=" + gid('itemID').value + "&item_pcs=" + gid('itemPcs').value + "&full_name=" + encodeURI(gid('Fullname').value) + "&company=" + encodeURI(gid('Company').value) + "&address=" + encodeURI(gid('Address').value) + "&city=" + encodeURI(gid('City').value) + "&zipcode=" + gid('Zipcode').value + "&phone=" + gid('Phone').value + "&email=" + gid('Email').value + "&delivery_type=" + encodeURI(gid('DeliveryType').value) + "&send=1" , true);
	ajax[i].onreadystatechange = handleHttpResponse;
	ajax[i].send(null);	
	},1000);
}

//funkce pro vyplneni nazvu zbozi do formulare
function setItemName(text,id)
{
	gid('itemName').value = text;
	gid('itemID').value = id;
}

//funkce pro rozbaleni/sbaleni menu
function colapse(item,level)
{
	if (gid(item+'_sub'+level).style.display == "none")
	{
		gid(item+'_sub'+level).style.display = "block";
		gid(item).style.backgroundImage = "url(../graphics/minus.gif)";
	}
	else
	{
		gid(item+'_sub'+level).style.display = "none";
		gid(item).style.backgroundImage = "url(../graphics/plus.gif)";
	}
}

//funkce pro zobrazeni oken pro mazani a napovedu
function showwindow(e,num)
{
   	kam=0;

    if(document.getElementById) 
    {
    	kam = -230;
        elm = gid(num);
        // Netscape 6.0+ and Internet Explorer 5.0+
        if (document.all && !window.opera) {
            x = event.clientX + document.documentElement.scrollLeft+kam;
            y = event.clientY + document.documentElement.scrollTop;
         }
         else {
            x = e.pageX+kam;
            y = e.pageY+5;
         }
         if (document.all && !window.opera) {
            elm.style.pixelLeft = x;
            elm.style.pixelTop = y;
         }
         else {
            elm.style.left = x + "px";
            elm.style.top = y + "px";
         }
         elm.style.display = "block"
    }
}

//funkce pro uzavreni oken pro mazani a napovedu
function closewindow(num){
  if(document.getElementById){
     elm=document.getElementById(num)
     elm.style.display = "none"
  }
}