// www.jamesburgess.co.uk :)

function disableOnLoad() {
	fields = [document.form1.select14,document.form1.select15,document.form1.select16,document.form1.select17,document.form1.select18,document.form1.select28,document.form1.select29,document.form1.select30,document.form1.select31,document.form1.select32,document.form1.select42,document.form1.select43,document.form1.select44,document.form1.select45,document.form1.select4,document.form1.textfield1211,document.form1.textfield1222,document.form1.textfield1232,document.form1.textfield1242,document.form1.textfield1252,document.form1.textfield22,document.form1.textfield225,document.form1.textfield226,document.form1.textfield227,document.form1.textfield228];
	for(i=0; i<(fields.length); i++)
	{
		fields[i].disabled = true;
	}
}

function checkNumber(input){
  var num = input.value.replace(/\,/g,'');
  if(!isNaN(num)){
    if(num.indexOf('.') > -1) {
      alert("You may not enter any decimals.");
      input.value = input.value.substring(0,input.value.length-1);
    }
  } else {
    alert('You may enter only numbers in this field!');
    input.value = input.value.substring(0,input.value.length-1);
  }
}

function updatePrice(ignore)
{
	//orderForm = document.forms[0];
	//orderForm = document.forms.form1;
	// Base price is £27.50, standard
	// If sponsor decals are added, add £5, plus 50p per 'extra' player
	// If number decals are added, add 50p for each player for which a number is specified
	// If name decals are added, add 50p for each player for which a name is specified
	// If extra players are added, add £1 each
	sponsorDecalsBox = document.form1.checkbox1;
	numberDecalsBox = document.form1.checkbox2;
	nameDecalsBox = document.form1.checkbox3;
	price = 27.5;
	postage = 3.5;

	// Basic fields
	basicNumberFields = [document.form1.textfield12, document.form1.textfield122, document.form1.textfield123, document.form1.textfield124, document.form1.textfield125, document.form1.textfield126, document.form1.textfield127, document.form1.textfield128, document.form1.textfield129, document.form1.textfield1210 ];
	basicNameFields = [document.form1.textfield2, document.form1.textfield3, document.form1.textfield4, document.form1.textfield5, document.form1.textfield6, document.form1.textfield7, document.form1.textfield8, document.form1.textfield9, document.form1.textfield10, document.form1.textfield11];

	// Extra fields
	extraCheckBoxes = [document.form1.checkbox222222, document.form1.checkbox2222222, document.form1.checkbox2222223, document.form1.checkbox2222224, document.form1.checkbox2222225];
	extraNumberFields = [document.form1.textfield1211, document.form1.textfield1222, document.form1.textfield1232, document.form1.textfield1242, document.form1.textfield1252];
	extraNameFields = [document.form1.textfield22, document.form1.textfield225, document.form1.textfield226, document.form1.textfield227, document.form1.textfield228];

	// Add extra £1 for each extra player
	for(i=0; i<(extraCheckBoxes.length); i++)
	{
		if(extraCheckBoxes[i].checked)
		{
			price += 1;
		}
	}

	// Sponsor decals & extra players price
	// Add base price for first 10
	if(sponsorDecalsBox.checked)
	{
		price += 5;
	
		// Add subsequent for each one ticked
		for(i=0; i<(extraCheckBoxes.length); i++)
		{
			if(extraCheckBoxes[i].checked)
			{
				price += 0.5;
			}
		}
	}

	// Number decals
	// Add for each one with a number entered
	if(numberDecalsBox.checked)
	{
		for(i=0; i<(basicNumberFields.length); i++)
		{
			if(basicNumberFields[i].value.length>0)
			{
				price += 0.5;
			}
		}

		// Add subsequent for each one ticked and with a number entered
		for(i=0; i<(extraCheckBoxes.length); i++)
		{
			if((extraCheckBoxes[i].checked) && (extraNumberFields[i].value.length>0))
			{
				price += 0.5;
			}
		}
	}

	// Name decals
	// Add for each one with a name entered
	if(nameDecalsBox.checked)
	{
		for(i=0; i<(basicNameFields.length); i++)
		{
			if(basicNameFields[i].value.length>0)
			{
				price += 0.5;
			}
		}
	}

	// Add subsequent for each one ticked and with a name entered
	for(i=0; i<(extraCheckBoxes.length); i++)
	{
		if((extraCheckBoxes[i].checked) && (extraNameFields[i].value.length>0))
		{
			price += 0.5;
		}
	}

	// Update price
	totalString = (price+postage).toString();
	teamString = price.toString();
	if (totalString.indexOf('.') == -1)
	{
		totalString += '.00';
	}
	else if (totalString.indexOf('.') == totalString.length -2)
	{
		totalString += '0';
	}
	if (teamString.indexOf('.') == -1)
	{
		teamString += '.00';
	}
	else if (teamString.indexOf('.') == teamString.length -2)
	{
		teamString += '0';
	}
	document.getElementById('totalPrice').innerHTML = totalString;
	document.getElementById('teamPrice').innerHTML = teamString;

	setTimeout("updatePrice(null)",200);
}

function disableFieldsIfChecked(checkbox,fields)
{
	if(checkbox.checked)
	{
		for(i=0; i<(fields.length); i++)
		{
			fields[i].disabled = false;
		}
	} else {
		for(i=0; i<(fields.length); i++)
		{
			fields[i].disabled = true;
		}
	}
}

function cancelSubmitIfNotEqual(email,confirm)
{
	if((email.value.length>0) || (confirm.value.length>0))
	{
		if(email.value!=confirm.value)
		{
			alert("Email addresses do not match.");
			return false;
		}
	} else {
		alert("Please fill out both email fields.");
		return false;
	}

	return true;
}

function cancelSubmitIfNotPresent(field,name)
{
	if(field.value.length==0)
	{
		alert("The '"+name+"' field was not completed.");
		return false;
	}

	return true;
}
