/*-------------------------------------------------------------------------------- This library contains generic Web functions   --- Name (string) Related Functions -----------------    atNameCommon(abbreviatedName) - returns common version of abbreviated Notes name    fixNameLastFirst(tempName) - changes "User, Joe" to "Joe User"        --- Misc Functions -----------------------------------------    atUnique(userInitials) - essentially the same as @Unique used on its own - produces a unique string of characters        --- Cookie Functions ---------------------------------------    setCookie(name, value, expires, path, domain, secure)     getCookie(name)    deleteCookie(name, path, domain)      --- Array Functions -------------------------------------------    atIsMember(origArray, testVal) - same as @IsMember    getMemberIndex(origArray, indexVal) - returns the index number of the passed indexVal. returns -1 if not there.    uniqueArray(origArray) - makes the array elements unique (no repeats) - added feb 8, 2006     removeElement(arrayObj, elementnr) - pass array object and index number, reduces the array and removes element           --- String related Functions ---------------------------------    atTrim(value) - @Trim    atLeft, atLeftBack, atMiddle, atMiddleBack, atRight, atRightBack    Right(string, num), Left(string, num)    atReplaceSubstring(origString, fromVal, toVal)     getNthString(number) - returns "st" if 1, "nd" if 2, "rd" if 3, "th" etc... (for intsance getNthString(23) returns "rd")    invalidChars(string) - returns true if any characters are invalid (based on badchars variable below)    invalidAllChars(string) - same as above but with a more robust list of characters (badcharsall below)    isValidEmail(emailAddress) - returns true or false if the format of the field is a valid e-mail address      --- Document & Object Related Functions ---------------   updateFieldValue(fieldName, newValue) - works with radio, select, checkbox and text   reloadWithQuery(queryString) - reloads the current document with a new query string (removes old)   wrapText(obj, openTag, closeTag) - wraps selected text from a field with the passed opening and closing tags   cursorPos(obj) - works with wrapText, returns the cursor position   hideAllSelect() - this function checks all of the elements in the form and hides any <select> objects   showAllHiddenSelect() - this function checks for any "hidden" <select> elements and shows them if they are hidden (works with hideAllSelect)   getElementsByClass(object,className,tag) - pass an object (like document or other node), a class name and optional Tag name to limit search   setSelectVal(selectObj, selectVal) - Sets the <select> option to the passed value. If it doesn't exist, sets the selected option = 0.   toggle(objectID) - toggles the element, passed by ID, to be visible or not (using style.display)}          --- Windows Clipboard Functions -------------------------   copyToClipboard(fieldName) - Copies the contents of a field to the clipboard - Web & Windows only   pasteFromClipboard(fieldName) - Pastes the contents of the clipboard to a field - Web & Windows only   */var badcharsall = '~{}\;/\"\\';  // invalid charactersvar badCharsDisplayAll = "invalid characters:  ~    {    }  ;  \" \\  / ";var badchars = '~{}\;';  // invalid charactersvar badCharsDisplay = "invalid characters:  ~    {    }  ; ";var badcharsdisplay = badCharsDisplay;// --- NAME (related) FUNCTIONS -----------------------------------------------//COMMON NAME (from abbreviated version ONLY)function atNameCommon(abbreviatedName) {	var finalName = "";	var nameArray = abbreviatedName.split("/");	if (nameArray[0] != "") {		finalName = nameArray[0];	}	return finalName;}//FIX Name LastFirst - Left Name, First Name switchfunction fixNameLastFirst(tempName) {	if (tempName.indexOf(",") == -1) {		return tempName;	} else {		var nameArray = tempName.split(",");		var newName = atTrim(nameArray[1]) + " " + atTrim(nameArray[0]);		return newName;	}}// --- MISC FUNCTIONS -------------------------------------------------------------//@Unique - essentially the same as the @function//Unless someone creates the number at the same second of the same day// ... with the same initials, this will be unique.function atUnique(userInitials) {	var today = new Date();	var number = "" + today.getFullYear() + today.getMonth() + today.getDay();	number = number + today.getHours() + today.getMinutes() + today.getSeconds();	number = number.toUpperCase();	var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";	var listCount = list.length;	var dec = 0;	for (var i = 0; i <=  number.length; i++) {		dec += (list.indexOf(number.charAt(i))) * (Math.pow(10 , (number.length - i - 1)));	}	number = "";	var magnitude = Math.floor((Math.log(dec))/(Math.log(listCount)));	for (var i = magnitude; i >= 0; i--) {		var amount = Math.floor(dec/Math.pow(listCount,i));		number = number + list.charAt(amount); 		dec -= amount*(Math.pow(listCount,i));	}	var finalReturn = userInitials + number.substring(0,2) + "-" + number.substring(2);	return finalReturn;}// --- COOKIE FUNCTIONS -------------------------------------------------------------//SET Cookiefunction setCookie(name, value, expires, path, domain, secure) {  var curCookie = name + "=" + escape(value) +      ((expires) ? "; expires=" + expires.toGMTString() : "") +      ((path) ? "; path=" + path : "") +      ((domain) ? "; domain=" + domain : "") +      ((secure) ? "; secure" : "");  document.cookie = curCookie;}//GET Cookiefunction getCookie(name) {  var dc = document.cookie;  var prefix = name + "=";  var begin = dc.indexOf("; " + prefix);  if (begin == -1) {    begin = dc.indexOf(prefix);    if (begin != 0) return null;  } else    begin += 2;  var end = document.cookie.indexOf(";", begin);  if (end == -1)    end = dc.length;  return unescape(dc.substring(begin + prefix.length, end));}//DELETE Cookiefunction deleteCookie(name, path, domain) {  if (getCookie(name)) {    document.cookie = name + "=" +     ((path) ? "; path=" + path : "") +    ((domain) ? "; domain=" + domain : "") +    "; expires=Thu, 01-Jan-70 00:00:01 GMT";  }}// --- ARRAY FUNCTIONS -------------------------------------------------------------// AT IS MEMBER//Mimics @IsMember - returns true/false if testVal exists in origArrayfunction atIsMember(origArray, testVal) {     for (var i = 0; i < origArray.length; i++) {     if (origArray[i] == testVal) {          return true;          }     }     return false;}//GET MEMBER INDEX// Mimics @Member but returns the zero-based index number (0 = first element)function getMemberIndex(origArray, testVal) {	var ind = -1;	for (var i=0; i < origArray.length; i++) {		if (testVal == origArray[i]) { 			ind = i;			break;		}	}	return ind;}// UNIQUE ARRAY (origArray)// pass this function an array and it returns a new array with unique valuesfunction uniqueArray(originalArray) {	finalArray = new Array();	for ( i=0 ; i < originalArray.length ; i++) {		if (!atIsMember(finalArray,originalArray[i])) {finalArray.push(originalArray[i]);}	}	return finalArray;}//REMOVE ARRAY ELEMENT//Pass the array and the specific index value to removefunction removeElement(arrayObj, elementnr) {    // elementnr is the number of the element you wish to remove    for (elementnr ;elementnr<arrayObj.length ; elementnr++) {        // assigns the value of elementnr+1 to elementnr, so you move all items by 1        arrayObj[elementnr] = arrayObj[elementnr + 1];    }    arrayObj.length=arrayObj.length-1;}// --- STRING related FUNCTIONS -------------------------------------------------------------//Mimics @RIGHTfunction atRight(fullString, subString) {   if (fullString.indexOf(subString) == -1) {      return "";   } else {      return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));   }}//  LEFT (string, number chars)function Left(str, n){	if (n <= 0)	    return "";	else if (n > String(str).length)	    return str;	else	    return String(str).substring(0,n);}//  RIGHT (string, number chars)function Right(str, n){    if (n <= 0)       return "";    else if (n > String(str).length)       return str;    else {       var iLen = String(str).length;       return String(str).substring(iLen, iLen - n);    }}//Mimics @RIGHTBACKfunction atRightBack(fullString, subString) {	if (fullString.lastIndexOf(subString) == -1) {		return "";	} else {		return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);	}}//Mimics @MIDDLEfunction atMiddle(fullString, startString, endString) {	if (fullString.indexOf(startString) == -1) {		return "";	} else {		var sub = fullString.substring(fullString.indexOf(startString)+startString.length, fullString.length);		if (sub.indexOf(endString) == -1) {			return sub;		} else {			return (sub.substring(0, sub.indexOf(endString)));		}	}}//Mimics @MIDDLEBACKfunction atMiddleBack(fullString, startString, endString) {	if (fullString.lastIndexOf(startString) == -1) {		return "";	} else {		var sub = fullString.substring(0, fullString.lastIndexOf(startString));		if (sub.indexOf(endString) == -1) {			return sub;		} else {			return (sub.substring(sub.indexOf(endString)+endString.length, sub.length));		}	}}//Mimics @LEFTfunction atLeft(fullString, subString) {	if (fullString.indexOf(subString) == -1) {		return "";	} else {		return (fullString.substring(0, fullString.indexOf(subString)));	}}//Mimics @LEFTBACKfunction atLeftBack(fullString, subString) {	if (fullString.lastIndexOf(subString) == -1) {		return "";	} else {		return fullString.substring(0, fullString.lastIndexOf(subString));	}}//Mimics @TRIM - but ONLY for a single value, not an arrayfunction atTrim(value) {	while (value.charAt(0) == " ") {		value = value.substring(1, value.length);	}	while (value.charAt(value.length-1) == " ") {		value = value.substring(0, value.length-1);	}	return value}//Mimics @REPLACESUBSTRINGfunction atReplaceSubstring(origString, fromVal, toVal) {	var newString='';	while (origString.length >= fromVal.length && origString.length > 0 && fromVal.length > 0) {		if (origString.substring(0,fromVal.length) == fromVal) {			newString += toVal;			origString = origString.substring(fromVal.length);		}else{			newString += origString.substring(0,1);			origString = origString.substring(1);		}	} 	return newString + origString;}// NTH Stringfunction getNthString(origNum) {	var numString = origNum + "";	var lastChar = numString.substr(numString.length-1, 1);	var appender = "th";	switch(lastChar) {		case "1":			appender = "st";			break;		case "2":			appender = "nd";			break;		case "3":			appender = "rd";			break;	}	return appender;}// INVALID CHARSfunction invalidChars(origString) {	var returnValue = false;	var testChar = "";	for (var i=0; i < origString.length; i++) {			testChar = origString.charAt (i);			if (badchars.indexOf(testChar) != -1) {				returnValue = true;				break;			}				}	return returnValue;}// INVALID ALL CHARSfunction invalidAllChars(origString) {	var returnValue = false;	var testChar = "";	for (var i=0; i < origString.length; i++) {			testChar = origString.charAt (i);			if (badcharsall.indexOf(testChar) != -1) {				returnValue = true;				break;			}				}	return returnValue;}//VALID EMAIL ADDRESS CHECKER//Returns true or falsefunction isValidEmail(emailAddress) {    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/    return re.test(emailAddress);}// --- DOCUMENT & OBJECT related FUNCTIONS -------------------------------//UPDATE FIELD VALUE //pass the fieldObj and the new value//if it's just a text field we simply use .value, but this works for radio, and select as well.function updateFieldValue(fieldObj, newValue) {	if ((fieldObj.type == "select") || (fieldObj.type == "select-one")) {		for (var s=0; s < fieldObj.options.length ; s++) {			var testVal =fieldObj.options[s].value;			if (testVal == "") { testVal = fieldObj.options[s].text; }			if (testVal == newValue) {				fieldObj.options[s].selected = true;				break;			}		}	} else if (fieldObj.type == "radio") {			for (var r=0; r < fieldObj.length; r++) {				if (fieldObj.value == newValue) {					fieldObj.checked = true;				} else {					fieldObj.checked = false;				}			}	} else {		fieldObj.value = newValue;		}}//RELOAD WITH QUERY STRING//Reload the current page by removing any existing query strings and appending//the passed value as the new query string.function reloadWithQuery(queryString) {	var newLocation = window.location.href;	if (window.location.search != "") {		var sindex = newLocation.indexOf("?");		newLocation = newLocation.substr(0, sindex);	}	newLocation = newLocation + "?" + queryString;	window.location = newLocation;}// INSERT TEXT // Inserts the passed text (or html) into the object at the cursor position.function insertText(obj, newText) {	 if (document.selection && document.selection.createRange) {		obj.focus(); //or else text is added to the activating control		var range = document.selection.createRange();		range.text = newText;	}}// WRAP TEXT // Wraps the selected text with the passed open and close tagsfunction wrapText(obj, openTag, closeTag) {	openTag = "<" + openTag + ">";	closeTag = "</" + closeTag + ">";	if (obj.setSelectionRange) {		// W3C/Mozilla		if (obj.selectionStart != obj.value.length) {			obj.value = obj.value.substring(0,obj.selectionStart) + openTag + obj.value.substring(obj.selectionStart,obj.selectionEnd) + closeTag + obj.value.substring(obj.selectionEnd,obj.value.length);		}	} else if (document.selection && document.selection.createRange) {		// IE code goes here		obj.focus(); //or else text is added to the activating control		var range = document.selection.createRange();		if(range.text != "") {			range.text = openTag + range.text + closeTag;		}	}}// CURSOR POSITION // used in conjunction with wrapText(). Locates the current position of the cursor// within a given input field (obj)// Note: removed all but the IE specifi code (oct. 2005 - c.defrisco)function cursorPos(obj) {		obj.focus(); //or else text is added to the activating control		var range = document.selection.createRange();		if(range.text != "") {			var objText = obj.value;			return objText.indexOf(range.text);		} else {			return 0;		}} // HIDE ALL <SELECT>// Hide ALL of the <select> objects within the current documentfunction hideAllSelect() {	if (document.all) {		for(var i=0; i<document.all.length;i++) {    			currObj =  document.all[i];    			if ((currObj.type == "select") || (currObj.type == "select-one")) {	currObj.style.visibility = "hidden"; }    		}	}}// SHOW ALL HIDDEN <SELECT>// Locates any <select> objects in the current document and un-hides it (if it is currently hidden)function showAllHiddenSelect() {	if (document.all) {		for(var i=0; i<document.all.length;i++) {    			currObj =  document.all[i];    			if ((currObj.type == "select") || (currObj.type == "select-one")) {						if (currObj.style.visibility == "hidden") {currObj.style.visibility = "visible";}			}    		}	}}// GET ELEMENTS BY CLASS (node, searchClass, tag)// pass this function an object (node), the name of a class and a tag type// it returns an array of the actual element objectsfunction getElementsByClass(node,searchClass,tag) { var classElements = new Array(); var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (i = 0, j = 0; i < elsLen; i++) {    if ( pattern.test(els[i].className) ) {      classElements[j] = els[i];      j++;    }  }  return classElements;}// SET SELECT VALfunction setSelectVal(selectObj, selectVal) {	for (i =0; i < selectObj.options.length; i++) {		if (selectObj.options[i].value == selectVal) { 			selectObj.options[i].selected = true;			return true;		}		if (selectObj.value == "") {			if (selectObj.options[i].text == selectVal) {				selectObj.options[i].selected = true;				return true;			}		}	}	selectObj.options[0].selected =true;	return true;}// TOGGLE Display of Objectfunction toggle(objectID) {	var el = document.getElementById(objectID);	if ( el.style.display != 'none' ) {		el.style.display = 'none';	} else {		el.style.display = '';	}}// GET Object (getObj) - any DOM elementfunction getObj(name) {  if (document.getElementById) {  	this.obj = document.getElementById(name);	this.style = document.getElementById(name).style;  } else if (document.all) {	this.obj = document.all[name];	this.style = document.all[name].style;  } else if (document.layers) {   	this.obj = document.layers[name];   	this.style = document.layers[name];  }}// Find Xfunction findPosX(obj) {	var curleft = 0;	if (obj.offsetParent)	{		while (obj.offsetParent) {			curleft += obj.offsetLeft			obj = obj.offsetParent;		}	} else if (obj.x) {		curleft += obj.x;	}	return curleft;}// FIND Yfunction findPosY(obj) {	var curtop = 0;	if (obj.offsetParent)	{		while (obj.offsetParent) {			curtop += obj.offsetTop			obj = obj.offsetParent;		}	} else if (obj.y) {		curtop += obj.y;	}	return curtop;}// --- WINDOWS CLIPBOARD FUNCTIONS -------------------------------------------------------------//Clipboard - COPY TO //Copies a field name value contents from a web page to the// windows clipboardfunction copyToClipboard(fieldName) {     var theObject = document.getElementById(fieldName);     if (!theObject) {     	if (document.all) {     		var theObject = eval("document.all." + fieldName);     	}     }     if (theObject) {  	   theObject.style.display = '';  	   theObject.focus();	     theObject.select();	     document.execCommand("copy"); 	    theObject.style.display = "none"; 	  }}// Clipboard - PASTE FROM // performs a window's paste command into a specific fieldfunction pasteFromClipboard(fieldName) {     var theObject = document.getElementById(fieldName);          if (!theObject) {     	if (document.all) {     		var theObject = eval("document.all." + fieldName);     	}     }     if (theObject) {     	theObject.style.display = '';   	  	theObject.focus();   	  	theObject.select();     	document.execCommand("paste");     }}