/* File: std_form.js  */
/* Ver: 2010-03-12    */

/********************************
 the following routines require 
 jquery.js
 jquery.scrollTo.js
 jquery-ui.js
*********************************/
function hideAckError(){
	$('#ackerror').fadeOut(400);
}


// validate email
function checkEmail(val) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}
// validate form
function validateForm(elem) {
	var submit = true;
	var this_form = '#' + $(elem).parents('form').attr('id');
	// reset errors
	$(this_form).find('.error').hide();
	// check for errors in input and textareas
	$(this_form).find('.required').each(function() {
		var pre_pop = '';
		if ($(this).hasClass('pre_pop')) {
			pre_pop = $(this).prev('label').text();
		}
		if ($(this).val() == '' || $(this).val() == pre_pop) {
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
		if ($(this).attr('type') == 'text' && $(this).attr('alt') == 'email_address') {
			check_email = checkEmail($(this).val());
			if (!check_email) {
				submit = false;
				$(this).next('.error').fadeIn(400);
			}
		}
	});
	// check for errors in grouped fields
	$(this_form).find('.group_required input').each(function() {
		if ($(this).val() == ''){
			submit = false;
			$(this).parents('.group_required').next('.error').fadeIn(400);
		}
	});
	// check for errors in checkboxes and radios
	$(this_form).find('.required_choice').each(function() {
		if ($('input:checked', this).size() == 0){
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
	});
	
	$(this_form).find('.required_checked').each(function() {
		//alert('#'+$(this).attr('id')+":checked");
		if($('#'+$(this).attr('id')+":checked").val()==null){
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
	});
	
	// check for errors with fields that must match
	$(this_form).find('.required_match').each(function() {
		var sibling = '#' + $(this).attr('rel');
		if ($(this).val() != $(sibling).val()) {
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
	});
	$(this_form).find('.required_dateformat').each(function(){
		if ($(this).val() == ''){
			submit = false;
			$(this).next('.error').fadeIn(400);
		}else{
			var re = /^\d{2}\/\d{2}\/\d{4}$/; 
			if(!$(this).val().match(re)) { 
				submit = false;
				$(this).next('.error').fadeIn(400); 
			}else{
	

			   var startDate = Date.parse($(this).val());
			   var today = new Date();
			   // Check the date range, 86400000 is the number of milliseconds in one day
			   // difference = the number of days difference
			   var difference = (today - startDate) / (86400000);
			   var maxDaysDifference = 60;
			   //alert(difference);
			   if (difference < 0) {
			       submit = false;
					$(this).next('.error').fadeIn(400);

			   }
			   if (difference > maxDaysDifference) {
			       submit = false;
					$(this).next('.error').fadeIn(400);
			    }

			}
		}
	});
	$(this_form).find('.required_phonenumber').each(function(){
		if ($(this).val() == '') {
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
		else {
			var re = /^\d{10}$/;
			if (!$(this).val().match(re)) {
				submit = false;
				$(this).next('.error').fadeIn(400);
			}
		}		
	});
	$(this_form).find('.required_currency').each(function(){
		if ($(this).val() == '') {
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
		else {
			var re = /^\d+\.\d{2}$/;
			if (!$(this).val().match(re)) {
				submit = false;
				$(this).next('.error').fadeIn(400);
			}
		}		
	});	
	return submit;
}

// onload routines
$(function(){

	// show errors returned by server
	$('.show_error').fadeIn(400);

	// pre-populate form fields
	$("form .pre_pop").each(function() {
		var label = $(this).prev('label').text();
		$(this).val(label);
		$(this).focus(function() {
			var tmp_val = $(this).val();
			if (tmp_val == label) {
				$(this).val("");
			}
		});
		$(this).blur(function() {
			var tmp_val = $(this).val();
			if (tmp_val == "" || tmp_val == null) {
				$(this).val(label);
			}
		});
	});

	// form validation
	$('.validate_form input[type="submit"]').click(function() {
		return validateForm(this);
	});

	// clear error messages upon blur
	$('.validate_form .required').blur(function() {
		if ($(this).val() != '') {
			var next_error = $(this).next('.error');
			$(next_error).fadeOut(400, function() {
				$(next_error).removeClass('show_error');
			});
		}
	});
	$('.validate_form .required_dateformat').blur(function() {
		if ($(this).val() != '') {
			var remove = true;
			var re = /^\d{2}\/\d{2}\/\d{4}$/; 
			if(!$(this).val().match(re)) { 
				remove = false;
			}else{
			   var startDate = Date.parse($(this).val());
			   var today = new Date();
			   // Check the date range, 86400000 is the number of milliseconds in one day
			   // difference = the number of days difference
			   var difference = (today - startDate) / (86400000);
			   var maxDaysDifference = 60;
			   //alert(difference);
			   if (difference < 0) {
			       remove = false;
			   }
			   if (difference > maxDaysDifference) {
			       remove = false;
			    }
			}
			if(remove){
				var next_error = $(this).next('.error');
				$(next_error).fadeOut(400, function() {
					$(next_error).removeClass('show_error');
				});
			}
		}
	});
	$('.validate_form .required_currency').blur(function() {
		if ($(this).val() != '') {
			var remove = true;
			var re = /^\d+\.\d{2}$/;
			if (!$(this).val().match(re)) {
				remove = false;
			}
			if(remove){
				var next_error = $(this).next('.error');
				$(next_error).fadeOut(400, function() {
					$(next_error).removeClass('show_error');
				});
			}
		}
	});
	$('.validate_form .required_phonenumber').blur(function() {
		if ($(this).val() != '') {
			var remove = true;
			var re = /^\d{10}$/;
			if (!$(this).val().match(re)) {
				remove = false;
			}
			
			if(remove){
				var next_error = $(this).next('.error');
				$(next_error).fadeOut(400, function() {
					$(next_error).removeClass('show_error');
				});
			}
		}
	});
	
	$('.validate_form .required_choice input').click(function() {
		var next_error = $(this).parents('.required_choice').next('.error');
		$(next_error).fadeOut(400, function() {
			$(next_error).removeClass('show_error');
		});
	});

});

