$(document).ready(function()
{
	// the location of php file where ajax request will be implemented
	var sUrl = "./leftpanel_ajax.php";
	var locationWindow = window.location.pathname+window.location.search+window.location.hash;

	// This function will be called when a user sends the link with message for particular content to specify email address
	$("#email_this_page_send_btn").click(function()    
 {
	  $('li#sharedSection').prepend('<span class="icpl_loading">loading..</span>');
  // email validation expression
  var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)+$/;
  // collecting email and message to post as string
  var str   = $("form#email_this_page_form_id").serialize();
  // getting the value of email to validate
  var emails  = $("#txt_email_this_page_email_id").val();
  // split email if there is more email address
  var emailArr = emails.split(',');
  // getting the value of message box for validation if empty
  var message  = $("#txt_email_this_page_message_id").val();
  var test  = $("#test_hdn").val();
  // adding page url to send the specific content user need to share and action to track ajax request
  str    += "&link="+document.URL+'&lAction=emailSend';
  str    += "&test="+test;
 
  // checkig if email box is empty
  
  if(emails.length < 1){
	document.getElementById('sharedSection').innerHTML = "Please enter the e-mail address!";
   //alert("Please enter the e-mail address!");
   $("#txt_email_this_page_email_id").focus()
   return false;
  }
 
  // checking if any email address is invalid
  for(var i=0; i<emailArr.length; i++){
   emailArr[i] = (emailArr[i].replace(/^\s+/,'')).replace(/\s+$/,'');
   if(!emailArr[i].match(emailRegEx)){
	   document.getElementById('sharedSection').innerHTML = "Please enter valid e-mail address!";
   // alert('Invalid format!');
    $("#txt_email_this_page_email_id").focus();
    return false;
   }
  }
  
  // checking if message box is empty
  if(message.length < 2){
	  document.getElementById('sharedSection').innerHTML = "Please enter the message!";
  // alert("Please enter your message!");
   $("#txt_email_this_page_message_id").focus();
   return false;
  }
  
  // sending request to PHP 
  $.ajax({ type: "POST",
   url: sUrl,
   cache: false,
   data:str,
   dataType: "string",
   success:function(data){
    // showing message
    //alert(data);
	document.getElementById('sharedSection').innerHTML = data;
	document.getElementById('txt_email_this_page_email_id').value = '';
	document.getElementById('txt_email_this_page_message_id').value = '';
   }
    });
  return false;
 });
 

 // This function will be called when a user click on report abuse button
 
 $("#report_abuse_message_btn").click(function()    
 {
	  $('li#abuseSection').prepend('<span class="icpl_loading">loading..</span>');
  // getting the value of message box for validation if empty
  var message = $("#txt_report_abuse_message_id").val();
  // collecting  message to post as string
  var str = $("form#report_abuse_form_id").serialize();
  // adding page url to send the specific content to admin and action to track ajax request
  str += "&link="+document.URL+'&lAction=reportAbuse';
 
  // checking if message box is empty
  if(message.length < 1){
	  document.getElementById('abuseSection').innerHTML = "Please enter your message!";
   //alert("Please enter your message!");
   $("#txt_report_abuse_message_id").focus();
   return false;
  }
 
  // sending request to PHP 
  $.ajax({ type: "POST",
   url: sUrl,
   cache: false,
   data:str,
   dataType: "string",
   success:function(data){
    // showing message
    document.getElementById('abuseSection').innerHTML = data;
	document.getElementById('txt_report_abuse_message_id').value = '';
   }
    });
  return false;
 });
 
 
	// This function will be called when a user likes to send a message to the owner of content
	$("#txt_send_message_btn").click(function()    
	{
		// getting the value of message box for validation if empty
		var message = $("#txt_send_message_id").val();
		// collecting  message to post as string

		var str = "&link="+document.getElementById('pageLink').value;
		
		// adding page url to send the information about content
		str += "&messageTxt="+message+'&lAction=messageSend';
		// checking if message box is empty
		
		if(message.length < 2)
		{
			document.getElementById('mesSection').innerHTML = "Please enter your message!";
			// alert("Please enter your message!");
			$("#txt_send_message_id").focus();
			return false;
		}
 
		// sending request to PHP 
		$.ajax({ type: "POST",
		   url: sUrl,
		   cache: false,
		   data:str,
		   dataType: "string",
		   success:function(data){
			// showing message
			
			 document.getElementById('mesSection').innerHTML = data;
			 document.getElementById('txt_send_message_id').value = '';
		   }
		});
		return false;
	});
 
 // This function used to pring the content page
 $("#detail_print").click(function()    
 {
  print();
 });
 
 // This funtion used to add particular content in favourite list as temprary for the user and he can save it.
 $("#add_to_favourite").click(function()    
 {
	var userid = getCookie('memberID');
	if(userid==0)
	{
		showItemEditForm("login_div"); 
		$("#login_div").show().load( 'member.php?action=show_login_form&relocate=' + encodeURIComponent( locationWindow) );	
	}else
	{
		showLoaderImg('showFavLoader');
		// collecting the info of conntent to add as favourite
		//var str = $("#hidden_content_id").val();
		// adding page url to send the information about content
		str = '&lAction=favouriteAdd&link='+locationWindow;
		
		// sending request to PHP 
		$.ajax({ type: "POST",
			url: sUrl,
			cache: false,
			data:str,
			dataType: "string",
			success:function(data){
				// showing message
				document.getElementById('detailPageLoaderMsg').style.display='block';
				document.getElementById('detailPageLoaderMsg').innerHTML = data;
				hideLoaderImg('showFavLoader');
			}
		});
	}
	return false;
 });

 
 
});
 
function ismaxlength(txtObj){
//	var txtNum = txtObj.value;
	return;

}

// This funtion used to add particular content in favourite list as temprary for the user and he can save it.
function add_to_rating(rateNo)
{
	var sUrl = "./leftpanel_ajax.php";
	var locationWindow = window.location.pathname+window.location.search+window.location.hash;
	var userid = getCookie('memberID');
	if(userid==0 || userid == '')
	{
		showItemEditForm("login_div"); 
		$("#login_div").show().load( 'member.php?action=show_login_form&relocate=' + encodeURIComponent( locationWindow) );	
	}else
	{
		showLoaderImg('showRateLoader');
		// collecting the info of conntent to add as favourite
		//var str = $("#hidden_content_id").val();
		// adding page url to send the information about content
		str = '&rate='+rateNo+'&lAction=ratingAddModule&link='+locationWindow;
		
		// sending request to PHP 
		$.ajax({ type: "POST",
			url: sUrl,
			cache: false,
			data:str,
			dataType: "string",
			success:function(data){
				var starShow = '';
				for(i=1;i<=5;i++)
				{
					tmpStar = document.getElementById('star_'+i);
					if(i<=rateNo){
						tmpStar.src = "images/rat_star21.gif";
					}else{
						tmpStar.src = "images/rat_star22.gif";
					}
					$(tmpStar).removeAttr('onmouseout');
					$(tmpStar).removeAttr('onmouseover');
				}
				// showing message
//				alert(data);
				document.getElementById('detailPageLoaderMsg').style.display='block';
				document.getElementById('detailPageLoaderMsg').innerHTML = data;
				
				hideLoaderImg('showRateLoader');
			}
		});
	}
	return false;
}

function sendReportSection(section)
{
	var locationWindow = window.location.pathname+window.location.search+window.location.hash;
	var userid = getCookie('memberID');
	if(userid==0)
	{
		showItemEditForm("login_div"); 
		$("#login_div").show().load( 'member.php?action=show_login_form&relocate=' + encodeURIComponent( locationWindow) );	
	}else
	{
		showItemEditForm("login_div"); 
		$("#login_div").show().load( section+'.php?linkName=' + locationWindow);	
	}
	return false;

}

function sendMessageModule()
{
	var sUrl = "./leftpanel_ajax.php";
	//var locationWindow = window.location.pathname+window.location.search+window.location.hash;
	var message = $("#txt_send_message_id").val();
	// collecting  message to post as string
	var str = "&link="+document.getElementById('pageLink').value;
	
	// adding page url to send the information about content
	str += "&messageTxt="+message+'&lAction=messageSend';
	// checking if message box is empty
	
	if(message.length < 2)
	{
		document.getElementById('mesSection').innerHTML = "Please enter your message!";
		// alert("Please enter your message!");
		$("#txt_send_message_id").focus();
		return false;
	}
	// sending request to PHP 
	$.ajax({ type: "POST",
	   url: sUrl,
	   cache: false,
	   data:str,
	   dataType: "string",
	   success:function(data){
		// showing message
		
		 document.getElementById('mesSection').innerHTML = data;
		 document.getElementById('txt_send_message_id').value = '';
	   }
	});
	return false;
}

function reportAbuseModule()
{
	var sUrl = "./leftpanel_ajax.php";
	// getting the value of message box for validation if empty
	var message = $("#txt_report_abuse_message_id").val();
	// collecting  message to post as string
	var str = '&abuseReason='+document.getElementById('abuseReason').value;
	// adding page url to send the specific content to admin and action to track ajax request
	str += "&link="+document.getElementById('pageLink').value;
	str += "&message="+message+"&lAction=reportAbuse";
	
	
	// checking if message box is empty
	if(message.length < 1){
		document.getElementById('abuseSection').innerHTML = "Please enter your message!";
		//alert("Please enter your message!");
		$("#txt_report_abuse_message_id").focus();
		return false;
	}
	
	// sending request to PHP 
	$.ajax({ type: "POST",
		url: sUrl,
		cache: false,
		data:str,
		dataType: "string",
		success:function(data){
			// showing message
			document.getElementById('abuseSection').innerHTML = data;
			document.getElementById('txt_report_abuse_message_id').value = '';
		}
	});
	return false;
}

function emailPageModule()
{
	var sUrl = "./leftpanel_ajax.php";

	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)+$/;
	// collecting email and message to post as string
	//var str   = $("form#email_this_page_form_id").serialize();
	// getting the value of email to validate
	var emails  = $("#emailToSend").val();
	
	// split email if there is more email address
	var emailArr = emails.split(',');
	// getting the value of message box for validation if empty
	var message  = $("#txt_email_page_message_id").val();
	var str="&message="+message+"&emailIds="+emails;
	// adding page url to send the specific content user need to share and action to track ajax request
	str    += "&link="+document.URL+'&lAction=emailSend';
	
	// checkig if email box is empty
	if(emails.length < 1)
	{
		document.getElementById('sharedSection').innerHTML = "Please enter the e-mail address!";
		$("#emailToSend").focus()
		return false;
	}
 
	// checking if any email address is invalid
	for(var i=0; i<emailArr.length; i++)
	{
		emailArr[i] = (emailArr[i].replace(/^\s+/,'')).replace(/\s+$/,'');
		if(!emailArr[i].match(emailRegEx))
		{
			document.getElementById('sharedSection').innerHTML = "Please enter valid e-mail address!";
			$("#emailToSend").focus();
			return false;
		}
	}
  
	// checking if message box is empty
	if(message.length < 2)
	{
		document.getElementById('sharedSection').innerHTML = "Please enter the message!";
		$("#txt_email_page_message_id").focus();
		return false;
	}
	
	// sending request to PHP 
	$.ajax({ type: "POST",
		url: sUrl,
		cache: false,
		data:str,
		dataType: "string",
		success:function(data){
    // showing message
    //alert(data);
	document.getElementById('sharedSection').innerHTML = data;
	document.getElementById('emailToSend').value = '';
	document.getElementById('txt_email_page_message_id').value = '';
   }
    });
  return false;

}