/**
 * Base validation function
 * 
 * @author Milan Machain - info@kosoj.cz
 * @since 21.3.2009
 * @version 1.0
 * @required JQuery
 */

/**
 * Item Utils
 * 
 */
var ItemUtil = 
{
	TYPE_REQUIRED: 	'required', 
	TYPE_DATE: 		'date', 
	
	/**
	 * @return boolean
	 */
	anyedit: function(e)
	{
		return true;
	}, 

	/**
	 * @return boolean
	 */
	validate: function(e)
	{
		if (e)
		{
			ItemUtil.clearError(e);
			if (e.val().length > 0)
			{
				if (e.hasClass(ItemUtil.TYPE_DATE))
				{
					return DateUtil.validate(e);
				}
			} else if (e.hasClass(ItemUtil.TYPE_REQUIRED))
			{
				ItemUtil.setError(e, ItemUtil.TYPE_REQUIRED);
				return false;
			}
			return true;
		}
		return false;
	}, 
	
	setError: function(e, type)
	{
		e.attr('error', type);
		if (!e.attr('css.border'))
		{
			e.attr('css.border', e.css('border'));
		}
		e.css('border', '1px solid red');
	}, 
	
	clearError: function(e)
	{
		e.removeAttr('error');
		e.css('border', '1px solid rgb(220,220,230)');
	}
	
} // ItemUtil



/**
 * DateTime Utils - formating, validations ...
 * 
 */
var DateUtil = 
{
	DATE_PATTERN_NO_SEP: "^(\\d{2})(\\d{2})(\\d{2,4})$",
	DATE_PATTERN: "^(\\d{1,2})" + "(\\-|\\/|\\.|\\,|\\s)" + "(\\d{1,2})" + "(\\-|\\/|\\.|\\,|\\s)" + "(\\d{2,4})$", 
	SEP: ".", 
		
	/**
	 * Validate element for date value
	 */
	validate: function(e)
	{
		if (e && e.val().length > 0)
		{
			var value = e.val();
			// numbers without separators
			var reg = new RegExp(DateUtil.DATE_PATTERN_NO_SEP); 
			var matched = reg.exec(e.val());
			if (matched != null)
			{
				value = matched[1] + DateUtil.SEP + matched[2] + DateUtil.SEP + matched[3];
			}
			// numbers with separators
			reg = new RegExp(DateUtil.DATE_PATTERN); 
			var matched = reg.exec(value);
			if (matched != null)
			{
				if (matched[5].length == 2) 
				{
					matched[5] = ((matched[5] > 49) ? '19' : '20' )  + matched[5];
				}
				var day = String(parseInt(matched[1]*1));
				var month = String(parseInt(matched[3]*1));
				var year = String(parseInt(matched[5]*1));
				value = day.lpad(2, '0') + DateUtil.SEP + month.lpad(2, '0') + DateUtil.SEP + year;			
			}
			// check value for valid date
			if (!DateUtil.isDate(value))
			{
				ItemUtil.setError(e, ItemUtil.TYPE_DATE);
				return false;
			}
			// setting value, if any
			if (e.val() != value)
			{
				//alert('DateUtil.validate - value of element has changed:\n' + 'before: ' + e.val() + '\nafter: ' + value);
				e.val(value);
			}
		}
		return true;
	},

	/**
	 * @return boolean
	 */
	isDate: function(value)
	{
		reg = new RegExp(DateUtil.DATE_PATTERN); 
		var matched = reg.exec(value);
		if (matched != null)
		{
			var day = parseInt(matched[1]*1);
			var month = parseInt(matched[3]*1);
			var year = parseInt(matched[5]*1);
			// parse values
			if (String(year).length == 2) 
			{
				year = ((year > 49) ? '19' : '20' )  + year;
			}
			// comparing
			var date = new Date();
			date.setHours(0, 0, 0, 0);
			date.setFullYear(year, (month - 1), day);
			if (date.getFullYear() != year || date.getMonth() != (month-1) || date.getDate() != day)
			{
				return false;
			}
			return true;
		}
		return false;
	},
	
	getDate: function(value)
	{
		reg = new RegExp(DateUtil.DATE_PATTERN); 
		var matched = reg.exec(value);
		if (matched != null)
		{
			// parse values
			if (matched[5].length == 2) 
			{
				matched[5] = ((matched[5] > 49) ? '19' : '20' )  + matched[5];
			}
			var day = parseInt(matched[1]);
			var month = parseInt(matched[3]);
			var year = parseInt(matched[5]);
			// comparing
			var date = new Date();
			date.setHours(0, 0, 0, 0);
			date.setFullYear(year, (month-1), day);
			if (date.getFullYear() != year || date.getMonth() != (month-1) || date.getDate() != day)
			{
				return false;
			}
			return date;
		}
		return false;
	}
	
} // DateUtil


function validateForm(form, err)
{
	form.find('input[type=text]').each
	(
		function() 
		{
			if (!ItemUtil.validate($(this)))
			{
				err.push(new Array($(this).attr('name'), $(this).attr('error')));
			}
		}
	);
	return (err.length == 0);
}	
