// General Gainesville Cycling Club routines

goback=false;
SFfreeT=0;
T1=0;
T2=0;

function specialCheck(instring){
	outstring = "";
	isNormal = /[a-zA-Z0-9\.\, ]/;
	isSpecial = "&#$%()*+-:;<>=?@'\""; //"
	transSpecial = ["&#38;","#","$","%","(",")","*","+","-",":",";","&#60;","&#62;","=","?","@","'","&#34;"];
	for (i=0;i<instring.length;i++) {
		work = instring.charAt(i); // instring.substr(i,1);
		if (isNormal.test(work)) {
			outstring = outstring + work;
		} // endif
		else {
			whereat = isSpecial.indexof(work);
			if (whereat != -1) {
				alert ("Found Special " +  whereat + " " + transSpecial[whereat]);
				outstring = outstring + transSpecial[whereat];
			} // endif
		} // endelse 
	} // endfor 
	return outstring;
} // end specialCheck

function capCheck(nameField){
	instring = nameField.value;
	outstring = "";
	startword = true;
	M = false;
	a = false;
	c = false;
	isCap=/[A-Z]/;
	isLow=/[a-z]/;
	isOpen=/[\"(]/; //"
	isClose=/[\.\")]/; //"
	for (i=0;i<instring.length;i++) {
		work = instring.charAt(i); // instring.substr(i,1);
		if (isCap.test(work)) {
			if (startword) {
				startword = false;
				outstring = outstring + work;
				M = (work == "M");
			} // endif
			else {
				if (M && c) {
					outstring = outstring + work;
					if (a) {alert("Check capitalization");}
					M = false;
					c = false;
					a = false;
				} // endif
				else {
					outstring = outstring + work.toLowerCase();
					if (M && a) {
						c = (work == "C");
						if (!c) {M = false; a = false;}
					} // endif
					else if (M) {
						a = (work == "A");
						if (!a) {
							c = (work == "C");
							if (!c) {M = false;}
						} // endif
					} // endelseif
				} // endelse
			} // endelse
		} // endif
		else if (isLow.test(work)) {
			if (startword) {
				startword = false;
				outstring = outstring + work.toUpperCase();
				M = (work == "m");
			} // endif
			else {
				if (M && c && !a) {
					outstring = outstring + work.toUpperCase();
					M = false;
					c = false;
				} // endif
				else {
					if (M && c && a) // do not force cap on Mac
						{M = false; c = false; a = false;}
					outstring = outstring + work;
					if (M && a) {
						c = (work == "c");
						if (!c) {M = false; a = false;}
					} // endif
					else if (M) {
						a = (work == "a");
						if (!a) {
							c = (work == "c");
							if (!c) {M = false;}
						} // endif
					} // endelseif
				} // endelse
			} // endelse
		} // end elseif
		else {
			if (work==" " && !startword) {
				startword = true;
				outstring = outstring + " ";
				M = false;
				a = false;
				c = false;
			} // endif
			else if (work=="-" && !startword) {
				startword=true;
				outstring=outstring + "-";
				M = false;
				a = false;
				c = false;
			}// end elseif
			else if (isOpen.test(work) && startword) {
				outstring=outstring + work;
				M = false;
				a = false;
				c = false;
			}// end elseif
			else if (isClose.test(work) && !startword) {
				startword=true;
				outstring=outstring + work + " ";
				M = false;
				a = false;
				c = false;
			}// end elseif
			else if (work=="'" && !startword) {
				startword=true;
				outstring=outstring + " ";
				M = false;
				a = false;
				c = false;
			}// end elseif
		} // end else
	} // end for
	nameField.value = outstring;
} // end capCheck

function numCheck(theNum) {
	outNum = "";
	isNum=/\d/
	for (i=0;i<theNum.length;i++) {
		work = theNum.charAt(i);
		if (isNum.test(work)) {
			outNum = outNum + work;}
	} // endfor
	return outNum;
} // end numCheck

function abortCheck(theString) {
	outString = "";
	isNotBad=/[^'"\\]/; //"'
	for (i=0;i<theString.length;i++) {
		work = theString.charAt(i);
		if (isNotBad.test(work)) {
			outString = outString + work;}
	} // endfor
	return outString;
} // end abortCheck

function doAbortCheck(theString) {
	theString.value = abortCheck(theString.value);
} // end doAbortCheck 

function phoneCheck(theNum, goodLen, goodNext) {
	if(goback) {goback=false;}
	else {
		theNum.value = numCheck(theNum.value);
		if(theNum.value.length==goodLen) {goodNext.focus();}
	}
}// end phoneCheck

function phoneLeave(theNum, goodLen, zeroNext) {
	if (theNum.value.length==0) {
		if (!goback)  {
			zeroNext.focus();
			goback=true;}
	}
	else if (theNum.value.length!=goodLen) {
		theNum.focus();
		theNum.select();
		goback=true;
		return false;
	}
}// end phoneLeave

function numberCheck(theNum) {
	theNum.value = numCheck(theNum.value);
} // end numberCheck

function checkEmail(email) {
	if (email.value == "") {return true;}
	validEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
	if (validEmail.test(email.value)) {return true;}
	alert("Invalid email address");
	email.focus();
	email.select();
	return false;
} // end checkEmail

function checkPass(pw) {
	if (!(pw.value == "" || pw.value == " ")) {return true;}
	else {
		alert("Password is required.");
		pw.focus();
		pw.select();
		return false;
	}
} // end checkEmail

function setFamily(last,first,sal) {
	if (sal.value <= " ") {
		sal.value =
			first.value + " " +
			last.value;}
} // end setFamily

function setCity(city) {
	switch (city.value) {
		case "g": // 1001
			city.value = "Gainesville"; break;
		case "t": // 73
			city.value = "Tallahassee"; break;
		case "tp": // 59
			city.value = "Tampa"; break;
		case "s": // 32
			city.value = "St Petersburg"; break;
		case "st": // 25
			city.value = "St Augustine"; break;
		case "sa": // 21
			city.value = "Sarasota"; break;
		case "j": // 100
			city.value = "Jacksonville"; break;
		case "o": // 149
			city.value = "Orlando"; break;
		case "oc": // 88
			city.value = "Ocala"; break;
		case "ov": // 17
			civy.value = "Oviedo"; break;
		case "a": // 51
			city.value = "Alachua"; break;
		case "ar": // 15
			city.value = "Archer"; break;
		case "m": // 18
			city.value = "Micanopy"; break;
		case "w": // 40
			city.value = "Winter Park"; break;
		case "c": // 18
			city.value = "Clearwater"; break;
		case "d": // 17
			city.value = "Dunnellon"; break;
		case "h": // 14
			city.value = "High Springs"; break;
		case "l": // 26
			city.value = "Longwood"; break;
		case "la": // 25
			city.value = "Lakeland"; break;
		case "n": // 27
			city.value = "Newberry"; break;
	} // endswitch
	capCheck(city);
} // end setCity

function showCodes() {
	alert("g Gainesville" + "\n" +
"a Alachua" + "\n" +
"ar Archer" + "\n" +
"c Clearwater" + "\n" +
"d Dunnellon" + "\n" +
"h High Springs" + "\n" +
"j Jacksonville" + "\n" +
"la Lakeland" + "\n" +
"l Longwood" + "\n" +
"m Micanopy" + "\n" +
"n Newberry" + "\n" +
"oc Ocala" + "\n" +
"o Orlando" + "\n" +
"ov Oviedo" + "\n" +
"sa Sarasota" + "\n" +
"st St Augustine" + "\n" +
"s St Petersburg" + "\n" +
"t Tallahassee" + "\n" +
"tp Tampa" + "\n" +
"w Winter Park");
} // end showCodes

function zipCheck(zip) {
	isNum=/^\d+$/
	if (zip.value.length == 5) {
		if (!isNum.test(zip.value)) {
			alert("Non-numerics in zip code");
			zip.focus();
			zip.select();
			return false;
		} // endif
	} // endif
	else if (zip.value.length == 9) {
		if (!isNum.test(zip.value)) {
			alert("Non-numerics in zip code");
			zip.focus();
			zip.select();
			return false;
		} // endif
		zip.value = zip.value.substr(0,5) + "-" + zip.value.substr(5,4);
	} // endelseif
	else if (zip.value.length == 10) {
		zip1 = zip.value.substr(0,5);
		zip2 = zip.value.substr(5,1);
		zip3 = zip.value.substr(6,4);
		if (!isNum.test(zip1)) {
			alert("Non-numerics in zip code");
			zip.focus();
			zip.select();
			return false;
		} // endif
		if (zip2 != "-") {
			alert("Bad zip code format");
			zip.focus();
			zip.select();
			return false;
		} // endif
		if (!isNum.test(zip3)) {
			alert("Non-numerics in zip code");
			zip.focus();
			zip.select();
			return false;
		} // endif
	} // endelseif
	else {
		alert("Invalid zip code");
		zip.focus();
		zip.select();
		return false;
	} // endelse
} // end zipCheck

function setColor () {
//	alert("db.js version 22");
	if (document.personform.clubcode) {
		clubcode = document.personform.clubcode.value;
		deceased = document.personform.deceased.value;
		if (clubcode == "G") {document.body.bgColor = "lemonchiffon";}
		if (clubcode == "F") {document.body.bgColor = "lightpink";}
		if (deceased == "X") {document.body.bgColor = "darkgray";}
	}
}// end setColor

function highl(fld) {
	fld.select();
} // end highl 

function outCheck() {
	ok = true;// ok to leave	unless something comes up below
	if (!confirm("Abandon Updates?")) {ok = false;}
	return ok;
} // end outCheck


