$( document ).ready( function()
{
	/* Makes the tabs clickable */
	$( "#containerTabs div" ).click(
		function()
		{
			/* Removes all classes corelating to hover */
			$( "#containerTabs div" ).removeClass( 'hover' );
			/* Adds the hover class to the selected div */
			$( this ).addClass( 'hover' );
			/* Hides the container divs */
			$( ".containerForm" ).hide();
			/* Shows the container div for the selected tab */
			$( "#" + $(this).attr('id') + "Form" ).show();
			
			$( "#selectedTab" ).val( this.id );
		}
	);
	
	/* Makes the checkboxs clickable */
	$( "div[id*=checkbox_]").click(
		function()
		{
			/* Grabs just the part of the id needed.[ After the _ ] */
			var divId	= this.id.substring( this.id.lastIndexOf( "_" ) + 1 );

			/* If the checkbox is already checked, then uncheck it. Otherwise, have it checked.*/
			if( $(this).hasClass( 'formChecked' ) )
			{
				$(this).removeClass('formChecked');
				$( ":input[name=select_" + divId + "]" ).val( "false" );
			}
			else
			{
				$(this).addClass('formChecked');
				$( ":input[name=select_" + divId + "]" ).val( "true" );
			}
			
			checkMakeModel( savedJsonData );
			checkYears();
		}
	);

	/* Makes sure the years make sense */
	$( ":input[name=yearFrom], :input[name=yearTo]" ).change( 
		function()
		{
			if( $(":input[name=yearFrom] option:selected").val() > $(":input[name=yearTo] option:selected").val() )
			{
				$(":input[name=yearFrom]").val( $(":input[name=yearTo] option:selected").val() );
			}

			$( ":input[name=selectedToYear]" ).val( $(":input[name=yearTo] option:selected").val() );
			$( ":input[name=selectedFromYear]" ).val( $(":input[name=yearFrom] option:selected").val() );
		}
	).trigger('change');
	
	/* Ensures that the zip code and price fields are numbers only */
	$(":input[name=zipcode]").keypress(
		function( e )
		{
			if( e.which != 8 && e.which != 0 && ( e.which < 48 || e.which > 57 ) )
			{
				return false;
			}
		
			$( this ).val( $( this ).val().replace( /[A-Za-z\s]/g, "" ) );
		}
	).click(
		function()
		{
			$( this ).val( $( this ).val().replace( /[A-Za-z\s]/g, "" ) );
		}
	);

	
	/* Checks that the checkboxs are checked properly */
	$( ".selectBoxInput" ).each(
		function()
		{
			if( $(this).val() == "true" )
			{
				/* Grabs just the part of the id needed.[ After the _ ] */
				var divId	= this.name.substring( this.name.lastIndexOf( "_" ) + 1 );
				
				$( "#checkbox_" + divId ).addClass( 'formChecked' );
			}
		}
	);

	$( ":input[name=make][type!=hidden]").change(
		function()
		{
			$( ":input[id=selectedMake]" ).val( $( this ).val() );
			checkMakeModel( savedJsonData );
		}
	);

	$( ":input[name=model][type!=hidden]").change(
		function()
		{
			$( ":input[id=selectedModel]" ).val( $( this ).val() );
		}
	);
	
	$( ".byPriceTab" ).click(
		function()
		{
			var price = $(this).attr("value").split( "-" );
			$( ":input[name=priceFrom]" ).val( price[0] );
			$( ":input[name=priceTo]" ).val( price[1] );

			if( precioValid == "false" )
			{
				$.getJSON("/zipCheck", {zipCode: $("#priceForm :input[name=zipcode]").val()},
					function(jsonData)
					{
						if( jsonData.message == "Valid Zip" )
						{ 
							$("#zipPrecioConfirmMsg").html("");
							precioValid	= "true";
							$("#priceForm").submit();
						}
						else
						{
							$("#zipPrecioConfirmMsg").html( jsonData.message );
						}
					}
				);
				return false;
			}
			else
			{
				return true;
			}
		}
	);
	
	
	/* Populates the MakeModel dropdowns */
	$.getJSON( "/makemodel.txt",
		function( jsonData )
		{
			savedJsonData	= jsonData;
			checkMakeModel( savedJsonData );
		}
	);

	checkYears();
	var autosValid	= "false";
	var precioValid	= "false";
	
	/* Validate zip code */
	$("#autosForm").submit(
		function()
		{
			if( autosValid == "false" )
			{
				$.getJSON("/zipCheck", {zipCode: $("#autosForm :input[name=zipcode]").val()},
					function(jsonData)
					{
						if( jsonData.message == "Valid Zip" )
						{ 
							$("#zipAutosConfirmMsg").html("");
							autosValid	= "true";
							$("#autosForm").submit();
						}
						else
						{
							$("#zipAutosConfirmMsg").html( jsonData.message );
						}
					}
				);

				return false;
			}
			else
			{
				return true;
			}
		}
	);
});