$(document).ready(function() {
	$('form').find(':input.required').each( function (i) {
		$(this).prev('span.required').html("*");
	});
	
	$('form input.date').attr('autocomplete','off');
	w = jQuery('form input.date').width();
	$('form input.date').width(w-22);
	$('form input.date').after('<input type="button" class="date_picker" value="" />');
	$('form input.date').dynDateTime({
		firstDay: 1,
		showsTime: false,
		ifFormat: "%d/%m/%Y",
		align: "bl",
		button: ".next()"
	});
	
	$('form input.file').attr('autocomplete','off');
	w = jQuery('form input.file').width();
	$('form input.file').width(w-80);
	$('form input.file').after('<input type="button" class="browse" value="Browse" />');
	$('form input.browse').click(function(event) { return OpenFileBrowser('field3','banners') });
	
	$('form input.integer').keydown(function(event) { return checkInt(event, true) });
});

$( function() { $('form#form select').combobox(
	{
		comboboxContainerClass: "comboboxContainer", 
		comboboxValueContainerClass: "comboboxValueContainer", 
		comboboxValueContentClass: "comboboxValueContent", 
		comboboxDropDownClass: "comboboxDropDownContainer", 
		comboboxDropDownButtonClass: "comboboxDropDownButton", 
		comboboxDropDownItemClass: "comboboxItem", 
		comboboxDropDownItemHoverClass: "comboboxItemHover", 
		comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader", 
		comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer", 
		animationType: "none", 
		width: "60.9%"
	})
});

function checkEmail(email)
{
	var email_test = /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (email.length == 0 || !email_test.test(email)) return false;
	else return true;
}

function checkInt(evt, dot)
{
	evt = ( evt ) ? evt : ( ( window.event ) ? event : null );
	if (
		((evt.shiftKey || evt.altKey || evt.ctrlKey) && evt.keyCode>0) || 
		(
			(!evt.shiftKey || !evt.altKey || !evt.ctrlKey) && 
			(evt.keyCode<48 || evt.keyCode>57) && 
			(evt.keyCode<96 || evt.keyCode>105) && 
			evt.keyCode!=46 && evt.keyCode!=8 && 
			evt.keyCode!=37 && evt.keyCode!=39 && 
			(dot!=true || evt.keyCode!=110) &&
			(dot!=true || evt.keyCode!=190)
		)
	)
	{
		evt.CancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	return true;
}

function checkForm( obj, err_msg )
{
	var form = $(obj).parents('form').get(0);
	var error = false;

	$(form).find(':input.required').each( function (i) {
		if ($(this).attr('type')=='checkbox') { field = $(this).attr('checked') ? true : false; }
		else if ($(this).val()) field = $(this).hasClass('email') && !checkEmail($(this).val()) ? false : true;
			else field = false;
		if (field) {
			$(this).removeClass("warning");
			$(this).next('div.comboboxValueContainer').removeClass("warning");
		}
		else {
			error = true;
			$(this).addClass("warning");
			$(this).next('div.comboboxValueContainer').addClass("warning");
		}
	});

	$(form).find('input.password').each( function (i) {
		if ($(this).val() && $(this).val()==$(form).find('input[name="password"]').val()) {
			$(this).removeClass("warning");
		}
		else {
			error = true;
			$(this).addClass("warning");
		}
	});

	if (error) alert (err_msg);
	return error ? false : true;
}
