$(document).ready(function() {

// Disable Purchase button
$("#ContinueID").attr("disabled",true);

// validate signup form on keyup and submit
	var validator = $("#order").validate({
		rules: {
			//TicketQty: "required digits",
			BillFirstName: "required",
			BillLastName: "required",
			BillEmail: {
				required: true,
				email: true
			},
			PaymentCardType: "required",
			CardNumber: {
				required: true,
				creditcard: true
			},
			ExpireMonth: "required",
			ExpireYear: "required",
			CSC: {
				required: true,
				digits: true,
				minlength: 3
			},
			BillAddress1: "required",
			BillCity: "required",
			BillState: "required",
			BillZipcode: "required"
		},
		groups: {
			Expiration: "ExpireMonth ExpireYear"
		},
		messages: {
			TicketQty: "Quantity must be a number.",
			BillFirstName: "First Name is required.",
			BillLastName: "Last Name is required.",
			BillEmail: "Valid email address is required.",
			PaymentCardType: "Payment Card Type is required.",
			CardNumber: "Valid Credit Card Number is required.",
			ExpireMonth: "Credit Card Expiration is required.",
			ExpireYear: "Credit Card Expiration is required.",
			CSC: "Card Security Code is required.",
			BillAddress1: "Street Address is required.",
			BillCity: "City is required.",
			BillState: "State is required.",
			BillZipcode: "Zip / Postal Code is required."
		},
		errorPlacement: function(error, element) {
			if ( element.is(":radio") ){
				error.appendTo( element.parent().next().next() );
			} else if ( element.is(":checkbox") ){
				error.appendTo ( element.next() );
			} else if (element.is("#ExpireMonthID") || element.is("#ExpireYearID")){
				error.insertAfter("#ExpireYearID");
			}/* else if(element.is("#TicketQtyID")){
				error.insertAfter(element);
			}*/
			else {
				error.insertAfter( element);
			}
		}/*,
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			alert("submitted!");
		}*//*,
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}*/
	});





// Prepopulate Name of Person for Pickup
	$("#BillFirstNameID").bind("blur keyup",function(){
		$("#WhoPickUpID").val($(this).val());
	});
	$("#BillLastNameID").bind("blur keyup",function(){
		$("#WhoPickUpID").val($("#BillFirstNameID").val()+" "+$(this).val());
	});
	// And handle on reload of page
	if($("#BillFirstNameID").length>0){
		if($("#BillFirstNameID").val().length>0){
			$("#WhoPickUpID").val($("#BillFirstNameID").val()+" "+$("#BillLastNameID").val());
		}
	}
	




// Handle toggling of mailing address
	if($("#HoldOrMailID2").length>0){
		$("#HoldOrMailID2, #HoldOrMailID1").change(function(){
			toggleMailingAddy();
		});	
	}
	// And handle it onload in case it's already clicked
	if($("#HoldOrMailID2").length>0){
		if($("#HoldOrMailID2").attr("checked")== true){
			toggleMailingAddy();
		}
	}
	function toggleMailingAddy(){
		if($("#HoldOrMailID2").attr("checked") == true) {
			// Do we need to disable these? - CL
			$("#MailingAddress1ID").attr("disabled",false);
			$("#MailingAddress2ID").attr("disabled",false);
			$("#MailingCityID").attr("disabled",false);
			$("#MailingStateID").attr("disabled",false);
			$("#MailingZipcodeID").attr("disabled",false);
			$("#PickUpID").hide();
			$(".mailingAddy").show();
			$("#MailingAddress1ID").rules("add", {required: true,messages: {required: "Street Address is required."}});
			$("#MailingCityID").rules("add", {required: true,messages: {required: "City is required."}});
			$("#MailingStateID").rules("add", {required: true,messages: {required: "State is required."}});
			$("#MailingZipcodeID").rules("add", {required: true,messages: {required: "Zipcode is required."}});
		} else {
			// Do we need to disable these? - CL
			$("#MailingAddress1ID").attr("disabled",true);
			$("#MailingAddress2ID").attr("disabled",true);
			$("#MailingCityID").attr("disabled",true);
			$("#MailingStateID").attr("disabled",true);
			$("#MailingZipcodeID").attr("disabled",true);
			$("#PickUpID").show();
			$(".mailingAddy").hide();
			$("#MailingAddress1ID").rules("remove");
			$("#MailingCityID").rules("remove");
			$("#MailingStateID").rules("remove");
			$("#MailingZipcodeID").rules("remove");
		}
	}
	
	


// Handle "Same as Billing"
	if($("#SameAsBillID").length>0){
		$("#SameAsBillID").change(function(){
			if ($("#SameAsBillID").attr("checked") == true) {
				$("#MailingAddress1ID").val($("#BillAddress1ID").val());
				$("#MailingAddress2ID").val($("#BillAddress2ID").val());
				$("#MailingCityID").val($("#BillCityID").val());
				$("#MailingStateID").val($("#BillStateID").val());
				$("#MailingZipcodeID").val($("#BillZipcodeID").val());
			} else {
				$("#MailingAddress1ID").val("");
				$("#MailingAddress2ID").val("");
				$("#MailingCityID").val("");
				$("#MailingStateID").val("");
				$("#MailingZipcodeID").val("");
			}
		});
	}
	


// Handle Section Dropdown
	if($("#ticketSection").length>0){
		$("#ticketSection").change(function(){
			updateNumOfTix(0);
		});
	}
//	Handle Quantity Field
	if($("#TicketQtyID").length>0){
		$("#TicketQtyID").bind("keyup",function(){
			updateNumOfTix(0);
		}).bind("blur",function(){
			updateNumOfTix(1);
		});
	}
// If the Back Button is used and the Qty field it populated
	if($("#TicketQtyID").length>0){
		if($("#TicketQtyID").val().length>0){
			updateNumOfTix(1);
		}
	}
// Update Fieldset.NumOfTix and Purchase button area
	function updateNumOfTix(pull){
		// FOR DEBUGGING
		//alert("ScheduleID="+$("#ScheduleID").val());
		//alert("TicketSectionID="+$("#ticketSection").val());
		//alert("TicketQty="+$("#TicketQtyID").val());
		//alert("pull="+pull);
		
		var validVal = true;
		// Validate Quantity is a positive, whole integer
		var qty = $("#TicketQtyID").val();
		if(qty=="" || isNaN(qty) == true || qty.indexOf("-")!=-1 || qty.indexOf(".")!=-1){validVal = false;}
		// Has the user selected a section?
		if($("#ticketSection").val()=="none"){validVal = false;}
		
		// If all of the above is true...
		if(validVal){
			// Check Qty
			
			$.ajax({
				   type: "POST",
				   url:	"_ajax_subtotal.cfm",
				   dataType: "text",
				   data: "TicketSectionID="+$("#ticketSection").val()+"&ScheduleID="+$("#ScheduleID").val()+"&TicketQty="+$("#TicketQtyID").val()+"&pull="+pull,
				   success: function(data, textStatus){
					   			if(data == "true"){
									$("#subtotal_error").text("");
									// Enable Purchase button
									$("#ContinueID").attr("disabled",false);
									// Update Price Total
									var qty = $("#TicketQtyID").val();
									if(qty!="" && $("#ticketSection").val()!="none"){
										// Get Section Price
										var sec_price = $("#ticketSection option:selected").attr("class").substring(6);
										// Get total price based on quantity
										var total = (sec_price * qty).toString();
										if(total.indexOf(".")!=-1){
											var dd = total.substring(0,total.indexOf("."));
											var cc = total.substring(total.indexOf(".")+1);
											if(cc.length==1) cc = cc+"0";
										} else {
											var dd = total;
											var cc = "00";
										}
										//if(total.length<=2){ total = total + ".00";}
										//if(total.length>5){ total = total.substring(0,5);}
										$("#subtotal").text("$"+dd+"."+cc);
									} else {
										$("#subtotal").text("$0.00");
									}
								} else {
									$("#ContinueID").attr("disabled",true);
									$("#subtotal, #subtotal_error").html("<span style='font-weight:normal; text-align:left; width:auto; color:#2A326F'>There are not enough tickets<br />available to fulfill your order.</span>");
									//debug code
									//$("#subtotal, #subtotal_error").html(data);
									
								}
				   },
				   error: function(XMLHttpRequest, textStatus, errorThrown){
					   //alert("error with AJAX request");
				   }
			});
		} else{
			$("#ContinueID").attr("disabled",true);
			$("#subtotal").html("$0.00");
			$("#subtotal_error").html("");
		}
	}
	

// Open CSC "What is this?" page in popup window
	$(".csc").click(function(e){
		e.preventDefault();
		window.open($(this).attr("href"),"csc_help","width=490,height=400,scrolling=auto,toolbars=0,location=0");
	});
	
	
});


function stateChanged2() {
		//only run this once the ajax call is complete and ready to display to the user
		if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") {
			htmloutput2 = xmlHttp2.responseText;
			//if there isn't enough ticket inventory, disable the submit button so the user can't order.
			if (htmloutput2 == 'ERROR') {
				var errorMsg = '<span style="color:#DB2F21; font-weight:normal;">There are not enough tickets<br />available to fulfill your order.</span>';
				document.getElementById("ContinueID").disabled = true;
				$("#subtotal").html("");
				$("#subtotal_error").html(errorMsg);
				$(".status").html(errorMsg);
			} else {
				$("#subtotal").html(htmloutput2);
				$(".status").html("");
				document.getElementById("ContinueID").disabled = false;
				document.getElementById("subtotal_error").innerHTML="";
			}
			
		}
	}
	
	
	
	
	
	
	









nav1=new Image(); nav1.src="images/nav/nav_gdyo.gif"
nav1ov=new Image(); nav1ov.src="images/nav/nav_gdyo_ov.gif"
nav2=new Image(); nav2.src="images/nav/nav_philharmonic.gif"
nav2ov=new Image(); nav2ov.src="images/nav/nav_philharmonic_ov.gif"
nav3=new Image(); nav3.src="images/nav/nav_wind.gif"
nav3ov=new Image(); nav3ov.src="images/nav/nav_wind_ov.gif"
nav4=new Image(); nav4.src="images/nav/nav_sinfonietta.gif"
nav4ov=new Image(); nav4ov.src="images/nav/nav_sinfonietta_ov.gif"
nav5=new Image(); nav5.src="images/nav/nav_dallasString.gif"
nav5ov=new Image(); nav5ov.src="images/nav/nav_dallasString_ov.gif"
nav6=new Image(); nav6.src="images/nav/nav_youngPerformers.gif"
nav6ov=new Image(); nav6ov.src="images/nav/nav_youngPerformers_ov.gif"
nav7=new Image(); nav7.src="images/nav/nav_fluteChoir.gif"
nav7ov=new Image(); nav7ov.src="images/nav/nav_fluteChoir_ov.gif"
nav8=new Image(); nav8.src="images/nav/nav_summer.gif"
nav8ov=new Image(); nav8ov.src="images/nav/nav_summer_ov.gif"



footer1=new Image(); footer1.src="images/footer_board.gif"
footer1ov=new Image(); footer1ov.src="images/footer_board_ov.gif"
footer2=new Image(); footer2.src="images/footer_staff.gif"
footer2ov=new Image(); footer2ov.src="images/footer_staff_ov.gif"
footer3=new Image(); footer3.src="images/footer_guild.gif"
footer3ov=new Image(); footer3ov.src="images/footer_guild_ov.gif"
footer4=new Image(); footer4.src="images/footer_jobs.gif"
footer4ov=new Image(); footer4ov.src="images/footer_jobs_ov.gif"
footer5=new Image(); footer5.src="images/footer_alumni.gif"
footer5ov=new Image(); footer5ov.src="images/footer_alumni_ov.gif"
footer6=new Image(); footer6.src="images/footer_press.gif"
footer6ov=new Image(); footer6ov.src="images/footer_press_ov.gif"
footer7=new Image(); footer7.src="images/footer_home.gif"
footer7ov=new Image(); footer7ov.src="images/footer_home_ov.gif"
footer8=new Image(); footer8.src="images/footer_contact.gif"
footer8ov=new Image(); footer8ov.src="images/footer_contact_ov.gif"


function iC(imageID, which){
	if(which==1){
		document.images[imageID+"img"].src = eval(imageID + "ov.src")
	} else {
		document.images[imageID+"img"].src = eval(imageID + ".src")
	}
}

function email(user, domain, text)
{
	var addr = user + '@' + domain;
	return '<a href="mailto:' + addr + '">' + (text ? text : addr) + '</a>';
}
