//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:   2005-08-08
// Updates:2005-08-22,2006-01-20
// Author:Rodolfo Machon
// File Name: rcdr.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var openwindow;
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function ShowMaster(){
	var msf = document.form.sf.value.split(',');
	var msv = document.form.sv.value.split('|');
	for (i=0;i<msf.length-1;i++){
		//alert("Fields ("+i+"/"+(msf.length-1)+") -> "+msf[i]+"="+msv[i]);
		if (eval("document.form."+msf[i]+"[0]")){
			listlength = eval("document.form."+msf[i]+".options.length");
			idx=0;
			for (j=0;j<listlength;j++){
				opt=eval("document.form."+msf[i]+".options["+j+"].value");
				if ( opt== msv[i]){idx = j;}
			}			
			eval("document.form."+msf[i]+"["+idx+"]"+".selected=true");
		}else{
			eval("document.form."+msf[i]+".value=\""+msv[i]+"\"");
		}
	}
	tmlf = document.form.lf.value.split('~~');
	tmlv = document.form.lv.value.split('~~');
	//alert(tmlf+"<>"+tmlv);
	for (h=0;h<tmlv.length-1;h++){
		mlf = tmlf[h].split(',');
		mlv = tmlv[h].split('|');
		//alert("Labels ("+h+"/"+(mlf.length-1)+") -> "+mlf[h]+"="+mlv[h]);
		for (i=0;i<mlf.length-1;i++){
			if (eval("document.form."+mlf[i]+"[0]")){
				listlength = eval("document.form."+msf[i]+".options.length");
				idx=0;
				for (j=0;j<listlength;j++){
					opt=eval("document.form."+msf[i]+".options["+j+"].value");
					if ( opt== msv[i]){idx = j;}
				}			
				eval("document.form."+msf[i]+"["+idx+"]"+".selected=true");
			}else{
				eval("document.form."+mlf[i]+".value=\""+mlv[i]+"\"");
			}
		}
	}
	tmlf = document.form.af.value.split('~~');
	tmlv = document.form.av.value.split('~~');
	//alert("3>"+document.form.af.value+"<>"+document.form.av.value);	
	for (h=0;h<tmlv.length-1;h++){
		mlf = tmlf[h].split(',');
		mlv = tmlv[h].split('|');
		//alert("3>"+mlf[i]+"<>"+mlv[i]);
		for (i=0;i<mlf.length-1;i++){
			if (eval("document.form."+mlf[i]+"[0]")){
				listlength = eval("document.form."+mlf[i]+".options.length");
				idx=0;
				for (j=0;j<listlength;j++){
					opt=eval("document.form."+mlf[i]+".options["+j+"].value");
					if ( opt== msv[i]){idx = j;}
				}			
				eval("document.form."+mlf[i]+"["+idx+"]"+".selected=true");
			}else{
				eval("document.form."+mlf[i]+".value=\""+mlv[i]+"\"");
			}
		}
	}
	tmlf = document.form.alf.value.split('~~');
	tmlv = document.form.alv.value.split('~~');
	//alert("3>"+document.form.alf.value+"<>"+document.form.alv.value);		
	for (h=0;h<tmlv.length-1;h++){
		mlf = tmlf[h].split(',');
		mlv = tmlv[h].split('|');
		//alert("4>"+mlf[i]+"<>"+mlv[i]);
		for (i=0;i<mlf.length-1;i++){
			if (eval("document.form."+mlf[i]+"[0]")){
				listlength = eval("document.form."+mlf[i]+".options.length");
				idx=0;
				for (j=0;j<listlength;j++){
					opt=eval("document.form."+mlf[i]+".options["+j+"].value");
					if ( opt== msv[i]){idx = j;}
				}			
				eval("document.form."+mlf[i]+"["+idx+"]"+".selected=true");
			}else{
				eval("document.form."+mlf[i]+".value=\""+mlv[i]+"\"");
			}
		}
	}
	//
	//if(document.all.forward){if(document.data.exist.value=="true"){if(document.data.passback.value!=""){document.all.forward.style.visibility='visible';}}}	
	if(document.getElementById('forward')){if(document.data.exist.value=="true"){if(document.data.passback.value!=""){document.getElementById('forward').style.visibility='visible';}}}
}
function Save(){
	if(CheckMasterNeededFields()){
		PassMasterToContainer();
		if(document.form.aif){PassAssociationsToContainer()}
		document.data.submit();
	}else{
		alert(document.form.msg_empty.value);
	}
}
function GetBack(component,clase,getback,opener_form,left,top,id_field,field_focus){
	var lan		= document.data.lan.value;
	var appid	= document.data.appid.value;
	var id_value	= "";
	if(id_field!=""){
		var ids=id_field.split(",");
		var num_ids=ids.length;
		for (var i=0;i<num_ids;i++)
		{
			id_value_temp = eval("document.form."+ids[i]+".value");
			if(id_value_temp==""){alert("Primero Seleccione ID");return;}
			else{id_value = id_value+id_value_temp;id_value=id_value+"|";}
		}
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('/cgi-bin/"+appid+"/bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id_field="+id_field+"&id="+id_value+"&field_focus="+field_focus+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=yes,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function ShowCalendar(form,datetype,separator){
	var appid	= document.data.appid.value;
	if (!openwindow || openwindow.closed){
		win ="window.open('/"+appid+"/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 ShowClock(form){
	var appid	= document.data.appid.value;
	if (!openwindow || openwindow.closed){
		win ="window.open('/"+appid+"/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 Forward(form,field_focus,params){
	var pbfs = "";
	var s    = document.form.sf.value.split(",");
	var id   = eval("document.form."+field_focus+".value")
	if(id!=""){
		if(eval(form+"."+field_focus)){
			eval(form+"."+field_focus+".value='"+id+"'");
			eval(form+"."+field_focus+".focus()")
		}
		var pbf = params.split(",");		
		for (j=0;j<pbf.length;j++){
			var pb	= pbf[j].split("|");
			for (k=0;k<s.length;k++){
				if(pb[0]==s[k]){
					if(pb.length>1){pbfs=pb[1]}else{pbfs=pb[0]}
					if(eval(form+"."+pbfs)){
						eval(form+"."+pbfs+".value=document.form."+s[k]+".value");
					}
				}
			}
		}
		window.close();
	}
}
function PassBack(form,field_focus){
	id  = document.data.id.value;
	if(id!=""){
		var pbf		 = "";
		var psback_fields= document.data.passback.value.split(",");
		var OpenerForm   = document.data.opener_form.value;
		var FieldFocus   = document.data.field_focus.value;	
		for (k=0;k<psback_fields.length;k++){
			var pb = psback_fields[k].split("|");
			if(pb.length>1){pbf=pb[1]}else{pbf=pb[0]}
			if(eval(OpenerForm+"."+pbf)){
				eval(OpenerForm+"."+pbf+".value=document.form."+pb[0]+".value");
			}
		}
		if(FieldFocus!=""){if(eval(OpenerForm+"."+FieldFocus)){eval(OpenerForm+"."+FieldFocus+".focus()")}}
		parent.window.close();
		
	}else{alert('Please Select a Record')}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Auxiliar Sub-Routines
function CheckMasterNeededFields(){
	var master_needed_fields = document.form.nf.value.split(',');
	var needed = true;
	for (i=0;i<master_needed_fields.length;i++){
		if (eval("document.form."+master_needed_fields[i]+"[0]")){		
			valor=eval("document.form."+master_needed_fields[i]+"[document.form."+master_needed_fields[i]+".selectedIndex].value");
		}else if(eval("document.form."+master_needed_fields[i])){
			valor=eval("document.form."+master_needed_fields[i]+".value");
		}
		if(valor==""){
			alert("Favor llenar el campo: "+master_needed_fields[i]);			
			needed = false
		}
	}
	if(document.form.aif){
		if (document.form.aif.value!="") {
			var associations_needed_fields = document.form.aif.value.split('~~');
			for (i=0;i<associations_needed_fields.length-1;i++){
				var members = associations_needed_fields[i].split('|');
				var fields  = members[1].split(','); 
				for (j=0;j<fields.length;j++){
					if (eval("document.form."+fields[j]+"[0]")){		
						valor=eval("document.form."+fields[j]+"[document.form."+fields[j]+".selectedIndex].value");
					}else if(eval("document.form."+fields[j])){
						valor=eval("document.form."+fields[j]+".value");
					}
					if(valor==""){needed = false}
				}
			}
		}
	}
	return needed;
}
function PassMasterToContainer(){
	var val = "";
	var master_needed_fields = document.form.iuf.value.split(',');
	for (i=0;i<master_needed_fields.length;i++){
		if (eval("document.form."+master_needed_fields[i]+"[0]")){
			campo=eval("document.form."+master_needed_fields[i]+"[document.form."+master_needed_fields[i]+".selectedIndex].value");
		}else{campo=eval("document.form."+master_needed_fields[i]+".value");}
		//alert(master_needed_fields[i]+"="+campo);
		val+=campo+"|";
	}
	document.data.iuv.value=val;
}
function PassAssociationsToContainer(){
	var aiv  = "";
	var associations_needed_fields = document.form.aif.value.split('~~');
	//alert(associations_needed_fields);
	for (i=0;i<associations_needed_fields.length-1;i++){
		var members = associations_needed_fields[i].split('|');
		var fields  = members[1].split(','); 
		for (j=0;j<fields.length;j++){
			if (eval("document.form."+fields[j]+"[0]")){
				campo=eval("document.form."+fields[j]+"[document.form."+fields[j]+".selectedIndex].value");
			}else{campo=eval("document.form."+fields[j]+".value");}
			aiv+=campo+",";
		}
		aiv = members[0]+"|"+aiv+"~~";
	}
	//alert(aiv);
	document.data.aiv.value=aiv;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////