function account_form_validate() {	
	jQuery('#phone').val(jQuery.trim(jQuery('#phone').val()));
	jQuery('#fax').val(jQuery.trim(jQuery('#fax').val()));
	jQuery('#mobile').val(jQuery.trim(jQuery('#mobile').val()));
	jQuery('#email').val(jQuery.trim(jQuery('#email').val()));
	jQuery('#email_confirm').val(jQuery.trim(jQuery('#email_confirm').val()));	
	jQuery('#address1').val(jQuery.trim(jQuery('#address1').val()));
	jQuery('#address2').val(jQuery.trim(jQuery('#address2').val()));
	jQuery('#address3').val(jQuery.trim(jQuery('#address2').val()));
	jQuery('#suburb').val(jQuery.trim(jQuery('#suburb').val()));
	jQuery('#state').val(jQuery.trim(jQuery('#state').val()));
	jQuery('#postcode').val(jQuery.trim(jQuery('#postcode').val()));
	
	var msg = new Array();
	var highlight = new Array();
	
	
	if(jQuery('#phone').val() == '') {
		msg[msg.length] =  '"Phone" can not be blank';
		highlight[highlight.length] = 'phone';
	}	
	if(jQuery('#email').val() != jQuery('#email_confirm').val()) {
		msg[msg.length] =  '"Email" not match';
		highlight[highlight.length] = 'email';
		highlight[highlight.length] = 'email_confirm';
	} else if(jQuery('#email').val() == '') {
		msg[msg.length] =  '"Email" can not be blank';
		highlight[highlight.length] = 'email';
		highlight[highlight.length] = 'email_confirm';
	} else if(!/^[a-z0-9,!\#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})/i.test(jQuery('#email').val())) {
		msg[msg.length] =  '"Email" is invalid';
		highlight[highlight.length] = 'email';
		highlight[highlight.length] = 'email_confirm';
	}
	if(jQuery('#password').val() != jQuery('#password_confirm').val()) {
		msg[msg.length] =  '"Password" not match';
		highlight[highlight.length] = 'password';
		highlight[highlight.length] = 'password_confirm';
	} else if(jQuery('#password').val().length < 5 && jQuery('#password').val().length != 0) {
		msg[msg.length] =  '"Password" length must be more than 4';
		highlight[highlight.length] = 'password';
		highlight[highlight.length] = 'password_confirm';
	}
	if(jQuery('#address1').val() == '') {
		msg[msg.length] =  '"Address 1" can not be blank';
		highlight[highlight.length] = 'address1';
	}
	if(jQuery('#suburb').val() == '') {
		msg[msg.length] =  '"Suburb" can not be blank';
		highlight[highlight.length] = 'suburb';
	}
	if(jQuery('#state').val() == '') {
		msg[msg.length] =  '"State" can not be blank';
		highlight[highlight.length] = 'state';
	}
	if(jQuery('#postcode').val() == '') {
		msg[msg.length] =  '"Postcode" can not be blank';
		highlight[highlight.length] = 'postcode';
	}
	
	var result = {'msg': msg, 'highlight': highlight };

	return result;
}

var global = {};

jQuery(document).ready(function() {
	jQuery(document).pngFix(); 
	if(jQuery('#sidebar #subs li').length == 0) {
		jQuery('#sidebar #subs').remove();	
	}
	jQuery('#login_button').live('click', function(e) {
		e.preventDefault();
		jQuery('#login_form').attr('action', '/index.php/function/login');
		jQuery('#login_form').submit();
		return false;
	});
	jQuery('#forget_password').live('click', function(e) {
		e.preventDefault();
		if(jQuery('#username').val() != '') {
			jQuery('#login_form').attr('action', '/index.php/function/forgot_password');
			jQuery('#login_form').submit();
		} else {
			alert('Please supply your username.');
		}
		return false;
	});
	jQuery('#search_submit').live('click', function(e) {
		e.preventDefault();
		jQuery('#search_form').submit();
		return false;
	});
	jQuery('#search_submit2').live('click', function(e) {
		e.preventDefault();
		jQuery('#search_form2').submit();
		return false;
	});
	jQuery('#order_form_submit').live('click', function(e) {
		e.preventDefault();
		jQuery('#go').click();		
	});
	jQuery('body.awards_index a.add_to_cart').live('click', function(e) {
		e.preventDefault();
		jQuery('#msg').css('display', 'none');
		jQuery('#add_to_cart_proceed').css('display', 'block');
		jQuery('#processing').css('display', 'none');
		jQuery.blockUI({ 
			message: '	<div id="add_to_cart_overlay"><a id="add_to_cart_cancel" href="#"></a><form id="add_to_cart_form" action="/index.php/function/add_to_cart" method="post"><h1>Quantity</h1><p>Please specify the quantity of this particular award<br/>you would like to purchase.</p>	<p><input type="text" id="qty" name="qty" value="1"/><span id="desc_text"></span><input type="hidden" id="uuid" name="uuid" value=""/><input type="hidden" id="points" name="points" value=""/><input type="hidden" id="description" name="description" value=""/><input type="hidden" id="code" name="code" value=""/></p>	<a id="add_to_cart_proceed" href="#">Proceed</a><img id="processing" src="/images/busy.gif" alt="processing"/>    <p id="msg"></p></form></div>', 
			css: { 
				border: 'none', 
				padding: '0',
 				width: '376px',
 				height: '195px',
 				left: '40%',
				backgroundColor: '#fff',
				cursor: 'auto'
			},
			overlayCSS:  {
				backgroundColor: '#eee',
				opacity: 0.6,
				cursor: 'auto'
			},
			fadeIn: 500
		});
		jQuery('#add_to_cart_overlay #uuid').val(jQuery(this).siblings('.uuid').val());
		jQuery('#add_to_cart_overlay #points').val(jQuery(this).siblings('.points').val());
		jQuery('#add_to_cart_overlay #description').val(jQuery(this).siblings('.desc').html());
		jQuery('#add_to_cart_overlay #desc_text').html(jQuery(this).siblings('.desc').html());
		jQuery('#add_to_cart_overlay #code').val(jQuery(this).siblings('.code').html());
		jQuery('#add_to_cart_overlay #add_to_cart_cancel').bind('click', function(e) {
			e.preventDefault();
			jQuery.unblockUI();
		});
		global.selected_award = jQuery(this).parent();
		jQuery('#add_to_cart_overlay #add_to_cart_proceed').bind('click', function(e) {
			e.preventDefault();
			jQuery('#msg').html('').css('display', 'none');
			jQuery('#add_to_cart_overlay #qty').val(jQuery.trim(jQuery('#add_to_cart_overlay #qty').val()));
			if(isNaN(jQuery('#add_to_cart_overlay #qty').val()) || jQuery('#add_to_cart_overlay #qty').val().length==0) {				
				jQuery('#add_to_cart_overlay #qty').val("1");
				return false;
			} else {
				jQuery('#add_to_cart_overlay #qty').val(''+parseInt(jQuery('#add_to_cart_overlay #qty').val()));
				if(parseInt(jQuery('#add_to_cart_overlay #qty').val()) < 1) {
					jQuery('#add_to_cart_overlay #qty').val("1");
					return false;
				}
			}
			var sum = 0;
	        jQuery(".added_points").each(function() {
	            if(!isNaN(this.value) && this.value.length!=0) {
					if(jQuery(this).parent().parent().find('.desc:first').html() != jQuery('#add_to_cart_overlay #description').val()) {						
						sum += parseInt(this.value);
					}
	            }	 
	        });
			if(parseInt(jQuery('#balance').val()) < sum + parseInt(jQuery('#add_to_cart_overlay #points').val()) * parseInt(jQuery('#add_to_cart_overlay #qty').val())) {				
				jQuery('#msg').html('Not enough balance').css('display', 'block');
				return false;
			}			
			
			jQuery('#add_to_cart_proceed').css('display', 'none');
			jQuery('#processing').css('display', 'block');
			jQuery.ajax({
				type: "POST",
				data: jQuery('#add_to_cart_form').serialize(),
				url: jQuery('#add_to_cart_form').attr('action'),
				timeout: 30000,
				dataType: 'text',
				error: function() {
					jQuery('#processing').css('display', 'none');
					jQuery('#msg').html('Sorry, there is an error on the website').css('display', 'block');
				},
				success: function(data) {
					if(data.indexOf("success") == 0) {
						var qty = data.substring(7);
						jQuery('#processing').css('display', 'none');
						setTimeout('jQuery.unblockUI({ fadeOut: 300 });', 1000);
						jQuery('span.added', global.selected_award).html('<img src="/images/cart.gif" alt=""/>x ' + qty);
					} else {
						jQuery('#processing').css('display', 'none');
						jQuery('#msg').html(data).css('display', 'block');
					}
				}
			});
		});
		
	});
	jQuery('#cart_update').live('click', function(e) {
		e.preventDefault();
		var error = "";
		jQuery('#cart_table .qty').removeClass('error');
		jQuery('#cart_table .qty').each(function() {
			jQuery(this).val(jQuery.trim(jQuery(this).val()));
			if(jQuery(this).val().search(/^[1-9][0-9]*$/) == -1 && jQuery(this).val() != "0") {				
				jQuery(this).addClass('error');				
				error = "qty";			
			}
		});
		
		var sum = 0;
		jQuery('#cart_table .qty').each(function() {
			sum += parseInt(jQuery(this).val()) * parseInt(jQuery(this).parent().parent().find('.points').val());
		});
		if(sum > parseInt(jQuery('#balance').val())) {
			error = "balance";
		}
		
		if(error == "") jQuery('#cart_form').submit();
		else if(error == "qty") alert('Please put in correct "Qty"');
		else if(error == "balance") alert('Not enough balance');
		return false;
	});
	jQuery('#account_form_submit').live('click', function(e) {
		e.preventDefault();
		jQuery('#account_form .error').removeClass('error');
		jQuery('#account_form_validate').html('');

		var result = account_form_validate();
		var msg = result.msg;
		var highlight = result.highlight;
		if(msg.length == 0) {
			jQuery('#account_form').submit();
		} else {
			var html = '';
			for(var i in msg) {
				html += '<span>' + msg[i] + '</span>';
			}
			jQuery('#account_form_validate').html(html);

			for(var j in highlight) {
				jQuery('#' + highlight[j]).parent().parent().addClass('error');
			}
		}
		
	});
	
	

});