﻿
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 getPedido(texto){    
   return getXmlNodoValue(texto,"codigo_pedido");
}

function getEstado(texto){    
   return getXmlNodoValue(texto,"estado");
}

function compruebaStock(referencia,stockTemp){
    var xmlHttp = CreateXmlHttp();
    var ajaxRequest = "Check_Stock_Compra.aspx";
    xmlHttp.open("POST", ajaxRequest, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
    xmlHttp.send("referencia=" + referencia + "&stock=" + stockTemp);
	var returnStock = xmlHttp.responseText;
    return returnStock;
}

function recogeReferencia(referencia){ 	  
    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 stockTemp = getStock(getXml(xmlHttp.responseText));
	var returnStock = compruebaStock(referencia,stockTemp);
	return returnStock;
}

function checkCantidad(idLinea,cantidad){
	var xmlHttp = CreateXmlHttp();
	var ajaxRequest = "Get_Referencia_Compra.aspx";  
	xmlHttp.open("POST", ajaxRequest, false);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	xmlHttp.send("idLinea=" + idLinea);	
	var referencia = xmlHttp.responseText;		
	var resultado = recogeReferencia(referencia);		
	return resultado;
}

function checkCambioCantidad(campoCantidad){

    if (validarNumero(campoCantidad.value) == false)
    {
        campoCantidad.value = 1;
    }
   
}

function enviaPedido(datos,idCampoReturn){
    var xmlHttp = CreateXmlHttp();   
    var ajaxRequest = "proxccalgirped.php";
    xmlHttp.open("POST", ajaxRequest, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
    xmlHttp.send("datos=" + datos);	
	var respuesta = xmlHttp.responseText;    
	document.getElementById(idCampoReturn).value = getPedido(getXml(respuesta));
}

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;
        
   
}

function checkListadoCompra(referencia, cantidad, stockMin, 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 stock = getStock(getXml(xmlHttp.responseText));
    if (validarNumero(stock)) {
        if ((parseInt(stock) - parseInt(stockMin)) > parseInt(cantidad))
        {
            document.getElementById(idCampoReturn).value = stock;
        }
        else
        {
            document.getElementById(idCampoReturn).value = -1;
        }
    }
    else
    {
        document.getElementById(idCampoReturn).value = -1;
    }
}

