
var ticket_prices = new Array();

var printed_name = false;

var get_total_requested = function() {
	var total = 0;
	for(i in ticket_prices) {
		if(isNaN(i)) continue;
		total += (document.frmRSVP['quantity['+i+']'].value*1);
	}
	return total;
}

var validate_rsvp_form = function(frm) {
	
	var frm_count = frm.elements.length;
	var elms = new Array();
	
	for(var i=0; i<frm_count; i++) {
		e = frm.elements[i];
		if(e.scrollWidth) {
			if(e.name.indexOf('name') != -1) {
				var tmp = e.value.split(" ");
				if(tmp.length < 2 || tmp[1] == '') {
				alert('You must enter a full name for all tickets.');
				e.focus();
				return false;	
				}
			}
			
			if(e.name.indexOf('email') != -1) {
				if(e.value.indexOf('@') == -1 || e.value.indexOf('.') == -1) {
					alert('You must enter a valid email address for all tickets.');
					e.focus();
					return false;	
				}
			}		
			
			if(e.name.indexOf('option') != -1 && e[e.selectedIndex].value == '') {
				alert('You must select a '+ticket_option+' option for all tickets.');
				e.focus();
				return false;	
			}	
		}
	}	
	
	return true;
}

var place_tickets = function(cnt, id, elm) {
	
	if(get_total_requested() > document.frmRSVP['seats_available'].value) {
		alert('You have requested more tickets than are available at this time.  Total tickets requested may not exceed '+document.frmRSVP['seats_available'].value);	
		elm.value = 0;
		cnt = 0;
	}
	
	if(cnt==0) {
		document.getElementById('buy_ticket_'+id).style.display = 'none';
		document.getElementById('buy_ticket_'+id).innerHTML = '<table cellspacing="1" cellpadding="1" style="font-weight: bold;"><tr><td width="121" style="padding-left: 33px;">&nbsp;Name</td><td width="139">Email</td><td>'+ticket_prices[id]['option']+'</td></tr></table>';
	}
	
	var current_count = document.getElementById('current_quantity_'+id).value;
	var difference = cnt - current_count;
	document.getElementById('current_quantity_'+id).value = cnt;
		
	var tkt_row = document.getElementById('buy_ticket_'+id);
	
	//alert(current_count+' : '+difference);
		
	if(isNaN(cnt)) {
		elm.value = 0;
		cnt = 0;
		document.getElementById('submit_button').innerHTML = '';
	}
	
	update_subtotal();
	
	if(cnt < 1) return false;
	
	tkt_row.style.display = '';
	
	if(difference > 0) {
		for(i = 0; i < difference; i++) {
			var tmp = document.createElement('div');
			tmp.innerHTML =  document.getElementById('ticket_row_'+id).innerHTML;
			tkt_row.appendChild(tmp);
		}
	} else {
		for(i = 0; i < difference*-1; i++) {
			tkt_row.removeChild(tkt_row.lastChild);
		}
	}
		
}

var update_subtotal = function() {
	
	cnt = document.frmRSVP.elements.length	
	
	total = new Array();
	total['online'] = 0;
	total['door'] = 0;
	total['total'] = 0;
	
	discount = new Array();
	discount['online'] = 0;
	discount['door'] = 0;
	discount['total'] = 0;
	
	var ticket_total = 0;
	
	tickets = 0;
	ids = '';
	var online = false;
	var door = false;
	
	for(i=0; i<cnt; i++) {
		elm = document.frmRSVP.elements[i];
		if(elm.type=='text') {
			if(elm.name.indexOf('quantity') != -1) {
				id = elm.name.substring(9, elm.name.length-1)*1;
				count = document.frmRSVP['quantity['+id+']'].value*1;
				if(count) {
					if(ticket_prices[id]['payment'] == 'online') online = true;
					if(ticket_prices[id]['payment'] == 'door') door = true;
				}
				price_per_ticket = ticket_prices[id]['price'];
				tickets+=count;	
				discount_ticket = 0;
				if(ticket_prices[id]['group_count'] && ticket_prices[id]['group_price'] && count >= ticket_prices[id]['group_count']) {
					d_count = Math.floor(count/ticket_prices[id]['group_count']);
					discount_ticket = d_count * ticket_prices[id]['group_count'] * (ticket_prices[id]['price'] - ticket_prices[id]['group_price']);
					discount['total'] += discount_ticket;
					discount[ticket_prices[id]['payment']] += d_count * ticket_prices[id]['group_count'] * (ticket_prices[id]['price'] - ticket_prices[id]['group_price']);
				}
				total_ticket = (count*price_per_ticket)-discount_ticket;
				//alert(total_ticket+' : '+discount_ticket);
				/*
				if(total_ticket > 0) {
					document.getElementById('total_ticket_'+id).style.display = '';
					document.getElementById('total_ticket_'+id).innerHTML = '$'+total_ticket.toFixed(2);	
				} else {
					document.getElementById('total_ticket_'+id).style.display = 'none;';
					document.getElementById('total_ticket_'+id).innerHTML = '';	
				}
				*/
				
				total[ticket_prices[id]['payment']]+=count*price_per_ticket;
				total['total']+=count*price_per_ticket;
			}	
		}
	}
	
	// print the ticket total:
	//  document.getElementById('online_price').innerHTML = '$'+total['online'].toFixed(2);
	// 	document.getElementById('online_discount').innerHTML = '$'+discount['online'].toFixed(2);
	// 	subtotal_online = total['online']-discount['online'];
	// 	document.getElementById('online_subtotal').innerHTML = '$'+subtotal_online.toFixed(2);
	// 
	// 	document.getElementById('door_price').innerHTML = '$'+total['door'].toFixed(2);
	// 	document.getElementById('door_discount').innerHTML = '$'+discount['door'].toFixed(2);
	// 	subtotal_door = total['door']-discount['door'];
	// 	document.getElementById('door_subtotal').innerHTML = '$'+subtotal_door.toFixed(2);

	ticket_total = Number(total['total']) - Number(discount['total']);
	//document.getElementById('ticket_total').innerHTML = '$'+ticket_total.toFixed(2);
	
	if(total['door']-discount['door'] > 0) {
		var total_door = total['door']-discount['door'];
		document.getElementById('total_door').style.display = '';
		document.getElementById('total_door').innerHTML = '<b>Due at the Door:</b> $'+total_door.toFixed(2);
	} else {
		document.getElementById('total_door').style.display = 'none';
	}
	
	if(total['online']-discount['online'] > 0) {
		var total_door = total['online']-discount['online'];
		document.getElementById('total_online').innerHTML = '<b>Due Now:</b> $'+total_door.toFixed(2);
		document.getElementById('total_online').style.display = '';
	} else {
		document.getElementById('total_online').innerHTML = '<b>Due Now:</b> $0.00';
		document.getElementById('total_online').style.display = '';
	}
	
	if(tickets && ticket_total == 0) {
		document.getElementById('total_online').innerHTML = '<b>Due Now:</b> $0.00';
		document.getElementById('total_online').style.display = '';	
	}
	
	if(!tickets) {
		document.getElementById('total_online').style.display = 'none';
	}
	
	if(online && ticket_total > 0) {
		document.getElementById('submit_button').innerHTML = '<br /><button type="submit" class="button">Continue &amp; Pay</button>';	
	} else if(door) {
		document.getElementById('submit_button').innerHTML = '<br /><button type="submit" class="button">RSVP Now</button>';
	} else {
		document.getElementById('submit_button').innerHTML = '';
	}
	
}