function isBlank(s) {
		for (var i=0; i < s.length; i++) {
			c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		if (s != "") return false;
		return true;
}

function isEmail(s) {
	if (s != null) {
		var pos1 = s.indexOf('@');
		var pos2 = s.indexOf('.');
		invalid_ch = "\/#?!, ";

			for (var i=0;i<s.length;i++) {  // check for invalid chars
			if(invalid_ch.indexOf(s.charAt(i)) != -1) {
				return false;				
			}
		}
		if ((pos1 == -1) || (pos2 == -1) || (s.indexOf('@', pos1+1) != -1)) {
			return false;
		}
		return true;
	}
}

function Strip(str) {
	str = str.replace("__", "");
	str = str.replace("_NUM", "");
	return str;
}

function validate(f) {
	var msg = "";
	var errors = "";
	var emptyFields = "";
	var error1 = -1;

	for (var i=0; i<f.length; i++) {
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password") || (e.type == "select-one") || (e.type == "file")) && (e.name.indexOf("__") != -1) && (!e.disabled)) {
			if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
				emptyFields += "\n- "+ Strip(e.name);
				if (error1 == -1) error1 = i;
				continue;
			}
		}
		if ((e.name.indexOf("email") != -1) && (!isBlank(e.value))) {
			var email = e.value;
			if (!isEmail(email)) {
				errors += "- Invalid email address format.\n";
				if (error1 == -1) error1 = i;
			}
		}
		if ((e.name.indexOf("_NUM") != -1) && (!isBlank(e.value)) && (isNaN(e.value.replace(",", "")))) {
			errors += "- Non-numeric: " + Strip(e.name) + ".\n";
			if (error1 == -1) error1 = i;
		}
		if ((e.name.indexOf("date") != -1) && (!isBlank(e.value)) && (e.value != "mm/dd/yyyy")) {
			var fdate = e.value;
			if (!Date.parse(fdate)) {
				errors += "- Please enter a valid "+ Strip(e.name) +"! (mm/dd/yyyy)\n";
				if (error1 == -1) error1 = i;
			}
		}
	}
	if (emptyFields) {
		msg += "The following required field(s) is(are) missing: "+ emptyFields + "\n\n";
	}
	if (errors) {
		msg += "Please correct the followng error(s):\n"+errors;
	}
	if ((errors) || (emptyFields)) {
		alert(msg);
		if ((f.elements[error1].type == "text") || (f.elements[error1].type == "textarea")) {
			f.elements[error1].select();
			f.elements[error1].focus();
		}
		else {
			f.elements[error1].focus();
		}
		return false;
	}
	if ((!emptyFields) && (!errors)) return true;
}

//---------

var offsetxpoint = -280 //Customize x offset of tooltip
var offsetypoint = 8 //Customize y offset of tooltip
var ie = document.all
var ns6 = document.getElementById && !document.all
var enabletip=false
if (ie || ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
	if (ns6||ie){
		if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		tipobj.innerHTML=thetext
		enabletip=true
		return false
	}
}

function positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
			//move the horizontal position of the menu to the left by it's width
			tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
			tipobj.style.left="5px"
		else
			//position the horizontal position of the menu where the mouse is positioned
			tipobj.style.left=curX+offsetxpoint+"px"
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
			tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
			tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"
	}
}

function hideddrivetip(){
	if (ns6||ie){
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}

document.onmousemove=positiontip
