//
// FormUtils.js
//   Form utilities used mainly by the membership registration wizards.
//
//   $Id: FormUtils.js,v 1.1.1.1 2003/10/28 19:04:10 mgalal Exp $
//

// Submits the given form to the given page
function submitFormToPage(form, page)  {
	form.action=page;
	form.submit();
}

// Prompts the user to cancel and if approved, redirected to the given page
function cancelWizard(page)  {
    if ( confirm("If you click 'OK', you will lose all data previously entered.\nClick 'Cancel' to close this window and continue with your membership.") )  {
        location.href=page;
    }
}

function stripNonNumeric(str)  {

	if ( str.match(/\d+/g) != null )  {
		return str.match(/\d+/g).join("");
	}  else  {
		return "";
	}
}

function formatPhoneNumber(str)  {

	if ( str.length == 10 )  {
	    return str.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)/, "($1)$2-$3");
	} else if ( str.length > 10 )  {
	    return str.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)(\d+)/, "($1)$2-$3 x $4");
	}
	return str;
}

/**
 *	Does all the strange things to the phone number that are required by Siebel
 *
 *	@param phone The hidden field where the Siebel-friendly phone number is stored (and picked up by our back-end code)
 *	@param inputPhone The text field the user enters their phone number (will be formatted if not an international number)
 *  @param phoneIntl The check box the user checks if the phone number is an international number
 */
function updatePhoneNumber(phone, inputPhone, phoneIntl)  {

	if ( phone.value = "" )
		inputPhone.value = "";

	if ( phoneIntl.checked ) {
		//The phone number is international

		phone.value = inputPhone.value;

		//Put a plus sign in front of the phone number if it doesn't have one already
		if ( phone.value.charAt(0) != '+' ) {
			phone.value = "+" + inputPhone.value;
		}

	}  else  {
		//The phone number is _not_ international

		phone.value = stripNonNumeric(inputPhone.value);
		//inputPhone.value = formatPhoneNumber(phone.value);
	}
}
