$(document).ready(function() {

	$(".divButton, .linkButton, .submitButton, .inputButton").button();
	$(".divButton, .linkButton, input.submit, input.button, #inputButton").button();
	$("textarea").elastic();

	$("#button_bar").buttonset();

	$("#tabs").tabs({
		spinner: 'Retrieving data...',
		ajaxOptions: {
			error: function( xhr, status, index, anchor ) {
				$( anchor.hash ).html("Couldn't load this tab. We'll try to fix this as soon as possible.");
			}
		}
	});

	var jdef = '';
	var req = '';
	$('.j-default').each(function(){
		jdef = $(this).attr('default');
		req = $(this).attr('req');
		if ( req == "1" ) { req = ' - <span class="red bold">Required</span>'; }
		else { req = ''; }
		$(this).attr('title', jdef+req);
		if ( $(this).val() == '' || $(this).val() == jdef ) {
			$(this).addClass('grey');
			$(this).val(jdef);
		}
		else {
			$(this).removeClass('grey');
		}
	});

	$('.j-default').focus(function(){
		jdef = $(this).attr('default');
		if ( $(this).val() == jdef ) {
			$(this).removeClass('grey');
			$(this).val('');
		}
	});

	$('.j-default').blur(function(){
		jdef = $(this).attr('default');
		if ( $(this).val() == '' ) {
			$(this).addClass('grey');
			$(this).val(jdef);
		}
	});

	$('input, textarea').focus(function(){
		$(this).removeClass('required-field');
	});

	$('input, textarea').blur(function(){
		$(this).removeClass('required-field');
		var req = $(this).attr('req');
		if ( $(this).attr('req') == 1 ) {
			jdef = $(this).attr('default');
			if ( $(this).val() == '' || $(this).val() == jdef ) {
				$(this).addClass('required-field');
			}
		}
	});

});
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function Chr(AsciiNum){ return String.fromCharCode(AsciiNum) }

function aj(DIV,URL,qryString,loader){
	if (loader==1) { loading_bar('show'); }
	$.ajax({
		type: "GET",
		url: URL,
		data: qryString,
		success: function(message) {
			if (message.length > 0) {
				$("#"+ DIV).html(message);
				if (loader==1) { loading_bar('hide'); }
			}
		},
		error: function (xhr, ajaxOptions, thrownError){
			alert(xhr.status+': '+xhr.statusText+'\n'+URL);
		}
	});
}

function aj_post(f,DIV,URL){
	var qryString = $(f).serialize();
	$.ajax({
		type: "POST",
		url: URL,
		data: qryString,
		success: function(message) {
			if (message.length > 0) {
				var arr = message.split(":");
				if(arr[0]=="ERROR"){alert(arr[1]);}
				else{$("#"+ DIV).html(message);}
			}
		},
		error: function (xhr, ajaxOptions, thrownError){
			alert(xhr.status+': '+xhr.statusText+'\n'+URL);
		}
	});
}

function aj_post_raw(DIV,URL,qryString){
	$.ajax({
		type: "POST",
		url: URL,
		data: qryString,
		success: function(message) {
			if (message.length > 0) {
				var arr = message.split(":");
				if(arr[0]=="ERROR"){alert(arr[1]);}
				else{$("#"+ DIV).html(message);}
			}
		},
		error: function (xhr, ajaxOptions, thrownError){
			alert(xhr.status+': '+xhr.statusText+'\n'+URL);
		}
	});
}

function justify_labels(tag,extraPadding) {
	var max = 0;
	if (!extraPadding) { extraPadding = 0; }
	$(tag).each(function(){
		$(this).css('float', 'left')
		if ($(this).width() > max) { max = $(this).width(); }
	});
	$(tag).width(max + extraPadding);
}

function aj_form_post(f,div,url){
	var qryString = $('#'+f).serialize();
	$.ajax({
		type: "POST",
		url: url,
		data: qryString,
		success: function(message) {
			var arr = message.split(":");

			if(message=="1"){$('#dialog-form').dialog( "close" );}	// Backwards compatible...

			else if(arr[0]=="CLOSE"){$('#dialog-form').dialog( "close" );}
			else if(arr[0]=="REDIR"){window.location=arr[1];}

			else if(arr[0]=="SUCCESS"){alert(arr[1]);}
			else if(arr[0]=="SUCCESS-ALERT-RELOAD"){alert(arr[1]);$('#dialog-form').dialog("close");aj(arr[2],arr[3],arr[4]);}
			else if(arr[0]=="SUCCESS-RELOAD"){$('#dialog-form').dialog("close");aj(arr[1],arr[2],arr[3]);}
			else if(arr[0]=="SUCCESS-REDIR"){alert(arr[1]);window.location=arr[2];}
			else if(arr[0]=="SUCCESS-CLOSE"){alert(arr[1]);$('#dialog-form').dialog( "close" );}

			else if(arr[0]=="ERROR"){alert(arr[1]);}
			else if(arr[0]=="ERROR-RELOAD"){alert(arr[1]);aj('dialog-form',url,queryString);}
			else if(arr[0]=="ERROR-CLOSE"){alert(arr[1]);$('#dialog-form').dialog( "close" );}

			else {
				alert(message);
			}
			//aj('dialog-form',url,queryString);
		},
		error: function (xhr, ajaxOptions, thrownError){
			alert(xhr.status+': '+xhr.statusText+'\n'+URL);
		}
	});
}

function ajv(DIV,qryString){
	$.ajax({
		type: "POST",
		url: "/scripts/various.php",
		data: qryString,
		success: function(message) { $("#"+DIV).val(message); },
		error: function (xhr, ajaxOptions, thrownError){
			alert(xhr.status+': '+xhr.statusText+'\n'+URL);
		}
	});
}

function loading_bar(a) {
	if (a=="show") {
		$('#dialog-loading').dialog({
			dialogClass: 'ui-dialog-titlebar-hide ui-dialog-round',
			draggable: false,
			resizable: false,
			modal: true,
			closeOnEscape: false,
			height: 100,
			width: 360
		});
	} else {
		$('#dialog-loading').dialog('close');
	}
}

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
jQuery.fn.slideFadeShow = function(speed, easing, callback) {
	return this.animate({opacity: 'show', height: 'show'}, speed, easing, callback);
};
jQuery.fn.slideFadeHide = function(speed, easing, callback) {
	return this.animate({opacity: 'hide', height: 'hide'}, speed, easing, callback);
};

