function requestAction(options){
	
}

function submitAction(form_id, options){
	var vaction = (typeof(options) != 'undefined' && typeof(options.action) != 'undefined'? options.action : $(form_id).action);
	
    new Ajax.Request(vaction, {method: $(form_id).method, parameters: $(form_id).serialize(true),
		onSuccess: function(transport,json) {
		if(json.status){
			showConfirmationMessage(json.msg);
		}else if(json.errors) {
			showErrorMessage(json.msg, json.errors);
		}
		if(json.redirect)
			window.location.href = json.redirect;
	    }
	});
	
}

function showAction(options){
	new Ajax.Updater('light', options.url, { method: 'post',
		onComplete: function(transport) {
			displayWindow(options.width, options.height);
		}
    });
}

function displayWindow(width_px, height_px){
	var fadeSize = getPageSize();
	$('light').style.display = 'block';
	$('fade').style.display = 'block';
	
	var popupWidth  = (width_px ? width_px : 670);
	var popupHeight = (height_px ? height_px : 420);
	
	$('fade').setStyle('height: '+(fadeSize[1])+'px; '+'width:'+fadeSize[0]+'px;');	
	$('light').setStyle('margin-left: ' + (document.body.clientWidth - popupWidth)/2 + 'px; margin-top: 100px;'+
		'height: ' + popupHeight + 'px; width: ' +popupWidth + 'px;');

}

function submitAndUpdate(options){
	$('errors').addClassName('hide');

	new Ajax.Request($(options.form).action, {method: $(options.form).method, parameters: $(options.form).serialize(true), 
		onSuccess: function(transport, json) {
			if(json.sucess){
				closeAction();
				new Ajax.Updater(options.container, options.url, {method: 'get'});
			}else{
				showErrorMessage(json.message, json.errors);
			}
			if(json.redirect)
			    window.location.href = json.redirect;
	    }
	});
}

function closeAction(){
	$('light').style.display = 'block';
	$('fade').style.display = 'block';
	$('light').update();
	$('light').hide();
	$('fade').hide();
}


function showTab(element_id){
	var elements = $(element_id).up().up().childElements(); //  li -> ul
	var link_id  = '';
	
	elements.each(function(item){
		link_id = item.down(0).id;
		if(link_id == element_id){
			$(link_id).addClassName('active');
			if($(link_id +'C').hasClassName('hide')){
				$(link_id +'C').removeClassName('hide');
				$(link_id +'C').addClassName('show');
			}
		}else{
			$(link_id).removeClassName('active');
			$(link_id +'C').removeClassName('show');
			$(link_id +'C').addClassName('hide');
		}
	});
}

function getPageSize() {
    
    var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function removeObject(vurl, element_id){
	 new Ajax.Request(vurl, {method: 'get',
		onComplete: function(transport,json) {
			if(json.sucess){
				if(element_id)
					$(element_id).remove();
				showConfirmationMessage(json.message);
			}else{
				showErrorMessage(json.message, json.description);
			} 
			if(json.redirect)
			    window.location.href = json.redirect;
	    }
	});
}


function showErrorMessage(message, errs){
	var error_str = message + '<br/>';
	for(key in errs)
		error_str += key + ' ' + errs[key]+ '<br/>';
	$('message').down(0).update(error_str);
	$('message').addClassName('error');
	$('message').removeClassName('hide');
}

function showConfirmationMessage(message){
	$('message').down(0).update(message);
	$('message').addClassName('confirmation');
	$('message').removeClassName('hide');
}

function selectAll(element, form_id){
	var elements = $(form_id).getInputs('checkbox');
	var vchecked = $(element).checked;
	
	elements.each(function(item){
		item.checked = vchecked;
	});
	
}
