/**************************************************/
/*                                                */
/* k.basic.js                                     */
/* Funciones básicas                              */
/*                                                */
/* (c) 2010 Komperio S.A. de C.V.                 */
/* Roberto G. Calvillo                            */
/* Rígel Noé Martíez                              */
/* David Vázquez Ramírez                          */
/*                                                */
/* Queda prohibido su uso, modificación, o        */
/* distribución, sin el consentimiento de su      */
/* propietario.                                   */
/*                                                */
/**************************************************/

// Detectar Internet Explorer

function version_ie() {
	var version = -1;
	if (navigator.appName == "Microsoft Internet Explorer") {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			version = parseFloat(RegExp . $1);
		}
	}
	return version;
}

var ie = (version_ie() == -1) ? 0 : 1;

// Características de la pantalla

function get_screen_width() {
    if (navigator.appName == "Netscape") { 
	  return window.innerWidth;
	}
    else {
	  return document.body.clientWidth;
	}
}

function get_screen_height() {
    if (navigator.appName == "Netscape") {
	  return window.innerHeight;
	}
    else {
	  return document.body.clientHeight;
	}
}

// Precarga de Imágenes

function preload(url) {
	var img = new Image();
	img.src = url;
}

// Manipulación de elementos

function get_element(id) {
	if (document.getElementById) {
		objeto = document.getElementById(id);
	}												  
	else if (document.all) {	
		objeto = document[id];
	}
	return objeto;
}

function show(id) {
	var objeto = get_element(id);
	objeto.style.display = "block";
}

function hide(id) {
	var objeto = get_element(id);
	objeto.style.display = "none";
}

function swap_style(id, style) {
	var objeto = document.getElementById(id);
	if (objeto != undefined) {
		objeto.setAttribute("class", style);
		objeto.setAttribute("className", style);
	}
}

function set_html(id, html) {
	var objeto = get_element(id);
	objeto.innerHTML = html;
}

// Ajax

function ajax(url, on_load_function) {
	var request = false;
	
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	else {
		return false
	}
	
	request.onreadystatechange = function() {
		if (request.readyState == 4 && (request.status == 200 || window.location.href.indexOf("http") == -1)) {
			on_load_function(unescape(request.responseText));
		}
	}
	
	request.open('GET', url, true);
	request.send(null);
}

// Encripción

function aleatorio(inferior, superior) {
	var numPosibilidades = superior - inferior;
	var aleat = Math.random() * numPosibilidades;
	aleat = Math.round(aleat);
	return parseInt(inferior) + aleat;
}

function encode_ds(input) {
	if (input == "") {
		return "";
	}
	
	// Requiere webtoolkit.base64.js
	
	else if (Base64.encode == undefined) {
		return input;
	}
	
	// Encode DS
	
	else {
		input = Base64.encode(input);
		
		var i = 0;
		var output = '';
		var cambio = aleatorio(1, 9);
		var vector = new Array();
		
		while (i < input.length) { 
			var aux = input.charCodeAt(i); 		
			aux = aux - cambio;
				
			vector[i] = aux;
			i++;			
		}
		vector[vector.length] = (cambio + 33);
		
		i = 0;
		while (i < (vector.length - 1)) {
			output += String.fromCharCode(vector[i]);
			i++;			
		}
		output += String.fromCharCode(vector[vector.length - 1]);
			
		return output;
	}
}

// Validaciones

function validar_email(input) {
	if (input == "") {
		return true;
	}
	else {
		var validar = /^(\w+(\w+|(\-\w+)|(\.\w+))+)@(\w+(\w+|(\-\w+)|(\.\w+))+)(\.([a-zA-Z]+))+$/;
		return validar.test(input);
	}
}

function validar_nombre(input) {
	if (input == "") {
		return true;
	}
	else {
		var validar = /^[a-zA-ZáéíóúüñÁÉÍÓÚÜÑ\s]+$/;
		return validar.test(input);
	}
}

function validar_numero (input) {
	if (input == "") {
		return true;
	}
	else {
		var validar = /^[0-9]+$/;
		return (validar.test(input));
	}
}

function validar_telefono(input) {
	if (input == "") {
		return true;
	}
	else {
		var patron = /[\(|\)|\-|\.|\s]/g;
		var numeros = /^[0-9]+$/;
		var sustituir = '';
		input = input.replace(patron, sustituir);
		var res = numeros.test(input);
		if (input.length != 10) {
			return false;
		}
		if (!res) {
			return false;
		}
		else {
			return true;
		}
	}
}

function validar_rfc(input) {
	if (input == "") {
		return true;
	}
	else {
		var patron = /[\-|\s]/g;
		var solo_asccis = /^[A-Z|a-z|0-9]+$/;
		var sustituir = '';
		input = input.replace(patron, sustituir);
		
		if (input.length != 12 && input.length != 13) {
			return false;
		}
		
		if (!solo_asccis.test(input)) {
			return false;
		}
		else {
			return true;
		}
	}
}

function formato_dinero(numero){
	var rango = 2;
	
    var NumericValue = parseFloat( numero );
	valor = NumericValue.toFixed( rango );
	valor.toString();
	
	var fragmentoTexto = valor.split('.');
	var numeros = fragmentoTexto[0].split('');
	var decimales = fragmentoTexto[1];
	
	var cont = 1;
	var numeros_formato = '';
	for(i=0 ; i < numeros.length ; i++){
		coma = "";
		if(( (numeros.length - (i + 1)) % 3) == 0 && (numeros.length - 1) != i ){
			coma = ",";
			cont = 1;
		}
		cont++;
		numeros_formato += numeros[i] + coma;
	}
	
	if(rango > 0 ){
		numeros_formato += "." + decimales;
	}
	
	return numeros_formato;
}
