// JavaScript Document

//ONLOAD
$(document).ready(function()
{	
	$('input').focus(function()
	{
		$(this).addClass("inputFocus");
	});  
	
	$('input').blur(function()
	{
		$(this).removeClass("inputFocus");
	});
});
//FIN ONLOAD

function mensaje( text )
{
	var ancho = ( text.length ) * 8;
	var medio = redondear( (( ancho / 2 ) * -1), 0);
	
	$("#mensaje").css({'width':ancho + 'px', 'left': medio + 'px'}).html( text ).fadeIn();
}

function ocultarMensaje()
{
	$("#mensaje").fadeOut();
}

function formatoRut( rut )
{
	var rut_ok = "";
	
	while( rut.indexOf(".") != -1 )
	{
		rut = rut.replace(".","");
	}
	
	while( rut.indexOf("-") != -1 )
	{
		rut = rut.replace("-","");
	}
	
	var dv = rut.charAt(rut.length-1);
	rut = rut.substring(0, rut.length-1);
		
	while( rut.length > 3 )
	{
		rut_ok = "." + rut.substr(rut.length - 3) + rut_ok;
		rut = rut.substring(0, rut.length - 3);
	}
		
	rut_ok = rut + rut_ok;
	
	if( rut_ok == '' )
	{
		return "";
	}
	else
	{
		return rut_ok + "-" + dv;
	}
}

function botonUpload( id , accion, tipo )
{
	$('#' + id).after("<input name='file_" + id + "' type='hidden' id='file_" + id + "' value='' />");
	
	var button = $('#' + id), interval;
	new AjaxUpload(button,
	{
		action: accion, 
		name: id,
		onSubmit : function(file, ext)
		{
			if( tipo == "img" )
			{
				if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext)))
				{
					// extensiones permitidas
					alert('Sólo se permiten Imágenes');
					// cancela upload
					return false;
				}
			}
			else
			if( tipo == "pdf" )
			{
				if (! (ext && /^(pdf)$/.test(ext)))
				{
					// extensiones permitidas
					alert('Debe ser un archivo .PDF');
					// cancela upload
					return false;
				}
			}

			button.html('<div class="text">Subiendo <img src="img/mini_load.gif" alt="Loading..." border="0" align="absmiddle" /> </div>');
			this.disable();
			
			/*
			// Uploding -> Uploading. -> Uploading...
			interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
			*/
		},
		onComplete: function(file, response)
		{
			if( response == 'success' )
			{
				$('#file_' + id).val( file );
			
				button.html( '<div class="text"><img src="icons/tick.png" alt="Ok" border="0" align="absmiddle" /> ' + file + '</div>' );
			}
			else
			{
				$('#file_' + id).val( "" );
			
				button.html( '<div class="text"><img src="icons/exclamation.png" alt="Ok" border="0" align="absmiddle" /> Error al subir : ' + file + '</div>' );
			}		
			//window.clearInterval(interval);
						
			// enable upload button
			this.enable();
		}
	});
}

function filtrar(e)
{
	tecla = (document.all) ? e.keyCode : e.which;
	
	if(tecla==13 || e == "drop")
	{
		$("#form").submit();
	}
}

function ordenar(campo)
{
	$("#by").val(campo);
	
	if( $("#order").val() == "DESC" )
	{
		$("#order").val("ASC");
	}
	else
	{
		$("#order").val("DESC");
	}
	
	$("#form").submit();
}

function paginar(p)
{	
	var pagina = Number($("#pagina").val());
	var paginas = Number($("#paginas").val());
	
	if( p == 'primera' )
	{
		$("#pagina").val("1");
	}
	else if( p == 'anterior' )
	{
		$("#pagina").val(pagina - 1);
	}
	else if( p == 'siguiente' )
	{
		$("#pagina").val(pagina + 1);
	}
	else
	{
		$("#pagina").val(paginas);
	}
	
	$("#form").submit();
}

function url( url )
{
	document.location.href = url;
}

function refrescar()
{
	document.location.reload();
}

function numeroIn( str )
{
	var num = str.split('.').join('');
	var num = num.split(',').join('.');
	return Number(num);
}

function numeroOut( num, dec )
{
	num = redondear(num, dec);
	var str = miles(num);
	return str;
}

function redondear(cantidad, decimales)
{
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	//decimales = (!decimales ? 2 : decimales);
	var resultado = Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
	
	if( !isNaN(resultado) )
	{
		return resultado;
	}
	else
	{
		return "0";
	}
} 

function miles(num)
{
	num = num.toString().replace(/$|,/g,'');
	
	if(isNaN(num))
	{
		num = "0";
	}
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
	{
		cents = "0" + cents;
	}
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
		num = num.substring(0,num.length-(4*i+3))+'.'+
		num.substring(num.length-(4*i+3));
	}
	
	if(cents!=0)
	{
		return (((sign)?'':'-') + '' + num + ',' + cents);
	}
	else
	{
		return (((sign)?'':'-') + '' + num);
	}
}

function requerido(text)
{
	if(text == "")
	{
		return true;
	}
	else
	{
		return false;
	}
}

function emailValido(email)
{
	var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/  
	
	if(!(b.test(email)))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function soloNumeros(event)
{
    //tecla = (document.all) ? e.keyCode : e.which; 
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	//alert(tecla);
    
	if (tecla == 8) return true; 
	//if (tecla == 46) return true; 
    if (tecla == 9) return true;
	//patron =/[a-z0-9-]/;
	patron =/[0-9]/;
   
	te = String.fromCharCode(tecla);
    
	return patron.test(te); 
}

function verDetalle(id)
{
	$.fancybox({
		'type'	: 'iframe',
		'href'	: 'pagar.php?id='+id,
		'showCloseButton' : false,
		'transitionIn'	  : 'elastic',
		'modal' : 'true'
	});
}


function selectAll( op )
{
	if ( $(op).is(':checked') ) 
	{
		$("input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
	}
	else
	{
		$("input[type='checkbox']:not([disabled='disabled'])").attr('checked', false);
	}
}

function formatoDominio(event)
{ 
    //tecla = (document.all) ? e.keyCode : e.which; 
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	//alert(tecla);
    
	if (tecla == 8) return true;   //backspace
	if (tecla == 9) return true;   //tab
	
	if (tecla == 46)//supr ( . )
	{
		$("#tld").focus();
	}
	
	//if (tecla == 37) return true;  //IZQ
	//if (tecla == 39) return true;  //DER
	if (tecla == 241) return true; //ñ
	if (tecla == 225) return true; //á
	if (tecla == 233) return true; //é
	if (tecla == 237) return true; //í
	if (tecla == 243) return true; //ó
	if (tecla == 250) return true; //ú
	
    
	patron =/[a-z0-9-]/;
	//patron =/[0-9]/;
   
	te = String.fromCharCode(tecla);
    
	return patron.test(te); 
}

function formatDominio(event)
{ 
    //tecla = (document.all) ? e.keyCode : e.which; 
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	//alert(tecla);
    
	if (tecla == 8) return true;   //backspace
	if (tecla == 9) return true;   //tab
	
	if (tecla == 46)//supr ( . )
	{
		return true;
		//document.whois.tld.focus();
	}
	
	//if (tecla == 37) return true;  //IZQ
	//if (tecla == 39) return true;  //DER
	if (tecla == 241) return true; //ñ
	if (tecla == 225) return true; //á
	if (tecla == 233) return true; //é
	if (tecla == 237) return true; //í
	if (tecla == 243) return true; //ó
	if (tecla == 250) return true; //ú
	
    
	patron =/[a-z0-9-]/;
	//patron =/[0-9]/;
   
	te = String.fromCharCode(tecla);
    
	return patron.test(te); 
}

function seleccionar(objInput)
{

    var valor_input = objInput.value;
    var longitud = valor_input.length;

    if (objInput.setSelectionRange) {
        objInput.focus();
        objInput.setSelectionRange (0, longitud);
    }
    else if (objInput.createTextRange) {
        var range = objInput.createTextRange() ;
        range.collapse(true);
        range.moveEnd('character', longitud);
        range.moveStart('character', 0);
        range.select();
    }
} 

function revisarDominio(dom)
{
	re=/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))$/
	
	if(!re.exec(dom)){
		return false;
	}else{
		return true;
	}
}

function salir()
{
	if(confirm("Esta seguro que desea salir del Sistema?"))
	{
		location.href = "logout.php";
	}
}
