// JavaScript Document
//Post comments validation
function validate(){
	var regexp1 = new RegExp("\\S");
	var emailexp =  new RegExp("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/");
	message= document.frmdebatecomments.user_message.value;
	if (!regexp1.test(document.frmdebatecomments.user_name.value) || document.frmdebatecomments.user_name.value=='Enter your Name'){
		alert("Enter Your Name");
		document.frmdebatecomments.user_name.focus();
		return false;
	}
	var specchar = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\"\'\<\>]/;
	var name1=new RegExp('[0-9]');
	if ((name1.test(document.frmdebatecomments.user_name.value))){
		alert('Please enter a valid name');
		document.frmdebatecomments.user_name.focus();
		return false;
	}
	if(document.frmdebatecomments.user_name.value.search(specchar) == -1){	}
	else {
		alert("Please use valid characters");
		document.frmdebatecomments.user_name.focus();
		return false;
	}
	if (document.frmdebatecomments.user_emailid.value=='Enter your email id'){
		alert('Please enter your email id');
		document.frmdebatecomments.user_emailid.focus();
		return false;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frmdebatecomments.user_emailid.value)==false){
		alert("Enter Valid Email ID");
		document.frmdebatecomments.user_emailid.focus();
		return false;
	}
	if (!regexp1.test(document.frmdebatecomments.message_title.value) || document.frmdebatecomments.message_title.value=='Message title'){
		alert('Please enter a valid title');
		document.frmdebatecomments.message_title.focus();
		return false;
	}
	if (!regexp1.test(document.frmdebatecomments.user_message.value)){
		alert("Enter Message");
		document.frmdebatecomments.user_message.focus();
		return false;
	}
	if (message.length >1000){
		alert(" More than 1000 characters are not allowed ");
		document.frmdebatecomments.user_message.focus();
		document.frmdebatecomments.user_message.select();
		return false;
	}
	if(message.search(/http/) != -1){
		alert(" Links are not allowed in the comments ");
		document.frmdebatecomments.user_message.focus();
		return false;
	}	
	if (message.indexOf(">")!=-1 || message.indexOf("<")!=-1){
		alert(" < and > characters are not allowed ");
		document.frmdebatecomments.user_message.focus();
		return false;
	}
}
// For send email friends validation
function Validate_Save(){
	/* From Validation */
	if(document.frmMailer.mailfrom.value == ""){
		alert ("Please enter your e-mail address");
		document.frmMailer.mailfrom.focus();
		return false;
	}
	if (! validateEmail(document.frmMailer.mailfrom.value)){
		alert("Invalid Email Id.");
		document.frmMailer.mailfrom.focus();
		return false;
	}
	if (Trim(document.frmMailer.mailfrom.value) == '' ){
		alert("Email Address should not be a whitespace.");
		document.frmMailer.mailfrom.value = "";
		document.frmMailer.mailfrom.focus();
		return false;
	}
	/* To validations */
	if(document.frmMailer.mailto.value == ""){
		alert ("Please enter the e-mail address of your friend.");
		document.frmMailer.mailto.focus();
		return false;
	}
	var temp=0;
	var pattern = /,/; 
	var mailList = new Array();
    mailList = document.frmMailer.mailto.value.split(pattern);
    for (var i=0; i<mailList.length; i++){
        if (! validateEmail(mailList[i]))
		    temp=1;
	}    
	if (temp==1){
		alert("Invalid Email Id.");
		document.frmMailer.mailto.focus();
	 	return false;
 	}
	if (Trim(document.frmMailer.mailto.value) == '' ){
		alert("Email Address should not have whitespaces.");
		document.frmMailer.mailto.value = "";
		document.frmMailer.mailto.focus();
		return false;
	}
	/* Cc validations */
	if (document.frmMailer.mailcc.value != "" ){
		var stEmailID = document.frmMailer.mailcc.value;
		 if(stEmailID){
			 stEmailIDRes = stEmailID.split(",");
			 for(var counter = 0; counter < stEmailIDRes.length; counter++){
				 validEmail = new RegExp ("^[a-z]+[a-z0-9]*[\.|\-|_]?[a-z0-9]+@([a-z]+[a-z0-9]*[\.|\-]?[a-z]+[a-z0-9]*[a-z0-9]+){1,4}\.[a-z]{2,4}$", "g");
				 stEmailIDRes[counter] = Trim(stEmailIDRes[counter]);
				 if (!validateEmail(stEmailIDRes[counter])){
					 alert("The Email ID("+stEmailIDRes[counter]+") you have entered as (one of) your friend is incorrect. Please enter a valid Email ID");
					 document.frmMailer.mailcc.focus();
					 return false;
				 }
			 }
		 }
	}
	/* Comments validations*/
	if (document.frmMailer.comments.value == "" ){
		alert("Please enter your comments .");
		document.frmMailer.comments.focus();
		return false;
	}
	if (document.frmMailer.comments.value.length >1000){
		alert(" More than 1000 characters are not allowed ");
		document.frmMailer.comments.focus();
		document.frmMailer.comments.select();
		return false;
	}
	document.frmMailer.action="http://sify.com/news/election/debate/debateforagainst/debate_sendmail.php";
    return true;
}
// Post Reply validation
function notempty(){
	var specchar = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\"\'\<\>]/;
	var firstpart=document.frm_reply.email.value.substr(0,document.frm_reply.email.value.indexOf('@'));
	var secondpart=document.frm_reply.email.value.substr(document.frm_reply.email.value.indexOf('@')+1, document.frm_reply.email.value.length-document.frm_reply.email.value.indexOf('@'));
	var email1=new RegExp('[^a-zA-Z0-9.\_]');
	var email2=new RegExp('[^a-zA-Z0-9.]');
    var emailFilter=/^.+@.+\..{2,3}$/;
	var name1=new RegExp('[0-9]');
	if ((name1.test(document.frm_reply.name.value))){
		alert('Please enter a valid name');
		document.frm_reply.name.focus();
		return false;
	}
	if(document.frm_reply.name.value.search(specchar) == -1){	}
	else {
		alert("Please use valid characters");
		document.frm_reply.name.focus();
		return false;
	}
	if (Trim(document.frm_reply.name.value) == ""){
		alert ("Enter your Name");
		document.frm_reply.name.focus();
		return false;
	}
	if (Trim(document.frm_reply.email.value) == ""){
		alert ("Enter your Email Id");
		document.frm_reply.email.focus();
		return false;
	}
    if(firstpart.length<3){
		alert('Please enter valid email id');
		document.frm_reply.email.focus();
		return false;
	}
	if((email2.test(secondpart))){
		 alert('Please enter a valid email address');
		 document.frm_reply.email.focus();
		 return false;
	}
	if((email1.test(firstpart))){
		 alert('Please enter a valid email address');
		 document.frm_reply.email.focus();
		 return false;
	}
	if ((!(emailFilter.test(document.frm_reply.email.value)))){
		 alert('Please enter a valid email address');
		 document.frm_reply.email.focus();
		 return false;
	}
	if (Trim(document.frm_reply.comments.value) == ""){
		alert ("Enter the Comments");
		document.frm_reply.comments.focus();
		return false;
	}
	comments = Trim(document.frm_reply.comments.value);
	if (comments.length >1000){
		alert(" More than 1000 characters are not allowed ");
		document.frm_reply.comments.focus();
		document.frm_reply.comments.select();
		return false;
	}
	document.frm_reply.action="http://sify.com/news/election/debate/debateforagainst/debate_reply.php";
	return true;
}
// Abuse form Validation
function abuseFormValidate(){
	frm=document.frmAbuse;
	var specchar = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\"\'\<\>]/;
	var firstpart=frm.email.value.substr(0,frm.email.value.indexOf('@'));
	var secondpart=frm.email.value.substr(frm.email.value.indexOf('@')+1, frm.email.value.length-frm.email.value.indexOf('@'));
	var email1=new RegExp('[^a-zA-Z0-9.\_]');
	var email2=new RegExp('[^a-zA-Z0-9.]');
    var emailFilter=/^.+@.+\..{2,3}$/;
	var name1=new RegExp('[0-9]');
	if ((name1.test(frm.name.value))){
		alert('Please enter a valid name');
		frm.name.focus();
		return false;
	}
	if(frm.name.value.search(specchar) == -1){	}
	else {
		alert("Please use valid characters");
		frm.name.focus();
		return false;
	}
	if (Trim(frm.name.value) == ""){
		alert ("Enter your Name");
		frm.name.focus();
		return false;
	}
	if (Trim(frm.email.value) == ""){
		alert ("Enter your Email Id");
		frm.email.focus();
		return false;
	}
    if(firstpart.length<3){
		alert('Please enter valid email id');
		frm.email.focus();
		return false;
	}
	if((email2.test(secondpart))){
		 alert('Please enter a valid email address');
		 frm.email.focus();
		 return false;
	}
	if((email1.test(firstpart))){
		 alert('Please enter a valid email address');
		 frm.email.focus();
		 return false;
	}
	if ((!(emailFilter.test(frm.email.value)))){
		 alert('Please enter a valid email address');
		 frm.email.focus();
		 return false;
	}
	var reason=Trim(frm.reason.value);
	if (reason== ""){
		alert ("Please enter reason for abuse");
		frm.reason.focus();
		return false;
	}
	if (reason.length >1000){
		alert("More than 1000 characters are not allowed ");
		document.frmAbuse.reason.focus();
		document.frmAbuse.reason.select();
		return false;
	}
	frm.action="http://sify.com/news/election/debate/debateforagainst/abuse_form.php";
	return true;
}
function checksize(e){
	isIE=document.all? 1:0
	keyEntry = !isIE? e.which:event.keyCode; 
	if((keyEntry < '99')) {
		return true;  
	}
    var str = document.frmdebatecomments.user_message.value;
    if (str.length >= 1000) {
		alert("Comments should not be more than 1000 characters");
		return false;
    }
    else{
		return true;
    }
}
function validateEmail(email){
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null){
		return false;
	}
    if (splitted[1] != null){
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null){
		  return false;
	  }
    }
    if(splitted[2] != null){
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null){
		var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
		if(splitted[2].match(regexp_ip) == null){
			return false;
		}
      }
      return true;
    } 
	return false;
}
function Trim(value){
	value = value.replace(/^\s+/g, "");
	value = value.replace(/\s+$/g, "");
	return value;
}