var urlBase = "update_edit.php"; 
var formVars = ""; 
var changing = false; 

function datosServidor() 
{ }; 

datosServidor.prototype.iniciar = function() { 
    try { 
        // Mozilla / Safari 
        this._xh = new XMLHttpRequest(); 

    } catch (e) { 
        // Explorer 
        var _ieModelos = new Array( 
        'MSXML2.XMLHTTP.5.0', 
        'MSXML2.XMLHTTP.4.0', 
        'MSXML2.XMLHTTP.3.0', 
        'MSXML2.XMLHTTP', 
        'Microsoft.XMLHTTP' 
        ); 
        var success = false; 
        for (var i=0;i < _ieModelos.length && !success; i++) { 
            try { 
                this._xh = new ActiveXObject(_ieModelos[i]); 
                success = true; 
            } catch (e) { 
                // Implementar manejo de excepciones 
            } 
        } 
        if ( !success ) { 
            // Implementar manejo de excepciones, mientras alerta. 
            return false; 
        } 
        return true; 
    } 
} 

datosServidor.prototype.ocupado = function() { 
    estadoActual = this._xh.readyState; 
    return (estadoActual && (estadoActual < 4)); 
} 

datosServidor.prototype.procesa = function() { 
    if (this._xh.readyState == 4 && this._xh.status == 200) { 
        this.procesado = true; 
    } 
} 

datosServidor.prototype.enviar = function(urlget, params) { 
    if (!this._xh) 
	{ 
        this.iniciar(); 
    } 
	
    if (!this.ocupado()) 
	{ 
 
		this._xh.open("POST", urlget, true);
		
		//Send the proper header information along with the request
		this._xh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this._xh.setRequestHeader("Content-length", params.length);
		this._xh.setRequestHeader("Connection", "close");

/*
		this._xh.onreadystatechange = function() {//Call a function when the state changes.
			if (this._xh.readyState==4 && this._xh.status == 200) {
				result = this._xh.responseText;
			}
		}
*/		
		this._xh.send(params);
		
		//return result;
         
    } 
    return false;  
} 

function fieldBlur(campo,idfld) {
	
    if (campo.value!="") { 
        elem = document.getElementById( idfld ); 
        remotos = new datosServidor; 
        nt = remotos.enviar(urlBase, "fieldname=" +escape(elem.id)+ "&content="+escape(campo.value)+"&"+formVars); 
        //elem.innerHTML = campo.value; 
		elem.innerHTML = replaceAll( campo.value, [["\n", "<br>"]]);
        changing = false; 
        return false; 
    } 
} 

function replaceAll( str, replacements ) {
    for ( i = 0; i < replacements.length; i++ ) {
        var idx = str.indexOf( replacements[i][0] );

        while ( idx > -1 ) {
            str = str.replace( replacements[i][0], replacements[i][1] );
            idx = str.indexOf( replacements[i][0] );
        }

    }

    return str;
}

//edit field created 
function cambia(actual) { 
    if(!changing)
	{ 
        width = widthEl(actual.id) + 20; 
        height =heightEl(actual.id) + 2; 
        if(width < 100) 
            width = 150; 
			
		var myText = replaceAll( actual.innerHTML, [["\n", ""], ["<br>", "\n"], [ "<br />", "\n" ], ["<BR>", "\n"]] );
		
        if(height < 40) 
            actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + myText + "\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />"; 
        else 
            actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + myText + "</textarea>"; 
     
        changing = true; 
    } 

    actual.firstChild.focus(); 
} 


//find all span tags with class editText and id as fieldname parsed to update script. add onclick function 
function editbox_init(){ 
    if (!document.getElementsByTagName){ return; } 
    var spans = document.getElementsByTagName("span"); 

    // loop through all span tags 
    for (var i=0; i<spans.length; i++){ 
        var spn = spans[i]; 

            if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) 
			{ 
            	spn.onclick = function () { cambia(this); } 
	            spn.style.cursor = "pointer"; 
	            spn.title = "Click to edit!"; 
            } 

    } 


} 

//crossbrowser load function 
function addEvent(elm, evType, fn, useCapture) 
{ 
  if (elm.addEventListener){ 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent){ 
    var r = elm.attachEvent("on"+evType, fn); 
    return r; 
  } else { 
    alert("Please upgrade your browser to use full functionality on this page"); 
  } 
} 

//get width of text element 
function widthEl(span){ 

    if (document.layers){ 
      w=document.layers[span].clip.width; 
    } else if (document.all && !document.getElementById){ 
      w=document.all[span].offsetWidth; 
    } else if(document.getElementById){ 
      w=document.getElementById(span).offsetWidth; 
    } 
return w; 
} 

//get height of text element 
function heightEl(span){ 

    if (document.layers){ 
      h=document.layers[span].clip.height; 
    } else if (document.all && !document.getElementById){ 
      h=document.all[span].offsetHeight; 
    } else if(document.getElementById){ 
      h=document.getElementById(span).offsetHeight; 
    } 
return h; 
} 

function highLight(span){ 
            span.parentNode.style.border = "2px solid #D1FDCD"; 
            span.parentNode.style.padding = "0"; 
            span.style.border = "1px solid #54CE43";           
} 

function noLight(span){ 
        span.parentNode.style.border = "0px"; 
        span.parentNode.style.padding = "2px"; 
        span.style.border = "0px";        

} 

//sets post/get vars for update 

function setVarsForm(vars){ 
    formVars  = vars; 
} 

addEvent(window, "load", editbox_init); 
--> 
