// Library of Common Functions.
//-----------------------------

function showmenu(idname) {

	// Split id_name into an array delimited by an underscore.
		
	var id_string = ""
	var idname_array = idname.split("_")
			 
	for (var i=0 ;i<idname_array.length ;i++) {
			
		id_string = id_string + idname_array[i]
				
		if (eval('document.getElementById("' + id_string + '_menu")')) {
			eval('document.getElementById("' + id_string + '_menu").style.display="block"')
		}
		if (eval('document.getElementById("' + id_string + '_label")')) {
			eval('document.getElementById("' + id_string + '_label").style.color="WHITE"')
		}
		id_string = id_string + "_"
	}
}

function country_entry(name,currency,description,url) {

	// Define the object to hold each element of the country array.
	this.name 	  	 = name
	this.currency 	 = currency
	this.description = description
	this.url 	  	 = url
}

function populate_list(dd_index,current_list) {

	// Populate the Selection List on the homepage with values from the array.
		
	var previous_name
	var select_ctr = 0
	var qs_exists
	var qs_ddindex
	
	current_list.length = 0
			
	for (var i = 0; i < country_array.length; i++) {
	
		// Put check in here if multiple countries are found in country_array.  If so we only want one menu
		// option displayed.
			
		if (country_array[i].name != previous_name) { 
			current_list.options[select_ctr] = new Option(country_array[i].name, country_array[i].currency)
			current_list.options[0].selected = true		
			select_ctr++
		}
		previous_name = country_array[i].name		
	}
	
	// Now set the drop-down list to show the option the user selected.  If no index has been returned then
	// the page has been redirected to a new site.  Therefore use the value of the querystring passed down.
	
	if (dd_index != "") {
		current_list.selectedIndex = dd_index
	}
	else {
		qs_exists = document.URL.indexOf('?')
		if (qs_exists != -1) { 
			qs_ddindex = document.URL.substring(qs_exists+1, document.URL.length)
			current_list.selectedIndex = qs_ddindex
		}
	}
}

function set_currency(current_form) {

	// Return the selected item from the drop_down currency list on the homepage.

	var sel_index = document.entry_form.country.selectedIndex

	// Ignore the first entry as it is the prompt to choose a country.	Determine whether a 
	// domain name exists for that country.  Loop through the country array and ignore the 0 
	// item as it just holds the prompt for a country.
	
	// If a domain name exists for the country then redirect the browser to that country's address.

	if (sel_index != 0) {
	
		//document.entry_form.country_name.value = document.entry_form.country.options[sel_index].text
	
		for (var i = 1; i < country_array.length; i++) {

			if (country_array[i].name == document.entry_form.country.options[sel_index].text) {
					  		
				document.entry_form.country_name.value  = country_array[i].name
				document.entry_form.currency.value 	  	= country_array[i].currency
				document.entry_form.currency_desc.value	= country_array[i].description
				document.entry_form.country_url.value	= country_array[i].url

				// Save the index of the selected option so you can set the drop-down list's currently
				// selected option.  Also, if we are redirecting to another country, pass the index of the
				// currently selected option from the drop-down.
				
				document.entry_form.dd_index.value	= sel_index
				
				if (country_array[i].url != "") {
					window.location = country_array[i].url + "?" + sel_index
					break
				}
				else {
					current_form.action = "default.asp"
					current_form.submit()
				}
			}
		}
	}
	else {
		alert("Please select a country from the list")
	}
}

function build_service_list(current_list,service_type) {
	
	// Build the main service drop-down boxes.  Set the counters to 1 as the first option wil say
	// "Please select"

	var previous_code
	var select_ctr = 1

	current_list.length = 1
			
	for (var i = 0; i < roomtypes_array.length; i++) {

		if (roomtypes_array[i].code != previous_code && roomtypes_array[i].stype == service_type) { 
			current_list.options[select_ctr] = new Option(roomtypes_array[i].service)
			current_list.options[0].selected = true		
			select_ctr++
		}
		previous_code = roomtypes_array[i].code		
	}
}	

function build_rooms_list(service_list) {

	// Get the index of the currently selected service eg. hotel.
	
	var sel_index = service_list.selectedIndex
	var sel_code  = service_list.options[sel_index].value
	var sel_text  = service_list.options[sel_index].text
	var sel_ctr	  = 1
	var room_list = service_list.name + "_room"

	// If "Please Select" is chosen, reset the corresponding room types drop-down list and put it back
	// to its default entry of "<-- Choose a hotel" otherwise read through the array and where the code 
	// matches, put those options in the drop-down.
	
 	if (sel_index == 0) {
 		eval("document.FrontPage_Form1." + room_list + ".options.length = 0")
		eval("document.FrontPage_Form1." + room_list + ".options[0] = new Option('<-- Choose','')")
		eval("document.FrontPage_Form1." + room_list + ".options[0].selected = true")		
	}
 	else {
		eval("document.FrontPage_Form1." + room_list + ".options[0] = new Option('Please Select','')")
		for (var i = 0; i < roomtypes_array.length; i++) {
			if (roomtypes_array[i].service == sel_text) { 
				eval("document.FrontPage_Form1." + room_list + ".options[sel_ctr] = new Option(roomtypes_array[i].room)")
				eval("document.FrontPage_Form1." + room_list + ".options[0].selected = true")		
				sel_ctr++
			}
		}
 	}
}

function validate_form(current_form) {
		
	var err_found = false
		
	err_msg = "Please correct the following Errors" + "\n" +
			  "___________________________" + "\n\n"
			
	if (check_line_error(current_form,current_form.hotel1)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.hotel2)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.hotel3)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.hotel4)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.package)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.cruise)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.sailing)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.honeymoon)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.car_hire_island)) {
		err_found = true
	}				

	if (check_line_error(current_form,current_form.car_category)) {
		err_found = true
	}				

	if (current_form.name.value == "") {
   		err_msg = err_msg + "Please enter a value in the \"Name\" field.\n\n"
		err_found = true
	}
		if (current_form.email.value == "") {
		err_msg = err_msg + "Please enter a value in the \"Email\" field.\n\n"
		err_found = true
	}
		if (current_form.country.selectedIndex == 0) {
		err_msg = err_msg + "Please select one of the options from the \"country\" drop-down box.\n\n"
		err_found = true
	}
		if (err_found) {
		alert(err_msg)
		return false
	}
	
	return (true)
}
	
function check_line_error(current_form,current_field) {

	// Check for empty fields.  If all empty, do nothing, otherwise update the error message.
	// Split the field based on the "_" delimter and capitalise the first letter of each part.

	var first_letter = ""
	var field_name	 = ""
	var error_found	 = false
			
	if (current_field.selectedIndex == 0 && 
		eval("current_form." + current_field.name + "_in.value") == "" && 
		eval("current_form." + current_field.name + "_out.value") == "") {
		return false
	}
	else {
		if (current_field.name.indexOf("_") == -1) {
			first_letter = current_field.name.substr(0,1).toUpperCase()
			field_name   = first_letter + current_field.name.substring(1)
		}
		else {
			fieldname_array = current_field.name.split("_")
			for (var i=0; i<fieldname_array.length;i++) {
				first_letter = fieldname_array[i].substr(0,1).toUpperCase()
				field_name   = field_name + (i==0 ? "" : " ") + first_letter + fieldname_array[i].substring(1)
			}
		}
		
		if (current_field.selectedIndex == 0) {
			err_msg = err_msg + field_name + " - Please select an option" + "\n" 
			error_found = true
		}
		if (eval("current_form." + current_field.name + "_in.value") == "") {
			err_msg = err_msg + field_name + " - Date In is empty" + "\n" 
			error_found = true
		}
		if (eval("current_form." + current_field.name + "_out.value") == "") {
			err_msg = err_msg + field_name + " - Date Out is empty" + "\n"
			error_found = true 
		}

		// Check the dates to ensure the check out date is less than the check in date.  Only do this
		// if both dates have been entered.
			
		if (eval("current_form." + current_field.name + "_in.value") != "" && eval("current_form." + current_field.name + "_out.value") != "") {
			var datein  = eval("current_form." + current_field.name + "_in.value")
			var dateout = eval("current_form." + current_field.name + "_out.value")
			if (makedate(datein) > makedate(dateout)) {
				err_msg = err_msg + field_name + " - The Date In is greater than the Date Out" + "\n"
				error_found = true 
			}
		}
			
		if (error_found) {
			err_msg = err_msg + "\n"
			return true
		}
		else {
			return false
		}
	}		
}

function month_number(monthval){

	// Converts a three letter month to a month number.
		
	switch(monthval) {
		case "Jan" : monthval = 0; break
		case "Feb" : monthval = 1; break
		case "Mar" : monthval = 2; break
		case "Apr" : monthval = 3; break
		case "May" : monthval = 4; break
		case "Jun" : monthval = 5; break
		case "Jul" : monthval = 6; break
		case "Aug" : monthval = 7; break
		case "Sep" : monthval = 8; break
		case "Oct" : monthval = 9; break
		case "Nov" : monthval = 10; break
		case "Dec" : monthval = 11; break
		default : monthval = 0
	}
	return(monthval)
}

function makedate(in_date) {
	
	// Take a string in the format dd mmm yyyy and convert it to Date object.
	
	if (in_date != "") {
		var v_ind 	 = in_date.indexOf(" ")
		var v_ind2 	 = in_date.lastIndexOf(" ")
		var day   	 = in_date.substring(0,v_ind)
		var month 	 = in_date.substring(v_ind + 1,v_ind2)
		var month_no = month_number(month)
		var year  	 = in_date.substring(v_ind2 + 1)
		var new_date = new Date(year,month_number(month),day)
		return new_date
	}
}

function GetDay(nDay)
{
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}

function GetMonth(nMonth)
{
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth] 	  	 
}

function DateString()
{
	var Today = new Date();
	var suffix = "th";
	switch (Today.getDate())
	{
		case 1:
		case 21:
		case 31: 
			suffix = "st"; break;
		case 2:
		case 22:
			suffix = "nd"; break;
		case 3:
		case 23:
			suffix = "rd"; break;
	};

	var strDate = GetDay(Today.getDay()) + " " + Today.getDate();
	strDate += suffix + " " + GetMonth(Today.getMonth()) + ", " + Today.getYear();
	return strDate
}

function field_empty(input_string) {

	// Return True if the input_string is empty.
	if (input_string == "" || input_string == null) {
		return true
	}
}

function phone_alpha(current_form) {

	// Check that no alpha characters have been entered in the phone number.
	var test_result = false
	
	if (current_form.phone.value != "" && !its_floating_point(current_form.phone.value)) {
		test_result = true
	}
	return test_result
}

function its_floating_point(string_value) {

	// Run through the rest of the characters in the string
	for (var counter = 0; counter < string_value.length; counter++) {

		current_char = string_value.charAt(counter)
		if (!its_a_digit_or_dot(current_char)) {
			return false
		}
	}
    
    // Otherwise, the string has nothing but digits, so return true
    return true
}

function its_a_digit_or_dot(character) {

	var floating_point_characters = " .0123456789"

	// If it's not in the floating_point_characters string, then it's not a valid floating point 
	// character, so return false

	if (floating_point_characters.indexOf(character) == -1) {
		return false
	}

	// Otherwise, it's a digit, so return true
	return true
}

function its_a_letter(character) {

    var lowercase_letters = "abcdefghijklmnopqrstuvwxyz"
    var uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    // If it's not in the lowercase_letters string or the uppercase_letters string, then it's not a letter, 
    // so return false
    
    if (lowercase_letters.indexOf(character) == -1 &&
        uppercase_letters.indexOf(character) == -1) {
        return false
    }
    
    // Otherwise, it's a letter, so return true
    return true
}

function its_alphabetic(string_value) {

    // Run through each character in the string
    for (var counter = 0; counter < string_value.length; counter++) {
        
        // Get the current character
        current_char = string_value.charAt(counter)
        
        // If it's not a letter, return false
        if (!its_a_letter(current_char)) {
            return false
        }
    }
    
    // Otherwise, the string has nothing but alphabetic characters, so return true
    return true
}