function setSelection(list, option)
{
	if (typeof(list) == "undefined")
		return;

	for (i = 0; i < list.length; i++)
	{
		if (list.options[i].text == option || list.options[i].value == option ||
			list.options[i].text.substring(0, option.length + 1) ==
				option + ";" ||
			list.options[i].value.substring(0, option.length + 1) ==
				option + ";")
		{
			list.options[i].selected = true;
			break;
		}
	}
}

var country = "USA";
var region = "";

function checkCountry(f)
{
//	var	f = document.forms['input'];
	var	list = f.countries;

	var code = list.options[list.selectedIndex].value;

	var parts = code.split(";");

	country = parts[0];
	region = parts[1];

	f.country.value = country;
/* */
	showElement("states", (country == "USA"));
	showElement("regions", (country != "USA" && parts[1] != ""));
	innerText("zipPostal", (country == "USA" ? "ZIP Code:" : "Postal Code:"));
	innerText("region", showRegion(region) + ":");
	showElement("region", (region != ""));
	showElement("phoneArea", (country == "USA"));
	showElement("phoneNum", (country != "USA"));
/* */
	return true;
}

function showRegion(region)
{
	switch (region)
	{
		case "AR":
			return "Arrondissement";
		break;

		case "AT":
			return "Atoll";
		break;

		case "CA":
			return "Canton";
		break;

		case "CL":
			return "Claim";
		break;

		case "CM":
			return "Commune";
		break;

		case "CO":
			return "County";
		break;

		case "DE":
			return "Department";
		break;

		case "DP":
			return "Dependency";
		break;

		case "DI":
			return "District";
		break;

		case "EM":
			return "Emirate";
		break;

		case "GO":
			return "Governate";
		break;

		case "IS":
			return "Region";
		break;

		case "KI":
			return "Kingdom";
		break;

		case "LO":
			return "Locality";
		break;

		case "MU":
			return "Municipality";
		break;

		case "PA":
			return "Parish";
		break;

		case "PF":
			return "Prefecture";
		break;

		case "PR":
			return "Province";
		break;

		case "QU":
			return "Quartier";
		break;

		case "RE":
			return "Region";
		break;

		case "ST":
			return "State";
		break;

		case "SU":
			return "Subdivision";
		break;

		case "TE":
			return "Territory";
		break;

		default:
			return "Region";
		break;
	}
}

function checkPhone(phone)
{
	if (country != "USA")
		return;

	if (typeof phone != "undefined" &&
		((v = trim(phone.value).replace(/[^0-9]/,"")).length != 7))
	{
		phone.value = v;
	}
/*
	for (i = 0; i < v.length; i++)
	{
		if (v.charAt(i) < '0' || v.charAt(i) > '9')
			f.value = v.substring(0, i) + v.substring(i + 1);
	}
*/
}

function checkFields(f)
{
	if (typeof f.firstName != "undefined" &&
		((v = trim(f.firstName.value)) == null || v.length == 0))
	{
		alert("Your first name is a required field.");
		f.firstName.focus();
		f.firstName.value = v;
		return false;
	}
/*
	if (typeof f.middleInitial != "undefined" &&
		((v = trim(f.middleInitial.value)) == null || v.length == 0))
	{
		alert("Your middle initial is a required field.");
		f.middleInitial.focus();
		f.middleInitial.value = v;
		return false;
	}
*/
	if (typeof f.lastName != "undefined" &&
		((v = trim(f.lastName.value)) == null || v.length == 0))
	{
		alert("Your last name is a required field.");
		f.lastName.focus();
		f.lastName.value = v;
		return false;
	}
/* */
	if (typeof f.addr != "undefined" &&
		((v = trim(f.addr.value)) == null || v.length == 0))
	{
		alert("Your address is a required field.");
		f.addr.focus();
		f.addr.value = v;
		return false;
	}

	if (typeof f.addr2 != "undefined" &&
		((v = trim(f.city.value)) == null || v.length == 0))
	{
		alert("Your city is a required field.");
		f.city.focus();
		f.city.value = v;
		return false;
	}

	if (country == "USA" && typeof f.state != "undefined" &&
		((v = trim(f.state.value)) == null || v.length == 0))
	{
		alert("Your State is a required field.");
		f.state.focus();
		f.state.value = v;
		return false;
	}

	if (country != "USA" && region != "" && typeof f.region != "undefined" &&
		((v = trim(f.region.value)) == null || v.length == 0))
	{
		alert("Your " + showRegion(region) + " is a required field.");
		f.region.focus();
		f.region.value = v;
		return false;
	}

	if (typeof f.zip != "undefined")
	{
		if ((v = trim(f.zip.value)) == null || v.length == 0)
		{
			alert("Your ZIP/Postal code is a required field.");
			f.zip.focus();
			f.zip.value = v;
			return false;
		}

		if (country == "USA")
		{
			if (v.length != 5)
			{
				alert("Your ZIP code must be 5 digits.");
				f.zip.focus();
				f.zip.value = v;
				return false;
			}

			if (isNaN(v))
			{
				alert("Your ZIP code may only contain numbers.");
				f.zip.focus();
				f.zip.value = v;
				return false;
			}
		}
	}

	if (country == "USA")
	{
		if (typeof f.acode != "undefined" &&
			(v = trim(f.acode.value).replace(/[^0-9]/,"")).length != 3)
		{
			alert("Your area code is a required fields.");
			f.acode.focus();
			f.acode.value = v;
			return false;
		}

		if (typeof f.phone != "undefined" &&
			(v = trim(f.phone.value).replace(/[^0-9]/,"")).length != 7)
		{
			alert("Your telephone number is a required fields.");
			f.phone.focus();
			f.phone.value = v;
			return false;
		}
	}
	else
	{
		if (typeof f.phonenum != "undefined" &&
			((v = trim(f.phonenum.value)) == null || v.length == 0))
		{
			alert("Your telephone number is a required fields.");
			f.phonenum.focus();
			f.phonenum.value = v;
			return false;
		}
	}
/* */
	if (typeof f.email != "undefined")
	{
		if ((v = trim(f.email.value)) == null || v.length == 0)
		{
			alert("Your email address is a required field.");
			f.email.focus();
			f.email.value = v;
			return false;
		}

		var i = v.indexOf('@'); 
		
		if (i == -1 || v.indexOf('.',i) == -1)
		{
			alert("Your email address does not appear to be formatted correctly.");
			f.email.focus();
			f.email.value = v;
			return false;
		}
	}

	if (typeof f.email != "undefined" && typeof f.confirmEmail != "undefined" &&
		((v = trim(f.confirmEmail.value)) == null || v.length == 0 ||
			v != f.email.value))
	{
		alert("Your email addresses do not match.");
		f.confirmEmail.focus();
		f.confirmEmail.value = v;
		return false;
	}
 
	if (typeof f.uname != "undefined" &&
		((v = trim(f.uname.value)) == null || v.length == 0))
	{
		alert("Your user name is a required field.");
		f.uname.focus();
		f.uname.value = v;
		return false;
	}

	if (typeof f.password != "undefined" &&
		((v = trim(f.password.value)) == null || v.length == 0))
	{
		alert("Your password is a required field.");
		f.password.focus();
		f.password.value = v;
		return false;
	}

	if (typeof f.password != "undefined" &&
			typeof f.confirmPassword != "undefined" &&
		((v = trim(f.confirmPassword.value)) == null || v.length == 0 ||
			v != f.password.value))
	{
		alert("Your passwords do not match.");
		f.confirmPassword.focus();
		f.confirmPassword.value = v;
		return false;
	}

	if (typeof f.pin != "undefined" &&
		((v = trim(f.pin.value)) == null || v.length != 4))
	{
		alert("Your Security ID is a required field and must be 4-digits long.");
		f.pin.focus();
		return false;
	}

	if (typeof f.accept != "undefined" && (! f.accept.checked))
	{
		alert("You must verify the information entered is correct by checking the appropriate checkbox.");
		f.accept.focus();
		return false;
	}

	if (typeof f.read != "undefined" && (! f.read.checked))
	{
		alert("You must acknowledge you ave read and accept the Terms & Conditions by checking the appropriate checkbox.");
		f.read.focus();
		return false;
	}

	return true;
}

function submitForm(f)
{
	if (checkFields(f))
		checkUserName(f);
}

function checkUserName(f)
{
	var a = new ajaxRequest();

	a.setCallbackParam(f);

	a.getData("/servlet/proxy?tgt=user&attr=lookup&data=all&uname=" +
		f.uname.value, checkUserNameCallback);

	return false;
}

function checkUserNameCallback(xml, params)
{
	var error = getValue(xml, "user.lookup.error");
//alert(xml);
	if (error != null)
	{
		switch (error)
		{
			case "ULR000":
				alert("This request is not allowed from your IP address. Please contact Customer Service");
			break;

			default:
				alert("An unknown error has occurred transferring your account.\n\nPlease contact Customer Service for assistance.");
			break;
		}
	}
	else if (getValue(xml, "user.lookup.exists.rd") == "true")
	{
		alert("The Username you have chosen already exists, please choose something different.");
		params[0].uname.focus();
	}
	else if (getValue(xml, "user.lookup.exists.datt") == "true")
	{
		alert("The Username you have chosen already exists.\n\nIf you have an account with this Username on Day At The Track, please transfer the account information using the link in the note at the top of the page.\n\nIf you do not have a Day At The Track account, please choose another Username.");
		params[0].uname.focus();
	}
	else
		params[0].submit();
}
