var gRatingSectionID;
var xmlHttp;

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

function getCheckedRadio(formWithRadio) {
	for (i=0; i<formWithRadio.radRating.length;i++)
		if (formWithRadio.radRating[i].checked == true)
			return formWithRadio.radRating[i].value;
	return '0';
}

function openRatingBox(rating_type, item_id, root_path, item_desc) {
	var div_box_id, div_box;
	div_box_id = 'ratingdiv_'+rating_type+item_id;
	div_box = document.getElementById(div_box_id);

  var div_html = "<form name=ratingform> <br/>\
		<p class=\"heading\">###</p><br/>\
		<b>Your rating:</b> (0-Worst 5-Best)<br/>\
		<input type=radio name=radRating value=0>0&nbsp;&nbsp;\
		<input type=radio name=radRating value=1>1&nbsp;&nbsp;\
		<input type=radio name=radRating value=2>2&nbsp;&nbsp;\
		<input type=radio name=radRating value=3>3&nbsp;&nbsp;\
		<input type=radio name=radRating value=4>4&nbsp;&nbsp;\
		<input type=radio name=radRating value=5>5&nbsp;&nbsp;<br/><br/> \
    <b>Comments:</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea rows=5 cols=40 name=comments></textarea><br/><br/> \
    <input type=button value=Submit onclick=\"sendRating('###/votz/saveRating','###', getCheckedRadio(this.form),this.form.comments.value,'###')\">\
    <input type=button value=Cancel onclick=\"this.form.parentNode.style.display='none';\"> </form>";

	div_html=div_html.replace('###',item_desc);
	div_html=div_html.replace('###',root_path);
	div_html=div_html.replace('###',rating_type);
	div_html=div_html.replace('###',item_id);
	div_box.innerHTML = div_html;
	div_box.style.display='block';
}

function sendRating(rating_url, rating_type, star_rating, usr_comments, item_id) { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var postvars = "ratingtype=" + rating_type + "&rating=" + star_rating + "&comments="+encodeURIComponent(usr_comments) + "&itemid="+item_id;
	gRatingSectionID = 'ratingdiv_'+rating_type+item_id;
	xmlHttp.onreadystatechange=sentRating ;
	xmlHttp.open("POST",rating_url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-Length", postvars.length);
	xmlHttp.send(postvars);
}

function sentRating() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById(gRatingSectionID).innerHTML=xmlHttp.responseText ;
	} 
}

function loadDiv(div_name, div_url) {

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	xmlHttp.onreadystatechange=eval('loadDiv' + div_name) ;
	xmlHttp.open("GET",div_url,true);
	xmlHttp.send(null);
}
function loadDivsearchtips() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchtips').innerHTML=xmlHttp.responseText ;
	} 
}

function loadDivsearchclps() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchclps').innerHTML=xmlHttp.responseText ;
	} 
}

function loadDivsearchgcs1() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchgcs1').innerHTML=xmlHttp.responseText ;
	} 
}
function loadDivsearchgcs() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchgcs').innerHTML=xmlHttp.responseText ;
	} 
}

function loadDivsearchaps() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchaps').innerHTML=xmlHttp.responseText ;
	} 
}

function loadDivsearchretaps() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('searchretaps').innerHTML=xmlHttp.responseText ;
	} 
}

function loadDivreviewsDiv() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('reviewsDiv').innerHTML=xmlHttp.responseText ;
	} 
}

function openSendEmailBox(send_url, root_path) {
	var div_box_id, div_box;
	div_box_id = 'send_mail_div';
	div_box = document.getElementById(div_box_id);

  var div_html = "<form name=smform> <p class=\"heading\">e-mail this link to a friend</p><br/>\
		<table><tr><td><b>To:</b></td><td><input type=text name=to_email size=40 style='font-size:9px'>&nbsp;<email ID of your friend></td></tr>\
		<tr><td><b>Subject:</b></td><td><input type=text disabled name = subj size=40 style='font-size:9px' value='Your friend has sent this EcoPlum link to you!'/></td></tr>\
    <tr><td><b>Your Msg:</b></td><td><textarea name=comments rows=5 cols=40 style='font-size:10px;'>Hi, You might find this interesting.. Check out this link, </textarea></td></tr> \
		<tr><td><b>Link:</b></td><td>###</td></tr>\
    <tr><td colspan=2 align=right><input type=button value=Send style='font-size:9px;font-weight:bold;' onclick=\"if (this.form.to_email.value=='') alert ('Please enter a valid email'); else sendEmail('###',this.form,'###');\">\
    <input type=button value=Cancel style='font-size:9px;font-weight:bold;' onclick=\"this.form.parentNode.style.display='none';\"></td></tr></table></form>";

	div_html=div_html.replace('###',window.location);
	div_html=div_html.replace('###',window.location);
	div_html=div_html.replace('###',root_path);

	div_box.innerHTML = div_html;
	div_box.style.display='block';
}

function sendEmail(send_url, smform, root_path) { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url=root_path + "/users/sendlink" + "?to_email=" + smform.to_email.value + "&subj=" + smform.subj.value + "&comments=" + smform.comments.value + "&sendlink=" + send_url;
	
	xmlHttp.onreadystatechange=sentEmail ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function sentEmail() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById('send_mail_div').innerHTML=xmlHttp.responseText ;
	} 
}
