$(document).ready(function()
{
	//Validates a form if an element has a 'rel' attribute of 'ValidateSubmit'
	$('[rel=ValidateSubmit]').click(function()
	{
		if(Validation($(this).attr("param")) && validateNonRequiredFields())
		{
			Submit();
		}
	});//End 'click' Event

	/* Test functionality 

	var element = new Object;
	element.action = $(this).attr("data-action");
	element.panel = $(this).attr("data-panel");
	element.method = $(this).attr("data-method");
	element.url = $(this).attr("data-url");

	if(element.action == "ValidateSubmit"){
		if(Validation(element.panel){
			Submit(element.method);
		}
	}

	$('[data-action=element.action].click(function(){
		switch (element.action){
			case "ValidateSubmit" :
				if(Validation(element.panel){
					Submit(element.method);
				}
				break;
		}		
	}

	*/

	//Redirects the page based on an element's param attribute
	$('[rel=hyperlink]').live('click',function(){
		//http://www.keyora.com/WebfootDev/Portal.aspx?CN=D349A53B8ABA
		window.location=$(this).attr("param");
	});//End 'click' Event
});//END 'ready' Event

function validateNonRequiredFields ()
{
	var isValid = true;
	
	$('#myForm').find('input, select').each(function ()
	{
		if ($(this).attr('type') !== 'hidden' && !$(this).hasClass('required') && $(this).val() !== '' && $(this).attr('id') !== '')
		{
			if (parseInt($(this).valid(), 10) === 0)
			{
				isValid = false;
			}
		}
	});
	
	return isValid;
}

/*****************************************************
 *	Function: Validation(Panel)
 *****************************************************
 *
 *	Change Log
 *	----------
 *	None
 *
 *	Function Description
 *	--------------------
 *	This function returns a boolean value dependent
 *	on a forms validity
 *
 *	Function Paramaters
 *	-------------------
 *	Panel - any block level element containing
 *		form elements
 *
 *	Function Returns
 *      ----------------
 *	Flag 
 *	- Type: Boolean 
 *	- Values: True or False
 *
 *	Dependencies
 *	------------
 *	- JQuery
 *      - JQuery Validate
 *
 *****************************************************/

/*function Validation(Panel, Form)
{
	if(Form == undefined)
		Form = "#myForm";
	var Flag = true;

	if(Panel == undefined)
	{
		Flag = $(Form).valid();
	}//END if
	else
	{
		var obj = $(Panel +" input,select,textarea");

		//Validates each form element withinn 'Panel' that has an 'id'
		jQuery.each(obj, function()
		{
			if($(this).attr("id") != "")
			{
				var Temp = $(Form).validate().element("#"+ $(this).attr("id"));
				
				if(Flag == true && Temp == false)
				{
					Flag = false;
				}
			}
		});
	}//END else

	return Flag;
}//END 'Validation' function*/


//Created by Dawid Kasperowicz (
function Validation (panel, formID)
{
	$('#myForm').validate();

	var flag = true,
		obj,
		temp;

	if (formID === undefined || formID === '')
	{
		formID = '#myForm';
	}

	if (formID.substring(0, 1) !== '#')
	{
		formID = '#' + formID;
	}

	if (panel === undefined || panel === '')
	{
		flag = $(formID).valid();
	}
	else
	{
		if (panel.substring(0, 1) !== '#')
		{
			panel = '#' + panel;
		}
		
		/*$(panel).find('input.required, select.required, textarea.required').each(function ()
		{
			element = $(formID).find('#' + $(this).attr('id'));

			try
			{
				if (element.attr('id') != undefined)
					$(formID).validate().element('#' + element.attr('id'));
			}
			catch(err)
			{
				alert(err);
			}
		});*/

		obj = $(panel + ' input, select.required, textarea');
		//obj = $(panel).find('input.required, select.required, textarea.required');

		jQuery.each(obj, function ()
		{
			
			if ($(this).attr('id') !== '' && $(this).attr('id').substring(0, 2) === 'tb')
			{
				temp = $(formID).validate().element('#' + $(this).attr('id'));
				//temp2 = true;

				if (temp === false)
				{
					flag = false;
				}

				/*if ($(this).attr('id') === 'tbZIPCODE')
				{
					temp2 = validateZipCode($(panel));
				}

				if (temp2 === false)
				{
					flag = false;
				}*/
			}
		});
	}

	return flag;
}

//Created by Dawid Kasperowicz - Modified by Matthew Muchin (Added deferred object ) (
function ValidationRemote (panel, formID)
{

	var $dfd = $.Deferred();

	$('#myForm').validate();
	
	var flag = true,
		obj,
		temp;

	if (formID === undefined || formID === '')
	{
		formID = '#myForm';
	}

	if (formID.substring(0, 1) !== '#')
	{
		formID = '#' + formID;
	}

	if (panel === undefined || panel === '')
	{
		flag = $(formID).valid();
	}
	else
	{
		if (panel.substring(0, 1) !== '#')
		{
			panel = '#' + panel;
		}
		
		/*$(panel).find('input.required, select.required, textarea.required').each(function ()
		{
			element = $(formID).find('#' + $(this).attr('id'));

			try
			{
				if (element.attr('id') != undefined)
					$(formID).validate().element('#' + element.attr('id'));
			}
			catch(err)
			{
				alert(err);
			}
		});*/

		obj = $(panel + ' input, select.required, textarea');
		//obj = $(panel).find('input.required, select.required, textarea.required');



		jQuery.each(obj, function ()
		{
			
			if ($(this).attr('id') !== '' && $(this).attr('id').substring(0, 2) === 'tb')
			{
				temp = $(formID).validate().element('#' + $(this).attr('id'));
				//temp2 = true;

				
				if ($(this).attr('id') === 'tbZIPCODE')
				{
					//temp2 = validateZipCode($(panel));
					temp = $(formID).validate().element('#' + $(this).attr('id'));
				}

				if (temp == false)
				{
					flag = false;
				}


				/*
				if (temp2 === false)
				{
					flag = false;
				}*/
			}
		});
	}
	
	setTimeout(function() {
		$dfd.resolve(flag);
	}, 200 );

	return $dfd.promise();
	
}	
