// JavaScript Document
var basepath = "/";

$().ready(function(){		
	check_form_validation(false);  
});

function check_form_validation(show_message){
	$('.validate').each(function(){
		if($(this).attr("type") == "text"){
			var current_width = $(this).css("width");
			current_width = current_width.replace("px","");
			var new_width = (current_width - 8)+'px';		
			$(this).css({'padding-right': '16px', 'width': new_width });	
		}//enf if
		if($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio"){
			$(this).css({'border': 'none', 'background-image': 'none' });	
		}//end if
	});
	$("form").submit(function(){
		if($(".validate").size() > 0){
			
			var errors = false;		
			$(".validate").each(function(){										 
				$father = $(this).parent();				
				switch($(this).attr("type")){
					case "radio":
					case "checkbox":
						var radio_name = $(this).attr("name");					
						if(!$("input[name="+radio_name+"]").is(":checked")){
							errors = true;
							if(show_message == true){
								if($father.find("em").size() == 0){
									$(this).parent().append("<em>Compulsory field</em>");
								}//end if
							}//end if
							$(this).parent().addClass("error");	
						} else {
							if(show_message == true){
								$father.find("em").each(function(){
									$(this).remove();								 
								});	
							}//end if
							$(this).parent().removeClass("error");			
						}//end if						
						break;
					case "text":
					default:						
						if($(this).val() == ""){						
							errors = true;
							if(show_message == true){
								if($father.find("em").size() == 0){
									$(this).after("<em>Compulsory field</em>");
								}//end if
							}//end if
							$(this).parent().addClass("error");					
						} else {
							if(show_message == true){
								$father.find("em").each(function(){
									$(this).remove();								 
								});	
							}//end if
							//check if it's an email
							if($(this).attr("name") == "email"){
								if(!isValidEmailAddress($(this).val())){
									errors = true;
									if(show_message == true){
										if($father.find("em").size() == 0){
											$(this).after("<em>Invalid email address</em>");
										}//end if
									}//end if
									$(this).parent().addClass("error");									   
								} else {
									$(this).parent().removeClass("error");
									if(show_message == true){
										$father.find("em").each(function(){
											$(this).remove();								 
										});	
									}//end if
								}
							} else {
								$(this).parent().removeClass("error");						
							}//end if
						}//end if
						if($(this).parent().is('.error')){
							$(this).focus(function(){
								$(this).parent().removeClass("error");					   
							});
						}//end if
					break;
				}//end switch
			});
			if(errors == false){
				return true;	
			} else {
				return false;
			}
		} else {
			return true;	
		}
	});	
}

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);
}
