﻿
function validarNumero(numero){
if (/^([0-9])*$/.test(numero))
return true;
else
return false;
}

function CreateXmlHttp()
{   
	var xmlHttpLocal;
    try
    {    
        xmlHttpLocal = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            xmlHttpLocal = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {  
            xmlHttpLocal = null;
        }
    }

    if(!xmlHttpLocal && typeof XMLHttpRequest != "undefined")
    {
        xmlHttpLocal = new XMLHttpRequest();
    }

    return xmlHttpLocal;
}

function getXml(texto){
    var indFin = texto.indexOf('%3C%2Fdatos%3E');
    if (indFin > -1)
    {
	    texto = texto.substring(texto.indexOf('%3Cdatos%3E'),indFin + 14);
	    return texto;
	}
	else {
	    return '';
	}
}

function getXmlNodoValue(textoXml,nombreNodo) {
     var ind = textoXml.indexOf('%3C%2F' + nombreNodo + '%3E');
        if (ind > -1) {
            textoXml = textoXml.substring(textoXml.indexOf('%3C' + nombreNodo + '%3E') + nombreNodo.length + 6,ind);
            ind = textoXml.indexOf('%2C');
            if (ind > -1){
                textoXml = textoXml.substring(0,ind);
            }
            return textoXml ;       
        }
        else {
            return '';
        }
}

function getStock(texto){ 
   return getXmlNodoValue(texto,"existencias");
}

function pideExistencias(referencia,idCampoReturn){
    var xmlHttp = CreateXmlHttp();   
    var ajaxRequest = "proxccalgirex.php";
    xmlHttp.open("POST", ajaxRequest, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
    xmlHttp.send("referencia=" + referencia);
    var respuesta = xmlHttp.responseText;   
    var stock =  getStock(getXml(respuesta));   
    document.getElementById(idCampoReturn).value = stock;
}

function updateNum(direccion, cantidadId)
{
    var valor = document.getElementById(cantidadId).value;
    var num = 0;
    if (valor.length == 0) {valor=0;}
    num = parseInt(valor);
    if (direccion == 1)
        {
            if (num >= 0 ) { num +=1; }
        }
    if (direccion == 2)
        {
            if (num > 0 ) { num -=1; }
        }
    if (num < 0) {num=0;}
        
    document.getElementById(cantidadId).value = num;        
    
}


