///<Documentation>
///<Class name="Phone">
Phone=function(number,required) 
{
	this.Constructor(number,required);
}

///<Constructor>
///<summary>Phone constructor</summary>
///<param name="number" type="string" required="false">
///     The text representation of the phone number.
///</param>
Phone.prototype.Constructor=function(number,required) 
{
	
	this.Exception=null;
	this.AreaCode='';
	this.LocalCode='';
	this.UniqueCode='';
	this.Extension='';
	this.Value=number?number:null;
	this.FormattedNumber='';
	this.IsRequired=required?required:false;
	
	__validPhoneNonNumericsPattern=/[\(\)\.\-\x\X\ ]/g;
	
	if(number!=undefined)
		this.Parse(number);	
}
///</Constructor>

///<Methods>
///<Method name="IsValid">
///<summary>Determines whether the number represents a valid phone number</summary>
///<param name="number" type="string" required="false">
///     The text representation of the phone number.
///</param>
///<returns>Boolean</returns>
Phone.prototype.IsValid=function(number)
{
	if(number==null)
		if(this.Value==null)
			return this.Exception==null;
		else
			number=this.Value;
	
	this.Exception=null;
   if (number == "" || number == null) 
   {
   	if(this.IsRequired)
   	{
      	this.Exception = new Exception("Phone", "IsValid", "No number was entered.");
        	return false;
      }
      return true;
   }
   
	var phone = number.replace(__validPhoneNonNumericsPattern, '');     
   if (/[^0-9]/.test(phone)) 
        this.Exception = new Exception("Phone", "IsValid", "The phone number contains illegal characters.");
   else if (phone.length < 10) 
        this.Exception = new Exception("Phone", "IsValid", "The phone number is too short. Please be sure to include the area code.");

   return this.Exception==null;
}
///</Method>

///<Method name="Parse">
///<summary>
///	Parses the number into it's base parts. This also creates the Formatted text
///	version of the phone number.
///</summary>
///<param name="number" type="string" required="false">
///     The text representation of the phone number.
///</param>
///<returns>Boolean</returns>
Phone.prototype.Parse=function(number)
{
	if(this.IsValid(number))
	{
		var phone = number.replace(__validPhoneNonNumericsPattern, '');     
		this.AreaCode=phone.substr(0,3);
		this.LocalCode=phone.substr(3,3);
		this.UniqueCode=phone.substr(6,4);
		
		this.FormattedNumber = "("+this.AreaCode+") "+this.LocalCode+" - "+this.UniqueCode;
		
		if(phone.length>10)
		{
			this.Extension=phone.substr(10,phone.length-10);
			this.FormattedNumber+=' x '+this.Extension;
		}
		return true;
	}
	
	return false;
}
///</Method>
///</Methods>
///</Class>


///<Class name="Fax">
Fax = function(number)
{
	this.Constructor(number);
}

///<Inherits class="Phone">
Fax.prototype=new Phone();
///</Inherits>

///<Methods>
///<Method name="IsValid">
///<summary>Overrides the Phone validation. The fax can not have an Extension</summary>
///<param name="number" type="string" required="false">
///     The text representation of the phone number.
///</param>
///<returns>Boolean</returns>
Fax.prototype.IsValid=function(number)
{
	if(number==null)
		return this.Exception==null;
		
	this.Exception=null;

	var phone = new Phone(number);
	if (phone.IsValid())
	{
		if(phone.Extension=='')
			return true;
      this.Exception = new Exception("Fax", "IsValid", "Faxes can not have extensions.");
	}
	else
		this.Exception=phone.Exception;
			
	return false;
}
///</Method>
///</Methods>
///</Class>

///<UnitTests>
try
{
	if(__JSUnit_version>0)
	{
		
		var test=function()
		{
			var 	expected={"Exception":null,"AreaCode":"614","LocalCode":"459","UniqueCode":"9841","Extension":"323498","Value":"(614) 459-9841x323498","FormattedNumber":"(614) 459 - 9841 x 323498","IsRequired":false},
					actual=new Phone('(614) 459-9841x323498');
					
			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Phone','Constructor','Tests the constructor for creating a phone object','positive',test);

		test=function()
		{
			var 	expected=true,
					actual=new Phone('(614) 459-9841x234').IsValid();

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Phone','IsValid','Tests valid phone number','positive',test);

		test=function()
		{
			var 	expected=false,
					actual=new Phone('(614) 4*&9-9841').IsValid();

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Phone','IsValid','Tests phone with special characters','negative',test);

		test=function()
		{
			var 	expected=false,
					actual=new Phone('(614) 4zh9-9841').IsValid();

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Phone','IsValid','Tests phone with alpha characters','negative',test);
		
		test=function()
		{
			var 	expected={"Exception":null,"AreaCode":"614","LocalCode":"459","UniqueCode":"9841","Extension":"","Value":"(614) 459-9841","FormattedNumber":"(614) 459 - 9841","IsRequired":false},
					actual=new Fax('(614) 459-9841');

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Fax','Constructor','Tests the constructor for creating a Fax object','positive',test);

		test=function()
		{
			var 	expected=true,
					actual=new Fax('(614) 459-9841').IsValid();

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Fax','IsValid','Tests valid fax number','positive',test);

		test=function()
		{
			var 	expected=false,
					actual=new Fax('(614) 459-9841x324').IsValid();

			return new JSUnitTestResult(actual,expected);
		}
		__jsUnit.AddTest('Fax','IsValid','Tests invalid fax number','negative',test);
	}
}
catch(err){}
///</UnitTests>
///</Documentation>