function defaultText(element,default_text) {
	$(element).focus(function() { if ($(element).val() == default_text) $(element).val(''); });
	$(element).blur(function() { if ($(element).val() == '') $(element).val(default_text); });
}

function ajaxPost(formid, errmsg_id, success_id, success_file, errorcheck_file, post_url) {
	if (typeof post_url == 'undefined') post_url = '/inc/ajax/post.php';
	formdata = $('#'+formid).serialize();
	if (typeof errorcheck_file != 'undefined') formdata += "&errorcheck_file=" + errorcheck_file;
	$.ajax({
		url: post_url,
		global: false,
		type: "POST",
		dataType: "html",
		data: formdata,
		success: function(result) {
			if (result.length == 0) { // Hit ajax success file if no error message was shown
				$.ajax({
					type: 'POST',
					url: '/inc/ajax/' + success_file,
					data: formdata,
					dataType: "html",
					success: function(result) {
						$('#'+success_id).html(result);
					}
				});
			} else { // Set error message
				$('#'+errmsg_id).html(result);
			}
		}
	});
	
}

function cleanNum(num,def) {
	if (isNaN(num)) var num = new String(num); 
	if (typeof def == 'undefined') def = 0;
	num = parseFloat(num.replace(/[^0-9]/g, ''));
	if (num.length == 0 || isNaN(num)) {
		return def;
	} else {
		return num;
	}
}

function cleanDec(id) {	
	var num = $('#'+id).val();
	num = num.replace(/[^0-9^.]/g, ''); 
	$('#'+id).val(num);
}


function numberFormat(dec) {
	dec = Math.round(dec * 100) / 100;
	num = dec.toString();
	num_arr = num.split(".");
	if (typeof num_arr[1] == 'undefined' || num_arr[1].length < 2) {
		if (typeof num_arr[1] == 'undefined' || num_arr[1].length == 0) {
			num_arr[1] = '00';
		} else {
			num_arr[1] += '0';
		}
	}
	dec = parseInt(num_arr[0]) + '.' + num_arr[1];
	return dec;
}

/* C.B. Related */
function tooltips() {
	$('a.tooltip').tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - "
	});	
}

function documentsInit() {
	$('ul#documents ul.sub, ul#documents ul.files, ul#documents ul.subfiles').hide();
}

function selectDocumentFolder(dcid) {	

	$('ul#documents li a').removeClass('open'); // Close all

	if ($('ul#fchild'+dcid).is(':hidden')) {
		childhidden = true;
	} else {
		childhidden = false;
	}

	documentsInit(); // Close all other folders

	if (childhidden) {
		$('ul#fchild'+dcid).show();
		$('a#link'+dcid).addClass('open');
	} else {
		$('ul#fchild'+dcid).hide();
		$('a#link'+dcid).removeClass('open');
	}

}

function getDirections(pcode) {
	//document.location = 'http://maps.google.co.uk/maps?f=d&hl=en&saddr=' + $('#pcode').val() + '&daddr=CR2%206DT&ie=UTF8&om=1';
	window.open('http://maps.google.co.uk/maps?f=d&hl=en&saddr=' + $('#pcode').val() + '&daddr=' + pcode + '&ie=UTF8&om=1');
}

function playAudio(file, ajaxurl) {
	if (typeof ajaxurl == 'undefined') ajaxurl = "/inc/ajax/audio_play.php";
	$.ajax({
		url: ajaxurl,
		global: false,
		type: "POST",
		dataType: "html",
		data: "file=" + file,
		success: function(msg){
			$('#mediaplayer').html(msg);
		}
	});	
}

function bookmark() {
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){
			$("a.jqbookmark").attr("rel","sidebar");
		} 
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;
		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else { 
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}
	});
}
