function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
     if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  	var win = window.open(theURL,winName,features);
	win.focus();
}

function format(expr, decplaces,no_comma) {
    // convert in case it arrives as a string value
    expr = parseFloat(expr);
    // make sure it passes conversion
    if (!isNaN(expr)) {
		// raise incoming value by power of 10 times the
		// number of decimal places; round to an integer; convert to string
		var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
		// pad small value strings with zeros to the left of rounded number
		while (str.length <= decplaces) {
			str = "0" + str
		}
		// establish location of decimal point
		var decpoint = str.length - decplaces
		// assemble final result from: (a) the string up to the position of
		// the decimal point; (b) the decimal point; and (c) the balance
		// of the string. Return finished product.
		if((decpoint > 3) && (!no_comma))
			result = str.substring(0,decpoint-3) + "," + str.substring(decpoint-3,decpoint)
		else
			result = str.substring(0,decpoint)

		if(decpoint < str.length) result = result + "." + str.substring(decpoint,str.length);
	 	return result;
	}
	else {
		 return "NaN";
	}
}


function getObject(obj) {

	var theObj
	if (document.layers) {
		if (typeof obj == 'string') {
			// just one layer deep
			return document.layers[obj]
		} else {
			// can be a nested layer
			return obj
		}
	}
	if (document.all) {
		if (typeof obj == 'string') {
			return document.all(obj)
		} else {
			return obj
		}
	}
	if (document.getElementById) {
		if (typeof obj == 'string') {
			return document.getElementById(obj)
		} else {
			return obj
		}
	}
	return null
}
function setPointer(id, thePointerColor){

	if (thePointerColor == '') return false;
	var theRow       = $('row_' + id);
	var theCells     = theRow.getElementsByTagName('td');

	$A(theCells).each(function(cell) {
		var type = cell.className.split(' ').detect(function(value) {return (value == 'fix');});
		if(type != 'fix') cell.style.backgroundColor = thePointerColor;
	});
	return true;
}
function row_focus(id, color) {
	if(!color) color = COL_form_row_hilite;
	setPointer(id, color);
}
function row_blur(id,color) {
	if(!color) color = COL_form_data_background;
	setPointer(id, color);
}

function cell_hl(e, mode, color) {
	if(color) {
		e.style.backgroundColor = color;
	}
	else {
		e.style.backgroundColor = (mode)? '#F7EAD7' : '#F5F8FB';
	}
}

function open_help() {
	new Effect.BlindUp($('help_button'), {duration: 0.1});
	new Effect.BlindDown($('help_box'), {duration: 0.1});
}
function close_help() {
	new Effect.BlindUp($('help_box'), {duration: 0.1});
	new Effect.BlindDown($('help_button'), {duration: 0.1});
}

var colorInputError="#fff6bf";
var COL_form_row_hilite="F7EAD7";
var COL_form_data_background="F5F8FB";

/**
 *
 * @access public
 * @return void
 **/

var ajax_spinner_status;

function set_ajax_spinnert(status){
	switch(status) {
		case 'active':
			$('ajax_spinner_on').style.display='block';
			$('ajax_spinner_off').style.display='none';
			$('ajax_spinner_err').style.display='none';
			break;

		case 'inactive':
			if(ajax_spinner_status == 'error') break;
			$('ajax_spinner_on').style.display='none';
			$('ajax_spinner_off').style.display='block';
			$('ajax_spinner_err').style.display='none';
			break;

		case 'error':
			$('ajax_spinner_on').style.display='none';
			$('ajax_spinner_off').style.display='none';
			$('ajax_spinner_err').style.display='block';
			break;
	}
	ajax_spinner_status = status;
};

Ajax.Responders.register({
	onCreate: function() {
		set_ajax_spinnert('active');
		Ajax.activeRequestCount++;
	},
	onComplete: function(request, transport) {
 		Ajax.activeRequestCount--;
 		if(Ajax.activeRequestCount == 0)	set_ajax_spinnert('inactive');
		if(transport.status != '200') {
			//alert('HTTPcode:' + transport.status + ':' + transport.responseText);
			set_ajax_spinnert('error');
		}
	},
	onFailure: function(request, response) {
		//alert('Failure:' + transport.status + ':' + transport.responseText);
		set_ajax_spinnert('error');
	},
	onException: function(request) {
		//alert('Execption:' + transport.status + ':' + transport.responseText);
		set_ajax_spinnert('error');
	}
});

function ServerError(errno, message) {
	var err_msg = (message)? message : null;
	switch(errno) {
		case 101:
			err_msg = 'La session caduc&oacute;. Favor de firmarse de nuevo. ';
			alert(err_msg);
			document.location.href='/';
	}
}

/**
 *
 * @access public
 * @return void
 **/
function iva_add(value, iva, tipo) {

	var result;

	switch(tipo) {
		case 'P':
			result = (value * (1+iva));
			break;

		case 'M':
			if(iva != 0.15) {
				var v15 = Math.max(Math.ceil(value * 0.115) * 10 - 1, 0);
				result = Math.max(Math.ceil(( v15 / 1.15 * (1+iva) ) / 10) * 10 - 1, 0);
			}
			else {
				result = Math.max(Math.ceil(value * (1+iva) / 10) * 10 - 1, 0);
			}
			break;

		case 'C':
			result = Math.max(Math.round(value * (1+iva) / 10) * 10 - 1, 0);
			break;
	}
	return result;
}

//Event.observe(window, 'load', setLog, false);
//var logConsole;
//function setLog(){
//	logConsole = new LogConsole();
//	new Draggable('logconsole',{revert:false,handle:logtitle});
//	logConsole.enable(true); //false to disable;
//	//logConsole.logMsg("message"); //log message;
//	//logConsole.logError("error message"); //log error message;
//}
