/*
Form Validation 
	functions for:
	validating email
	validating alpha
	validating alphanumeric
	validating numeric
	validating non-alphanumeric
*/
function isEmailAddr(email) {
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) {
		result = true;
		}
	}
	return result;
}
function isAlpha(obj) {
	var result = false;
	var inThere = obj.match(/\D/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/\w/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isNotAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/[^\w\s]/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isNum(obj) {
	var result = false;
	var inThere = obj.match(/\d/);
	if (inThere) {
		result = true;
	}
	return result;
}

function hasWhiteSpace(obj) {
	var result = false;
	var inThere = obj.match(/\s/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isChecked(obj) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked) {
			return "yes";
		}
	}
	return "no";
}



// Functions that validates the forms

function fvalid(form22) {

	// Billing First Name
		if (document.form22.sfname.value != "") {
		r1=new RegExp("^[a-zA-Z]+[-a-zA-Z' ]+[ ]*$");
		r2=new RegExp("[AaEeIiOoUuYy]+");
		
		if (!(r1.test(document.form22.sfname.value)&&r2.test(document.form22.sfname.value))) {
		 alert("Invalid value for \"First Name\" field.");
		 document.form22.sfname.focus();
		 return false;
		}
	} else {
		alert("The \"First Name\" field cannot be left blank.");
		document.form22.sfname.focus();
		return false;
	}
	
	// Billing Last Name
		if (document.form22.slname.value != "") {
		r1=new RegExp("^[a-zA-Z]+[-a-zA-Z' ]+[ ]*$");
		r2=new RegExp("[AaEeIiOoUuYy]+");
		
		if (!(r1.test(document.form22.slname.value)&&r2.test(document.form22.slname.value))) {
		 alert("Invalid value for \"Last Name\" field.");
		 document.form22.slname.focus();
		 return false;
		}
	} else {
		alert("The \"Last Name\" field cannot be left blank.");
		document.form22.slname.focus();
		return false;
	}
	
	// Shipping Address 1
	if (document.form22.sadd1.value != "") {
		r1=new RegExp("[a-zA-Z]+");
		r2=new RegExp("[0-9]+");
		if (!(r1.test(document.form22.sadd1.value)&&r2.test(document.form22.sadd1.value))) {
			alert("Invalid value for \"Street Address\" field.");
			document.form22.sadd1.focus();
			return false;
		}
	} else {
		alert("The \"Street Address\" field cannot be left blank.");
		document.form22.sadd1.focus();
		return false;
	}
	
	// Shipping State
	if (document.form22.sstate.value == "") {
		alert("The \"State\" field cannot be left blank.");
		document.form22.sstate.focus();
		return false;
	}
	
	// Shipping Zipcode
	if (document.form22.szip.value != "") {
		r1=new RegExp("^([ ]*)([0-9]{5})([ ]*)$");
		r2=new RegExp("^([ ]*)([A-Za-z][0-9][A-Za-z][ ]?([0-9][A-Za-z][0-9])?)([ ]*)$");
		
		if(document.form22.szip.value.indexOf("-") == 5)
			document.form22.szip.value=document.form22.szip.value.substring(0,5);
		
		if (!(r1.test(document.form22.szip.value)||r2.test(document.form22.szip.value))) {
			alert("Invalid value for \"Zipcode\" field.");
			document.form22.szip.focus();
			return false;
		}
	} else {
		alert("The \"Zipcode\" field cannot be left blank.");
		document.form22.szip.focus();
		return false;
	}
	
	//Email Address
	if (document.form22.email.value != "") {
		supported=0;
		if(window.RegExp){
			tempStr="a";
			tempReg=new RegExp(tempStr);
			if(tempReg.test(tempStr))
				supported=1;
		}
		if(!supported) {
			if (!((document.form22.email.value.indexOf(".")>2)&&(document.form22.email.value.indexOf("@")>0))) {
				alert("Invalid value for \"Email\" field.");
				document.form22.email.focus();
				return false;
			}
		}
			
		r1=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		r2=new RegExp("^.+\\@[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})([ ]*)$");

		if (!(!r1.test(document.form22.email.value)&&r2.test(document.form22.email.value))) {
			alert("Invalid value for \"Email\" field.");
			document.form22.email.focus();
			return false;
		}
	} else {
		alert("The \"Email Address\" field cannot be left blank.");
		document.form22.email.focus();
		return false;
	}
	
	// Phone Area Code
	if (document.form22.sph1.value != "") {
		with(new RegExp("[2-9][0-9]{2}"))
		if (!(test(document.form22.sph1.value))) {
			alert("Invalid value for \"Area Code\" field.");
			document.form22.sph1.focus();
			return false;
		}
	} else {
		alert("The \"Area Code\" field cannot be left blank.");
		document.form22.sph1.focus();
		return false;
	}
	
	// Phone Prefix
	if (document.form22.sph11.value != "") {
		r1=new RegExp("[0-9]{3}");
		r2=new RegExp("000|555|800");
		
		if (!(r1.test(document.form22.sph11.value)&&!r2.test(document.form22.sph11.value))) {
			alert("Invalid value for \"Phone Prefix\" field.");
			document.form22.sph11.focus();
			return false;
		}
	} else {
		alert("The \"Phone Prefix\" field cannot be left blank.");
		document.form22.sph11.focus();
		return false;
	}
	
	// Phone Suffix
	if (document.form22.sph111.value != "") {
		r1=new RegExp("[0-9]{4}");
		r2=new RegExp("0000");
		
		if (!(r1.test(document.form22.sph111.value)&&!r2.test(document.form22.sph111.value))) {
			alert("Invalid value for \"Phone Suffix\" field.");
			document.form22.sph111.focus();
			return false;
		}
	} else {
		alert("The \"Phone Suffix\" field cannot be left blank.");
		document.form22.sph111.focus();
		return false;
	}
	/*
	//Alt Phone
	if ((document.form22.sph2.value != "")||(document.form22.sph22.value != "")||(document.form22.sph222.value != "")) {
		// Alt Phone Area Code
		if (document.form22.sph2.value != "") {
			with(new RegExp("[2-9][0-9]{2}"))
			if (!(test(document.form22.sph2.value))) {
				alert("Invalid value for \"Phone Number\" field.");
				document.form22.sph2.focus();
				return false;
			}
		} else {
			alert("The \"Phone Number\" field cannot be left blank.");
			document.form22.sph2.focus();
			return false;
		}
	
		// Alt Phone Prefix
		if (document.form22.sph22.value != "") {
			r1=new RegExp("[0-9]{3}");
			r2=new RegExp("000|555|800");
		
			if (!(r1.test(document.form22.sph22.value)&&!r2.test(document.form22.sph22.value))) {
				alert("Invalid value for \"Phone Number\" field.");
				document.form22.sph22.focus();
				return false;
			}
		} else {
			alert("The \"Phone Number\" field cannot be left blank.");
			document.form22.sph22.focus();
			return false;
		}
	
		// Alt Phone Suffix
		if (document.form22.sph222.value != "") {
			r3=new RegExp("[0-9]{4}");
			r4=new RegExp("0000");
		
			if (!(r3.test(document.form22.sph222.value)&&!r4.test(document.form22.sph222.value))) {
				alert("Invalid value for \"Phone Number\" field.");
				document.form22.sph222.focus();
				return false;
			}
		} else {
			alert("The \"Phone Number\" field cannot be left blank.");
			document.form22.sph222.focus();
			return false;
		}
	}
	*/

	// Existing Customer
	if (document.form22.exist_user.value == "") {
		alert("Please select if you are an existing Dish Network customer.");
		document.form22.exist_user.focus();
		return false;
	}
	
	// Major Credit Card
	if (document.form22.HasCC.value == "") {
		alert("Please select if you have a Major Credit Card.");
		document.form22.HasCC.focus();
		return false;
	}
}
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
		found = true;
	else
		index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
return true;
}