$(function() {



	//Validates form fields on submition of add_coupon form

	$('#add_coupon').submit(function(){									 

									 

		$('.validation_error').hide();//hide all the error messages.  This is already done in the stylesheet for initial load, but this will clear any erros we have previously thrown here.

		

		var pass = 'yes';

									 

		//Tests the Expiration Date field to make sure it was left blank, or matches the mm/dd/yyyy format

		var expirationValue = $("#expiration").val();

		if(expirationValue != '' && expirationValue != 'mm/dd/yyyy'){
			
			var response = /[0-9]?[0-9]\/[0-3]?[0-9]\/20[0-9][0-9]/.test(expirationValue);

			if(!response){

				$('#validate_expiration').show();

				pass = 'no';

			}		

		};

		

		

		//Tests the Link field to make sure it was left blank, or starts with http:// or https://

		/*var linkValue = $("#link").value;

		if(linkValue != '' && linkValue != 'http://'){

			var linkResponse = /https?:\/\//.test(linkValue);

			if(!linkResponse){

				$('#validate_link').show();

				pass = 'no';

			}		

		};*/

		

		//If the "brand" field is left blank, show the "validate_brand" error message	

		var brand = $('#brand').val();

		if (brand == "") {

			$("span#validate_brand").show();

			pass = 'no';

		}

		

		//If the "item" field is left blank, show the "validate_item" error message

		var item = $('#item').val();

		if (item == "") {

			$("span#validate_item").show();

			pass = 'no';

		}

		

		//If the "value" field is left blank, show the "validate_value" error message

		var value = $('#value').val();

		if (value == "") {

			$("span#validate_value").show();

			pass = 'no';

		}

		

		//If the "brand" field is greater than 300 characters, show the "validate_brand_length" error message

		var brandLength = $('#brand').val().length;

		if(brandLength > 300){

			$("span#validate_brand_length").show();

			pass='no';

		}

		

		//If the "item" field is greater than 300 characters, show the "validate_item_length" error message

		var itemLength = $('#item').val().length;

		if(itemLength > 300){

			$("span#validate_item_length").show();

			pass='no';

		}

		

		//If the "value" field is greater than 50 characters, show the "validate_value_length" error message

		var valueLength = $('#value').val().length;

		if(valueLength > 50){

			$("span#validate_value_length").show();

			pass='no';

		}

				

		//If the "expiration" field is greater than 10 characters, show the "validate_expiration_length" error message

		var expirationLength = $('#expiration').val().length;

		if(expirationLength > 10){

			$("span#validate_expiration_length").show();

			pass='no';

		}

		

		//If the "source" field is greater than 100 characters, show the "validate_source_length" error message

		var sourceLength = $('#source').val().length;

		if(sourceLength > 100){

			$("span#validate_source_length").show();

			pass='no';

		}

		

		//If the "restrictions" field is greater than 300 characters, show the "validate_restrictions_length" error message

		var restrictionsLength = $('#restrictions').val().length;

		if(restrictionsLength > 300){

			$("span#validate_restrictions_length").show();

			pass='no';

		}



		//If the "link" field is greater than 300 characters, show the "validate_link_length" error message

		var linkLength = $('#link').val().length;

		if(linkLength > 300){

			$("span#validate_link_length").show();

			pass='no';

		}



		//If there was an error, return "false", otherwise, return "true"

		if(pass == 'no'){

			return false;

		}else{

			return true;

		}						

		

	});

});