/**
* 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 author = theForm.author.value;
	var submit_obj = document.getElementById('submit');
	var cancel_obj = document.getElementById('cancel');
	var loader_obj = document.getElementById('cmnt_ajax_loader');
	if(trim(author)==""){
		err +='Enter your name.\n';
		//return false;
	}
	if(trim(comment)==""){
		err +='Enter comment.\n';
		//return false;
	}
	if(trim(theForm.scode.value)==''){
		err +='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('Error in processing security code.\nPlease enter new security code.');
					loadCaptcha('frmComments');
			  }
			  else if(response == 'success')
			  {
			  	hideLoader(submit_obj, cancel_obj, loader_obj);
				closeDiv(container_id);
				alert('Your comment has been added 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 loadCaptcha(theForm)
{
	img_captcha = 'includes/captcha.php?rand=' + Math.floor(Math.random()*99999999);
	document.getElementById("cmncaptcha").src = img_captcha;
	document.getElementById("scode123").value = '';
}
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); return false;
			  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, lang_id)
{

	
	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
			,'onLoading':function(req){
				document.getElementById(theDiv).innerHTML = '<table width="100%"><tr><td align="center"><span class="loader">Loading...</span><br><br></td></tr></table>';
			}
			,'onSuccess':function(req){
					theFormTxt = req.responseText;
					//alert(theFormTxt);
					document.getElementById(theDiv).style.display = 'block';
					document.getElementById(theDiv).innerHTML = theFormTxt;
					if(lang_id!= "" && lang_id == '2'){
						onLoad();
					}
				}
			,'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
else
document.getElementById('remLen').innerHTML = maxlimit - field.value.length;
}
