jQuery.fn.exists = function(){return jQuery(this).length>0;}
//$(document).ready(function () {
// Use drupal.behaviours instead of $(document).ready with drupal
Drupal.behaviors.formval = function (context) {
	
	// validating Radio buttons
	// Vladimir 17 may 2011 
	function changeRadioErrorPlacement(error, element, formid)
	{
		if (element.attr("name") == "submitted[subscribe_to_newsletter]")
		{
			error.insertAfter(formid + " .form-radios");
		}
		else
		{
			error.insertAfter(element);
		}
	}
	
	if ($("#webform-client-form-2").exists())
	{
		$("#webform-client-form-2").validate({
			errorPlacement: function(error, element) {
					changeRadioErrorPlacement(error, element, "#webform-client-form-2");
				}
			}); 
	} //contact
	
	if ($("#webform-client-form-45").exists())
	{
		$("#webform-client-form-45").validate({
			errorPlacement: function(error, element) {
					changeRadioErrorPlacement(error, element, "#webform-client-form-45");
				}
			});
	} // interest
	
	if ($("#webform-client-form-47").exists())
	{
		$("#webform-client-form-47").validate({
												errorPlacement: function(error, element) {
													changeRadioErrorPlacement(error, element, "#webform-client-form-47");
												}, 
												submitHandler: function() {
													alert("If image code entered is correct, we will redirect you to secure gateway to proceed with payment.");
													var formElem = document.getElementById("webform-client-form-47");
													formElem.submit();
												}
											}); // booking



/*
		var formElem = document.getElementById("webform-client-form-47");
		formElem.submit(		function() {
										alert("We will redirect you to secure gateway to proceed with payment.");
										return true;
									}
								);
*/
	}
	
	// window.location.href
	
	if ($("#webform-client-form-46").exists())
		$("#webform-client-form-46").validate(); // feedback
	//$("#edit-submitted-subscribe-to-newsletter-0").rules("add", {submitted[subscribe_to_newsletter], "required"});

	if ($("#edit-submitted-amount").exists())
		$("#edit-submitted-amount").rules("add", {number: true});
	if ($("#edit-submitted-amount").exists())
		$("#edit-submitted-amount").rules("add", {range: [100, 10000]});
	if ($("#edit-submitted-email").exists())
		$("#edit-submitted-email").rules("add", {email: true});
	if ($("#edit-submitted-email-1").exists())
		$("#edit-submitted-email-1").rules("add", {email: true});
	if ($("#edit-submitted-email-2").exists())
		$("#edit-submitted-email-2").rules("add", {email: true});
	if ($("#webform-client-form-2 input:radio[name='submitted[subscribe_to_newsletter]']").exists())
		$("#webform-client-form-2 input:radio[name='submitted[subscribe_to_newsletter]']").rules("add", "required");
	if ($("#webform-client-form-45 input:radio[name='submitted[subscribe_to_newsletter]']").exists())
		$("#webform-client-form-45 input:radio[name='submitted[subscribe_to_newsletter]']").rules("add", "required");
	if ($("#webform-client-form-47 input:radio[name='submitted[subscribe_to_newsletter]']").exists())
		$("#webform-client-form-47 input:radio[name='submitted[subscribe_to_newsletter]']").rules("add", "required");
	//if ($("#edit-submitted-project").exists())
	//{
	
	// ADDED BY CHRIS 23/12/2010
	// The below 2 lines never worked because to change value with jquery you use the val() function, not value property
	// and it would only have ever worked on the first form because there are 3 inputs with the id 'edit-submitted-project'
	// but jquery will only return the first using $('#edit-submitted-project').
	// now been fixed
		//$("#edit-submitted-project").value = window.location.href;
		//alert(window.location.href);
		//var formProject0 = document.getElementById("edit-submitted-project");
		//formProject0.value = window.location.href;
	//}
	$("[id=edit-submitted-project]").val(window.location.href);
	
	
	
	if ($("#edit-submitted-project-1").exists())
	{
		var formProject1 = document.getElementById("edit-submitted-project-1");
		formProject1.value = window.location.href;
	}
	if ($("#edit-submitted-project-2").exists())
	{
		var formProject2 = document.getElementById("edit-submitted-project-2");
		formProject2.value = window.location.href;
	}
	
	if ($("#edit-submitted-reference").exists())
	{
		var formProject0 = document.getElementById("edit-submitted-reference");
		formProject0.value = receiptNumber;
	}

}
//});
