//*************************************************************************************
//*************************************************************************************
//*  Show/Hide Boxes
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() {

  $('#ajaxbox1').hide();  
  
  $('a#ajaxbox1-toggle').click(function() {
    $('#ajaxbox1').slideToggle(400);
    return false;
  });
  
   $('a#ajaxbox1-show').click(function() {
    $('#ajaxbox1').slideDown(400);
    return false;
  });
  
  $('a#ajaxbox1-hide').click(function() {
    $('#ajaxbox1').slideUp(400);
    return false;
  });
});

//*************************************************************************************
//*************************************************************************************
//*  Show/Hide Boxes
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() {
 // hides the slickbox as soon as the DOM is ready
  $('#ajaxbox2').hide();  
 // toggles the slickbox on clicking the noted link  
  $('a#ajaxbox2-toggle').click(function() {
    $('#ajaxbox2').slideToggle(400);
    return false;
  });
  $('a#ajaxbox2-show').click(function() {
    $('#ajaxbox2').slideDown(400);
    return false;
  });
  
  $('a#ajaxbox2-hide').click(function() {
    $('#ajaxbox2').slideUp(400);
    return false;
  });
});

//*************************************************************************************
//*************************************************************************************
//*  Show/Hide Boxes
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() {
 // hides the slickbox as soon as the DOM is ready
  $('#ajaxbox3').hide();  
 // toggles the slickbox on clicking the noted link  
  $('a#ajaxbox3-toggle').click(function() {
    $('#ajaxbox3').slideToggle(400);
    return false;
  });
  $('a#ajaxbox3-show').click(function() {
    $('#ajaxbox3').slideDown(400);
    return false;
  });
  
  $('a#ajaxbox3-hide').click(function() {
    $('#ajaxbox3').slideUp(400);
    return false;
  });
});


//*************************************************************************************
//*************************************************************************************
//*  Shows/hides threads
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() {

  $('#ajaxboxthread').hide();  

  $('a#ajaxboxthread-toggle').click(function() {
    $('#ajaxboxthread').slideToggle(400);
    return false;
  });
  $('a#ajaxboxthread-show').click(function() {
    $('#ajaxboxthread').slideDown(400);
    return false;
  });
  
  $('a#ajaxboxthread-hide').click(function() {
    $('#ajaxboxthread').slideUp(400);
    return false;
  });
});

//*************************************************************************************
//*************************************************************************************
//*  View different sections of a book in editbook
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() {

  $('#editBookOther').hide();   
  $('#editBookOld').hide();  

   $('a#editBookCurrent-show').click(function() {
    $('#editBookCurrent').show();
	$('#editBookOther').hide();
	$('#editBookOld').hide();
    return false;
  });
  
   $('a#editBookOld-show').click(function() {
    $('#editBookCurrent').hide();
	$('#editBookOther').hide();
	$('#editBookOld').show();
    return false;
  });
  
  $('a#editBookOther-show').click(function() {
    $('#editBookCurrent').hide();
	$('#editBookOther').show();
	$('#editBookOld').hide();
    return false;
  });
});

//*************************************************************************************
//*************************************************************************************
//*  Shows a section of the site
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() { 
	$('a.sectionHiddenShow').click(function() { 
    var id = $(this).attr('id'); 
	$('#sectionHiddenOpposite' + id).hide(500);
    $('#sectionHidden' + id).show(500);
	$("#sectionHiddenResult").load("ajaxactions.php?num1="+id+"&ajaxcase="+007+"");
    return false; 
    }); 
}); 

//*************************************************************************************
//*************************************************************************************
//*  Hides a section of the site
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() { 
$('a.sectionHiddenHide').click(function() { 
    var id = $(this).attr('id'); 
    $('#sectionHiddenOpposite' + id).show(500);
	$('#sectionHidden' + id).hide(500);
	$("#sectionHiddenResult").load("ajaxactions.php?num1="+id+"&ajaxcase="+008+"");
    return false; 
    }); 
}); 

//*************************************************************************************
//*************************************************************************************
//*  Toggles a section of the site
//*************************************************************************************
//*************************************************************************************
$(document).ready(function() { 
$('a.sectionHiddenToggle').click(function() { 
    var id = $(this).attr('id'); 
	$('#sectionHiddenOpposite' + id).toggle(500);
    $('#sectionHidden' + id).toggle(500); 
	$("#sectionHiddenResult").load("ajaxactions.php?num1="+id+"&ajaxcase="+009+"");
    return false; 
    }); 
}); 

//*************************************************************************************
//*************************************************************************************
//*  dhtml popups
//*************************************************************************************
//*************************************************************************************


function libPopup(id) {
	
	var offx = 50;
	var offy = 50;
	
  var scrolledX, scrolledY;
 if( self.pageYOffset ) {
   scrolledX = self.pageXOffset;
   scrolledY = self.pageYOffset;
 } else if( document.documentElement && document.documentElement.scrollTop ) {
   scrolledX = document.documentElement.scrollLeft;
   scrolledY = document.documentElement.scrollTop;
 } else if( document.body ) {
   scrolledX = document.body.scrollLeft;
   scrolledY = document.body.scrollTop;
 }

 var centerX, centerY;
 if( self.innerHeight ) {
   centerX = self.innerWidth;
   centerY = self.innerHeight;
 } else if( document.documentElement && document.documentElement.clientHeight ) {
   centerX = document.documentElement.clientWidth;
   centerY = document.documentElement.clientHeight;
 } else if( document.body ) {
   centerX = document.body.clientWidth;
   centerY = document.body.clientHeight;
 }

 var leftOffset = scrolledX + offx;
 var topOffset = scrolledY + offy;
 
 //var leftOffset = scrolledX + (centerX - offx) / 2;
 //var topOffset = scrolledY + (centerY - offy) / 2;

 document.getElementById("libPopup"+id+"").style.top = topOffset + "px";
 document.getElementById("libPopup"+id+"").style.left = leftOffset + "px";
 
 
 document.getElementById("libPopup"+id+"").style.display = "block";
 document.body.onscroll = function(){ 
 
  var scrolledX, scrolledY;
 if( self.pageYOffset ) {
   scrolledX = self.pageXOffset;
   scrolledY = self.pageYOffset;
 } else if( document.documentElement && document.documentElement.scrollTop ) {
   scrolledX = document.documentElement.scrollLeft;
   scrolledY = document.documentElement.scrollTop;
 } else if( document.body ) {
   scrolledX = document.body.scrollLeft;
   scrolledY = document.body.scrollTop;
 }

 var centerX, centerY;
 if( self.innerHeight ) {
   centerX = self.innerWidth;
   centerY = self.innerHeight;
 } else if( document.documentElement && document.documentElement.clientHeight ) {
   centerX = document.documentElement.clientWidth;
   centerY = document.documentElement.clientHeight;
 } else if( document.body ) {
   centerX = document.body.clientWidth;
   centerY = document.body.clientHeight;
 }

 var leftOffset = scrolledX + offx;
 var topOffset = scrolledY + offy;

 document.getElementById("libPopup"+id+"").style.top = topOffset + "px";
 document.getElementById("libPopup"+id+"").style.left = leftOffset + "px";
 
 };
 window.onscroll = function(){ 
 
	 var scrolledX, scrolledY;
 if( self.pageYOffset ) {
   scrolledX = self.pageXOffset;
   scrolledY = self.pageYOffset;
 } else if( document.documentElement && document.documentElement.scrollTop ) {
   scrolledX = document.documentElement.scrollLeft;
   scrolledY = document.documentElement.scrollTop;
 } else if( document.body ) {
   scrolledX = document.body.scrollLeft;
   scrolledY = document.body.scrollTop;
 }

 var centerX, centerY;
 if( self.innerHeight ) {
   centerX = self.innerWidth;
   centerY = self.innerHeight;
 } else if( document.documentElement && document.documentElement.clientHeight ) {
   centerX = document.documentElement.clientWidth;
   centerY = document.documentElement.clientHeight;
 } else if( document.body ) {
   centerX = document.body.clientWidth;
   centerY = document.body.clientHeight;
 }

 var leftOffset = scrolledX + offx;
 var topOffset = scrolledY + offy;

 document.getElementById("libPopup"+id+"").style.top = topOffset + "px";
 document.getElementById("libPopup"+id+"").style.left = leftOffset + "px";

	};
}



//*************************************************************************************
//*************************************************************************************
//*  Form validation - Sending a message in inbox
//*************************************************************************************
//*************************************************************************************

function checkFormInboxSendNew1(){
	
	returnvalue = true;
	
	var str2 = $("input#str2").val();
	var str3 = $("textarea#str3").val();
	var chks = document.getElementsByName('arr1[]');	
	
	if (str2 == '')	{ 
		returnvalue = false;
		$('#formErrorStr2').show();
	} else {
		$('#formErrorStr2').hide();
	}
	
	if (str3 == '')	{ 
		returnvalue = false;
		$('#formErrorStr3').show();
	} else {
		$('#formErrorStr3').hide();
	}
	
		var arrcheck = false;
		for (var i = 0; i < chks.length; i++) {
			if (chks[i].checked) {
				arrcheck = true;
			}
		}	
		if (arrcheck == false){
			returnvalue = false;
			$('#formErrorArr1').show();
		} else {
			$('#formErrorArr1').hide();
		}
	
	return returnvalue;
}

//*************************************************************************************
//*************************************************************************************
//*  Form validation - Replying to a message in inbox
//*************************************************************************************
//*************************************************************************************

function checkFormInboxReply(){
	
	returnvalue = true;

	var str3 = $("textarea#str3").val();

	if (str3 == '')	{ 
		returnvalue = false;
		$('#formErrorStr3').show();
	} else {
		$('#formErrorStr3').hide();
	}
	
	return returnvalue;
}

//*************************************************************************************
//*************************************************************************************
//*  Form validation - Setting a task as the editor
//*************************************************************************************
//*************************************************************************************

function checkFormManageBookTasks1()
{
	
	returnvalue = true;
	
	var chks = [];
	var chickens = [];
	
	var str100 = document.taskAssignForm.str100.value; //title
	var str200 = document.taskAssignForm.str200.value; //description
	var str4 = document.taskAssignForm.taskUsersCheckbox.checked; //assing to specific users
	var chks = document.getElementsByName('arr1[]');	//array of selected users
	var num2 = document.taskAssignForm.num2.value; //days
	var num5 = document.taskAssignForm.num5.value; //number of shareholders
	var str5 = document.taskAssignForm.str5.value; //corn
	
	var num1;

	for (var i=0; i<document.taskAssignForm.num1.length; i++)  {
		if (document.taskAssignForm.num1[i].checked)  {
		
			num1 = document.taskAssignForm.num1[i].value;
		
		}
	}
	
	if (str100 == '')	{ 
		returnvalue = false;
		$('#formErrorStr100').show();
	} else {
		$('#formErrorStr100').hide();
	}
	
	if (str200 == '')	{ 
		returnvalue = false;
		$('#formErrorStr200').show();
	} else {
		$('#formErrorStr200').hide();
	}
	
	if (str4 == true)	{ 
		var arrcheck = false;
		for (var i = 0; i < chks.length; i++) {
			if (chks[i].checked) {
				arrcheck = true;
				chickens.push(chks[i].value);
			}
		}	
		if (arrcheck == false){
			returnvalue = false;
			$('#formErrorStr4').show();
		} else {
			$('#formErrorStr4').hide();
		}
	} else {
		$('#formErrorStr4').hide();
	}
	
	if (returnvalue == true){
		$('#createTaskButtons').hide();
		$('#createTaskSending').show();
		//$("#createTaskSending1").load("ajaxactions.php?ajaxcase=022&str100="+str100+"&str200="+str200+"&str4="+str4+"&chks="+chks+"&num1="+num1+"&num2="+num2+"&str5="+str5+"");
	
		$("#createTaskSending").load("ajaxactions.php", { ajaxcase: "022", str100: str100, str200: str200, str4: str4, num1: num1, num2: num2, str5: str5, num5: num5, 'arr1[]': chickens } );
	}
	
	return returnvalue;
}


//*************************************************************************************
//*************************************************************************************
//*  Refresh the ajax box
//*************************************************************************************
//*************************************************************************************

 function refreshAjaxBox(type){
	 
	//if we are looking at the feed, refresh it
	if (managebookajaxbox == "feed"){
		managebookFeedType("feed");
	}
	
	//if we are looking at the task list and we have just added a task, refresh it
	if (managebookajaxbox == "tasks"){
		if (type == "tasks"){
			managebookFeedTasks("");
		}
	} 
	
	//if we are looking at the conversations  and we have just added a message, refresh it
	if (managebookajaxbox == "conversations"){
		if (type == "conversations"){
			managebookFeedType("conversations");	
		}
	} 
	
  
  }
  
//*************************************************************************************
//*************************************************************************************
//* Close the 'new task' box
//*************************************************************************************
//*************************************************************************************  

function newTaskClose(shallIRefresh){
	
	//refresh the feed
	if (shallIRefresh == "refresh"){
		refreshAjaxBox("tasks");
	}
	
	//close window
	document.getElementById("libPopup2").style.display="none"		
	
	//empty the form
	document.taskAssignForm.str100.value='';
	document.taskAssignForm.str200.value='';
	
	if (document.taskAssignForm.taskUsersCheckbox){
		document.taskAssignForm.taskUsersCheckbox.checked = false;
	}
	
	//hide the form error boxes
	$("#formErrorStr100").hide();
	$("#formErrorStr200").hide();
	
	//hide the user list
	$("#taskUsersDiv").hide();
	
	//show the buttons again
	$("#createTaskButtons").show();
	
	//hide and replace the 'sending' text
	$('#createTaskSending').hide();
	$('#createTaskSending').replaceWith('<div id="createTaskSending" style="display:none;"><img src="images/r/loading_sm.gif"> Sending...</div>');


}
//*************************************************************************************
//*************************************************************************************
//*  Form validation - Setting a task as a collaborator
//*************************************************************************************
//*************************************************************************************

function checkFormManageBookTasks2()
{
	
	returnvalue = true;
	
	var chickens = [];
	
	var str100 = document.taskAssignForm.str100.value; //title
	var str200 = document.taskAssignForm.str200.value; //description
	var chks = document.getElementsByName('arr1[]');	//array of selected users
	var num2 = document.taskAssignForm.num2.value; //days
	var str5 = document.taskAssignForm.str5.value; //corn
	
	var str4 = "true";
	var num5 = 1;
	
	var num1;
	
	for (var i=0; i<document.taskAssignForm.num1.length; i++)  {
		if (document.taskAssignForm.num1[i].checked)  {
		
			num1 = document.taskAssignForm.num1[i].value;
		
		}
	}
	
	chickens.push(chks[0].value);
	
	if (str100 == '')	{ 
		returnvalue = false;
		$('#formErrorStr100').show();
	} else {
		$('#formErrorStr100').hide();
	}
	
	if (str200 == '')	{ 
		returnvalue = false;
		$('#formErrorStr200').show();
	} else {
		$('#formErrorStr200').hide();
	}
	
	if (returnvalue == true){
		$('#createTaskButtons').hide();
		$('#createTaskSending').show();
		//$("#createTaskSending1").load("ajaxactions.php?ajaxcase=022&str100="+str100+"&str200="+str200+"&str4="+str4+"&chks="+chks+"&num1="+num1+"&num2="+num2+"&str5="+str5+"");
		
		$("#createTaskSending").load("ajaxactions.php", { ajaxcase: "022", str100: str100, str200: str200, str4: str4, num1: num1, num2: num2, str5: str5, num5: num5, 'arr1[]': chickens } );
	}
	
	return returnvalue;
}

//*************************************************************************************
//*************************************************************************************
//*  Form validation - Sending a message in a conversation in managebook
//*************************************************************************************
//*************************************************************************************

function checkFormManageBookConversation1()
{
	
	returnvalue = true;
	
	var str3 = $("textarea#str3Conv").val();
	
	if (str3 == '')	{ 
		returnvalue = false;
		$('#formConversationTextError').show();
	} else {
		returnvalue = true;
		$('#formConversationTextError').hide();
	}
	
	if (returnvalue == true){
		$('#loadingSendMessage').show();	
	}
	
	return returnvalue;
}


//*************************************************************************************
//*************************************************************************************
//*  Conversation popup
//*************************************************************************************
//*************************************************************************************

function editbookConversationPopupForm()
{
	
	returnvalue = true;
	
	var str100 = document.editbookCommentsForm.str100.value;
	str100 = escape(str100);
	var iamcorn = document.editbookCommentsForm.str4.value;
	
	if (str100 == '')	{ 
		returnvalue = false;
		$('#formConversationTextError').show();
	} else {
		returnvalue = true;
		$('#formConversationTextError').hide();
	}
	
	if (returnvalue == true){
		$('#loadingSendMessage').show();	
		$('#createMessageButtons').hide();	
		$("#loadingSendMessage").load("ajaxactions.php?ajaxcase=021&str100="+str100+"&str4="+iamcorn+"");
	}
	
	return false;
}

//*************************************************************************************
//*************************************************************************************
//*  cancel the message 
//*************************************************************************************
//*************************************************************************************

function editbookConversationPopupFormCancel(shallIRefresh){
	
	//refresh the feed
	if (shallIRefresh == "refresh"){
		refreshAjaxBox("conversations");
	}
	
	//hide the popup
	document.getElementById("libPopup4").style.display="none";
	
	//show the send buttons
	$('#createMessageButtons').show();	
	
	//hide and clear the sending box
	$('#loadingSendMessage').hide();
	$("#loadingSendMessage").replaceWith('<div id="loadingSendMessage" style="display:none;"><br/><img src="images/r/loading_sm.gif"> Sending...</div>');
	
	//clear the textbox
	document.editbookCommentsForm.str100.value = "";
	
	//hide error messages
	$('#formConversationTextError').hide();
}

//*************************************************************************************
//*************************************************************************************
//*  Select your Country
//*************************************************************************************
//*************************************************************************************


function selectCountry(){
	var num1 = document.countryForm.num1.value; //country selected
	var countryVariables = num1.split("~");
	
	//insert 'loading' animation
	$("#countrySelectionSaved").replaceWith('<div id="countrySelectionSaved"><div class="divgreen"><img src="images/r/loading_sm.gif"></div></div>');
	
	//load variables
	var countryNum = countryVariables[0];
	var countryCode = countryVariables[1];
	var countryName = escape(countryVariables[2]);
	var countryCurrency = countryVariables[3];
	var countryPod = countryVariables[4];
	
	//hide all countries
	$('.settingsCountryBox').hide();

	//show the correct country
	$('#country'+countryNum+'').show();
	
	//save the setting
	//$("#countrySelectionSaved").load("ajaxactionsall.php", { ajaxcase: "001", str1: countryCode, str200: countryName, str3: countryCurrency, str4: countryPod } );
	
	$("#countrySelectionSaved").load("ajaxactionsall.php?ajaxcase="+001+"&str1="+countryCode+"&str200="+countryName+"&str3="+countryCurrency+"&str4="+countryPod+"");

}

function selectCountryClose(){
	$('#libPopupCountry').hide();
	reloadTitleFlag();
}
function selectCountryShow(){
	$('#libPopupCountry').show();
}

function reloadTitleFlag(){
	var num1 = document.countryForm.num1.value; //country selected
	var countryVariables = num1.split("~");
	
	var countryCode = countryVariables[1];

	//$('.titleLinksFlag').load("ajaxactionsall.php", { ajaxcase: "002", str1: countryCode, str200: countryName, str3: countryCurrency, str4: countryPod } );
	$(".titleLinksFlag").load("ajaxactionsall.php?ajaxcase="+002+"&str1="+countryCode+"");

}

//*************************************************************************************
//*************************************************************************************
//*  Show/hide search boxes
//*************************************************************************************
//*************************************************************************************



	function basicShow(){
		$('#searchBasic').show();
		$('#searchAdvanced').hide();
    }
 
    function advancedShow(){
		$('#searchBasic').hide();
		$('#searchAdvanced').show();
    }
