/**
* pages modeule - 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;
	var submit_obj = document.getElementById('submit');
	var cancel_obj = document.getElementById('cancel');
	var loader_obj = document.getElementById('cmnt_ajax_loader');
	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)==""){
		//err +='Comment cannot be blank.\n';
		alert('Comment cannot be blank.\n');
		return false;
	}
	if(trim(theForm.scode.value)==''){
		//err +='Please enter Security Code.';
		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;
	//alert(container_id);

	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.');
			  }
			  else if(response == 'success')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				alert('Your comment has been added.');
				showComments(vid, pid);
			  }
			  else if(response == 'success_abuse')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				alert('Your comment contains abusive words and it will be displayed only after Approval.');
				showComments(vid, pid);
			  }
			  else
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				alert('Failed to post your comments.');
			  }
			}
			,'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 reportAbuse(theForm){
	var err='', response, checked = false;
	var submit_obj = document.getElementById('submit');
	var cancel_obj = document.getElementById('cancel');
	var loader_obj = document.getElementById('cmnt_ajax_loader');
	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;
	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.');
			  }
			  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 showComments(vid, pid){
	document.getElementById('preDiv').value = '';
	var pageURL = "/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("show comments"+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);
					}

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

function showCommentsForm(vid, parent_id, theDiv, level_id, pid, wid)
{

	//alert('theDiv'+theDiv);
	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?vid="+vid+"&parent_id="+parent_id+"&theDiv="+theDiv+"&level_id="+level_id+"&page="+pid+"&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 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 = "/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='#cmntsTop';
}

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


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

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
	alert("Your text is trimmed because it is more than 1500 Characters");
	}
	else{
	document.getElementById('remLen').innerHTML = maxlimit - field.value.length;
	}
}
