var btnCancel = false;

function removeAllItems(id)
{
	while (id.options.length){
		id.remove(0);
	}
}

function fillSelectBox(sel, from, to)
{
	if (to == 0 )
	{
		o = document.createElement("option");
		o.text = "-";
		o.value = -1 ;
		sel.options.add(o);
	}
	else
	{
		for(i = from;i<=to;i++)
		{
			o = document.createElement("option");
			o.text = i;
			o.value = i ;
			sel.options.add(o);
		}
	}
	sel.selectedIndex = 0;
}

function fnFanaticsTicketChange(id)
{
	var num = parseInt(id.options[id.selectedIndex].text);
	mincases = Math.floor(num/2);
	pinotageid = document.getElementById("fanatics_pinotage");
	removeAllItems(pinotageid);
	mixedid = document.getElementById("fanatics_mixed");
	removeAllItems(mixedid);
	if (id.selectedIndex == 0)
	{
		fillSelectBox(pinotageid, 0 , 0);
		fillSelectBox(mixedid, 0 , 0);
	}
	else
	{
		fillSelectBox(pinotageid, Math.max(mincases,2) , 20);
		fillSelectBox(mixedid, 1, 20);
	}
}

function fnFantasticTicketChange(id)
{
	var num = parseInt(id.options[id.selectedIndex].text);
	mincases = Math.floor(num/2);
	pinotageid = document.getElementById("fantastics_pinotage");
	removeAllItems(pinotageid);
	if (id.selectedIndex == 0)
	{
		fillSelectBox(pinotageid, 0, 0);
	}
	else
	{
		fillSelectBox(pinotageid, mincases, 20);
	}
}

function fnValidatePOTPPurchase()
{
	if (btnCancel == true)
		return true;
	
	return fnValidatePersonalDetails();
}

function fnValidatePurchase()
{
	if (btnCancel == true)
		return true;
	var fans = document.getElementById("fans");
	var fantastics = document.getElementById("fantastics");
	var fanatics = document.getElementById("fanatics");
	
	if (fans.selectedIndex == 0 && fantastics.selectedIndex == 0 && fanatics.selectedIndex == 0)
	{
		alert("You must select at least 1 ticket purchase");
		return false;
	}
	return true;
}


function fnValidateFanclub()
{
	
	if (!ValidateField("firstname", true, "*", "Please enter a Name"))
		return false;
	if (!ValidateField("surname", true, "*", "Please enter a Surname"))
		return false;
	if (!ValidateField("email", true, "*", "Please enter a valid Email address"))
		return false ;
	var obj = document.getElementById("region");
	if(obj.options[obj.selectedIndex].value == -1)
	{
		alert ("Please select a region");
		return false;
	}
	return true;
}

function fnValidatePersonalDetails()
{
	if (!ValidateField("firstname", true, "*", "Please enter a Name"))
		return false;
	if (!ValidateField("surname", true, "*", "Please enter a Surname"))
		return false;
	if (!ValidateField("email", true, "*", "Please enter a valid Email address or None if you do not have one"))
		return false ;
	if (!ValidateField("cell", false, "*", "Please enter a valid cell phone number"))
		return false ;
	if (!ValidateField("telephone", false, "*", ""))
		return false;
	if (!ValidateField("fax", false, "*", ""))
		return fals;
	if (IsBlank("cell") && IsBlank("telephone"))
	{
		alert("Please enter a valid contact telephone number either Home, Work or Cell");
		return false;
	}
	return true;
}

function IsBlank(id)
{
	if (document.getElementById(id).value == "")
		return true;
	return false;
}

function ValidateField(id, req, regex, msg)
{
	obj = document.getElementById(id) ;
	if (obj.value == "" && req == true)
	{
		if (msg != "")
			alert(msg) ;
		return false ;
	}
	else
	{
		// add regular expression check here
	}
	
	return true;
}	

function fnOnLoadPurchase()
{
	var $id = document.getElementById("fantastics");
	fnFantasticTicketChange($id);
	var $id = document.getElementById("fanatics");
	fnFanaticsTicketChange($id);
}