
/*
* 	Checks if the string is a valid email
*/
function isPhoneNumberValid(str) {

	var re11digit=/^\d{11}$/ //regular expression defining a 11 digit number
	
	if (str.charAt(0) == '1') {
		if (str.search(re11digit) > -1) return true;
	}
	
	return false;

}

/*
* 	Checks for the valid entries in a form
*/
function isValid() {
	// check if number is empty

	if (isPhoneNumberValid($F('txt-field')) ) {
		return true;

	}

	return false;
}

/*
* 	Submits the form if entries are valid, otherwise show error
*/
function submitForm() {
	if (isValid()) {
		$('optinForm').submit();
	} else {
		alert('Please enter a valid 11-digit phone number including your country code (e.g. 18185558585)');
	}
	
}


   
/*
* 	Restricts the input in the textfield to certain characters
*/


Event.observe(window, "load", function() {
	var numb = "0123456789";

	// clear text field when selected
	$('txt-field').onfocus = function() {
		// if already cleared, do nothing
		if (this._cleared) return
		// when this code is executed, "this" keyword will in fact be the field itself
		this.clear()
		this._cleared = true		
	},
	
	// restrict input to numbers only
	$('txt-field').onkeyup = function(e) {
	/*
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
		if (KeyID == 13) {
			if (isValid()) {
				// do nothing
			} else {
				alert('Please enter a valid 11-digit phone number including your country code (e.g. 18185558585)');
				document.location.href= 'index.html';
			}
		}
	*/	

		var w = "";
		for (i=0; i < this.value.length; i++) {
			x = this.value.charAt(i);
			if (numb.indexOf(x,0) != -1) w += x;
		}
		this.value = w;
	}

});