/**
* comments.oneindia.in - Client-Side Scripts - Comments
*
* LICENSE: This file belongs to Greynium Information Technologies Pvt.Ltd
*
* @category   JavaScript
* @package    script.js
* @author     Vikram Reddy N (vikram.reddy@greynium.com)
* @copyright  2007-2008 Greynium Information Technologies
* @version    1.0
* @see        comments.js
* @since      File available since Release 1.0.x
* @deprecated File Not yet deprecated
*/
var img_captcha = new Image();
img_captcha.src = '/scripts/comments/captcha.php';
var img_submit = new Image();
img_submit.src = '/img/btn_submit.gif';
var img_cancel = new Image();
img_cancel.src = '/img/btn_cancel.gif';

function refreshCaptcha()
{
	var randomnumber = Math.floor(Math.random()*99999999);
	img_captcha.src = '/scripts/comments/captcha.php?rand=' + randomnumber;
}

function Ltrim(val)
{
	return val.replace(/^\s+/,"");
}

function Rtrim(val)
{
	return val.replace(/\s+$/,"");
}

function trim(val)
{
	return Ltrim(Rtrim(val));
}

function chkSpecialChar(val)
{
	var iChars = "`~_!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	
	for (var i = 0; i < val.length; i++)
	{
		if (iChars.indexOf(val.charAt(i)) != -1)
		{
			return false;
		}
	}
	return true;
}

function IsNumeric(val)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;	
	for (i = 0; i < val.length && IsNumber == true; i++) 
	{
		Char = val.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function createCookie(name,value,seconds)
{
    var date = new Date();
    date.setTime(date.getTime()+(seconds*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name)
{
    createCookie(name,"",-1);
}

function addComment(theForm)
{
	var err='', response;
	var author = trim(theForm.author.value);
	var comment = theForm.description.value;
	var submit_obj = theForm.getElementsByTagName('input')[theForm.getElementsByTagName('input').length - 3];
	var cancel_obj = theForm.getElementsByTagName('input')[theForm.getElementsByTagName('input').length - 1];
	var loader_obj = theForm.getElementsByTagName('span')[theForm.getElementsByTagName('span').length - 1];
	
	if(author == ""){
		err +='Please enter your name.\n';
		//return false;
	}
	else if(author.length < 3){
		err +='Name should be atleast three characters.\n';
		//return false;
	}
	else if(!chkSpecialChar(author)){
		err +='Name should not have special characters.\n';
	}
	else if(IsNumeric(author)){
		err +='Name should contain atleast one alphabet.\n';
	}
	if(trim(comment)==""){
		err +='Comment cannot be blank.\n';
		//return false;
	}
	if(trim(theForm.scode.value)==''){
		err +='Please enter Security Code.';
		//return false;
	}
	if(err != '')
	{
		alert(err);
		return false;
	}	
	var theDiv = 'DisplayAllComments';
	var path = theForm.path.value;
	var aid = theForm.aid.value;
	var pid = theForm.pid.value;
	var container_id = theForm.container_id.value;
	var newpath = path.replace(/\//g, "_");
	showLoader(submit_obj, cancel_obj, loader_obj);
	var status = AjaxRequest.submit(
		theForm
		,{
			'onSuccess':function(req)
			{
			  response = trim(req.responseText);
			  //alert(response);
			  if(response == 'error')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				//alert("Please enter valid security code.");
				alert('Error in processing security code.\nPlease enter new security code.');
				loadCaptcha('frmComments');
			  }
			  else if(response == 'ip_blocked')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				createCookie('oneIndia-comments', '1', 300);
			  }
			  else if(response == 'file_generated')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				alert('Your comment has been added.');
				createCookie('oneIndia-comments', '1', 300);
				if (document.getElementById("no_comments_div"))
                                {
                                        document.getElementById("no_comments_div").style.display = 'none';
                                }
				showComments(newpath, aid, pid);
				window.setTimeout("getLatestComments()",100);
			  }
			  else
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				//loadCaptcha('frmComments');
				//alert('Error in processing data.\nPlease try again later.');
				alert('Your comment has been added.\nIt will appear in few minutes.');
				closeDiv(container_id);
				createCookie('oneIndia-comments', '1', 300);
			  }
			}
			,'onError':function(req)
			{
				hideLoader(submit_obj, cancel_obj, loader_obj);
				//alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
			}
		}
	);
	return false;
}

function showLoader(submit_obj,cancel_obj,loader_obj)
{
	submit_obj.style.display = 'none';
	cancel_obj.style.display = 'none';
	loader_obj.style.display = '';
}
function hideLoader(submit_obj,cancel_obj,loader_obj)
{
	loader_obj.style.display = 'none';
	submit_obj.style.display = '';
	cancel_obj.style.display = '';	
}

function loadCaptcha(theForm)
{
	img_captcha = '/scripts/comments/captcha.php?rand=' + Math.floor(Math.random()*99999999);
	document.getElementById("" + theForm + "").getElementsByTagName('img')[document.getElementById("" + theForm + "").getElementsByTagName('img').length - 4].src = img_captcha;
	document.getElementById("" + theForm + "").getElementsByTagName('input')[document.getElementById("" + theForm + "").getElementsByTagName('input').length - 4].value = '';
}

function reportAbuse(theForm)
{
	var err='', response, checked = false;
	if((theForm.abusecheck1.checked == true) || (theForm.abusecheck2.checked == true) || (theForm.abusecheck3.checked == true) || (theForm.abusecheck4.checked == true) || (theForm.abusecheck5.checked == true) || (theForm.abusecheck6.checked == true))
	{
		checked= true;
	}
	if(!checked)
	{
		err += 'Please select the reason(s) that best describe your complaint\n';
	}
	if(trim(theForm.scode.value)==''){
		err +='Please enter Security Code.';
	}
	if(err != '')
	{
		alert(err);
		return false;
	}

	var aid = theForm.aid.value;
	var container_id = theForm.container_id.value;
	var submit_obj = theForm.getElementsByTagName('input')[theForm.getElementsByTagName('input').length - 3];
	var cancel_obj = theForm.getElementsByTagName('input')[theForm.getElementsByTagName('input').length - 1];
	var loader_obj = theForm.getElementsByTagName('span')[theForm.getElementsByTagName('span').length - 1];
	showLoader(submit_obj, cancel_obj, loader_obj);
	var status = AjaxRequest.submit(
		theForm
		,{
			'onSuccess':function(req)
			{ 
			  response = req.responseText;
			  //alert(response);
			  if(response == 'error')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				alert('Please enter valid security code.');
			  }
			  else if(response == 'success')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				alert('Your abuse report has been sent.');
			  }
			  else if(response == 'failed1')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				alert('Failed to send your abuse report.');
			  }
			  else
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				alert('Failed to send your abuse report.');
			  }
			  
			}
			,'onError':function(req)
			{ 
				hideLoader(submit_obj, cancel_obj, loader_obj);
				//alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
			}
		}
	);
	return false;
}

function getLatestComments()
{
	var pageURL = "/scripts/comments/comments.php?func=recent";
	var status = AjaxRequest.get(
		{
			'url':pageURL
			,'onSuccess':function(req){
					//alert(req.responseText);
				}
			,'onError':function(req)
				{ 
					//alert(req.statusText+'\nContents='+req.responseText); 
				}
		}
	);
	return false;
}

function showComments(path, aid, pid)
{
	document.getElementById('preDiv').value = '';
	var pageURL = "/new_comments/comments_"+path+aid;
	var status = AjaxRequest.get(
		{
			'url':pageURL
			,'onSuccess':function(req){
					//alert(pid);
					//alert(req.responseText);
					var objDiv = document.getElementById('DisplayAllComments');
					if(objDiv.style.display=='none')
						objDiv.style.display='block';
					document.getElementById('DisplayAllComments').innerHTML = req.responseText;
					if(pid > 1)
					{
						var page = 'Comment'+pid;
						//switchPage(page);
					}
					else
					{
						var page = 'Comment1';
					}
					readComments(page);
					document.location.href='#cmntTop';
				}
			,'onError':function(req)
				{ 
					//alert(req.statusText+'\nContents='+req.responseText); 
				}
		}
	);
	return false;
}

/*function showCommentsForm(path, aid, parent_id, theDiv, level_id, pid, wid)
{
	var preDiv = document.getElementById('preDiv').value;
	if(preDiv == '')
	{
		document.getElementById('preDiv').value = theDiv;
	}
	else
	{
		closeDiv(preDiv);
		document.getElementById('preDiv').value = theDiv;
	}

	var pageURL = "/post_comments.php?path="+path+"&aid="+aid+"&parent_id="+parent_id+"&theDiv="+theDiv+"&level_id="+level_id+"&page="+pid+"&wid="+wid;
//alert(pageURL); return false;
	var status = AjaxRequest.get(
		{
			'url':pageURL
			,'onSuccess':function(req){
					theFormTxt = req.responseText;
					//alert(theFormTxt);
					document.getElementById(theDiv).style.display = 'block';
					document.getElementById(theDiv).innerHTML = theFormTxt;
				}
			,'onError':function(req){ alert(req.statusText+'\nContents='+req.responseText); }
		}
	);
	return false;
}

function showReportAbuse(aid, comment_id, theDiv, wid)
{
	var preDiv = document.getElementById('preDiv').value;
	if(preDiv == '')
	{
		document.getElementById('preDiv').value = theDiv;
	}
	else
	{
		closeDiv(preDiv);
		document.getElementById('preDiv').value = theDiv;
	}

	var pageURL = "/abuse_comments.php?aid="+aid+"&comment_id="+comment_id+"&theDiv="+theDiv+"&wid="+wid;
	//alert(pageURL);
	var status = AjaxRequest.get(
		{
			'url':pageURL
			,'onSuccess':function(req){
					theFormTxt = req.responseText;
					//alert(theFormTxt);
					document.getElementById(theDiv).style.display = 'block';
					document.getElementById(theDiv).innerHTML = theFormTxt;
				}
			,'onError':function(req){ alert(req.statusText+'\nContents='+req.responseText); }
		}
	);
	return false;
}*/

function readComments(id)
{
	if(document.getElementById('readComments'))
	{
		document.getElementById('readComments').style.display = 'none';
		document.getElementById('CommentsInitial').style.display = 'none';
	}
	document.getElementById(id).style.display = 'block';
}

function showCommentsForm(path, aid, parent_id, theDiv, level_id, pid, wid)
{
	if(pid == 1 && document.getElementById('prePage'))
	{
		var prepage = document.getElementById('prePage').value;
		readComments(prepage);
	}
	var preDiv = document.getElementById('preDiv').value;
	if(preDiv == '')
	{
		document.getElementById('preDiv').value = theDiv;
	}
	else
	{
		closeDiv(preDiv);
		document.getElementById('preDiv').value = theDiv;
	}

	var dest_div = document.getElementById('cmntsForm');
	
	dest_div.getElementsByTagName("form")[0].name = 'frm';
	dest_div.getElementsByTagName("form")[0].id = 'frmComments';
	//alert(document.getElementById('frmComments'));
	var cmntForm = document.getElementById('frmComments');
	//alert(getmetaContents('title'));
	//var randomnumber = Math.floor(Math.random()*99999999)
	cmntForm.elements['cmnwebsite'].value = window.location.hostname;
	cmntForm.elements['cmnpath'].value = path;	
	cmntForm.elements['cmnaid'].value = aid;
	cmntForm.elements['cmncontainer_id'].value = "CommentForm_"+aid+"_"+parent_id;
	cmntForm.elements['cmnpid'].value = pid;
	cmntForm.elements['cmnlevel'].value = level_id;
	cmntForm.elements['cmnparent_id'].value = parent_id;
	cmntForm.elements['cmntitle'].value = getmetaContents('title');
	cmntForm.getElementsByTagName('td')[0].width = wid;
	//cmntForm.getElementsByTagName('img')[cmntForm.getElementsByTagName('img').length - 4].src = '/common/captcha.php?rand=' +randomnumber;
	loadCaptcha('frmComments');	
	//theFormTxt = document.getElementById('cmntsForm').innerHTML;
	
	window.setTimeout("document.getElementById('"+theDiv+"').innerHTML = document.getElementById('cmntsForm').innerHTML; document.getElementById('"+theDiv+"').style.display = 'block';", 100);
	window.setTimeout("makeOriginalFormBlank()", 100);
	//dest_div.innerHTML = eval(8*8) + dest_div.innerHTML;
	//cmntForm.getElementsByTagName('img')[cmntForm.getElementsByTagName('img').length - 3].focus();
	return false;
	//setTimeout("checkSleep()", 5000);
	//cmntForm.elements['author'].select();
}

function makeOriginalFormBlank()
{
	var orig_div = document.getElementById('cmntsForm');

	orig_div.getElementsByTagName("form")[0].name = '';
	orig_div.getElementsByTagName("form")[0].id = '';
	orig_div.getElementsByTagName("input")[1].value = '';
	orig_div.getElementsByTagName("input")[2].value = '';
	orig_div.getElementsByTagName("input")[3].value = '';
	orig_div.getElementsByTagName("input")[4].value = '';
	orig_div.getElementsByTagName("input")[5].value = '';
	orig_div.getElementsByTagName("input")[6].value = '';
	orig_div.getElementsByTagName("input")[7].value = '';
	orig_div.getElementsByTagName("input")[8].value = '';
	orig_div.getElementsByTagName("input")[9].value = '';
	orig_div.getElementsByTagName("input")[10].value = '';
	orig_div.getElementsByTagName('img')[orig_div.getElementsByTagName('img').length - 4].src = '';
}

function checkSleep()
{
	return true;
}

function getmetaContents(mn)
{
	var m = document.getElementsByTagName('meta');
	var metaTitle = '';
	for(var i in m)
	{
		if(m[i].name == mn)
		{
			metaTitle = m[i].content;
		}
	}
	if((metaTitle == '') || (metaTitle == 'undefined'))
	{
		metaTitle = document.title;
	}
	return metaTitle;
}

function showReportAbuse(aid, comment_id, theDiv, wid)
{
	var preDiv = document.getElementById('preDiv').value;
	if(preDiv == '')
	{
		document.getElementById('preDiv').value = theDiv;
	}
	else
	{
		closeDiv(preDiv);
		document.getElementById('preDiv').value = theDiv;
	}

	var randomnumber = Math.floor(Math.random()*999999)
	document.getElementById('abuspace').width = wid;
	document.getElementById('abuaid').value = aid;
	document.getElementById('abucontainer_id').value = theDiv;
	document.getElementById('abucomment_id').value = comment_id;
	//refreshCaptcha();
	//captcha_pre_load = new Image('/common/captcha.php?rand=' + randomnumber);
	document.getElementById('abucaptcha').src = '/scripts/comments/captcha.php?rand=' + randomnumber;

	//theFormTxt = document.getElementById('abuseForm').innerHTML;

	window.setTimeout("document.getElementById('"+theDiv+"').innerHTML = document.getElementById('abuseForm').innerHTML; document.getElementById('"+theDiv+"').style.display = 'block';",100);
	//alert('ji');
	//return false;
}

function hideComments(theDiv)
{
	var objDiv = document.getElementById(theDiv);
	
	if(objDiv.style.display=='block')
		objDiv.style.display='none';
}

function switchPage(id)
{
	//safe function to show an element with a specified id
	var prepage = document.getElementById('prePage').value;
	var preDiv = document.getElementById('preDiv').value;
	if(preDiv != '')
	{
		closeDiv(preDiv);
	}
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
		document.getElementById(prepage).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
			document.prepage.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
			document.all.prepage.style.display = 'none';
		}
	}
	document.getElementById('prePage').value = id;
	document.location.href='#cmntTop';
}

function closeDiv(id)
{
document.getElementById(id).innerHTML='';
}

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);// otherwise, update 'characters left' counter
	else 
		countfield.innerHTML = maxlimit - field.value.length;
}

function showFullComments()
{
	if ((location.hash != '') && (location.hash != '#cmntTop'))
	{
		readComments('Comment1');
	}
}

