///// All added June, 2006 by Paul Appleby

function fix_tel(x,y,f){
	if (x!=""){
		x=x.replace(/\s/g, '');
		x=x.replace(/\D/g, '');
		if (x.length != 10){alert ("Please use this format 888-888-8888");eval("document."+f+"."+y+".focus()");return;}
		else {
			x=x.replace(/(\d\d\d)(\d\d\d)(\d\d\d\d)/g, '$1-$2-$3');
			eval("document."+f+"."+y+".value=x");
		}
	}
}

function fix_postal(x,y,f){
	if (x!=""){
		objThisform=eval("document."+f);
		x=x.replace(/\s/g, '');
		objThisform.postal_code.value=x;
		myRegexp=/^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]$/i;
		if (myRegexp.test(objThisform.postal_code.value)==0){
			alert ("Please use this format M2P2Q4");
			eval("document."+f+"."+y+".focus()");return;
		}
	}
}

var reset_it=0;
var f=1; //// This allows you to start with the Password.
function verify_chars(x,y){
	
	objThisform=eval("document."+y);
	//// We always want to make sure the Login format is correct before moving on to the Password.
	if (x=="login"){
		myRegexp=/^[a-zA-Z0-9]{4,10}$/i;
		if (myRegexp.test(objThisform.login.value)==0){
			alert("You must use 4 to 10 alpha-numeric characters for your Login Name.");
			objThisform.login.focus();
			f=0; //// The Login format is still not correct.
		}
		else {f=1;} //// Now the Login format is correct, and we'll allow moving on to the Password if necessary.
	}
	else if (x=="password"){ //// We start with the Password or the Login format above is correct, so let's check the Password format.
		myRegexp=/^[a-zA-Z0-9]{4,10}$/i;
		if (myRegexp.test(objThisform.password.value)==0){
			if (f==1){ //// If the Login is in the correct format, deal with the Password
				alert("You must use 4 to 10 alpha-numeric characters for your Password.");
				objThisform.password.focus();
			}
			else if (f==0){objThisform.login.focus();} //// Otherwise, go back and get the Login format correct.
		}
		if (f==0){objThisform.login.focus();} //// If the Password format is correct and the Login isn't, go back and deal with the Login.
	}
	//// This seemed necessary, but is not.
	//// This is triggered by the onMouseDown in the Reset button in registration.asp 
	//// and is necessary to reset the form after an alert is triggered in verify_chars() above.
	//// Clicking the Reset button, onMouseDown sets "reset_it=1".
	//if (reset_it==1){reset_it=0; objThisform.reset();}
}

/// Because of Netscape not doing verify_chars() properly, 
/// and because the user may have never entered the login and password fields, 
/// we validate the login and password fields again before submitting the form.
/// We validate the other fields on the server. 
function validate(x){
	objThisform=eval("document."+x);
	
	myRegexp=/^[a-zA-Z0-9]{4,10}$/i;
	if (myRegexp.test(objThisform.login.value)==0){
		alert("You must use 4 to 10 alpha-numeric characters for your Login Name.");
		objThisform.login.focus();
		return false;
	}
	
	myRegexp=/^[a-zA-Z0-9]{4,10}$/i;
	if (myRegexp.test(objThisform.password.value)==0){
		alert("You must use 4 to 10 alpha-numeric characters for your Password.");
		objThisform.password.focus();
		return false;
	}
	
	if (objThisform.student[1].checked  && (objThisform.school.value=="" || objThisform.school.value==" ")){
		alert("You say you are a student. Please enter the name of the school you are attending.");
		objThisform.school.focus();
		return false;
	}
	if (objThisform.amber_alert[1].checked && (objThisform.primary_call_letters.value=="" || objThisform.primary_call_letters.value==" ")){
		alert("Please enter your station's primary call letters.");
		objThisform.station.focus();
		return false;
	}
	
	//// After everything is validated, we send amber_alert and school info if their respective "yes" radio buttons are checked.
	//// Otherwise, we ensure their text input box values contain "".
	if (objThisform.student[0].checked){objThisform.school.value="";}
	if (objThisform.student[1].checked){objThisform.organization.value="";objThisform.category_id.value="";objThisform.department.value="";}
	if (objThisform.amber_alert[0].checked){objThisform.primary_call_letters.value="";}
	else {
		// Remove leading, trailing and extra spaces
		myObj=objThisform.primary_call_letters.value;
		myObj.replace(/^\s+/g,'');
		myObj.replace(/\s+$/g,'');
		myObj=myObj.replace(/\s+/g,' ');
	}
	
	return true;
}