/* 
Author: Jim Lam
Date: 22/06/10
Desc: Simulates HTML5 behavior to toggle form inputs original value if user does not enter anything.
*/
$(document).ready(function() {
	
	var stored_value = "";
	
	// when user focuses on a text input or textarea.
	$("input:text,textarea").focus(function(){
		
		// store the original value.	
		stored_value = $(this).val();
		
		// clear value.
		$(this).val("");
			
	});
	
	// when user unfocuses on a text input or textarea.
	$("input:text,textarea").blur(function(){
		
		// if user has NOT inputed anything.
		if ( $(this).val() == "" ) {
		
			// revert to original value.
			$(this).val( stored_value );
		
		}
		
	});
	
	// give your link a class of target-blank.
	$(".target-blank").click(function() {
		window.open( $(this).attr("href") );
		return false;
	});
	
	
	// one radio must be checked intially.
	var radio_count = 0;
	$(".radio").each(function(){
		if ( $(this).attr("checked") ) {
			radio_count =+ 1;	
		}
	});	
	if ( radio_count == 0 ) {
		$("#general-enquiry").attr("checked", true);
	}
	
	
	
	// red borders on error fields.
	$("#contact-form").submit(function(){
		
		var errors = 0;
		
	
		if ( $("#form-name").val() == '' || $("#form-name").val() == $("#form-name").next("span.default-value").text() ) {
			$("#form-name").addClass("form-error");
			errors =+ 1;
		}
		else {
			$("#form-name").removeClass("form-error");
		}
		
		
		
		if ( $("#form-email").val() == '' || $("#form-email").val() == $("#form-email").next("span.default-value").text() ) {
			$("#form-email").addClass("form-error");
			errors =+ 1;
		}
		else {
			$("#form-email").removeClass("form-error");
		}
		
		
		if ( $("#form-phone").val() == '' || $("#form-phone").val() == $("#form-phone").next("span.default-value").text() ) {
			$("#form-phone").addClass("form-error");
			errors =+ 1;
		}
		else {
			$("#form-phone").removeClass("form-error");
		}
		
		
		if ( $("#form-message").val() == '' || $("#form-message").val() == $("#form-message").next("span.default-value").text() ) {
			$("#form-message").addClass("form-error");
			errors =+ 1;
		}
		else {
			$("#form-message").removeClass("form-error");
		}
		
		
		if ( errors > 0 ) {
			return false;
		}
		
	});
	

	$("div#blog-news").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	$("div#contact-us").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	$("div#resource").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	



	
});
