/*--------------------------------------------------------------------------*/
/* Common '$' extensions                                                    */
/*--------------------------------------------------------------------------*/
function $F(element) {
	var value = $(element).get('value');
	if (typeof(value) == 'string') {value = value.trim();}
	return value;
}

function $RF(name) {
	var radios = $$('input[name='+name+']');
	for (var i=0; i < radios.length; i++) {
		if (radios[i].checked) {
			return radios[i].getProperty('value');
		}
	}	
}

function $show(element) {
	return $(element).style.display = '';
}

function $hide(element) {
	return $(element).style.display = 'none';
}

function $toggle(element) {
    element = $(element);
	if (element.style.display == 'none') {
		return $show(element);
	} else {
		return $hide(element);
	}		
}

/*----------------------------------------------------------------*/
/* Send Ajax Request with the Form data                           */
/*----------------------------------------------------------------*/
function SendRequestByAjax(process, params, callback_func) {
//	$show('loading_bar');
	var aj = new Request.JSON({
		url: 'process.php?process='+process, 
		method: 'post',
		data: params,
		onSuccess: function(o){
			callback_func(o);
//			$hide('loading_bar');
		},
		onFailure: function(o) {
//			$hide('loading_bar');
			alert('Request error'); //  o.status + o.statusText  
		} 
	}).send();
}

/*--------------------------------------------------------------------------*/
/* Send Ajax Request with the Form data                                     */
/*--------------------------------------------------------------------------*/
function SendFormByAjax(process, form_id, callback_func) {
	var  aj = new Request.JSON({
		url: 'process.php?process=' + process, 
		method: 'post',
		data: $(form_id),
		onSuccess: function(o) {
   			callback_func(o);
		},
		onFailure: function(o) {
			alert('Request error'); //  o.status + o.statusText  
		} 
	}).send();
	return aj;
}

/*--------------------------------------------------------------------------*/
// MooTools modal view for the whole screen 
// ver 2.04 
/*--------------------------------------------------------------------------*/
var toggleModal = function(options) {
	if ($("modalBackground")) {
		$("modalBackground").dispose();
		return false;
	}

	var options = $merge({
		bgColour : '#777',
		zIndex: 100,
		opacity: .8,
		events: $empty()
	}, options);

	return new Element("div", {
		id: "modalBackground",
		styles: {
			position: "absolute",
			top: 0,
			left: 0,
			width: window.getScrollWidth(),
			height: window.getScrollHeight(),
			background: options.bgColour,
			"z-index": options.zIndex
		},
		opacity: options.opacity,
		events: options.events
	}).inject(document.body);
} 


/*--------------------------------------------------------------------------*/
// depenent ListBox
// ver 1.04 
/*--------------------------------------------------------------------------*/
function setDepenentlistbox(parentId, childId, valueArray) {

	var mainValue = $F('parentId');
	childObject = $('childId');

	//Clear child listbox
	for(var i=childObject.length;i>1;i--) {
		childObject.options[i] = null;
	}
	
	for (i=0; i < valueArray.length; i++) {
		if (childArray[i][1] == mainValue) {
			childObject.options[i+1] = new Option(valueArray[i][2], valueArray[i][0]);
		}
	}
	//Select first option
	childObject.selectedIndex = 0;
}

