//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)  // Except on the Mac - ahead of its time
		date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the 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";
	}
}

/* check form functions */

// cross-browser "this" finder
function getTargetElement(evt) {
	var elem;
	if (evt.target) {
		elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
	}
	else {
		elem = evt.srcElement;
	}
	return elem;
}

function invalidInput(field_id, btn_id, label_id, prompt_id, warning_id) {
	if (btn_id) {
		MM_swapImage(btn_id, "", "/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/Warning.gif", 1);
		changeClassName(btn_id, "warningImg");
	}
	if ((document.getElementById(label_id).className == "form2ndInput") || (document.getElementById(label_id).className == "form2ndInputWarning")) {
		changeClassName(label_id, "form2ndInputWarning");
	}
	else {
		changeClassName(label_id, "formLabelWarning");
	}
	changeClassName(field_id, "warningInput");
	if (prompt_id != null) {
		document.getElementById(prompt_id).style.display = "none";
	}
	document.getElementById(warning_id).style.display = "inline";
//	document.getElementById(field_id).focus();
}

function validInput(field_id, btn_id, label_id, prompt_id, warning_id) {
	if (btn_id) {
		MM_swapImage(btn_id, "", "/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/Required.gif", 1);
		changeClassName(btn_id, "required");
	}
	if ((document.getElementById(label_id).className == "form2ndInput") || (document.getElementById(label_id).className == "form2ndInputWarning")) {
		changeClassName(label_id, "form2ndInput");
	}
	else {
		changeClassName(label_id, "formLabel");
	}
	changeClassName(field_id, "text");
	document.getElementById(warning_id).style.display = "none";
	if (prompt_id != null) {
		document.getElementById(prompt_id).style.display = "inline";
	}
}

function checkNumeric(field_str) {
	var num_pattern = /^[0-9]+$/;
	if (field_str.match(num_pattern)) {
		return true;
	}
	else {
		return false;
	}
}

function checkSize(field_str, min, max) {
	if ((field_str.length >= min) && (field_str.length <= max)) {
		return true;
	}
	else {
		return false;
	}
}

function formatPhone(evt) {
	var key_num;
	var key_char;
	var num_check;
	var char_check;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		var elem = getTargetElement(evt);
		if (elem) {
			key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
			key_char = String.fromCharCode(key_num);
			num_check = /\d/;
			if (num_check.test(key_char)) {
				if ((elem.value.length == 3) || (elem.value.length == 7)) {
					elem.value = elem.value + "-";
				}
				if (elem.value.length < 12) {
					elem.value = elem.value + key_char;
				}
				return false;
			}
			char_check = /[\x00\x08\x0D]/;
			if (char_check.test(key_char)) {
				return true;
			}
			return false;
		}
	}
}

function formatCurrency(evt) {
	var key_num;
	var key_char;
	var num_check;
	var char_check;
	var len;
	evt = (evt) ? evt : ((window.event) ? window.event : "");	
	if (evt) {
		var elem = getTargetElement(evt);
		if (elem) {
			len = elem.value.length;
			key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
			key_char = String.fromCharCode(key_num);
			num_check = /\d/;
			if (num_check.test(key_char)) {
				tmp = elem.value.split(".");
				if (len == 0) {
					elem.value = key_char + ".00";
					setCursorPos(elem, 1, 1);
				}
				else if ((tmp[0].length == 0) && (tmp[1].length == 0)) {
					if (getCursorPos(elem) == len) {					
						elem.value =  "." + key_char;
						setCursorPos(elem, len + 1, len + 1);
					}
					if (getCursorPos(elem) == (len - 1)) {					
						elem.value = key_char + ".00";
						setCursorPos(elem, 1, 1);
					}
				}
				else if (getCursorPos(elem) < (len - 2)) {
					tmp[0] = tmp[0] + key_char + "." + tmp[1];
					elem.value = tmp[0];
					setCursorPos(elem, len - 2, len - 2);
				}
				else if (getCursorPos(elem) == (len - 2)) {
					tmp[0] = tmp[0] +  "." + key_char + tmp[1].substring(1, 2);
					elem.value = tmp[0];
					setCursorPos(elem, len - 1, len - 1);
				}
				else if (getCursorPos(elem) == (len - 1)) {
					tmp[0] = tmp[0] +  "." + tmp[1].substring(0, 1) + key_char;
					elem.value = tmp[0];
					setCursorPos(elem, len, len);
				}
				else if (tmp[1].length == 0) {
					tmp[0] = tmp[0] +  "." + key_char;
					elem.value = tmp[0];
					setCursorPos(elem, len + 1, len + 1);
				}
				else if (tmp[1].length == 1) {
					if (getCursorPos(elem) == len) {
						tmp[0] = tmp[0] +  "." + tmp[1] + key_char;
						elem.value = tmp[0];
						setCursorPos(elem, len + 1, len + 1);
					}
					if (getCursorPos(elem) == (len - 1)) {
						tmp[0] = tmp[0] +  "." + key_char + tmp[1] ;
						elem.value = tmp[0];
						setCursorPos(elem, len, len);
					}
				}
				return false;
			}
			char_check = /[\b]/;			
			if ((char_check.test(key_char)) && (getCursorPos(elem) == (elem.value.indexOf('.') + 1))) {
				setCursorPos(elem, getCursorPos(elem) - 1, getCursorPos(elem) - 1);
				return false;
			}
			char_check = /[\x00\x08\x0D]/;
			if (char_check.test(key_char)) {
				return true;
			}
			return false;
		}
	}
}

function onlyNumbers(evt) {
	var key_num;
	var key_char;
	var num_check;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
		key_char = String.fromCharCode(key_num);
		num_check = /[\d\x00\x08\x0D]/;
		return num_check.test(key_char);
	}
}

function clearError(field_id) {
	if (document.getElementById(field_id + "Error")) {
		document.getElementById(field_id + "Error").style.display = "none";
	}
	if (document.getElementById(field_id + "ErrorMsg")) {
		document.getElementById(field_id + "ErrorMsg").style.display = "none";
		document.getElementById(field_id + "ErrorMsg").innerHTML = "";
	}
	if (document.getElementById(field_id + "Req")) {
		document.getElementById(field_id + "Req").style.display = "inline";
	}
	if (document.getElementById(field_id + "Label")) {
		changeClassName(field_id + "Label", "grp");
	}
}

function errorMsg(field_id, error_num) {
	errorText = new Array("(Required)", "(Invalid Email Address Format)", "(Invalid Phone Format)", "(Invalid Zip Code)", "(Password Must Be At Least 5 Characters)", "(Password Cannot Be The Same As Username)", "(Passwords Do Not Match)", "(State&#160;Or&#160;Province&#160;Required)", "(Invalid Website Format)", "(Invalid Date Format)", "(Enter Grant Elements)");
	if (document.getElementById(field_id + "Label")) {
		changeClassName(field_id + "Label", "grp error");
	}
	if (document.getElementById(field_id + "Req")) {
		document.getElementById(field_id + "Req").style.display = "none";
	}
	if (document.getElementById(field_id + "Error")) {
		document.getElementById(field_id + "Error").style.display = "inline";
	}
	if (document.getElementById(field_id + "ErrorMsg")) {
		document.getElementById(field_id + "ErrorMsg").innerHTML = errorText[error_num];
		document.getElementById(field_id + "ErrorMsg").style.display = "inline";
	}
}

function checkPassword(passwd, vpasswd, username) {
	if (document.getElementById(passwd).value.length < 5) {
		errorMsg(passwd, 4);
		return false;
	}
	if (document.getElementById(passwd).value == document.getElementById(username).value) {
		errorMsg(passwd, 5);
		return false;
	}
	if (document.getElementById(passwd).value != document.getElementById(vpasswd).value) {
		errorMsg(passwd, 6);
		return false;
	}
	return true;
}

function checkZipcodePlus4(field) {
	if (field_str != "") {
		var zip_pattern = /^[0-9]{4}/;
		if (field.match(zip_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkZipcode(field) {
	if (field != "") {
		var zip_pattern = /^[0-9]{5}/;
		if (field.match(zip_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkPhone(field) {
	if (field != "") {
		var phone_pattern = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
		if (field.match(phone_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkEmail(field) {
	if (field != "") {
		// check if the address fits the user@domain format. It also is used to separate the username from the domain
		var email_pattern = /^(.+)@(.+)$/;
		// pattern for matching all special characters...don't allow special characters in the address...includes ( ) < > @ , ;  : \ " . [ ]
		var special_chars = "\\(\\)<>@,; :\\\\\\\"\\.\\[\\]";
		// range of characters allowed in a  username or domainname....states which chars aren't allowed
		var valid_chars = "\[^\\s" + special_chars + "\]";
		// pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't;  anything goes)...e.g. "yak boy"@disney.com is a legal address.
		var quoted_user = "(\"[^\"]*\")";
		// pattern for domains that are IP addresses, rather than symbolic names...e.g. joe@[123.124.233.4] is a legal address...square brackets are required
		var ip_domain_pattern = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		// an atom (basically a series of non-special characters.)
		var atom = valid_chars + "+";
		// one word in the typical username...e.g. in yak.boy@somewhere.com, yak and boy are words....a word is either an atom or quoted string
		var word = "(" + atom + "|" + quoted_user + ")";
		// pattern for structure of the use
		var user_pattern = new RegExp("^" + word + "(\\." + word + ")*$");
		// pattern for structure of a normal symbolic domain, as opposed to ip_domain_pattern, shown above
		var domain_pattern = new RegExp("^" + atom + "(\\." + atom +")*$");

		// coarse pattern to break up user@domain into different pieces that are easy to analyze
		var match_array = field.match(email_pattern);
		if (match_array == null) {
		// Too many/few @'s or something;  basically, this address doesn't fit the general form of a valid address
			return false;
		}
		var user = match_array[1];
		var domain = match_array[2];
		// See if "user" is valid
		if (user.match(user_pattern) == null) {
			// user is not valid
			return false;
		}
		// if the address is at an IP make sure the IP  is valid
		var ip_array = domain.match(ip_domain_pattern);
		if (ip_array != null) {
			// an IP address
			for (var i = 1; i <= 4; i++) {
				if (ip_array[i] > 255) {
					return false;
				}
			}
			// IP ok
			return true;
		}
		// domain is symbolic name
		var domain_array = domain.match(domain_pattern);
		if (domain_array == null) {
			return false;
		}
		// break up the domain to get a count of how many atoms it consists of
		var atom_pattern = new RegExp(atom,"g");
		var dom_arr = domain.match(atom_pattern);
		var len = dom_arr.length;
		if ((dom_arr[dom_arr.length-1].length < 2) || (dom_arr[dom_arr.length-1].length > 4)) {
			// address doesn't end in a 2 - 4 letter top level domain
			return false;
		}
		// make sure there's a host name preceding the domain
		if (len < 2) {
			return false;
		}
		// all's well
		return true;
	}
	else {
		return false;
	}
}

function checkURL(field_id, field_val) {
	if (field_val != "") {
		field_val = field_val.toLowerCase();
		var url_pattern = /^((ftp|https?):\/\/)?([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+(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|ye|yt|yu|za|zm|zw)((\/)?([a-z0-9]([-a-z0-9]*[a-z0-9])?\.))?/;
		if (field_val.match(url_pattern)) {
			var scheme_pattern = /^ftp|https?:\/\//;
			if (!field_val.match(scheme_pattern)) {
				document.getElementById(field_id).value = "http:\/\/" + field_val;
			}
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkDate(field) {
	if (field != "") {
		var url_pattern = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)([0-9]){4}$/;
		if (field.match(url_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function saveGroup(thisform, field) {
	var aElem = new Array();
	var type_str;
	var group_check = true;
	var hidden_field = field.substring(0, field.length - 1);
//	alert(field);
//	alert(hidden_field);
	aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		type_str = String(aElem[i].type).toLowerCase();
		if ((type_str == "radio")  || (type_str == "checkbox")) {
			if (aElem[i].name == field) {
				group_check = false;
				group = aElem[aElem[i].name];
				for (j = 0; j < group.length; j++) {
					if (group[j].checked) {
//						alert(aElem[hidden_field].value = group[j].value);
						return true;
					}
				}
			}
		}
	}
}

function saveForm(thisform, req, err, prc) {
	var aFields= new Array();
	var aSubs= new Array();
	var fields_str;
	var field_str;
	var field;
	var field_id;
	var field_type;
	var i, j, c;
	var check = true;
	if ((!document.getElementById) || (!document.createTextNode)) {
		return null;
	}
	if (document.getElementById(req)) {
		aFields = document.getElementById(req).value.split(", ");
		fields_str = aFields;
		fields_str = fields_str.toString();
		for (i = 0; i < aFields.length; i++) {
			field_str = aFields[i].toString();
			if (field_str.indexOf("_0") > -1) {
				check = saveGroup(thisform, field_str);
			}
			else {
				field  = document.getElementById(aFields[i]);
				field_id = field.id;
				if (!field) {
					continue;
				}
				switch(field.type.toLowerCase()) {
					case "hidden":
						changeClassName(field_id + "Frame", "");
						if (field.value == "") {
							check = false;
							break;
						 }
						break;
					case "text":
						changeClassName(field_id, "text");
						if (field.value == "") {
							check = false;
							break;
						 }
						 else if ((field_id.toString().indexOf("email") > -1) && (!checkEmail(field.value))) {
							check = false;
							break;
						}
						else if ((field_id.toString().indexOf("phone") > -1) && (!checkPhone(field.value))) {
							check = false;
							break;
						}
						else if ((field_id.toString().indexOf("zip") > -1) && (!checkZipcode(field.value))) {
							check = false;
							break;
						}
						break;
					case "password":
						if (field.value == "") {
							check = false;
						}
						else if (!checkPassword("Password", "VerifyPassword", "LoginName")) {
							check = false;
							break;
						}
						break;
					case "textarea":
						changeClassName(field_id, "textarea");
						if (field.value == "") {
							check = false;
						}
						break;
					case "checkbox":
						if (!field.checked) {
							check = false;
						}
						break;
					case "select-one":
						if (document.getElementById(field_id)[document.getElementById(field_id).selectedIndex].value == "") {
							check = false;
						}
						break;
				}
			}
		}
	}
	if (document.getElementById("format_fields")) {
		aFields = document.getElementById("format_fields").value.split(", ");
		fields_str = aFields;
		fields_str = fields_str.toString();
		for(i = 0; i < aFields.length; i++) {
			field  = document.getElementById(aFields[i++]);
			field_type  = aFields[i];
			field_id = field.id;
			if ((!field) || (field.value == "")) {
				continue;
			}
			switch(field_type) {
				case "[email]":
					if (!checkEmail(field.value)) {
						check = false;
					}
					break;
				case "[phone]":
					if (!checkPhone(field.value)) {
						check = false;
					}
					break;
				case "[zipcode]":
					if (!checkZipcode(field.value)) {
						check = false;
					}
					break;
				case "[url]":
					if (!checkURL(field_id, field.value)) {
						check = false;
					}
					break;
				case "[date]":
					if (!checkDate(field.value)) {
						check = false;
						errorMsg(field_id, 9);
					}
					break;
				default:
					continue;
			}
		}
	}
	if (document.getElementById("tbls")) {
		aT = document.getElementById("tbls").value.split("; ");
		for (var i = 0; i < aT.length; i++) {
			var str = aT[i];
			str = str.toString();	
			aFields = str.split(", ");
			id_ = aFields[0].toString();
			check = true;
			for (var j = 1; j < aFields.length; j++) {
				if (document.getElementById(aFields[j]).value == "") {
					check = false;
				}
			}
		}
	}
	if (document.getElementById("subs")) {
		aSubs = document.getElementById("subs").value.split("; ");
		for (i = 0; i < aSubs.length; i++) {
			var subs_str = aSubs[i];
			subs_str = subs_str.toString();	
			aFields = subs_str.split(", ");
			c = aFields[0].toString();
			val = aFields[1].toString();
			c = document.getElementById(c);
			if (c.type.toLowerCase() == "radio") {
				c = c.checked ? true : false;
			}
			else {
				c = (c.value == val) ? true : false;
			}
			if (c)  {
				for (j = 1; j < aFields.length; j++) {
					field  = document.getElementById(aFields[j]);
					field_id = field.id;
					if (!field) {
						continue;
					}
					switch(field.type.toLowerCase()) {
						case "text":
							changeClassName(field_id, "text");
							if (field.value == "") {
								check = false;
							}
							break;
						case "textarea":
							changeClassName(field_id, "textarea");
							if (field.value == "") {
								check = false;
							}
							break;
						case "checkbox":
							if (!field.checked) {
								check = false;
							}
							break;
						case "select-one":
							if (document.getElementById(field_id)[document.getElementById(field_id).selectedIndex].value == "") {
								check = false;
							}
							break;
						default:
							continue;
					}
				}
			}
		}
	}
	if (!check) {
		return false;
	}
	else {		
		return true;
	}
}

function checkGroup(thisform, field) {
	var aElem = new Array();
	var type_str;
	var group_check = true;
	aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		type_str = String(aElem[i].type).toLowerCase();
		if ((type_str == "radio")  || (type_str == "checkbox")) {
			if (aElem[i].name == field) {
				group_check = false;
				group = aElem[aElem[i].name];
				for (j = 0; j < group.length; j++) {
					if (group[j].checked) {
						return true;
					}
				}
				if (!group_check) {
					errorMsg(field, 0);
					return false;
				}
			}
		}
	}
}

function checkForm(thisform, req, err, prc) {
	var aFields= new Array();
	var aSubs= new Array();
	var fields_str;
	var field_str;
	var field;
	var field_id;
	var field_type;
	var i, j, c;
	var check = true;
	if ((!document.getElementById) || (!document.createTextNode)) {
		return null;
	}
	if (document.getElementById(req)) {
		aFields = document.getElementById(req).value.split(", ");
		fields_str = aFields;
		fields_str = fields_str.toString();
		for (i = 0; i < aFields.length; i++) {
			field_str = aFields[i].toString();
			if (field_str.indexOf("_0") > -1) {
				check = checkGroup(thisform, field_str.substring(0, field_str.length - 2));
			}
			else {
				field  = document.getElementById(aFields[i]);
				field_id = field.id;
				if (!field) {
					continue;
				}
				clearError(field_id);
				switch(field.type.toLowerCase()) {
					case "hidden":
						changeClassName(field_id + "Frame", "");
						if (field.value == "") {
							check = false;
							errorMsg(field_id, 10);
							changeClassName(field_id + "Frame", "error");
							break;
						 }
						break;
					case "text":
						changeClassName(field_id, "text");
						if (field.value == "") {
							check = false;
							errorMsg(field_id, 0);
							changeClassName(field_id, "text_error");
							break;
						 }
						 else if ((field_id.toString().indexOf("email") > -1) && (!checkEmail(field.value))) {
							check = false;
							errorMsg(field_id, 1);
							changeClassName(field_id, "text_error");
							break;
						}
						else if ((field_id.toString().indexOf("phone") > -1) && (!checkPhone(field.value))) {
							check = false;
							errorMsg(field_id, 2);
							changeClassName(field_id, "text_error");
							break;
						}
						else if ((field_id.toString().indexOf("zip") > -1) && (!checkZipcode(field.value))) {
							check = false;
							errorMsg(field_id, 3);
							changeClassName(field_id, "text_error");
							break;
						}
						break;
					case "password":
						if (field.value == "") {
							check = false;
							errorMsg(field_id, 0);
							changeClassName(field_id, "text_error");
						}
						else if (!checkPassword("Password", "VerifyPassword", "LoginName")) {
							check = false;
							changeClassName(field_id, "text_error");
							break;
						}
						break;
					case "textarea":
						changeClassName(field_id, "textarea");
						if (field.value == "") {
							check = false;
							errorMsg(field_id, 0);
							changeClassName(field_id, "textareaError");
						}
						break;
					case "checkbox":
						if (!field.checked) {
							check = false;
							errorMsg(field_id, 0);
						}
						break;
					case "select-one":
						if (document.getElementById(field_id)[document.getElementById(field_id).selectedIndex].value == "") {
							check = false;
							errorMsg(field_id, 0);
						}
						break;
				}
			}
		}
	}
	if (document.getElementById("format_fields")) {
		aFields = document.getElementById("format_fields").value.split(", ");
		fields_str = aFields;
		fields_str = fields_str.toString();
		for(i = 0; i < aFields.length; i++) {
			field  = document.getElementById(aFields[i++]);
			field_type  = aFields[i];
			field_id = field.id;
			if ((!field) || (field.value == "")) {
				continue;
			}
			clearError(field_id);
			changeClassName(field_id, "text");
			switch(field_type) {
				case "[email]":
					if (!checkEmail(field.value)) {
						check = false;
						errorMsg(field_id, 1);
						changeClassName(field_id, "text_error");
					}
					break;
				case "[phone]":
					if (!checkPhone(field.value)) {
						check = false;
						errorMsg(field_id, 2);
						changeClassName(field_id, "text_error");
					}
					break;
				case "[zipcode]":
					if (!checkZipcode(field.value)) {
						check = false;
						errorMsg(field_id, 3);
						changeClassName(field_id, "text_error");
					}
					break;
				case "[url]":
					if (!checkURL(field_id, field.value)) {
						check = false;
						errorMsg(field_id, 8);
						changeClassName(field_id, "text_error");
					}
					break;
				case "[date]":
					if (!checkDate(field.value)) {
						check = false;
						errorMsg(field_id, 9);
						changeClassName(field_id, "text_error");
					}
					break;
				default:
					continue;
			}
		}
	}
	if (document.getElementById("tbls")) {
		aT = document.getElementById("tbls").value.split("; ");
		for (var i = 0; i < aT.length; i++) {
			var str = aT[i];
			str = str.toString();	
			aFields = str.split(", ");
			id_ = aFields[0].toString();
			clearError(id_);
			check = true;
			for (var j = 1; j < aFields.length; j++) {
				if (document.getElementById(aFields[j]).value == "") {
					check = false;
					errorMsg(document.getElementById(aFields[j]).id, 0);
					changeClassName(document.getElementById(aFields[j]).id, "text_error");
				}
			}
			if (!check) {
				errorMsg(id_, 0);
				changeClassName(id_, "th_error");
			}
		}
	}
	if (document.getElementById("subs")) {
		aSubs = document.getElementById("subs").value.split("; ");
		for (i = 0; i < aSubs.length; i++) {
			var subs_str = aSubs[i];
			subs_str = subs_str.toString();	
			aFields = subs_str.split(", ");
			c = aFields[0].toString();
			val = aFields[1].toString();
			c = document.getElementById(c);
			if (c.type.toLowerCase() == "radio") {
				c = c.checked ? true : false;
			}
			else {
				c = (c.value == val) ? true : false;
			}
			if (c)  {
				for (j = 1; j < aFields.length; j++) {
					field  = document.getElementById(aFields[j]);
					if (!field) {
						continue;
					}
					field_id = field.id;
					clearError(field_id);
					switch(field.type.toLowerCase()) {
						case "text":
							changeClassName(field_id, "text");
							if (field.value == "") {
								check = false;
								errorMsg(field_id, 0);
								changeClassName(field_id, "text_error");
							}
							break;
						case "textarea":
							changeClassName(field_id, "textarea");
							if (field.value == "") {
								check = false;
								errorMsg(field_id, 0);
								changeClassName(field_id, "textarea_error");
							}
							break;
						case "checkbox":
							if (!field.checked) {
								check = false;
								errorMsg(field_id, 0);
							}
							break;
						case "select-one":
							if (document.getElementById(field_id)[document.getElementById(field_id).selectedIndex].value == "") {
								check = false;
								errorMsg(field_id, 0);
							}
							break;
						default:
							continue;
					}
				}
			}
		}
	}
	if (document.getElementById("types")) {
		aTypes = document.getElementById("types").value.split("; ");
		for (i = 0; i < aTypes.length; i++) {
			var str = aTypes[i];
			str = str.toString();	
			aFields = str.split(", ");
			c = aFields[0].toString();
			val = document.getElementById("Field3377943").value;
			c = (c == val) ? true : false;
			if (c) {
				for (j = 1; j < aFields.length; j++) {
					field  = document.getElementById(aFields[j]);
					if (!field) {
						continue;
					}
					field_id = field.id;
					clearError(field_id);
					switch(field.type.toLowerCase()) {
						case "text":
							changeClassName(field_id, "text");
							if (field.value == "") {
								check = false;
								errorMsg(field_id, 0);
								changeClassName(field_id, "text_error");
							}
							break;
						case "textarea":
							changeClassName(field_id, "textarea");
							if (field.value == "") {
								check = false;
								errorMsg(field_id, 0);
								changeClassName(field_id, "textarea_error");
							}
							break;
						case "checkbox":
							if (!field.checked) {
								check = false;
								errorMsg(field_id, 0);
							}
							break;
						case "select-one":
							if (document.getElementById(field_id)[document.getElementById(field_id).selectedIndex].value == "") {
								check = false;
								errorMsg(field_id, 0);
							}
							break;
						default:
							continue;
					}
				}
			}
		}
	}
	if (!check) {
		if (err) {
			document.getElementById(err).style.display = "inline";
		}
		if (document.getElementById("check")) {
			document.getElementById("check").src = "/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/incomplete.gif";
			document.getElementById("check").width = "146";
			document.getElementById("check").height = "22";
			document.getElementById("check").onmouseout = null;
		}
		return false;
	}
	else {		
		if (err) {
			document.getElementById(err).style.display = "none";
		}
		if (prc) {
			document.getElementById("processing").style.display = "inline";
		}
		return true;
	}
}

function checkComplete(thisform, elem, check, md) {
	document.getElementById(elem).value = (check) ? "Yes" : "";
	if (md == "save") {
		thisform.submit();
	}
	else if ((md == "check") && (check)) {
		thisform.submit();
	}
}

/* window popup functions */
var l_popup_win;

function popupWinClosePopup() {
	if (l_popup_win) {
		l_popup_win.close();
		l_popup_win = null;
	}
}

function jsTools_popup2(url, winParams) {
	l_popup_win = window.open(url, "KinteraSphere", winParams);
	l_popup_win.focus();
}
window.onfocus = popupWinClosePopup;

function popupStickyWindow(url, width, height, b_has_top_bars) {
	var bar_str = b_has_top_bars ? "yes" : "no";
	var curr_time = new Date();
	w_popup = window.open(url, "KinteraSphere" + curr_time.getHours() + curr_time.getMinutes() + curr_time.getSeconds(), "width=" + width + ",height=" + height + ", scrollbars, resizable, menubar=" + bar_str + ", toolbar=" + bar_str);
	if (window.focus) {
		w_popup.focus();
	}
	return false;
}

function popupVolatileWindow(url, width, height, b_has_top_bars) {
	popupWinClosePopup();
	l_popup_win = popupStickyWindow(url, width, height, b_has_top_bars);
	return l_popup_win;
}

function jsTools_popup_calendar2(root_path, element, name, type) {
	var esc;
	if (type == 1) { //input box in a form
		esc = escape(element.value) + "&inp=" + name;
	}
	else if (type == 100) {
		esc = escape(element.value) + "&inp=" + name + "&cs=1";
	}
	else if (type == 200) {
		esc = escape(element.start_month.value + "/" + element.start_day.value + "/" + element.start_year.value) + "&form=" + name + "&inp=" + type;
	}
	else if (type == 300) {
		esc = escape(element.end_month.value + "/" + element.end_day.value + "/" + element.end_year.value) + "&form=" + name + "&inp=" + type;
	}
	else {
		esc = escape(element.month.value + "/" + element.day.value + "/" + element.year.value) + "&form=" + name;
	}
	jsTools_popup(root_path + "/common/asp/calendar_popup.asp?date=" + esc, 240, 265);
}

function jsTools_popup_calendar3(root_path, element, name, type, winParams) {
	var esc;
	if (type == 1)	{ //input box in a form
		esc = escape(element.value) + "&inp=" + name;
	}
	else if (type == 100) {
		esc=escape(element.value) + "&inp=" + name + "&cs=1";
	}
	else if (type == 400) {
		esc = escape(element.value) + "&inp=" + type;
	}
	else {
		esc=escape(element.month.value + "/" + element.day.value + "/" + element.year.value) + "&form=" + name;
	}
	jsTools_popup2(root_path + "/common/asp/calendar_popup.asp?date=" + esc, "width=240, height=265," + winParams);
	return l_popup_win;
}

function jsTools_popup_calendar(element, name, type) {
	jsTools_popup_calendar2("../..", element, name, type);
}

function popUp(url, win, w, h, scrollbar, stat, resize, tools, menu, loc) {
	var newWindow;
	newWindow = window.open(url, win, "height = " + h + ", width = " + w + ", scrollbars = " + scrollbar + ", status = " + stat + ", resizable = " + resize + ", toolbar = " + tools + ", menubar = " + menu + ", location = " + loc);
	if (window.focus) {
		newWindow.focus();
	}
	return false;
}


/* misc utilities */
function changeSrc(obj, s ) {
	document.getElementById(obj).src = s;
}

function closeFrameWin() {
	parent.window.close();
}

function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();	
	node = (node == null) ? document : document.getElementById(node); 
	if (tag == null) {
		tag = '*';
	}
	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;
}

/* toggle functions */

function toggleDisplay(elem, disp) {
	document.getElementById(elem).style.display = (document.getElementById(elem).style.display == "none") ? disp : "none";
}

function toggleCheck(check, elem) {
	document.getElementById(elem).checked = (check.checked) ? true : false;
//	document.getElementById(elem).disabled = (check.checked) ? true : false;
}

function toggleSection(elem, stat) {
	var s, el, i;
	var a = new Array();
	var str = new String(document.getElementById(elem).className);
	str = str.replace(/ disabled/, "");
	document.getElementById(elem).className = (stat ==  "off") ? str + " disabled": str;
	s = document.getElementById(elem);
	a = s.getElementsByTagName("INPUT");
	for (i = 0; i < a.length; i++) {
		if (document.getElementById(a[i].id)) {
			el = document.getElementById(a[i].id);
		}
		if (el.type.toLowerCase() !="hidden") {
			el.disabled = (stat == "off") ? true : false;
			if (stat == "off") {
				switch(el.type.toLowerCase()) {
					case "text":
						el.value = "";
						break;
					case "checkbox":
						el.checked = false;
						break;
					case "radio":
						el.checked = false;
						break;
					default:
						break;
				}
			}
		}
	}
	a = s.getElementsByTagName("SELECT");
	for (i = 0; i < a.length; i++) {
		el = document.getElementById(a[i].id);
		el.disabled = (stat == "off") ? true : false;
		if (stat == "off") {
			el.selectedIndex = 0;
		}
	}	
	a = s.getElementsByTagName("TEXTAREA");
	for (i = 0; i < a.length; i++) {
		el = document.getElementById(a[i].id);
		el.disabled = (stat == "off") ? true : false;
		if (stat == "off") {
			el.value = "";
		}
	}	
	a = s.getElementsByTagName("IMG");
	for (i = 0; i < a.length; i++) {
		if (a[i].id) {
			el = document.getElementById(a[i].id);
			if (el) {
				if (el.className == "req") {
					el.src = (stat ==  "off") ? "http://www.ussoccerfoundation.org/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/required_disabled.gif" : "http://www.ussoccerfoundation.org/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/required.gif";
				}
			}
		}
	}
}

function toggleFieldset(check, elem) {
	var str = new String;
	var i;
	var field, label;
	var c = document.getElementById(check);
	var fs = document.getElementById(elem);
	aFields = fs.getElementsByTagName("INPUT");
	for (i = 0; i < aFields.length; i++) {
		field = aFields[i].id;
		document.getElementById(field).checked = false;
		document.getElementById(field).disabled = (c.checked) ? false : true;
		label = field + "Label";
		document.getElementById(label).className = (c.checked) ? "right" : "right disabled";
	}
}

function toggleField(check, field) {
	var str = new String(document.getElementById(field).className);
	document.getElementById(field).disabled = (check.checked) ? false : true;
	document.getElementById(field).className = (document.getElementById(field).disabled) ? str + " disabled": str.replace(/ disabled/,"");
	if (document.getElementById(field).disabled) {
		document.getElementById(field).value = "";
	}
	else {
		document.getElementById(field).focus();
	}	
}

function cancelLink() {
	return false;
}

function disableLink(link) {
	if (link.onmouseover) {
		link.oldOnMouseOver = link.onmouseover;
	}
	link.onmouseover = cancelLink;
	if (link.onmouseout) {
		link.oldOnMouseOut = link.onmouseout;
	}
	link.onmouseout = cancelLink;
	if (link.onclick) {
		link.oldOnClick = link.onclick;
	}
	link.onclick = cancelLink;
	if (link.style) {
		link.style.cursor = "default";
	}
	link.className = "disabled";
}

function enableLink(link) {
	link.onmouseover = link.oldOnMouseOver ? link.oldOnMouseOver : null;
	link.onmouseout = link.oldOnMouseOut ? link.oldOnMouseOut : null;
	link.onclick = link.oldOnClick ? link.oldOnClick : null;
	if (link.style) {
		link.style.cursor = document.all ? "hand" : "pointer";
	}
	link.className = "";
}

function toggleLink(link) {
	if (link.disabled) {
		enableLink(link);
	}
	else {
		disableLink(link);
	}
	link.disabled = !link.disabled;
}

function toggleImg(img_id, img_src_enabled, img_src_disabled) {
	src_string = new String("");
	if (document.getElementById(img_id)) {
		src_string = String(document.getElementById(img_id).src);
		 if (src_string.indexOf("Disabled.gif") > 0 ) {
			document.getElementById(img_id).src = img_src_enabled;
		}
		else {
			document.getElementById(img_id).src = img_src_disabled;
		}
	}
}

function changeClassName(btn_id, class_name) {
	document.getElementById(btn_id).className = class_name;
}

/* misc form functions */

function getCellValue (cellOrId) {
	var cell =  typeof cellOrId == 'string' ? (document.all ? document.all[cellOrId] : document.getElementById(cellOrId)) : cellOrId;
	return cell.innerHTML;
}

function completeButton (check) {
	if (document.getElementById(check).value == "Yes") {
		document.getElementById("check").src = "/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/complete.gif";
		document.getElementById("check").width = "135";
		document.getElementById("check").height = "22";
	}
	else {
		document.getElementById("check").src = "/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/check.gif";
		document.getElementById("check").width = "150";
		document.getElementById("check").height = "22";
	}
}

function toggleDivOn(div1) {
	if (document.getElementById(div1).style.display == "none") {
		document.getElementById(div1).style.display = "block";
	}
}

function toggleDivOff(div1) {
	if (document.getElementById(div1).style.display == "block") {
		document.getElementById(div1).style.display = "none";
	}
}

function characterLimit(formName, fieldName, charLimit, remainingCharField) {
	var charCount = document.getElementById(fieldName).value;
	var charsUsed = charCount.length;
	var remainingChars;
	if (charsUsed >= charLimit) {
		document.getElementById(remainingCharField).value = "0";
		// truncate characters after charLimit
		 document.getElementById(fieldName).value = charCount.substring(0, charLimit);
	}
	else	{
		// update available characters
		remainingChars = (charLimit - charsUsed);
		document.getElementById(remainingCharField).value = remainingChars;
	}
}

function readTextAreas() {	
	var el, i;
	var s = document.getElementById("form");
	var a = new Array();
	a = s.getElementsByTagName("TEXTAREA")
	for (i = 0; i < a.length; i++) {
		el = document.getElementById(a[i].id);
//		el.innerHTML = el.value.replace(/[\r]/g, "<br \/>");
	}
}

/* dates & calendar */
function positionInfo(object) {
	var p_elm = object;
	this.getElementLeft = getElementLeft;
	function getElementLeft() {
		var x = 0;
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		while (elm != null) {
			x+= elm.offsetLeft;
			elm = elm.offsetParent;
		}
		return parseInt(x);
	}

	this.getElementWidth = getElementWidth;
	function getElementWidth(){
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		return parseInt(elm.offsetWidth);
	}

	this.getElementRight = getElementRight;
	function getElementRight(){
		return getElementLeft(p_elm) + getElementWidth(p_elm);
	}

	this.getElementTop = getElementTop;
	function getElementTop() {
		var y = 0;
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		while (elm != null) {
			y+= elm.offsetTop;
			elm = elm.offsetParent;
		}
		return parseInt(y);
	}

	this.getElementHeight = getElementHeight;
	function getElementHeight(){
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		return parseInt(elm.offsetHeight);
	}

	this.getElementBottom = getElementBottom;
	function getElementBottom(){
		return getElementTop(p_elm) + getElementHeight(p_elm);
	}
}

function CalendarControl() {
	var calendarId = 'CalendarControl';
	var currentYear = 0;
	var currentMonth = 0;
	var currentDay = 0;
	var selectedYear = 0;
	var selectedMonth = 0;
	var selectedDay = 0;
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dateField = null;

	function getProperty(p_property){
		var p_elm = calendarId;
		var elm = null;

		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		if (elm != null){
			if(elm.style){
				elm = elm.style;
				if(elm[p_property]){
					return elm[p_property];
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
	}

	function setElementProperty(p_property, p_value, p_elmId){
		var p_elm = p_elmId;
		var elm = null;

		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		if((elm != null) && (elm.style != null)){
			elm = elm.style;
			elm[ p_property ] = p_value;
		}
	}

	function setProperty(p_property, p_value) {
		setElementProperty(p_property, p_value, calendarId);
	}

	function getDaysInMonth(year, month) {
		return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
	}

	function getDayOfWeek(year, month, day) {
		var date = new Date(year,month-1,day)
		return date.getDay();
	}

	this.clearDate = clearDate;
	function clearDate() {
		dateField.value = '';
		hide();
	}

	this.setDate = setDate;
	function setDate(year, month, day) {
		if (dateField) {
			if (month < 10) {month = "0" + month;}
			if (day < 10) {day = "0" + day;}

			var dateString = month+"/"+day+"/"+year;
			dateField.value = dateString;
			hide();
		}
		return null;
	}

	this.changeMonth = changeMonth;
	function changeMonth(change) {
		currentMonth += change;
		currentDay = 0;
		if(currentMonth > 12) {
			currentMonth = 1;
			currentYear++;
		} else if(currentMonth < 1) {
			currentMonth = 12;
			currentYear--;
		}

		calendar = document.getElementById(calendarId);
		calendar.innerHTML = calendarDrawTable();
	}

	this.changeYear = changeYear;
	function changeYear(change) {
		currentYear += change;
		currentDay = 0;
		calendar = document.getElementById(calendarId);
		calendar.innerHTML = calendarDrawTable();
	}

	function getCurrentYear() {
		var year = new Date().getYear();
		if(year < 1900) year += 1900;
		return year;
	}

	function getCurrentMonth() {
		return new Date().getMonth() + 1;
	}

	function getCurrentDay() {
		return new Date().getDate();
	}

	function calendarDrawTable() {
		var dayOfMonth = 1;
		var validDay = 0;
		var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
		var daysInMonth = getDaysInMonth(currentYear, currentMonth);
		var css_class = null; //CSS class for each day

		var table = "<table cellspacing='0' cellpadding='0' border='0'>";
		table = table + "<tr class='header'>";
		table = table + "  <td colspan='2' class='previous'><a href='javascript:changeCalendarControlMonth(-1);'>&lt;</a> <a href='javascript:changeCalendarControlYear(-1);'>&laquo;</a></td>";
		table = table + "  <td colspan='3' class='title'>" + months[currentMonth-1] + "<br>" + currentYear + "</td>";
		table = table + "  <td colspan='2' class='next'><a href='javascript:changeCalendarControlYear(1);'>&raquo;</a> <a href='javascript:changeCalendarControlMonth(1);'>&gt;</a></td>";
		table = table + "</tr>";
		table = table + "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>";

		for(var week=0; week < 6; week++) {
			table = table + "<tr>";
			for(var dayOfWeek=0; dayOfWeek < 7; dayOfWeek++) {
				if(week == 0 && startDayOfWeek == dayOfWeek) {
					validDay = 1;
				} else if (validDay == 1 && dayOfMonth > daysInMonth) {
					validDay = 0;
				}

				if(validDay) {
					if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
						css_class = 'current';
					} else if (dayOfWeek == 0 || dayOfWeek == 6) {
						css_class = 'weekend';
					} else {
						css_class = 'weekday';
					}

					table = table + "<td><a class='"+css_class+"' href=\"javascript:setCalendarControlDate("+currentYear+","+currentMonth+","+dayOfMonth+")\">"+dayOfMonth+"</a></td>";
					dayOfMonth++;
				} else {
					table = table + "<td class='empty'>&nbsp;</td>";
				}
			}
			table = table + "</tr>";
		}

		table = table + "<tr class='header'><th colspan='7' style='padding: 3px;'><a href='javascript:hideCalendarControl();'>Close</a></td></tr>";
		table = table + "</table>";

		return table;
	}

	this.show = show;
	function show(field) {
		can_hide = 0;

		// If the calendar is visible and associated with
		// this field do not do anything.
		if (dateField == field) {
			return null;
		} else {
			dateField = field;
		}

		if(dateField) {
			try {
				var dateString = new String(dateField.value);
				var dateParts = dateString.split("-");

				selectedMonth = parseInt(dateParts[0],10);
				selectedDay = parseInt(dateParts[1],10);
				selectedYear = parseInt(dateParts[2],10);
			} catch(e) {}
		}

		if (!(selectedYear && selectedMonth && selectedDay)) {
			selectedMonth = getCurrentMonth();
			selectedDay = getCurrentDay();
			selectedYear = getCurrentYear();
		}

		currentMonth = selectedMonth;
		currentDay = selectedDay;
		currentYear = selectedYear;

		if(document.getElementById){

			calendar = document.getElementById(calendarId);
			calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);

			setProperty('display', 'block');

			var fieldPos = new positionInfo(dateField);
			var calendarPos = new positionInfo(calendarId);

			var x = fieldPos.getElementLeft();
			var y = fieldPos.getElementBottom();

			setProperty('left', x + "px");
			setProperty('top', y + "px");

			if (document.all) {
				setElementProperty('display', 'block', 'CalendarControlIFrame');
				setElementProperty('left', x + "px", 'CalendarControlIFrame');
				setElementProperty('top', y + "px", 'CalendarControlIFrame');
				setElementProperty('width', calendarPos.getElementWidth() + "px", 'CalendarControlIFrame');
				setElementProperty('height', calendarPos.getElementHeight() + "px", 'CalendarControlIFrame');
			}
		}
	}

	this.hide = hide;
	function hide() {
		if(dateField) {
			setProperty('display', 'none');
			setElementProperty('display', 'none', 'CalendarControlIFrame');
			dateField = null;
		}
	}

	this.visible = visible;
	function visible() {
		return dateField
	}

	this.can_hide = can_hide;
	var can_hide = 0;
}

var calendarControl = new CalendarControl();

function showCalendarControl(textField) {
	// textField.onblur = hideCalendarControl;
	calendarControl.show(textField);
}

function clearCalendarControl() {
	calendarControl.clearDate();
}

function hideCalendarControl() {
	if (calendarControl.visible()) {
		calendarControl.hide();
	}
}

function setCalendarControlDate(year, month, day) {
	calendarControl.setDate(year, month, day);
}

function changeCalendarControlYear(change) {
	calendarControl.changeYear(change);
}

function changeCalendarControlMonth(change) {
	calendarControl.changeMonth(change);
}
document.write("<iframe id='CalendarControlIFrame' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>");
document.write("<div id='CalendarControl'></div>");

function fillDate(date_id) {
	today = new Date();
	date_str = new String();
	date_str = (today.getMonth() + 1) < 10 ? "0" + String(today.getMonth() + 1) : String(today.getMonth() + 1);
	date_str = today.getDate() < 10 ? date_str + "\/0" + String(today.getDate()) : date_str + "\/" + String(today.getDate());
	date_str = date_str + "\/" + String(today.getFullYear());
	document.getElementById(date_id).value = date_str;
}