﻿var idListRegExp = /^\d{1,8}(\,\d{1,8})*$/;
var allowAllRegExp = /^.*$/;
var allowAlphaNumeric = /^[a-zA-Z0-9]*$/;
var dateRegExp = /^((((0[13578])|(1[02]))[\/](([0-2][0-9])|(3[01])))|(((0[469])|(11))[\/](([0-2][0-9])|(30)))|(02[\/][0-2][0-9]))[\/]\d{4}$/;
var decimalRegExp = /^-?((\d{1,6}(\.\d{1,3})?)|\.\d{1,3})$/;
var decimalRegExp2 = /^-?(((\d+(\.\d{1,5})?)|((\d*\.)?\d{1,5})))$/;
var emailRegExp = /^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@([a-zA-Z0-9_\-]+\.)+([a-zA-Z]{2,6})$/;
var emailListRegExp = /((^|(([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@([a-zA-Z0-9_\-]+\.)+([a-zA-Z]{2,6}))+\s?(,|;)\s?))[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@([a-zA-Z0-9_\-]+\.)+([a-zA-Z]{2,6}))+$/;
var hexColorRegExp = /^\#[a-fA-F0-9]{6}$/;
var integerRegExp = /(^[0]$)|(^-?[1-9](\d{1,8})?$)/;
var integerRegExpNotNeg = /^\d+$/;
var leaveNumbersRegExp = /\D/g;
var numbersRegExp = /^\d+$/;
var phoneRegExp = /^[1-9]{1}\d{6}$/;
var areaCodeRegExp = /^[1-9]{1}\d{2}$/;
var rmvEmAdnEnDashes = /\u2012|\u2013|\u2014|\u2015/g;
var rmvEllipsis = /\u2026|\u2025/g;
var rmvSglQuotes = /\u0027|\u2018|\u2019/g;
var rmvDblQuotes = /\u201C|\u201D|\u0022/g;
var rmvPrePstSpcs = /^(\s*)([\W\w]*)(\b\s*$)/;
var rmvAllSpacesRegExp = /\s/;
var rmvSpacesRegExp = /^(\s*)$/;
var smallIntRegExp = /(^[0]$)|(^-?[1-9]{1}(\d{1,3})?$)|(^-?[1-2]{1}\d{4}$)|(^-?[3]{1}[0-1]{1}\d{3}$)|(^-?[3]{1}[2]{1}[0-6]{1}\d{2}$)|(^-?[3]{1}[2]{1}[7]{1}[0-5]{1}\d{1}$)|(^-?[3]{1}[2]{1}[7]{1}[6]{1}[0-7]{1}$)/;
var timeRegExp = /^(((0|1){1}\d{1})|(2{1}[0-3]{1}))\:{1}[0-5]{1}\d{1}$/;
var tinyIntRegExp = /(^[0]$)|(^[1-9]{1}(\d{1})?$)|(^[1]{1}(\d{1,2})?$)|(^[2]{1}[0-4]{1}\d{1}$)|(^[2]{1}[5]{1}[0-5]{1}$)/;
var urlRegExp = /^(ht|f)tp(s?)\:\/\/[a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+){2,}(\/?)([a-zA-Z0-9\-\.\=\?\,\@\'\/\\\+&%\$#_]*)?$/;
var findHTMLRegExp =  /<\/?(html|head|body|form|table|tr|td|script|javascript|input|select)/i;
var findHTMLBrochuresRegExp =  /<\/?(html|head|body|form|script|javascript|input|select)/i;
var findAllHTMLTags = /<\/?(a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dir|div|dfn|dl|dt|em|fieldset|font|form|frame|frameset|h[1-6]|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var|xmp){1}(\s(\"[^\"]*\"*|[^>])*)*>/i;
var ipRegExp = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var fileRegExp = /^[\w][\w.-]{1,100}$/i;
var passwordRegExp = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{1,20}$/;

function trim(origStr) {
	noNbspStr = convertNbspsToSps(origStr);
	fnlStr = noNbspStr.replace(rmvPrePstSpcs, '$2');
	return fnlStr;
}

function convertNbspsToSps(origStr) {
	noNbspStr = '';
	for (iChr = 0; iChr < origStr.length; iChr++) {
		if (origStr.charCodeAt(iChr) == '160')
			noNbspStr = noNbspStr + ' ';
		else
			noNbspStr = noNbspStr + origStr.charAt(iChr);
	}

	return noNbspStr;
}

function trimExtraSpaces(origStr) {
	trimedStr = trim(origStr);
	cleanStr = '';
	prevChar = trimedStr.charAt(0);
	for (jChr = 0; jChr < trimedStr.length; jChr++) {
		if (!(trimedStr.charAt(jChr) == ' ' && prevChar == ' ')) {
			cleanStr = cleanStr + trimedStr.charAt(jChr);
			prevChar = trimedStr.charAt(jChr);
		}
	}

	return cleanStr;
}

function phoneFormat(str) {
	if ((trimNNChars(str.value)).length == 10) {
		sTemp = "(" + sTemp.substring(0, 3) + ") " + sTemp.substring(3, 6) + "-" + sTemp.substring(6, 10);str.value = sTemp;
		return true;
	}

	return false;
}

function validateDate(inputStr) {
	var delim1 = inputStr.indexOf("/");
	var delim2 = inputStr.lastIndexOf("/");
	var mm = parseInt(inputStr.substring(0,delim1),10);
	var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10);
	var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);

	if (mm == 2) {
		if (yyyy % 4 > 0 && dd > 28) {
			return false;
		} else if (dd > 29) {
			return false;
		}
	} if (dd < 1 || yyyy < 1900) {
		return false;
	} else {
		return true;
	}
}

function trimAll(strValue){return strValue.replace(rmvSpacesRegExp, '');}

function rplcEmAndEnDash(strValue){return strValue.replace(rmvEmAdnEnDashes, '-');}

function rplcDblQuotes(strValue){return strValue.replace(rmvDblQuotes, '&quot;');}

function rplcSnglQuotesOld(strValue){return strValue.replace(rmvSglQuotes, '`');}

function rplcDblQuotesOld(strValue){return strValue.replace(rmvDblQuotes, '"');}

function trimNNChars(strValue){return strValue.replace(leaveNumbersRegExp, '');}

function rplcEllipsis(strValue){return strValue.replace(rmvEllipsis, '...');}

function replaceSpecialChars(str) {
	//str.value = rplcSnglQuotes(str.value);
	//str.value = rplcDblQuotes(str.value);
	str.value = rplcEmAndEnDash(str.value);
	str.value = trimExtraSpaces(str.value);
	str.value = rplcEllipsis(str.value);
}

function isProper(string) {
	var tmpStr = string.toLowerCase();

	tmpStr = tmpStr.replace(/(\d+)([a-z]{3,})/gi, "$1 $2"); // add space after numbers when 3+ alphachars follow
	tmpStr = tmpStr.replace(/\,/g, " "); // delete commas - replace by space (essential!)
	tmpStr = tmpStr.replace(/(\d+)([.{2,}$])/gi, " $1$2"); // add 4 spaces before numbers not at end of string
	tmpStr = tmpStr.replace(/(\S*)\"(\D+)\"(\S*)/g, '$1 "$2" $3'); // put spaces around "string" (force uppercase)
	tmpStr = tmpStr.replace(/o\'/gi, "O' "); // change o' to O'+space (O' Reilly) (force uppercase)
	tmpStr = tmpStr.replace(/(\s|\.)(mc)([a-z])/g, "$1mc $3"); // add space after Mc (force uppercase)
	tmpStr = tmpStr.replace(/(\D)\./g, "$1. "); // add space after literal . (B. M. Smith > uppercase)
	tmpStr = tmpStr.replace(/\-([a-z])/g, " - $1"); // add spaces around hyphens (force uppercase)
	tmpStr = tmpStr.replace(/(^\s+)/, ""); // remove spaces at start of string
	tmpStr = tmpStr.replace(/\s{4,}/g, " "); // remove excessive spaces > 4

	if ((/fpo\b/.test(tmpStr)) || (/fpo\d/.test(tmpStr))) {
		tmpStr = tmpStr.toUpperCase(); 
		tmpStr = tmpStr.replace (/([A-Z])(\s{2,})/g, "$1 "); // remove spaces >1 between A-Z and A-Z/0-9 (not converse)
	}

	var SplitStuff = tmpStr.split(" ");
	var ArrLen = SplitStuff.length;

	for (var k = 0; k < ArrLen; k++) {
		var word = SplitStuff[k];
		if(/\d/.test(word)) { word = word.toUpperCase(); }
		var wordLen = word.length;
		var posn = word.search(/[a-zA-Z]/); 
		var firstChars = word.substring(0, posn+1).toUpperCase();
		var postString = word.substring(posn+1,wordLen);
		word = firstChars + postString;
		word = word.replace( /C\/O\b/g, "C\/o" );
		word = word.replace( /Po\b/g, "PO" );
		word = word.replace(/\'a/, "'A"); // De'Ath and D'Arcy!

		SplitStuff[k] = word;
	}

	/* post fixes */
	var newline = SplitStuff.join(" ");
	// newline = newline.replace (/\.\s/g, "."); // re-format names B. M. Smith > B.M.Smith

	/* Recall that "Replace()" is case sensitive */
	newline = newline.replace(" Of ", " of ");
	newline = newline.replace(" And ", " and ");
	newline = newline.replace("'S ", "'s " );
	newline = newline.replace(" At ", " at ");
	newline = newline.replace(" The ", " the ");
	newline = newline.replace(" For ", " for ");
	newline = newline.replace(" De ", " de ");
	newline = newline.replace(" Y ", " y ");
	newline = newline.replace(" In ", " in ");
	newline = newline.replace("å", "Å");

	/* roman numerals */
	newline = newline.replace(" Iii", " III");
	newline = newline.replace(" Ii", " II");

	/* specific cases of acronyms */
	newline = newline.replace("Abc ", "ABC ");
	newline = newline.replace("Abcd", "ABCD ");
	newline = newline.replace("Aaa ", "AAA ");
	newline = newline.replace("Cbe ", "CBE ");
	newline = newline.replace("Cei ", "CEI ");
	newline = newline.replace("Itt ", "ITT ");
	newline = newline.replace("Mbti ", "MBTI ");
	newline = newline.replace("Cuny ", "CUNY ");
	newline = newline.replace("Suny ", "SUNY ");
	newline = newline.replace("Mta ", "MTA ");
	newline = newline.replace("Mti ", "MTI ");
	newline = newline.replace("Qpe ", "QPE ");
	newline = newline.replace(" Ogc ", " OGC ");
	newline = newline.replace("Tci ", "TCI ");
	newline = newline.replace("The Cdl ", "The CDL ");
	newline = newline.replace("The Mbf ", "The MBF");
	newline = newline.replace("Lpn", "LPN");
	newline = newline.replace("Cvph ", "CVPH ");
	newline = newline.replace("Dch ", "DCH ");
	newline = newline.replace("Bmr ", "BMR ");
	newline = newline.replace("Isim ", "ISIM ");

	/* contractions */
	newline = newline.replace(" Mgt", " Management");
	newline = newline.replace("Trng", "Training");
	newline = newline.replace("Xray", "X-Ray");
	newline = newline.replace(" Sch ", " School ");
	newline = newline.replace(" Dba ", " dba ");

	/* specific names with special case */
	newline = newline.replace("Mcc", "McC");
	newline = newline.replace("Mcd", "McD");
	newline = newline.replace("Mch", "McH");
	newline = newline.replace("Mcg", "McG");
	newline = newline.replace("Mci", "McI");
	newline = newline.replace("Mck", "McK");
	newline = newline.replace("Mcl", "McL");
	newline = newline.replace("Mcm", "McM");
	newline = newline.replace("Mcn", "McN");
	newline = newline.replace("Mcp", "McP");

	/* adding punctuation */
	newline = newline.replace(" Inc", ", Inc");
	newline = newline.replace("Ft ", "Ft. ");
	newline = newline.replace("St ", "St. ");
	newline = newline.replace("Mt ", "Mt. ");

	/* U.S. state abbreviations */
	newline = newline.replace(" Ak ", " AK ");
	newline = newline.replace(" As ", " AS ");
	newline = newline.replace(" Az ", " AZ ");
	newline = newline.replace(" Ar ", " AR ");
	newline = newline.replace(" Ca ", " CA ");
	newline = newline.replace(" Co ", " CO ");
	newline = newline.replace(" Ct ", " CT ");
	newline = newline.replace(" De ", " DE ");
	newline = newline.replace(" Dc ", " DC ");
	newline = newline.replace(" Fl ", " FL ");
	newline = newline.replace(" Ga ", " GA ");
	newline = newline.replace(" Gu ", " GU ");
	newline = newline.replace(" Hi ", " HI ");
	newline = newline.replace(" Id ", " ID ");
	newline = newline.replace(" Il ", " IL ");
	newline = newline.replace(" In ", " IN ");
	newline = newline.replace(" Ia ", " IA ");
	newline = newline.replace(" Ks ", " KS ");
	newline = newline.replace(" Ky ", " KY ");
	newline = newline.replace(" La ", " LA ");
	newline = newline.replace(" Me ", " ME ");
	newline = newline.replace(" Md ", " MD ");
	newline = newline.replace(" Mh ", " MH ");
	newline = newline.replace(" Ma ", " MA ");
	newline = newline.replace(" Mi ", " MI ");
	newline = newline.replace(" Fm ", " FM ");
	newline = newline.replace(" Mn ", " MN ");
	newline = newline.replace(" Ms ", " MS ");
	newline = newline.replace(" Mo ", " MO ");
	newline = newline.replace(" Mt ", " MT ");
	newline = newline.replace(" Ne ", " NE ");
	newline = newline.replace(" Nv ", " NV ");
	newline = newline.replace(" Nh ", " NH ");
	newline = newline.replace(" Nj ", " NJ ");
	newline = newline.replace(" Nm ", " NM ");
	newline = newline.replace(" Ny ", " NY ");
	newline = newline.replace(" Nc ", " NC ");
	newline = newline.replace(" Nd ", " ND ");
	newline = newline.replace(" Mp ", " MP ");
	newline = newline.replace(" Oh ", " OH ");
	newline = newline.replace(" Ok ", " OK ");
	newline = newline.replace(" Or ", " OR ");
	newline = newline.replace(" Pw ", " PW ");
	newline = newline.replace(" Pa ", " PA ");
	newline = newline.replace(" Pr ", " PR ");
	newline = newline.replace(" Ri ", " RI ");
	newline = newline.replace(" Sc ", " SC ");
	newline = newline.replace(" Sd ", " SD ");
	newline = newline.replace(" Tn ", " TN ");
	newline = newline.replace(" Tx ", " TX ");
	newline = newline.replace(" Ut ", " UT ");
	newline = newline.replace(" Vt ", " VT ");
	newline = newline.replace(" Va ", " VA ");
	newline = newline.replace(" Vi ", " VI ");
	newline = newline.replace(" Wa ", " WA ");
	newline = newline.replace(" Wv ", " WV ");
	newline = newline.replace(" Wi ", " WI ");
	newline = newline.replace(" Wy ", " WY ");

	return newline;
}

function validate(str, regx, req, properTitleCase) {
	if ((trimAll(str.value)).length == 0) {
		str.value = '';
		if (req == '1') {
			return false;
		}
		return true;
	}

	if (regx == 'phoneRegExp' || regx == 'areaCodeRegExp'){
		str.value = str.value.replace(rmvAllSpacesRegExp, '');
		str.value = str.value.replace(/-/g, '');
	}
	result = eval(regx).test(str.value);
	replaceSpecialChars(str);

	if (regx == 'dateRegExp' && req == '0' && str.value == 'mm/dd/yyyy')
		return true;
	if (regx == 'dateRegExp' && result)
		return validateDate(str.value);

	if(regx == 'allowAllRegExp' && properTitleCase == '1') str.value = isProper(str.value);

	return result;
}

function validateTxtLen(str, maxLen, req) {
	if ((trimAll(str.value)).length == 0) {
		if (req == '1')
			return false;

		str.value = '';
		return true;
	}

	if (str.value.length > maxLen)
		return false;

	replaceSpecialChars(str);
	return true;
}

function oneIsChecked(object) {
	if (object.length) {
		for (iChk = 0; iChk < object.length; iChk++) {
			if (object[iChk].checked)
				return true;
		}
	} else {
		if (object.checked)
			return true;
	}

	return false;
}

function oneIsSelected(object) {
	if (object.selectedIndex != '-1')
		return true;

	return false;
}

function checkFileExt(str, regx, req) {
	if((trimAll(str.value)).length == 0) {
		if (req == '1')
			return false;

		str.value = '';
		return true;
	}

	if (str.value.lastIndexOf('.')) {
		lastDot = str.value.lastIndexOf('.') + 1;
		fileExt = str.value.substring(lastDot, str.value.length);
		return eval(regx).test(fileExt.toLowerCase());
	}

	return false;
}

function checkFile(str, regx, req) {
	if ((trimAll(str.value)).length == 0) {
		if (req == '1')
			return false;

		str.value = '';
		return true;
	}

	if (str.value.lastIndexOf('\\')) {
		lastSlash = str.value.lastIndexOf('\\') + 1;
		fileName = str.value.substring(lastSlash, str.value.length);
		return eval(regx).test(fileName);
	}

	return false;
}

function compareDates(date1, date2, regx) {
	var result = '0';
	if (validate(date1, 'dateRegExp', '1') && validate(date2, 'dateRegExp', '1')) {
		var delim11 = date1.value.indexOf("/");
		var delim12 = date1.value.lastIndexOf("/");
		var delim21 = date2.value.indexOf("/");
		var delim22 = date2.value.lastIndexOf("/");
		var mm1 = parseInt(date1.value.substring(0,delim11),10);
		var dd1 = parseInt(date1.value.substring(delim11 + 1,delim12),10);
		var yyyy1 = parseInt(date1.value.substring(delim12 + 1, date1.value.length),10);
		var mm2 = parseInt(date2.value.substring(0,delim21),10);
		var dd2 = parseInt(date2.value.substring(delim21 + 1,delim22),10);
		var yyyy2 = parseInt(date2.value.substring(delim22 + 1, date2.value.length),10);

		if (yyyy2 < yyyy1) {
			result = '1';
		} else if (yyyy2 > yyyy1) {
			result = '2';
		} else if (mm2 < mm1) {
			result = '1';
		} else if (mm2 > mm1) {
			result = '2';
		} else if (dd2 < dd1) {
			result = '1';
		} else if (dd2 > dd1) {
			result = '2';
		}
	} else {
		result = '3';
	}

	return eval(regx).test(result);
}

function dspError(msg, fld) {
	alert(msg);
	if (fld.type) {
		fld.focus();
		if (fld.type == 'text') {
			fld.select();
		}
	}

	return false;
}

function rplcSnglQuotes(frmName){
	if(frmName.elements && frmName.elements.length){
		for (currFF = 0; currFF < frmName.elements.length; currFF++) {
			if ((frmName.elements[currFF].type == "text" || frmName.elements[currFF].type == "textarea") && !frmName.elements[currFF].disabled && frmName.elements[currFF].style.visibility != "hidden" && frmName.elements[currFF].style.display != "none" && frmName.elements[currFF].value != "") {
				strValue = frmName.elements[currFF].value;
				strValue = strValue.replace(rmvSglQuotes, '`');
				// strValue = strValue.replace(rmvDblQuotes, '`');
				frmName.elements[currFF].value = strValue;
			}
		}
	}
}
function phoneFocus(areaFld, phoneFld){
	areaFldLen = areaFld.value.length;
	if(areaFldLen == 3){
		phoneFld.focus();
	}
}