// ---------------------  Script file used by UserInfo.aspx and its derivates (CreateAccount.aspx)--------------------------------
function iTalkiesEmailIDOnKeyUpValidate(e)
{
	var srcElement;
	var normalizedEvent;
	
	if (ie)
	{
		srcElement = event.srcElement;
		normalizedEvent = event;
	}
	if (moz) 
	{
		srcElement = e.target;
		normalizedEvent = e;
	}

	// let all keys other than alphanumeric keys pass thru
	if (normalizedEvent.keyCode < 47) return;
	if ((normalizedEvent.keyCode >= 91) && (normalizedEvent.keyCode <= 93)) return;
	if ((normalizedEvent.keyCode >= 112) && (normalizedEvent.keyCode <= 165)) return;
	if (normalizedEvent.keyCode >= 246) return;

	// The regexp for a valid email addres is: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
	// The following regexp is an expression that encapsulated what happens when the user is entering an email address
	// and not all the parts of the email address have been typed yet.
	var re = /^\w+([-+.]($|\w+))*(@(\w+([-.]($|\w+))*(\.(\w+([-.]($|\w+))*)?)?)?)?$/;
	var tempValue = srcElement.value;
	var matchResult = tempValue.match (re);
	
	if (matchResult == null)
	{
		srcElement.value = srcElement.savedValue;
		// need to warn the user somehow that this character was ignored and why
		iTalkiesIssueMissTypedWarning (srcElement);
		normalizedEvent.returnValue = false;
		return false;
	}
}

function iTalkiesTelNumAreaCodeOnKeyUpValidateAndTab(e)
{
	var srcElement;
	var normalizedEvent;
	
	if (ie)
	{
		srcElement = event.srcElement;
		normalizedEvent = event;
	}
	if (moz) 
	{
		srcElement = e.target;
		normalizedEvent = e;
	}

	// let all keys other than alphanumeric keys pass thru
	if (normalizedEvent.keyCode < 47) return;
	if ((normalizedEvent.keyCode >= 91) && (normalizedEvent.keyCode <= 93)) return;
	if ((normalizedEvent.keyCode >= 112) && (normalizedEvent.keyCode <= 165)) return;
	if (normalizedEvent.keyCode >= 246) return;

	var re = /^\d{0,3}$/;
	var tempValue = srcElement.value;
	var matchResult = tempValue.match (re);
	
	if (matchResult == null)
	{
		srcElement.value = srcElement.savedValue;
		// need to warn the user somehow that this character was ignored and why
		iTalkiesIssueMissTypedWarning (srcElement);
		normalizedEvent.returnValue = false;
		return false;
	}
	else if (srcElement.value.length == 3)
	{
		window.document.getElementById ("UserInformation_TelNumFirstThree").focus();
		normalizedEvent.returnValue = false;
		return false;
	}
}

function iTalkiesTelNumFirstThreeOnKeyUpValidateAndTab(e)
{
	var srcElement;
	var normalizedEvent;
	
	if (ie)
	{
		srcElement = event.srcElement;
		normalizedEvent = event;
	}
	if (moz) 
	{
		srcElement = e.target;
		normalizedEvent = e;
	}

	if (normalizedEvent.keyCode < 47) return;
	if ((normalizedEvent.keyCode >= 91) && (normalizedEvent.keyCode <= 93)) return;
	if ((normalizedEvent.keyCode >= 112) && (normalizedEvent.keyCode <= 165)) return;
	if (normalizedEvent.keyCode >= 246) return;

	var re = /^\d{0,3}$/;
	var tempValue = srcElement.value;
	var matchResult = tempValue.match (re);
	
	if (matchResult == null)
	{
		srcElement.value = srcElement.savedValue;
		// need to warn the user somehow that this character was ignored and why
		iTalkiesIssueMissTypedWarning (srcElement);
		normalizedEvent.returnValue = false;
		return false;
	}
	else if (srcElement.value.length == 3)
	{
		window.document.getElementById ("UserInformation_TelNumLastFour").focus();
		normalizedEvent.returnValue = false;
		return false;
	}
}

function iTalkiesTelNumLastFourOnKeyUpValidate(e)
{
	var srcElement;
	var normalizedEvent;
	
	if (ie)
	{
		srcElement = event.srcElement;
		normalizedEvent = event;
	}
	if (moz) 
	{
		srcElement = e.target;
		normalizedEvent = e;
	}

	// if the user hit the enter key - let it pass thru to affect the submit and the validation that happens on the
	// submit
	if (normalizedEvent.keyCode < 47) return;
	if ((normalizedEvent.keyCode >= 91) && (normalizedEvent.keyCode <= 93)) return;
	if ((normalizedEvent.keyCode >= 112) && (normalizedEvent.keyCode <= 165)) return;
	if (normalizedEvent.keyCode >= 246) return;

	var re = /^\d{0,4}$/;
	var tempValue = srcElement.value;
	var matchResult = tempValue.match (re);
	
	if (matchResult == null)
	{
		srcElement.value = srcElement.savedValue;
		// need to warn the user somehow that this character was ignored and why
		iTalkiesIssueMissTypedWarning (srcElement);
		normalizedEvent.returnValue = false;
		return false;
	}
}

function ValidateUserName (srcElement, validationResult)
{
	var re;
	var matchResult;

	// Username is require - it must be a valid email address
	var userName = window.document.getElementById("UserInformation_Username");
	userName.style.backgroundColor = "#ffffa0";
	if ((userName.value != null) && (userName.value != ""))
	{
		re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		matchResult = userName.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			userName.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Email ID: specify a valid email address (e.g. someone@somedomain.com).<br>";
		}
	}
	else
	{
		validationResult.isValid = false;
		userName.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Email ID: please specify your email address.<br>";
	}
}

function ValidateSecretAnswer (srcElement, validationResult)
{
	var re;
	var matchResult;

	var secretAnswer = window.document.getElementById("UserInformation_SecretAnswer");
	
	// the secret answer control is not shown on the ForgotPass.aspx page initially
	// It is shown only once the user sends the server their username
	if (secretAnswer == null) return;
	secretAnswer.style.backgroundColor = "#ffffa0";
	if ((secretAnswer.value != null) && (secretAnswer.value != "") && (secretAnswer.value.length > 3))
	{
		re = /^\S+$/;
		matchResult = secretAnswer.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			secretAnswer.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Secret Answer: the answer to your secret question must be at least 4 characters long and without any spaces.<br>";
		}
	}
	else
	{
		if (srcElement.form.id != "UserInfoForm")
		{
			validationResult.isValid = false;
			secretAnswer.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Secret Answer: specify the answer to your secret question.  The answer must be at least 4 characters long and without any spaces.<br>";
		}
	}
}

function ValidateLoginInfo (srcElement, validationResult)
{
	var re;
	var matchResult;
	// First validate that all the required fields have values

	// Username is require - it must be a valid email address
	ValidateUserName (srcElement, validationResult);

	// Password is a required field that must be 8-20 characters long
	var password = window.document.getElementById("UserInformation_Password");
	password.style.backgroundColor = "#ffffa0";
	if ((password.value != null) && (password.value != "") && (password.value.length > 7))
	{
		re = /^\S+$/;
		matchResult = password.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			password.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Password: specify a password that is between 8-20 characters long and without any whitespace characters.<br>";
		}
	}
	else
	{
		if (srcElement.form.id != "UserInfoForm")
		{
			validationResult.isValid = false;
			password.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Password: please specify a password for your account that is between 8-20 characters long and without any whitespace characters.<br>";
		}
	}
}

function ValidateForgotPassInfo (srcElement, validationResult)
{
	ValidateUserName (srcElement, validationResult);
	
	ValidateSecretAnswer (srcElement, validationResult);
}

function ValidateUserInfo (srcElement, validationResult)
{
	var re;
	var matchResult;
	// First validate that all the required fields have values

	// Customer name is required - any value is a legal value
	var customerFirstName = window.document.getElementById("UserInformation_CustomerFirstName");
	var customerLastName = window.document.getElementById("UserInformation_CustomerLastName");
	customerFirstName.style.backgroundColor = "#ffffa0";
	customerLastName.style.backgroundColor = "#ffffa0";
	if ((customerFirstName.value == null) || (customerFirstName.value == "") ||
		(customerLastName.value == null) || (customerLastName.value == ""))
	{
		validationResult.isValid = false;
		customerFirstName.style.backgroundColor = "#dfad7b";
		customerLastName.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Your Name: please specify.<br>";
	}
	
	ValidateLoginInfo (srcElement, validationResult);
	
	// Now get a reference to the password control for use in this function
	var password = window.document.getElementById("UserInformation_Password");
	// Password confirmation is a required field and must match the password field
	var confirmPassword = window.document.getElementById("UserInformation_ConfirmPassword");
	confirmPassword.style.backgroundColor = "#ffffa0";
	if ((confirmPassword.value == null) || (confirmPassword.value == "") || (confirmPassword.value != password.value))
	{
		// The following is the NOT OF ((confirmPassword.value == "") && (srcElement.form.id == "UserInfoForm"))
		if ((confirmPassword.value != "") || (srcElement.form.id != "UserInfoForm"))
		{
			validationResult.isValid = false;
			password.style.backgroundColor = "#dfad7b";
			password.value = "";
			confirmPassword.style.backgroundColor = "#dfad7b";
			confirmPassword.value = "";
			validationResult.errorMessage += "- Password & Confirmation: Your password and its confirmation do not match. Ensure they match by retyping them.<br>";
		}
	}

	// SecretQuestion is a required field
	var secretQuestion = window.document.getElementById("UserInformation_SecretQuestion");
	secretQuestion.style.backgroundColor = "#ffffa0";
	if ((secretQuestion.value == null) || (secretQuestion.value == ""))
	{
		validationResult.isValid = false;
		secretQuestion.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Secret Question: specify a secret question the answer to which is easily remembered by you but not easily guessable by others.<br>";
	}
	
	ValidateSecretAnswer (srcElement, validationResult);

	// Now get a reference to the SecretAnswer control for use in this function
	var secretAnswer = window.document.getElementById("UserInformation_SecretAnswer");

	var confirmSecretAnswer = window.document.getElementById("UserInformation_ConfirmSecretAnswer");
	confirmSecretAnswer.style.backgroundColor = "#ffffa0";
	if ((confirmSecretAnswer.value == null) || (confirmSecretAnswer.value == "") || (confirmSecretAnswer.value != secretAnswer.value))
	{
		// The following is the NOT OF ((confirmSecretAnswer.value == "") && (srcElement.form.id == "UserInfoForm"))
		if ((confirmSecretAnswer.value != "") || (srcElement.form.id != "UserInfoForm"))
		{
			validationResult.isValid = false;
			secretAnswer.style.backgroundColor = "#dfad7b";
			secretAnswer.value = "";
			confirmSecretAnswer.style.backgroundColor = "#dfad7b";
			confirmSecretAnswer.value = "";
			validationResult.errorMessage += "- Secret Answer & Confirmation: the secret answer and its confirmation do not match. Ensure they match by retyping them.<br>";
		}
	}

	// Telephone number parts are required, of certain lenght and must be digits
	// Do not show too many error messages with respect to telephone numbers hence recurse the validation
	var telNumAreaCode = window.document.getElementById("UserInformation_TelNumAreaCode");
	telNumAreaCode.style.backgroundColor = "#ffffa0";
	var telNumberFirstThree = window.document.getElementById("UserInformation_TelNumFirstThree");
	telNumberFirstThree.style.backgroundColor = "#ffffa0";
	var telNumberLastFour = window.document.getElementById("UserInformation_TelNumLastFour");
	telNumberLastFour.style.backgroundColor = "#ffffa0";
	if ((telNumAreaCode.value != null) && (telNumAreaCode.value != ""))
	{
		re = /^\d{3}$/;
		matchResult = telNumAreaCode.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			telNumAreaCode.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Tel. # Area Code: specify a valid area code for your telephone number.<br>";
		}
		else
		{
			if ((telNumberFirstThree.value != null) && (telNumberFirstThree.value != ""))
			{
				re = /^\d{3}$/;
				matchResult = telNumberFirstThree.value.match (re);
				
				if (matchResult == null)
				{
					validationResult.isValid = false;
					telNumberFirstThree.style.backgroundColor = "#dfad7b";
					validationResult.errorMessage += "- Tel. #: specify a valid telephone number.<br>";
				}
				else
				{
					if ((telNumberLastFour.value != null) && (telNumberLastFour.value != ""))
					{
						re = /^\d{4}$/;
						matchResult = telNumberLastFour.value.match (re);
						
						if (matchResult == null)
						{
							validationResult.isValid = false;
							telNumberLastFour.style.backgroundColor = "#dfad7b";
							validationResult.errorMessage += "- Tel. #: specify a valid telephone number.<br>";;
						}
					}
					else
					{
						validationResult.isValid = false;
						telNumberLastFour.style.backgroundColor = "#dfad7b";
						validationResult.errorMessage += "- Tel. #: specify a valid telephone number.<br>";
					}
				}
			}
			else
			{
				validationResult.isValid = false;
				telNumberFirstThree.style.backgroundColor = "#dfad7b";
				validationResult.errorMessage += "- Tel. #: specify a valid telephone number.<br>";
			}
		}
	}
	else
	{
		validationResult.isValid = false;
		telNumAreaCode.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Tel. # Area Code: specify a valid area code for your telephone number.<br>";
	}

	// Shipping Address line 1 is required
	var shippingAddressLine1 = window.document.getElementById("UserInformation_ShippingAddress_Line1");
	shippingAddressLine1.style.backgroundColor = "#ffffa0";
	if ((shippingAddressLine1.value == null) || (shippingAddressLine1.value == ""))
	{
		validationResult.isValid = false;
		shippingAddressLine1.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Shipping Address Line1: specify the first line of your shipping address.<br>";
	}
	
	// No check required for Line2
	// var shippingAddressLine2 = window.document.getElementById("UserInformation_ShippingAddress_Line2");
	// No check required for UnitNumber
	// var shippingAddressUnitNumber = window.document.getElementById("UserInformation_ShippingAddress_UnitNumber");
	
	// Shipping address city is required
	var shippingAddressCity = window.document.getElementById("UserInformation_ShippingAddress_City");
	shippingAddressCity.style.backgroundColor = "#ffffa0";
	if ((shippingAddressCity.value == null) || (shippingAddressCity.value == ""))
	{
		validationResult.isValid = false;
		shippingAddressCity.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Shipping Address City: specify a city for your shipping address.<br>";
	}
	
	// Shipping address state is required
	var shippingAddressState = window.document.getElementById("UserInformation_ShippingAddress_State");
	shippingAddressState.style.backgroundColor = "#ffffa0";
	if ((shippingAddressState.value == null) || (shippingAddressState.value == ""))
	{
		validationResult.isValid = false;
		shippingAddressState.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Shipping Address State: specify a state for your shipping address.<br>";
	}
	
	// Shipping address zip code is required and should be a 5 digit number
	var shippingAddressPostalCode = window.document.getElementById("UserInformation_ShippingAddress_PostalCode");
	shippingAddressPostalCode.style.backgroundColor = "#ffffa0";
	if ((shippingAddressPostalCode.value != null) && (shippingAddressPostalCode.value != ""))
	{
		re = /^\d{5}$/;
		matchResult = shippingAddressPostalCode.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			shippingAddressPostalCode.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Shipping Address Zip: specify a valid Zip Code for your shipping address.<br>";
		}
	}
	else
	{
		validationResult.isValid = false;
		shippingAddressPostalCode.style.backgroundColor = "#dfad7b";
		validationResult.errorMessage += "- Shipping Address Zip: specify a valid Zip Code for your shipping address.<br>";
	}
	
	// Shipping address Zip code + 4 is present should be a four digit number
	var shippingAddressPostalCodeOptional = window.document.getElementById("UserInformation_ShippingAddress_PostalCodeOptionalPart");
	shippingAddressPostalCodeOptional.style.backgroundColor = "#ffffa0";
	if ((shippingAddressPostalCodeOptional.value != null) && (shippingAddressPostalCodeOptional.value != ""))
	{
		re = /^\d{4}$/;
		matchResult = shippingAddressPostalCodeOptional.value.match (re);
		
		if (matchResult == null)
		{
			validationResult.isValid = false;
			shippingAddressPostalCodeOptional.style.backgroundColor = "#dfad7b";
			validationResult.errorMessage += "- Shipping Address Zip+4: specify a valid Zip+4 value for your shipping address.<br>";
		}
	}
}

function iTalkiesUpdateUserInfoOnClick (e)
{
	var srcElement;
	var normalizedEvent;
	
	if (ie)
	{
		srcElement = event.srcElement;
		normalizedEvent = event;
	}
	if (moz) 
	{
		srcElement = e.target;
		normalizedEvent = e;
	}

	var errorMessage = "The following entries do not conform to the iTalkies user account requirements.  Please correct them and re-submit the user information update request.<br><br>";
	
	var validationResult = new iTalkiesValidationResult (true, errorMessage);
	ValidateUserInfo(srcElement, validationResult);
	
	if (validationResult.isValid == false)
	{
		window.document.getElementById ("ErrorMessage").innerHTML = validationResult.errorMessage;
		window.document.getElementById ("ErrorMessageRow").style.display = "";
		if (!safari) window.document.getElementById ("ErrorMessage").scrollIntoView (true);
		normalizedEvent.returnValue = false;
		delete validationResult;
		return false;
	}
	else
	{
		iTalkiesSetSubmitAction (srcElement);
	}
	delete validationResult;
}