var accountCountry = "USA";
var accountState = "";

function CoolsacCountryChange(countryClientID, phoneClientID)
{
	accountCountry = document.getElementById(countryClientID).value;
/*	
	if(!ValidateCoolsacPhone(document.getElementById(phoneClientID).value))
	{
		document.getElementById(phoneClientID).value = "";
	}
*/
}

function CoolsacStateChange(stateClientID)
{
	accountState = document.getElementById(stateClientID).value;
}

function ValidateCoolsacPhone(phoneNumber)
{
	var phoneValid = true;
	
	phoneNumber = new String(phoneNumber);
	
	if(
			(
					((accountCountry == "USA") || (accountCountry == "Canada"))
				&&	((accountState != "PR") && (accountState != "VI"))
			)
		&&	(phoneNumber != "")
	)
	{
	
		// var regexText = "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}";
		var phoneRegex = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/ig; // new RegExp(regexText, "ig");
		var result = phoneNumber.match(phoneRegex);
		
		if((result == null) || (result.index != 0) || (result[0].length != phoneNumber.length))
		{
			phoneValid = false;
		}
	}
	
	return phoneValid;
}


function CoolsacPhoneValidation(sender, args)
{
	args.IsValid = ValidateCoolsacPhone(args.Value)
}


function CoolsacPhoneFormatAllCountries(vID, vValue, e)  
{

	if(
			((accountCountry != "USA") && (accountCountry != "Canada"))
		||	((accountState == "PR") || (accountState == "VI"))
	)
	{
		return;
	}
	
	var vName = document.getElementById(vID);

	// vName = object name
	// vValue = value in the field being checked
	// e = event
	var nLen = vValue.length;
	var strSeperator = "-";
	
	// Get ASCII value of key that user pressed
	var key = window.event.keyCode;
				
	//Do not allow more than one decimal point
	// Was key that was pressed a numeric character (0-9)?
	if (((key > 47 && key < 58) || (key >= 96 && key <= 105)) && (nLen < 13))
	{ 
		if (nLen == 3)  
		{
			vName.value = '(' + vValue + ')'; //strSeperator;
		}
		else if(nLen == 8)
		{
			vName.value = vValue + strSeperator; //strSeperator;
		}
		return; // if so, do nothing
	}
	else if(key == 8 || key == 37 || key == 39) //backspace
		return;
	else if(key == 46) // delete
		return;
	else if(key == 9) //tab
		return;
	else
		window.event.returnValue = null; // otherwise, discard character
	
}

