// JavaScript Document

var xmlHttp = createXmlHttpRequestObject();
	
//get post code and send it to a php page to process
function send_request(languages_id,products_id){
	// proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){  
	
	var description = document.getElementById('productdescription_edit');
	var image =  document.getElementById('products_image');
	var weight = document.getElementById('products_weight');
	var quantity = document.getElementById('products_quantity');
	var manufacturer = document.getElementById('products_manufacturer');
	var price = document.getElementById('price');
	var products_name = document.getElementById('products_name');
	
	if(description && image && weight && quantity && manufacturer && price && products_name){
	params =''; 
	params += "description="+description.value.toString()+'&';
	params += "languages_id="+languages_id+'&';
	params += "products_id="+products_id+'&';
	params += "products_image="+image.value+'&';
	params += "products_weight="+weight.value+'&';
	params += "products_quantity="+quantity.value+'&';
	params += "price="+price.value+'&';
	params += "products_manufacturer="+manufacturer.value+'&';
	params += "products_name="+products_name.value;
	
	alert(params);
    xmlHttp.open("POST","test/p.php",true);  
    // define the method to handle server responses
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
    xmlHttp.onreadystatechange = handleServerResponse_get_description;
    // make the server request
    xmlHttp.send(params);	
		}
  }
  else
    setTimeout(function(){send_request(languages_id,products_id)}, 5000);
}


function handleServerResponse_get_description(){
	// move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      var xmlResponse = xmlHttp.responseXML;
	  xmlDoc = xmlResponse.documentElement;
	  
	  var description	= 
	  xmlDoc.getElementsByTagName("description")[0].firstChild.data;	  
	 var image = xmlDoc.getElementsByTagName("image")[0].firstChild.data;
	 var weight = xmlDoc.getElementsByTagName("weight")[0].firstChild.data;
	 var quantity = xmlDoc.getElementsByTagName("quantity")[0].firstChild.data;
	 var manufacturer = xmlDoc.getElementsByTagName("manufacturer")[0].firstChild.data;
	 var price = xmlDoc.getElementsByTagName("price")[0].firstChild.data;
	 var name = xmlDoc.getElementsByTagName("name")[0].firstChild.data;
	 
	 var sql1= xmlDoc.getElementsByTagName("sql1")[0].firstChild.data;
	
	 var sql2= xmlDoc.getElementsByTagName("sql2")[0].firstChild.data;
	 
	if(sql1.length >0){//make sure we got result		
	alert.write(sql1);
		}
	
	if(sql2.length >0){//make sure we got result		
	alert(sql2);
		}
		
	if(image.length >0){//make sure we got result
	alert(image);
		if(document.getElementById('productMainImage'))
	document.getElementById('productMainImage').innerHTML='<img src=images/'+image+' >';
		else 
			alert('can not get image');
		} 
		
	if(weight.length >0){//make sure we got result		
	if(document.getElementById('display_weight'))
	 document.getElementById('display_weight').innerHTML=weight+'kg';
	 else 
			alert('can not get weight');
		}
		
	if(quantity.length >0){//make sure we got result
	if(document.getElementById('display_quantity'))
	  document.getElementById('display_quantity').innerHTML=quantity;
	  else 
			alert('can not get quantity');
		}
		
	if(manufacturer.length >0){//make sure we got result		
	if(document.getElementById('display_manufacturer'))
	 document.getElementById('display_manufacturer').innerHTML=manufacturer;
	 else 
			alert('can not get manufacturer');
		}
		
	if(price.length >0){//make sure we got result		
	if(document.getElementById('productPrices'))
	 document.getElementById('productPrices').innerHTML=price+'円';
	 else 
			alert('can not get price');
		}
		
	if(name.length >0){//make sure we got result		
	if(document.getElementById('productName'))
	 document.getElementById('productName').innerHTML=name;
	 else 
			alert('can not get name');
		}
		
	if(description.length >0){//make sure we got result		
	if(document.getElementById('productDescription'))
	 document.getElementById('productDescription').innerHTML=description;
	 else 
			alert('can not get description');
		}
		
    } 
    // a HTTP status different than 200 signals an error
    else 
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
  }
}
