// a bed and breakfast afloat
// jquery based form checking and google submit - rates.js
// emm 10.06.01
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) return false ;
		
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;

	return ( ((sign)?'':'-') + num + '.' + cents ); 
}
function init_selection() {
	if ($('#item_selection_1').val()=='5') {
		$('#custom').show('slow').fadeIn() ;
		//$('span#custom_ip').append('<input type="text" name="item_option_price_7" id="item_option_price_7" value="" />') ;
		// $('#hidden_custom_price').remove() ;
	}
	else {
		//$('#item_option_price_7').attr('value','');
		$('#custom').hide('slow').fadeOut();
		// $('#hidden_custom_price').append('<input type="hidden" name="item_option_price_7" value="0" />') ;
	}
}

$(document).ready(function() {

	// on doc init
	init_selection() ;
	
	// on select option changed
	$('#item_selection_1').change( init_selection ) ;

	// pre- submit processing & validation	 
	$('#google_btn').click( function() {

	  var select_option = $("#item_selection_1").val() ; 
	  var custom_price = $('[name=item_option_price_5]').val(); 
	  
	  // debug alert( select_option ) ;
	  if ( select_option == '0' ) {
		alert('Please select a Charter type from the dropdown list.') ;
		return false ;
	  }
	  if ( select_option == '5' ) {
		if ( (custom_price == null) || (custom_price == '' ) ) {
			alert('Please specify a custom price (dollar amount).') ;
			return false ;
		}
		
		custom_price = formatCurrency(custom_price) ;
		if ( custom_price == '0.00' ) {
			alert('Please specify a numeric value for your custom price.') ;
			$("#item_option_price_5").attr('value','0.00') ;
			return false ;
		}
		else {
			if ( document.getElementById ) {
				var priceID = document.getElementById("item_option_price_5") ;
				if ( priceID ) priceID.value = custom_price ;
			}
			return true ;
		}
	  }
	  return true ;
	  
	}) ;

}) ;
