
//ajax
var xmlHttp

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

function new_rating(slug,rtg){ //watch args here and ordering
	
	//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"); //next ajax this
  		return;
  	}
	
	//alert("srt:"+str+"\nrtg:"+rtg);
	//dont use &amp; in this url below
	
	//check if undefined rating? dont send if it is

	var url="scripts/add_vote_2.php?rating="+rtg+"&slug="+slug; //called script
	//var url="scripts/add_vote_2.php?rid="+str+"&rating="+rtg; //called script
	//url=url+"&sid="+Math.random(); ?not sure what this is
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true); //next maybe post vars?
	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);}
		
		//see 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){
  		// IE
  		try{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch(e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
	}
	return xmlHttp;
}










function new_comment(uid){ //called from commentform.php (php5 enabled on bath site)
	
	var frm = document.review_form;
	
	ReqObj=GetXmlHttpObject();
	if(ReqObj==null){ alert ("Sorry your browser does not support our comments");
  		return;
  	}
	
	ReqObj.onreadystatechange=function(){ //monitors activity

		if(ReqObj.readyState==4){ //ok complete
			
			//clear ajxmsg ready for new msg
			var mydiv = document.getElementById("ajmsg");
			while(mydiv.firstChild){mydiv.removeChild(mydiv.firstChild);}
			
			if(ReqObj.responseText=="ok"){
				frm.Add.disabled = true; //disable submit
				var newdiv = document.createElement("div"); 
				newdiv.innerHTML = "Thankyou, your comment is waiting to be checked.";
				mydiv.appendChild(newdiv);
				
			}else{ //error msg
	
				var newdiv = document.createElement("div"); 
				newdiv.innerHTML = ReqObj.responseText; //server response
				mydiv.appendChild(newdiv);	
				
			}
		}
	} //end func
	
	
	
	
	
	//prep data rdy to POST
	
	//if they signed in some vars are set else undefined!
	
		
	if(frm.uid){ //they signed in
		
		var poststr="name="+escape(encodeURI(frm.name.value))+"&email="+escape(encodeURI(frm.email.value))+"&uid="+escape(encodeURI(frm.uid.value))+"&nickname="+escape(encodeURI(frm.nickname.value))+"&review="+escape(encodeURI(frm.review.value))+"&ajax=yes";
		
		//simpler errors?
		//var poststr = "name="+frm.name.value+"&email="+frm.email.value+"&uid="+frm.uid.value+"&nickname="+frm.nickname.value+"&review="+frm.review.value+"&ajax=yes";
		
		//poststr = escape(encodeURI(poststr));
	
	}else{
	
		//do these need to be encoded if posted?
		var poststr="name="+escape(encodeURI(frm.name.value))+"&email="+escape(encodeURI(frm.email.value))+"&review="+escape(encodeURI(frm.review.value))+"&ajax=yes";
	
	}


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