/**************************************************************************************************************************
Author - Arvind Singh Rawat
Creation Date - 10 July 2010
Desc - Files contains the funciton required for the display comment module

*************************************************************************************************************************/

var isIE7 = false;
var isIE = false;

if(navigator.appName.indexOf("Internet Explorer")){
	isIE = true;
	if(navigator.appVersion.indexOf("MSIE 7.0") != -1){
		isIE7 = true;
	}
}

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}



function isSpecialChar(val){
	var data = val;
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"; 
	var flag = 1;
	for (var i = 0; i < data.length; i++) {
		if (iChars.indexOf(data.charAt(i)) != -1) {
			flag = 0;
			//alert ("Your string has special characters. \nThese are not allowed.");
		}
	}
	return flag;
}

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}



/*************************Function called on the click of reply button of comment and get the updated data thorugh Ajax***************************/
			function callReply(url,comment_id,userid,blackwhiteliststatus,pageno,trusted,type) {
				document.getElementById('error_Summary_'+comment_id).style.display='none';
				document.getElementById('error_comment_'+comment_id).style.display='none';
				
				if(blackwhiteliststatus=='BlackListed') {
					//sm('popData', 300, 100,'You status is marked as Blacklisted.So you can\'t reply to any comment');
					//alert("You status is marked as Blacklisted.So you can't reply to any comment");
					document.getElementById('notificationdiv').innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Your status is marked as blacklisted. So you can\'t reply to any comment</div>';
					//$('#notificationatten').focus();
					$('#notificationdiv').fadeIn(1000, function () {  });
					/*$('#notificationdiv').fadeOut(4000, function () {  });*/
					return false;
				}
				//alert(userid);return false;
				if(userid=='0') {
					//alert("Please Login First");
					areaid='message_area_'+comment_id;
					document.getElementById('response_'+comment_id).style.display='none';
					document.getElementById(areaid).innerHTML="<br clear='all' /><div class='warning' style='margin-top: 10px;'>Please login first</div>";
					return false;
				}
				var Name=document.getElementById('Name_'+comment_id).value;
				var Summary=trim(document.getElementById('Summary_'+comment_id).value);
				var comment=trim(document.getElementById('comment_'+comment_id).value);

				if(comment=='') {
					//alert("Please enter Summary");
					//return false;
					document.getElementById('error_comment_'+comment_id).style.display='block';
					/*$('#error_comment_'+comment_id).fadeOut(4000,function(){})*/
					return false;
				}
				
				if(Summary=='') {
					//alert("Please enter Comment");
					document.getElementById('error_Summary_'+comment_id).style.display='block';
					/*$('#error_Summary_'+comment_id).fadeOut(4000,function(){})*/
					return false;
				}
				
				var Summary=addslashes(Summary);
				var Email=document.getElementById('Email_'+comment_id).value;
				var Notify=document.getElementById('Notify_'+comment_id).value;
				var Anonymous=document.getElementById('Anonymous_'+comment_id).value;
				var comments=addslashes(document.getElementById('comments_'+comment_id).value);
				var commentid=document.getElementById('commentid_'+comment_id).value;
				
				
				Encoder.EncodeType = "entity";
				comment=Encoder.htmlEncode(comment);
				Summary=Encoder.htmlEncode(Summary);
				var timestamp= new Date().getTime();
				url=url+'&ajaxx=1&task_comment=reply&Name='+Name+'&Email='+Email+'&Notify='+Notify+'&Anonymous='+Notify+'&commentid='+commentid+'&page='+pageno+'&blackwhiteliststatus='+blackwhiteliststatus+'&timestamp='+timestamp;
				//alert(comment);
				$("#disp_comments").empty().html('<img src=/images/loading.gif>');
				//getRequest(url,'main_call_page','');
				$.post(url, { Comment: Summary, summary : comment },
				   function(data){
					$("#disp_comments").empty().html(data);
				   });
				

				if(trusted==1 || blackwhiteliststatus=='WhiteListed') {
					updateNotification("notificationdiv", "Thank you for replying to a comment on the EHI website. Unless you are a trusted poster, your comment will now be reviewed by a member of the editorial team. Please bear with us. We will endeavour to post it promptly.");
					$('#notificationdiv').fadeIn(1000, function () {  });
				}
				else {
					updateNotification("notificationdiv", "Thank you for replying to a comment on the EHI website. Unless you are a trusted poster, your comment will now be reviewed by a member of the editorial team. Please bear with us. We will endeavour to post it promptly.");
				}
				
			}
/*****************************************************************************************************************************************/
/*************************Function called on the click of pagination link and get the updated data thorugh Ajax***************************/			
			function callpagination(url) {
				url = CreateAjaxURL(url);
				//getRequest(url,'disp_comments','Please wait... loading <br/><img src=/images/loading.gif>');
				$("#disp_comments").empty().html('<img src=/images/loading.gif>');
				$.get(url, { },
				   function(data){
					/*$("#disp_comments").empty().html(data);*/
					document.getElementById("disp_comments").innerHTML = data;

				   });
			}

			function selectchange(url) {
				var e = document.getElementById("selecttype");
				var selecttype = e.options[e.selectedIndex].value;
				
				if(selecttype=='0')
				{url=url;}
				else if(selecttype=='Likes')
				{url=url+'&selecttype=Likes';}
				else 
				{url=url+'&selecttype=latest';}
				
				
				url = CreateAjaxURL(url);
				//getRequest(url,'main_call_page','Please wait... loading <br/><img src=/images/loading.gif>');
				$("#disp_comments").empty().html('<img src=/images/loading.gif>');
				$.get(url, { },
					function(data){
						$("#disp_comments").empty().html(data);
					});
			}
	
			function callcomajax(RID,objectid,commment_ID,extraparam) {	
				document.getElementById('error_abuse_type_'+commment_ID).style.display='none';
				document.getElementById('error_r_emailAddress_'+commment_ID).style.display='none';

				url='/comments/comajax.cfm?RID='+RID+'&objectid='+objectid+'&comment_ID='+commment_ID+extraparam;//alert(url);return false;
				
				if(document.getElementById("emailAddress_subscribe_"+commment_ID).value!='') {
					url=url+'&emailAddress='+document.getElementById("emailAddress_subscribe_"+commment_ID).value;
				}

				if(extraparam=='&report_abuse=1') {
					if(document.getElementById("abuse_type_"+commment_ID).value=='') {
							//alert('Please Select Abuse Type');
							document.getElementById('error_abuse_type_'+commment_ID).style.display='block';
							/*$('#error_abuse_type_'+commment_ID).fadeOut(4000,function(){})*/
							return false;
					}
					if(document.getElementById("r_emailAddress_"+commment_ID).value=='') {
						//alert("Please Enter Email Address");
						document.getElementById('error_r_emailAddress_'+commment_ID).style.display='block';
						/*$('#error_r_emailAddress_'+commment_ID).fadeOut(4000,function(){})*/
						return false;
					}
					if(!isValidEmail(document.getElementById("r_emailAddress_"+commment_ID).value)) {
						//alert("Please Enter Valid Email Address");
						document.getElementById('error_r_emailAddress_'+commment_ID).style.display='block';
						document.getElementById('error_r_emailAddress_'+commment_ID).innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Email address is invalid</div>';
						/*$('#error_r_emailAddress_'+commment_ID).fadeOut(4000,function(){})*/
						//return false;
						return false;
					}
						
					url=url+'&abuse_type='+document.getElementById("abuse_type_"+commment_ID).value+'&r_emailAddress='+document.getElementById("r_emailAddress_"+commment_ID).value;
					
					url = CreateAjaxURL(url);
					areaid='message_area_'+commment_ID;//alert(areaid)
					getRequest(url,areaid,'');
					document.getElementById("reportabuse_"+commment_ID).style.display='none';
				 	
					document.getElementById(areaid).style.display="block";
					/*$('#message_area_'+commment_ID).fadeOut(5000, function () {  });*/

					return true;
				}
				else {
					url = CreateAjaxURL(url);
					
					$.post(url,{commment_ID:commment_ID},function(data){
					
					//alert("Data Loaded: " + data);
					areaid='message_area_'+commment_ID;//alert(areaid)
					array=data.split('~~');
					logincheck=data.split(' <!-');
					 if(array[0]=='SUCCESS') {
					  	 document.getElementById('lik_'+commment_ID).innerHTML=array[1];
						 document.getElementById('unlik_'+commment_ID).innerHTML=array[2];
						//document.getElementById(areaid).innerHTML='Success';
					 } else {
						if(array[0]=='You already liked this' || array[0]=='You already disliked this') {
							document.getElementById(areaid).innerHTML="<br clear='all' /><div class='warning' style='margin-top: 10px;'>" + array[0] + "</div>";
						}
						else if(logincheck[0]=='Please Login First') {
							document.getElementById(areaid).innerHTML="<br clear='all' /><div class='warning' style='margin-top: 10px;'>" + logincheck[0] + "</div>";
						}
						else {
							document.getElementById(areaid).innerHTML="<br clear='all' /><div class='warning' style='margin-top: 10px;'>Technical Failure. Please try again</div>";
						}
						document.getElementById(areaid).style.display="block";
						/*('#message_area_'+commment_ID).fadeOut(5000, function () {  });*/
					}
    				 // $('#message_area_'+commment_ID).hide("fade", {}, 3000);
					return false;
				   });
				}
				return true;
			}
/*************************fucntion call the file to add the coment and display the updated code****************************************************/
			function add_comment_submit(RID,objectid,blackwhiteliststatus,trusted,type,title,formtitle,showviewby,showlikesdislikes,displayfirst,form_header_image) {
				document.getElementById('error_f_summary').style.display='none';
				document.getElementById('error_f_comment').style.display='none';
				document.getElementById('notificationdiv_signup').style.display='none';
				
				if(blackwhiteliststatus=='BlackListed') {
					//alert("Your status is marked as Blacklisted.So you can't post a comment");
						document.getElementById('notificationdiv_signup').style.display='block';
						document.getElementById('notificationdiv_signup').innerHTML="<br clear='all' /><div class='warning' style='margin-top: 10px;'>Your status is marked as Blacklisted. So you can't post a comment</div>";
						/*$('#notificationdiv_signup').fadeOut(4000,function(){})*/
						document.getElementById('notificationdiv_signup').focus();
					//document.getElementById('error_f_summary').style.display='block';
					return false;
				}
				
				url='/comments/commentsection.cfm?ID='+RID+'&objectid='+objectid+'&ajaxx=2&task_comment=addcomment'+'&title='+title+'&showviewby='+showviewby+'&formtitle='+formtitle+'&type='+type+'&showlikesdislikes='+showlikesdislikes+'&form_header_image='+form_header_image+'&displayfirst='+displayfirst;
				if(trim(document.getElementById("f_summary").value)=='' ) {
					//alert("Please enter Summary");
					document.getElementById('error_f_summary').style.display='block';
					/*$('#error_f_summary').fadeOut(4000,function(){})*/
					return false;
				}
				
				if(trim(document.getElementById("f_comment").value)=='' ) {
					//alert("Please enter Comment");
					document.getElementById('error_f_comment').style.display='block';
					/*$('#error_f_comment').fadeOut(4000,function(){})*/
					return false;
					
				}
				
				// JN: removed addslashes
				var Summary=trim(document.getElementById("f_summary").value);
				var comments=trim(document.getElementById("f_comment").value);
				
				//var Summary=addslashes(trim(document.getElementById("f_summary").value));
				//var comments=addslashes(trim(document.getElementById("f_comment").value));
				var f_email=document.getElementById("f_email").value;
				if(document.getElementById("f_comment_subscription").checked){
					var comment_subscription=1;					
				}
				else {
					var comment_subscription=0;
				}
				if(document.getElementById("f_reply_subscription").checked) {
					var reply_subscription=1;					
				}
				else {
					var reply_subscription=0;
				}
				
				// JN: changed htmlEncode to escape
				comments=escape(comments);	
				Summary=escape(Summary);
				
				//Encoder.EncodeType = "entity";
				//comments=Encoder.htmlEncode(comments);
				//Summary=Encoder.htmlEncode(Summary);
				
				var timestamp= new Date().getTime();
				
				url=url+"&Summary="+Summary+"&comment_subscription="+comment_subscription+"&email="+f_email+"&reply_subscription="+reply_subscription+"&blackwhiteliststatus="+blackwhiteliststatus+'&timestamp='+timestamp;
				
				$("#disp_comments").empty().html('<img src=/images/loading.gif>');
				
				$.post(url, {  comments : comments },
				   function(data){
					 $("#disp_comments").empty().html(data);
				   });
				
				if(trusted==1 || blackwhiteliststatus=='WhiteListed') {
					if (type != 'Endorsement') {
						updateNotification("notificationdiv", "Thank you for leaving your " + type + ". Unless you are a trusted poster, your " + type + " will be reviewed by a member of the editorial team before it appears on site. Please bear with us. We will endeavour to post it promptly.");		
					} else  {
						updateNotification("notificationdiv", "Thank you for leaving your " + type + ".");
						showSendDialog();
					}
				}
				else {
					//alert("Thank you for your comment it will now be reviewed by the EHI editorial team.  This may entail a short delay");
					if (type != 'Endorsement') {
						updateNotification("notificationdiv", "Thank you for posting your " + type + " on EHI. Your " + type + " will now be reviewed by a member of the editorial team, this may take a little time.");
					}else {
						updateNotification("notificationdiv", "Thank you for posting your " + type + " on EHI.");
						showSendDialog();
					}
						
				}
			}
			
			function open_reportabuse_div(comment_ID,id) {
				if(id=='S') {
					document.getElementById('reportabuse_'+comment_ID).style.display="none";
					document.getElementById('response_'+comment_ID).style.display="none";
					document.getElementById('subscribe_'+comment_ID).style.display="block";
				}
				if(id=='A') {
					document.getElementById('subscribe_'+comment_ID).style.display="none";
					document.getElementById('response_'+comment_ID).style.display="none";
					//document.getElementById('reportabuse_'+comment_ID).style.display="block";
					$('#reportabuse_'+comment_ID).fadeIn(1000, function () {  });
				}
				if(id=='R') {
					document.getElementById('reportabuse_'+comment_ID).style.display="none";
					document.getElementById('subscribe_'+comment_ID).style.display="none";
					//document.getElementById('response_'+comment_ID).style.display="block";
					$('#response_'+comment_ID).fadeIn(1000, function () {  });
					document.getElementById('message_area_'+comment_ID).innerHTML="";
				}
			}

			function addslashes(str) {
				str=str.replace(/\\/g,'\\\\');
				str=str.replace(/\'/g,'\\\'');
				str=str.replace(/\"/g,'\\"');
				str=str.replace(/\0/g,'\\0');
				return str;
			}
/*******************************hyper sharing code is defined here**************************************************************************/
			function share(button,id,url) {
				var comment=document.getElementById('commm_'+id).value;
				// or to set it to encode to html entities e.g & instead of &
				Encoder.EncodeType = "entity";

				// HTML encode text from an input element
				// This will prevent double encoding.
				comment = Encoder.htmlEncode(comment);
				//comment=comment.replace(")"," ");
				//comment=comment.replace("("," ");
				if(button=='F') {
					urls='http://'+url+'/comments/facebookshare.cfm?ID='+id;
					urls=escape(urls);//alert(urls);//return false;
					window.open("http://www.facebook.com/share.php?u="+urls+"","EHI","status=1");
					return true;
				}
				if(button=='L') {
					urls='http://'+url+'/comments/facebookshare.cfm?ID='+id;
					urls=escape(urls);
					title=comment.replace(" ","+");
					urls="http://www.linkedin.com/shareArticle?mini=true&url="+urls;
					//alert(urls);return false;
					window.open(urls,"EHI","status=1");
					return true;
					
				}
				if(button=='T') {
					urls=window.location;
					urls=escape(urls);
					title=comment.replace(" ","+");
					urls="http://twitter.com/home?status="+title+"%3a+"+urls;
					//alert(urls);return false;
					window.open(urls,"EHI","status=1");
					return true;
					//http://twitter.com/home?status=This+is+a+comment%3a+http%3A%2F%2Fbeta.twiddeo.com%2F39f6
				}
			}


	
function ReplyBox(commentid) {
	if(document.getElementById("ReplyBox_"+commentid).style.display=="block"){
		document.getElementById("ReplyBox_"+commentid).style.display="none";
		document.getElementById("arrows_"+commentid).innerHTML="<img class='cm-reply-close-button' src='/gfx/buttons/comment-right-arrow.gif'><div class='cm-reply-close'>Open</div>";
	}
	else{
		document.getElementById("ReplyBox_"+commentid ).style.display="block";
		document.getElementById("arrows_"+commentid).innerHTML="<img class='cm-reply-close-button' src='/gfx/buttons/comment-down-arrow.gif'><div class='cm-reply-close'>Close</div>";
	}
}

function ReplyBox2(commentid) {
	if(document.getElementById("ReplyBox2_"+commentid).style.display=="block"){
		document.getElementById("ReplyBox2_"+commentid).style.display="none";
		document.getElementById("arrows_"+commentid).innerHTML="<img class='cm-reply-close-button' src='/gfx/buttons/comment-right-arrow.gif'><div class='cm-reply-close'>Open</div>";
	}
	else{
		document.getElementById("ReplyBox2_"+commentid ).style.display="block";
		document.getElementById("arrows_"+commentid).innerHTML="<img class='cm-reply-close-button' src='/gfx/buttons/comment-down-arrow.gif'><div class='cm-reply-close'>Close</div>";
	}
}

function Report() {
	if(document.getElementById("Report").style.display=="block"){
		document.getElementById("Report").style.display="none";
	}
	else{
		document.getElementById("Report").style.display="block";
	}
}

function cancelcomment(id) {
	//if(document.getElementById(id).style.display=="block"){
		//document.getElementById(id).style.display="none";
		$('#'+id).fadeOut(1000, function () {  });
	//}
	//else{
	//	document.getElementById(id).style.display="block";
	//}
}


function showdetail(id) {
 document.getElementById("detail_"+id).style.display="block";
	if(isIE7){
		document.getElementById("reply_box_"+id).style.visibility="hidden";
	}
}

function hidedetail(id) {
 document.getElementById("detail_"+id).style.display="none";
	if(isIE7){
		document.getElementById("reply_box_"+id).style.visibility="visible";
	}
}

/*************************validates email**********************************************************************************/

function isValidEmail(el) {	
	var str=el;
	var testresults;
	var filter=/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

	if (filter.test(str))
		testresults=true;
	else 
		testresults=false;
	return (testresults);
}

Encoder = {

	// When encoding do we convert characters into html or numerical entities
	EncodeType : "entity",  // entity OR numerical

	isEmpty : function(val){
		if(val){
			return ((val===null) || val.length==0 || /^\s+$/.test(val));
		}else{
			return true;
		}
	},
	
	
// Convert HTML entities into numerical entities *********************************************************************************/
	HTML2Numerical : function(s){
		var arr1 = new Array('&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&agrave;','&aacute;','&acirc;','&atilde;','&Auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&Ouml;','&times;','&oslash;','&ugrave;','&uacute;','&ucirc;','&Uuml;','&yacute;','&thorn;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&Oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;','&quot;','&amp;','&lt;','&gt;','&oelig;','&oelig;','&scaron;','&scaron;','&yuml;','&circ;','&tilde;','&ensp;','&emsp;','&thinsp;','&zwnj;','&zwj;','&lrm;','&rlm;','&ndash;','&mdash;','&lsquo;','&rsquo;','&sbquo;','&ldquo;','&rdquo;','&bdquo;','&dagger;','&dagger;','&permil;','&lsaquo;','&rsaquo;','&euro;','&fnof;','&alpha;','&beta;','&gamma;','&delta;','&epsilon;','&zeta;','&eta;','&theta;','&iota;','&kappa;','&lambda;','&mu;','&nu;','&xi;','&omicron;','&pi;','&rho;','&sigma;','&tau;','&upsilon;','&phi;','&chi;','&psi;','&omega;','&alpha;','&beta;','&gamma;','&delta;','&epsilon;','&zeta;','&eta;','&theta;','&iota;','&kappa;','&lambda;','&mu;','&nu;','&xi;','&omicron;','&pi;','&rho;','&sigmaf;','&sigma;','&tau;','&upsilon;','&phi;','&chi;','&psi;','&omega;','&thetasym;','&upsih;','&piv;','&bull;','&hellip;','&prime;','&prime;','&oline;','&frasl;','&weierp;','&image;','&real;','&trade;','&alefsym;','&larr;','&uarr;','&rarr;','&darr;','&harr;','&crarr;','&larr;','&uarr;','&rarr;','&darr;','&harr;','&forall;','&part;','&exist;','&empty;','&nabla;','&isin;','&notin;','&ni;','&prod;','&sum;','&minus;','&lowast;','&radic;','&prop;','&infin;','&ang;','&and;','&or;','&cap;','&cup;','&int;','&there4;','&sim;','&cong;','&asymp;','&ne;','&equiv;','&le;','&ge;','&sub;','&sup;','&nsub;','&sube;','&supe;','&oplus;','&otimes;','&perp;','&sdot;','&lceil;','&rceil;','&lfloor;','&rfloor;','&lang;','&rang;','&loz;','&spades;','&clubs;','&hearts;','&diams;');
		var arr2 = new Array('&#160;','&#161;','&#162;','&#163;','&#164;','&#165;','&#166;','&#167;','&#168;','&#169;','&#170;','&#171;','&#172;','&#173;','&#174;','&#175;','&#176;','&#177;','&#178;','&#179;','&#180;','&#181;','&#182;','&#183;','&#184;','&#185;','&#186;','&#187;','&#188;','&#189;','&#190;','&#191;','&#192;','&#193;','&#194;','&#195;','&#196;','&#197;','&#198;','&#199;','&#200;','&#201;','&#202;','&#203;','&#204;','&#205;','&#206;','&#207;','&#208;','&#209;','&#210;','&#211;','&#212;','&#213;','&#214;','&#215;','&#216;','&#217;','&#218;','&#219;','&#220;','&#221;','&#222;','&#223;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&#230;','&#231;','&#232;','&#233;','&#234;','&#235;','&#236;','&#237;','&#238;','&#239;','&#240;','&#241;','&#242;','&#243;','&#244;','&#245;','&#246;','&#247;','&#248;','&#249;','&#250;','&#251;','&#252;','&#253;','&#254;','&#255;','&#34;','&#38;','&#60;','&#62;','&#338;','&#339;','&#352;','&#353;','&#376;','&#710;','&#732;','&#8194;','&#8195;','&#8201;','&#8204;','&#8205;','&#8206;','&#8207;','&#8211;','&#8212;','&#8216;','&#8217;','&#8218;','&#8220;','&#8221;','&#8222;','&#8224;','&#8225;','&#8240;','&#8249;','&#8250;','&#8364;','&#402;','&#913;','&#914;','&#915;','&#916;','&#917;','&#918;','&#919;','&#920;','&#921;','&#922;','&#923;','&#924;','&#925;','&#926;','&#927;','&#928;','&#929;','&#931;','&#932;','&#933;','&#934;','&#935;','&#936;','&#937;','&#945;','&#946;','&#947;','&#948;','&#949;','&#950;','&#951;','&#952;','&#953;','&#954;','&#955;','&#956;','&#957;','&#958;','&#959;','&#960;','&#961;','&#962;','&#963;','&#964;','&#965;','&#966;','&#967;','&#968;','&#969;','&#977;','&#978;','&#982;','&#8226;','&#8230;','&#8242;','&#8243;','&#8254;','&#8260;','&#8472;','&#8465;','&#8476;','&#8482;','&#8501;','&#8592;','&#8593;','&#8594;','&#8595;','&#8596;','&#8629;','&#8656;','&#8657;','&#8658;','&#8659;','&#8660;','&#8704;','&#8706;','&#8707;','&#8709;','&#8711;','&#8712;','&#8713;','&#8715;','&#8719;','&#8721;','&#8722;','&#8727;','&#8730;','&#8733;','&#8734;','&#8736;','&#8743;','&#8744;','&#8745;','&#8746;','&#8747;','&#8756;','&#8764;','&#8773;','&#8776;','&#8800;','&#8801;','&#8804;','&#8805;','&#8834;','&#8835;','&#8836;','&#8838;','&#8839;','&#8853;','&#8855;','&#8869;','&#8901;','&#8968;','&#8969;','&#8970;','&#8971;','&#9001;','&#9002;','&#9674;','&#9824;','&#9827;','&#9829;','&#9830;');
		return this.swapArrayVals(s,arr1,arr2);
	},	

	// Convert Numerical entities into HTML entities
	NumericalToHTML : function(s){
		var arr1 = new Array('&#160;','&#161;','&#162;','&#163;','&#164;','&#165;','&#166;','&#167;','&#168;','&#169;','&#170;','&#171;','&#172;','&#173;','&#174;','&#175;','&#176;','&#177;','&#178;','&#179;','&#180;','&#181;','&#182;','&#183;','&#184;','&#185;','&#186;','&#187;','&#188;','&#189;','&#190;','&#191;','&#192;','&#193;','&#194;','&#195;','&#196;','&#197;','&#198;','&#199;','&#200;','&#201;','&#202;','&#203;','&#204;','&#205;','&#206;','&#207;','&#208;','&#209;','&#210;','&#211;','&#212;','&#213;','&#214;','&#215;','&#216;','&#217;','&#218;','&#219;','&#220;','&#221;','&#222;','&#223;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&#230;','&#231;','&#232;','&#233;','&#234;','&#235;','&#236;','&#237;','&#238;','&#239;','&#240;','&#241;','&#242;','&#243;','&#244;','&#245;','&#246;','&#247;','&#248;','&#249;','&#250;','&#251;','&#252;','&#253;','&#254;','&#255;','&#34;','&#38;','&#60;','&#62;','&#338;','&#339;','&#352;','&#353;','&#376;','&#710;','&#732;','&#8194;','&#8195;','&#8201;','&#8204;','&#8205;','&#8206;','&#8207;','&#8211;','&#8212;','&#8216;','&#8217;','&#8218;','&#8220;','&#8221;','&#8222;','&#8224;','&#8225;','&#8240;','&#8249;','&#8250;','&#8364;','&#402;','&#913;','&#914;','&#915;','&#916;','&#917;','&#918;','&#919;','&#920;','&#921;','&#922;','&#923;','&#924;','&#925;','&#926;','&#927;','&#928;','&#929;','&#931;','&#932;','&#933;','&#934;','&#935;','&#936;','&#937;','&#945;','&#946;','&#947;','&#948;','&#949;','&#950;','&#951;','&#952;','&#953;','&#954;','&#955;','&#956;','&#957;','&#958;','&#959;','&#960;','&#961;','&#962;','&#963;','&#964;','&#965;','&#966;','&#967;','&#968;','&#969;','&#977;','&#978;','&#982;','&#8226;','&#8230;','&#8242;','&#8243;','&#8254;','&#8260;','&#8472;','&#8465;','&#8476;','&#8482;','&#8501;','&#8592;','&#8593;','&#8594;','&#8595;','&#8596;','&#8629;','&#8656;','&#8657;','&#8658;','&#8659;','&#8660;','&#8704;','&#8706;','&#8707;','&#8709;','&#8711;','&#8712;','&#8713;','&#8715;','&#8719;','&#8721;','&#8722;','&#8727;','&#8730;','&#8733;','&#8734;','&#8736;','&#8743;','&#8744;','&#8745;','&#8746;','&#8747;','&#8756;','&#8764;','&#8773;','&#8776;','&#8800;','&#8801;','&#8804;','&#8805;','&#8834;','&#8835;','&#8836;','&#8838;','&#8839;','&#8853;','&#8855;','&#8869;','&#8901;','&#8968;','&#8969;','&#8970;','&#8971;','&#9001;','&#9002;','&#9674;','&#9824;','&#9827;','&#9829;','&#9830;');
		var arr2 = new Array('&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&agrave;','&aacute;','&acirc;','&atilde;','&Auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&Ouml;','&times;','&oslash;','&ugrave;','&uacute;','&ucirc;','&Uuml;','&yacute;','&thorn;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&Oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;','&quot;','&amp;','&lt;','&gt;','&oelig;','&oelig;','&scaron;','&scaron;','&yuml;','&circ;','&tilde;','&ensp;','&emsp;','&thinsp;','&zwnj;','&zwj;','&lrm;','&rlm;','&ndash;','&mdash;','&lsquo;','&rsquo;','&sbquo;','&ldquo;','&rdquo;','&bdquo;','&dagger;','&dagger;','&permil;','&lsaquo;','&rsaquo;','&euro;','&fnof;','&alpha;','&beta;','&gamma;','&delta;','&epsilon;','&zeta;','&eta;','&theta;','&iota;','&kappa;','&lambda;','&mu;','&nu;','&xi;','&omicron;','&pi;','&rho;','&sigma;','&tau;','&upsilon;','&phi;','&chi;','&psi;','&omega;','&alpha;','&beta;','&gamma;','&delta;','&epsilon;','&zeta;','&eta;','&theta;','&iota;','&kappa;','&lambda;','&mu;','&nu;','&xi;','&omicron;','&pi;','&rho;','&sigmaf;','&sigma;','&tau;','&upsilon;','&phi;','&chi;','&psi;','&omega;','&thetasym;','&upsih;','&piv;','&bull;','&hellip;','&prime;','&prime;','&oline;','&frasl;','&weierp;','&image;','&real;','&trade;','&alefsym;','&larr;','&uarr;','&rarr;','&darr;','&harr;','&crarr;','&larr;','&uarr;','&rarr;','&darr;','&harr;','&forall;','&part;','&exist;','&empty;','&nabla;','&isin;','&notin;','&ni;','&prod;','&sum;','&minus;','&lowast;','&radic;','&prop;','&infin;','&ang;','&and;','&or;','&cap;','&cup;','&int;','&there4;','&sim;','&cong;','&asymp;','&ne;','&equiv;','&le;','&ge;','&sub;','&sup;','&nsub;','&sube;','&supe;','&oplus;','&otimes;','&perp;','&sdot;','&lceil;','&rceil;','&lfloor;','&rfloor;','&lang;','&rang;','&loz;','&spades;','&clubs;','&hearts;','&diams;');
		return this.swapArrayVals(s,arr1,arr2);
	},


	// Numerically encodes all unicode characters
	numEncode : function(s){
		
		if(this.isEmpty(s)) return "";

		var e = "";
		for (var i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if (c < " " || c > "~")
			{
				c = "&#" + c.charCodeAt() + ";";
			}
			e += c;
		}
		return e;
	},
	
	// HTML Decode numerical and HTML entities back to original values
	htmlDecode : function(s){

		var c,m,d = s;
		
		if(this.isEmpty(d)) return "";

		// convert HTML entites back to numerical entites first
		d = this.HTML2Numerical(d);
		
		// look for numerical entities &#34;
		arr=d.match(/&#[0-9]{1,5};/g);
		
		// if no matches found in string then skip
		if(arr!=null){
			for(var x=0;x<arr.length;x++){
				m = arr[x];
				c = m.substring(2,m.length-1); //get numeric part which is refernce to unicode character
				// if its a valid number we can decode
				if(c >= -32768 && c <= 65535){
					// decode every single match within string
					d = d.replace(m, String.fromCharCode(c));
				}else{
					d = d.replace(m, ""); //invalid so replace with nada
				}
			}			
		}

		return d;
	},		

	// encode an input string into either numerical or HTML entities
	htmlEncode : function(s,dbl){
			
		if(this.isEmpty(s)) return "";

		// do we allow double encoding? E.g will &amp; be turned into &amp;amp;
		dbl = dbl | false; //default to prevent double encoding
		
		// if allowing double encoding we do ampersands first
		if(dbl){
			if(this.EncodeType=="numerical"){
				s = s.replace(/&/g, "&#38;");
			}else{
				s = s.replace(/&/g, "&amp;");
			}
		}

		// convert the xss chars to numerical entities ' " < >
		s = this.XSSEncode(s,false);
		
		if(this.EncodeType=="numerical" || !dbl){
			// Now call function that will convert any HTML entities to numerical codes
			s = this.HTML2Numerical(s);
		}

		// Now encode all chars above 127 e.g unicode
		s = this.numEncode(s);

		// now we know anything that needs to be encoded has been converted to numerical entities we
		// can encode any ampersands & that are not part of encoded entities
		// to handle the fact that I need to do a negative check and handle multiple ampersands &&&
		// I am going to use a placeholder

		// if we don't want double encoded entities we ignore the & in existing entities
		if(!dbl){
			s = s.replace(/&#/g,"##AMPHASH##");
		
			if(this.EncodeType=="numerical"){
				s = s.replace(/&/g, "&#38;");
			}else{
				s = s.replace(/&/g, "&amp;");
			}

			s = s.replace(/##AMPHASH##/g,"&#");
		}
		
		// replace any malformed entities
		s = s.replace(/&#\d*([^\d;]|$)/g, "$1");

		if(!dbl){
			// safety check to correct any double encoded &amp;
			s = this.correctEncoding(s);
		}

		// now do we need to convert our numerical encoded string into entities
		if(this.EncodeType=="entity"){
			s = this.NumericalToHTML(s);
		}

		return s;					
	},

	// Encodes the basic 4 characters used to malform HTML in XSS hacks
	XSSEncode : function(s,en){
		if(!this.isEmpty(s)){
			en = en || true;
			// do we convert to numerical or html entity?
			if(en){
				s = s.replace(/\'/g,"&#39;"); //no HTML equivalent as &apos is not cross browser supported
				s = s.replace(/\"/g,"&quot;");
				s = s.replace(/</g,"&lt;");
				s = s.replace(/>/g,"&gt;");
			}else{
				s = s.replace(/\'/g,"&#39;"); //no HTML equivalent as &apos is not cross browser supported
				s = s.replace(/\"/g,"&#34;");
				s = s.replace(/</g,"&#60;");
				s = s.replace(/>/g,"&#62;");
			}
			return s;
		}else{
			return "";
		}
	},

	// returns true if a string contains html or numerical encoded entities
	hasEncoded : function(s){
		if(/&#[0-9]{1,5};/g.test(s)){
			return true;
		}else if(/&[A-Z]{2,6};/gi.test(s)){
			return true;
		}else{
			return false;
		}
	},

	// will remove any unicode characters
	stripUnicode : function(s){
		return s.replace(/[^\x20-\x7E]/g,"");
		
	},

	// corrects any double encoded &amp; entities e.g &amp;amp;
	correctEncoding : function(s){
		return s.replace(/(&amp;)(amp;)+/,"$1");
	},


	// Function to loop through an array swaping each item with the value from another array e.g swap HTML entities with Numericals
	swapArrayVals : function(s,arr1,arr2){
		if(this.isEmpty(s)) return "";
		var re;
		if(arr1 && arr2){
			//ShowDebug("in swapArrayVals arr1.length = " + arr1.length + " arr2.length = " + arr2.length)
			// array lengths must match
			if(arr1.length == arr2.length){
				for(var x=0,i=arr1.length;x<i;x++){
					re = new RegExp(arr1[x], 'g');
					s = s.replace(re,arr2[x]); //swap arr1 item with matching item from arr2	
				}
			}
		}
		return s;
	},

	inArray : function( item, arr ) {
		for ( var i = 0, x = arr.length; i < x; i++ ){
			if ( arr[i] === item ){
				return i;
			}
		}
		return -1;
	}

}

/************************************************************************************************************************************************/
function loginalertforabuse(id) {
	if(document.getElementById("loginalertforabuse"+id).style.display=="block"){
		document.getElementById("loginalertforabuse"+id).style.display="none";
	}
	else{
		var mydiv = document.getElementsByTagName("div");
		for(i=0;i< mydiv.length;i++) {
			if(mydiv[i].id.indexOf("loginalertforreply") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertforlike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertfordislike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";		
			if(mydiv[i].id.indexOf("loginalertforabuse") != -1 && mydiv[i].id != "loginalertforabuse"+id)
				document.getElementById(mydiv[i].id).style.display="none";		
		}
		document.getElementById("loginalertforabuse"+id).style.display="block";
	}
}

function loginalertforreply(id) {
	if(document.getElementById("loginalertforreply"+id).style.display=="block"){
		document.getElementById("loginalertforreply"+id).style.display="none";
	}
	else{
		var mydiv = document.getElementsByTagName("div");
		for(i=0;i< mydiv.length;i++) {
			if(mydiv[i].id.indexOf("loginalertforabuse") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertforlike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertfordislike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";		
			if(mydiv[i].id.indexOf("loginalertforreply") != -1 && mydiv[i].id != "loginalertforreply"+id)
				document.getElementById(mydiv[i].id).style.display="none";		
		}		
		document.getElementById("loginalertforreply"+id).style.display="block";
	}
}

function loginalertforlike(id) {
	if(document.getElementById("loginalertforlike"+id).style.display=="block"){
		document.getElementById("loginalertforlike"+id).style.display="none";
	}
	else{
		var mydiv = document.getElementsByTagName("div");
		for(i=0;i< mydiv.length;i++) {
			if(mydiv[i].id.indexOf("loginalertforabuse") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertforreply") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertfordislike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";		
			if(mydiv[i].id.indexOf("loginalertforlike") != -1 && mydiv[i].id != "loginalertforlike"+id)
				document.getElementById(mydiv[i].id).style.display="none";		
		}		
		document.getElementById("loginalertforlike"+id).style.display="block";
	}
}

function loginalertfordislike(id) {
	if(document.getElementById("loginalertfordislike"+id).style.display=="block"){
		document.getElementById("loginalertfordislike"+id).style.display="none";
	}
	else{
		var mydiv = document.getElementsByTagName("div");
		for(i=0;i< mydiv.length;i++) {
			if(mydiv[i].id.indexOf("loginalertforabuse") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertforreply") != -1)
				document.getElementById(mydiv[i].id).style.display="none";
			if(mydiv[i].id.indexOf("loginalertforlike") != -1)
				document.getElementById(mydiv[i].id).style.display="none";		
			if(mydiv[i].id.indexOf("loginalertfordislike") != -1 && mydiv[i].id != "loginalertfordislike"+id)
				document.getElementById(mydiv[i].id).style.display="none";		
		}
		document.getElementById("loginalertfordislike"+id).style.display="block";
	}
}

function submitloginform(RID,objectid,autologin) {
	var email = document.getElementById('user_email').value;
	var password = document.getElementById('user_password').value;
	document.getElementById('error_user_email').style.display='none';
	document.getElementById('error_user_password').style.display='none';
	document.getElementById('notificationdiv_signup').style.display='none';
	
	if(email.length == 0) {
		document.getElementById('error_user_email').style.display='block';
		/*$('#error_user_email').fadeOut(4000,function(){})*/
		//alert("Please enter Email Address");
		document.getElementById('user_email').focus();
		return false;
	}
	
	if(password.length == 0) {
		//alert("Please enter Password");
		document.getElementById('error_user_password').style.display='block';
		/*$('#error_user_password').fadeOut(4000,function(){})*/
		document.getElementById('user_password').focus();
		return false;
	}
					
	url='/comments/comajaxlogin.cfm?ID='+RID+'&task=formlogin&objectid='+objectid+'&email='+email+'&pwd='+password+'&autologin='+autologin;
				
	//getRequest(url,'loginformvalidation','');	
	//ssalert("Logged in Successfully");
	$.get(url,{},function(data) {
					array=data.split(" <!-");
					if(array[0].trim()=='Your login was unsuccessful... Please try again') {
						//alert('Please enter correct Email and Password');
						//alert("Please enter Password");
						document.getElementById('notificationdiv_signup').style.display='block';
						document.getElementById('notificationdiv_signup').innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Please enter correct Email and Password</div>';
						/*$('#notificationdiv_signup').fadeOut(4000,function(){})*/
						document.getElementById('user_email').focus();
						return false;
					}
					else if(array[0].trim()=='Success') {
						//alert("Logged In Successfully");
						window.location.reload();
						
						window.location = window.location.pathname + "#loginformvalidation";
					}
					else {
						//alert('Your login was unsuccessful... Please try again');
						document.getElementById('notificationdiv_signup').style.display='block';
						document.getElementById('notificationdiv_signup').innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Your login was unsuccessful... Please try again</div>';
						/*$('#notificationdiv_signup').fadeOut(4000,function(){})*/
						document.getElementById('user_email').focus();
					}
			}
		);
}

// ********************** Twitter Login display fucntion ******************************************************//
function twitterLogin() {

	if(document.getElementById("twitterLogin").style.display=="block"){
		document.getElementById("twitterLogin").style.display="none";
	}
	else{
		document.getElementById("twitterLogin").style.display="block";
	}
}
			
function twittercheck() {
	if(document.getElementById("t_uid").value=='' ) {
		//alert("Please enter Username");
		document.getElementById('error_t_uid').style.display='block';
		/*$('#error_t_uid').fadeOut(4000,function(){})*/
		return false;
		//return false;
	}
	if(document.getElementById("t_pwd").value=='' ) {
		//alert("Please enter Password");
		//return false;
		document.getElementById('error_t_pwd').style.display='block';
		/*$('#error_t_pwd').fadeOut(4000,function(){})*/
		return false;
	}
	userid=document.getElementById("t_uid").value;
	password=document.getElementById("t_pwd").value;

	url="/comments/hyperloginsession.cfm?uid="+userid+"&pwd="+password+"&twitterlogincheck=true";//alert(url)
	$.post(url,{},function(data) {
			array=data.split(" <!-");
			if(array[0]=='Could not authenticate you') {
				//alert('Please Enter Correct Username and Password');
				document.getElementById('notificationdiv_signup').style.display='block';
				document.getElementById('notificationdiv_signup').innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Please Enter Correct Username and Password</div>';
				/*$('#notificationdiv_signup').fadeOut(4000,function(){})*/
				document.getElementById('t_uid').focus();
				return false;
			}
			else {
				twitterdata=array[0].split('~~');
				document.getElementById('twitterId').value=twitterdata[0];
				document.getElementById('Name').value=twitterdata[1];
				document.getElementById('username').value=twitterdata[2];
				
				turl="/comments/hyperloginsession.cfm?uid="+twitterdata[0]+"&hyperlogin=twitter";
				if(twitterdata[0]!=''){
					//getRequest(url,'facebooksucess','Success');
					$.post(turl, { },
					function(data){
						  arrays=data.split(' <!-');
						// alert("Data Loaded: " + arrays[0]);return false;
						if(arrays[0]=='Logged In') {
							//alert("Logged In Successfully");
							window.location.reload()
						}
						else if(arrays[0]=='Account Not Activated') {
							//alert("Account Not Activated");
							document.getElementById('notificationdiv_signup').style.display='block';
							document.getElementById('notificationdiv_signup').innerHTML='<br clear="all" /><div class="warning" style="margin-top: 10px;">Account Not Activated</div>';
							/*$('#notificationdiv_signup').fadeOut(4000,function(){})*/
							document.getElementById('t_uid').focus();
							return false;
							return false;
						}
						else{
							document.ftsignup.submit();
						}
						 
					   });
				}return false;
				
			}
		}
	);
	
}

/***************************************twitter code ends*************************************************/
			
			
function loginpopupdivclose(ids) {
	document.getElementById(ids).style.display='none';
	return;
	/*if(ids!='')
	{
		var b=ids.indexOf("loginalertforlike");
		if(b == 0)
		{
			document.getElementById('changtext').innerHTML='like';
		}
		var c=ids.indexOf("loginalertfordislike");
		if(c == 0)
		{
			document.getElementById('changtext').innerHTML='dislike';
		}
		var d=ids.indexOf("loginalertforreply");
		if(d == 0)
		{
			document.getElementById('changtext').innerHTML='reply';
		}
		var e=ids.indexOf("loginalertforabuse");
		if(e == 0)
		{
			document.getElementById('changtext').innerHTML='abuse';
		}
	}*/
}
			
			


function updateNotification(div_id, msg){
	$("#"+div_id).show();
	$("#"+div_id).html(msg);
	$("#"+div_id).fadeIn(1000, function () {  });
	$("#"+div_id).css("margin-bottom", "15px");
}

$("#notificationdiv").hide();
