/******* created for African Bush Camps  *******/

var timer = null;

function swap_img(name,img) {
	document.getElementById(name).src = img;
}

function doTab(new_tab) {
	
	var tab_container = document.getElementById("tabnav");
	var tab_links = tab_container.getElementsByTagName("a");	
	var next_tab = (new_tab == 'left') ? 'right' : 'left';
	
	for ( var i = 0 ; i < tab_links.length ; i++ ) {
		if ( tab_links[i].className == 'light' ) {
			tab_links[i].className = 'dark';
			tab_links[i].onclick = function onclick(event) { };
		} else {
			tab_links[i].className = 'light';
			tab_links[i].onclick = function onclick(event) { doTab(next_tab) };
		}
	}
	document.getElementById('tab_' + next_tab).style.display = 'none';
	document.getElementById('tab_' + new_tab).style.display = 'block';
	tab_container.style.backgroundImage = 'url(images/tabnav/2tab_' + new_tab + '_active.jpg)';
}

function showHide(id) {
	document.getElementById(id).style.display = ( document.getElementById(id).style.display == 'block' ) ? 'none' : 'block';
}

function showMap(id, path) {
	
	var tab_container = document.getElementById("map_links");
	var tab_links = tab_container.getElementsByTagName("a");	
	
	for ( var i = 0 ; i < tab_links.length ; i++ ) {
		tab_links[i].className = '';
	}
	tab_links[id].className = 'selected';
	
	document.getElementById('map_large').src = path;
}

function printWindow() {
   bV = parseInt( navigator.appVersion )
   if ( bV >= 4 ) window.print()
}


// checks for valid telephone characters
function validNum(telno) {
	
	var pattern = "0123456789+-)( ";
	var i = 0;
	
	do {
		var pos = 0;
		for ( var j = 0 ; j < pattern.length ; j++ )
			if ( telno.charAt(i) == pattern.charAt(j) )
				pos = 1;
		i++;
	}
	while ( pos == 1 && i < telno.length )
	
	if ( pos == 0 ) return false;
	
	return true;
}


// checks email matches pattern, use DART filter
function validEmail(addr) {
	
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var filter_enabled = true;  // enable ~ disable DART filter
	
	// match pattern
	if ( !regex.test(addr) ) {
		alert("Invalid email address");
		return false;
	} else {
		if ( filter_enabled ) {
			/******* ADDRESS FILTER *****
			In your form validation, note that in addition to standard email validation, the following keywords are not allowed in the email address:
			-> INFO, MARKETING, SALES, SUPPORT, ABUSE, NOC, SECURITY, POSTMASTER, HOSTMASTER, USENET, NEWS, WEBMASTER, WWW, UUCP, FTP and SPAM
			*******************************/
			var DART_invalid = 'INFO,MARKETING,SALES,SUPPORT,ABUSE,NOC,SECURITY,POSTMASTER,HOSTMASTER,USENET,NEWS,WEBMASTER,WWW,UUCP,FTP,SPAM';
			var inv_array = DART_invalid.split(',');
			_a = addr.split("@");
			addr = _a[0].toUpperCase();
			for ( var i = 0 ; i < inv_array.length ; i++ ) {
				if (addr == inv_array[i]) {
					alert("Email addresses starting with '" + inv_array[i] + "' are not accepted.\nPlease try another address.");
					return false;
				}
			}
		}
	}
	
	return true;
}

function validENews(theForm) {
	
	if (theForm.frm_nme.value == '') {
		alert("Please enter your Full Name");
		theForm.frm_nme.focus();
		return false;
	}
	if (!validEmail(theForm.frm_addr.value)) {
		theForm.frm_addr.focus();
		return false;
	}
	return true;
	
}

function validENewsUnsubscribe(theForm) {
	if (!validEmail(theForm.frm_addr.value)) {
		alert("Invalid email address");
		theForm.frm_addr.focus();
		return false;
	}
	return true;
}

function doSubscribe(theForm) {
	
	var full_name, email, country;
	
	full_name = theForm.frm_nme.value;
	email = theForm.frm_addr.value;
	country = theForm.frm_country.value;
	
	if ( full_name == 'Enter Full Name' || full_name == '' ) {
		alert("Please enter your Full Name");
		theForm.frm_nme.focus();
		return false;
	}

	if ( !validEmail( email ) ) {
		alert("Please enter a valid email");
		theForm.frm_addr.focus();
		return false;
	}
	
	if ( country == 'Choose Country' ) {
		alert("Please select your country of residence");
		theForm.frm_country.focus();
		return false;
	}
	
	var ajaxCall = '/ajax/subscribe.php?do=add_subscriber&attribs=';
	ajaxCall += full_name + '|';
	ajaxCall += email + '|';
	ajaxCall += country;
	
	var results = do_ajax_call( ajaxCall );
	
	if ( results != '' ) {
		document.getElementById("newsletter_message_form").style.display = 'none';
		document.getElementById("newsletter_message_box").style.display = 'block';
		document.getElementById("newsletter_message_box").innerHTML = '<img src="/images/preloaders/small.gif" border="0" />';
		timer = setTimeout("showMessage('"+results+"')",1500);
	}
	
	return false;
			
}

function showMessage(msg) {
	clearTimeout(timer);
	document.getElementById("newsletter_message_box").innerHTML = msg;
	if ( msg != 'You have been successfully<br />added to our mailing list.' )
		document.getElementById("newsletter_message_box").innerHTML += '<div style="padding: 7px 0 0 0;"><a href="javascript:;" onclick="javascript: resetSubsForm();">Try Again</a></div>';
}

function resetSubsForm() {
	document.getElementById("newsletter_message_box").style.display = 'none';
	document.getElementById("newsletter_message_form").style.display = 'block';
	document.getElementById("newsletter_message_box").innerHTML = '';
}

function doSubscribeFull(theForm) {
	
	var full_name, email, country, status;
	
	full_name = theForm.frm_nme.value;
	email = theForm.frm_addr.value;
	country = theForm.frm_country.value;
	substatus = theForm.frm_status.value;
	
	if ( full_name == 'Enter Full Name' || full_name == '' ) {
		alert("Please enter your Full Name");
		theForm.frm_nme.focus();
		return false;
	}

	if ( !validEmail( email ) ) {
		alert("Please enter a valid email");
		theForm.frm_addr.focus();
		return false;
	}
	
	if ( country == '' ) {
		alert("Please select your country of residence");
		theForm.frm_country.focus();
		return false;
	}
	
	var ajaxCall = '/ajax/subscribe.php?do=add_subscriber&attribs=';
	ajaxCall += full_name + '|';
	ajaxCall += email + '|';
	ajaxCall += country + '|';
	ajaxCall += substatus;
	
	var results = do_ajax_call( ajaxCall );

	if ( results != '' ) {
		document.getElementById("newsletter_message_form").style.display = 'none';
		document.getElementById("newsletter_message_box").style.display = 'block';
		document.getElementById("newsletter_message_box").innerHTML = '<img src="/images/preloaders/small.gif" border="0" />';
		timer = setTimeout("showMessage('"+results+"')",1500);
	}
	
	return false;
			
}
