
function ValidatePhone(source, arguments) 
{
    if (typeof(source.controltovalidate) == "string") {
		// Reformat the phone number
		var sPhone = formatPhone( arguments.Value );

		// Replace the value of the control with the formatted number
		$('#' + source.controltovalidate).val(sPhone);
		
		// Validate that the resulting phone number is a valid phone number
	    var rx = new RegExp("((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}(( x){1,1}\\d{1,}){0,1}");
	    var matches = rx.exec(sPhone);
		arguments.IsValid = (matches != null && sPhone == matches[0]);
    }
}

function ValidatePhoneStrict(source, arguments) 
{
	// Same as above except we require that the phone has area code and no extension
    if (typeof(source.controltovalidate) == "string") {
		// Reformat the phone number
		var sPhone = formatPhone( arguments.Value );

		// Replace the value of the control with the formatted number
		$('#' + source.controltovalidate).val(sPhone);
		
		// Validate that the resulting phone number is a valid phone number
	    var rx = new RegExp("((\\(\\d{3}\\) ?)|(\\d{3}-))\\d{3}-\\d{4}");
	    var matches = rx.exec(sPhone);
		arguments.IsValid = (matches != null && sPhone == matches[0]);
    }
}

function ValidatePhoneNoFormat(source, arguments) 
{
    if (typeof(source.controltovalidate) == "string") {
		// Since we're not checking anything, it's always valid
		arguments.IsValid = true;
    }
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)
{   
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Removes all characters which do NOT appear in string bag from string s.
function stripCharsNotInBag (s, bag)
{   
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}


// reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... )       
//
// Handy function for arbitrarily inserting formatting characters
// or delimiters of various kinds within TARGETSTRING.
//
// reformat takes one named argument, a string s, and any number
// of other arguments.  The other arguments must be integers or
// strings.  These other arguments specify how string s is to be
// reformatted and how and where other strings are to be inserted
// into it.

function reformat (s)
{   
	var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

//	Reformat a text string of into a phone number represented as (###) ###-####
//	returns an empty string if string cannot be represented properly
//	bExtrasOK indicates that any extra digits should be appended to the phone
//	number as x#### (as for an extension).
function formatPhone( sInput )
{
	var sResult = "";
	var sInProgress = "";
	var sPhone = sInput;
	var sExt = "";
	
	// Is there an extension included?
	var nExt = sPhone.indexOf('x');
	if (nExt > 0) {
		sExt = sPhone.substr(nExt+1);
		sPhone = sPhone.substr(0, nExt);
	}
	// Remove all non-digit character from the string
	sInProgress = stripCharsNotInBag (sPhone, "0123456789");
	if (sInProgress.length >= 10) {
		sResult = reformat (sInProgress, "(", 3, ") ", 3, "-", 4);
		if (sInProgress.length > 10) {
			sExt += sInProgress.substr(10);
		}
	} else {
		if (sInProgress.length == 7) {
			sResult = reformat (sInProgress, "", 3, "-", 4);
		} else {
			// just leave it as it was
			sResult = sInput;
			sExt = "";
		}
	}
	if (sExt != "") 
		sResult += ' x' + sExt;
	return sResult;
}


