/************************************************************************
'   File:           tdbFormScripts.js
'
'   Description:    Scripts to perform common form functions.
'					EMAIL VALIDATION CHANGES MADE HERE SHOULD BE
'					REPLICATED IN EMAILUTILS.JS.
'                   
'   Author:         Sean Gardner
'
'   Copyright:      (c) 2007 thedatabank, inc.
'
'************************************************************************/

var sFrmOrigFldVal = "";
var gInternetTopLevelDomains = null;

//----------------------
function ProperCase(s) {
//----------------------

	var sWord;
	
	if (sFrmOrigFldVal == "") {
		var sReg = s.replace(/(\w)(\w*)/g, function (strMatch, strFirst, strRest, intMatchPos, strSource) 
			{
				var sRtn = strFirst.toUpperCase() + strRest.toLowerCase();
				if (sRtn.length > 2 && sRtn.substring(0, 2) == "Mc") {
					sRtn = "Mc" + sRtn.charAt(2).toUpperCase() + sRtn.substring(3, sRtn.length);
				}
				if (sRtn == "Ne" || sRtn == "Se" || sRtn == "Sw" || sRtn == "Nw" || sRtn == "Po" || sRtn == "Apo" || sRtn == "Fpo") {
					sRtn = sRtn.toUpperCase();
				}
				return sRtn;
			});
		return sReg;
	} else {
		return s;
	}
}

//-----------------------------
function IsValidEmail(sEmail) {
//-----------------------------
	// First do the simple checks.
	if (sEmail.indexOf("@") == -1) return false;
	if (sEmail.indexOf(".") == -1) return false;
	if (sEmail.length > 255) return false;
	
	// Now the more expensive ones.
	//if (!(/^\w+([\.\'-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(sEmail))) return false;
	if (!IsStructurallyValidEmail(sEmail)) return false;
		
	if (gInternetTopLevelDomains == null) LoadTopLevelDomains();
	var sDomain = sEmail;
	if (sDomain.substring(sDomain.length - 1, sDomain.length) == ".") {
		sDomain = sDomain.substring(0, sDomain.length - 1);
	}
	var iDotPos = sDomain.indexOf(".");
	while (iDotPos > -1) {
		sDomain = sDomain.substring(iDotPos + 1, sDomain.length);
		iDotPos = sDomain.indexOf(".");
	}
	if (("," + gInternetTopLevelDomains + ",").indexOf("," + sDomain.toUpperCase() + ",") == -1) return false;
	
	return true;

}

//-----------------------------------------
function IsStructurallyValidEmail(sEmail) {
//-----------------------------------------
	// RFC 3696.

	var sPart, sAllowedChars, sCh;
	var bValid, bCharacterQuoting, bQuoting;
	var iCharIdx, iLen;
	
	bValid = true;
	sPart = "L";
	sAllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&'*+-/=?^_`.{|}~";
	bCharacterQuoting = false;
	bQuoting = false;
	
	iCharIdx = 0;
	iLen = sEmail.length;
	while (sPart != "D" && iCharIdx < iLen && bValid) {
		sCh = sEmail.charAt(iCharIdx);
		switch (sCh) {
			case "\\":
				if (!bQuoting) {
					bCharacterQuoting = !bCharacterQuoting;
				}
				break;
			case '"':
				if (bQuoting) {
					bQuoting = false;
				} else {
					if (bCharacterQuoting) {
						bCharacterQuoting = false;
					} else {
						bQuoting = true;
					}
				}
				break;
			case "@":
				if (!bQuoting) {
					if (bCharacterQuoting) {
						bCharacterQuoting = false;
					} else {
						if (iCharIdx == 0)
							bValid = false;
						else if (sEmail.charAt(iCharIdx - 1) == "." || sEmail.substring(iCharIdx - 2, iCharIdx) == '."')
							bValid = false;
						else
							sPart = "D";
					}
				}
				break;
			case ".":
				if (bQuoting) {
					if (iCharIdx == 1) bValid = false;
					if (sEmail.charAt(iCharIdx - 1) == ".") bValid = false;
				} else {
					if (bCharacterQuoting) {
						if (iCharIdx == 1) bValid = false;
						bCharacterQuoting = false;
					} else {
						if (iCharIdx == 0) {
							bValid = false;
						} else {
							if (sEmail.charAt(iCharIdx - 1) == ".")	bValid = false;
						}
					}
				}
				break;
			default:
				if (bQuoting) {
					if (sCh.charCodeAt(0) > 127) {
						bValid = false;
					}
				} else {
					if (bCharacterQuoting) {
						bCharacterQuoting = false;
						if (sCh.charCodeAt(0) > 127) {
							bValid = false;
						}
					} else {
						if (sAllowedChars.indexOf(sCh) == -1) {
							bValid = false;
						}
					}
				}
		}
		if (iCharIdx > 63 && sPart == "L") bValid = false;
		iCharIdx++;
	}
	if (bValid) {
		if (bQuoting || bCharacterQuoting || iCharIdx == iLen) {
			bValid = false;
		} else {
			if (sEmail.substring(0, iCharIdx).indexOf("\\.\\.") > -1 ||
				sEmail.substring(0, iCharIdx).indexOf('".""."') > -1 ||
				sEmail.substring(0, iCharIdx).indexOf('\\."."') > -1 ||
				sEmail.substring(0, iCharIdx).indexOf('"."\\.') > -1 ) {
				bValid = false;
			} else {
				sPart = sEmail.substring(iCharIdx, sEmail.length)
				if (sPart.length > 255) {
					bValid = false;
				}
			}
		}
	}
	
	sAllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789";
	sPart = "";
	while (iCharIdx < iLen && bValid) {
		sCh = sEmail.charAt(iCharIdx);
		if (sCh == ".") {
			if (sPart.length < 1 || sPart.length > 63) {
				bValid = false;
			} else {
				if (sPart.substring(2, 4) == "--" && sPart.substring(0, 2).toLowerCase() != "xn") {
					bValid = false;
				}
			}
			sPart = "";
		} else {
			if (sAllowedChars.indexOf(sCh) == -1) {
				bValid = false;
			} else {
				sPart += sCh;
			}
		}
		iCharIdx++;
	}

	return bValid;
}

//------------------------------
function LoadTopLevelDomains() {
//------------------------------
	gInternetTopLevelDomains = "AC,AD,AE,AERO,AF,AG,AI,AL,AM,AN,AO,AQ,AR,ARPA,AS,ASIA,AT,AU,AW,AX,AZ,BA,BB,BD,BE,BF," 
			+ "BG,BH,BI,BIZ,BJ,BM,BN,BO,BR,BS,BT,BV,BW,BY,BZ,CA,CAT,CC,CD,CF,CG,CH,CI,CK,CL,"
			+ "CM,CN,CO,COM,COOP,CR,CU,CV,CX,CY,CZ,DE,DJ,DK,DM,DO,DZ,EC,EDU,EE,EG,ER,ES,ET,"
			+ "EU,FI,FJ,FK,FM,FO,FR,GA,GB,GD,GE,GF,GG,GH,GI,GL,GM,GN,GOV,GP,GQ,GR,GS,GT,GU,"
			+ "GW,GY,HK,HM,HN,HR,HT,HU,ID,IE,IL,IM,IN,INFO,INT,IO,IQ,IR,IS,IT,JE,JM,JO,JOBS,"
			+ "JP,KE,KG,KH,KI,KM,KN,KP,KR,KW,KY,KZ,LA,LB,LC,LI,LK,LR,LS,LT,LU,LV,LY,MA,MC,MD,ME,"
			+ "MG,MH,MIL,MK,ML,MM,MN,MO,MOBI,MP,MQ,MR,MS,MT,MU,MUSEUM,MV,MW,MX,MY,MZ,NA,NAME,"
			+ "NC,NE,NET,NF,NG,NI,NL,NO,NP,NR,NU,NZ,OM,ORG,PA,PE,PF,PG,PH,PK,PL,PM,PN,PR,PRO,"
			+ "PS,PT,PW,PY,QA,RE,RO,RS,RU,RW,SA,SB,SC,SD,SE,SG,SH,SI,SJ,SK,SL,SM,SN,SO,SR,ST,SU,"
			+ "SV,SY,SZ,TC,TD,TEL,TF,TG,TH,TJ,TK,TL,TM,TN,TO,TP,TR,TRAVEL,TT,TV,TW,TZ,UA,UG,"
			+ "UK,UM,US,UY,UZ,VA,VC,VE,VG,VI,VN,VU,WF,WS,"
			+ "XN--0ZWM56D,XN--11B5BS3A9AJ6G,XN--80AKHBYKNJ4F,XN--9T4B11YI5A,XN--DEBA0AD,"
			+ "XN--G6W251D,XN--HGBK6AJ7F53BBA,XN--HLCJ6AYA9ESC7A,XN--JXALPDLP,XN--KGBECHTV,XN--ZCKZAH, "
			+ "YE,YT,YU,ZA,ZM,ZW";
}

//------------------------
function EnterHandler(e) {
//------------------------
	if (!e) {
		if (event) {
			e = event;
		} else {
			// Not expected in the modern browser age.  But if it happens...
			// In a textarea Enter will make a line feed.  Otherwise Enter may post the form.
			// If that happens the form will end up redisplay itself because we won't have run
			// the script to set the form action.
			return true;
		}
	}
	var node;
	if (e.target) {
		node = e.target;
	} else {
		if (e.srcElement) {
			node = e.srcElement;
		} else {
			node = null;
		}
	}
	if (e.keyCode == 13) {
		if (node && node.type && node.type.toLowerCase() == "textarea") {
			return true;
		} else {
			validate();
			return false;
		}
	} else {
		return true;
	}
}
document.onkeypress = EnterHandler;
