/***************************************************************************************
* Validates a form data ....
***************************************************************************************/

var form_submitted = false;

/*jQuery(document).ready
(
	function()
	{
		$('form').each
		(
			function(i)
			{
				$(this).submit
				(
					function()
					{
						var id = $(this).attr('id');
						
						return validate_form(id);
					}
				);
			}
		);    
	}
);*/



function validate_form(form_id)
{
	var form = jQuery('#' + form_id);
	var valid = true;
	
	// form not found ...
	if(form.length == 0)
	{
		alert('A form with id "' + form_id + '" not found!');
		
		return false;
	}
	
	// loop through the controls ...
	jQuery('#' + form_id + ' *').each
	(
		function(index)
		{
			var tagName = jQuery(this)[0].tagName;
			
			if(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA')
			{
				var control = jQuery(this);
					
				valid = validate_control(control);

				return valid;
				
			}
			
		}
	);
	
	return valid;
}

function validate_control(control)
{
	
	var valid = true;
	
	// is it required?    
				
	valid = check_required(control) && valid;
	
	if(valid == false)
		return false;
	
	// check for a valid email ...
	
	valid = check_email(control) && valid;
	
	if(valid == false)
		return false;
	
	// validate a integer
	
	valid = check_integer(control) && valid;
	
	if(valid == false)
		return false;
	
	
	// validate a positive integer
	valid = check_positive_integer(control) && valid;
	
	if(valid == false)
		return false;
		
	// validate a positive integer
	valid = check_positive_float(control) && valid;
	
	if(valid == false)
		return false;
		
	return valid;
}

// Check for an floating number

function check_positive_float(control)
{
	if(control.hasClass('validate_positive_numeric'))
	{
		var val = control.attr('value').replace(/^[\s]+|[\s]+$/ig, "");
		var title = control.attr('title');
		
		if(title == undefined || title.length == 0)
		{
			title = control.attr('id').replace("_"," ");
		}
		
		if (val.match(/^[+]?[0-9]*[.]?[0-9]+$/im)) 
		{
			control.removeClass('validation_error');
			return true;
		} 
		else
		{
			control.addClass('validation_error');
			alert(title + ' is not a valid numeric!');
			control.focus();
			
			return false;
		}
	}
	else
	{
		return true;
	} 
}

// Check for a positive integer

function check_positive_integer(control)
{
	if(control.hasClass('validate_positive_integer'))
	{
		var val = control.attr('value').replace(/^[\s]+|[\s]+$/ig, "");
		var title = control.attr('title');
		
		if(title == undefined || title.length == 0)
		{
			title = control.attr('id').replace("_"," ");
		}
		
		if (val.match(/^[+]?\d+$/)) 
		{
			control.removeClass('validation_error');
			return true;
		} 
		else
		{
			control.addClass('validation_error');
			alert(title + ' is not a valid positive number!');
			control.focus();
			
			return false;
		}
	}
	else
	{
		return true;
	}
}

// Check for an integer number

function check_integer(control)
{
	if(control.hasClass('validate_integer'))
	{
		var val = control.attr('value').replace(/^[\s]+|[\s]+$/ig, "");
		var title = control.attr('title');
		
		if(title == undefined || title.length == 0)
		{
			title = control.attr('id').replace("_"," ");
		}
		
		if (val.match(/^[\-+]?\d+$/)) 
		{
			control.removeClass('validation_error');
			return true;
		} 
		else
		{
			control.addClass('validation_error');
			alert(title + ' is not a valid integer!');
			control.focus();
			
			return false;
		}
	}
	else
	{
		return true;
	} 
}

// check for valid email
function check_email(control)
{
	if(control.hasClass('validate_email'))
	{
		var val = control.attr('value').replace(/^[\s]+|[\s]+$/ig, "");
		var title = control.attr('title');
		
		if(title == undefined || title.length == 0)
		{
			title = control.attr('id').replace("_"," ");
		}
		
		if (val.match(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b/i)) 
		{
			if(control.hasClass('validate_email_taken'))
			{
				control.addClass('validation_error');
				alert(title + ' is in use already!');
				
				control.focus();
				return false;
			}
			else
			{
				control.removeClass('validation_error');
				return true;  
			}
			
		} 
		else
		{
			control.addClass('validation_error');
			alert(title + ' is not a valid email address!');
			
			control.focus();
			return false;
		}
	}
	else
	{
		return true;
	}
}

// checks for a required field ...
function check_required(control)
{ 
	if(control.hasClass('validate_required'))
	{
		var val = control.attr('value').replace(/^[\s]+|[\s]+$/ig, "");
		var title = control.attr('title');
		
		if(title == undefined || title.length == 0)
		{
			title = control.attr('id').replace("_"," ");
		}
		
		if(val.length == 0)
		{
			control.addClass('validation_error');
			alert(title + ' is required!');
			control.focus();
			
			return false;
		}
		else
		{
			control.removeClass('validation_error');
			return true;
		}
	}
	else
	{
		return true;
	}
}