// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() {	
	var xmlHttp;
  
	// if IE
	if(window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	// other
  	else {
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	
	// return the created object or display an error message
	if (!xmlHttp) 
		alert("Error creating the XMLHttpRequest object.");
	else 
		return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function suggest(user) {
	var name = document.getElementById("productName").value;
	if(name.length > 2) {
		// proceed only if the xmlHttp object isn't busy
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {		
		      
			name = encodeURIComponent(name);
			xmlHttp.open("GET", "/ajax/productName.php?name=" + name + "&user=" + user, true);  
			xmlHttp.onreadystatechange = handleServerResponse;
			xmlHttp.send(null);
		}
		else
			// if the connection is busy, try again after one second  
			setTimeout('suggest()', 3000);
	}
	else document.getElementById('suggest').style.display = 'none';
}

// executed automatically when a message is received from the server
function handleServerResponse() {
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200) {
			if (xmlHttp.responseText){
				$("#suggest").css({display:"block"});
				document.getElementById("suggest").innerHTML = xmlHttp.responseText;
			}
			else
				$("#suggest").css({display:"none"}); 			
		} 
		// a HTTP status different than 200 signals an error
		else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

//hide suggest
function hideSuggest(){
	document.getElementById('suggest').style.display = 'none';
}

/*
$(document).ready(function(){	
	$("#productName, #suggest").click(function(e){
		if ((e.target).tagName != 'A')
			return false;
	});
	
	$("body").click(function(e){
		if ((e.target).tagName != 'A')
			$("#suggest").css({display:"none"});
	});
}); // close document.ready
*/
function handle_send_product() {
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200) {
			if (xmlHttp.responseText){
			  document.getElementById("error_send_product").style.display = 'none';
			  document.getElementById("ok_send_product").innerHTML = "Váš návrh byl odeslán.";
			  document.getElementById("new_product_text").value = "";
			  document.getElementById('ok_send_product').style.display = 'block';
        //alert(xmlHttp.responseText);				
			}
			else{
				document.getElementById("error_send_product").innerHTML = "Neodesláno, chyba připojení.";
      } 			
		} 
		// a HTTP status different than 200 signals an error
		else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}


function send_product() {
	var product = document.getElementById("new_product_text").value;

	if(product.length > 2) {
		// proceed only if the xmlHttp object isn't busy
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {		
		      
			product = encodeURIComponent(product);
			xmlHttp.open("GET", "/ajax/send_product.php?product=" + product, true);  
			xmlHttp.onreadystatechange = handle_send_product;
			xmlHttp.send(null);
		}
		
	}
	else {
		document.getElementById('ok_send_product').style.display = 'none';
	    document.getElementById('error_send_product').style.display = 'block';
	    document.getElementById("error_send_product").innerHTML = "Vyplňte název produktu.";		
   
  }
	
}

function handle_send_suggest() {
  
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200) {
			if (xmlHttp.responseText){
			     
			  document.getElementById("error_send_suggest").style.display = 'none';
			  document.getElementById("ok_send_suggest").innerHTML = "Váš návrh byl odeslán. ";
			  document.getElementById("new_suggest_text").value = "";
			  document.getElementById('ok_send_suggest').style.display = 'block';
        //alert(xmlHttp.responseText);			
        
			}
			else{
				document.getElementById("error_send_suggest").innerHTML = "Neodesláno, chyba připojení.";
      } 			
		} 
		// a HTTP status different than 200 signals an error
		else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}


function send_suggest() {
	var product = document.getElementById("new_suggest_text").value;
	var id_enquiry = document.getElementById("id_enquiry").value;
	
	if(product.length > 2) {
		// proceed only if the xmlHttp object isn't busy
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {		
		  
			product = encodeURIComponent(product);
			xmlHttp.open("GET", "/ajax/send_suggest.php?suggest=" + product + "&id_enquiry="+id_enquiry, true);  
			xmlHttp.onreadystatechange = handle_send_suggest;
			xmlHttp.send(null);
		}
	}
	else {
		document.getElementById('ok_send_suggest').style.display = 'none';
	    document.getElementById('error_send_suggest').style.display = 'block';
	    document.getElementById("error_send_suggest").innerHTML = "Zadejte návrh.";		
   
  }
	
}
