/**
* videos.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
*/
function Ltrim(val)
{
return val.replace(/^\s+/,"");
}
function Rtrim(val)
{
return val.replace(/\s+$/,"");
}
function trim(val)
{
return Ltrim(Rtrim(val));
}

function addComment(theForm){
	var err='', response;
	var comment = theForm.description.value;

	if(trim(theForm.visitor_email.value)==''){
		//err +='Please enter valid Email Id.';
		alert('Email Id cannot be blank.');
		return false;
	}

	if(trim(theForm.visitor_email.value)!=''){
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(theForm.visitor_email.value)) {
			//return true;
		}
		else {
			//err +='Email Id cannot be blank.';
			alert('Please enter valid Email Id.');
			return false;
		}
	}

	if(trim(comment)==""){
		alert('Comment cannot be blank.\n');
		return false;
	}
	if(trim(theForm.scode.value)==''){
		alert('Please enter Security Code.');
		return false;
	}
	if(err != '')
	{
		alert(err);
		return false;
	}
	var theDiv = 'DisplayAllComments';
	var vid = theForm.vid.value;
	var pid = theForm.pid.value;
	var container_id = theForm.container_id.value;
	var status = AjaxRequest.submit(
		theForm
		,{
			'onSuccess':function(req)
			{
			  response = req.responseText;
			  //alert(response);
			  if(response == 'error')
			  {
				alert('Please enter valid security code.');
			  }
			  else if(response == 'success')
			  {
				closeDiv(container_id);
				alert('Your comment has been added.');
				showComments(vid, pid);
			  }
			  else
			  {
				alert('Failed to post your comments.');
			  }
			}
			,'onError':function(req)
			{
			  alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
			}
		}
	);
	return false;
}

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 vid = theForm.vid.value;
	var container_id = theForm.container_id.value;

	var status = AjaxRequest.submit(
		theForm
		,{
			'onSuccess':function(req)
			{
			  response = req.responseText;
			  //alert(response);
			  if(response == 'error')
			  {
				alert('Please enter valid security code.');
			  }
			  else if(response == 'success')
			  {
				closeDiv(container_id);
				alert('Your abuse report has been sent.');
			  }
			  else if(response == 'failed1')
			  {
				alert('Failed to send your abuse report.');
			  }
			  else
			  {
				alert('Failed to send your abuse report.');
			  }

			}
			,'onError':function(req)
			{
			  alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
			}
		}
	);
	return false;
}

function showComments(vid, pid){
	document.getElementById('preDiv').value = '';
	var pageURL = "/videos/templates/comments/show_comments_"+vid+".html"; //"/show_comments.php?id="+vid+"&page="+pid;
	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);
					}
					document.location.href='#top';
				}
			,'onError':function(req){ alert(req.statusText+'\nContents='+req.responseText); }
		}
	);
	return false;
}

function showCommentsForm(vid, 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 = "/videos/post_comments.php?vid="+vid+"&parent_id="+parent_id+"&theDiv="+theDiv+"&level_id="+level_id+"&page="+pid+"&wid="+wid;
	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(vid, 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 = "/videos/abuse_comments.php?vid="+vid+"&comment_id="+comment_id+"&theDiv="+theDiv+"&wid="+wid;
	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 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='#top';
}

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.value = maxlimit - field.value.length;
}
/*
var clipped;
var lenUSig;
var maxLength=1500;

function calcCharLeft() {
	clipped = false;
	lenUSig = 0;
	maxLength = 1500;
	var charleft;
	if (document.getElementById("description").value.length > maxLength) {
		charleft = 0;
		clipped = true;
	} else {
		charleft = maxLength - document.getElementById("description").value.length;
	}
alert(charleft);
	document.getElementById("msgCL").innerHTML = charleft;
	return clipped;
}

function textKey() {
	supportsKeys = true;
	calcCharLeft();
}
*/
