
//
// Funciones para el envio del form de login
//

var handleSuccess_enviarFormBusqueda = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            for(var campo in messages) { 
             		for (var razon in messages[campo])	{
             			if (messages[campo][razon] !== undefined && messages[campo][razon] != '')	
             				mensajeError += "<p>&raquo; " + messages[campo][razon] + "</p>\n";
             				
             			 
             				clase = new String(document.getElementById(campo).className);
             				
             				//alert(clase + "Error");
							document.getElementById(campo).className = clase + "Error";
             		} 
             }
             if (mensajeError != '')	{
             	document.getElementById("mensaje-error-busqueda").innerHTML = mensajeError;
             	document.getElementById("mensaje-error-busqueda").className = "mensaje-error-busqueda";
             }
             else	{
             	window.location.href="/intranet/buscarpublicaciones/texto-busc/" + urlencode(document.getElementById("texto-busc").value);
             }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_enviarFormBusqueda = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_enviarFormBusqueda =
{
  success:handleSuccess_enviarFormBusqueda,
  failure:handleFailure_enviarFormBusqueda
};

function enviarFormBusqueda()	{

	//Reset todos los estilos:
	
	document.getElementById("texto-busc").className		= "form_campoBuscadorIntranet";
    //document.getElementById("mensaje-error").className 			= "oculto";
   
	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	var formObject = document.getElementById('form-buscador-intranet');
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var request = YAHOO.util.Connect.asyncRequest('POST', '/validar/formulario', callback_enviarFormBusqueda);
}

// {{{ urlencode
function urlencode( str ) {
    // URL-encodes string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
    // +       version: 809.1713
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}// }}}

