function allButtons() {
	$(".ui-button:not(.ui-state-disabled)")
	.hover(
		function(){
			$(this).addClass("ui-state-hover cursor-hover");
		},
		function(){
			$(this).removeClass("ui-state-hover cursor-hover");
		}
	);
}

function ajaxUpdater(url, params, updateID, type) {
	$.ajax({
		type: ((type == "post") ? "post" : "get"),
		processData: true,
		url: url,
		data: params,
		cache: false,
		success: function(data) {
			$("#" + updateID).html(data);
		}
	});
}

function checkAll(rangeID, elements, is_checked) {
	var range = document.getElementById(rangeID);
	var inputs = range.getElementsByTagName("input");
	for (i = 1; i < inputs.length; i++) {
		if (inputs[i].name == elements) {
			inputs[i].checked = is_checked;
		}
	}
}

function cleanCell(node) {
	var stripped = strip_tags(node.innerHTML);
	if (stripped.indexOf('|') != -1) {
		var a = stripped.split('|');
		return a[0];
	} else {
		return stripped;
	}
}

function cleanCellH4(node) {
	var n = node.getElementsByTagName('h4');
	if (n.length > 0) {
		return n[0].innerHTML;
	} else {
		return node.innerHTML;
	}
}

function cursor_wait() {
  document.body.style.cursor = "wait";
}

function cursor_clear() {
  document.body.style.cursor = "default";
}

function highlight(item, is_true) {
	item.className = (is_true) ? "elementError" : "element";
}

function is_email(v) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v)) {
		return true;
	} else {
		return false;
	}
}

function is_object(obj) {
	if (null == obj)
		return false;
	if ("undefined" == typeof(obj))
		return false;
	return true;
}

function rand(min, max) {
	var argc = arguments.length;
	if (argc == 0) {
		min = 0;
		max = 2147483647;
	} else if (argc == 1) {
		throw new Error("Warning: rand() expects exactly 2 parameters, 1 given");
	}
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function right(str, n) {
	if (n <= 0) {
		return "";
	} else if (n > String(str).length) {
		return str;
	} else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

/*function showDialog(titre, contents) {

	if ((contents.indexOf(".php?") > 0 || contents.indexOf(".htm") > 0) && contents.indexOf(">") <= 0) {
		$.ajax({
			type: "get",
			processData: true,
			url: contents,
			cache: false,
			success: function(data) {
				$("#dialog").html(data);
			}
		});
	} else {
		$("#dialog").html(contents);
	}
	$("#dialog").data("title.dialog", titre);

	var dia = $("#dialog").dialog({
		title: titre,
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		resizable: true,
		draggable: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	if (!dia.dialog('isOpen')) {
		dia.dialog('open');
	}
}*/

function showDialog(titre, contents, x, y) {
	if (!(x > 0))
		x = 320;
	if (!(y > 0))
		y = "auto";

	//if (right(contents, 4) == ".php" || right(contents, 4) == ".htm") {
	if ((contents.indexOf(".php?") > 0 || contents.indexOf(".htm") > 0) && contents.indexOf(">") <= 0) {
		$.ajax({
			type: "get",
			processData: true,
			url: contents,
			cache: false,
			success: function(data) {
				$("#dialog").html(data);
			}
		});
	} else {
		$("#dialog").html(contents);
	}
	$("#dialog").data("title.dialog", titre);
	$("#dialog").data("width.dialog", x).data("height.dialog", y);

	var dia = $("#dialog").dialog({
		title: titre,
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		width: x,
		height: y,
		resizable: true,
		draggable: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	if (!dia.dialog('isOpen')) {
		dia.dialog('open');
	}
}

function strip_tags(str, allowed_tags) {
	var key = '', allowed = false;
	var matches = [];
	var allowed_array = [];
	var allowed_tag = '';
	var i = 0;
	var k = '';
	var html = '';
	var replacer = function(search, replace, str) {
		return str.split(search).join(replace);
	};
	// Build allowes tags associative array
	if (allowed_tags) {
		allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
	}
	str += '';
	// Match tags
	matches = str.match(/(<\/?[\S][^>]*>)/gi);
	// Go through all HTML tags
	for (key in matches) {
		if (isNaN(key)) {
			// IE7 Hack
			continue;
		}
		// Save HTML tag
		html = matches[key].toString();
		// Is tag not in allowed list? Remove from str!
		allowed = false;
		// Go through all allowed tags
		for (k in allowed_array) {
			// Init
			allowed_tag = allowed_array[k];
			i = -1;
			if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
			if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
			if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
			// Determine
			if (i == 0) {
				allowed = true;
				break;
			}
		}
		if (!allowed) {
			str = replacer(html, "", str); // Custom replace. No regexing
		}
	}
	return str;
}

function viewMap(address, city, state, zip) {
	var height = screen.height - 200;
	var width = (screen.width * 0.75);

	var popup = window.open("http://maps.google.com/maps?f=q&hl=en&geocode=&q=" + address + '+' + city + ',+' + state + '+' + zip + '&ie=UTF8&z=16&iwloc=addr', "popup", "height=" + height + ",left=50,resizable=1,scrollbars=1,status=0,top=50,width=" + width);

	//var popup = window.open("http://maps.yahoo.com/py/maps.py?BFCat=&Pyt=Tmap&newFL=Use+Address+Below&addr=" + address + "&csz=" + zip + "&Country=us&Get%A0Map=Get+Map", "popup", "height=" + height + ",left=50,resizable=1,scrollbars=1,status=0,top=50,width=" + width);
	popup.focus();
}
