
//ajax functions
//updated 14 july 09 to match update in rate_2.php

var xmlHttp

//called from rate_2.php and ref in head of restaurant pages

function new_comment(){
	
	var frm = document.review_form;
	
	/*
	//checks (might ajax these)
	if(frm.email.value==""){
		alert("Please fill all boxes");
		return;
	}
	else if(frm.review.value==""){
		alert("Please fill all boxes");
		return;
	}
	else if(frm.review.value==""){
		alert("Please fill all boxes");
		return;
	}
	*/

	
	
	//this uses more secure POST method unlike star ratings which use GET

	ReqObj=GetXmlHttpObject();//request object
	if(ReqObj==null){
  		alert ("Sorry your browser does not support our comments");
  		return;
  	}
	
	
	//prepare data to POST (escaping some chars eg & sign)
	//The escape() function encodes special characters, with the exception of: * @ - _ + . /
	//if you add +++++++ it will not add them?????
	//i think php and js escape decode.. are not compatable
	//works but not right?
	var poststr="name="+escape(encodeURI(frm.name.value))+"&email="+escape(encodeURI(frm.email.value))+"&review="+escape(encodeURI(frm.review.value))+"&ajax=yes"; //create messy code
	
	//or encodeURIComponent
	//var poststr="name="+encodeURIComponent(frm.name.value)+"&email="+encodeURIComponent(frm.email.value)+"&review="+encodeURIComponent(frm.review.value)+"&ajax=yes";
	
	
	//alternative, I can add chars as I want?
	//var poststr="name="+addslashes(frm.name.value)+"&email="+addslashes(frm.email.value)+"&review="+addslashes(frm.review.value)+"&ajax=yes";
	
	//prepare POST data (simple test)
	//var poststr="name="+frm.name.value+"&email="+frm.email.value+"&review="+frm.review.value+"&ajax=yes";

	ReqObj.onreadystatechange=function(){

		/*
		0 request is not initialized 
		1 request has been set up 
		2 request has been sent 
		3 request is in process 
		4 request is complete
		*/
		
		if(ReqObj.readyState==4){
			
			//alert("Thankyou, your restaurant review will be first checked before being added to our site."); //old - works!
			
			//clear ajmsg box ready for new msg
			var mydiv = document.getElementById("ajmsg"); //for some reason msg did not work maybe js keyword?
			while(mydiv.firstChild){mydiv.removeChild(mydiv.firstChild);}
			
			
			//remove the form to stop more submits if comment sent ok
			if(ReqObj.responseText=="ok"){
				
				//remove form
				//var myform = document.review_form;
				//while(myform.firstChild){myform.removeChild(myform.firstChild);}
				
				//or disable submit button
				document.review_form.Add.disabled = true; 
				
				//custom ok msg
				var newdiv = document.createElement("div"); 
				newdiv.innerHTML = "Thankyou, your comment will be first checked before being added to our site.";
				mydiv.appendChild(newdiv);
				
			
			}else{ //all other error msgs's
				
				var newdiv = document.createElement("div"); 
				newdiv.innerHTML = ReqObj.responseText; //server response (create code to display in php, easy!)
				mydiv.appendChild(newdiv);	
				
			}

		}


	}
	ReqObj.open("POST","/restaurants/scripts/comment.php",true); 
	ReqObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); //needed for post method
	ReqObj.send(poststr);
	
}














function new_rating(str,rtg){ //phasing out str (rest id)
	
	//if rating=0 selected?
	if(rtg==0){
		alert("Please select your rating first!");
		return;
	}

	
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
  		alert ("Sorry your browser does not support our ratings!"); //ajax?
  		return;
  	}

	//alert("srt:"+str+"\nrtg:"+rtg);
	//dont use &amp; in this url below

	//var url="scripts/add_vote_2.php?rid="+str+"&rating="+rtg; //called script
	var url="scripts/add_vote_3.php?rating="+rtg; //new without rid
	//url=url+"&sid="+Math.random(); ?not sure what this is
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function stateChanged(){ 
	if(xmlHttp.readyState==4){ 
		//innerhtml is non standard and might be causing the image errors!
		//document.getElementById("txtHint").innerHTML=xmlHttp.responseText; //replaces whats inside txtHint div on page!
		
		obj = document.getElementById("stars");
		
		//document.getElementById("txtHint").innerHTML=""; //clear innerHTML is bad!
		
		//better dom
		while(obj.firstChild){
			obj.removeChild(obj.firstChild);
		}
		
		//this works but still no images?
		

		
		//see also http://slayeroffice.com/articles/innerHTML_alternatives/
		
		//instead try dom method aproved by w3c from http://domscripting.com/blog/display/99
		var newdiv = document.createElement("div");
		newdiv.innerHTML = xmlHttp.responseText;
		
		var container = document.getElementById("stars");
		container.appendChild(newdiv);

		//not sure if this take html? only text
	}
}


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;
}


