// JavaScript Document
// rating of story
function searchinput(str){
	var txtfind = "Thanks";
   if (str.search(txtfind) != -1)
       var midstring = true;
   else
       var midstring = false;
   return midstring;
}
function setCookie(val)
{
	var cookName = "ratingdoneCricket-"+val; 
	if(getCookie(cookName))
	{
		alert('Rating already done.');
		return false;
		//return true;
	}
	else
	{
		document.cookie = cookName+"=yes";
		return true;
	}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function showCurrentRating(contentId){
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null) { 
		alert("Browser does not support HTTP Request");
		return;
	}

	var url = "/storyresources.php?"
	url = url+"content_id="+contentId+"&website=cricket&flag=showCurrentRating"
	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

function setRating(rating, contentId){
	/* check cookies */
	if(setCookie( contentId )){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp == null) { 
			alert("Browser does not support HTTP Request");
			return;
		}
		for(counter=1; counter<=rating; counter++){
			var spanIdName = "star"+counter;
			document.getElementById(spanIdName).className = "starActive";
		}
		var url = "/storyresources.php"
		url = url+"?rating="+rating+"&content_id="+contentId+"&website=cricket&flag=Rating"
		xmlHttp.onreadystatechange = stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)	
	}
}


// Checking browser compatabile
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

// State Changed 
function stateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("ratingRes").innerHTML = xmlHttp.responseText 
	}
	else{
		document.getElementById("ratingRes").innerHTML = "Loading.....";
	} 
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
	
		
		if(str.indexOf(dot) == str.length -1){
			return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

/* insert comments */
function insertCommetMain( content_id, thisObj )
{
    var xmlHttpReq1 = false;
    var self = this;
	var formObj = document.frmcommentmain
    var name = formObj.name.value
    var email = formObj.email.value
    var comment = formObj.comment.value
    var subject = formObj.subject.value
	var parent = 0;
    var flag=true;
	var done=false;
   
    if(name=="")
    {
		document.getElementById("name").style.backgroundColor = "#FFFF00";
		formObj.name.focus();
		flag=false;
    }
    else
    {
		document.getElementById("name").style.backgroundColor = "white";
    }
    if((email=="")||(echeck(email)==false))
    {
		document.getElementById("emailid").style.backgroundColor = "#FFFF00";
		formObj.email.focus();
		flag=false;
    }
    else
    {
		document.getElementById("emailid").style.backgroundColor = "white";
    }

    if(subject=="")
    {
		document.getElementById("subject").style.backgroundColor = "#FFFF00";
		formObj.subject.focus();
		flag=false;
    }
    else

    {
		document.getElementById("subject").style.backgroundColor = "white";
    }

    if(comment=="")
    {
		document.getElementById("message").style.backgroundColor = "#FFFF00";
		formObj.comment.focus();
		flag=false;
    }
    else
    {
		document.getElementById("message").style.backgroundColor = "white";
    }

   if(!formObj.checkbox.checked)
   {
		document.getElementById("agree").style.backgroundColor = "#FFFF00";
		flag=false;
   }

if(flag==true)
{
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq1 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq1.open('GET', '/storyresources.php?name='+name+'&content_id='+content_id+'&email='+email+'&subject='+subject+'&comment='+comment+'&flag=SubmitComment&parent='+parent, true);
	self.xmlHttpReq1.onreadystatechange = function()
	{
        if (self.xmlHttpReq1.readyState == 4)
	{    	
		str = self.xmlHttpReq1.responseText;
		document.getElementById("divmessagemain").innerHTML = self.xmlHttpReq1.responseText;
		var txtfind = searchinput(str);
		if(txtfind == true){
		window.location = location.href;
		}
    }
	else
	{
		//formObj.submit.disabled = true;
	   document.getElementById("divmessagemain").innerHTML = "Submitting...";	
	}
	}	
    self.xmlHttpReq1.send(null);
}
return false;
}



/* insert by child */
function insertCommetChild( content_id )
{
    var xmlHttpReq1 = false;
    var self = this;
	var formObj = document.replycomment;
    var name = formObj.name.value
    var email = formObj.email.value
    var comment = formObj.comment.value
    var subject = formObj.subject.value
	var parent = formObj.parent.value
    var flag=true;
	var done=false;
   
    if(name=="")
    {
		document.getElementById("nameReply").style.backgroundColor = "#FFFF00";
		formObj.name.focus();
		flag=false;
    }
    else
    {
		document.getElementById("nameReply").style.backgroundColor = "white";
    }
     if((email=="")||(echeck(email)==false))
    {
		document.getElementById("emailidReply").style.backgroundColor = "#FFFF00";
		formObj.email.focus();
		flag=false;
    }
    else
    {
		document.getElementById("emailidReply").style.backgroundColor = "white";
    }

    if(subject=="")
    {
		document.getElementById("subjectReply").style.backgroundColor = "#FFFF00";
		formObj.subject.focus();
		flag=false;
    }
    else
    {
		document.getElementById("subjectReply").style.backgroundColor = "white";
    }

    if(comment=="")
    {
		document.getElementById("messageReply").style.backgroundColor = "#FFFF00";
		formObj.comment.focus();
		flag=false;
    }
    else
    {
		document.getElementById("messageReply").style.backgroundColor = "white";
    }

   if(!formObj.checkbox.checked)
   {
		document.getElementById("agreeReply").style.backgroundColor = "#FFFF00";
		flag=false;
   }

if(flag==true)
{
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq1 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq1.open('GET', '/storyresources.php?name='+name+'&content_id='+content_id+'&email='+email+'&subject='+subject+'&comment='+comment+'&flag=SubmitComment&parent='+parent, true);
	self.xmlHttpReq1.onreadystatechange = function()
	{
        if (self.xmlHttpReq1.readyState == 4)
	{    	
		str = self.xmlHttpReq1.responseText;
		document.getElementById("replymessage").innerHTML = self.xmlHttpReq1.responseText;
		var txtfind = searchinput(str);
		if(txtfind == true){
		window.location = location.href;
		}
    }
	else
	{
		//formObj.submit.disabled = true;	
	   document.getElementById("replymessage").innerHTML = "Submitting...";	
	}
	}	
    self.xmlHttpReq1.send(null);
}
return false;
}


function forwardLink()
{
    var xmlHttpReq1 = false;
    var self = this;
	var formObj = document.frmcommentforward
    var myname = formObj.myname.value
    var myemail = formObj.myemail.value
    var friendsemail = formObj.friendsemail.value
    var linkforward = window.location;
    var flag=true;
    
    if(myname=="")
    {
		document.getElementById("nameForw").style.backgroundColor = "#FFFF00";
		formObj.myname.focus();
		flag=false;
    }
	else{
		document.getElementById("nameForw").style.backgroundColor = "white";

	}

    if(myemail=="")
    {
		document.getElementById("emailidForw").style.backgroundColor = "#FFFF00";
		formObj.myemail.focus();
		flag=false;
    }
	else{
		document.getElementById("emailidForw").style.backgroundColor = "white";
	}

    if(friendsemail=="")
    {
		document.getElementById("frndemailForw").style.backgroundColor = "#FFFF00";
		formObj.friendsemail.focus();
		flag=false;
    }
	else{
		document.getElementById("frndemailForw").style.backgroundColor = "white";
	}

if(flag)
{
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq1 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq1.open('GET', '/storyresources.php?myname='+myname+'&friendsemail='+friendsemail+'&myemail='+myemail+'&linkforward='+linkforward+"&flag=LinkForward", true);
	self.xmlHttpReq1.onreadystatechange = function()
	{
        if (self.xmlHttpReq1.readyState == 4)
	{    	
		 document.getElementById("divmynameforward").innerHTML = self.xmlHttpReq1.responseText
    }
	else
	{
	   document.getElementById("divmynameforward").innerHTML = "Submitting...";
	}
    }
    self.xmlHttpReq1.send(null);

}
else
{
	document.getElementById("divmynameforward").innerHTML = "Please fill the fields indicated in the yellow color.";	
}
return false;
}

/* reply the comments */
function replyComments( divid,parent )
{
	if(lastDiv=="")
	{
		document.getElementById(divid).innerHTML = frmData;
		document.getElementById(divid).style.display='block';
		document.replycomment.parent.value=parent;
		lastDiv=divid;
	}
	else
	{
		document.getElementById(lastDiv).innerHTML='';
		document.getElementById(lastDiv).style.display='none';
		document.getElementById(divid).innerHTML=frmData;
		document.getElementById(divid).style.display='block';
		document.replycomment.parent.value=parent;
		lastDiv=divid;
	}
}

/* forward */
function forward(divid)
{
	if(lastDiv=="")
	{
		document.getElementById(divid).innerHTML=frmdataforward;
		document.getElementById(divid).style.display='block';
		lastDiv=divid;
	}
	else
	{
		document.getElementById(lastDiv).innerHTML='';
		document.getElementById(lastDiv).style.display='none';
		document.getElementById(divid).innerHTML=frmdataforward;
		document.getElementById(divid).style.display='block';
		lastDiv=divid;
	}
}


/* abuse */
function abuse(divid,commentid)
{
	if(lastDiv=="")
	{

		document.getElementById(divid).innerHTML=frmdataabuse;
		document.getElementById(divid).style.display='block';
		document.frmcommentabuse.commentid.value=commentid;
		lastDiv=divid;
	}
	else
	{
		document.getElementById(lastDiv).innerHTML='';
		document.getElementById(lastDiv).style.display='none';
		document.getElementById(divid).innerHTML=frmdataabuse;
		document.getElementById(divid).style.display='block';
		document.frmcommentabuse.commentid.value=commentid;
		lastDiv=divid;
	}
}

/* report abuse */
function updateAbuse()
{
    var xmlHttpReq1 = false;
    var self = this;
	var formObj = document.frmcommentabuse
    var comment = formObj.comment.value
    var commentid = formObj.commentid.value
    var storyid = formObj.storyId.value
    var flag=true;
if(flag) {	
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq1 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var url = '/storyresources.php?comment='+comment+'&commentid='+commentid+'&storyid='+storyid+"&flag=ProcessAbuse"
    self.xmlHttpReq1.open('GET', url, true);
	self.xmlHttpReq1.onreadystatechange = function()
	{
        if (self.xmlHttpReq1.readyState == 4)
	{    
 	    document.getElementById("divmynameabuse").innerHTML = self.xmlHttpReq1.responseText;
	}
	else
	{
	   document.getElementById("divmynameabuse").innerHTML = "Submitting..."
	}
    }
    self.xmlHttpReq1.send(null);
	return false;
}
else{
	document.getElementById("divmynameabuse").innerHTML = "Please fill the fields indicated in the yellow color.";
}
return false;
}
