
//wait for the DOM to be loaded
jQuery( document ).ready( function() {
	
	
	//Reset submit button
	$( "button#submitBtn" ).attr('disabled', '');
	
	//VALIDATOR
	
	jQuery.validator.addMethod( "notEqualTo", function( value, element, param ) {
		return this.optional( element ) || value != $( param ).val();
	}, "This has to be different..." );
	
	jQuery.validator.addMethod("notEqualToValue", function(value, element, param) {
		return $(param).val()?(value != $(param).val()):(value != param);
	}, "error");
	
	// validate the contact when it is submitted
	jQuery( "form#orderForm" ).validate( {
		rules: {
			//Total: { required: true, notEqualToValue: "0.00" },
			FirstName: "required",
			Surname: "required",
			Address: "required",
			Suburb: "required",
			Postcode: "required",
			Telephone: "required",
			Email: { required: true, email: true },
			EmailConfirm: {	required: true, email: true, equalTo: "#Email" }
			
		}
		,
		messages: {
			//Total: { required: "Purchase required", notEqualToValue: "Purchase required" },
			FirstName: "Please enter your first name.",
			Surname: "Please enter your last name.",
			Address: "Please enter your address.",
			Suburb: "Please enter your suburb.",
			Postcode: "Please enter your postcode.",
			Telephone: "Please enter your telephone number.",
			Email: { required: "Please enter a valid email address.", email: "Please insure the email is in a valid format." },
			EmailConfirm: { required: "Please confirm your email address.", email: "Please insure the email is in a valid format.", equalTo: "Please insure that your email is correct and matches in both fields." }
		}
		,
		submitHandler: function( pmForm ) {
			//alert("submitted!");
			//var poo = $( 'form#orderForm' ).validate().element( "table#pricingTable input#Total" );
			var myNonPurchase = $( 'table#pricingTable input#Total' ).val() == "0.00";
			if( myNonPurchase )
			{
				alert( "You appear not to have made a purchase." );
				$( 'table#pricingTable input#Total' ).focus();
				return;
			};//end if
			$( 'button#submitBtn' ).attr( 'disabled', 'disabled' );
			pmForm.submit();
		}
	} );
	
	OrderForm.init();
	
} );

//Check if a passed value is an integer.
function isInteger( pmNum )
{
	return /^\s*(\+|-)?\d+\s*$/.test(pmNum);
};//end function isInteger

//Handles order form functionality
OrderForm = 
{
	init : function()
	{
		//Check whether order form functionality is required.
		if( $( 'form#orderForm' ).size() == 0 ){ return; };
		
		//Set form to reset totals on price fields losing focus.
		$( 'table#pricingTable input' ).not( 'table#pricingTable input#Total' ).blur( function(){
			OrderForm.setTotal();
			//$( 'table#pricingTable input#Total' ).focus();
			//$( 'form#orderForm' ).validate().element( "table#pricingTable input#Total" );
		} );
		
		//Set total on startup.
		OrderForm.setTotal();
	}
	,
	getTotal : function()
	{
		var myTotal = 0;
		$( 'table#pricingTable tr' ).each( function( pmIndex ){
			//Get cost.										
			var myCost = $( this ).children( 'td' ).eq(2).text();
			myCost = myCost.replace( "$" , "" );
			myCost = parseFloat( myCost );
			
			//Get quantity.
			var myQty = $( this ).children( 'td' ).eq(3).children( 'input' ).val();
			//Check for non-integer values.
			if( !isInteger( myQty ) )
			{
				$( this ).children( 'td' ).eq(3).children( 'input' ).val( 0 );
				myQty = 0;
			};//end if
			myQty = parseFloat( myQty );
			
			var mySubTotal = myCost * myQty;
			
			if( !isNaN( mySubTotal ) ){ myTotal += mySubTotal; };
		} );
		return( myTotal );
	}
	,
	setTotal : function()
	{
		var myTotal = OrderForm.getTotal();
		myTotal = myTotal.toFixed(2);
		//alert( $( 'table#pricingTable input#Total' ).size() );
		$( 'table#pricingTable input#Total' ).val( myTotal );
		//$( 'table#pricingTable input#Total' ).trigger( 'focus' );
		//$( 'table#pricingTable input#Total' ).focus();
		//$( 'form#orderForm' ).validate().element( "table#pricingTable input#Total" );
	}
}


