var cookieEnabled = (navigator.cookieEnabled) ? true : false;
var browser=navigator.appName
if ( cookieEnabled == false )
	window.location = "/missingjavascript.html";


	errorCookie=Get_Cookie("error_login_cookie");

	function Get_Cookie(name) 
	{	
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) ); 
	}


	function loadComments (topic_id, content_id, main_site_id, offset, div_id, total_comments )
	{

			var url='/api/web18/apiFetchComments.php';
			var parametri="content_id="+content_id+"&topic_id="+topic_id+"&main_site_id="+main_site_id+"&offset="+offset+"&total_comments="+total_comments;

			$("load-"+offset+"-more").html("<div align='center'><img src='http://static.ibnlive.com/pix/comments-cmsmodule/loadinfo.net.gif' border='0'></div>");

			$.ajax({	
				  type: "GET",
				  url: url,
				  data: parametri,
				  success: function(msg, textStatus, XMLHttpRequest){
						//alert ("Div id :" +div_id+" offset "+offset );
					  document.getElementById("load-"+offset+"-more").style.display = 'none';
					  document.getElementById(div_id).style.display = 'block';
					  document.getElementById(div_id).innerHTML=msg;
				  }
			  });
	}

	function getDefaultEvent()
	{
		var postId=Get_Cookie('MESSAGE_POSTID');
		var eventId=Get_Cookie('MESSAGE_EVENT');
  	    
		var d = new Date();
		document.cookie = "MESSAGE_POSTID=1;expires=" + d.toGMTString() + ";" + ";";
		document.cookie = "MESSAGE_EVENT=1;expires=" + d.toGMTString() + ";" + ";";
		
		if(errorCookie!="")
		{
			//displays error page..
			var field="popUpLogin"+postId;
			var url='/conversations/popuplogin.php';
			var parametri="post_id="+postId+"&element="+eventId+"&errorMessage="+errorCookie;			
			
			$.ajax({	
				  type: "POST",
				  url: url,
				  data: parametri,
				  success: function(msg){
					  document.getElementById(field).style.display = 'block';
					  document.getElementById(field).innerHTML=msg;
				  }
			  });
		}
		else
		{
			if(postId==0)
			{
				window.location.href = "/conversations/post/####TOPIC_ID####.html";
			}
			else if(postId>0)
			{
				var cookie = Get_Cookie("POST_DATA_IBN");
				if(cookie!='')
				{
					if(eventId==5)
					{
						field='####DEFAULT_FIELD####';
						document.getElementById(field).style.display = 'block';
					}
					else
					{
						url='####DEFAULT_FILE####';
						parametri='####DEFAULT_URL####';
						$.ajax({	
							  type: "POST",
							  url: url,
							  data: parametri,
							  success: function(msg){
								  document.getElementById('####DEFAULT_FIELD####').style.display = 'block';
								  document.getElementById('####DEFAULT_FIELD####').innerHTML=msg;
  							  }
						  });
					}
				}
				else
				{
					document.getElementById('####DEFAULT_FIELD####').style.display = 'block';
				}
			}
		
		}
	}
	
	//$(document).ready(function() 
	//{
	//	getDefaultEvent();
	//	funcLoading(####TOPIC_ID####, '/conversations/save_topic_hits.php');
	 //});

	//$(document).ready(function() 
	//{	
	//	loadLoginContainer ('0', '116978'); //####TOPIC_ID####);
	 //});

	function loadLoginContainer (post_id, topic_id)
	{	
		//alert ("Default : "+post_id+" "+topic_id+" Cookie value "+ Get_Cookie("POST_DATA_IBN") );
		if ( Get_Cookie("POST_DATA_IBN")== null  )
		{		
			url="/conversations/popuploginArticle.php";
			parametri="topic_id="+topic_id+"&post_id="+post_id+"&element=0";
			field="popUpLogin"+post_id;
					
			$.ajax({	
			  type: "POST",
			  url: url,
			  data: parametri,
			  success: function(msg){
				//alert (msg);
			  	//document.getElementById(field).style.display = 'block';
				  document.getElementById(field).innerHTML=msg;
			  }
	 	       });
			return false;
		}	
	}	 
	
	function validateCookie (post_id, arg, web18_user_id, web18_user_name, topic_id)
	{
		//alert ("validate post id  "+post_id+" Cookie "+cookie);
		var cookie = Get_Cookie("POST_DATA_IBN");
		//if ( post_id != 0 ){
			$("div[id^=popUpReplyPost]").hide();
			$("div[id^=popUpRating]").hide();
			$("div[id^=popUpTrackBorder]").hide();
			$("div[id^=popUpOffensive]").hide();
			$("div[id^=popUpTrackTopic]").hide();
			$("div[id^=popUpLogin]").hide();
		//}
		
		//document.getElementById("popUpLogin0").style.display = 'block';
		//By default make it on
		$("#popUpReplyPost0").show();


		if ( cookie == null  )
		{		
			url="/conversations/popuploginArticle.php";
			parametri="topic_id="+topic_id+"&post_id="+post_id+"&element="+arg;
			field="popUpLogin"+post_id;
					
			//$("#popUpLogin0").ajaxSend(function(evt, request, settings){
			 //       $(this).html("<div class='cblueBox'><b>Loading..</b>.</div>");
		      //});
			//alert ("----------"+field);
			$.ajax({	
			  type: "POST",
			  url: url,
			  data: parametri,
			  success: function(msg){
			  	document.getElementById(field).style.display = 'block';
				  document.getElementById(field).innerHTML=msg;
			  }
	 	       });


			return false;
		}
		
			
		switch (arg)
		{

			case 1:				
				url="/conversations/replypostArticle.php";
				parametri="post_id="+post_id+"&web18_user_id="+web18_user_id+"&web18_user_name="+web18_user_name+"&TOPIC_ID="+topic_id;
				field="popUpReplyPost"+post_id;
				break;	
			case 2:
					url="/conversations/rating.php";
					parametri="post_id="+post_id+"&web18_user_id="+web18_user_id+"&web18_user_name="+web18_user_name+"&TOPIC_ID="+topic_id;
				field="popUpRating"+post_id;
				break;				
			case 3:
					url="/conversations/tracking.php";
					parametri="post_id="+post_id+"&web18_user_id="+web18_user_id+"&web18_user_name="+web18_user_name+"&TOPIC_ID="+topic_id;
				field="layer"+post_id;	
				break;
			case 4:
					url="/conversations/offensive.php";
					parametri="post_id="+post_id+"&web18_user_id="+web18_user_id+"&web18_user_name="+web18_user_name+"&TOPIC_ID="+topic_id;
				field="popUpOffensive"+post_id;
				break;
			case 5:
				document.getElementById("layer").style.display='block';
				return;
				break;
			case 6:
				window.location.href = "/conversations/post/"+topic_id+".html";
				return;
				break;	
		}
		
		$.ajax({	
			  type: "POST",
			  url: url,
			  data: parametri,
			  success: function(msg){
				  document.getElementById(field).style.display = 'block';
				  document.getElementById(field).innerHTML=msg;
			  }
		  });
		  	
		

	}
	
	
	function procesareForm(option, post_id, web18_user_id, web18_user_name, topic_id, arg, arg2 )
	{
		//alert (post_id);
	
		if ( (Get_Cookie("POST_DATA_IBN") == null) && (post_id ==0) ) {
			$("#popUpReplyPost0").hide();
			$("#popUpLogin0").show();			
			$("#popUpReplyPost0").slideDown("slow");
			return;
		}
	
		  var url_option = Array ("/conversations/submitReplyPost.php", "/conversations/submitOfRating.php", "/conversations/submitTrackBorder.php", "/conversations/submitOffensive.php", "/conversations/submitTrackTopic.php");	
		  
		  var tempInnerHTML = Array ("popUpReplyPost", "popUpRating", "popUpTrackBorder", "popUpOffensive",  "popUpTrackTopic");	
	
		if ( option == 3 || option == 4 || option == 5)
		{
			  var data = "";
			  var obj = document.getElementsByName(arg);

			  for ( var i=0 ; i < obj.length ; i++ ) {
				  if ( (obj[i].checked) == true ) {
					  data = obj[i].value; continue; 
				  }
			  }
			  if ( data == "" )
			  {
			 	alert ("Please select an option");
				return false;
			  }			  
			 arg = data;
		}
	
	  	 if ( option == 5 )
			 var temp = tempInnerHTML[ option - 1 ]; 
		  else
			  var temp = tempInnerHTML[ option - 1 ]+post_id;
			  
		arg3 = '';
		if(option ==1)
		{
			argArr = arg2.split("#");
			arg2 = argArr[0];
			arg3 = argArr[1];
			
			var data = "";
			  var obj = document.getElementsByName(arg2);
	
			  for ( var i=0 ; i < obj.length ; i++ ) {
				  if ( (obj[i].checked) == true ) {
					  data = obj[i].value; continue; 
				  }
			  }
			 arg2 = data;
			 if(document.getElementById(arg3).checked == true)
			 	arg3 = 1;
			else
				arg3 = 0;
		}
		  var parametri = "post_id="+post_id+"&web18_user_id="+web18_user_id+"&web18_user_name="+web18_user_name+"&topic_id="+topic_id+"&arg="+escape(arg)+"&arg2="+arg2+"&arg3="+arg3;
		  	
		if ( (option == 1) && (document.getElementById("post_text"+post_id).value == "") )
		{
			alert ("Please Put some comments");
			document.getElementById("post_text"+post_id).focus();
		}	  
		else
		{
  			if ( option == 3 )  // Track Border Layer to Blocked
			{
				document.getElementById("layer"+post_id).style.display='none';				
				document.getElementById("popUpTrackBorder"+post_id).style.display='block';				
			}

  			if ( option == 5 )  // Track Topic Layer to Blocked
			{
				document.getElementById("layer").style.display='none';				
				document.getElementById("popUpTrackTopic").style.display='block';				
			}

			$.ajax({	
			  type: "POST",
			  url: url_option[ option - 1 ],
			  data: parametri,
			  success: function(msg)
			  {	
			  
				if (msg == 0 )
					window.top.location.href = '?topic_id='+topic_id;
				else	
					$('#'+temp).attr( "innerHTML", msg );
			  }
		  });
		}  
	 }
	 function validateLogin ( form , post_id, element, topic_id)
	 {
		//alert (post_id);
		var login    = document.getElementById("login"+post_id).value;
		var password = document.getElementById("password"+post_id).value;

	 	if ( login == "" ) {
			alert ("Login should not be blank"); document.getElementById("login"+post_id).focus();
			return false;
		}
		if ( password == "" ) 		{
			alert ("Password should not be blank");  document.getElementById("password"+post_id).focus();
			return false;
		}

		$("#popUpLogin"+post_id).html("<div align='center'><img src='http://static.ibnlive.com/pix/comments-cmsmodule/loadinfo.net.gif' border='0'></div>");

		$.ajax({	
			  type: "POST",
			  url: "/ibnregistration/loginverify_ibnArticle.php",
			  data: "login="+login+"&password="+password,
			  success: function(msg){				
				if (msg == 1) {
					document.getElementById("popUpLogin"+post_id).style.display = 'none';		
				       //document.getElementById("popUpReplyPost"+post_id).style.display = 'block';		

					//After Login by default it will open the top container
					//validateCookie (0, 1, 0, '0', topic_id);
					//By default make it on
					$("#popUpReplyPost0").show();
					$("#popUpReplyPost0").focus();
				}
				else
					document.getElementById("popUpLogin"+post_id).style.display = 'block';

				 // document.getElementById(field).innerHTML=msg;
			  }
		  });

		var today = new Date();
		today.setTime( today.getTime() );

		var expires_date = new Date( today.getTime() + (20*1000) );

		document.cookie="MESSAGE_POSTID="+post_id+";expires="+expires_date.toGMTString();
		document.cookie="MESSAGE_EVENT="+element+";expires="+expires_date.toGMTString();
	 }


	 function checkMaxInput(frm, post_id, topic_id)
	 {	
		if ( Get_Cookie("POST_DATA_IBN") != null)	{
	 		maxLen = 2000;
			if ( document.getElementById(post_id).value.length > maxLen)
				document.getElementById(post_id).value.length = document.getElementById(post_id).value.substring(0, maxLen);
			else
				frm.charnum.value = maxLen - document.getElementById(post_id).value.length;
		}
		else
		{
			$("#popUpReplyPost0").hide();
			$("#popUpLogin0").show();			
			$("#popUpReplyPost0").slideDown("slow");
		}
	 }
	 
	$(document).ready(function() 
	{	
		$('#post_text0').click(function () {
			if ( Get_Cookie("POST_DATA_IBN") == null)	{
				$("#popUpReplyPost0").hide();
				$("#popUpLogin0").show();			
				$("#popUpReplyPost0").slideDown("slow");
			}
		});
	});

	  function funcPolling(arg1, arg2, topic_id)
	 {
	 	var obj = document.getElementsByName ("poll");
		
		for ( var i=0; i < obj.length ; i++ ) 
		{
			if ( obj[i].checked == true)
			{
				$.ajax({	
				  type: "POST",
				  url: "/conversations/poll_submit.php",
				  data: "arg1="+obj[i].value+"&arg2="+arg2+"&topic_id="+topic_id,
				  success: function(msg){
					$('#ajax_poll').attr( "innerHTML", msg );
				  }
			  });
				return false;
			}			
		}
		alert ("Please select a poll option");
		return false;		
	 }
	 
var state = 'none';
function changeImage(imgName, thread_box)
{
	if(document[imgName].alt == 'less')
	{
		document[imgName].alt = 'more';
		document[imgName].src = '/pix/conversion/plus.gif';
		state = 'none';
	}
	else
	{
		document[imgName].alt = 'less';
		document[imgName].src = '/pix/conversion/minus.gif';
		state = 'block';
	}
	if (document.all) 
	{ //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + thread_box + ".style.display = state");
	}
	
	if (document.layers) 
	{ //IS NETSCAPE 4 or below
		document.layers[thread_box].display = state;
	}
	if (document.getElementById && !document.all) {
		hza = document.getElementById(thread_box);
		hza.style.display = state;
	}
}

function changeTopicType(val, TOPIC_ID)
{
	var fileName='';
	switch(val)
	{
		case 'withthreading':
					fileName = "/conversations/thread/"+TOPIC_ID+".html";	
					break;	
		case 'nothreading':
					fileName = "/conversations/thread/flat/"+TOPIC_ID+".html";	
					break;
		case 'oldestfirst':
					fileName = "/conversations/thread/flat-asc/"+TOPIC_ID+".html";	
					break;	
		case 'toprated':
					fileName = "/conversations/thread/rate/"+TOPIC_ID+".html";	
					break;
	}
	window.location.href=fileName;
}

