/*
	To Use:
		
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="thisFile.js"></script>
	
	Then add js_required to the class of all items you want to have checked on  the page ,
	and add js_email_required to the class of the ones that need an email address
	then change the "submit" button to have 
	   onclick="apply_submit(this)"
	in its attribute. 
	
	Also, no form should have an input element with 
		name="submit"
	Doing so will break the 
	   form.submit();
	function
	
	The rest should simply magically work ( assuming you have the appropriate classes  'incomplete' and 'incomplete_rad'
	available somewhere.
	
######################
# Copyright (C) Artworks TravelIndex Group
#		www.artworks.net.nz
#
# Use by prior approval only
#########################
 
 */
 
var passes=true;


function validate_all()
{
	var a = validate(".js_required", 'test_empty', 'tweak_input' ) ;
	var b = validate(".js_required[@type=radio]", 'test_radio_set', 'tweak_radio' );
	var c = validate(".js_email_required", 'test_email', 'tweak_input' );
	var d = validate("textarea.js_required", 'test_children','tweak_input');
	if ( a && b && c && d)
	{

		return true;
	}
		else
	{
		return false;
	}
	
}


/* Email Validation Function */
function email_validate( email )
{
	var pattern = /^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return email.match(pattern);
}
	
/* Checks to see if one of a group of radio buttons are checked */
function checkRadio( radioName )
{
	var passes = false;
	rads = $("input[@name="+radioName+"]");
	for( var i =0; i<rads.length; i++ )
	{
		if ( $(rads[i]).attr("checked")==true )
		{
			passes=true;
		}
	}
	return passes;
}
											
/* oLD WAY: rEDUNDANT */
function checkValid( ipbox )
{
	if( $( ipbox ).attr('type') =="radio" )
	{
		var rname = $(ipbox).attr('name');
		if ( checkRadio( rname ) )
		{
			$(ipbox.parentNode).removeClass("incomplete_rad");
			return true;
	}
	else
	{
	$(ipbox.parentNode).addClass("incomplete_rad");
	return false;
	}
	}
	else
	{
	if( $( ipbox ).val()=="" || $( ipbox ).val()==0 )
	{
	$(ipbox).addClass("incomplete");
	return false;
	}
	else
	{
	$(ipbox).removeClass("incomplete");
	return true;
	}
	}
	}
											
	/* testers */
	/* return true to tweak 
	* return false to untweak */
	
	
	function test_radio_set( obj )
	{
		return !checkRadio( $(obj).attr('name') );
	}
	
	function test_empty( obj )
	{
		return  ( $( obj ).val()=="" || $( obj ).val()==0  );
	}
	
	function test_children( obj )
	{
		$('input').keydown(function(e)
		{
			if (e.keyCode == 13) 
			{
				apply_submit($(this).parents('form'));
				return false;
			}
		});
		return ( $(obj)[0].textLength==0 );
	}
	
	function test_email( obj )
	{
		return !email_validate( $(obj).val() );
	}
	
	/* Tweakers */
	/* Tweaker: object-to-tweak , tweak-if-true/un-tweak-if-false */
	
	function tweak_input( obj, cond )
	{
		if ( cond )
		{
			$(obj).addClass("incomplete");
			return true;
		}
		else
		{
			$(obj).removeClass("incomplete");
			return false;
		}
	}
	
	function tweak_radio( obj, cond )
	{
		if ( cond )
		{
			$(obj.parentNode).addClass("incomplete_rad");
			return true;
		}
		else
		{
			$(obj.parentNode).removeClass("incomplete_rad");
			return false;
		}
	}


function validate( matcher, tester, tweaker )
{
	var passes = true;
	var items = $(matcher);
	for ( var i = 0; i < items.length ; i++ )
	{
		if( eval( tester+"( items[i] )" ) )
		{
			eval( tweaker +"( items[i], true )" );
			passes = false;
		}
		else
		{
			eval( tweaker + "( items[i], false )" );
		}
	}
	return passes;
}
							

							
function apply_submit( f )
{
	if ( validate_all() )
	{
		f.form.submit();
	}
	else
	{
	return false;
	}
}
							
	/* Magic Validation while working */
	$(function()
	{
		validate_all();
		$('input').keydown(function(e)
		{
			if (e.keyCode == 13) 
			{
				apply_submit($(this).parents('form'));
				return false;
			}
		});
		$('textarea').keydown(function(e)
		{
			if (e.keyCode == 13) 
			{
				apply_submit($(this).parents('form'));
				return false;
			}
		});
		
		$('input').keyup(function(e)
		{
			validate_all();
		});
		$('textarea').keyup(function(e)
		{
			validate_all();
		});
		
		$('select').select( function()
		{
			validate_all();
		});	
	
		$('select').change( function()
		{
			validate_all();
		});
		
		$('input').select( function()
		{
			validate_all();
		});	
		$('textarea').select( function()
		{
			validate_all();
		});	

	
		$('input').change( function()
		{
			validate_all();
		});
		$('textarea').change( function()
		{
			validate_all();
		});

		
	});