function openAjaxWindow(title,url,options){
	 var width = '500';
     var height = '200';
     var modal = false;
     var maximizable = true;
     var closable = true;
     var method = 'post';
     var parameters = '';

     if(typeof(options) != 'undefined'){
             if(options.width) var width = options.width;
             if(options.height) var height = options.height;
             if(options.minimizable != null) var minimizable = options.minimizable;
             if(options.maximizable != null) var maximizable = options.maximizable;
             if(options.closable != null) var closable = options.closable;
             if(options.modal) var modal = options.modal;
             if(options.method) var method = options.method;
             if(options.parameters) var parameters = options.parameters;
     }
     contentWin = new Window({className: 'alphacube', width:width, height:height, title: title, closable:closable, maximizable: maximizable, minimizable: minimizable, showEffectOptions:{duration:0.1}, hideEffectOptions:{duration:0.1}, resizable: true});
     contentWin.setHTMLContent("<div style='text-align:center'><img src='/images/spinner.gif' /> Carregando...</div>");
     contentWin.setAjaxContent(url,{method:method,parameters:parameters,evalScripts:true});
     contentWin.showCenter(modal);
     return contentWin;
}

function openFrameWindow(title,url,options){
	 var height = '400';
     var width = '500';
     var title = '';

     if(typeof(options) != 'undefined') {
             if(options.width) var width = options.width;
             if(options.height) var height = options.height;
             if(options.title) var title = options.title;
     }

     contentWin = new Window({title:title,className: 'alphacube', destroyOnClose:true, maximizable: true, minimizable: false, resizable: true});
     contentWin.setURL(url);
     contentWin.showCenter();
	$(options.divId).dialog('open');
}

function ajaxSubmit(form,options){
	var button = null;
	var validate = false;
	if(typeof(options.button) != 'undefined') button = options.button;
	if(typeof(options.validate) != 'undefined') validate = options.validate;
	if(validate) if (!validateForm(form)) return false;
	if(button != null) old_inner = startWait(button,'processando');
	new Ajax.Request(form.action,{evalScripts:true,evalJS:true,method:form.method, parameters:form.serialize(),onComplete:function(){if(button != null) endWait(button,old_inner);}});
}

function ajaxUpdater(element,url,callback){
	$("#"+element).load(url,callback);
}

function ajaxInsert(element,url,options){
	if(!options.data) options.data= ''; 
	if(!options.callback) options.callback = function(){};
	$.ajax({
		type: "POST",
		url: url,
		data: options.data,
		success: function(msg){
			$("#"+element).append(msg);
		},
		complete: options.callback
	});
}

function validateForm(form){
	 validation= new Validation(form,{useTitles : true, stopOnFirst:true});
	 return validation.validate();
}

function startWait(element,message){
	old_inner = element.innerHTML;
	element.disabled = true;
	element.update("<div style='text-align:center'><img src='/images/spinner.gif' border='0' align='top' /> "+ message+"</div>");
	return old_inner;
}

function endWait(element,old_inner){
	element.update(old_inner);
	element.disabled = false;
	return true;
}

function match(str1, str2){
	return (str1 == str2);
}

function enter(e,nextField,selectText){
	if(typeof(event) != 'undefined'){
		e = event;
	}
	if(e.which == '13'){
		$(nextField).focus();
		if(selectText)
			$(nextField).select();
	}
	return false;
}

function change_field(field, nextfield){
	if(field.value.length == field.maxLength) $(nextfield).focus();
}

function numbersOnly(field){
	field.value = field.value.replace(/[^0-9].*/,'');
}

function floatOnly(field){
	field.value = field.value.replace(/[^0-9\.,].*/,'');
}

function loadPanel(page,target){
		new Ajax.Updater(target,page);
}
