// JavaScript Document

//this script will look to the server for dummy data every minute to keep the session alive
function loadDummyData()
{
	$("#dummyData").load("includes/dummyData.php", function(){ setTimeout( "loadDummyData();", 60000); });
}


//------------------------------------------------
//--- Scripts for validation stuff (book now) ----
//------------------------------------------------

//global variables
var capState = false;

//this function will do the overall validation
function submitBookNow(thisForm)
{
	//hide the form while checking
	$("#formContent").hide();
	$("#loadingDetails").show();
	
	//go through the different parts of the form, checking they are correct. If not, return the error message, if they are, go on the the captcha
	var inError = false;
	var errorMessage = "Please correct the following:\r\n\r\n";
	
	//name
	if (thisForm.Name.value == '')
	{
		inError = true;
		errorMessage += "- Name is a required field\r\n";
	}
	
	//Address
	if (thisForm.Address.value == '')
	{
		inError = true;
		errorMessage += "- Address is a required field\r\n";
	}
	
	//Telephone
	if (thisForm.Telephone.value == '')
	{
		inError = true;
		errorMessage += "- Telephone is a required field\r\n";
	}
	
	//Mobile
	if (thisForm.Mobile.value == '')
	{
		inError = true;
		errorMessage += "- Mobile is a required field\r\n";
	}
	
	//Email
	if (thisForm.Email.value == '')
	{
		inError = true;
		errorMessage += "- Email is a required field\r\n";
	}
	
	//Number_of_Adults
	if (!thisForm.Number_of_Adults[0].checked && !thisForm.Number_of_Adults[1].checked && !thisForm.Number_of_Adults[2].checked && !thisForm.Number_of_Adults[3].checked && !thisForm.Number_of_Adults[4].checked)
	{
		inError = true;
		errorMessage += "- Number of Adults is a required field\r\n";
	}
	
	//Number_of_Children
	if (!thisForm.Number_of_Children[0].checked && !thisForm.Number_of_Children[1].checked && !thisForm.Number_of_Children[2].checked && !thisForm.Number_of_Children[3].checked && !thisForm.Number_of_Children[4].checked && !thisForm.Number_of_Children[5].checked)
	{
		inError = true;
		errorMessage += "- Number of Children is a required field\r\n";
	}
	
	//Childrens Ages
	if (!thisForm.Number_of_Children[5].checked && thisForm.Childrens_Ages1.checked == false && thisForm.Childrens_Ages2.checked == false && thisForm.Childrens_Ages3.checked == false)
	{
		inError = true;
		errorMessage += "- Childrens Ages is a required field\r\n";
	}
	
	//Length_of_Recreational_Vehicle
	if (thisForm.Length_of_Recreational_Vehicle.value == '')
	{
		inError = true;
		errorMessage += "- Length of Recreational Vehicle is a required field\r\n";
	}
	
	//Check_In_Date
	if (thisForm.Check_In_Date.value == '')
	{
		inError = true;
		errorMessage += "- Check In Date is a required field\r\n";
	}
	
	//Check_Out_Date
	if (thisForm.Check_Out_Date.value == '')
	{
		inError = true;
		errorMessage += "- Check Out Date is a required field\r\n";
	}
	
	if(inError)
	{
		$("#formContent").show();
		$("#loadingDetails").hide();
		alert (errorMessage);
	} else
	{
		checkCaptcha(thisForm)
		//alert ("OK");
	}
}

//this functin will validate the recaptcha box
function checkCaptcha(thisForm)
{
	var valResponse = thisForm.recaptcha_response_field.value;
	var valChallenge = thisForm.recaptcha_challenge_field.value;
	$("#capValRes").load("includes/checkValid.php", { valResponse:valResponse, valChallenge:valChallenge }, function(){ returnCapResponse($("#capValRes").html(), thisForm);});
}

//this function checks the recaptcha results and either displays an error or goes to the function to submit the details
function returnCapResponse(response, thisForm)
{
	if(response == 'true')
	{
		//the captcha passed, so submit the form
		sendFormValues();
	} else
	{
		$("#formContent").show();
		$("#loadingDetails").hide();
		Recaptcha.reload ();
		alert("The words you typed in the validation did not match up with the ones in the image, please try again.");
	}
}

//this function submits the form via ajax
function sendFormValues()
{
	var formDetails = new Object();
	var $inputs = $('#booknow :input');
	$inputs.each(function() { if((this.type != "checkbox" || this.checked == true) && (this.type != "radio" || this.checked == true)){ formDetails[this.name] = $(this).val();} });
	//alert ( JSON.stringify(formDetails));
	$("#capValRes").load("bookFormStuff/sendBooknow.php", { data: JSON.stringify(formDetails) }, function(){ displaySendResult($("#capValRes").html()); });
}

//this function will display either the success or fail message
function displaySendResult(resultReturned)
{
	if (resultReturned == 'true')
	{
		//the email sent
		$("#confirmTrue").show();
		$("#loadingDetails").hide();
	} else
	{
		//the email failed
		$("#confirmFalse").show();
		$("#loadingDetails").hide();
	}
}



//-----------------------------------------------
//--- Scripts for validation stuff (contact) ----
//-----------------------------------------------

//global variables
//var capState = false; already set

//this function will do the overall validation
function submitContact(thisForm)
{
	//hide the form while checking
	$("#formContent").hide();
	$("#loadingDetails").show();
	
	//go through the different parts of the form, checking they are correct. If not, return the error message, if they are, go on the the captcha
	var inError = false;
	var errorMessage = "Please correct the following:\r\n\r\n";
	
	//name
	if (thisForm.Name.value == '')
	{
		inError = true;
		errorMessage += "- Name is a required field\r\n";
	}
	
	//Email
	if (thisForm.Email.value == '')
	{
		inError = true;
		errorMessage += "- Email is a required field\r\n";
	}
	
	if(inError)
	{
		$("#formContent").show();
		$("#loadingDetails").hide();
		alert (errorMessage);
	} else
	{
		checkCaptchaContact(thisForm)
		//alert ("OK");
	}
}

//this functin will validate the recaptcha box
function checkCaptchaContact(thisForm)
{
	var valResponse = thisForm.recaptcha_response_field.value;
	var valChallenge = thisForm.recaptcha_challenge_field.value;
	$("#capValRes").load("includes/checkValid.php", { valResponse:valResponse, valChallenge:valChallenge }, function(){ returnCapResponseContact($("#capValRes").html(), thisForm);});
}

//this function checks the recaptcha results and either displays an error or goes to the function to submit the details
function returnCapResponseContact(response, thisForm)
{
	if(response == 'true')
	{
		//the captcha passed, so submit the form
		sendFormValuesContact();
	} else
	{
		$("#formContent").show();
		$("#loadingDetails").hide();
		Recaptcha.reload ();
		alert("The words you typed in the validation did not match up with the ones in the image, please try again.");
	}
}

//this function submits the form via ajax
function sendFormValuesContact()
{
	var formDetails = new Object();
	var $inputs = $('#contact :input');
	$inputs.each(function() { if((this.type != "checkbox" || this.checked == true) && (this.type != "radio" || this.checked == true)){ formDetails[this.name] = $(this).val();} });
	//alert ( JSON.stringify(formDetails));
	$("#capValRes").load("contactFormStuff/sendContact.php", { data: JSON.stringify(formDetails) }, function(){ displaySendResultContact($("#capValRes").html()); });
}

//this function will display either the success or fail message
function displaySendResultContact(resultReturned)
{
	if (resultReturned == 'true')
	{
		//the email sent
		$("#confirmTrue").show();
		$("#loadingDetails").hide();
	} else
	{
		//the email failed
		$("#confirmFalse").show();
		$("#loadingDetails").hide();
	}
}




//-------------------------------------
//------ Scripts for menu stuff -------
//-------------------------------------

//set global variables
currMain = 0; //keep track of the main menu hovered on
currSub = 0; //keep track of the submenu hovered on

//this function will run when the page loads and change the size of the submenus
function completeMenu()
{
	//loop through the submenus, setting up hover and mouseleave
	var tempText = $("#submenuArray").html();
	var submenuArray = tempText.split("&amp;");
	var numSubmenus = submenuArray.length;
	
	for (i=1; i<=numSubmenus; i++)
	{
		$("#submenu_" + submenuArray[i]).bind("mouseleave", function() { currSub=0; hideSubmenu($(this).attr('menuNum')); });
		$("#menuTd_" + submenuArray[i]).bind("mouseenter", function() { currMain=$(this).attr('menuNum'); displaySubmenu($(this).attr('menuNum')); });
		$("#submenu_" + submenuArray[i]).bind("mouseenter", function() { currSub=$(this).attr('menuNum'); });
		$("#menuTd_" + submenuArray[i]).bind("mouseleave", function() { currMain=0; hideSubmenu($(this).attr('menuNum')); });
	}
}

//this function will be called when a main menu is hovered that has a sub menu
function displaySubmenu(showDiv)
{
	//hide all other menus
	$(".submenuDiv").hide();
	//show requested item
	$("#submenu_" + showDiv).show();
}

//this function runs when the submenu is mouseouted
function hideSubmenu(hideDiv)
{
	//hide the requested submenu
	if (currMain != hideDiv && currSub != hideDiv)
	{
		$("#submenu_" + hideDiv).hide();
	}
}


/*-------------------------------------------------------
----------------- Calendar Stuff ------------------------
-------------------------------------------------------*/

//this function will be called on page load and will do the inital load of the calendar and the prev and next buttons
function pageLoad()
{
	//set the loading div to show
	$("#calLoad").show();
	
	//set the click events on the arrows
	$("#calPrevDiv").click(function(){ changeMonth("prev"); });
	$("#calNextDiv").click(function(){ changeMonth("next"); });
	
	//set up the current month
	var currMonth = $("#currMonth").html();
	var currYear = $("#currYear").html();
	loadNewCal(currMonth, currYear);
}

//this function will be called when the month needs to be changed
function changeMonth(monthDirection)
{
	//set the loading div to show and the calender div to hide
	$("#calLoad").show();
	$("#calContent").hide();
	
	//change the divs with the month and year to update to the new month and year
	var currMonth = $("#currMonth").html();
	var currYear = $("#currYear").html();
	
	if (monthDirection == 'prev')
	{
		currMonth--;
	} else
	{
		currMonth++;
	}
	
	if (currMonth == 0)
	{
		currYear--;
		currMonth = 12;
	} else if (currMonth == 13)
	{
		currYear++;
		currMonth = 1;
	}
	
	$("#currMonth").html(currMonth);
	$("#currYear").html(currYear);
	
	$("#calMonthDiv").html(getMonthName(currMonth) + " " + currYear);
	
	loadNewCal(currMonth, currYear);
}

//this function call a php page to set up the new calendar
function loadNewCal(loadMonth, loadYear)
{
	$("#tempCalContent")
		.stop()
		.load("jsIncludes/calendarBuild.php", { loadMonth:loadMonth, loadYear:loadYear }, function(){ showNewCal($("#tempCalContent").html()); });
}

//this function will display the new calendar
function showNewCal(newCal)
{
	$("#calContent")
		.html(newCal)
		.show();
	setUpEvents();
	$("#calLoad").hide();
}
