//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Fecha:  2007-01-11
// Updates:2007-01-11
// Author:Rodolfo Machon
// File Name: asws.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// VARIABLES
var lan;
var appid;
var clase;
var id;
var xmlHttp;
var xmlHttpStream;
var GridFormat;
var GridData;
var i2_dias_mora;
var d82_multa_minima;
var i2_digitos;
var RowRCD = new Array();
var RowVTar= new Array();
var RowITar= new Array();
var RowDataStruct  = new Array("PER","DIM","FIE","INT","MUL","FEC","REC","TIP","AGE","SUC");
var RowDataStruct1 = new Array("PER","DIM","TAR","VAL","FIE","INT","MUL");
var ZDigits  = new Array("0","0.0","0.00","0.000","0.0000","0.00000","0.000000");
var DDigits  = new Array("1","10","100","1000","10000","100000","1000000")
var GridParams	   = new Object();
var openwindow;
var openwindow1;
var FITariff="";
if (window.ActiveXObject){
	var GridHeadXML = new ActiveXObject("Microsoft.XMLDOM");
	var GridBodyXML = new ActiveXObject("Microsoft.XMLDOM");
	var GridFootXML = new ActiveXObject("Microsoft.XMLDOM");
	var BillingXML  = new ActiveXObject("Microsoft.XMLDOM");
	var PropXML  = new ActiveXObject("Microsoft.XMLDOM");
}else if (document.implementation && document.implementation.createDocument){
	var GridHeadXML = document.implementation.createDocument("","",null);
	var GridBodyXML = document.implementation.createDocument("","",null);
	var GridFootXML = document.implementation.createDocument("","",null);
	var BillingXML  = document.implementation.createDocument("","",null);
	var PropXML	= document.implementation.createDocument("","",null);
}else{
	alert('Your browser cannot handle this script');
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 1
function GetPageParams(){
	GetParams();
	GetGrid();	
}
function ConstructPage(){
	var Gens  = Generales.split("|");
	i2_dias_mora	= Gens[0];
	d82_multa_minima= Gens[1];
	i2_digitos	= Gens[2];
	balance_type	= Gens[3];	
	ConstructPageHeader();
	ConstructPageGrid();
}
function ClearGrid(chk){
	if(chk==true){
		DeleteGrid();
		document.getElementById('Grid').deleteTHead();
		document.getElementById('Grid').deleteTFoot();
		GetGrid();
	}else{
		DeleteGrid();
		document.getElementById('Grid').deleteTFoot();
		ConstructGridFooter();
		GetTotal();
	}
}
function ClearRow(objRef){
	var chk_name	= objRef.name;
	var chk_indx	= objRef.name.split("_");
	var Rows	= GridData.split("~~");
	var RowColsData	= GetRowColsData(Rows[chk_indx[1]-1]).split("^^");
	var RowRCD1	= RowColsData[0].split("|");
	var RowVTar1	= RowColsData[1].split("|");
	var RowITar1	= RowColsData[2].split("|");
	var ATributos	= Tributos.split("|");
	for (var p=0;p<ATributos.length-1;p++ ) {
		var ATributoMembers = ATributos[p].split(",");
		var Trib = ATributoMembers[0].substr(0,8);
		if(!eval("document.form."+chk_name+".checked")){
			eval("document.form.TAR_"+Trib+"_"+chk_indx[1]+".value='"+ZDigits[i2_digitos]+"'")
		}else{
			for(var r=0;r<RowITar1.length;r++){
				if(RowITar1[r].substr(0,8)==Trib){
					if(RowVTar1[r]==""){RowVTar1[r]=ZDigits[i2_digitos]}
					eval("document.form.TAR_"+Trib+"_"+chk_indx[1]+".value='"+RowVTar1[r]+"'")
				}	
			}
		}
	}
	var AAccesorios = Accesorios.split("|");
	for (var q=0;q<AAccesorios.length-1;q++ ) {
		var AAccesorioMembers = AAccesorios[q].split(",");
		var Acce = AAccesorioMembers[0].substr(5,3);
		if(!eval("document.form."+chk_name+".checked")){
			eval("document.form.ACC_"+Acce+"_"+chk_indx[1]+".value='"+ZDigits[i2_digitos]+"'")
		}else{
			if(RowRCD1[q+2]==""){RowRCD1[q+2]=ZDigits[i2_digitos]}
			if(eval("document.form.ACC_"+AAccesorioMembers[1]+"_chk.checked")){eval("document.form.ACC_"+Acce+"_"+chk_indx[1]+".value='"+RowRCD1[q+2]+"'")}
		}
	}
	
	var RowStream	= GetRowDefinition('GridBodyXML','body').split("^^");
	var RowDef	= RowStream[0];
	var RowStyle	= RowStream[1];
	var RowPro	= RowStream[2];
	CalculateRow(chk_indx[1],RowPro);
	
	document.getElementById('Grid').deleteTFoot();
	ConstructGridFooter();
	GetTotal();
}
function ClearColumn(objRef){
	var chk_name = objRef.name;
	var chk_indx = objRef.name.split("_");
	var Rows     = document.getElementById('Grid').rows.length;
	if(Rows>0){
		var Trib      = "";
		var Acce      = "";
		var tindex    = "";
		var aindex    = "";		
		var Rows1     = GridData.split("~~");
		var RowStream = GetRowDefinition('GridBodyXML','body').split("^^");
		var RowDef    = RowStream[0];
		var RowStyle  = RowStream[1];
		var RowPro    = RowStream[2];
		
		var OTributo  = new Object();
		var ATributos = Tributos.split("|");
		for (var p=0;p<ATributos.length;p++ ) {
			var ATributoMembers = ATributos[p].split(",");
			OTributo[ATributoMembers[1]] = ATributoMembers[0];
			if(chk_indx[1]==ATributoMembers[1]){tindex=p;}
		}

		var OAccesorio	= new Object();
		var AAccesorios	= Accesorios.split("|");
		for (var q=0;q<AAccesorios.length;q++ ) {
			var AAccesorioMembers = AAccesorios[q].split(",");
			OAccesorio[AAccesorioMembers[1]] = AAccesorioMembers[0].substr(5,3);
			if(chk_indx[1].substr(0,3)==AAccesorioMembers[0].substr(5,3)){aindex=q;}			
		}
		
		if(chk_indx[0]=="TAR"){Trib = OTributo[chk_indx[1]]}
		if(chk_indx[0]=="ACC"){Acce = OAccesorio[chk_indx[1]]}
		for(var a=1;a<Rows-1;a++ ) {
			var RowColsData	= GetRowColsData(Rows1[a-1]).split("^^");
			var RowRCD1	= RowColsData[0].split("|");
			var RowVTar1	= RowColsData[1].split("|");
			var RowITar1	= RowColsData[2].split("|");
			if(!eval("document.form."+chk_name+".checked")){
				if(chk_indx[0]=="TAR"){
					var TAR_Value = eval("document.form.TAR_"+Trib+"_"+a+".value");
					for (var b=0;b<AAccesorios.length-1;b++ ) {
						var TAR_ACC_Value	= 0;
						var AAccesorioMembers1	= AAccesorios[b].split(",");
						var Acce1		= AAccesorioMembers1[0].substr(5,3);
						var ACC_Value 		= eval("document.form.ACC_"+Acce1+"_"+a+".value")*1;
						if(Acce1=="FIE"){TAR_ACC_Value = TAR_Value*GridParams[Acce1];}
						if(Acce1=="INT"){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams[Acce1]/365);}
						if(Acce1=="MUL"){
						      if(RowRCD1[1]>=1 && RowRCD1[0]<90){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams['M89']/365)}
						      if(RowRCD1[1]>=90){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams['M90']/365)}
						      if(RowRCD1[1]<1){TAR_ACC_Value = 0}
						}
						var NewACC_Value	= Math.abs(ACC_Value-TAR_ACC_Value);
						//alert(RowRCD1[1]+"<=>"+TAR_Value+"<=>"+ACC_Value+"<=>"+TAR_ACC_Value+"<=>document.form.ACC_"+Acce1+"_"+a+".value='"+NewACC_Value.toFixed(i2_digitos)+"'");
						if(eval("document.form.ACC_"+AAccesorioMembers1[1]+"_chk.checked")){eval("document.form.ACC_"+Acce1+"_"+a+".value='"+NewACC_Value.toFixed(i2_digitos)+"'")}
					}
					eval("document.form.TAR_"+Trib+"_"+a+".value='"+ZDigits[i2_digitos]+"'")
				}
				if(chk_indx[0]=="ACC"){eval("document.form.ACC_"+Acce+"_"+a+".value='"+ZDigits[i2_digitos]+"'")}
			}else{
				var TAR_Value1 = "";
				if(chk_indx[0]=="TAR"){
					for(var r=0;r<RowITar1.length;r++){
						if(RowITar1[r].substr(0,8)==Trib){
							if(RowVTar1[r]==""){RowVTar1[r]=ZDigits[i2_digitos]}
							eval("document.form.TAR_"+Trib+"_"+a+".value='"+RowVTar1[r]+"'")
							TAR_Value1 = RowVTar1[r];
						}	
					}
					for (var c=0;c<AAccesorios.length-1;c++ ) {
						var TAR_ACC_Value1	= 0;
						var AAccesorioMembers11	= AAccesorios[c].split(",");
						var Acce11		= AAccesorioMembers11[0].substr(5,3);
						var ACC_Value1 		= eval("document.form.ACC_"+Acce11+"_"+a+".value")*1;
						if(Acce11=="FIE"){TAR_ACC_Value1 = TAR_Value1*GridParams[Acce11];}
						if(Acce11=="INT"){TAR_ACC_Value1 = RowRCD1[1]*TAR_Value1*(GridParams[Acce11]/365);}
						if(Acce11=="MUL"){
						      if(RowRCD1[1]>=1 && RowRCD1[0]<90){TAR_ACC_Value1 = RowRCD1[1]*TAR_Value1*(GridParams['M89']/365)}
						      if(RowRCD1[1]>=90){TAR_ACC_Value1 = RowRCD1[1]*TAR_Value1*(GridParams['M90']/365)}
						      if(RowRCD1[1]<60){TAR_ACC_Value1 = 0}
						}
						var NewACC_Value1	= Math.abs(ACC_Value1+TAR_ACC_Value1);
						//alert(RowRCD1[1]+"<=>"+TAR_Value+"<=>"+ACC_Value+"<=>"+TAR_ACC_Value+"<=>document.form.ACC_"+Acce1+"_"+a+".value='"+NewACC_Value.toFixed(i2_digitos)+"'");
						if(eval("document.form.ACC_"+AAccesorioMembers11[1]+"_chk.checked")){eval("document.form.ACC_"+Acce11+"_"+a+".value='"+NewACC_Value1.toFixed(i2_digitos)+"'")}
					}
				}
				if(chk_indx[0]=="ACC"){
					for (var c=0;c<AAccesorios.length-1;c++ ) {
						var STT_Value	= eval("document.form.STT_"+a+".value")*1;
						var AAccesorioMembers11	= AAccesorios[c].split(",");
						var Acce11		= AAccesorioMembers11[0].substr(5,3);
						var ACC_Value1 		= eval("document.form.ACC_"+Acce11+"_"+a+".value")*1;
						if(Acce11=="FIE"){TAR_ACC_Value1 = STT_Value*GridParams[Acce11];}
						if(Acce11=="INT"){TAR_ACC_Value1 = RowRCD1[1]*STT_Value*(GridParams[Acce11]/365);}
						if(Acce11=="MUL"){
						      if(RowRCD1[1]>=1 && RowRCD1[0]<90){TAR_ACC_Value1 = RowRCD1[1]*STT_Value*(GridParams['M89']/365)}
						      if(RowRCD1[1]>=90){TAR_ACC_Value1 = RowRCD1[1]*STT_Value*(GridParams['M90']/365)}
						      if(RowRCD1[1]<60){TAR_ACC_Value1 = 0}
						}
						var NewACC_Value1	= Math.abs(TAR_ACC_Value1);
						//alert("document.form.ACC_"+Acce11+"_"+a+".value='"+NewACC_Value1.toFixed(i2_digitos)+"'");
						if(eval("document.form.ACC_"+AAccesorioMembers11[1]+"_chk.checked")){eval("document.form.ACC_"+Acce11+"_"+a+".value='"+NewACC_Value1.toFixed(i2_digitos)+"'")}
					}					
				}				
			}
			CalculateRow(a,RowPro);
		}
		document.getElementById('Grid').deleteTFoot();
		ConstructGridFooter();
		GetTotal();		
	}
}
function ChangeCell(objRef){
	var tindex	= "";	
	var chk_name	= objRef.name;
	var chk_indx	= objRef.name.split("_");
	var Rows	= GridData.split("~~");
	var RowColsData	= GetRowColsData(Rows[chk_indx[2]-1]).split("^^");
	var RowRCD1	= RowColsData[0].split("|");
	var RowVTar1	= RowColsData[1].split("|");
	var RowITar1	= RowColsData[2].split("|");
	var TAR_Value	= eval("document.form."+chk_name+".value")*1;
	var ATributos	= Tributos.split("|");
	var OTributo	= new Object();
	for (var p=0;p<ATributos.length;p++ ) {
		var ATributoMembers = ATributos[p].split(",");
		OTributo[ATributoMembers[1]] = ATributoMembers[0];
		if(chk_indx[2]==ATributoMembers[1]){tindex=p;}
	}	
	var TAR_Value1	= RowVTar1[tindex]*1;
	var AAccesorios	= Accesorios.split("|");	
	for (var b=1;b<AAccesorios.length-1;b++ ) {
		var NewACC_Value	= 0;
		var TAR_ACC_Value	= 0;
		var AAccesorioMembers1	= AAccesorios[b].split(",");
		var Acce1		= AAccesorioMembers1[0].substr(5,3);
		var ACC_Value 		= eval("document.form.ACC_"+Acce1+"_"+chk_indx[2]+".value")*1;
		if(Acce1=="FIE"){TAR_ACC_Value = TAR_Value*GridParams[Acce1];}
		if(Acce1=="INT"){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams[Acce1]/365);}
		if(Acce1=="MUL"){
		      if(RowRCD1[1]>=1 && RowRCD1[0]<90){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams['M89']/365)}
		      if(RowRCD1[1]>=90){TAR_ACC_Value = RowRCD1[1]*TAR_Value*(GridParams['M90']/365)}
		      if(RowRCD1[1]<60){TAR_ACC_Value = 0}
		}
		if((TAR_Value1-TAR_Value)>=0){
			NewACC_Value  = Math.abs(ACC_Value-TAR_ACC_Value);
		}else{
			NewACC_Value  = Math.abs(ACC_Value-TAR_ACC_Value);				
		}
		eval("document.form.ACC_"+Acce1+"_"+chk_indx[2]+".value='"+NewACC_Value.toFixed(i2_digitos)+"'");
	}
	var RowStream	= GetRowDefinition('GridBodyXML','body').split("^^");
	var RowDef	= RowStream[0];
	var RowStyle	= RowStream[1];
	var RowPro	= RowStream[2];
	CalculateRow(chk_indx[2],RowPro);
	document.getElementById('Grid').deleteTFoot();
	ConstructGridFooter();
	GetTotal();	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 2
function GetParams(){
    var query   = window.location.search.substring(1);
    var params	= query.split("&");
    var cl      = params[0].split("=");
    var it      = params[1].split("=");    
    clase	= cl[1];
    id		= it[1];    
}
function GetGrid(){
	//var dtv = document.form.dt_vencimiento.value;
	//var dtf = document.form.dt_futuro.value;
	//if(!document.form.paga.checked){document.form.tp.value="NORMAL"}
	var tp	= document.form.tp.value;
	//var URL	= "../bin/aswsp.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&dt_vencimiento="+dtv+"&dt_futuro="+dtf+"&tp="+tp;
	var URL	= "../bin/aswsp.pl?clase="+clase+"&id="+id;
	//alert(URL);
	xmlHttp	= GetXmlHttpObject(StateChanged);
	xmlHttp.open("GET",URL,true) ;
	xmlHttp.send(null);
}
function ConstructPageHeader(){

}
function ConstructPageGrid(){
	ConstructGridHeader();
	ConstructGridBody();
	ConstructGridFooter();
	//GetGridParams();
	//GetTotal();
}
function DeleteGrid(){
	var Rows = document.getElementById('Grid').rows.length;
	if(Rows>0){
		for(var a=1;a<Rows;a++ ) {
			document.getElementById('Grid').deleteRow(1);
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 3
function ConstructGridHeader(){
	var GridHeader	 	= GridHeadXML.getElementsByTagName('head')[0];
	var HeaderCols 	 	= GridHeader.getElementsByTagName("columns")[0].childNodes[0].nodeValue;
	var Columns	 	= HeaderCols.split(",");
	var RowCells 	 	= new Array(Columns.length);	
	var NewColumn	 	= new Array(Columns.length);
	for(var a=0;a<Columns.length;a++ ) {
		var Column	= GridHeader.getElementsByTagName("column")[a];
		var ColumnName	= Column.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		for(var b=0;b<Columns.length;b++ ) {
			if(ColumnName==Columns[b]){
				NewColumn[b] = GridHeader.getElementsByTagName("column")[a];
			}
		}
	}
	var NewColumnElements	= new Array(Columns.length);
	for(var f=0;f<Columns.length;f++ ) {
		var Elements		= NewColumn[f].getElementsByTagName("elements")[0].childNodes[0].nodeValue.split(",");
		NewColumnElements[f]	= new Array(Elements.length);
		for(var g=0;g<Elements.length;g++ ) {
			var ElementL	= NewColumn[f].getElementsByTagName("element");
			for(var h=0;h<ElementL.length;h++ ) {			
				var ElementName	= ElementL[h].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				if(Elements[g]==ElementName){
					NewColumnElements[f][g] = ElementL[h];
				}
			}
		}		
	}
	var Grid	= document.getElementById('Grid');
	var newTHEAD	= Grid.createTHead();
	//var GridRow	= Grid.insertRow(0);
	var GridRow	= newTHEAD.insertRow(0);	
	var CCells 	= new Array(Columns.length);
	var p		= 0;
	GridRow.style.cssText   = GridHeader.getElementsByTagName("style")[0].childNodes[0].nodeValue;
	for(var k=0;k<Columns.length;k++ ) {
		var CInnerHTML	= "";		
		var CName   = NewColumn[k].getElementsByTagName("name")[0].childNodes[0].nodeValue;
		var CData   = NewColumn[k].getElementsByTagName("data")[0].childNodes[0].nodeValue;
		var CHeader = NewColumn[k].getElementsByTagName("header")[0].childNodes[0].nodeValue;
		var CType   = NewColumn[k].getElementsByTagName("type")[0].childNodes[0].nodeValue;
		var CWidth  = NewColumn[k].getElementsByTagName("width")[0].childNodes[0].nodeValue;
		Elements    = NewColumn[k].getElementsByTagName("elements")[0].childNodes[0].nodeValue.split(",");
		for(var l=0;l<Elements.length;l++ ) {
			var EName    = "";
			var EControl = "";
			var EType    = "";
			var ECaption = "";
			var ESize    = "";
			var EMax     = "";
			var EEventos = "";
			if(NewColumnElements[k][l].getElementsByTagName("name")[0]){EName = NewColumnElements[k][l].getElementsByTagName("name")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("control")[0]){EControl = NewColumnElements[k][l].getElementsByTagName("control")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("type")[0]){EType = NewColumnElements[k][l].getElementsByTagName("type")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("caption")[0]){ECaption = NewColumnElements[k][l].getElementsByTagName("caption")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("size")[0]){ESize = NewColumnElements[k][l].getElementsByTagName("size")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("maxlength")[0]){EMax = NewColumnElements[k][l].getElementsByTagName("maxlength")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("events")[0]){
				var EEvents  = NewColumnElements[k][l].getElementsByTagName("events")[0];
				for(var m=0;m<EEvents.childNodes.length;m++ ) {
					var Event  = EEvents.childNodes[m].nodeName;
					var Method = EEvents.childNodes[m].childNodes[0].nodeValue;
					EEventos  += Event+"="+Method+" ";
					//alert(EName+"=>"+Event+"="+Method)
				}
			}
			//alert(CName+"=>"+k+"<>"+l+"=>"+EControl+"=>"+EName+"=>"+EType+"=>"+ESize+"=>"+EMax+"=>"+ECaption+"=>"+EEventos);
			CInnerHTML  += ConstructElement(EControl,EName,EType,ESize,EMax,ECaption,EEventos);

		}
		if(CName=="tributos"){
			var ATributos = Tributos.split("|");
			for(var n=p;n<ATributos.length+p-1;n++ ) {
				var PTributo	     = ATributos[n-p].split(",");
				var NewCInnerHTML    = CInnerHTML.replace(/{tributo}/g,PTributo[1]);
				CCells[n]  	     = GridRow.insertCell(n);
				CCells[n].innerHTML  = NewCInnerHTML;
				//alert(CCells[n].innerHTML);
				CCells[n].setAttribute('width',CWidth[n])
			}
			p=n;
		}else if(CName=="accesorios"){
			var AAccesorios = Accesorios.split("|");

			for(var o=p;o<AAccesorios.length+p-1;o++ ) {
				var PAccesorio	     = AAccesorios[o-p].split(",");
				var NewCInnerHTML1   = CInnerHTML.replace(/{accesorio}/g,PAccesorio[1]);
				CCells[o]  	     = GridRow.insertCell(o);
				CCells[o].innerHTML  = NewCInnerHTML1;
				//alert(CCells[o].innerHTML);
				CCells[o].setAttribute('width',CWidth[o])
			}
			p=o;
		}else{
			CCells[p]  	    = GridRow.insertCell(p);
			CCells[p].innerHTML = CInnerHTML;
			//alert(CCells[p].innerHTML);
			CCells[p].setAttribute('width',CWidth[p])
			p++;
		}
	}
}
function ConstructGridBody(){
	var RowStream	= GetRowDefinition('GridBodyXML','body').split("^^");
	var RowDef	= RowStream[0];
	var RowStyle	= RowStream[1];
	var RowPro	= RowStream[2];
	var Rows	= GridData.split("~~");
	for(var i=1;i<Rows.length;i++ ) {
		AddRow(i,Rows[i-1],RowDef,RowStyle);
		CalculateRow(i,RowPro);
	}	
}
function ConstructGridFooter(){
	var z 		= 0;
	var RowStream	= GetRowDefinition('GridFootXML','foot').split("^^");
	var RowColsDef	= RowStream[0].split("~~");
	var RowColsPro  = RowStream[2].split("~~");
	var RowStyle	= RowStream[1];
	var FIT 	= Tributos.split("|");	
	var CCells  	= new Array(RowColsDef.length);	
	var Grid    	= document.getElementById('Grid');
	var Rows 	= document.getElementById('Grid').rows.length-1;
	var newTFOOT	= Grid.createTFoot();
	NewRow   	= newTFOOT.insertRow(-1);
	NewRow.style.cssText = RowStyle;
	for(var a=0;a<RowColsDef.length-1;a++ ) {
		var NewCInnerHTML = RowColsDef[a];
		var NewColsPro	  = RowColsPro[a];
		for(var c=z;c<FIT.length-1;c++ ) {
			var Ocurrence = NewCInnerHTML.indexOf("{TAR}");
			if(Ocurrence>=0){
				var FIT_c = FIT[c].split(",");
				NewCInnerHTML = NewCInnerHTML.replace(/{TAR}/g,FIT_c[0]);
				NewColsPro    = NewColsPro.replace(/{TAR}/g,FIT_c[0]);
				z++;
			}
		}
		CCells[a]  	     = NewRow.insertCell(a);
		CCells[a].innerHTML  = NewCInnerHTML;
		if(RowColsPro[a]!=""){
			var NEC = eval(NewColsPro)
			if(NEC==false){CCells[a].innerHTML=ZDigits[i2_digitos]}
		}
	}	
}
function GetGridParams(){
	var AAP = AccesoriosParams.split("~~");
	for (var a=0;a<AAP.length;a++ ) {
		var AAPMembers = AAP[a].split("|");
		if(AAPMembers.length>2){
			var M89 = AAPMembers[1].split("=");
			var M90 = AAPMembers[2].split("=");			
			GridParams['M89'] = M89[1];
			GridParams['M90'] = M90[1];
		}else{
			GridParams[AAPMembers[0]] = AAPMembers[1];
		}
	}
}
function GetTotal(){
	if(document.form.TTOT){
		var TOTAL1 = document.form.TTOT.value*1;
		var TOTAL=Math.round(TOTAL1*DDigits[i2_digitos])/DDigits[i2_digitos];
		document.form.us_total.value = TOTAL.toFixed(i2_digitos)}else{document.form.us_total.value=ZDigits[i2_digitos]};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 4
function AddRow(indx,Row,RowDef,RowStyle){
	var z = 0;
	var RowColsDef 	= RowDef.split("~~");
	var RowColsData	= GetRowColsData(Row).split("^^");
	RowRCD		= RowColsData[0].split("|");
	RowVTar		= RowColsData[1].split("|");
	RowITar		= RowColsData[2].split("|");
	var SRowITar	= RowITar.join(",");
	var Grid    	= document.getElementById('Grid');
	var GridRow 	= Grid.insertRow(indx);
	var CCells  	= new Array(RowColsDef.length);
	var ATributos 	= Tributos.split("|");	
	GridRow.style.cssText = RowStyle;
	for(var a=0;a<RowColsDef.length-1;a++ ) {
		var NewCInnerHTML  = RowColsDef[a];
		for(var b=0;b<RowRCD.length-1;b++ ) {
			var NewDataStruct = "{"+RowDataStruct[b]+"}";
			var TReplace 	  = new RegExp(NewDataStruct, "g");
			NewCInnerHTML     = NewCInnerHTML.replace(TReplace,RowRCD[b]);
		}
		var Ocurrence = NewCInnerHTML.indexOf("{TAR}");
		if(Ocurrence>=0){
			var PTributo = ATributos[z].split(",");
			for(var d=0;d<RowITar.length-1;d++ ) {
				if(PTributo[0]==RowITar[d].substr(0,8)){
					NewCInnerHTML = NewCInnerHTML.replace(/{VAL}/g,RowVTar[d]);
					NewCInnerHTML = NewCInnerHTML.replace(/{TAR}/g,RowITar[d].substr(0,8));
				}
			}
			NewCInnerHTML = NewCInnerHTML.replace(/{VAL}/g,ZDigits[i2_digitos]);
			NewCInnerHTML = NewCInnerHTML.replace(/{TAR}/g,PTributo[0]);
			z++;
		}
		CCells[a]  	     = GridRow.insertCell(a);
		CCells[a].innerHTML  = NewCInnerHTML.replace(/{indx}/g,indx);
	}	
}
function CalculateRow(indx,RowPro){
	var RowColsPro = RowPro.split("~~");
	for(var a=0;a<RowColsPro.length-1;a++ ) {
		var NewColsPro = RowColsPro[a].replace(/{indx}/g,indx);
		if(RowColsPro[a]!=""){
			//alert(NewColsPro);
			eval(NewColsPro)
		};
	}
}
function GetRowDefinition(xml,part){
	var GridSection	 	= eval(xml+".getElementsByTagName('"+part+"')[0]");
	var SectionCols 	= GridSection.getElementsByTagName("columns")[0].childNodes[0].nodeValue;
	var Columns	 	= SectionCols.split(",");
	var RowCells 	 	= new Array(Columns.length);	
	var NewColumn	 	= new Array(Columns.length);
	for(var a=0;a<Columns.length;a++ ) {
		var Column	= GridSection.getElementsByTagName("column")[a];
		var ColumnName	= Column.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		for(var b=0;b<Columns.length;b++ ) {
			if(ColumnName==Columns[b]){
				NewColumn[b] = GridSection.getElementsByTagName("column")[a];
			}
		}
	}
	var NewColumnElements	= new Array(Columns.length);
	for(var f=0;f<Columns.length;f++ ) {
		var Elements		= NewColumn[f].getElementsByTagName("elements")[0].childNodes[0].nodeValue.split(",");
		NewColumnElements[f]	= new Array(Elements.length);
		for(var g=0;g<Elements.length;g++ ) {
			var ElementL	= NewColumn[f].getElementsByTagName("element");
			for(var h=0;h<ElementL.length;h++ ) {			
				var ElementName	= ElementL[h].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				if(Elements[g]==ElementName){
					NewColumnElements[f][g] = ElementL[h];
				}
			}
		}		
	}
	var RowDef	= "";
	var RowStyle	= GridSection.getElementsByTagName("style")[0].childNodes[0].nodeValue;
	var RowPro	= "";
	var p		= 0;
	for(var k=0;k<Columns.length;k++ ) {
		var CInnerHTML	= "";		
		var CProcess	= "";
		var CName	= NewColumn[k].getElementsByTagName("name")[0].childNodes[0].nodeValue;
		var CData	= NewColumn[k].getElementsByTagName("data")[0].childNodes[0].nodeValue;
		var CHeader	= NewColumn[k].getElementsByTagName("header")[0].childNodes[0].nodeValue;
		var CType	= NewColumn[k].getElementsByTagName("type")[0].childNodes[0].nodeValue;
		var CWidth	= NewColumn[k].getElementsByTagName("width")[0].childNodes[0].nodeValue;
		if(NewColumn[k].getElementsByTagName("process")[0]){CProcess = NewColumn[k].getElementsByTagName("process")[0].childNodes[0].nodeValue;}		
		Elements	= NewColumn[k].getElementsByTagName("elements")[0].childNodes[0].nodeValue.split(",");
		for(var l=0;l<Elements.length;l++ ) {
			var EName="";var EControl="";var EType="";var ECaption="";var ESize="";var EMax="";var EEventos="";var EVal="";
			if(NewColumnElements[k][l].getElementsByTagName("name")[0]){EName = NewColumnElements[k][l].getElementsByTagName("name")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("control")[0]){EControl = NewColumnElements[k][l].getElementsByTagName("control")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("type")[0]){EType = NewColumnElements[k][l].getElementsByTagName("type")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("caption")[0]){ECaption = NewColumnElements[k][l].getElementsByTagName("caption")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("size")[0]){ESize = NewColumnElements[k][l].getElementsByTagName("size")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("maxlength")[0]){EMax = NewColumnElements[k][l].getElementsByTagName("maxlength")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("value")[0]){EVal = NewColumnElements[k][l].getElementsByTagName("value")[0].childNodes[0].nodeValue;}
			if(NewColumnElements[k][l].getElementsByTagName("events")[0]){
				var EEvents  = NewColumnElements[k][l].getElementsByTagName("events")[0];
				for(var m=0;m<EEvents.childNodes.length;m++ ) {
					var Event  = EEvents.childNodes[m].nodeName;
					var Method = EEvents.childNodes[m].childNodes[0].nodeValue;
					EEventos  += Event+"="+Method+" ";
					//alert(EName+"=>"+Event+"="+Method)					
				}
			}
			//alert(CName+"=>"+k+"<>"+l+"=>"+EControl+"=>"+EName+"=>"+EType+"=>"+ESize+"=>"+EMax+"=>"+ECaption+"=>"+EEventos);
			//alert(ESize+"=>"+EMax);	
			CInnerHTML  += ConstructElement(EControl,EName,EType,ESize,EMax,ECaption,EEventos,EVal);
		}
		if(CName=="tributos"){
			var ATributos = Tributos.split("|");
			for(var n=p;n<ATributos.length+p-1;n++ ) {
				var PTributo	     = ATributos[n-p].split(",");
				var NewCInnerHTML    = CInnerHTML.replace(/{tributo}/g,PTributo[1]);
				RowDef += NewCInnerHTML+"~~";
				RowPro += CProcess+"~~";				
			}
			p=n;
		}else if(CName=="accesorios"){
			var AAccesorios = Accesorios.split("|");
			for(var o=p;o<AAccesorios.length+p-1;o++ ) {
				var PAccesorio	     = AAccesorios[o-p].split(",");
				var CAccesorio	     = PAccesorio[0].substr(5,3)
				var NewCInnerHTML1   = CInnerHTML.replace(/{ACC}/g,CAccesorio);
				var NewCProcess1     = CProcess.replace(/{ACC}/g,CAccesorio);				
				RowDef += NewCInnerHTML1+"~~";
				RowPro += NewCProcess1+"~~";
			}
			p=o;
		}else{
			RowDef += CInnerHTML+"~~";			
			RowPro += CProcess+"~~";
			p++;
		}
	}	
	return RowDef+"^^"+RowStyle+"^^"+RowPro;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 5
function ConstructElement(control,name,type,size,max,caption,events,val){
	var line = "";
	if(control=="INPUT"){
		line += "<INPUT  ";
		//line += "name="+name+"_{indx}";
		line += "name='"+name+"'";
		if(type=="TEXT"){
			line += " type='text'";
			if(val!=""){line += " value='"+val+"'";}
			line += " size='"+size+"'";
			line += " maxlength='"+max+"'";
			line += " "+events;
			line += " class=my_style0";
		}
		if(type=="CHECKBOX"){
			line += " type=checkbox";
			line += " "+events;
			line += " class=checkbox";
			//line += " checked";
		}
		if(type=="HIDDEN"){
			line  = " type=hidden";
		}
		line += ">";
	}
	if(control=='LINK'){line += "<A name='"+name+"' href='#' "+events+" class=my_style>"+caption+"</A>";}
	if(control=='SELECT'){}
	if(control=='TEXTAREA'){}
	if(control=='TEXT'){
		line += caption;
	}
	return line;
}
function GetRowColsData(Row){
	var RowData = Row.split("&&");
	var VTariff = "";
	var ITariff = "";
	var RCD = "";
	var PER	= "";
	var DIM = "";
	var TAR = "";
	var VAL = "";	
	var FIE = 0;
	var INT = 0;
	var MUL = 0;
	var FEC = "";
	var REC = "";
	var TIP = "";
	var AGE = "";
	var SUC = "";
	for(var a=0;a<RowData.length-1;a++ ) {
		var ColsData = RowData[a].split("|");
		PER  	 = ColsData[0];
		DIM  	 = ColsData[1];
		ITariff += ColsData[2]+"|";		
		VTariff += ColsData[3]+"|";
		FIE 	+= ColsData[4]*1;
		INT 	+= ColsData[5]*1;
		MUL 	+= ColsData[6]*1;
		FEC  	 = ColsData[7];
		REC  	 = ColsData[8];
		TIP  	 = ColsData[9];
		AGE  	 = ColsData[10];
		SUC  	 = ColsData[11];
	}
	var IT_length  = ITariff.split("|").length;
	var FIT_length = FITariff.split("|").length;
	//alert(IT_length+"="+ITariff+"<=>"+FIT_length+"="+FITariff)
	//if(IT_length>FIT_length){FITariff+=ITariff}
	if(IT_length>=FIT_length){FITariff=ITariff}	
	RCD = PER+"|"+DIM+"|"+FIE.toFixed(i2_digitos)+"|"+INT.toFixed(i2_digitos)+"|"+MUL.toFixed(i2_digitos)+"|"+FEC+"|"+REC+"|"+TIP+"|"+AGE+"|"+SUC+"|";
	return RCD+"^^"+VTariff+"^^"+ITariff;
}
function RowSum(indx,addends,sum){
	var Acumulated	= 0;
	var ExecStream 	= "";
	var IA 		= addends.substr(0,1);
	if(IA=="@"){
		var AD = addends.substr(2,addends.length-3).split(",");
		for (var a=0;a<AD.length;a++){
			var Value  = eval("document.form."+AD[a]+"_"+indx+".value");
			Acumulated = (Acumulated*1)+(Value*1);
		}
	}else{
		if(addends=="TAR"){
			//alert(RowITar.length);
			//for (var b=0;b<RowITar.length-1;b++){			
			var FIT = Tributos.split("|");	
			for (var b=0;b<FIT.length-1;b++){
				var FIT_c = FIT[b].split(",");
				//var Value1 = eval("document.form."+addends+"_"+RowITar[b].substr(0,8)+"_"+indx+".value");
				var Value1 = eval("document.form."+addends+"_"+FIT_c[0]+"_"+indx+".value");				
				Acumulated = (Acumulated*1)+(Value1*1);			
			}
		}
		if(addends=="ACC"){
			var AA = Accesorios.split("|");
			for (var c=0;c<AA.length-1;c++){
				var AAC	   = AA[c].split(",");
				var CA     = AAC[0].substr(5,3);
				var Value2 = eval("document.form."+addends+"_"+CA+"_"+indx+".value");
				Acumulated = (Acumulated*1)+(Value2*1);
			}
		}
	}
	ExecStream = "document.form."+sum+".value='"+Acumulated.toFixed(i2_digitos)+"'";
	eval(ExecStream);
}
function InitGrid(){
	var records = parent.registros.document.detail.dsv.value.split("~~");
	for(var i=1;i<records.length;i++ ) {
		InitRow(i,records[i]);
	}
}
function CalculateGrid(){
	CalculateRows();
	CalculateColums();
	CalculateTotal();
}
function InitRow(indx,record){
	var rcd_members = record.split("~~");
	for(var i=1;i<rcd_members.length;i++ ) {
		
	}	
}
function CalculateRows(){
	var records	   = parent.registros.document.detail.dsv.value.split("~~");
	for(var i=1;i<records.length;i++ ) {
		CalculateRow(i);
	}	
}
function CalculateColumns(){
	var records	   = parent.registros.document.detail.dsv.value.split("~~");
	for(var i=1;i<records.length;i++ ) {
		CalculateColumn(col);
	}	
}
function CalculateColumn(col){

}
function ColSum(T,C){
	var Rows = document.getElementById('Grid').rows.length-1;
	var t = 0;
	var succ = false;
	if(Rows>1){
		for(var a=1;a<Rows;a++ ) {
			//alert("document.form."+T+"_"+a+".value");
			sub = eval("document.form."+T+"_"+a+".value");
			if(sub==""){sub=0}
			t += (sub*1);
		}
		eval("document.form."+C+".value='"+t.toFixed(i2_digitos)+"'");
		succ = true;
	}
	return succ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// AUXILIAR
function DescargarPeriodos(){
	var DescarPers 	= "";
	var Rows   	= GridData.split("~~");	
	var ok = confirm("Esta Segur@ que Desea Eliminar los Movimientos?");
	if(ok!=true){return;}
	if(Rows<1){alert(document.form.empty_grid.value);return}
	for(var k=1;k<Rows.length;k++ ) {
		if(eval("document.form.chk_"+k+".checked")){
			var RowsValues	= Rows[k-1].split("&&");
			var PER = eval("document.form.PER_"+k+".value");
			var FEC = eval("document.form.FEC_"+k+".value");
			var REC = eval("document.form.REC_"+k+".value");			
			var TIP = eval("document.form.TIP_"+k+".value");			
			DescarPers += PER+"|"+FEC+"|"+REC+"|"+TIP+"~~";
		}
	}
	document.descargar.Descargas.value = DescarPers;
	if(document.descargar.Descargas.value!=""){
		//alert(document.descargar.Descargas.value)
		document.descargar.submit();
	}else{ alert("No Hay Periodos Marcados a Eliminar")}
}
function CargarPeriodos(){
	var id_field = document.form.id_field.value;
	var id		 = eval("document.form."+id_field+".value");
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../pl/procesar_cargar_periodos_r.pl?lan="+lan+"&appid="+appid+"&id_field="+id_field+"&id="+id+"','CargarPeriodos','left=50,top=50,width=800,height=600,scrollbars=yes,toolbar=no,menu=no,statusbar=no')";
		openwindow1=eval(win);
	}
	else{openwindow1.focus()}
}
function MarkAll(){
	var T  			= document.getElementById('Grid');
	var trows 		= T.rows;
	for (i = trows.length-2; i != 0; i--) {
		if(eval("document.form.chk_"+i+".checked")){
			eval("document.form.chk_"+i+".checked=false")
		}else{
			eval("document.form.chk_"+i+".checked=true")			
		}
	}
}
function ReloadPage(){
	var dt_vencimiento = document.form.dt_vencimiento.value;
	var dt_futuro	   = document.form.dt_futuro.value;
	var frame 	   = "location.href='asws.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&dt_vencimiento="+dt_vencimiento+"&dt_futuro="+dt_futuro+"'";
	eval(frame);
}
function ShowCalendar(form,datetype,separator){
	if (!openwindow || openwindow.closed){
		win ="window.open('../libraries/html/clnd_asws.html?pbf="+form+"&datetype="+datetype+"&separator="+separator+"','Calendar','left=200,top=200,width=250,height=260,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function OpenWindow(winame,comm,fields,values){
	var commands	= comm.split(",");
	var id_field	= document.form.id_field.value;
	var id		= eval("document.form."+id_field+".value");
	if (!openwindow || openwindow.closed){
		win ="window.open('../bin/"+commands[0]+".pl?lan="+lan+"&appid="+appid+"&clase="+commands[1]+"&id="+id+"&fields="+fields+"&values="+values+"','"+commands[0]+commands[1]+"','left="+commands[2]+",top="+commands[3]+",width=800,height=600,scrollbars=yes,toolbar=no,menu=no,statusbar=no')";
		openwindow=eval(win);
	}
	else{openwindow.focus()}
}
function ShowPago(chk){
	if(chk==true){
		document.all.CalendarLink.style.visibility='visible';
	}else{
		document.all.CalendarLink.style.visibility='hidden';
	}
}
function GetFechaHoy(){
	var dia = today.getDate();
	var mes = (today.getMonth()*1)+1;
	var ano = today.getFullYear();
	if(String(dia).length<2){dia = "0"+dia}
	if(String(mes).length<2){mes = "0"+mes}
	return ano+"-"+mes+"-"+dia;
}
function StateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		xmlHttpStream=xmlHttp.responseText;
		//document.write(xmlHttpStream);		
		//alert("Account Statement=>\n"+xmlHttpStream);
		//alert("Balance");		
		//return;
		var Grid   = xmlHttpStream.split("^^");
		GridData   = Grid[0];
		//alert(Grid[0]);
		Tributos   = Grid[1];
		//alert(Tributos);
		Accesorios = Grid[2];
		//alert(Accesorios);
		Header 	   = Grid[3];
		//alert(Header);
		Body 	   = Grid[4];
		//alert(Body);
		Footer	   = Grid[5];
		
		Generales  = Grid[6];
		//alert(Generales);
		loadXML1();
	} 
}
function loadXML1(){
	if (window.DOMParser){
		parser = new DOMParser();
		GridHeadXML=parser.parseFromString(Header,"text/xml");
		GridBodyXML=parser.parseFromString(Body,"text/xml");
		GridFootXML=parser.parseFromString(Footer,"text/xml");
		//BillingXML=parser.parseFromString(Billing,"text/xml");
		ConstructPage();		
	}else {// Internet Explorer
		GridHeadXML.async=false;
		GridHeadXML.loadXML(Header);

		GridBodyXML.async=false;
		GridBodyXML.loadXML(Body);
		
		GridFootXML.async=false;
		GridFootXML.loadXML(Footer);
		
		//BillingXML.async=false;
		//BillingXML.loadXML(Billing);		

		ConstructPage(); 
	}	
}
function GetXmlHttpObject(handler){ 
    var objXmlHttp=null
    if (navigator.userAgent.indexOf("Opera")>=0){
	alert("This example doesn't work in Opera") 
	return 
    }
    if (navigator.userAgent.indexOf("MSIE")>=0){ 
	var strName="Msxml2.XMLHTTP"
	if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
	    strName="Microsoft.XMLHTTP"
	} 
	try{ 
	    objXmlHttp=new ActiveXObject(strName)
	    objXmlHttp.onreadystatechange=handler 
	    return objXmlHttp
	}catch(e){ 
	    alert("Error. Scripting for ActiveX might be disabled") 
	    return 
	} 
    } 
    if (navigator.userAgent.indexOf("Mozilla")>=0){
	objXmlHttp=new XMLHttpRequest()
	objXmlHttp.onload=handler
	objXmlHttp.onerror=handler 
	return objXmlHttp
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////