// VALIDATE AN EMAIL //

	function validateEmail(email) {
		invalidChars = ' /:,;'

		for (i=0; i<invalidChars.length; i++) {// does it contain any invalid characters?
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) > -1) {
				return false
			}
		}
		atPos = email.indexOf('@',1)  // there must be one '@' symbol
		if (atPos == -1) {
			return false
		}
		if (email.indexOf('@',atPos+1) != -1) {  // and only one '@' symbol
			return false
		}
		periodPos = email.indexOf('.',atPos)
		if (periodPos == -1) {  // and at least one '.' after the '@'
			return false
		}
		if (periodPos+3 > email.length) {  // must be at least 2 characters after the '.'
			return false
		}
		return true;
	}




// SEND AN ENQUIRY //

function send_enquiry(){

// Validate form before continuing
var sender = document.sendenquiry.sender.value;
var senders_email = document.sendenquiry.senders_email.value;
var message = document.sendenquiry.message.value;
var advert_id = document.sendenquiry.advert_id.value;
var email = document.sendenquiry.email.value;

if(sender == '') {
	alert('Please enter your name.');
	document.sendenquiry.sender.focus();
	return false;
}
if((!validateEmail(senders_email)) || (senders_email == '')) {
	alert('Please enter your email address.');
	document.sendenquiry.senders_email.focus();
	return false;
}
if(message == '') {
	alert('Please enter a message.');
	document.sendenquiry.message.focus();
	return false;
}

	// Construct URL variable
	var url = "includes/forms/ajax_forms.php?form=send_enquiry&senders_email=" + senders_email + "&sender=" + sender + "&message=" +  message + "&advert_id=" + advert_id+ "&email=" + email;

	var form = 'sendenquiry';
	construct(url,form);
	}




// SEND AD TO FRIEND //

function sendfriend_ad(){

// Validate form before continuing
var recepient_email = document.sendfriend.recepient_email.value;
var sender = document.sendfriend.sender.value;
var message = document.sendfriend.message.value;
var advert_id = document.sendfriend.advert_id.value;
var name = document.sendfriend.name.value;

if((!validateEmail(recepient_email)) || (recepient_email == '')) {
	alert('Please enter the email address you are sending to.');
	document.sendfriend.recepient_email.focus();
	return false;
}
if(sender == '') {
	alert('Please enter your name.');
	document.sendfriend.sender.focus();
	return false;
}
if(message == '') {
	alert('Please enter a message.');
	document.sendfriend.message.focus();
	return false;
}


	// Construct URL variable
	var url = "includes/forms/ajax_forms.php?form=sendfriend_ad&recepient_email=" + recepient_email + "&sender=" + sender + "&message=" +  message + "&advert_id=" + advert_id + "&name=" +  name;

	var form = 'sendfriend';
	construct(url,form);
}


// RECOMMEND SITE //

function do_popup_recommend(){
 elem = document.getElementById("div_recommend");
 elem.style.display = "block";
}

function undo_popup_recommend(){
 elem = document.getElementById("div_recommend");
 elem.style.display = "none";
}

function do_recommend(){

// Validate form before continuing
var recepient_email = document.recommend.recepient_email.value;
var sender_email = document.recommend.sender_email.value;
var message = document.recommend.message.value;

if((!validateEmail(recepient_email)) || (recepient_email == '')) {
	alert('Please enter a recepient email address.');
	document.recommend.recepient_email.focus();
	return false;
}
if((!validateEmail(sender_email)) || (sender_email == '')) {
	alert('Please enter a your email address.');
	document.recommend.sender_email.focus();
	return false;
}

	// Construct URL variable
	var url = "includes/forms/ajax_forms.php?form=recommend&recepient_email=" + recepient_email + "&sender_email=" + sender_email + "&message=" + message;

	var form = 'recommend';
	construct(url,form);
}



// ORDER A TRAVEL PLANNER //

function do_order(){

// Validate form before continuing
var sender_name = escape(document.order.sender_name.value);
var sender_email = escape(document.order.sender_email.value);
var address = escape(document.order.address.value);
var comments = escape(document.order.comments.value);

if(sender_name == '') {
	alert('Please enter your name.');
	document.order.sender_name.focus();
	return false;
}
if((!validateEmail(sender_email)) || (sender_email == '')) {
	alert('Please enter your email address.');
	document.order.sender_email.focus();
	return false;
}
if(address == '') {
	alert('Please enter your mailing address.');
	document.order.address.focus();
	return false;
}

	// Construct URL variable
	var url = "includes/forms/ajax_forms.php?form=order&sender_name=" + sender_name + "&sender_email=" + sender_email + "&address=" + address + "&comments=" + comments;

	var form = 'order';
	construct(url,form);
}



// SEARCH FEATURES //

function srchType1(){

var elem1 = document.getElementById("search_region");
var elem2 = document.getElementById("search_prod_type");
var sel = document.getElementById("srchtype1");
var srch = sel.options[sel.selectedIndex].value;

	if(srch == "region"){
		elem2.style.display = "none";
		elem1.style.display = "block";
	}
	if(srch == "prod_type"){
		elem1.style.display = "none";
		elem2.style.display = "block";
	}
}

function srchType2(){

var sel = document.getElementById("srchtype2");
var srch = sel.options[sel.selectedIndex].value;

	if(srch == "region"){
		document.search.prod_type.disabled=true;
		document.search.region.disabled=false;
	}
	if(srch == "prod_type"){
		document.search.region.disabled=true;
		document.search.prod_type.disabled=false;
	}
}

function do_popup_search(){
	elem = document.getElementById("div_search");
	elem.style.display = "block";
}
function undo_popup_search(){
	elem = document.getElementById("div_search");
	elem.style.display = "none";
}




function construct(url,form) { 

	// first remove the childnodes presently in the div
	while (document.getElementById(form).hasChildNodes()) 
       {
	document.getElementById(form).removeChild(document.getElementById(form).firstChild);
       }

	// Initialize connections
	var ajaxConnectionUpdate = new AJAXConnection('ajaxConnectionUpdate');

	// Initialize templates 
	var displayTemplate = new DisplayTemplate();

	// Create unique url each http send, to prevent browser pulling content from cache
	var randomNum = parseInt(Math.random()*99999999);

	// Process Function
	ajaxConnectionUpdate.xmlhttpPost(url + "&rand=" + randomNum, displayTemplate, form);
}




/** Display Class */
function DisplayTemplate() {	
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(update,form) {

	// indicate http response complete (stop waiting...)
	document.getElementById(form).innerHTML = "";

	document.getElementById(form).innerHTML = update;
				
    }    
}