document.write("<link href='css/estilo.css' rel='stylesheet' type='text/css' />");

document.write("<script type='text/javascript' src='js/swfobject.js'></script>"); 				// PARA FLASH
document.write("<script type='text/javascript' src='js/jquery.js'></script>"); 					// LIBRERIA JQUERY
document.write("<script type='text/javascript' src='js/ui.jquery.js'></script>"); 				// USER INTERFACE JQUERY (DATEPICKER Y OTRAS MAS)

document.write("<script type='text/javascript' src='js/jquery.Rut.min.js'></script>"); 			// FUNCIONES PARA VALIDAR Y DAR FORMATO AL RUT.


function ltrim(s) {
	return s.replace(/^\s+/, "");
}

function rtrim(s) {
	return s.replace(/\s+$/, "");
}

function trim(s) {
	return rtrim(ltrim(s));
}
function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }
function formato_numero(numero, decimales, separador_decimal, separador_miles){
    numero=parseFloat(numero);
    if(isNaN(numero)){
        return "";
    }

    if(decimales!==undefined){
        // Redondeamos
        numero=numero.toFixed(decimales);
    }

    // Convertimos el punto en separador_decimal
    numero=numero.toString().replace(".", separador_decimal!==undefined ? separador_decimal : ",");

    if(separador_miles){
        // Añadimos los separadores de miles
        var miles=new RegExp("(-?[0-9]+)([0-9]{3})");
        while(miles.test(numero)) {
            numero=numero.replace(miles, "$1" + separador_miles + "$2");
        }
    }

    return numero;
}

function txt(texto){
	texto = str_replace("á","\u00e1",texto);
	texto = str_replace("é","\u00e9",texto);
	texto = str_replace("í","\u00ed",texto);
	texto = str_replace("ó","\u00f3",texto);
	texto = str_replace("ú","\u00fa",texto);

	texto = str_replace("Á","\u00c1",texto);
	texto = str_replace("É","\u00c9",texto);
	texto = str_replace("Í","\u00cd",texto);
	texto = str_replace("Ó","\u00d3",texto);
	texto = str_replace("Ú","\u00da",texto);

	texto = str_replace("ñ","\u00f1",texto);
	texto = str_replace("Ñ","\u00d1",texto);

	return texto;
}

function valida_rut(run){
	var tmpstr = "";
	var intlargo = run
	if (intlargo.length> 0)
	{
		crut = run
		largo = crut.length;
		if ( largo <2 )
		{
			alert('rut inválido')
			//Objeto.focus()
			return false;
		}
		for ( i=0; i <crut.length ; i++ )
		if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
		{
			tmpstr = tmpstr + crut.charAt(i);
		}
		rut = tmpstr;
		crut=tmpstr;
		largo = crut.length;

		if ( largo> 2 )
			rut = crut.substring(0, largo - 1);
		else
			rut = crut.charAt(0);

		dv = crut.charAt(largo-1);

		if ( rut == null || dv == null )
		return 0;

		var dvr = '0';
		suma = 0;
		mul  = 2;

		for (i= rut.length-1 ; i>= 0; i--)
		{
			suma = suma + rut.charAt(i) * mul;
			if (mul == 7)
				mul = 2;
			else
				mul++;
		}

		res = suma % 11;
		if (res==1)
			dvr = 'k';
		else if (res==0)
			dvr = '0';
		else
		{
			dvi = 11-res;
			dvr = dvi + "";
		}

		if ( dvr != dv.toLowerCase() )
		{
			//alert('El Rut Ingreso es Invalido')
			//Objeto.focus()
			return false;
		}
		//alert('El Rut Ingresado es Correcto!')
		//Objeto.focus()
		return true;
	}
}

function validar_email(valor) {
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true);
	}else{
		return (false);
	}
}

function replaceAll( text, str1, str2 ){
	var command = "text.replace(/"+str1+"/g,\""+str2+"\")";
	return eval(command);
}

function msg(mensaje,tipo,link,tiempo) {
	tipo = tipo || "alert";
	tiempo = tiempo || 0;
	link = link || '';
	$(document).ready(function() {
		var link2 = replaceAll(link,"&","#");
		var mensaje2 = replaceAll(mensaje,"&","#");
		$('body').css('overflow','hidden');
		$('#msg1').fadeIn("fast", function() {
			$.ajax({
				type: "POST",
				url: "ajax/mensaje.php",
				data: "mensaje="+mensaje2+"&tipo="+tipo+"&link="+link2,
				success: function(msg){
					$('#msg').html(msg);
				}
			});
		});


		if(tiempo>0) {
			if(link!=''){
			 	setTimeout('location.href="'+link+'"',tiempo);
			}else{
				setTimeout('cerrar_msg();',tiempo);
			}
		}
	});
}
function cerrar_msg() {
	$('#msg1').fadeOut('fast', function() {$('#msg').html('');});
	$('body').css('overflow','auto');
}
