/* 
	Title: MKK - Global JavaScript file
	Description: Contains all JavaScript functions for use on site
	Client: MMK
	Date: 01/12/2006
	Updated: 20/11/2008
	Version 1.01
	Author: Ben Ellis
*/


/* 
	Title: nav()
	Description: Function to add custom behaviours to the main navigation
	Arguments: None
*/
function nav(){
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

/* 
	Title: hideEmail()
	Description: Function to add custom behaviours to the main navigation
	Arguments: 
		$name: name for email
		$domain: domain for email
*/
function hideEmail(name,domain){
   document.write("<a href=\"mailto");
   document.write(":" + name + "@");
   document.write(domain + "\" title=\"Contact Us\">" + name + "@" + domain + "<\/a>");
}

/* 
	Title: openWindow()
	Description: Function to open a new window
	Arguments: 
		$url: url of new window
		$name: name of new window
		$width: width of new window
		$height: height of new window
*/
function openWindow(url,name,width,height){
	widthHeight = 'height=' + height +', width=' + width;
	newWindow = window.open(url, name, widthHeight);
	newWindow.focus();
}

/* 
	Title: toggle()
	Description: Function to toggle objects: plugs into the prototyp/scriptaculous framework
	Arguments: 
		$id: id of object to toggle
		$effect: effect to use
		$duration: length of the effect

function toggle(id,effect,effectDuration){
	if(document.getElementById){
		if(!effectDuration) effectDuration = 0.5;
		Effect.toggle(document.getElementById(id),effect,{duration:effectDuration});	
	}
}
*/
/* 
	Title: toggle()
	Description: Function to toggle objects: plugs into the prototyp/scriptaculous framework
	Arguments: 
		$id: id of object to toggle
*/
function toggle(id){
	// If browser understands the DOM
	if(document.getElementById){
		// Get the object
		var obj = document.getElementById(id);
		// Toggle the visability
		if(obj) $(obj).toggle();
	}
}


/* 
	Title: changeInnerHTML()
	Description: Function to change the inner html of an object
	Arguments: 
		$id: id of object to toggle
		$defaultHTML: the initial html for the object
		$newHTML: the new html for the object
*/
function changeInnerHTML(id,defaultHTML,newHTML){
	if(document.getElementById){
		target = document.getElementById(id).innerHTML;
		if(target == defaultHTML){
			document.getElementById(id).innerHTML = newHTML;
			document.getElementById(id).title = newHTML;
		}
		else if(target == newHTML){
			document.getElementById(id).innerHTML = defaultHTML;
			document.getElementById(id).title = defaultHTML;
		}
	}
}

var http_request = false;

function makePOSTRequest(url, parameters){
	http_request = false;
  
  // Mozilla, Safari,...
	if (window.XMLHttpRequest){ 
		http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType){
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} 
	else if (window.ActiveXObject){ // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	if (!http_request){
		alert('Cannot create XMLHTTP instance');
		return false;
	}
  
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContents(){
	if(http_request.readyState == 4){
		if(http_request.status == 200){
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById('checkAvail').innerHTML = result;            
		 } 
		 else {
			alert('There was a problem with the request.');
		}
	}
}

function get(){
	// Set up the variable to parse the form values to PHP
	var postValues ="";
	

	/*
	// Loop through the form elements
	for(var i=0;i<obj.childNodes.length;i++){
		
		
		
		// If the form element are a  sleect or input item
		if(obj.childNodes[i].tagName == "SELECT" || obj.childNodes[i].tagName == "INPUT"){
			// Get all the elements
			postValues += "&"+obj.childNodes[i].id+"="+obj.childNodes[i].value
		}
	}
	
	*/
	
	// Set up the rooms array
	var room = document.getElementById("room");
	var rooms = "";
	
	/*rooms = room.options[0].value;*/
	
	// Loop through the items
	for(i=0;i<room.length;i++){
	   if(room.options[i].selected == true){
	       rooms += room.options[i].value+","
	   }	   
	}
	

	var postValues = 
		"itinTitle=" + encodeURI(document.getElementById("itinTitle").value)+
		"&title=" + encodeURI(document.getElementById("title").value)+
		"&firstname=" + encodeURI( document.getElementById("firstname").value)+
		"&surname=" + encodeURI( document.getElementById("surname").value)+
		"&email=" + encodeURI( document.getElementById("email").value)+
		"&address=" + encodeURI( document.getElementById("address").value)+
		"&postcode=" + encodeURI( document.getElementById("postcode").value)+
		"&country=" + encodeURI( document.getElementById("country").value)+
		"&phone=" + encodeURI( document.getElementById("phone").value)+
		"&departure=" + encodeURI( document.getElementById("departure").value)+
		"&adults=" + encodeURI( document.getElementById("adults").value)+
		"&children=" + encodeURI( document.getElementById("children").value)+
		"&room=" + rooms+
		"&room_no=" + encodeURI( document.getElementById("room_no").value)+
		"&moreInfo=" + encodeURI( document.getElementById("moreInfo").value)+
		"&marketing=" + encodeURI( document.getElementById("marketing").value)+
		"&other_marketing=" + encodeURI( document.getElementById("other_marketing").value);
		
	makePOSTRequest('php/includes/processForm.php', postValues);
}

/* ------------------------------------------------------------
	Online Booking functions
---------------------------------------------------------------- */

// Set up variables for guest counter
var current_guest= 0;
var guest_total = 5;
			
// Function to add a guest to the form
function add_guest(id){
	current_guest = current_guest + id;
	// If we haven't then increment the field number
	if(current_guest<0) current_guest = guest_total;
			
	// Create our new form fields via AJAX
	if(current_guest<=guest_total){
		
		// Create the current guest div id
		this_guest = 'guest_'+current_guest;
		// Create the fields
		new Ajax.Updater(this_guest, 'php/includes/guest_details.php?id='+current_guest+'', {asynchronous:true});
		new Effect.Appear(this_guest);
	}
	// Deactivate the add guest button
	if(current_guest>=guest_total){
		current_guest= 0;
		new Effect.Highlight('add');
		document.getElementById('add').innerHTML = 'You have reached the maximum no of guests: '+guest_total+' | <a href=\"javascript: reset_guest(\'add\',\'guests\')\">Reset</a>';
	}
}
			
// Function reset all guests on the form
function reset_guest(id,group){
	var html = '<a href="javascript:add_guest(1)">Add names of other party members. +</a></span>';
	document.getElementById(id).innerHTML = html;
	// Loop through and hide all the guest elements
	obj = document.getElementById(group);
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.nodeName=="DIV"){
			new Effect.Fade(node.id);
		}
	}
}

// Function to remove a guest from the form
function remove_guest(id){
	new Effect.Fade(id);
}

/* 
	Title: newEffect()
	Description: Function to add new effect to an object: plugs into the prototyp/scriptaculous framework
	Arguments: 
		$id: id of object to toggle
		$effect: effect to use
		$duration: length of the effect
*/
function newEffect(id,effect,effectDuration){
	if(document.getElementById){
		if(!effectDuration) effectDuration = 0.5;
		Effect.toggle(document.getElementById(id),effect,{duration:effectDuration});	
	}
}

/* 
	Title: validate()
	Description: Function to  validate a form
	Arguments: 
		$data: the data to validate
*/
function validate(data){
	var errors = "";
	for (i=0;i<data.length;i++){
		if(document.getElementById(data[i]).value == "") errors = 1;
	}
	if(errors == 1) {
		// Error message popup
		alert("**Form errors**\nYou missed out some important fields, please correct the information and then press continue.");
		// jQuery scroll back to top of page 
		$.scrollTo( '#container', 1500);
	}
}


/* ----------------------------------------------------------
NEW HOMEPAGE FUNCTIONS 
----------------------------------------------------------*/

/*
	Title: swapContent()
	Description: Function to change the image, content and set the active nav state
	Arguments: content id
*/
function swapContent(id){

	// If the browser supports the DOM
	if (document.getElementById){
	
		// Create the variables
		var content_parent = "";
		var content_item = "";
		var nav_parent = "";
		var nav_item = "";
		var img_parent = "";
		var img_item = "";
				
		// Get the objects through the DOM
		content_parent = document.getElementById("slide");
		content_item = document.getElementById('slideshow_content_'+id);
		
		nav_parent = document.getElementById("slideshow_nav");
		nav_item = document.getElementById('slideshow_nav_'+id);
		
		img_parent = document.getElementById("slideshow_images");
		img_item = document.getElementById('slideshow_image_'+id);
		
		// Hide content elements
		for (i=0; i<content_parent.childNodes.length; i++) {
			node = content_parent.childNodes[i];
			if (node.nodeName=="LI") $(node).fadeOut(500);
		}
		
		// Hide image elements
		for (i=0; i<img_parent.childNodes.length; i++) {
			node = img_parent.childNodes[i];
			if (node.nodeName=="A") $(node).fadeOut(750);
		}
		
		// Deactivate the nav elements
		for (i=0; i<nav_parent.childNodes.length; i++) {
			node = nav_parent.childNodes[i];
			if (node.nodeName=="LI") node.className = "";
		}
		
		// Show the current content item
		if(content_item) $(content_item).fadeIn(500);
		
		// Show the current image item
		if(img_item) $(img_item).fadeIn(1750);
		
		// Highlight the current nav item
		if(nav_item) nav_item.className = "active";
	}
}

/*
	Title: loopContent()
	Description: Function to loop the swapContent function above
	Arguments: direction (forwards or backwards)
*/

// Current item
var currentItem = 0;
// Number of items in the array
var count = 5;

function loopContent(direction){
	// Set the current item
	currentItem = currentItem + direction;
	// If we have reached the end of the loop reset the current item
	if(currentItem>count) currentItem = 0;
	// If we have reached the start of the loop reset the current item to the total
	if(currentItem<0) currentItem = count;
	// Run the swap content function
	swapContent(currentItem);
}

var interval = "";

/*
	Title: startSlideshow()
	Description: Function to start the slideshow automatically on the homepage
*/
function startSlideshow(){
	if(interval=="") interval = setInterval ("loopContent(1)",7000);
	else stopSlideshow();
}

/*
	Title: stopSlideshow()
	Description: Function to stop the slideshow automatically scrolling through the items
*/
function stopSlideshow(){
	if(interval !="")
	clearInterval(interval);
	interval = "";
}

/*
	Title: formMsg()
	Description: Function to remove the default message from a form field 
	when a user places their mouse on the field
	Arguments: object
*/
function formMsg(obj){
	// Setup
	var formValue = obj.value;
	if(formValue == obj.value) obj.value = "";
}

/*
	Title: showPage()
	Description: Function to show and hide the search results
	Arguments: page id
*/
function showPage(id){
	// If the browser supports the DOM
	if (document.getElementById){
	
		var parent = "";
		var nav_parent = "";
		var nav_item = "";
		var active_result = "";
	
		// Get the objects through the DOM
		parent = document.getElementById("search_results");
		nav_parent = document.getElementById("search_paging");
		
		nav_item = document.getElementById("page_"+id);
		active_result = document.getElementById("result_"+id);

		// Hide result sets
		for (i=0; i<parent.childNodes.length; i++){
			node = parent.childNodes[i];
			if (node.nodeName=="LI") $(node).fadeOut(250);
		}
		
		// Set the nav classes to null
		for (i=0; i<nav_parent.childNodes.length; i++){
			node = nav_parent.childNodes[i];
			if (node.nodeName=="LI") node.className = "";
		}
		
		// Show the current result set
		if(active_result) $(active_result).fadeIn(250);
		
		// Highlight the current nav item
		if(nav_item) nav_item.className = "active";
	}
}