var xmlhttp=null;
var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

function rating(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
				document.getElementById("rateStatus").innerHTML = me.parentNode.title;
			}
		}else{
			rating(preSet);
			document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
		}
	}
}

// When you actually rate something //
function rateIt(me,id){
	if(!rated){
		document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
		preSet = me;
		rated=1;
		sendRate(me,id);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel,id){
	alert("Your rating was: "+sel.title);
//convert the url to a string
  url="voting.do?vote="+sel.id+id;
  
  if(xmlhttp==null)
	 createRequestObject();
	
   xmlhttp.open('GET',url,true);  xmlhttp.send(null);

     
}
function createRequestObject() {
		
		try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	  catch(e) {
	    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	    catch(f) { xmlhttp=null; }
	  }
	  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	  	xmlhttp=new XMLHttpRequest();
	  }
		
	}

function sendComments(){

var article_id=document.getElementById("article_id").value;
var name=document.getElementById("name").value;
if(name=='')
	{
	alert('Please Enter your Name!');
	return false;
	}
var emailid=document.getElementById("emailid").value;
if(emailid=='')
	{
	alert('Please Enter your Email-id!');
	return false;
	}
var comments=document.getElementById("comments").value; 
if(comments=='')
	{
	alert('Please Enter Your Comments!');
	return false;
	}
var code=document.getElementById("code").value;
if(code=='')
	{
	alert('Please Enter Code!');
	return false;
	}
var data="name="+escape(name)+"&emailid="+escape(emailid)+"&comments="+comments+"&code="+escape(code)+"&article_id="+escape(article_id);

  url="comments.do";
  
  if(xmlhttp==null)
	 createRequestObject();
	
   xmlhttp.open('POST',url,true);
   xmlhttp.onreadystatechange=commentresponse;
   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlhttp.setRequestHeader("Content-length", data.length);

   xmlhttp.send(data);
return false;  
}

function commentresponse(){

if (xmlhttp.readyState == 4) 
    {
	    if (xmlhttp.status == 200) 
	    {
			
			var userList = xmlhttp.responseText;

			if(userList=='success'){ 
			document.getElementById('commentstatus').innerHTML = "&#2357;&#2367;&#2330;&#2366;&#2352; &#2360;&#2369;&#2352;&#2325;&#2381;&#2359;&#2367;&#2340;";
			document.getElementById('commenttable').innerHTML="";
			}
			
			if(userList=='invalidcode'){
				
				document.getElementById('invalidcode').style.visibility='visible';
				document.getElementById('invalidcode').innerHTML = "&#2325;&#2379;&#2337; &#2327;&#2354;&#2340; &#2346;&#2369;&#2344;&#2307; &#2325;&#2379;&#2358;&#2367;&#2358; &#2325;&#2352;&#2375;&#2306;";
				
				}
		}
	}

}

function subscription(){
var emailid=document.getElementById("subsc").value;
if(emailid=='')
	{
	alert('Please Enter Email-id!');
	return false;
	}
 url="subscription.do";
  var data="emailid="+escape(emailid);
  if(xmlhttp==null)
	 createRequestObject();
	
   xmlhttp.open('POST',url,true);
   xmlhttp.onreadystatechange=subscriptionresponse;
   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlhttp.setRequestHeader("Content-length", data.length);

   xmlhttp.send(data);
return false; 

}
function subscriptionresponse(){
	if (xmlhttp.readyState == 4) 
    {
	 if (xmlhttp.status == 200) 
	    {
			
			var userList = xmlhttp.responseText;
			if(userList=='success'){ 
			document.getElementById('subscription').innerHTML = "&#2312;-&#2350;&#2375;&#2354; &#2310;&#2312;&#2337;&#2368; &#2360;&#2369;&#2352;&#2325;&#2381;&#2359;&#2368;&#2340;";
			}
		}
	}
}
function encode_utf8( s )
{
  
}

 function encoder(st) {
		st = st.replace("/\r\n/g","\n");
		var utftext = "";
 
		for (var n = 0; n < st.length; n++) {
 
			var c = st.charCodeAt(n);
 
			utftext += "&#"+c+";";

			
 
		}
 
		return utftext;
	}

