
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

function substr (str, start, len) {
	
    var i = 0, allBMP = true, es = 0, el = 0, se = 0, ret = '';
    str += '';
    var end = str.length;

    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.ini = this.php_js.ini || {};
    // END REDUNDANT
    switch(
        (this.php_js.ini['unicode.semantics'] && 
            this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) {
        case 'on': // Full-blown Unicode including non-Basic-Multilingual-Plane characters
            // strlen()
            for (i=0; i < str.length; i++) {
                if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                    allBMP = false;
                    break;
                }
            }

            if (!allBMP) {
                if (start < 0) {
                    for (i = end - 1, es = (start += end); i >= es; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            start--;
                            es--;
                        }
                    }
                }
                else {
                    var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
                    while ((surrogatePairs.exec(str)) != null) {
                        var li = surrogatePairs.lastIndex;
                        if (li - 2 < start) {
                            start++;
                        }
                        else {
                            break;
                        }
                    }
                }

                if (start >= end || start < 0) {
                    return false;
                }
                if (len < 0) {
                    for (i = end - 1, el = (end += len); i >= el; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            end--;
                            el--;
                        }
                    }
                    if (start > end) {
                        return false;
                    }
                    return str.slice(start, end);
                }
                else {
                    se = start + len;
                    for (i = start; i < se; i++) {
                        ret += str.charAt(i);
                        if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                            se++; // Go one further, since one of the "characters" is part of a surrogate pair
                        }
                    }
                    return ret;
                }
                break;
            }
            // Fall-through
        case 'off': // assumes there are no non-BMP characters;
                           //    if there may be such characters, then it is best to turn it on (critical in true XHTML/XML)
        default:
            if (start < 0) {
                start += end;
            }
            end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
            // PHP returns false if start does not fall within the string.
            // PHP returns false if the calculated end comes before the calculated start.
            // PHP returns an empty string if start and end are the same.
            // Otherwise, PHP returns the portion of the string from start to end.
            return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
    }
    return undefined; // Please Netbeans
}

$(document).ready(function(){

	$("#formulario").submit(function () {

		if($("#direccion").val().length < 4) {
			jAlert("La dirección de correo es obligatoria");
			return false;
		}

		if($("#direccion").val().indexOf('@', 0) == -1) {
			jAlert("La dirección de correo parece incorrecta");
			return false;
		}
		
		if($("#password").val().length < 4) {
			jAlert("La contraseña es incorrecta");
			return false;
		}

		login_form();

		return false;

	});

	function login_form() {

		$("#div_cargando").fadeIn(200);

		$.ajax({
			type: "POST",
			url: "tutoriales.php",
			data: "javascript=1&direccion="+$("#direccion").val()+"&password="+$("#password").val(),
			success: function(data){
				
				$("#contenido").css('min-height',$("#div_formulario").height()+$("#logo").height());
				$("#contenido").append('<div name="ajax" id="ajax" class="ajax">'+data+'</div>');
				$("#contenido").append('<div name="ajax_tutorial" id="ajax_tutorial" class="ajax_tutorial"></div>');
				$("#div_cargando").fadeOut(200);
				
				setTimeout(function(){
					$("#div_formulario").fadeOut(200);
					setTimeout(function(){
						$("#ajax").fadeIn(200);
					}, 300);
				}, 300);

				$('.tutorial_link').unbind();
				$('.tutorial_link').click(function() {
					archivo_ajax_load = $(this).find("a").attr("href");
					load_tutorial(archivo_ajax_load);
					return false;
				});
				
			}
		});
		
	}

	function load_tutorial(url_tutorial) {
		
		url_tutorial = str_replace(rutaweb+"/","",url_tutorial);
		parametros_url_tutorial = substr(url_tutorial,strpos(url_tutorial,".php?")+5);
		parametros_url_tutorial = parametros_url_tutorial+"&javascript=1&direccion="+$("#direccion").val()+"&password="+$("#password").val();
		url_tutorial = substr(url_tutorial,0,strpos(url_tutorial,".php?")+4);

		$("#ajax").css("display","none");
		$("#div_cargando").fadeIn(200);

		$.ajax({
			type: "POST",
			url: "tutorial.php",
			data: parametros_url_tutorial+"javascript=1&direccion="+$("#direccion").val()+"&password="+$("#password").val(),
			success: function(data){

				$("#ajax_tutorial").html(data);
				
				$('.tutorial_volver').unbind();
				$('.tutorial_volver').click(function() {
					volver_inicio();
					return false;
				});
				
				setTimeout(function(){
					$("#div_cargando").fadeOut(200);
					setTimeout(function(){
						$("#ajax_tutorial").fadeIn(200);
					}, 300);
				}, 300);
				
			},
			error: function(request, errorType, errorThrown){
				alert(errorType." ".errorThrown);
			}
		});

	}
	
	function volver_inicio() {
		
		$("#ajax_tutorial").css("display","none");
		$("#div_cargando").fadeIn(200);
		setTimeout(function(){
			$("#div_cargando").fadeOut(200);
			setTimeout(function(){
				$("#ajax").fadeIn(200);
			}, 300);
		}, 300);

	}

});
