var eager = false;	
var first_invalid_obj;

// Show the tip above the input
function show_tip(selector) {
	var id = "#" + selector;
	var o = $("<div class='tip' id='" + selector + "_tip'>" + ($(id).attr("errorMsg") ? $(id).attr("errorMsg") : $(id).attr("title")) + "</div>");
	
	$("body").append(o);  o.show().hide();
	
	var l = $(id).offset().left + $(id).width()/2 - 95;
	var t = $(id).offset().top - o.height() - 23; // minus padding
	o.css("left" , l + "px" );
	o.css("top"  , t  + "px");
	o.fadeIn('fast');
}		

// Hide tip
function hide_tip(selector) {
	var s = "#" + selector + "_tip";
	if ($(s)) {
		$(s).fadeOut('fast');
		setTimeout("$('" + s + "').remove()", 1000);
	}
}

// Is an input valid, to be used with is_form_valid();
function is_valid(obj) {
	var valid = true;
	
	if (obj.hasClass("required")) {
		valid = obj.val() != "" && obj.val() != obj.attr("title");
	}
	
	if (valid && obj.val()!= obj.attr("title") && obj.val().length > 0) {
		if (valid && (obj.hasClass("email"))) {			
			valid = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/.test(obj.val());
		}
		if (valid && (obj.hasClass("number") || obj.hasClass("numbers") || obj.hasClass("onlyNumbers"))) {
			valid = /^[0-9\ ]+$/.test(obj.val());
		}
		if (valid && (obj.hasClass("letter") || obj.hasClass("letters") || obj.hasClass("onlyLetters"))) {
			valid = /^[a-zA-Z\ \']+$/.test(obj.val());
			console.log(obj.attr("id") + " - " + valid);
		}
		if (valid && (obj.hasClass("noSpecialCharacters") || obj.hasClass("noSpecialCharacter"))) {
			valid = /^[0-9a-zA-Z]+$/.test(obj.val());
		}
		if (valid && (obj.hasClass("date"))) {
			valid = /(?:0?[1-9]|[12][0-9]|3[01])\/(?:0?[1-9]|1[0-2])\/(19\d{2}|20\d{2}|\d{2})$/.test(obj.val());
		}
		/*
		if (valid && obj.attr("minLength")!=null) {
			valid = (obj.val().length >= obj.attr("minLength")) ? true : false;
		}
		if (valid && obj.attr("maxLength")!=null) {
			valid = (obj.val().length <= obj.attr("maxLength")) ? true : false;
		}
		if (valid && obj.attr("minRange")!=null) {
			valid = (obj.val() >= obj.attr("minRange")) ? true : false;
		}
		if (valid && obj.attr("maxRange")!=null) {
			valid = (obj.val() <= obj.attr("maxRange")) ? true : false;
		}
		if (valid && (obj.attr("confirmTo") !=null)) {
			valid = (obj.val() == $("#" + obj.attr("confirmTo")).val());
		}*/
	}
	return valid;
}

// Validate all input and start eager mode
function is_form_valid() {
	var form_valid = true;
	eager = true;
	
	first_invalid_obj = null;
	$("input, select, textarea").each(function() {
		if (!is_valid($(this))) {
			if (form_valid) first_invalid_obj = $(this);
			form_valid = false;
			$(this).addClass("error");
		}		
	});
	
	if (!form_valid) first_invalid_obj.focus();
	
	return form_valid;
}

// Fill a select check box
function fill_select(id, value) {
	$("#" + id + " option:selected").removeAttr('selected');
	$("#" + id + " option[value='" + value + "']").attr('selected','selected'); //HCM
	$("#" + id).trigger("change");
}

// Remove dirty input mark and any error
function remove_dirty_input() {
	$("input:text, select, textarea").blur();
	$("input, textarea, select").each(function(){
		if ($(this).val() != $(this).attr("title")) {
			$(this).removeClass("notdirty");
		}
	});
}

// Remove default value before submission
function clear_clean_input() {
	$("input, textarea, select").each(function(){
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	});
}


// Initialise the form
$(document).ready(function(){
	// on focus, show default help text
	$("input:text, textarea").focus(function(srcc) {			
		if ($(this).val() == $(this).attr("title")) {			
			$(this).val("");
		}
		$(this).removeClass("notdirty");
		$(this).addClass("focus");
		
		if (eager && !is_valid($(this))) {
			show_tip($(this).attr("id"));
		}
	});
	
	// when blur
	$("select").blur(function() {
		if (eager) {			
			if (!is_valid($(this))) {
				$(this).addClass("error");
			} else {
				$(this).removeClass("error");
			}
		}
	});
	$("input:text, textarea").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("notdirty");
			$(this).val($(this).attr("title"));
		}
		
		$(this).removeClass("focus");
		
		if (eager) {
			hide_tip($(this).attr("id"));
			
			if (!is_valid($(this))) {
				$(this).addClass("error");
			} else {
				$(this).removeClass("error");
			}
		}
	});
	$("input:text, select, textarea").blur();
});


