// JavaScript Document
var img_rnd = new Array ("highlights_christy_1.png", "highlights_christy_2.png",
"highlights_christy_3.png");

var r = Math.floor(3*Math.random());

function scrollPage(target) {
	//get the full url - like mysitecom/index.htm#home
	var full_url = target.href;
	
	//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
	var parts = full_url.split("#");
	var trgt = parts[1];
	var page = trgt.split("/");
	var loadPage = page[1];
	
	if (page[0] == "content") {
		var scollto = "#main_content";	
	} else {
		var scollto = "#highlights";
		loadPage = "special.html";
	}
	//get the top offset of the target anchor
	var target_offset = $(scollto).offset();
	var target_top = target_offset.top - 120;

	//goto that anchor by setting the body scroll top to anchor top
	$('html, body').animate({scrollTop:target_top}, 1000, 'easeOutQuad');
	
	publishContent(loadPage);
}

function publishContent(page) {
	var toLoad = page;		
	
	/*if (toLoad == "contact.html") {
		$('#main_content').css('height', '2200px');
	} else {
		$('#main_content').css('height', '700px');
	}*/
	$('#content_holder').fadeOut('fast', function(){
		//PROBLEM WITH FIREFOX DIRECTLY DO THE CALLBACK, HENCE MANUAL FUNCTION WHERE INTRODUCED HERE
		//$('#content').load(toLoad,'',showNewContent());
		loadContent();	
	});
	//$('#content').hide('fast', loadContent);
	$('#load').remove();
	//$('#wrapper').append('<span id="load">LOADING...</span>');
	$('#load').fadeIn('normal');
	//window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
	function loadContent() {
		//$('#content_holder').load(toLoad,'',showNewContent());
	}
	function showNewContent() {
		$('#content_holder').fadeIn('normal');
		var loadTimeout = setTimeout(resize, 500);
		
	}
	
}

$(document).ready(function(){
	$('.error').hide();
	
	var pathname = $(location).attr('href');
	/*var parts = pathname.split("#");
	var trgt = parts[1];
	var page = trgt.split("/");*/
	if (pathname.indexOf("#") != -1) {
		var parts = pathname.split("#");
		var trgt = parts[1];
		var page = trgt.split("/");
		
		if (page[0] == "content") {
			var scollto = "#main_content";	
			var loadPage = page[1];
		} else {
			var scollto = "#highlights";
			var loadPage = "special.html";
		}
		//get the top offset of the target anchor
		var target_offset = $(scollto).offset();
		var target_top = target_offset.top - 120;
	
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 1000, 'easeOutQuad');
		
		publishContent(loadPage);
	} else {
		//publishContent("special.html");
	}
	
	$("#random_model").attr('src', 'assets/images/' + img_rnd[r]);
	//, ".footer_title_link"
	$(".navi_link").click(function(event){
		//prevent the default action for the click event
		//event.preventDefault();
		scrollPage(this);		
	});
	
	$(".footer_title_link").click(function(event){
		//prevent the default action for the click event
		//event.preventDefault();
		scrollPage(this);		
	});
	$(".footer_link").click(function(event){
		//prevent the default action for the click event
		//event.preventDefault();
		scrollPage(this);		
	});
	
	// Add this behavior to all text fields
	$('.text_field').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	$("#submit_btn").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();
			
		if (pathname.indexOf("?") != -1) {
			var sparts = pathname.split("?");
			var strgt = sparts[1];
			var spage = strgt.split("=");
			var source = spage[1];
		} else {
			var source = "";
		}
		
		var name = $("input#uname").val();
		if (name == "Name") {
			$("label#uname_error").show();
			$("input#uname").focus();
			return false;
		}
		
		var gender = $('input:radio[name=gender]:checked').val();
		if (gender == undefined) {
			$("label#gender_error").show();
			$("input:radio[name=gender]").focus();
			return false;
		}
		
		var selected_age = $("#age2 option:selected"); 
		var age = selected_age.val();
		if (age == "") {
			$("label#age2_error").show();
			return false;
		} 
		
		var phone = $("input#ucontact").val();
		if (phone == "Contact Number") {
			$("label#ucontact_error").show();
			$("input#ucontact").focus();
			return false;
		}
		
		var email = $("input#uemail").val();
		if (email == "Email") {
			$("label#uemail_error").show();
			$("input#uemail").focus();
			return false;
		} else if(!isValidEmailAddress(email)) {
			$("label#uemail_error").show();
			$("input#uemail").focus();
			return false;
		}
				
		var selected_preferred_centre = $("#preferred_centre2 option:selected"); 
		var preferred_centre = selected_preferred_centre.val();
		if (preferred_centre == "") {
			$("label#preferred_centre2_error").show();
			return false;
		} 
		
		var dataString = 'name='+ name + '&gender=' + gender + '&age=' + age + '&phone=' + phone + '&email=' + email + '&preferred_centre=' + preferred_centre + '&source=' + source;
		//alert (dataString);return false;
		
		$('#contact_form').html("<div id='message'></div>");
		$('#message').html("<table class=\"form_tbl\"><tr><td width=\"12\"></td><td height=\"20\"></td></tr><tr><td width=\"12\"></td><td>Please wait for a moment while the system <br />is processing your form</td></tr></table>")
				
		$.ajax({
			type: "POST",
			url: "inc/process.php",
			data: dataString,
			success: function() {
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<table class=\"form_tbl\"><tr><td width=\"12\"></td><td height=\"20\"></td></tr><tr><td width=\"12\"></td><td>Your form has been submitted.<br />Thank you for writing to us!</td></tr></table>")
	//			.append("<p>We will be in touch soon.</p>")
				.hide()
				.fadeIn(1500);
		  }
		 });
		return false;
	});
	
	$("a#foot_2").fancybox({
		'titleShow'     : false
	});
	
	$("#foot_3").fancybox({
		'titleShow'     : false
	});
});

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

var resize = function() {
	var new_height = $(".content_details").height() + 200;
	$('#main_content').animate({
        height: new_height
  		}, 500, function() {
    // Animation complete.
  });
  
	//$('#main_content').css({"height": new_height + "px"});
}

function numbersonly(e, decimal) {
	var key;
	var keychar;

	if (window.event) {
	   key = window.event.keyCode;
	}
	else if (e) {
	   key = e.which;
	}
	else {
	   return true;
	}
	keychar = String.fromCharCode(key);

	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
	   return true;
	}
	else if ((("0123456789-").indexOf(keychar) > -1)) {
	   return true;
	}
	else if (decimal && (keychar == ".")) { 
	  return true;
	}
	else
	   return false;
}

function goLite(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.color = "#bd4e8a";
}

function goDim(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.color = "#FFF";
}


