var http    = createRequestObject();
var target  = '';
var action  = '';
var id      = '';
var cad     = '';
var from    = '';
var append  = '';
var type    = '';
var content = 'content';
var delay   = 1000;

// Creamos conexion
function createRequestObject() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(f) {
            xmlhttp = null;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

// Envia TEXTO por POST
function sendRequestTextPost(type, id) {
    var rnd = Math.random();
    try {

        type = escape(type);
        id   = escape(id);

		var actions = 'type='+type;
		if (id) {
			actions += '&id='+id;
		}
		if (action) {
			actions += '&action='+action;
		}
		if (cad) {
			actions += '&name='+escape(cad);
		}
		if (from) {
			actions += '&from='+escape(from);
		}
		if (append) {
			actions += append;
		}
		actions += '&rnd='+rnd;

        http.open('POST', '/register.php', true);
        //http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = handleResponseText;
        http.send(actions);
    } catch(e) {
//        alert("error send: "+e);
    } finally {}
}

// Recoge respuesta del servidor como TEXTO
function handleResponseText() {
	try {
		if (http.readyState == 4 && http.status == 200) {
		    var response = ""+http.responseText+"";
		    if (target) {
    			var obj = document.getElementById(target);
    			if (obj && response) {
    				obj.innerHTML = response;
    			}
    			if (content) {
    				var to = document.getElementById(content);
    				if (to) {
    					if (to.style.display != "block") {
    						to.style.display = "block";
    					}
    				}
    			}
    			if (!response.match("ERROR:") && (action == 'ADD')) {
    				setTimeout('location.reload();', delay);
    			}
		    } else if (response.match("ERROR:")) {
    		    var error = document.getElementById('error');
    		    if (!error) {
    		        return;
    		    }
    		    error.innerHTML = response;
		    } else if (response.substring(0,7) == 'precio:') {
                setPrecio(response);
		    }
		}
	} catch(e) {
	   // alert("error rcv: "+e);
	} finally {}
}

function getPrecio(append) {

    this.type   = 'precio';
    this.from   = '';
    this.append = append;
    this.target = '';

    this.id     = id;
	this.action = '';

	sendRequestTextPost(this.type);
}

function setPrecio(texto) {
    texto = texto.substring(7);
    var v = texto.split('#');

    var id = v[0];
    if (!id) {
        return;
    }

    // Totales
    var subtotal = document.getElementById('subtotal');
    if (subtotal) {
        subtotal.innerHTML = v[1];
    }
    var ttotal = document.getElementById('total');
    if (ttotal) {
        ttotal.innerHTML = v[1];
    }

    // Fila afectada
    var precio = document.getElementById('precio_'+id);
    if (precio) {
        precio.innerHTML = v[2];
    }
    var total = document.getElementById('total_'+id);
    if (total) {
        total.innerHTML = v[3];
    }
    var dto = document.getElementById('dto_'+id);
    if (dto) {
        if (v[3] == '0.00') {
            dto.innerHTML = '';
        } else {
            dto.innerHTML = v[4]+' %';
        }
    }

}
