// JavaScript Document

// Abreviacion de "document.getElementById(id)" 
function GEBI(id){
	return document.getElementById(id);
}

// Muestra u Oculta la flecha del ordenamiento para SortTable campos date
function switchArrowDate(down, up, type){
	if (type == 1){
		if (GEBI(down).style.display == 'none' && GEBI(up).style.display == 'none'){
			GEBI(up).style.display = '';
		}else if (GEBI(down).style.display == 'none'){
			GEBI(down).style.display = '';
			GEBI(up).style.display = 'none';	
		}else if (GEBI(up).style.display == 'none'){
			GEBI(up).style.display = '';
			GEBI(down).style.display = 'none';
		}else{}						
	}else{
		GEBI(down).style.display = 'none';
		GEBI(up).style.display = 'none';
	}
}

// muestra un componente cuando se hace "click" en el componente invocante
function clickShowData(id){
	if (GEBI(id).style.display == "none" ){
		GEBI(id).style.display = "";
	}else{
		GEBI(id).style.display = "none";
	}
}

// muestra un componente cuando se "checked" al componente invocante
function checkedShowData(check, id){
	if (check.checked){
		GEBI(id).style.display = "";
	}
	else{
		GEBI(id).style.display = "none";
	}
}

// Mensaje de Alerta al intentar borrar algun registro.
function delete_this(type, name, url) {
	if (confirm(">>> Are you sure you want to delete" + type +  " " + name + "?\n\n <WARNING> \nThis action can't be undo!!!")) {
		window.location=url;
	} else {
		return false;
	}
}

// Redirecciona para Ordenamiento por URL
function reloadSorter(url, criteria, asc){
	window.location = "" + url + "?sortby=" + criteria + "&asc_desc=" + asc;
}

// Solo permite numero en caja de texto
function numberValidate(e) { 
	if (e.keyCode == 9 || e.keyCode == 8 || e.keyCode == 39 || e.keyCode == 37 || e.keyCode == 46 || e.keyCode == 13) return true;
	if (e.which == 9 || e.which == 8 || e.which == 39 || e.keyCode == 37 || e.which == 46 || e.which == 13) return true;	
	tecla = (document.all) ? e.keyCode : e.which; 	
	patron =/[0-9]/;  
	te = String.fromCharCode(tecla); 
	return patron.test(te); 
};

// Solo permite valor para USD en caja de texto
function dollarValidate(e) { 
	if (e.keyCode == 9 || e.keyCode == 8 || e.keyCode == 39 || e.keyCode == 37 || e.keyCode == 46 || e.keyCode == 13) return true;
	if (e.which == 9 || e.which == 8 || e.which == 39 || e.keyCode == 37 || e.which == 46 || e.which == 13) return true;	
	tecla = (document.all) ? e.keyCode : e.which; 	 
	patron =/[0-9,.]/;  
	te = String.fromCharCode(tecla); 
	return patron.test(te); 
};

// Redondea un valor a nDec decimales y retorna valor
function redondea(sVal, nDec){
	var n = parseFloat(sVal);
	var s = "0.00";
	if (!isNaN(n)){
	 n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec);
	 s = String(n);
	 s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1);
	 s = s.substr(0, s.indexOf(".") + nDec + 1);
	}
	return s;
} 

// Redondea un valor de un campo de texto a nDec decimales
function redondea_field(sVal, nDec){
	var n = parseFloat(sVal.value);
	var s = "0.00";
	if (!isNaN(n)){
	 n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec);
	 s = String(n);
	 s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1);
	 s = s.substr(0, s.indexOf(".") + nDec + 1);
	}
	sVal.value = s;
} 

// quta espacios antes y despues de una cadena
function trim(cadena){
	for(i=0; i<cadena.length; )	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

function compareDate(fini, ffin){
	// Format day for fini and ffin mm/dd/yyyy
	a = new Date(fini.value.substring(6),parseInt(fini.value.substring(0,2))-1,fini.value.substring(3,5));
	b = new Date(ffin.value.substring(6),parseInt(ffin.value.substring(0,2))-1,ffin.value.substring(3,5));
	if (a.getTime()<b.getTime()){										
		return 1;
	} 
	if (a.getTime()>b.getTime()){ 
		alert(ffin.alt + " must be after or equal than " + fini.alt)
		ffin.value = fini.value;
		return -1;
	}
	if (a.getTime()==b.getTime()){ 
		return 0;
	}
}							