/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	USED FOR 	: VISHAL BLOG FRONT END
*/

// CODE STARTS HERE

/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	MODULE 		: COMMON 
	USED FOR 	: TRIM INPUT DATA
*/
var chk =0;
function trim(name)
{
	return name.replace(/^\s+|\s+$/g,"");
}

/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	MODULE 		: FORUM 
	USED FOR 	: POST NEW TOPIC VALIDATION
*/
function validateTopic(){
	var errormsg=0;
	var emailExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w\{2,3}+)*(\.\w{2,3})+$/;
	var topic=trim(jQuery('#txtTopic').val());
	var description=trim(jQuery('#txtDescription').val());
	if(topic == ""){
		jQuery('#errTopic').show();
		if(chk == 0){
			jQuery('#errTopic').html("Please enter the Topic.");
		}
		jQuery('#txtTopic').val("");
		errormsg=1;
		chk =0;
	}else if(topic){
		jQuery('#errTopic').hide();
	}
	
	if(description == ""){
		jQuery('#errDescription').show();
		jQuery('#errDescription').html("Please enter the Description.");
		jQuery('#txtDescription').val("");
		errormsg=1;
	}else if(description){
		jQuery('#errDescription').hide();
	}
	if(errormsg == 1){
		return false;
	}else{
		description.replace('\n','<br>')
		var urlreg= url+"forums/addTopic/";
		description = nl2br(description)
		var params="VTOPIC="+topic+"&VDESCRIPTION="+description;
		//alert(urlreg+params);
		jQuery.ajax({type: "GET",url: urlreg,data:params,success: function(data){
			if(trim(data) == 'No'){
				jQuery('#errTopic').fadeOut().text('Topic exists.').fadeIn();
				jQuery('#txtTopic').val("");
				jQuery('#txtTopic').focus();
				return false;
			}else if(trim(data) == 'Yes'){
				jQuery('#jason').hide();
				jQuery('#successMsg').html("Your topic successfully posted.");
				setTimeout("jQuery('#successMsg').show();",30);
				setTimeout("jQuery('#successMsg').fadeOut();",2000);
				setTimeout('location.href="'+url+'forums";',2000);
			}else if(trim(data) == 'SV'){
				jQuery('#jason').hide();
				jQuery('#successMsg').html("Please Login to pot your topic.");
				setTimeout("jQuery('#successMsg').show();",30);
				setTimeout("jQuery('#successMsg').fadeOut();",2000);
				setTimeout('location.href="'+url+'registers";',2000);
			}else{
				jQuery('#jason').hide();
				jQuery('#successMsg').html("Error posting topic. Please try again Later.");
				setTimeout("jQuery('#successMsg').show();",30);
				setTimeout("jQuery('#successMsg').fadeOut();",2000);
				setTimeout("location.href='"+url+"forums';",2000);
			}
	}	
	});	
	}
}
/*CHECKING TOPIC EXISTS OR NOT */
function checkTopic() { 
	var uval= trim(jQuery('#txtTopic').val());
	if(uval != ""){
	var urlcheck= url+"forums/checktopic/";
		jQuery.ajax({type: "GET",url: urlcheck,data:'VTOPIC='+uval,success: function(data){
					if(trim(data) == 'No'){
						jQuery('#errTopic').fadeOut().text('Topic exists.').fadeIn();
						jQuery('#txtTopic').val("");
						jQuery('#txtTopic').focus();
						chk = 1;
					}else if(trim(data) == 'Yes'){
						jQuery('#errTopic').fadeOut().text('Topic is available.').fadeIn();
					}
				}		
		});
	}
}

function validateTopic1(){
	
	var errormsg=0;
	var emailExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w\{2,3}+)*(\.\w{2,3})+$/;
	var topic=trim(jQuery('#txtTopic1').val());
	var description=trim(jQuery('#txtDescription1').val());
	
	if(topic == ""){
		jQuery('#errTopic1').show();
		if(chk == 0){
			jQuery('#errTopic1').html("Please enter the Topic.");
		}
		jQuery('#txtTopic1').val("");
		errormsg=1;
		chk =0;
	}else if(topic){
		jQuery('#errTopic1').hide();
	}
	
	if(description == ""){
		jQuery('#errDescriptio1n').show();
		jQuery('#errDescription1').html("Please enter the Description.");
		jQuery('#txtDescription1').val("");
		errormsg=1;
	}else if(description){
		jQuery('#errDescription1').hide();
	}
	if(errormsg == 1){
		return false;
	}else{
		var urlreg= url+"forums/addTopic/";
		description = nl2br(description)
		var params="VTOPIC="+topic+"&VDESCRIPTION="+description;
		//alert(urlreg+params);
		jQuery.ajax({type: "GET",url: urlreg,data:params,success: function(data){
			if(trim(data) == 'No'){
				jQuery('#errTopic1').fadeOut().text('Topic exists.').fadeIn();
				jQuery('#txtTopic1').val("");
				jQuery('#txtTopic1').focus();
				return false;
			}else if(trim(data) == 'Yes'){
				jQuery('#jason1').hide();
				jQuery('#successMsg1').html("Your topic successfully posted.");
				setTimeout("jQuery('#successMsg1').show();",30);
				jQuery('#successMsg1').focus();
				setTimeout("jQuery('#successMsg1').fadeOut();",2000);
				setTimeout('location.href="'+url+'forums";',2000);
			}else if(trim(data) == 'SV'){
				jQuery('#jason').hide();
				jQuery('#successMsg1').html("Please Login to pot your topic.");
				setTimeout("jQuery('#successMsg1').show();",30);
				setTimeout("jQuery('#successMsg1').fadeOut();",2000);
				setTimeout('location.href="'+url+'registers";',2000);
			}else{
				jQuery('#jason1').hide();
				jQuery('#successMsg1').html("Error posting topic. Please try again Later.");
				setTimeout("jQuery('#successMsg1').show();",30);
				setTimeout("jQuery('#successMsg1').fadeOut();",2000);
				setTimeout('location.href="'+url+'forums";',2000);
			}
	}	
	});	
	}
}
/*CHECKING TOPIC EXISTS OR NOT */
function checkTopic1() { 
	var uval= trim(jQuery('#txtTopic1').val());
	if(uval != ""){
	var urlcheck= url+"forums/checktopic/";
		jQuery.ajax({type: "GET",url: urlcheck,data:'VTOPIC='+uval,success: function(data){
					if(trim(data) == 'No'){
						jQuery('#errTopic1').fadeOut().text('Topic exists.').fadeIn();
						jQuery('#txtTopic1').val("");
						jQuery('#txtTopic1').focus();
						chk = 1;
					}else if(trim(data) == 'Yes'){
						jQuery('#errTopic1').fadeOut().text('Topic is available.').fadeIn();
					}
				}		
		});
	}
}
/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 06-NOV-2008
	MODULE 		: COMMON 
	USED FOR 	: REPLACE NEWLINE CHARACTER TO <BR> TAG
*/
function nl2br(text){
	text = escape(text);	
	if(text.indexOf('%0D%0A') > -1)
	{		
	re_nlchar = /%0D%0A/g ;	
	}else if(text.indexOf('%0A') > -1){		
		re_nlchar = /%0A/g ;	
	}else if(text.indexOf('%0D') > -1){		
		re_nlchar = /%0D/g ;	
	}else{re_nlchar = '<br />';}	
	return unescape( text.replace(re_nlchar,'<br />') );
}
	
/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	MODULE 		: FAN"S FORUM 
	USED FOR 	: VALIDATION FOR POST REPLYS
*/
function showPostArea(id){
	jQuery('#postText'+id).slideToggle('slow');
}

function postReply(idval,topicid){
	
	var textvalue = trim(document.getElementById("userComments"+idval).value);
	if(textvalue == ""){
		document.getElementById("commentErr"+idval).innerHTML="Please enter your Reply.";
		return false;
	}else{
		textvalue = nl2br(textvalue);
		ITOPIC = document.getElementById('txtTopic').value;
		IREPLYFOR = trim(document.getElementById('txtReplyfor'+idval).value);
		//var urlcmt= url+"forums/addreply/";
		var urlcmt= url+"forums/addreply/";
		var params='IBELONG='+topicid+'&VCOMMENT='+textvalue+'&ITOPIC='+ITOPIC+'&IREPLYFOR='+IREPLYFOR;
		jQuery.ajax({type: "GET",url: urlcmt,data:params,success: function(data){
			if(trim(data) == "Yes"){
				jQuery('#successMsg'+idval).show();
				document.getElementById('successMsg'+idval).innerHTML="Comment Added Successfully";
				jQuery('#postText'+idval).slideToggle('slow');
				msgId = '#successMsg'+idval;
				setTimeout("jQuery(msgId).show();",30);
				setTimeout("jQuery(msgId).slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'forums/topic/'+ITOPIC+'";',2000);
			}else if(trim(data) == "SV"){
				jQuery('#successMsg'+idval).show();
				document.getElementById('successMsg'+idval).innerHTML="Please Login to post your reply.";
				jQuery('#postText'+idval).slideToggle('slow');
				msgId = '#successMsg'+idval;
				setTimeout("jQuery(msgId).show();",30);
				setTimeout("jQuery(msgId).slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'registers";',2000);
			}else{
				document.getElementById("commentErr"+idval).innerHTML='Error posting reply.';
			}
		   }		
		});
	}
}
/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	MODULE 		: LET'S BLOG
	USED FOR 	: VALIDATION FOR POST COMMENTS
*/
function showTextArea(id){
	jQuery('#textarea'+id).slideToggle('slow');
}

function sendDatai(idval,topicid){
	var textvalue=eval("document.frmComment.textarea"+idval+".value");
	if(textvalue == ""){
		document.getElementById("errormsg"+idval).innerHTML="Please enter Comment";
		return false;
	}else{
		document.getElementById("errormsg"+idval).style.display='none';
		var urlcmt= url+"homes/addcomment/";
		textvalue = nl2br(textvalue);
		jQuery.ajax({type: "GET",url: urlcmt,data:'ITOPIC='+topicid+'&VCOMMENT='+textvalue,success: function(data){
			if(trim(data) == "Yes"){
				document.getElementById('successmsg').innerHTML="Comment Added Successfully";
				jQuery('#textarea'+idval).hide();
				jQuery('#successmsg'+idval).html("Your comment successfully posted.");
				msgId = '#successmsg'+idval;
				setTimeout("jQuery(msgId).show();",30);
				setTimeout("jQuery(msgId).slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'letsblog/topic/'+topicid+'";',2000);
			}else if(trim(data) == "SV"){
				document.getElementById('successmsg').innerHTML="Please login to post comments.";
				jQuery('#textarea'+idval).hide();
				jQuery('#successmsg'+idval).html("Please login to continue.");
				msgId = '#successmsg'+idval;
				setTimeout("jQuery(msgId).show();",30);
				setTimeout("jQuery(msgId).slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'registers";',2000);
			}else{
				document.getElementById('successmsg').innerHTML="Error posting comment. try again later.";
				jQuery('#textarea'+idval).hide();
				jQuery('#successmsg'+idval).html("Error posting comment. try again later.");
				msgId = '#successmsg'+idval;
				setTimeout("jQuery(msgId).show();",30);
				setTimeout("jQuery(msgId).slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'letsblog/topic/'+topicid+'";',2000);
			}
		   }		
		});
	}
}
/* 
	AUTHOR		: VARADHARAJAN S
	DATE 		: 02-NOV-2008
	MODULE 		: LET'S BLOG TOPIC PAGE
	USED FOR 	: VALIDATION FOR POST COMMENTS
*/
function showTxtArea(){
	jQuery('#divtextarea').slideToggle('slow');
}

function sendData(topicid){
	var textvalue=eval("document.frmComment.textarea.value");
	if(textvalue == ""){
		document.getElementById("errormsg").innerHTML="Please enter Comment";
		return false;
	}else{
		document.getElementById("errormsg").style.display='none';
		var urlcmt= url+"homes/addcomment/";
		textvalue = nl2br(textvalue);
		jQuery.ajax({type: "GET",url: urlcmt,data:'ITOPIC='+topicid+'&VCOMMENT='+textvalue,success: function(data){
			if(trim(data) == "Yes"){
				jQuery('#divtextarea').hide();
				jQuery('#successmsg').html("Your comment successfully posted.");
				setTimeout("jQuery('#successmsg').show();",30);
				setTimeout("jQuery('#successmsg').slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'letsblog/topic/'+topicid+'";',2000);
			}else if(trim(data) == "SV"){
				jQuery('#divtextarea').hide();
				jQuery('#successmsg').html("Please Login to post comments.");
				setTimeout("jQuery('#successmsg').show();",30);
				setTimeout("jQuery('#successmsg').slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'registers";',2000);
			}else{
				jQuery('#divtextarea').hide();
				jQuery('#successmsg').html("Error posting comment try again later.");
				setTimeout("jQuery('#successmsg').show();",30);
				setTimeout("jQuery('#successmsg').slideToggle('slow');",2000);
				setTimeout('location.href="'+url+'letsblog/topic/'+topicid+'";',2000);
			}
		   }		
		});
	}
}
// CODE ENDS HERE