/* JAVASCRIPT \\\
**************************************************
TITLE: Page Scripts
DESCRIPTION:
   This file contains a set of JavaScript
   functions that are used site-wide.
COMPANY: JPL Productions
AUTHOR: David Mingos
CONTACT: dmingos@jplprod.com
///
**************************************************
*/

var tabs = 'ammenities, features, meetings, attractions, directions';
tabs = tabs.split(', ');

function init() {

	if (document.getElementById('feature')) {
		var so = new SWFObject('lib/swf/home_features.swf', 'home_features', '534', '275', '7', "#01385c");
		so.addParam('wmode', 'opaque');
		so.write('feature');
	}

	var tabloaded = false;
	for (var i in tabs) {
		if (tabloaded == false && document.getElementById(tabs[i]+'-tab') && document.getElementById(tabs[i])) {
			loadTab(document.getElementById(tabs[i]+'-tab'));
			tabloaded = true;
		}
	}

}

function selectDestination() {
	document.getElementById('destination').disabled = false;
	document.getElementById('city').disabled = true;
}

function selectCity() {
	document.getElementById('destination').disabled = true;
	document.getElementById('city').disabled = false;
}

function swapPhotos(thumbAnchor) {

	var thumbId = thumbAnchor.id.replace('link_', 'thumb_');
	
	var photo = document.getElementById('swapPhoto');
	var thumbImage = document.getElementById(thumbId);
	
	var newPhotoSrc = thumbAnchor.href;
	var newThumbHref = photo.src;
	var newThumbSrc = photo.src.replace('lg_', 'th_');
	

	photo.src = newPhotoSrc;
	thumbAnchor.href = newThumbHref;
	thumbImage.src = newThumbSrc;
	thumbAnchor.blur();
	
	return false;
}

function loadTab(tab) {

	var contentId = tab.id.replace('-tab','');

	document.getElementById('activetab').innerHTML = document.getElementById(contentId).innerHTML
	tab.className = 'active tab';

	for (var i in tabs)
		if (tab.id != tabs[i]+'-tab' && document.getElementById(tabs[i]+'-tab'))
			document.getElementById(tabs[i]+'-tab').className = 'tab';

}

function validateMeetingInquiry() {

	var errors = 0;
	var requiredFields = 'name, email, address_1, destination, city, state, preferred_hotel, zip, country, dest_location, phone';
	requiredFields = requiredFields.split(', ');

	for (var i in requiredFields)
		if (!checkRequiredField(requiredFields[i]))
			errors++;

	if (!checkRequiredField('flexible'))
		errors++;

	if (!hasValue('email') || !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value))) {
		flagField('email');
		errors++;
	} else {
		clearFlag('email');
	}

	if (errors > 0) {
        document.getElementById('error_message').innerHTML = '<p>Sorry, the form cannot be submitted because required information is incomplete or invalid.</p>';
        return false;
    }
	return true;

}

function checkRequiredField(field_id) {
	if (!hasValue(field_id)) {
		flagField(field_id);
		return false;
	} else {
		clearFlag(field_id);
		return true;
	}
}
function hasValue(field_id) {
	var field = document.getElementById(field_id);
	if (!field) {
		var choices = document.getElementsByName(field_id);
		for (var i in choices)
			if (choices[i].checked) return true;
		return false;
	} else {
		if (field.value.length > 0) return true;
		else return false;
	}
}
function clearFlag (field_id) {
	var field_label = document.getElementById('lbl_'+field_id);
	if (field_label) {
		field_label.className = 'required';
	}
}
function flagField(field_id) {
	var field_label = document.getElementById('lbl_'+field_id);
	if (field_label) {
		field_label.className += ' attention';
	}
}

/******************************/


var reservationValidation = {
	'#reservation' : function(el) {
		el.onsubmit = function() {
			if (!document.getElementById('radio_destination').checked && !document.getElementById('radio_city').checked) {
				alert('Please select a destination or a city.');
				return false;
			}
		}
	}
};

var reserveByDestination = {
	'#reservation #radio_destination' : function(el) {
		el.onclick = function() {
			selectDestination();
		}
	},
	'#reservation #destination' : function(el) {
		el.onchange = function() {
			document.getElementById('radio_destination').checked = true;
			selectDestination();
		}
	}
};

var reserveByCity = {
	'#reservation #radio_city' : function(el) {
		el.onclick = function() {
			selectCity();
		}
	},
	'#reservation #city' : function(el) {
		el.onchange = function() {
			document.getElementById('radio_city').checked = true;
			selectCity();
		}
	}
};

var loadTabs = {
	'#ammenities-tab' : function(el) {
		el.onclick = function() {
			loadTab(el);
		}
	},
	'#features-tab' : function(el) {
		el.onclick = function() {
			loadTab(el);
		}
	},
	'#meetings-tab' : function(el) {
		el.onclick = function() {
			loadTab(el);
		}
	},
	'#attractions-tab' : function(el) {
		el.onclick = function() {
			loadTab(el);
		}
	},
	'#directions-tab' : function(el) {
		el.onclick = function() {
			loadTab(el);
		}
	}
};

var meetingInquiryValidation = {
	'#inquire' : function(el) {
		el.onsubmit = function() {
			return validateMeetingInquiry();
		}
	}
};

/******************************/


Behaviour.register(meetingInquiryValidation);
Behaviour.register(reservationValidation);
Behaviour.register(reserveByDestination);
Behaviour.register(reserveByCity);
Behaviour.register(loadTabs);

addEvent(window, 'load', init);
