function openPopUp(id,data)
{
 $(id).show();
}

function closePopUp(id,data)
{
 $(id).hide();
}

function popuserlogin(rurl)
{
	$(function() {
	 Boxy.load(BASEPATH + "ajaxpopup/loadPopLogin",{modal:true,title: 'Oops! You must log in to do that!',center:true ,
					behaviours: function(c) {
						c.find('#ajaxloginform').submit(function() {
							var dataString = $("#ajaxloginform").serialize();
							//Boxy.get(this).setContent("<div style=\"width: 300px; height: 300px\">Sending...</div>");
							$.ajax({
								type: "POST",
								url: BASEPATH+"ajaxpopup/postPopLogin",
								data: dataString,
								success: function(msg) {
									if(msg=="1")
									{
										if(rurl !="")
										{
											window.location= rurl;
										}
										else
										{
											window.location.reload();
										}	
					
									}
									else
									{
										$("#login_failed").html("Oops! Your Login is Incorrect.");
									}
								
								}
							});
						
							return false;
						});
					}
				},
				{}
		)
  
	});
	
}

function select_twitter_template(message)
{
	$(function() {
		
		Boxy.load(BASEPATH + "ajaxpopup/loadPopTwitter", 
				{
					modal:true,title: 'Share On Twitter',center:true,
					afterShow: function() {
						$("textarea#twitter_message").text(message);
					},
					behaviours: function(c) {
						c.find('#ajaxtwitterform').submit(function() {
							var dataString = $("#ajaxtwitterform").serialize();
							//Boxy.get(this).setContent("<div style=\"width: 300px; height: 300px\">Sending...</div>");
							$.ajax({
								type: "POST",
								url: BASEPATH+"ajaxpopup/post_to_twitter",
								data: dataString,
								success: function(msg) {
									if(msg=="1")
									{
										$("#ajaxtwitterform").html("This has been added to your twitter account.");
									
									}else
									{
										$("#twitter_login_failed").html(msg);
									}
								
								}
							});
						
							return false;
						});
					}
					
				},
				{}
				);
		
	});
}

function addSubscription(reference_type,reference_id,  user_id)
{
	$.ajax({
	  type: "POST",
	  url: BASEPATH + "Ajax/addTosubscription/"+ reference_id +"/reference_type/"+ reference_type +"/user_id/"+ user_id ,
	  success: function(msg){
		
		if (msg=='Sorry! You already subscribed it.') {
			Boxy.alert(msg, null, {title: "Subscribe",center:true});
		}
		
		if (msg=='You have Successfully subscribed it') {
			 Boxy.alert(msg, null,{title: 'Subscribe',center:true});
		}
		
		if (msg=='Sorry! You are not logged in.') {
			Boxy.alert(msg, null, {title: "Subscribe"});
		}	
		//else {
		//  blockMoment("<h3>" + msg + "</h3>", 2);
		//}
		
	  }
	});
}

function openForwardToFriendsDialog(ref_type,ref_id , rurl)
{
	var pos_top =null;
	var pos_left =null;
	$(function() {
		
		Boxy.load(BASEPATH + "ajaxpopup/load_pop_forward_to_friend", 
				{
					modal:true,fixed:false,title: 'Share / Forward to Friends & Colleagues',center:true,
					afterShow: function() {
					//var pos = this.getPosition();
						var b = this.boxy[0];
						pos_top = b.offsetTop;
						pos_left = b.offsetLeft;
					},
					behaviours: function(c) {
						 	

							$("#personalized_message").val('Message from your friend:\n');
							c.find('#addNameBox').click(function() {	
								
								$('#namelist').append('<tr><td><input class="ajaxBox_input" type="text" name="name[]" /></td><td><input class="ajaxBox_input" type="text" name="email[]" /></td></tr>');
							});
							c.find('#shareForm').submit(function() {
								
								var dataString = $("#shareForm").serialize();
								//Boxy.get(this).setContent("<div style=\"width: 300px; height: 300px\">Sending...</div>");
								$.ajax({
									type: "POST",
									url: BASEPATH+"ajaxpopup/post_share",
									data: dataString,
									dataType: "json", 
									success: function(msg) {
									
										if(msg.success == "1")
										{
											var content = msg.message;
											var email_list = msg.list;
											
											$('#form_document').html(content+email_list);
											pos_top = pos_top+200;
											pos_left =pos_left +200;
											$('.boxy-wrapper').css({'top': pos_top+'px' ,'left': pos_left+'px'} );
										}
										else
										{
											//failed
											$('#share_failed').html(msg.message);
										}
								
									}
								});
								return false;
							});
					}

				},
				{ref_id: ref_id,ref_type:ref_type,rurl:rurl}
				
		);
		
	});
}

function importEmail()
{
	showPlaxoABChooser('imported_email', callBackURL);
	if($('#imported_email').length>0)
	{
		$('#importer').show();
	}
	return false;
}
//group make new captch image
function recaptchatakeover() {
	  var cap = document.getElementById("capchato");
	  //cap.load("<?php echo site_url("auth/regenerate_captcha")?>", {'val' : 0 });
	  $("#capchato").load(BASEPATH+"auth/regenerate_captcha/238", {'val' : 0 });
}

//Unsubscribe user
function deleteSubscription(reference_id, reference_type)
{
	$.ajax({
	  type: "POST",
	  url: BASEPATH + "ajaxpopup/deleteSubscription/"+ reference_id +"/reference_type/"+ reference_type ,
	  success: function(msg){
		 Boxy.ask(msg, ["Close"], null, {title: "Unsubscribed",closeable:true,closeText:"[Close]"});
	  }
	});
}
function addToFriends(user_id) {
	  $.ajax({
		type: "GET",
		url: BASEPATH + 'friends/create/' + user_id,
		success: function(msg) {
		
			  if(msg.substring(0, 5) == 'ERROR') { 
				  if(msg.substring(6, msg.length-1) == ''){
					//  $("#friends_popup").html("Oops, try again !!!").show();
					  Boxy.alert("Oops, try again !!!", null, {title: "Add To Connections",center:true});
				  }else{
					  var message = msg.substring(6, msg.length);
					  Boxy.alert(message, null, {title: "Add To Connections",center:true});
					 // $("#friends_popup").html(msg.substring(6, msg.length)).show();            
				  }    
			  }else{
				  //$("#friends_popup").html().show();
				  Boxy.alert("Your invitation has been sent to the user successfully!", null, {title: "Add To Connections",center:true});
			  }

	  }});
}

function deleteGroupMember(groupId, id)
{
	if (confirm("Are you sure you want to remove this group membership ?")) {
		$.ajax({
		  type: "POST",
		  data: {'groupId':groupId},
		  url: BASEPATH + "groups/deleteGMember" ,
		  success: function(msg){
		 
		  $('#' + id).show(); 
			if(msg == 'success')
			{
				Boxy.ask("Group membership removed successfully !!!", ["Close"], null, {title: "Delete Group Member",closeable:true,closeText:"[Close]"});
				$('#' + id).css('background', '#eee');
				$('#' + id).fadeOut('normal');
			}
			else
			{
			     Boxy.ask("Group membership cannot be removed. Try later!!!", ["Close"], null, {title: "Delete Group Member",closeable:true,closeText:"[Close]"});
				
			}
			
		  }
		});
	} 	
}

function addToRVSP(reference_id, reference_type, calendar) {
	  $.ajax({
		type: "GET",
		url: BASEPATH + 'Ajax/addRVSP/all/reference_type/' + reference_type + '/reference_id/' + reference_id + '/calendar/' + calendar,
		success: function(msg) {
		  if(msg == 0){
			
				Boxy.ask("Oops, try again !!!", ["Close"], null, {title: "Add To RVSP",closeable:true,closeText:"[Close]"});
		  } else if (msg == 2) {
				Boxy.ask("You already added this item to your calendar. <a href='"+BASEPATH+"events/viewCalendar/"+calendar+"'>click here</a> to view your calendar", ["Close"], null, {title: "Add To RVSP",closeable:true,closeText:"[Close]"});
		  } else if (msg == 3) {
				Boxy.ask("You do not have a "+calendar+" account, <a href='"+BASEPATH+"auth/register/"+calendar+"'>click here</a> to create one.", ["Close"], null, {title: "Add To RVSP",closeable:true,closeText:"[Close]"});
		  } else {  
				Boxy.ask("You have successfully added this item to your calendar! <a href='"+BASEPATH+"events/viewCalendar/"+calendar+"'>click here</a> to view your calendar", ["Close"], null, {title: "Add To RVSP",closeable:true,closeText:"[Close]"});
			}

	  }});
}
