function ajaxForm2(formid,onAjaxSuccess,indicatorid,beforeRequest) {
	form = $('#'+formid);
	
	form.submit(function(){
		var form = $(this);
		
		//disable all form buttons
		$('input[type=submit]',form).attr('disabled','disabled');
		
		//show indicator if specified
		if( indicatorid ) $('#'+indicatorid).show();
		
		//clear previous entries
		$('div.error-message',form).remove();
		$('.form-error',form).removeClass('form-error');
		
		if( beforeRequest && !beforeRequest()) {
			if( indicatorid ) $('#'+indicatorid).hide();
			$('input[type=submit]',form).attr('disabled',null);
			return false;
		}

		$.ajax({
			url: form.attr('action'),
			type: form.attr('method'),
			data: form.serialize(),
			dataType: 'json',
			success: function(json, textStatus, XMLHttpRequest) {
				if( json[0] && json[0] == 'r' ) {
					switch(json[1][0]) {
					case 'good':
						if( onAjaxSuccess ) onAjaxSuccess(json[1][0]);
						form[0].reset();
						break;
					case 'dberr':
						alert('Error: Database error');
						break;
					case 'validation':
						for( var name in json[1][1] ) {
							
							var t = $('*[name^="'+name+'"]',form);
							if( t.length > 0 ) {
								t.addClass('form-error');
								t.last().parent().parent().children().first().append($('<div/>').addClass('error-message').text(json[1][1][name]));
							}
							else alert('error: validation failed, but no fields match: '+name);
						}
						break;
					case 'no_data':
						alert('No data was sent');
						break;
					default:
						alert('Error: '+json);
						break;
					}
				}
				else alert('Error: '+json);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert('Error on ajax call: '+ textStatus);
			},
			complete: function() {
				//hide indicator if specified
				if( indicatorid ) $('#'+indicatorid).hide();
				
				//enable buttons
				$('input[type=submit]',form).attr('disabled',null);
			}
		});

		return false;
	});
}

/**
 * Fill a select box by quering using Ajax
 * @param select the object or id of the select box
 * @param options the key,value array of the options to fill it with
 */
function ajaxSelectOptions(selectBoxId, url) {
	selectBox = $('#'+selectBoxId);
	
	$.ajax({
		url: url,
		dataType: 'json',
		success: function(json, textStatus, XMLHttpRequest) {
			if( json[0] && json[0] == 'r' ) {
				selectBox.children().remove();
				if($.isArray(json[1])) {
					for( var value in json[1] ) {
						selectBox.append($('<option value="'+json[1][value]['id']+'"/>').text(json[1][value]['name']));
					}
				}
			}
			else alert('Error: ' + json);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Error on ajax call: ' + textStatus);
		}
	});
}

/**
* onchange function for the calendar_date_select
*/
$(function(){
	//put the cursor in the first input box, if there is one
	var firstInput = $('.firstFocus');
	if( !firstInput.length ) {
		firstInput = $('.firstFocus,textarea,input[type=text]').not('.datepicker');
	}
	firstInput.focus();
	
	//if there were validation errors, scroll to the first one
	var error = $('.error-message');
	if( error.length ) $.scrollTo(error.parent());
	
	//phone number format
	$('.phone_number').keyup(format_phonenumber).each(format_phonenumber);
	
	//date picker
	$('.datepicker').datepicker();
	
	//fade the status out after 2 seconds
	$('div.success').delay(2000).fadeOut('slow');
});

function format_phonenumber() {
	//get value
	var $this = $(this),
		val = $this.val(),
		numbers = val.replace(/[^0-9]/g, ''),
		length = numbers.length;
	
	//format the number
	if( length < 4 ) val = numbers;
	else if( length < 8 ) {
		val = numbers.substr(0, 3)
			+ '-'
			+ numbers.substr(3);
	}
	else {
		val = '('
			+ numbers.substr(0, 3)
			+ ') '
			+ numbers.substr(3, 3)
			+ '-'
			+ numbers.substr(6);
	}
	
	//
	$this.val(val);
	return true;
}

function iframe_popup(url, width, height, title) {
	$('<iframe id="externalSite" src="' + url + '" />').dialog({
        title: title ? title : 'External Site',
        autoOpen: true,
        width: width,
        height: height,
        modal: true,
        resizable: true,
        autoResize: true,
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    }).width(width).height(height);
}

function GB_showCenter(title,url) {
	iframe_popup(url,500,500,title);
}

var Popup = {
	initialize: function(options) {
		this.options = {
			url: '#',
			width: 400,
			height: 500,
			scrollbars: 'no',
			menubar: 'no',
			resize: 'yes',
			toolbar: 'yes'
		};
		$.extend(this.options, options || {});
		window.open(this.options.url, '',
			'width='+this.options.width+
			', height='+this.options.height+
			', scrollbars='+this.options.scrollbars+
			', resize='+this.options.resize+
			', menubar='+this.options.menubar+
			', toolbar='+this.options.toolbar
		);
	}
};
