
// if we have javascript enabled, hide the url used by non-js enabled browsers
var setstatus = function(stat){
	if(!stat)
		stat = "Done";	
	window.status = stat;
	return true;
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function getElementValue(el) {
	var v = (el && $(el)) ? $F(el).replace(/^\s+/,'').replace(/\s+$/,'') : '';
	return v!='' ? v : null;
}

function lesserOrEquals(v1,v2) {
	return v1 <= v2;
}

//////////////////////////////////////////////////////////////////////////
// Form Validation
/////////////////////////////////////////////////////////////////////////

// fields = [
	// {name:field, required:true, format:int, label:nice_name},
	// ...
// ]

// el: 			array of HTMLElement/id: If not provided, the elements will be searched by mame. (optional)
// name: 		string/array: name(s) of element(s) (optional)
// required:  	true/false/string: check that the value is not empty (optional)
// format:		function: 
//				'int': check for an integer (optional)
//				'date':
//				'time':
//				'email':
//				'phone':
// 				string: regex
// label: 		string: Nice name (optional)
// formatMessage:

validateAndSubmitForm = function(form, fields) {
	var errors = [];
	// validate
	if (fields) {
		fields.each(function(o) {
			o.error = null;
			
			// elements and values
			o.elements = {};
			o.values = {};
			if (!isArray(o.name)) o.name = [o.name];
			o.name.each(function(name) {
				var els = document.getElementsByName(name);
				o.elements[name] = null;
				o.values[name] = null;					
				if (els.length > 1) {
					o.elements[name] = [];
					for(var i=0; i<els.length; i++) {
						o.elements[name].push($(els[i]));
					}
				}
				else if (els.length == 1) {
					var el = $(els[0])
					o.elements[name] = el;
					o.values[name] = getElementValue(el);	
				}
			});
			o.elements = $H(o.elements);
			o.count = (o.elements.keys().length);
			
			// default element
			var name =  (o.count >= 1) ? o.elements.keys()[0] : null;
			o.element = name ? o.elements[name] : null;
			o.value = name ? o.values[name] : null;
			o.label = o.element ? (o.element.title || o.element.name) : null;
			
			// check required
			if (!o.error && o.required) {
				o.elements.keys().each(function(name) {
					var el = o.elements[name];
					if (!o.values[name]) {
						o.error = (typeof o.required == 'string') ? o.required : ((el.title || el.name) + ' is required');
						return false;
					}
				});
			}
			// check format
			if (!o.error && o.format) 
			{
				// format: function
				if (typeof o.format==='function') {
					o.error = o.format(fields, o, errors);
				}
				// format: type
				else if (o.element && o.value) 
				{
					// int
					if (o.format==='int') {
						if (!o.value.match(/^\d+$/)) 
							o.error = o.formatMessage || (o.label  + ' must be an integer');
					}				
					// date: mm/dd/yyyy
					else if (o.format==='date') {
						var valid = false;
						if (o.value.match(/^(\d{2})\/(\d{2})\/(\d{4})$/i)) {							
							var base = new Date(o.value.replace(/^(\d{2})\/(\d{2})\/(\d{4})$/i, "$1/01/$3"));
							var date = new Date(o.value);							
							valid = ((date!="Invalid Date") && date.getMonth()==base.getMonth());
						}
						if (!valid)
							o.error = o.formatMessage || (o.label  + ' must be a valid date');
					}
					// time: hh:mm AM/PM
					else if (o.format==='time') { 
						if (!o.value.match(/^\d{1,2}:\d{2} (AM|PM)$/i) || isNaN(Date.parse("01/01/2000 " + o.value)))
							o.error = o.formatMessage || (o.label  + ' must be a valid time');
					}			
					// email
					else if (o.format==='email') {
						if (!o.value.match(/^([0-9a-zA-Z]+[-._'+&])*[_0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/i)) 
							o.error = o.formatMessage || (o.label  + ' must be a valid email');
					}
					// phone
					else if (o.format==='phone') {
						if (!o.value.match(/^\d{10}$/)) 
							o.error = o.formatMessage || (o.label  + ' must be a valid phone number');
					}
					// regex
					else if (typeof o.format==='string') {
						var re = new RegExp(o.format);
						if (!re.match(o.value))
							o.error = o.formatMessage || (o.label  + ' is not valid');
					}	
				}
			}
			// process error
			if (o.error) {
				errors.push(o.error);
			}
		});
	}
	// handle errors
	if (errors.length > 0) {
		alert(errors.join("\n\n") + "\n ");
		return false;
	}
	// submit
	$(form).submit();
	return true;
}

/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
var ddrivetip = {
	offsetxpoint: -60, //Customize x offset of tooltip
	offsetypoint: 20, //Customize y offset of tooltip
	ie: (document.all),
	ns6: (document.getElementById && !document.all),
	enabletip: false,
	tipobj: "dhtmltooltip",
	ietruebody: function() {
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
	},
	show: function(thetext, thecolor, thewidth){
		if ($(this.tipobj)){
			if (typeof thewidth!="undefined") $(this.tipobj).style.width=thewidth+"px";
			if (typeof thecolor!="undefined" && thecolor!="") $(this.tipobj).style.backgroundColor=thecolor;
			$(this.tipobj).innerHTML=thetext.replace(/\'/g,"'");
			this.enabletip=true;
			return false;
		}
	},
	hide: function(){
		if ($(this.tipobj)){
			this.enabletip=false;
			$(this.tipobj).style.visibility="hidden";
			$(this.tipobj).style.left="-1000px";
			$(this.tipobj).style.backgroundColor='';
			$(this.tipobj).style.width='';
		}
	},
	position: function(e){
		if (this.enabletip){
			var curX=(this.ns6)? e.pageX : event.clientX+this.ietruebody().scrollLeft;
			var curY=(this.ns6)? e.pageY : event.clientY+this.ietruebody().scrollTop;
			//Find out how close the mouse is to the corner of the window
			var rightedge=this.ie&&!window.opera? this.ietruebody().clientWidth-event.clientX-this.offsetxpoint : window.innerWidth-e.clientX-this.offsetxpoint-20;
			var bottomedge=this.ie&&!window.opera? this.ietruebody().clientHeight-event.clientY-this.offsetypoint : window.innerHeight-e.clientY-this.offsetypoint-20;
			var leftedge=(this.offsetxpoint<0)? this.offsetxpoint*(-1) : -1000;
			//if the horizontal distance isn't enough to accomodate the width of the context menu
			if (rightedge<$(this.tipobj).offsetWidth)
				//move the horizontal position of the menu to the left by it's width
				$(this.tipobj).style.left=this.ie? this.ietruebody().scrollLeft+event.clientX-$(this.tipobj).offsetWidth+"px" : window.pageXOffset+e.clientX-$(this.tipobj).offsetWidth+"px";
			else if (curX<leftedge)
				$(this.tipobj).style.left="5px";
			else
				//position the horizontal position of the menu where the mouse is positioned
				$(this.tipobj).style.left=curX+this.offsetxpoint+"px";
			//same concept with the vertical position
			if (bottomedge<$(this.tipobj).offsetHeight)
				$(this.tipobj).style.top=this.ie? this.ietruebody().scrollTop+event.clientY-$(this.tipobj).offsetHeight-this.offsetypoint+"px" : window.pageYOffset+e.clientY-$(this.tipobj).offsetHeight-this.offsetypoint+"px";
			else
				$(this.tipobj).style.top=curY+this.offsetypoint+"px";
			$(this.tipobj).style.visibility="visible";
		}
	}
}

document.onmousemove = function(e) {
	ddrivetip.position(e)
};
