//Código para búsqueda de productos...

var objFB=null, bolFB=false;

function A_BsqProInicializa(strF) {
	if(document.forms[strF]) objFB = document.forms[strF];
	if(objFB) bolFB = true;
	if(bolFB){
		objTM = new clsCbo();
		objTM.cboE = objFB.cboE;
		objTM.cboC = objFB.cboC;
		objTM.cboT = objFB.cboT;
		objTM.InicializaCbos();

		pPreProcesaDatosTipoMaq(objTM);

		objFB.cboE.strFrase = "--- Selecciona ---";
		objFB.cboE.strFraseVacio = "No se encontraron Clasificaciones en esta selección.";
		objFB.cboE.onchange = function() {objTM.A_BsqSelecciona('E', this);}

		objTM.LlenaCbo(1);

		objFB.cboC.strFrase = "--- Selecciona ---";
		objFB.cboC.strFraseVacio = "No se encontraron Tipos de Máquinas en esta selección.";
		objFB.cboC.onchange = function() {objTM.A_BsqSelecciona('C', this);}

		objFB.cboT.strFrase = "--- Selecciona ---";
		objFB.cboT.strFraseVacio = "No se encontraron Productos en esta selección.";
		objFB.cboT.onchange = function() {LinPa(this);}


		objCO = new clsCbo();
		objCO.cboE = objFB.cboE2;
		objCO.cboC = objFB.cboC2;
		objCO.cboT = objFB.cboT2;
		objCO.InicializaCbos();

        pPreProcesaDatosCodTra(objCO);
        
		objFB.cboE2.onchange = function() {objCO.A_BsqSelecciona('E', this);}
		objFB.cboC2.onchange = function() {objCO.A_BsqSelecciona('C', this);}
		objFB.cboT2.onchange = function() {LinPa(this);}
		objFB.cboE2.strFrase = "--- Selecciona ---";
		objFB.cboE2.strFraseVacio = "No se encontraron Clasificaciones en esta selección.";
		objFB.cboC2.strFrase = "--- Selecciona ---";
		objFB.cboC2.strFraseVacio = "No se encontraron Código Tra en esta selección.";
		objFB.cboT2.strFrase = "--- Selecciona ---";
		objFB.cboT2.strFraseVacio = "No se encontraron Productos en esta selección.";
		objCO.LlenaCbo(1);


		objOA = new clsCbo();
		objOA.cboT = objFB.cboT3;
        objOA.InicializaCbos();
        
		objFB.cboT3.onchange = function() {LinPa(this);}
		objFB.cboT3.strFrase = "--- Selecciona ---";
		objFB.cboT3.strFraseVacio = "No se encontraron Productos en esta selección.";

        pPreProcesaDatosPro(objOA);
		objOA.LlenaCbo(3);

	}
}

String.prototype.Right = function(intC) { return this.substr(this.length - intC); };

function pPreProcesaDatosTipoMaq(objD) {
    var intN1, intN2, intN3, intN4;
    var strN0, strN1, strN2, strN3, strN4;
    var arrD = arrMaq;
    
    strN0 = "01";
	objD.gAP(strN0, 'Raíz');

    for(intN1 = 0; intN1< arrD.length; intN1++)
    {
        strN1 = ("00"+(intN1+1)).Right(2);
        objD.gAE(strN0+strN1, arrD[intN1][0]);
        for(intN2 = 0; intN2<arrD[intN1][1].length; intN2++)
        {
            strN2 = ("000"+(intN2+1)).Right(3);
            objD.gAC(strN0+strN1+strN2, arrD[intN1][1][intN2][0]);
            for(intN3 = 0; intN3<arrD[intN1][1][intN2][1].length; intN3++)
            {
                strN3 = ("000"+(intN3+1)).Right(3);
                strN4 = arrD[intN1][1][intN2][1][intN3];
                objD.gAT(strN0+strN1+strN2+strN3, pArmaClavePro(strN4) + "||" + pArmaNombrePro(strN4));
            }
        }
    }
}

function pPreProcesaDatosCodTra(objD) {
    var intN1, intN2, intN3, intN4;
    var strN0, strN1, strN2, strN3, strN4;
    var arrD = arrCod;
    
    strN0 = "01";
	objD.gAP(strN0, 'Raíz');

    for(intN1 = 0; intN1< arrD.length; intN1++)
    {
        strN1 = ("00"+(intN1+1)).Right(2);
        objD.gAE(strN0+strN1, arrD[intN1][0]);
        for(intN2 = 0; intN2<arrD[intN1][1].length; intN2++)
        {
            strN2 = ("000"+(intN2+1)).Right(3);
            objD.gAC(strN0+strN1+strN2, arrD[intN1][1][intN2][0]);
            for(intN3 = 0; intN3<arrD[intN1][1][intN2][1].length; intN3++)
            {
                strN3 = ("000"+(intN3+1)).Right(3);
                strN4 = arrD[intN1][1][intN2][1][intN3];
                objD.gAT(strN0+strN1+strN2+strN3, pArmaClavePro(strN4)+"||"+pArmaNombrePro(strN4));
            }
        }
    }
}

function pPreProcesaDatosPro(objD) {
    var arrD = arrPro;
    
    for(var strIdx in arrD)
    {
        objOA.gAT(pArmaClavePro(strIdx), (pArmaNombrePro(strIdx)));
    }
}


function pArmaClavePro(strClv) {
	try {
		if(arrPro[strClv][1] == "PDF")
			return arrPro[strClv][1]+strClv;
		else
			return arrPro[strClv][1];
	}
	catch(e) {
		return strClv;
	}
}

function pArmaNombrePro(strClv) {
	try {
		return arrPro[strClv][0];
	}
	catch(e) {
		return "No existe (" + strClv + ")";
	}
}



function A_BsqBusca(arrD, strBsq, intLCmp, cboD)
{
    //alert("Busca="+ strBsq);
	if(strBsq.length>0)
		pAgrCbo(arrD, cboD, false, strBsq, intLCmp);
	return (cboD.length);
}

function pCboInicializa(cboD) {
    if(cboD!=null) {
        cboD.style.display="";
	    cboD.length=0;
	    if(cboD.length==0 && typeof(cboD.strFraseInicializa) != "undefined" && cboD.strFraseInicializa.length>0)
		    cboD.options[0] = new Option(cboD.strFraseInicializa,"","False","False");
	    else
		    cboD.options[0] = new Option("-- Debe elegir del nivel anterior --","","False","False");
    }
}

function pAgrCbo(arrD, cboD, intSelec, strB, intLCmp)
{
	var intI,intMax,objO;
	var bolBsq =false, bolAgregar=false;
	var intArgs = pAgrCbo.arguments.length;

	cboD.length=0;
	intMax=arrD.length;
	//alert(intArgs);
	if(pAgrCbo.arguments.length<4) strB="";
	if(pAgrCbo.arguments.length<5) intLCmp=0;
	if(strB.length>0) bolBsq=true; else bolAgregar=true;

	for(intI=0;intI<intMax;intI++)
	{
		if (bolBsq){
		//alert("Busca " + strB + " contra " + arrD[intI][0] + " substracto = " + arrD[intI][0].substr(0,intLCmp) + "  longitud = " + intLCmp);
			if(arrD[intI][0].substr(0,intLCmp)==strB)
				bolAgregar = true;
			else
				bolAgregar = false;
		}
		if (bolAgregar) {
			if(cboD.length==0 && arrD[intI][1].length>0)
				if(typeof(cboD.strFrase) != "undefined" && cboD.strFrase.length>0)
					cboD.options[0] = new Option(cboD.strFrase,"","False","False");
			var strDesc = arrD[intI][1];
			var strClave = arrD[intI][0];
			if(strDesc.indexOf("||")>=0) {
			    strClave = strDesc.substring(0, strDesc.indexOf("||"));
			    strDesc = strDesc.substr(strDesc.indexOf("||")+2);
			}
			cboD.options[cboD.length] = new Option(strDesc, strClave,"False","False");
		}
	}
	if(cboD.length==0 && typeof(cboD.strFraseVacio) != "undefined" && cboD.strFraseVacio.length>0)
		cboD.options[0] = new Option(cboD.strFraseVacio,"","False","False");
	if(isNaN(parseInt(intSelec))) intSelec = 0;
	if(cboD.length>0)
		cboD.options[intSelec].selected=true;
}




function clsCbo() {
	var bolCboP = false;
	var bolCboE = false;
	var bolCboC = false;
	var bolCboT = false;
	var bolCboS = false;

	this.arrPai = new Array();
	this.arrEst = new Array();
	this.arrCiu = new Array();
	this.arrTnd = new Array();
	this.arrSuc = new Array();
	
	this.cboP = null;
	this.cboE = null;
	this.cboC = null;
	this.cboT = null;
	this.cboS = null;

	this.gAP = function(strClv, strValor){genArrAgrega(this.arrPai, strClv, strValor);}
	this.gAE = function(strClv, strValor){genArrAgrega(this.arrEst, strClv, strValor);}
	this.gAC = function(strClv, strValor){genArrAgrega(this.arrCiu, strClv, strValor);}
	this.gAT = function(strClv, strValor){genArrAgrega(this.arrTnd, strClv, strValor);}
	this.gAS = function(strClv, strValor, strValor2){genArrAgrega(this.arrSuc, strClv, strValor, strValor2);}
	
	this.A_BsqSelecciona = function(strTipo, cboD)
	{
		var strBsq, intS;

		switch(strTipo)
		{
			case 'E':
				strBsq = cboD.options[cboD.selectedIndex].value;
				pCboInicializa(this.cboC);
				pCboInicializa(this.cboT);
				A_BsqBusca(this.arrCiu, strBsq, 4, this.cboC);
				if(this.cboC.length==1 && strBsq.length>0) {
				    this.cboC.style.display="none";
				    //this.cboC.style.visibility="hidden";
				    this.A_BsqSelecciona('C', this.cboC);
				    }
				break;
			case 'C':
				strBsq = cboD.options[cboD.selectedIndex].value;
				pCboInicializa(this.cboT);
				A_BsqBusca(this.arrTnd, strBsq, 7, this.cboT);
				break;
			case 'T':
				strBsq = cboD.options[cboD.selectedIndex].value;
	//			A_BsqBuscaTiendaCompleto(strBsq, 11, gObj("celInfo"));
				//intS = A_BsqBusca(arrSuc, strBsq, 11, objFB.cboS);
				//if (intS==1){
					//alert("muestra");
				//}
				break;
		}
	};
	
	this.InicializaCbos = function() {
	    if(this.cboP!=null) pCboInicializa(this.cboP);
	    if(this.cboE!=null) pCboInicializa(this.cboE);
	    if(this.cboC!=null) pCboInicializa(this.cboC);
	    if(this.cboT!=null) pCboInicializa(this.cboT);
	    if(this.cboS!=null) pCboInicializa(this.cboS);
	}
	
	this.LlenaCbo = function(intTipo) {
		switch(intTipo) {
			case 0: pAgrCbo(this.arrPai, this.cboP, false); break;
			case 1: pAgrCbo(this.arrEst, this.cboE, false); break;
			case 2: pAgrCbo(this.arrCiu, this.cboC, false); break;
			case 3: pAgrCbo(this.arrTnd, this.cboT, false); break;
			case 4: pAgrCbo(this.arrSuc, this.cboS, false); break;
		}
	};
}

function genArrAgrega(arrD, strClv, strValor){ if(genArrAgrega.arguments.length==4) arrD[arrD.length] = new Array(strClv, strValor, genArrAgrega.arguments[3]); else arrD[arrD.length] = new Array(strClv, strValor); }








function gObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  if(!x && d.layers) for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=gObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function gDelegate(fn1, fn2){return function(){if(fn1)fn1();if(fn2)fn2();}}
