//USER SETS
var details_cutoff = 9; //the number of people before we start cutting off details

//Variables
var tickets = new Array(); //will store ticket level info
var ticket_counter = 1; //will ticket_counter the number of tickets
var details_cutoff_on = false; //Details cutoff switch
var kosher_offset = 0; //how much extra for kosher meals
var info = new Array(); //will cache entered info 

function refreshTickets()
{
	
	ticket_counter = 1; //will restart the ticket counter
	
	var total = totalPrice() + kosher_offset;
	details_cutoff_on = false; //reset the value of the cutoff
	document.getElementById('tix_total').innerHTML = '$' + total;
	document.getElementById('tix_total_field').value = total;
	document.getElementById('tix_total_num').value = totalTickets();

	var table="<div style='clear:both;'></div><b style='font-size:20px'> NOTE:Please check ME next to the record that is yours</B> <table><tr>"+
		"<td>ME</td>"+	
		"<td>Ticket Type</td>"+						
		"<td>First Name</td>"+
		"<td>Last Name</td>" +
		"<td>Email</td>" + 
		"<td>Phone (Day)</td>" +
		"<td>Phone (Mobile)</td>" +
		"<td>Company/Organization</td>" +
		"<td>Organization Website:</td>" +
		"<td>Job Title</td>" + 
		"<td>Kosher Lunch? ($+20)</td>" +
		"</tr>"; //clear the old table
	
	cacheInfo(); //cache the old form info
	table += compileDetailsTable();
	
	document.getElementById('buytix_details').innerHTML = table  + '</table>';
	retrieveInfo(); //and put it back
}

function cacheInfo()
{
	info = new Array();

	for(i=0;i<document.tixForm.elements.length;i++) 
	{
		if(document.tixForm[i].name.indexOf("tickets")!=-1)
		{
			info[document.tixForm[i].name] = document.tixForm[i].value;
		}
	}
}

function retrieveInfo()
{
	for(i=0;i<document.tixForm.elements.length;i++)
	{
		if(document.tixForm.elements[i].name.indexOf("tickets") != -1 && info[document.tixForm.elements[i]['name']]!=undefined)
		{
			document.tixForm.elements[i].value = info[document.tixForm.elements[i]['name']];
		}
	}
}

//Finds the total price of the tickets
function totalPrice()
{
	var total = 0; //will store our total
	
	//Go through every ticket level
	for (var x=0; x < tickets.length; x++)
	{
		//get the selected quantity and multiply it by the price
		total += document.getElementById('tixlevel_qty_' + tickets[x]['id']).selectedIndex * tickets[x]['price'];
	}
	return total;
}

function Kosher(value)
{
	if(value.checked)
	{
		kosher_offset+= 20;
	}
	else
	{
		kosher_offset = kosher_offset - 20;
	}
	var total = totalPrice() + kosher_offset;
	document.getElementById('tix_total').innerHTML = '$' + total;
	document.getElementById('tix_total_field').value = total;
}

function totalTickets()
{
	var total = 0; //will store our total
	
	//Go through every ticket level
	for (var x=0; x < tickets.length; x++)
	{
		//get the selected quantity
		total += document.getElementById('tixlevel_qty_' + tickets[x]['id']).selectedIndex * tickets[x]['num_seats'];
	}
	return total;
}

//Compiles the details table
function compileDetailsTable()
{
	var table = ""; //will store our table
	
	//Go through every ticket level
	for (var x=0; x < tickets.length; x++)
	{
		table += addToTable(tickets[x]['title'], document.getElementById('tixlevel_qty_' + tickets[x]['id']).selectedIndex);
	}
	return table;
}

//Creates a details row (need the title of ticket and it's quantity)
function addToTable(title, quantity)
{
	var out = "";
	var first = false; //tracks first record
	
	for(var x=1; x <= quantity; x++)
	{
		if(details_cutoff_on)
		{
			break;
		}
		
		var checked = "";
		if(!first)
		{
			checked = "CHECKED";
			first = true;
		}
		
		out += "<tr>" + 
			"<input type='hidden' name='tickets["+ticket_counter+"][level_id]' value='"+title+"' />" + 
			"<td><input type='checkbox' name='tickets["+ticket_counter+"][me]' value='1' " + checked + "/> </td>" + 
			"<td>" + title + "</td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][first_name]' value='' id='tickets["+ticket_counter+"][first_name]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][last_name]' value='' id='tickets["+ticket_counter+"}][last_name]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][email]' value='' id='tickets["+ticket_counter+"][email]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][phone_day]' value='' id='tickets["+ticket_counter+"][phone_day]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][phone_cell]' value='' id='tickets["+ticket_counter+"][phone_cell]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][organization]' value='' id='tickets["+ticket_counter+"][organization]' /></td>" + 
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][org_website]' value='http://' id='tickets["+ticket_counter+"][org_website]' /></td>" +
			"<td><input type='text' size=7 name='tickets["+ticket_counter+"][job_title]' value='' id='tickets["+ticket_counter+"][job_title]' /></td>" +
			"<td><input type='checkbox' onclick='Kosher(this)' name='tickets["+ticket_counter+"][kosher_meal]' value='1' />Kosher</td>" + 
		"</tr>";
	
		//check whether we need to cut off (we need to leave 1 details row at least, so this part is after)
		if(totalTickets() > details_cutoff)
		{
			details_cutoff_on = true;
			alert('Thank you for inviting such a large number of guests. We will contact  you later about the details. Just put your contact details. ');
			break;
		}
		
		ticket_counter++; //increase our ticket index ticket_counterer
	}
	return out;
}

function validate()
{
	var me_selected = false; //tracks whether a me is selected
	var valid = true; //true until it turns false 
	var check_next_phone = 0 //when a me record is selected it must have a phone. 0 if no check required yet, 1 if checking is on, 2 if failed so far, 3 if failed completely
	

	for(i=0;i<document.tixForm.elements.length;i++) 
	{
		if(document.tixForm[i].name.indexOf("tickets")!=-1)
		{
			var name = document.tixForm[i].name;
			var value = document.tixForm[i].value;
			var checked = document.tixForm[i].checked;
			
			if((name.indexOf('first_name')!=-1 || name.indexOf('last_name')!=-1 || name.indexOf('email')!=-1) && (value==''))
			{
				valid = false;
				error_msg = "You must enter the first name and last name of each person. You must also enter the email of each person";
			}
			
			//check whether it's a me field (must contain me and be checked)
			if(name.indexOf('[me]')!=-1 && checked==true)
			{
				if(me_selected == true)
				{
					valid = false;
					error_msg = "Only one person may be checked as yourself";
				}
				me_selected = true; //then we have our me
				check_next_phone = 1; //we need to check the next phone record that appears to be not empty
			}
			
			//check phone record only if it is needed
			if(check_next_phone >= 1 && name.indexOf('phone')!=-1 && value=='')
			{
				check_next_phone++;
			}
			
			if(check_next_phone >= 1 && name.indexOf('phone')!=-1 && value!='') //we found a phone
			{
				check_next_phone = 0;
			}
			
			if(check_next_phone == 3)
			{
				valid = false;
				error_msg = "You must enter a phone number at least for your record";
			}
		}
	}
	
	if(!me_selected)
	{
		valid = false;
		error_msg = 'You MUST check which record is yours';
	}
	
	if(!valid)
	{
		alert(error_msg);
		return false;
	}
	else
	{
		return true;
	}
}

