//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:    2006-05-29
// Updates: 2006-05-29
// Author:Rodolfo Machon
// File Name: queu.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var openwindow;
var openwindow1;
var master_needed_fields;
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function ConstructTable(){
	var grid_header		= parent.list.document.detail.gh.value.split("~~");	
	var grid_body		= parent.list.document.detail.gv.value.split("~~");
	var T  			= parent.form.document.getElementById('t1');
	var header_length	= (grid_header.length*1);
	var Cells 		= new Array(header_length);
	var fields		= new Array(header_length);	
	var columns_width	= new Array(header_length);	
	var element_type	= new Array(header_length);
	var element_properties	= new Array(header_length);	
	var NewRow		= T.insertRow(0);
	NewRow.style.cssText 	= parent.list.document.detail.tdheader.value;
	for (j=0;j<grid_header.length-1;j++){
		record			= grid_header[j].split("|");
		Cells[j]  		= NewRow.insertCell(j);
		Cells[j].innerHTML	= record[1];
		fields[j]		= record[0];
		columns_width[j]	= record[2];
		element_type[j]		= record[3];
		element_properties[j]	= record[4];		
	}
	var Rows = (parent.list.document.detail.t1BodyRows.value*1);
	for (var i=0;i<Rows;i++){	
		k = (i*1)+1;
		NewRow	= T.insertRow(i+1);
		////NewRow.attachEvent('onmouseover',Hilite);
		////NewRow.attachEvent('onmouseout',Restore);		
		NewRow.style.cssText 	= parent.list.document.detail.tdbody.value;		
		for (j=0;j<grid_header.length-1;j++){
			var checked = "";
			if(element_type[j]=="text"){
				href	= "";
			}else if(element_type[j]=="input_text"){
				href  = "<input type=text name="+fields[j]+"__"+k;
				href += " size="+columns_width[j];
				href += " maxlength="+fields[j].substr(1,1);
				href += " "+element_properties[j];
				href += " class=my_style>";
			}else if(element_type[j]=="checkbox"){
			
			}
			Cells[j]  		= NewRow.insertCell(j);
			Cells[j].innerHTML	= href;
		}
	}
	PopulateTable();	
}
function PopulatePage(){
	var id	= parent.list.document.data.id.value;	
	if(id!=""){
		ShowMaster();
		PopulateTable();
		ProcessTable();
	}		
}
function Save(){
	if(CheckMasterNeededFields()){
		PassMasterToContainer();
		PassDetailToContainer();
		parent.list.document.data.submit();
	}else{alert(parent.form.document.form.msg_empty.value);}
}
function GetBack(component,clase,getback,opener_form,left,top,id_field,field_focus){
	var lan		= parent.list.document.data.lan.value;
	var appid	= parent.list.document.data.appid.value;
	var id_value	= "";
	if(id_field!=""){
		id_value = eval("parent.form.document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('../bin/"+component+".exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id="+id_value+"&field_focus="+field_focus+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function Process(id_field){
	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 		= eval("parent.form.document.form."+id_field+".value");
	parent.list.document.data.id.value = id;
	parent.form.document.form.guardar.focus();
	if(id!=""){
		parent.list.location.href='mdx.exe?lan='+lan+'&appid='+appid+'&clase='+clase+'&id='+id;		
	}
}
function ShowCalendar(form){
	var appid	= parent.list.document.data.appid.value;
	if (!openwindow || openwindow.closed){
		win ="window.open('../libraries/html/clnd.html?pb="+form+"','Calendar','left=200,top=200,width=250,height=260,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function Padg(objRef,form,field){
	var appid	= parent.list.document.data.appid.value;
	var lan		= parent.list.document.data.lan.value;
	var clase	= parent.list.document.data.clase.value;
	var field_name 	= objRef.name;
	var pers	= eval("parent.form.document.form."+field_name+".value");
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/padg.exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&form="+form+"&field="+field_name+"&pers="+pers+"','"+clase+"','left=200,top=200,width=350,height=350,scrollbars=yes,toolbar=no,menu=no,resizable=yes')";
		openwindow1=eval(win);
	}else{openwindow1.focus()}
}
function ShowClock(form){
	var appid	= parent.list.document.data.appid.value;
	if (!openwindow || openwindow.closed){
		win ="window.open('../libraries/html/clock.html?pb="+form+"','Calendar','left=200,top=200,width=150,height=125,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function GridGetBack(objRef,fldRef){
	var lan		= parent.list.document.detail.lan.value;
	var appid	= parent.list.document.detail.appid.value;
	var id_value	= "";
	var id_field	= "";
	var objIndx  	= objRef.name.split("__");
	var references  = parent.list.document.detail.fr.value.split("~~");
	for (i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(fldRef==reference[0]){
			var component	= reference[1];
			var clase	= reference[2];
			var getback	= reference[3];
			var opener_form	= reference[4];
			var left	= reference[5];
			var top		= reference[6];
			id_field	= reference[7];			
		}
	}
	if(id_field!=""){
		id_value = eval("parent.form.document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	var gbFlds  	= getback.split(",");
	getback		= "";
	for (i=0;i<gbFlds.length;i++){
		gbFld = gbFlds[i]+"__"+objIndx[1];
		getback += gbFld+",";
	}
	getback = getback.substr(0,getback.length-1)
	if (!openwindow || openwindow.closed){
		win ="window.open('../bin/"+component+".exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id="+id_value+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}	
}
function GridOpenWindow(objRef,fldRef){
	var lan		= parent.list.document.detail.lan.value;
	var appid	= parent.list.document.detail.appid.value;
	var id_value	= "";
	var id_field	= objRef.name;
	var objIndx  	= objRef.name.split("__");
	var references  = parent.list.document.detail.fr.value.split("~~");
	for (i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(fldRef==reference[0]){
			var component	= reference[1];
			var clase	= reference[2];
			var getback	= reference[3];
			var opener_form	= reference[4];
			var left	= reference[5];
			var top		= reference[6];
			//id_field	= reference[7];
			//id_field	= fldRef;
		}
	}
	//alert(id_field);
	if(id_field!=""){
		id_value = eval("parent.form.document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('../bin/"+component+".exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&opener_form="+opener_form+"&id="+id_value+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		//alert(win);
		openwindow=eval(win);
	}else{openwindow.focus()}	
}
function GridMultiply(objRef,CellQty,CellValue,CellSub){
	var objIndx  	= objRef.name.split("__");	
	var ColTotal	= CellSub+"_Total"
	qty   = eval("document.form."+CellQty+"__"+objIndx[1]+".value");
	value = eval("document.form."+CellValue+"__"+objIndx[1]+".value");
	sub=qty*value;
	sub_formatted=sub.toFixed(2);
	eval("document.form."+CellSub+"__"+objIndx[1]+".value='"+sub_formatted+"'");
	Sum(CellSub,ColTotal);	
}
function GridClean(objRef,cols){
	var objIndx  	= objRef.name.split("__");
	var Columns  	= cols.split(",");
	for (var i=0;i<Columns.length;i++){
		eval("document.form."+Columns[i]+"__"+objIndx[1]+".value=''");
	}
	//Sum(CellSub,ColTotal);
}
function GetClase(){
	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_field	= parent.list.document.data.id.value;
	if(id!=""){
		var frame = "parent.list.location.href='mdx.exe?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"'";
		eval(frame);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Auxiliar Sub-Routines
function CheckMasterNeededFields(){
	master_needed_fields = parent.list.document.detail.mif.value.split(',');
	var needed = true;
	for (i=0;i<master_needed_fields.length;i++){
		if (eval("parent.form.document.form."+master_needed_fields[i]+"[0]")){		
			valor=eval("parent.form.document.form."+master_needed_fields[i]+"[parent.form.document.form."+master_needed_fields[i]+".selectedIndex].value");
		}else if(eval("parent.form.document.form."+master_needed_fields[i])){
			valor=eval("parent.form.document.form."+master_needed_fields[i]+".value");
		}else{
			alert(parent.form.document.form.msg_empty.value+master_needed_fields[i]);
			needed = false;
		}
	}
	return needed;
}
function PassMasterToContainer(){
	var miv  = "";
	id_field = parent.list.document.detail.id_field.value;
	parent.list.document.data.id.value = eval("parent.form.document.form."+id_field+".value");
	for (i=0;i<master_needed_fields.length;i++){
		if (eval("parent.form.document.form."+master_needed_fields[i]+"[0]")){
			campo=eval("parent.form.document.form."+master_needed_fields[i]+"[parent.form.document.form."+master_needed_fields[i]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.form."+master_needed_fields[i]+".value");}
		miv+=campo+"|";
	}
	parent.list.document.data.miv.value=miv;
}
function PassDetailToContainer(){
	var div = "";
	var detail_needed_fields = parent.list.document.detail.dif.value.split(',');
	var Rows = (parent.list.document.detail.t1BodyRows.value*1);
	for(i=1;i<Rows;i++) {
		if(eval("parent.form.document.form."+detail_needed_fields[0]+"__"+i+".value")!=""){
			for(j=0;j<detail_needed_fields.length;j++) {		
				campo=eval("parent.form.document.form."+detail_needed_fields[j]+"__"+i+".value");
				div+=campo+"|";
			}
			div=div+"~~";
		}
	}
	parent.list.document.data.div.value=div;
}
function Clear(formulario){
	var name ="";
	qty = eval("parent."+formulario+".elements.length");
	for (i=0;i<qty;i++){
		if (eval("parent."+formulario+".elements["+i+"].type")!="hidden"){
			if (eval("parent."+formulario+".elements["+i+"].type")!="button"){			
				name= eval("parent."+formulario+".elements["+i+"].name");
				if(eval("parent."+formulario+"."+name+"[0]")){					
					eval("parent."+formulario+"."+name+"[0].selected=true");
				}
				else{
					eval("parent."+formulario+".elements["+i+"].value=''");
				}
			}
		}
     }
}
function ShowMaster(){
	var msf = parent.list.document.detail.msf.value.split(',');
	var msv = parent.list.document.detail.msv.value.split('|');
	for (i=0;i<msf.length-1;i++){
		if (eval("parent.form.document.form."+msf[i]+"[0]")){
			listlength = eval("parent.form.document.form."+msf[i]+".options.length");
			idx=0;
			for (j=0;j<listlength;j++){
				opt=eval("parent.form.document.form."+msf[i]+".options["+j+"].value");
				if ( opt== msv[i]){idx = j;}
			}			
			eval("parent.form.document.form."+msf[i]+"["+idx+"]"+".selected=true");
		}else{
			eval("parent.form.document.form."+msf[i]+".value=\""+msv[i]+"\"");
		}
	}
	tmlf = parent.list.document.detail.mlf.value.split('~~');
	tmlv = parent.list.document.detail.mlv.value.split('~~');
	for (h=0;h<tmlv.length-1;h++){
		mlf = tmlf[h].split(',');
		mlv = tmlv[h].split('|');
		for (i=0;i<mlf.length-1;i++){
			if (eval("parent.form.document.form."+mlf[i]+"[0]")){
				listlength = eval("parent.form.document.form."+msf[i]+".options.length");
				idx=0;
				for (j=0;j<listlength;j++){
					opt=eval("parent.form.document.form."+msf[i]+".options["+j+"].value");
					if ( opt== msv[i]){idx = j;}
				}			
				eval("parent.form.document.form."+msf[i]+"["+idx+"]"+".selected=true");
			}else{
				eval("parent.form.document.form."+mlf[i]+".value=\""+mlv[i]+"\"");
			}
	}	}
}
function PopulateTable(){
	var sv 		 = parent.list.document.detail.gv.value.split("~~");
	var gh		 = parent.list.document.detail.gh.value.split("~~");
	for (var i=1;i<sv.length;i++){
		var values = sv[i-1].split("|");		
		for (var j=0;j<values.length-1;j++){
			var fields = gh[j].split("|");
			eval("document.form."+fields[0]+"__"+i+".value ='"+values[j]+"'");		
		}
	}	
}
function ProcessTable(){
	//GetOperations();
	//RunOperations();
}
function Sum(CellSub,ColTotal){
	t=0;
	var Rows = (parent.list.document.detail.t1BodyRows.value*1);
	for(var x=1;x<Rows+1;x++) {
		sub = eval("document.form."+CellSub+"__"+x+".value");
		//if(sub==""){sub=0};
		t = (t*1)+(sub*1);
	}
	t_formatted=t.toFixed(2);
	if(eval("document.form."+ColTotal)){
		eval("document.form."+ColTotal+".value='"+t_formatted+"'");
	}
}
function onlyDigits(e,decReq) {
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}
function Hilite(){
	var tname=event.srcElement.tagName;
	var eName = event.srcElement;
	var trName=eName.parentElement;
	eName.style.backgroundColor = "#D5D5D5";	
	trName.style.backgroundColor = "#D5D5D5";
	eName.style.cursor = "hand";
}
function Restore(Ref){
	var tname=event.srcElement.tagName;	
	var eName = event.srcElement;
	var trName=eName.parentElement;
	eName.style.backgroundColor = "#FFFFFF";
	trName.style.backgroundColor = "#FFFFFF";
	eName.style.cursor = "";	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////