function mktime() { //Funcion mktime de Php para javascript
    var d = new Date(), r = arguments, i = 0,
        e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];
    for (i = 0; i < e.length; i++) {
        if (typeof r[i] === 'undefined') {
            r[i] = d['get' + e[i]]();
            r[i] += (i === 3); // +1 to fix JS months.
        } else {
            r[i] = parseInt(r[i], 10);
            if (isNaN(r[i])) {
                return false;
            }
        }
    }
    r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
    d.setFullYear(r[5], r[3] - 1, r[4]);
    d.setHours(r[0], r[1], r[2]);
    return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
};
function compruebaFechaMinima(inicio, fin, dias){ //Para comprobar un minimo de dias entre dos fechas
	if(compruebaFecha(inicio) && compruebaFecha(fin)){
		var fechaInicio = inicio.split('/');
		var fechaFin = fin.split('/');
		var anoInicio = fechaInicio[2]; 
		var mesInicio = fechaInicio[1]; 
		var diaInicio = fechaInicio[0]; 
		var anoFin = fechaFin[2]; 
		var mesFin = fechaFin[1]; 
		var diaFin = fechaFin[0]; 
		var timestamp1 = mktime(0,0,0,mesInicio,diaInicio,anoInicio); 
		var timestamp2 = mktime(4,12,0,mesFin,diaFin,anoFin); 
		var segundos_diferencia = timestamp1 - timestamp2; 
		var dias_diferencia = segundos_diferencia / (60 * 60 * 24); 
		dias_diferencia = Math.abs(dias_diferencia); 
		dias_diferencia = Math.floor(dias_diferencia); 
		if(dias_diferencia >= dias){
			return true;	
		}else{
			return false;
		}; 
	}else{
		return false	
	}
};
function calculaDias(inicio, fin){ //Para calcular la diferencia de dias entre dos fechas
	var fechaInicio = inicio.split('/');
		var fechaFin = fin.split('/');
		var anoInicio = fechaInicio[2]; 
		var mesInicio = fechaInicio[1]; 
		var diaInicio = fechaInicio[0]; 
		var anoFin = fechaFin[2]; 
		var mesFin = fechaFin[1]; 
		var diaFin = fechaFin[0]; 
		var timestamp1 = mktime(0,0,0,mesInicio,diaInicio,anoInicio); 
		var timestamp2 = mktime(4,12,0,mesFin,diaFin,anoFin); 
		var segundos_diferencia =   timestamp2-timestamp1; 
		var dias_diferencia = segundos_diferencia / (60 * 60 * 24); 
		//dias_diferencia = Math.abs(dias_diferencia); 
		dias_diferencia = Math.floor(dias_diferencia);
		return dias_diferencia
}
function compruebaEmail(JSValor) {
	if (JSValor == "") {
		return false;
	}
	JSRegExp =/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@+([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,5}$/;
	if(JSRegExp.test(JSValor)){
		return true;
	}else{
		return false;
	}
}
function compruebaFecha(JSValor) {
	if (JSValor == "") {
		return false;
	}
	JSRegExp =/^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
	if(JSRegExp.test(JSValor)){
		return true;
	}else{
		return false;
	}
}
function compruebaHora(JSValor) {
	if (JSValor == "") {
		return true;
	}
	JSRegExp =/^(0[1-9]|1\d|2[0-3]):([0-5]\d):([0-5]\d)$/;
	if(JSRegExp.test(JSValor)){
		return true;
	}else{
		return false;
	}
}
function compruebaDni(JSValor) {
	if (JSValor == "") {
		return false;
	}
	JSRegExp =/^\d{1,8}$/;
	
	n = JSValor.substr(0,8);
	l = JSValor.substr(8,1);
	l = l.toUpperCase()
	n = n % 23;
	lt = 'TRWAGMYFPDXBNJZSQVHLCKET';
	lt = lt.substring(n,n+1);
	
	if(JSRegExp.test(n) && l == lt){
		return true;
	}else{
		return false;
	}
}
function compruebaCif(JSValor) {
	if (JSValor == "") {
		return true;
	}
		var pares = 0;
        var impares = 0;
        var suma;
        var ultima;
        var unumero;
        var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
        var xxx;
        
        JSValor = JSValor.toUpperCase();
        
        var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
         if (!regular.exec(JSValor)) return false;
             
         ultima = JSValor.substr(8,1);

         for (var cont = 1 ; cont < 7 ; cont ++){
             xxx = (2 * parseInt(JSValor.substr(cont++,1))).toString() + "0";
             impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
             pares += parseInt(JSValor.substr(cont,1));
         }
         xxx = (2 * parseInt(JSValor.substr(cont,1))).toString() + "0";
         impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
         
         suma = (pares + impares).toString();
         unumero = parseInt(suma.substr(suma.length - 1, 1));
         unumero = (10 - unumero).toString();
         if(unumero == 10) unumero = 0;
         
         if ((ultima == unumero) || (ultima == uletra[unumero]))
             return true;
         else
             return false; 
}
function compruebaCifNifNie(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 false;
		}
 
		//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 true;
			}
			else
			{
				return false;
			}
		}
 
		//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 true;
			}
			else
			{
				return false;
			}
		}
 
		//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 true;
			}
			else
			{
				return false;
			}
		}
 
		//comprobacion de NIEs
		//T
		if (/^[T]{1}/.test(temp))
		{
			if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
 
		//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 true;
			}
			else
			{
				return false;
			}
		}
	}
 
	return false;

}
function compruebaTarjeta(JSValor) {
	if (JSValor == "") {
		return true;
	}
	JSRegExp =/^((67\d{2})|(4\d{3})|(5[1-5]\d{2})|(6011))(-?\s?\d{4}){3}|(3[4,7])\ d{2}-?\s?\d{6}-?\s?\d{5}$/;
	if(JSRegExp.test(JSValor)){
		return true;
	}else{
		return false;
	}
}
function compruebaTelefono(JSValor) {
	if (JSValor == "") {
		return true;
	}
	JSRegExp =/^[0-9]{2,2}-? ?[0-9]{7,7}$/;
	if(JSRegExp.test(JSValor)){
		return true;
	}else{
		return false;
	}
}

var Base64 = {};  // Base64 namespace
Base64.code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

base64_encode = function(str, utf8encode) {  // http://tools.ietf.org/html/rfc4648
  utf8encode =  (typeof utf8encode == 'undefined') ? false : utf8encode;
  var o1, o2, o3, bits, h1, h2, h3, h4, e=[], pad = '', c, plain, coded;
  var b64 = Base64.code;
   
  plain = utf8encode ? str.encodeUTF8() : str;
  
  c = plain.length % 3;  // pad string to length of multiple of 3
  if (c > 0) { while (c++ < 3) { pad += '='; plain += '\0'; } }
  // note: doing padding here saves us doing special-case packing for trailing 1 or 2 chars
   
  for (c=0; c<plain.length; c+=3) {  // pack three octets into four hexets
    o1 = plain.charCodeAt(c);
    o2 = plain.charCodeAt(c+1);
    o3 = plain.charCodeAt(c+2);
      
    bits = o1<<16 | o2<<8 | o3;
      
    h1 = bits>>18 & 0x3f;
    h2 = bits>>12 & 0x3f;
    h3 = bits>>6 & 0x3f;
    h4 = bits & 0x3f;

    // use hextets to index into code string
    e[c/3] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
  }
  coded = e.join('');  // join() is far faster than repeated string concatenation in IE
  
  // replace 'A's from padded nulls with '='s
  coded = coded.slice(0, coded.length-pad.length) + pad;
   
  return coded;
}
base64_decode = function(str, utf8decode) {
  utf8decode =  (typeof utf8decode == 'undefined') ? false : utf8decode;
  var o1, o2, o3, h1, h2, h3, h4, bits, d=[], plain, coded;
  var b64 = Base64.code;

  coded = utf8decode ? str.decodeUTF8() : str;
  
  
  for (var c=0; c<coded.length; c+=4) {  // unpack four hexets into three octets
    h1 = b64.indexOf(coded.charAt(c));
    h2 = b64.indexOf(coded.charAt(c+1));
    h3 = b64.indexOf(coded.charAt(c+2));
    h4 = b64.indexOf(coded.charAt(c+3));
      
    bits = h1<<18 | h2<<12 | h3<<6 | h4;
      
    o1 = bits>>>16 & 0xff;
    o2 = bits>>>8 & 0xff;
    o3 = bits & 0xff;
    
    d[c/4] = String.fromCharCode(o1, o2, o3);
    // check for padding
    if (h4 == 0x40) d[c/4] = String.fromCharCode(o1, o2);
    if (h3 == 0x40) d[c/4] = String.fromCharCode(o1);
  }
  plain = d.join('');  // join() is far faster than repeated string concatenation in IE
   
  return utf8decode ? plain.decodeUTF8() : plain; 
}
