//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Fecha: 2006-03-20
// Updates:2006-03-20,2006-04-11
// Author:Rodolfo Machon
// Nombre del archivo: astm.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var acuerdos;
var BR;
var openwindow;
function ConstructTable(){
	ConstructTableColumns();
	ConstructResultColumns();
	//ConstructTableFooter();
}
function ConstructTableColumns(){
	var grid_body		= parent.list.document.detail.dgb.value.split("~~");
	var grid_header		= parent.list.document.detail.dgh.value.split("~~");
	var grid_footer		= parent.list.document.detail.dgf.value.split("~~");
	var labels_values	= parent.list.document.detail.dlv.value.split("~~");
	var labels_fields	= parent.list.document.detail.dlf.value.split(",");
	var elements		= parent.list.document.detail.del.value.split("~~");

	//var select_values	= parent.list.document.detail.dsv.value.split("~~");
	//var select_fields	= parent.list.document.detail.dsf.value.split(",");

	var header_length	= (grid_header.length*1);
	var Cells 		= new Array(header_length+2);
	var fields		= new Array(header_length);	
	var column_width	= new Array(header_length);	
	var column_type		= new Array(header_length);
	var column_events	= new Array(header_length);
	var column_elements	= new Array(header_length);	
	var k			= 0;
	var l			= 0;
	var m			= 0;
	
	var T  			= parent.form.document.getElementById('t1');
	var NewRow		= T.insertRow(0);
	NewRow.style.cssText 	= parent.list.document.detail.tdheader.value;

	if(parent.list.document.detail.counter.value=="ON"){
		Cells[k] = NewRow.insertCell(k);
		Cells[k].innerHTML = "NO.";
		k++;
		l++;
	}
	for (j=k;j<grid_header.length-1;j++){
		record			= grid_header[m].split("|");
		Cells[j]  		= NewRow.insertCell(j);
		fields[j]		= record[0];
		Cells[j].innerHTML	= record[1];
		column_width[j]		= record[2];
		column_type[j]		= record[3];
		column_events[j]	= record[4];
		column_elements[j]	= record[5];
		l++;
		m++;
	}
	if(parent.list.document.detail.dlt.value=="ON"){
		Cells[j] = NewRow.insertCell(j);
		Cells[j].innerHTML = "DELETE";
	}	
	for (var i=1;i<grid_body.length;i++){	
		k		= 0;
		l		= 0;
		var status	= true;
		var svalues 	= new Object;
		var lvalues 	= new Object;
		var srecord	= grid_body[i-1].split("|");
		var lrecord	= labels_values[i-1].split("|");		
		NewRow		= T.insertRow(i);
		//NewRow.attachEvent('onmouseover',Hilite);
		//NewRow.attachEvent('onmouseout',Restore);
		NewRow.style.cssText 	= parent.list.document.detail.tdbody.value;
		if(parent.list.document.detail.counter.value=="ON"){
			Cells[k] = NewRow.insertCell(k);
			if(parent.list.document.detail.component.value=="gdfl"){Cells[k].innerHTML = "&nbsp;&nbsp;<a name=a"+i+" href='javascript:MoveUp("+i+","+status+")'>"+i+"</a>";}
			if(parent.list.document.detail.component.value=="srch"){Cells[k].innerHTML = "&nbsp;&nbsp;<a name=a"+i+" href='javascript:GridPassBack("+i+")'>"+i+"</a>";}
			k++;
			l++;
		}
		for (j=k;j<grid_header.length-1;j++){
			var inHTML  	= srecord[j]; 
			var checked	= "";
			var link	= "";
			var elementos	= column_elements[j].split(",");
		
			for (x=0;x<elementos.length;x++){
				if(elementos[0]!='NO'){
					for (y=0;y<elements.length-1;y++){
						var element_members = elements[y].split("|");
						if(elementos[x]==element_members[0]){
							var event  = "";
							var params = "";						
							var element_type   = element_members[1];
							var element_data   = element_members[2];
							var element_events = element_members[3].split("^^");
							for (z=0;z<element_events.length-1;z++){
								var events = element_events[z].split("^");
								event += " "+events[0];
							}
							if(element_type=='input'){
							
							}else if(element_type=='link'){
								link += "&nbsp;<a name='"+element_members[0]+"__"+i+"' href='#' "+event+">"+inHTML+"</a>";
							}else if(element_type=='select'){
								
							}else if(element_type=='textarea'){}
						}
					}
					inHTML=link;
				}
			}			
			Cells[j]  		= NewRow.insertCell(j);
			Cells[j].innerHTML	= inHTML;
			l++;
		}
		if(parent.list.document.detail.dlt.value=="ON"){Cells[l] = NewRow.insertCell(l);Cells[l].innerHTML = "<input type=checkbox name="+i+">";}
	}
}
function ConstructResultColumns(){
	var grid_body		= parent.list.document.detail.dgb.value.split("~~");
	var grid_header		= parent.list.document.detail.dgh.value.split("~~");
	var grid_soft_header	= parent.list.document.detail.dsh.value.split("~~");
	var grid_result_columns	= parent.list.document.detail.drc.value.split("~~");

	var header_length	= (grid_result_columns.length*1);
	var Cells 		= new Array(header_length+2);
	var fields		= new Array(header_length);	
	var column_name		= new Array(header_length);	
	var column_properties	= new Array(header_length);
	var column_events	= new Array(header_length);
	var column_operations	= new Array(header_length);	
	
	for (j=0;j<grid_result_columns.length;j++){
		record			= grid_result_columns[j].split("|");
		column_name[j]		= record[0];
		column_properties[j]	= record[1];
		column_events[j]	= record[2];
		column_operations[j]	= record[3];
	}
	var col_indx 	= grid_header.length;
	for (var j=0;j<column_name.length-1;j++){
		var properties	= column_properties[j].split(",");
		var type	= properties[0];
		var data	= properties[1];
		var header	= properties[2];
		var width	= properties[3];
		col_indx	= col_indx+j;
		for (var i=0;i<grid_body.length;i++){
			var x = parent.form.document.getElementById('t1').rows[i];
			var y = x.insertCell(col_indx-1);
			if(i==0){
				y.innerHTML = header;
			}else{
				y.innerHTML = CalculateColumns(column_operations[j],i);
			}
		}
	}
}
function ReloadOpener(p){
	var lan		= parent.list.document.data.lan.value;
	var appid	= parent.list.document.data.appid.value;
	var clase	= parent.list.document.data.clase.value;
	var id		= parent.list.document.data.id.value;
	var frame 	= "parent.location.href='ASTM.exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&period_values="+p+"'";
	eval(frame);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function CalculateColumns(oper,indx){
	var calculation			= 0.00;
	var grid_header			= parent.list.document.detail.dgh.value.split("~~");
	var grid_soft_header		= parent.list.document.detail.dsh.value.split("~~");
	var x 				= parent.form.document.getElementById('t1').rows[indx].cells;
	var gsh_indx			= "";	
	for(var i=0;i<grid_header.length-1;i++) {
		var grid_header_elements = grid_header[i].split("|");
		for(var j=0;j<grid_soft_header.length-1;j++) {		
			var grid_soft_header_elements 	= grid_soft_header[j].split("|");
			if(grid_header_elements[0]==grid_soft_header_elements[0]){
				gsh_indx += i+",";	
			}
		}
	}
	if(oper=="SUM()"){
		var cols = gsh_indx.split(",");
		for(var k=0;k<cols.length-1;k++) {
			var cell = x[cols[k]].innerHTML;
			calculation += (cell*1);	
		}		
	}
	calculation=calculation.toFixed(2);
	return calculation;
}
function Hilite(){
	//var tname=event.srcElement.tagName;
	var eName = event.srcElement;
	//eName.bgColor = "#D5D5D5";
	var trName=eName.parentElement;
	trName.style.backgroundColor = "#D5D5D5";
	//trName.style.cursor = "hand";
}
function Restore(){
	var eName = event.srcElement;
	var trName=eName.parentElement;
	//trName.style.cursor = "";
	trName.style.backgroundColor = "#FFFFFF";
}
function ShowCalendar(form,datetype,separator){
	var appid	= parent.list.document.data.appid.value;
	if (!openwindow || openwindow.closed){
		win ="window.open('../libraries/html/clnd.html?pbf="+form+"&datetype="+datetype+"&separator="+separator+"','Calendar','left=200,top=200,width=250,height=250,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
