

function load_xml_content_string(xmlData)
{
	var xmlDoc;

	// if (window.ActiveXObject)

	if (jQuery.browser.msie)
	{
		//for IE
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.loadXML(xmlData);

	//	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	//	xmlDoc.async="false";
	//	xmlDoc.loadXML(xmlData);

		return xmlDoc;
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		//for Mozila
		parser=new DOMParser();
		xmlDoc=parser.parseFromString(xmlData,"text/xml");
		return xmlDoc;
	}

	return xmlDoc;
}


function validarEmail(valor)
{
	var objRegExp = /^[A-Za-z][A-Za-z0-9_.\-]*@[A-Za-z0-9_\-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	return objRegExp.test(valor);
}

function validarCP(valor)
{
	var objRegExp = /(^[0-9]{5}$)|(^[0-9]{4}$)/;
	return objRegExp.test(valor);
}

function validarTelefono(valor)
{
	var objRegExp = /(^[0-9]{6}$)|(^[0-9]{7}$)|(^[0-9]{8}$)|(^[0-9]{9}$)|(^[0-9]{10}$)|(^[0-9]{11}$)|(^[0-9]{12}$)|(^[0-9]{13}$)|(^[0-9]{14}$)|(^[0-9]{15}$)|(^[0-9]{16}$)/;
	return objRegExp.test(valor);
}

function str_replace(search, replace, subject) {

    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };

    return sa ? s : s[0];
}

//Retorna: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF error, -2 = CIF error, -3 = NIE error, 0 = ??? error
function valida_nif_cif_nie(a)
{
	var temp=a.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";

	if (temp!==''){
		//si no tiene un formato valido devuelve error
		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return 0;
		}

		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return 1;
			}
			else
			{
				return -1;
			}
		}

		//algoritmo para comprobacion de codigos tipo CIF
		suma = parseInt(a[2])+parseInt(a[4])+parseInt(a[6]);
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a[i]);
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a[i]);
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}

			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));

		//comprobacion de NIFs especiales (se calculan como CIFs)
		if (/^[KLM]{1}/.test(temp))
		{
			if (a[8] == String.fromCharCode(64 + n))
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}

		//comprobacion de CIFs
		if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
		{
			temp = n + '';
			if (a[8] == String.fromCharCode(64 + n) || a[8] == parseInt(temp.substring(temp.length-1, temp.length)))
			{
				return 2;
			}
			else
			{
				return -2;
			}
		}

		//comprobacion de NIEs
		//T
		if (/^[T]{1}/.test(temp))
		{
			if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}

		//XYZ
		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
			if (a[8] == cadenadni.substring(pos, pos + 1))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
	}

	return 0;
}

function intval (mixed_var, base)
{
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30    var tmp;

    var type = typeof( mixed_var );

    if (type === 'boolean') {        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

function IsNumeric(expression)
{
    return (String(expression).search(/^\d+$/) != -1);
}

function IsDecimal(expression)
{
    return (String(expression).search(/^\d+(\.\d+)?$/) != -1);
}

// Impide que se puedan rellenar en un capo de texto
// un numero de caracteres
//   * @param texto Texto a verificar
//   * @param numero maximo de caracteres permitido
//   * @param tipo_caracter Tipo de caracteres permitidos ( all|num )
//
//   * Return string cadena formateada
function maxCaracter( texto, maxcaracteres, tipo_caracter )
{
	if ( tipo_caracter === undefined )
		tipo_caracter = "all";

	var a = texto;
	var b = 0;

	switch( tipo_caracter )
	{
		case "num":
			b = intval(a);
			a = b.toString();

			break;
		default:
		case "all":
			break;
	}

	return a.substring( 0, maxcaracteres );
}

function ocultar_mostrar_capa( capa, lnk )
{
	if ( $("."+capa).css("display") == "none" )
	{
		$("."+capa).show(1000);
		$("."+lnk).html( "Ocultar " + $("."+lnk).attr("anade") );
	}
	else
	{
		$("."+capa).hide(1000);
		$("."+lnk).html( "Mostrar " + $("."+lnk).attr("anade") );
	}
}

