$(document).ready(function() {

	$("input[name='flagsameaddress']").click(function(){

		if ( $("input[name='flagsameaddress']").is(":checked") ) {

			toggleBillingAddress();

		} else {

			$("input[name='cardaddress']").val( "" );
			$("input[name='cardcity']").val( "" );
			$("input[name='cardstate']").val( "" );
			$("input[name='cardzipcode']").val( "" );
			$("select[name='cardcountry']").val( "" );

	//		$("input[name='cardaddress']").attr( "disabled", false ); 
	//		$("input[name='cardcity']").attr( "disabled", false ); 
	//		$("input[name='cardstate']").attr( "disabled", false ); 
	//		$("input[name='cardzipcode']").attr( "disabled", false ); 
	//		$("select[name='cardcountry']").attr( "disabled", false ); 

		}

	});

	$("input[name='paymentmethod']").click(function(){

		togglePaymentEntry();

	});

	$("input[type='checkbox']").click(function(){

		if ( this.name.substring( 0, 6 ) == "course" ) {

			updatePrice( this.name );

		} else if ( this.name.substring( 0, 7 ) == "partner" ) {

			updatePrice( "" );

		}

	});

      	$("input[name='promo']").keyup(function(){

		updatePrice( "" );

   	});

	updatePrice( "" );
	toggleBillingAddress();
	togglePaymentEntry();

	$("#btnupdateprice").hide();

});

function updatePrice( source )
{

	var query_string   = "";
	var query_discount = 0;
	var promo_code     = "";

	if ( source.length > 0 ) {

		//query_string = "(" + source + ")";

	}

	$("input[type='checkbox']").each(function()  {

		if ( ( this.name.substring( 0, 6 ) == "course" ) && ( this.checked ) ) {

			if ( query_string.length > 0 ) {

				query_string += ",";

			}

			if ( this.name == source ) {

				query_string += "(" + this.name + ")";

			} else {

				query_string += this.name;

			}

		} else if ( ( this.name.substring( 0, 7 ) == "partner" ) && ( this.checked ) ) {

			query_discount = 1;

                }

	});

	if ( $("input[name='promo']").val().length == 11 ) {

		promo_code = $("input[name='promo']").val();

	}

	if ( query_string.length > 0 ) {

		$.ajax( 
			{ 
			type: "GET",
			url: "/webservices.php",
			data: "data=" + query_string + "&discount=" + query_discount + "&promo=" + promo_code, 
			dataType: "json",
			success: function( result ) {

				$("#ordertotalcost").html( result.pricetotal );

				if ( result.pricediscount != "$0.00" ) {

					$("#orderdiscount").html( "<br /><b>Your Savings:</b> " + result.pricediscount  );

				} else {

					$("#orderdiscount").html( ""  );

				}

				for( var key in result.excluded )
				{

					$("input[name='course" + result.excluded[key] + "']").attr( "checked", "" );

				}


			}

		});

	} else {

		$("#orderdiscount").html( "" );
		$("#ordertotalcost").html( "$0.00" );

	}

}

function toggleBillingAddress()
{
	if ( $("input[name='flagsameaddress']").is(":checked") ) {

		$("input[name='cardaddress']").val( $("input[name='address1']").val() );
		$("input[name='cardcity']").val( $("input[name='city']").val() );
		$("input[name='cardstate']").val( $("input[name='state']").val() );
		$("input[name='cardzipcode']").val( $("input[name='zipcode']").val() );
		$("select[name='cardcountry']").val( $("select[name='country']").val() ); 

		//$("input[name='cardaddress']").attr( "disabled", true ); 
		//$("input[name='cardcity']").attr( "disabled", true ); 
		//$("input[name='cardstate']").attr( "disabled", true ); 
		//$("input[name='cardzipcode']").attr( "disabled", true ); 
		//$("select[name='cardcountry']").attr( "disabled", true ); 

	}

}

function togglePaymentEntry()
{

	if ( $("input[name='paymentmethod']:checked").val() == 1 ) {

		$("#payment-cc").show();

	} else {

		$("#payment-cc").hide();

	}


}
