// fix things up first
//if (self.location.href != top.location.href) { top.location.href = self.location.href; }
if(document.all && !document.getElementById) { document.getElementById = function(id) { return document.all[id]; } }

// cookie functions
function createCookie(name, value, days) { if(days > 0) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }

// transfer tracking cookie
//window.onload = function(e) { if(!readCookie("trans") && document.URL.indexOf("lptssecure.com") == -1) { var ti = new Image(); ti.src = "http://www.lptssecure.com/data/track.php?c="+readCookie('click'); var d = new Date(); createCookie("trans", d.getTime(), 365); } }

// popups/transfers
function popWin(url, width, height) { if(url != '') { var popup_win = window.open(url,'popup','scrollbars,resizable,toolbar,width='+width+',height='+height+',left=50,top=50'); popup_win.focus(); return false; } return true; }
function popWinOption(url, width, height) { if(url != '') { var popup_win = window.open(url,'popup','resizable,width='+width+',height='+height+',left=50,top=50'); popup_win.focus(); return false; } return true; }
function go(url) { if(url != '') { self.location.href = url; return false; } return true; }

// css class functions
function addCSSClass(obj, class_name) { if(isString(obj)) obj = document.getElementById(obj); if(!hasCSSClass(obj, class_name)) { obj.className += obj.className ? ' '+class_name : class_name; } }
function removeCSSClass(obj, class_name) { if(isString(obj)) obj = document.getElementById(obj); if(hasCSSClass(obj, class_name)) { var rep = obj.className.match(' '+class_name) ? ' '+class_name : class_name; obj.className = obj.className.replace(rep, ''); } }
function hasCSSClass(obj, class_name) { if(isString(obj)) obj = document.getElementById(obj); if(!obj) return false; return new RegExp('\\b'+class_name+'\\b').test(obj.className); }

// validation wrappers
function require(obj) { if(isString(obj)) obj = document.getElementById(obj); addCSSClass(obj, "required"); moveFocus(obj); }
function unrequire(obj) { if(isString(obj)) obj = document.getElementById(obj); removeCSSClass(obj, "required"); }
function disable(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj) { addCSSClass(obj, "disabled"); removeCSSClass(obj, "required"); obj.disabled = true; } }
function enable(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj) { removeCSSClass(obj, "disabled"); obj.disabled = false; } }
function makeEnabledIf(condition, toggle_id) { if(condition) enable(toggle_id); else disable(toggle_id); }
function invisible(obj) {return addCSSClass(obj, 'invisible'); }
function visible(obj) { return removeCSSClass(obj, 'invisible'); }
function makeVisibleIf(condition, toggle_id) { if(condition) visible(toggle_id); else invisible(toggle_id); }
function moveFocus(obj) { if(isString(obj)) obj = document.getElementById(obj); if(!obj) return; if(isArray(obj) && (typeof(obj.type)=="undefined")) { obj = obj[0]; } if(obj.type=="text" || obj.type=="textarea" || obj.type=="password") { obj.select(); } obj.focus(); }

// text box functions
function clearValue(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj.value) obj.value = ''; }

// select box functions
function getSelectValue(obj) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj) && obj.selectedIndex >= 0) return obj.options[obj.selectedIndex].value; else return ''; }
function getSelectText(obj) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj) && obj.selectedIndex >= 0) return obj.options[obj.selectedIndex].text; else return ''; }
function selectHasOptions(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj != null && obj.options != null) return true; else return false; }
function selectHasOption(obj, value) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj, value)) { for(var i=(obj.options.length-1); i>=0; i--) { if(obj.options[i].value == value) return true; } } return false; }
function selectRemoveAllOptions(obj) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj)) { for(var i=(obj.options.length-1); i>=0; i--) { obj.options[i] = null; }  obj.selectedIndex = -1; } }
function selectRemoveOption(obj, value) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOption(obj, value)) { for(var i=(obj.options.length-1); i>=0; i--) { if(obj.options[i].value == value) obj.options[i] = null; }  obj.selectedIndex = -1; } }
function selectAddOption(obj, value, text, selected) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj) && !selectHasOption(obj, value)) { obj.options[obj.options.length] = new Option(text, value, false, selected); } }
function selectAddOptions(obj, values, texts) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj)) { for(var i=0; i<values.length; i++) { selectAddOption(obj, values[i], texts[i], false); } } }
function setSelectValue(obj, selected) { if(isString(obj)) obj = document.getElementById(obj); if(selectHasOptions(obj)) { for(var i=0; i<obj.options.length; i++) { if(obj.options[i].value == selected) { obj.selectedIndex = i; return true; } } } return false; }

// radio functions
function getRadioValue(radio_list) { if(isString(radio_list)) radio_list = document.getElementById(radio_list); if(!radio_list) return null; if(radio_list.length) { for(var i=0; i<radio_list.length; i++) if(radio_list[i].checked) return radio_list[i].value; } else return radio_list.value; return null; }

// date validation functions
function checkDate(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj.value && obj.value != "") { var p_date = parseDate(obj.value); if(p_date != null) { obj.value = formatDate(p_date, 'MM/dd/yyyy'); unrequire(obj); } else if(obj.value != "") { require(obj); alert("Invalid date: please use mm/dd/yyyy format."); } } else { unrequire(obj); } return true; }
function checkTime(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj.value && obj.value != "") { var p_date = parseTime(obj.value); if(p_date != null) { obj.value = formatDate(p_date, 'h:mm a'); unrequire(obj); } else if(obj.value != "") { require(obj); alert("Invalid date/time: please use 'h:m a' format."); } } else { unrequire(obj); } return true; }
function checkDateTime(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj.value && obj.value != "") { var p_date = parseDateTime(obj.value); if(p_date != null) { obj.value = formatDate(p_date, 'MM/dd/yyyy h:mm a'); unrequire(obj); } else if(obj.value != "") { require(obj); alert("Invalid date/time: please use 'mm/dd/yyyy h:m a' format."); } } else { unrequire(obj); } return true; }

// credit card validation function
function checkCCNumber(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj && obj.value && obj.value != "") { obj.value = obj.value.replace(/[^0-9]/g, ''); var i, j, prod; var sum = 0; for(i = 0; i <obj.value.length; i++) { if((i % 2) != (obj.value.length % 2)) sum += parseInt(obj.value.charAt(i)); else { prod = parseInt(obj.value.charAt(i)) * 2; sum += prod % 10; if(prod >= 10) sum += 1; } } if((sum % 10) == 0) { unrequire(obj); } else { require(obj); alert("Invalid credit card account number."); } } else { unrequire(obj); } return true; }

// phone format
function formatPhone(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj && obj.value && obj.value != "") { var regex = /[^0-9]/g; var v = obj.value.replace(regex, ''); if(v.length < 10) return true; obj.value = '('+v.substring(0, 3)+') '+v.substring(3, 6)+'-'+v.substring(6, v.length); return true; } return true; }

// DL format
function checkDL(obj, state_obj)
{
	if(isString(obj)) obj = document.getElementById(obj);
	if(isString(state_obj)) state_obj = document.getElementById(state_obj);
	var state = "";
	if(state_obj && state_obj.value && state_obj.value != "") state = state_obj.value;
	else if(state_obj && selectHasOptions(state_obj)) state = getSelectValue(state_obj);

	if(obj && obj.value && obj.value != "")
	{
		unrequire(obj);
		switch(state)
		{
			case "FL":
				{
					obj.value = obj.value.replace(/[^A-Za-z0-9]/g, '');
					if(/^[A-Za-z][0-9]{12}$/.test(obj.value)) return true;
					require(obj);
					alert("Invalid Florida driver's license. License numbers are a letter followed by 12 digits.");
					return false;
				} break;
			default: return true;
		}
	}
	return true;
}

// citation format
function checkCitationNumber(obj, county)
{
	if(isString(obj)) obj = document.getElementById(obj);
	if(obj && obj.value && obj.value != "")
	{
		unrequire(obj);
		obj.value = obj.value.replace(/[^A-Za-z0-9]/g, '');
		if(county == 'Palm Beach')
		{
			if(/^[A-Za-z0-9]{8}$/.test(obj.value)) return true;
			alert("Invalid citation number - it must be 8 characters long");
		} else {
			if(/^[A-Za-z0-9]{7}$/.test(obj.value)) return true;
			alert("Invalid citation number - it must be 7 characters long");
		}
		require(obj);
		return false;
	}
	return true;
}

// email validation
function checkEmail(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj.value && obj.value != "") { var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if(!filter.test(obj.value)) { require(obj); alert("Invalid email address"); return false; } } unrequire(obj); return true; }

// format ssn
function checkSSN(obj) { if(isString(obj)) obj = document.getElementById(obj); if(obj && obj.value && obj.value != "") { var regex = /[^0-9]/g; var v = obj.value.replace(regex, ''); if(v.length != 9) { require(obj); alert("Invalid SSN"); return false; } obj.value = v.substring(0, 3)+'-'+v.substring(3, 5)+'-'+v.substring(5, 9); unrequire(obj); return true; } return true; }

// generic functions
function isAlien(a) { return isObject(a) && typeof a.constructor != 'function'; }
function isArray(a) { return isObject(a) && a.constructor == Array; }
function isBoolean(a) { return typeof a == 'boolean'; }
function isFunction(a) { return typeof a == 'function'; }
function isNull(a) { return typeof a == 'object' && !a; }
function isNumber(a) { return typeof a == 'number' && isFinite(a); }
function isObject(a) { return (a && typeof a == 'object') || isFunction(a); }
function isString(a) { return typeof a == 'string'; }
function isUndefined(a) { return typeof a == 'undefined'; }
function isEmpty(o) { var i, v; if(isObject(o)){for(i in o) { v = o[i]; if (isUndefined(v) && isFunction(v)) return false; } } return true; }

function popitup(url) {
	newwindow=window.open(url,'name','height=250,width=150');
	if (window.focus) {newwindow.focus()}
	return false;
}