//var n;
//var f;
//var p;
//var p1;
//function ValidatePhone(field, form)
//{
//	var pv=p1.value;
//	p="";
//	for (var i = 0; i < pv.length; i++)
//	{
//		if (pv.charAt(i).match(/\d/) != null)
//			p+=pv.charAt(i);
//	}
//	if (p.length>10)
//		p=p.substring(0,10);
//	if (p.length>0)
//		p="("+p;
//	if (p.length>4)
//	{
//		length1=p.length;
//		pre1=p.substring(0,4);
//		post1=p.substring(4,length1);
//		p=pre1+")"+post1;
//	}
//	if(p.length>8){
//		length3=p.length;
//		pre3=p.substring(0,8);
//		post3=p.substring(8,length3);
//		p=pre3+"-"+post3;
//	}
//	if (p!=pv)
//	{
//		eval('document.' + f + '.' + n + '.value="";');
//		eval('document.' + f + '.' + n + '.value="' + p + '";');
//	}
//}

//function PhoneFormat(field, form, country)
//{
//	n = field.id;
//	f = form.id;
//	
//	if (country == 'USA')
//	{
//		eval('p1 = document.' + f + '.' + n + ';');
//		eval('document.' + f + '.' + n + '.maxLength=13;');
//		ValidatePhone(field, form);
//	}
//	else
//	{
//		eval('document.' + f + '.' + n + '.maxLength=20;');
//	}		
//}


function ValidatePhone(field)
{
	var caretPos = getCaretPosition(field);
	var pv=field.value;
	var p="";
	for (var i = 0; i < pv.length; i++)
	{
		if (pv.charAt(i).match(/\d/) != null)
			p+=pv.charAt(i);
	}
	if (p.length>10)
		p=p.substring(0,10);
	if (p.length>0 && p.charAt(0) != '(')
	{
		p='('+p;
		caretPos+=1;
	}
	if (p.length > 4 && p.charAt(4) != ')')
	{
		length1=p.length;
		pre1=p.substring(0,4);
		post1=p.substring(4,length1);
		p=pre1+")"+post1;
		if (caretPos>=4 && pv.charAt(4)!=')') caretPos++;
	}
	if (p.length>8 && p.charAt(8) != '-')
	{
		length1=p.length;
		pre1=p.substring(0,8);
		post1=p.substring(8,length1);
		p=pre1+"-"+post1;
		if (caretPos>=8 && pv.charAt(8)!='-') caretPos++;
	}
	if(p.length>13) p = p.substring(0,13);
	if (p!=pv)
	{
		field.value = p;
		setCaretPosition(field, caretPos);
	}
}

function PhoneFormat(field, form, country)
{
	if (country == 'USA')
	{
		field.maxLength=13;
		ValidatePhone(field);
	}
	else
		field.maxLength=20;
}