var donationAmount=0,depositNote="Total Donation:",q;


var validationRules = {
	rules: {
    	Name:{
    		required:true,
    		noInitText:true
    	},
    	Last_Name:{
    		required:true,
    		noInitText:true
    	},
    	quantity:{
    		required:true,
    		min:1
    	}
       
    
	},
	messages:{
		quantity:{
		required: "This field is required",
		min: jQuery.format("This tour requires at least {0} guests")
	},

	txtNoInitText: "* Required"
	},
	submitHandler: function(form) {
		//after successful validation
		$("#myDeposit table,#gcDeposit table", "#OForm").attr('style','');  	
		$("#myDepositTA").val($("#myDeposit").html() );
   		form.submit();
   }	
}

$(function() {
	document.OForm.isJavaScriptEnabled.value="yes";
 	parseURLParams();
 	$.validator.addMethod("noInitText", noInitTextValidation, "Please enter only alpha characters( a-z ).");
	$("#OForm").validate(validationRules);


	//MANDATORY DIAMONDS
	$('label.mandatory').append("<span></span>");
	//this one is for "divLabels"
	$('div.mandatory').append("<span></span>");
	
	//the following is preferable, but doesn't work. dunno why
	//$('.mandatory label, .mandatory>label').append("<span></span>");
	
	
	
	//INITIAL TEXT FOR CERTAIN FIELDS
    //$(".initText").addClass('screened');

    $(".initText").focus(function () {
  		if($(this).is('.initText') ){$(this).attr('value','');}  
		$(this).removeClass('initText');
    });
    
 
 	
 	
 	
    //ABOUT YOU 
    $("input#Home_Phone, input#Office_Phone, input#Home_Mobile, input#Office_Fax").mask("(999) 999-9999");
  	$("input#Zip").mask("99999");



	//GIFT AMOUNT
	
	$("input[type=radio]","#giftSection").click(function(){
		donationAmount=$(this).attr('value');
		if(donationAmount== "stuff: "){
				$('#donationItems').show('fast');//fadeIn is flunky here. dunno why
				
				//turn off mandatory payment fields
				$('div.paymentBox').hide('fast');
				$('#CardNumber, #NameOnCard, #ExpMonYear').removeClass('required');
				$('#CardNumber').removeClass('creditcard');
				//$('#ccBox .mandatory').removeClass('mandatory');

				
		}else{
				$('#donationItems').hide('fast');
				//turn on mandatory payment fields
				$('div.paymentBox').show('fast');
				$('#CardNumber, #NameOnCard, #ExpMonYear').addClass('required');
				$('#CardNumber').addClass('creditcard');
				//$('#ccBox .mandatory').addClass('mandatory');
		}


		if(donationAmount== "other amount"){
				$('#otherAmountDiv').show('fast');
		}else{
				$('#otherAmountDiv').hide('fast');
		
		}
		
		if (isNaN(donationAmount)) {
			donationAmount=0;
		}
		figureDeposit(donationAmount);

	});
	
	
	
	
	
	$('#otherAmount',"#otherAmountDiv").blur(function(){
		//alert('hi');
		donationAmount=$(this).attr('value');
		figureDeposit(donationAmount);
	});
	
	
	

	//PAYMENT
	$('#applyGiftCertificate_More, #makeDonation_More, #gcDiv').hide();
	
	$("#applyGiftCertificate, #makeDonation").click(function () {
		var thisID =   $(this).attr('id') ;
		var moreDiv = "#" + thisID + "_More";
		if($(this).is(':checked')){
			//alert('about to show ' + moreDiv);
			fancyShow(moreDiv);

		}else{
			//alert('about tohide ' + moreDiv);
			fancyHide(moreDiv);

		}
	});
	
	
	
	
	
		
	$("#CardType").change(function () {

		if(this.value=="Invoice"){
			$('#creditcard, #authorizeauthorize').removeClass('required');
			$('#ccBox .mandatory').removeClass('mandatory');
			$('#authorizeDiamond').hide();

		}else if(this.value=="Gift Certificate"){
			$('#creditcard, #authorizeauthorize').removeClass('required');
			$('#ccBox .mandatory').removeClass('mandatory');
			fancyShow('#applyGiftCertificate_More');
			$('#authorizeDiamond').hide();

		}else{
			$('#creditcard, #authorizeauthorize').addClass('required');
			$('#ccBox .mandatory').addClass('mandatory');
			$('#authorizeDiamond').show();
		}
	});
	
	
	
	
	$("select[name='ExpMon'], select[name='ExpYear']").change(function () {
	checkExpiry();
	});

	
	
	
	
	//CLICK SUBMIT 	(prior to validation)
    $('#OForm input[type="submit"]').click(function () { 
		$("textarea.initText", "#OForm").html();  	
		$(".initText", "#OForm").attr('value','').html(); 
    });

});






function noInitTextValidation(value, element){
	var isInitText = $(element).is(".initText");
	return !isInitText;
}


function checkExpiry(){
	var year = 20 + $('select[name="ExpYear"]').val();
	var month = $('select[name="ExpMon"]').val();
	today = new Date();
	expiry = new Date(year, month);
	if (today > expiry){
		$('input[name="ExpMonYear"]').val('');
	}else{
		$('input[name="ExpMonYear"]').val('valid');
	}	
	$("#OForm").validate().element("#ExpMonYear");
}





function figureDepositAmount(thisClass,thisAmount,notes){
	updateCalculator("#myDeposit",thisClass,thisAmount,notes);
	//retrieve gcTotal from calculator
	gcTotal = $("#gcPurchaseAmount").attr('value');
	depositAmount = Number(donationAmount);
	
	if(q!==null){depositNote ="Total " + q + ":"}
	updateCalculator("#myDeposit",'total',depositAmount,depositNote);
	var prettyDeposit =   Number(depositAmount).toFixed(2);
	document.OForm.Deposit_Amount.value=prettyDeposit;
	document.OForm.Deposit_Shown.value=  "$"+prettyDeposit;
}

function figureDeposit(donationAmount){
	figureDepositAmount('donation',donationAmount,"Subtotal:");
}

	
				
function prepTAs(){$("#depositTA").val($("#myDeposit").html() );}


function parseURLParams(){
	q =unescape($.getURLParam("q"))
	if(window.console){console.log(q)}

	if(q!=="null"){
		$('#content h2').html(q);
		$('input[name=q]').val(q);
		$('#giftSection h3').html('Gift Amount (per month)');
	}	
}
