
// 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;
}