
//variable is used to store the program id when the user clicks "description"
$PROGRAM_ID = 0;

//after loading the initial page, it's stored in this variable
$BODY = "";

$FADEBOX_COUNTER = 0;
var $FADEBOX = new Array();
$FADEBOX[0] = "";
$FADEBOX[1] = "volunteering and culture";
$FADEBOX[2] = "adventure and relaxation";
$FADEBOX[3] = "... people from all over the world ...";
$FADEBOX[4] = "All in one program!"
$FADEBOX[5] = "... a memory that will last a lifetime!";
$FADEBOX[6] = "The <span style=\"color:#F58220\">In 2 Asia programs</span>, starting at $1033.";
$FADEBOX[7] = "Click any country in Asia for more information";
$FADEBOX[8] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('35', 'loaderasia')\">Indonesia</span>";
$FADEBOX[9] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('30', 'loaderasia')\">Nepal</span>";
$FADEBOX[10] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('29', 'loaderasia')\">Sri Lanka</span>";
$FADEBOX[11] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('23', 'loaderasia')\">China</span>";
$FADEBOX[12] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('27', 'loaderasia')\">Thailand</span>";
$FADEBOX[12] = "<span style=\"cursor:pointer\" onClick=\"getPrograms('26', 'loaderasia')\">Vietnam</span>";

function getPrograms(id, loader) {
	
	$lang = $('lang').value
	
	$(loader).style.display = 'block';
	
	new Ajax.Request('/php/getprograms.php',
	{
		method:'post',
		onSuccess: function(transport) {
			var response = transport.responseText || "empty";
			
			$('left_content').innerHTML = response;
			$(loader).style.display = 'none';
		},
		parameters: {id: id, lang: $lang}
	});
}

function sendMailList(e) {
	
	$('maillistmessage').innerHTML = "";
	new Effect.Appear($('maillistmessage'), {duration: 0.01});
	
	var characterCode;
	
	if(e && e.which){ 
		e = e;
		characterCode = e.which;
	} else {
		e = event
		characterCode = e.keyCode
	}
	
	if(characterCode == 13){ 
		emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
		
		if (emailRe.test($('maillist').value)) {
			new Ajax.Request('/php/saveformaillist.php',
			{
				method:'post',
				onSuccess: function(transport) {

					var response = transport.responseText || "empty";

					$('lightboxloader').style.visibility = 'hidden';

					$('maillistmessage').innerHTML = response;
				},
				onFailure: function(){

					$('lightboxloader').style.visibility = 'hidden';

					$('maillistmessage').innerHTML = "can not save email address";
				},
				parameters: {email: $('maillist').value}
			});
		} else {
			$('maillistmessage').innerHTML = "invalid email address";
		}
	}
	
	setTimeout("new Effect.Fade($('maillistmessage'), { duration: 1.0 });", 9000)
}

function start() {
	preLoadImages(); 
	new Effect.Appear('worldmap_container', { duration: 5.0 });
	$('container').style.visibility = 'visible'
	
	window.setTimeout("setCountriesLayer('map_asia_.gif', 'asia')", 5000);
	
	setTimeout("$('topsouthamerica').href = \"javascript:setCountriesLayer('map_southamerica_.gif', 'southamerica')\"", 8000);
	setTimeout("$('topafrica').href = \"javascript:setCountriesLayer('map_africa_.gif', 'africa')\"", 8000);
	setTimeout("$('topnorthandcentralamerica').href = \"javascript:setCountriesLayer('map_northandcentralamerica_.gif', 'northandcentralamerica')\"", 8000);
	setTimeout("$('topeurope').href = \"javascript:setCountriesLayer('map_europe_.gif', 'europe')\"", 8000);
	setTimeout("$('topasia').href = \"javascript:setCountriesLayer('map_asia_.gif', 'asia')\"", 8000);
	setTimeout("$('topaustralia').href = \"javascript:setCountriesLayer('map_australia_.gif', 'australia')\"", 8000)
	
	setTimeout("new Effect.Appear('container', {duration: 1.0})", 2000);
	
	$BODY = $('body').innerHTML;
	
	setTimeout("switchFadeBox()", 6000);
}

function switchFadeBox() {
	
	new Effect.Fade($('fadebox'), { duration: 2.0 });
	
	if ($FADEBOX_COUNTER >= ($FADEBOX.length-1)) {
		$FADEBOX_COUNTER = 0;
	}
	setTimeout("$('fadebox').innerHTML = $FADEBOX[$FADEBOX_COUNTER]", 2002)
	
	setTimeout("new Effect.Appear($('fadebox'), {duration: 2.0})", 2003); 
	
	$FADEBOX_COUNTER++;
	
	setTimeout("switchFadeBox()", 6000);
}

function reload() {
	$('body').innerHTML = $BODY;
	
	start();
}

function saveemail() {
	
	$('lightboxloader').style.visibility = 'visible';
		
	$emailaddress = $('email').value;
	
	new Ajax.Request('/php/saveemail.php',
	{
		method:'post',
		onSuccess: function(transport) {
			
			var response = transport.responseText || "empty";
			
			$('lightboxloader').style.visibility = 'hidden';
			
			$('emailsavemessage').innerHTML = response;
		},
		onFailure: function(){
			
			$('lightboxloader').style.visibility = 'hidden';
			
			$('emailsavemessage').innerHTML = "There was an error saving your email address.";
		},
		parameters: {email: $emailaddress, emailsfor: EMAILSFOR}
	});
}

function getDescription(program) {

	$PROGRAM_ID = program;
	$lang = $('lang').value
	
	//$(loader).style.display = 'block';
	
	new Ajax.Request('/php/getdescription.php',
	{
		method:'post',
		onSuccess: function(transport) {
			
			var response = transport.responseText || "empty";
			
			/*	this will return two possibilities; all good or not all good;)
				some JSON will lead the way here; if it's all good, we will know
				and we will also no where to go. If all is not well; somehow we
				will also know this and ask the user's help to make it all good;)
			
			*/
			
			response = eval('('+response+')');
			
			if (response[0] == 1) {
				//user is authenticated, show the program description
				description_window = window.open(response[1],'description','status=no, menubar=no, location=yes, scrollbars=yes, width=1024')
			} else {
				//user is not authenticated, authenticate
				lightDisplay(1, 300, 300, '/php/getHTML.php', {template: 'signin.html'});
			}
			
			//$(loader).style.display = 'none';
		},
		parameters: {program: program, lang: $lang}
	});
}

function authenticate() {

	if ($('emaill').value != "" && $('pw').value != "") {
		
		/*email = $('emaill').value;
		temp = $('pw').value;
		pw = hex_md5(temp);*/
		
		

		new Ajax.Request('/php/authenticate.php',
		{
			method:'post',
			onSuccess: function(transport) {
				
				var response = transport.responseText || "empty";
				
				response = eval('('+response+')');
				
				if (response[0] == 0) {
					$('signinmessage').innerHTML = response[1];
				} else if (response[0] == 1) {
					$('signinmessage').innerHTML = response[1];
					//we will save this users email address in a hidden input field
					$('emailhidden').value = response[2];
				
					setTimeout("getDescription($PROGRAM_ID)", 2000);
					setTimeout("lightHide()", 2000);
				} else if (response[0] = 2) {
					$('lightbox').style.height = '400px';
					$('signinmessage').innerHTML = response[1];
					
					$('confirm_container').innerHTML = response[2];
				}

				
			},
			parameters: {email: $('emaill').value, pw: hex_md5($('pw').value)}
		});
		
	} else {
		$('signinmessage').innerHTML = "Please enter your email address and password.";
	}
}

function getSignin() {
	
	$('lightbox').style.height = '400px';
	
	new Ajax.Request('/templates/register.html',
	{
		method:'get',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";
			
			$('lightboxcontent').innerHTML = response
		}
	});
}

function register() {
	
	$('registermessage').innerHTML = "";
	
	if($('email').value != "" && $('pw1').value != "" && $('pw2').value != "") {
		emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
		if (emailRe.test($('email').value)) {
			if ($('pw1').value == $('pw2').value) {
				
				email = $('email').value;
				pw = hex_md5($('pw1').value);
				
				new Ajax.Request('/php/register.php',
				{
					method:'post',
					onSuccess: function(transport) {

						var response = transport.responseText || "empty";

						response = eval('('+response+')');
						
						if (response[0] == 0) {//not ok
							$('registermessage').innerHTML = response[1];
						} else if (response[0] == 1) {//ok
							$('registermessage').innerHTML = response[1];
							
							setTimeout("getDescription($PROGRAM_ID)", 2000);
							setTimeout("lightHide()", 2000);
						}
					},
					parameters: {email: email, pw: pw}
				});
				
			} else {
				$('registermessage').innerHTML = "Sorry, the passwords don't match.";
			}
		} else {
			$('registermessage').innerHTML = "Sorry, the email address is invalid.";
		}
	} 
}

function verify() {
	if ($('vcode').value != "") {
		
		new Ajax.Request('/php/verify.php',
		{
			method:'get',
			onSuccess: function(transport) {

				var response = transport.responseText || "empty";

				if (response == 'ok') {
					$('verifymessage').innerHTML = "You have successfully confirmed your email address.";
										
					setTimeout("getDescription($PROGRAM_ID)", 2000);
					setTimeout("lightHide()", 2000);
				} else if (response == 'not') {
					$('verifymessage').innerHTML = "You have entered an incorrect code. Please try again.";
				}
			},
			parameters: {vcode: $('vcode').value, t: 2}
		});
		
	} else {
		$('verifymessage').innerHTML = "Please enter a code.";
	}
}