//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:   2005-02-17
// Updates:2005-08-22,2006-01-29,2006-02-22
// Author:Rodolfo Machon
// File Name: srch.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var openwindow1;
var openwindow;
var indx1;
var OW   = new Object();
function ConstructTable(){
	var grid_header		= parent.list.document.detail.dgh.value.split("~~");
	var select_values	= parent.list.document.detail.dsv.value.split("~~");
	var labels_values	= parent.list.document.detail.dlv.value.split("~~");
	var elements		= parent.list.document.detail.del.value.split("~~");
	var select_fields	= parent.list.document.detail.dsf.value.split(",");
	var labels_fields	= parent.list.document.detail.dlf.value.split(",");
	var T  			= parent.list.document.getElementById('t1');
	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 NewRow		= T.insertRow(0);
	var k			= 0;
	var l			= 0;
	var h			= 0;
	var ghl			= grid_header.length;
	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++;}else{ghl--;}
	for (j=k;j<ghl;j++){
		if(k==0){h=j}else{h=j-1}
		var record		= grid_header[h].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++;
	}
	if(parent.list.document.detail.dlt.value=="ON"){Cells[l] = NewRow.insertCell(l);Cells[l].innerHTML = "DELETE";}	
	for (var i=1;i<select_values.length;i++){	
		k		= 0;
		l		= 0;
		var status	= true;
		var svalues 	= new Object;
		var lvalues 	= new Object;
		var srecord	= select_values[i-1].split("|");
		var lrecord	= labels_values[i-1].split("|");		
		NewRow		= T.insertRow(i);
		if(navigator.appName=='Microsoft Internet Explorer'){
			//NewRow.attachEvent('onmouseover',Hilite);
			//NewRow.attachEvent('onmouseout',Restore);		
		}else{
			NewRow.addEventListener("mouseover", Hilite, false);
			NewRow.addEventListener("mouseout", Restore, false);
		}
		NewRow.style.cssText 	= parent.list.document.detail.tdbody.value;
		for (j=0;j<srecord.length;j++){if(srecord[j]==""){srecord[j]="&nbsp;"};svalues[select_fields[j]] = srecord[j];}
		for (j=0;j<lrecord.length;j++){if(lrecord[j]==""){lrecord[j]="&nbsp;"};lvalues[labels_fields[j]] = lrecord[j];}
		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"||document.detail.component.value=="list"){Cells[k].innerHTML = "&nbsp;&nbsp;<a name=a"+i+" href='javascript:GridPassBack("+i+")'>"+i+"</a>";}
			k++;
			l++;
		}
		for (j=k;j<ghl;j++){
			var inHTML  	= ""; 
			var checked	= "";
			var link	= "";
			var elementos	= column_elements[j].split(",");
			if(svalues[fields[j]]){inHTML = svalues[fields[j]]}else{inHTML = lvalues[fields[j]]}
			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);
			if(!inHTML){inHTML="&nbsp;";}
			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+">";}
	}
	var clase		= parent.list.document.data.clase.value;
	if(clase == "contribuyentes")
	{
		parent.form.dess1("search");//
		parent.form.dess("cargando");//		
	}
}
function Search(){
	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 passback		= parent.list.document.detail.passback.value;
	var opener_form		= parent.list.document.detail.opener_form.value;
	var field_focus		= parent.list.document.detail.field_focus.value;
	var search_fields	= parent.list.document.detail.dnf.value.split(",");
	var dff			= "";
	var dfv			= "";
	for (y=0;y<search_fields.length;y++){
		var campo	= "";
		if (eval("parent.form.document.master."+search_fields[y]+"[0]")){
			campo=eval("parent.form.document.master."+search_fields[y]+"[parent.form.document.master."+search_fields[y]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.master."+search_fields[y]+".value");}
		if(campo!=""){
			dff+=search_fields[y]+",";
			dfv+=campo+"|";
		}
	}
	var frame = "parent.list.location.href='srchl.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&passback="+passback+"&opener_form="+opener_form+"&field_focus="+field_focus+"&search_fields="+dff+"&search_values="+dfv+"'";
	eval(frame);
}
function FirstPage(){
	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 page		= 1;
	var limit		= parent.list.document.detail.limit.value;
	var search_fields	= parent.list.document.detail.dnf.value.split(",");
	var psbk		= parent.list.document.detail.passback.value.split(",");
	var opfr		= parent.list.document.detail.opener_form.value.split(",");
	var fldf		= parent.list.document.detail.field_focus.value.split(",");
	var dff			= "";
	var dfv			= "";
	for (y=0;y<search_fields.length;y++){
		var campo	= "";
		if (eval("parent.form.document.master."+search_fields[y]+"[0]")){
			campo=eval("parent.form.document.master."+search_fields[y]+"[parent.form.document.master."+search_fields[y]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.master."+search_fields[y]+".value");}
		if(campo!=""){
			dff+=search_fields[y]+",";
			dfv+=campo+"|";
		}
	}
	var frame = "parent.list.location.href='srchl.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&search_fields="+dff+"&search_values="+dfv+"&passback="+psbk+"&opener_form="+opfr+"&field_focus="+fldf+"&page="+page+"&limit="+limit+"'";
	eval(frame);
}
function BeforePage(){
	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 page		= parent.list.document.detail.page.value;
	var limit		= parent.list.document.detail.limit.value;
	var search_fields	= parent.list.document.detail.dnf.value.split(",");
	var psbk		= parent.list.document.detail.passback.value.split(",");
	var opfr		= parent.list.document.detail.opener_form.value.split(",");
	var fldf		= parent.list.document.detail.field_focus.value.split(",");
	var dff			= "";
	var dfv			= "";
	for (y=0;y<search_fields.length;y++){
		var campo	= "";
		if (eval("parent.form.document.master."+search_fields[y]+"[0]")){
			campo=eval("parent.form.document.master."+search_fields[y]+"[parent.form.document.master."+search_fields[y]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.master."+search_fields[y]+".value");}
		if(campo!=""){
			dff+=search_fields[y]+",";
			dfv+=campo+"|";
		}
	}
	if(page>1){
		page 	  = (page*1)-1;
		var frame = "parent.list.location.href='srchl.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&search_fields="+dff+"&search_values="+dfv+"&passback="+psbk+"&opener_form="+opfr+"&field_focus="+fldf+"&page="+page+"&limit="+limit+"'";
		eval(frame);
	}
}
function NextPage(){
	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 page		= (parent.list.document.detail.page.value*1)+1;
	var limit		= parent.list.document.detail.limit.value;
	var search_fields	= parent.list.document.detail.dnf.value.split(",");
	var psbk		= parent.list.document.detail.passback.value.split(",");
	var opfr		= parent.list.document.detail.opener_form.value.split(",");
	var fldf		= parent.list.document.detail.field_focus.value.split(",");
	var dff			= "";
	var dfv			= "";
	for (y=0;y<search_fields.length;y++){
		var campo	= "";
		if (eval("parent.form.document.master."+search_fields[y]+"[0]")){
			campo=eval("parent.form.document.master."+search_fields[y]+"[parent.form.document.master."+search_fields[y]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.master."+search_fields[y]+".value");}
		if(campo!=""){
			dff+=search_fields[y]+",";
			dfv+=campo+"|";
		}
	}
	var frame = "parent.list.location.href='srchl.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&search_fields="+dff+"&search_values="+dfv+"&passback="+psbk+"&opener_form="+opfr+"&field_focus="+fldf+"&page="+page+"&limit="+limit+"'";
	eval(frame);
}
function LastPage(){
	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 page		= parent.list.document.detail.pend.value;
	var limit		= parent.list.document.detail.limit.value;
	var search_fields	= parent.list.document.detail.dnf.value.split(",");
	var psbk		= parent.list.document.detail.passback.value.split(",");
	var opfr		= parent.list.document.detail.opener_form.value.split(",");
	var fldf		= parent.list.document.detail.field_focus.value.split(",");
	var dff			= "";
	var dfv			= "";
	for (y=0;y<search_fields.length;y++){
		var campo	= "";
		if (eval("parent.form.document.master."+search_fields[y]+"[0]")){
			campo=eval("parent.form.document.master."+search_fields[y]+"[parent.form.document.master."+search_fields[y]+".selectedIndex].value");
		}else{campo=eval("parent.form.document.master."+search_fields[y]+".value");}
		if(campo!=""){
			dff+=search_fields[y]+",";
			dfv+=campo+"|";
		}
	}
	var frame = "parent.list.location.href='srchl.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"&search_fields="+dff+"&search_values="+dfv+"&passback="+psbk+"&opener_form="+opfr+"&field_focus="+fldf+"&page="+page+"&limit="+limit+"'";
	eval(frame);
}
function Clear(form){
	var name ="";
	qty=parent.form.document.master.elements.length;
	for (i=0;i<qty;i++){
		if (eval("parent.form.document."+form+".elements["+i+"].type")!="hidden"){
			if (eval("parent.form.document."+form+".elements["+i+"].type")!="button"){			
				name= eval("parent.form.document."+form+".elements["+i+"].name");
				if(eval("parent.form.document."+form+"."+name+"[0]")){
					eval("parent.form.document."+form+"."+name+"[0].selected=true");
				}
				else{
					eval("parent.form.document."+form+".elements["+i+"].value=''");
				}
			}
		}

	}
	parent.form.document.all.add.style.visibility='visible';
	parent.form.document.all.modify.style.visibility='hidden';
	if (eval("parent.form.document.all.passback")){parent.form.document.all.passback.style.visibility='hidden';}
}
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		= "";
	if(id_field!=""){
		id = eval("parent.form.document.master."+id_field+".value");
		if(id==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id="+id+"&field_focus="+field_focus+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow1=eval(win);
	}else{openwindow1.focus()}
}
function GetBackPl(component,clase,getback,opener_form,left,top,id_field,field_focus){
	var lan		= parent.document.master.lan.value;
	var appid	= parent.document.master.appid.value;
	var id		= "";
	if(id_field!=""){
		id = eval("parent.document.master."+id_field+".value");
		if(id==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id="+id+"&field_focus="+field_focus+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow1=eval(win);
	}else{openwindow1.focus()}
}
function OpenWindow(component,clase,left,top,id_field){
	alert("");
	var lan		= parent.list.document.data.lan.value;
	var appid	= parent.list.document.data.appid.value;
	var id		= eval("parent.list.document.detail."+id_field+".value");
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow1=eval(win);
	}
	else{openwindow1.focus()}
}
function OpenWindow1(component,clase,getback,opener_form,left,top,id_field,field_focus,id){
	var lan		= parent.list.document.data.lan.value;
	var appid	= parent.list.document.data.appid.value;
	if(id_field!=""){
		var select_values= parent.list.document.detail.dsv.value.split("~~");
		var select_fields= parent.list.document.detail.dsf.value.split(",");
		var select_value = select_values[indx1-1];
		var record	 = select_value.split("|");
		var idf		 = id_field.split(",");
		id		 = "";
		for (i=0;i<select_fields.length;i++){
			for (j=0;j<idf.length;j++){
				if(idf[j]==select_fields[i]){
					id+=record[i]+"|";
				}
			}
		}
	}
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id_field="+id_field+"&field_focus="+field_focus+"&id="+id+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow1=eval(win);
	}
	else{openwindow1.focus()}
	close();
}
function GridOpenWindow(objRef,fldRef){
	var lan		 	= parent.list.document.data.lan.value;
	var appid	 	= parent.list.document.data.appid.value;
	var select_values= parent.list.document.detail.dsv.value.split("~~");
	var select_fields= parent.list.document.detail.dsf.value.split(",");	
	var component	= "";
	var clase	 	= "";
	var left	 	= "";
	var top		 	= "";
	var id		 	= "";
	var id_field	= "";
	var objIndx		= objRef.name.split("__");
	var references	= parent.list.document.detail.del.value.split("~~");
	for (var i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(objIndx[0]==reference[0]){
			var event	= reference[3].split("^^");
			var emembers= event[0].split("^");
			var cmembers= emembers[1].split(",");
			component	= cmembers[0];
			clase		= cmembers[1];
			left		= cmembers[2];
			top			= cmembers[3];
			id_fields	= cmembers[4];
		}
	}
	var id_field	= id_fields.split(";");
	var select_value= select_values[objIndx[1]-1];
	var record	= select_value.split("|");
	for (i=0;i<record.length;i++){
		for (j=0;j<id_field.length;j++){
			if(id_field[j]==select_fields[i]){
				id+=record[i]+"|";
			}
		}
	}	
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"','"+component+clase+"','left="+left+",top="+top+",width=600,height=400,scrollbars=no,toolbar=no,menu=no')";
		openwindow1=eval(win);
	}else{openwindow1.focus()}	
}
function GridPassBack(indx){
	var pbf		= "";
	var select_values= parent.list.document.detail.dsv.value.split("~~");
	var labels_values= parent.list.document.detail.dlv.value.split("~~");
	var select_fields= parent.list.document.detail.dsf.value.split(",");
	var labels_fields= parent.list.document.detail.dlf.value.split(",");
	var psback_fields= parent.list.document.detail.passback.value.split(",");
	var OpenerForm   = parent.list.document.detail.opener_form.value;
	var OFV		 	 = OpenerForm.split("|")
	var FieldFocus   = parent.list.document.detail.field_focus.value;	
	var select_value = select_values[indx-1];
	var record	 	 = select_value.split("|");
	indx1=indx;
	if(OFV.length>1){
		OpenWindow1(OFV[0],OFV[1],OFV[2],OFV[3],OFV[4],OFV[5],OFV[6],OFV[7],record[0]);
		//setTimeout("parent.window.close()",1000);
		return;
	}
	for (i=0;i<record.length;i++){
		for (j=0;j<psback_fields.length;j++){
			var pb	= psback_fields[j].split("|");
			var pb1	= pb[0].split("__");
			if(pb1[0]==select_fields[i]){
				if(pb.length>1){pbf=pb[1]}else{pbf=pb[0]}
				if(eval(OpenerForm+"."+pbf)){
					eval(OpenerForm+"."+pbf+".value='"+record[i]+"'");
				}
			}
		}
	}
	var labels_value = labels_values[indx-1];
	var record1 = labels_value.split("|");	
	for (i=0;i<record1.length;i++){
		if(record1[0]!=""){
			for (j=0;j<psback_fields.length;j++){
				pb = psback_fields[j].split("|");
				var pb1	= pb[0].split("__");
				if(pb1[0]==labels_fields[i]){
					if(pb.length>1){pbf=pb[1]}else{pbf=pb[0]}
					if(eval(OpenerForm+"."+pbf)){
						eval(OpenerForm+"."+pbf+".value='"+record1[i]+"'");
					}
				}
			}
		}
	}
	if(FieldFocus!=""){if(eval(OpenerForm+"."+FieldFocus)){eval(OpenerForm+"."+FieldFocus+".focus()")}}
	parent.window.close();
}
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()}
}
function GridOpenWindowPL(objRef){
	var lan		 = parent.list.document.data.lan.value;
	var appid	 = parent.list.document.data.appid.value;
	var select_values= parent.list.document.detail.dsv.value.split("~~");
	var select_fields= parent.list.document.detail.dsf.value.split(",");	
	var component	 = "";
	var clase	 = "";
	var left	 = "";
	var top		 = "";
	var id		 = "";
	var id_field	 = "";
	var objIndx  	 = objRef.name.split("__");
	var references   = parent.list.document.detail.del.value.split("~~");
	for (var i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(objIndx[0]==reference[0]){
			var event	= reference[3].split("^^");
			var emembers	= event[0].split("^");
			var cmembers	= emembers[1].split(",");
			component	= cmembers[0];
			clase		= cmembers[1];
			left		= cmembers[2];
			top		= cmembers[3];
			id_fields	= cmembers[4];
		}
	}
	var id_field	= id_fields.split(";");
	var select_value= select_values[objIndx[1]-1];
	var record	= select_value.split("|");
	for (i=0;i<record.length;i++){
		for (j=0;j<id_field.length;j++){
			if(id_field[j]==select_fields[i]){
				id+=record[i]+"|";
			}
		}
	}
	var win_name = clase+"_"+component;	
	id = id.substr(0,id.length-1)
	if (!OW[win_name] || OW[win_name].closed){
		win ="window.open('../pl/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"','"+component+clase+"','left="+left+",top="+top+",width=600,height=400,scrollbars=yes,toolbar=no,menu=no,resizable=yes')";
		OW[win_name]=eval(win);
	}else{OW[win_name].focus()}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Auxiliar Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function ReloadOpener(){
	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 abridor	= parent.list.document.detail.opener_form.value;
	var id		= parent.form.document.data.id.value;
	yo = abridor+".location.href='menul.pl?lan="+lan+"&appid="+appid+"&clase=user_menus&id="+id+"'";
	//alert(yo);
	eval(yo);
}
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";
}
////////////////////////////////////////////////////////////////////////////////////////////////