//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:   2011-06-17
// Updates:2011-06-17
// Author:Rodolfo Machon
// File Name: rcrd.js BSB v2.0
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var id;
var select_fields;
var labels_fields;
var needed_fields;
var insert_update_fields;
var select_values;
var labels_values;
var association_fields="";
var association_labels_fields="";
var associations_needed_fields="";
var association_values="";
var associations_insert_values="";
var association_labels_values="";
var xmlHttp;
var xmlHttpStream;
var openwindow;
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 1
function GetPageData(){
	GetParams();
	GetClase(id);
}
function GetParams(){
	var query   = window.location.search.substring(1);
	var params  = query.split('&');
	if(params[0]){var la = params[0].split('=');lan	= la[1];}	
	if(params[1]){var ap = params[1].split('=');app	= ap[1];}
	if(params[2]){var cl = params[2].split('=');clase = cl[1];}
	if(params[3]){var ii = params[3].split('=');id = ii[1];}
	if(params[4]){var ffs= params[4].split('=');filter_fields = ffs[1];}
	if(params[5]){var fvs= params[5].split('=');filter_values = fvs[1];}	
	if(params[6]){var gb = params[6].split('=');PassBack = gb[1];}
	if(params[7]){var op = params[7].split('=');OpenerForm = op[1];}
	if(params[8]){var ff = params[8].split('=');FieldFocus = ff[1];}
}
function GetClase(id){
	var URL	= "/cgi-bin/"+app+"/bin/recordp.pl?lan="+lan+"&app="+app+"&clase="+clase+"&id="+id;
	xmlHttp	= GetXmlHttpObject(SC_GetClase);
	xmlHttp.open("GET",URL,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(null);
}
function Save(){
	if(CheckClaseNeededFields()){
		PassClaseToContainer();
		if(associations_needed_fields!=""){PassAssociationsToContainer()}
		if(ClaseValues==""){alert("NO DATA TO SEND");return}
		var str	 = "lan="+lan+"&app="+app+"&clase="+clase+"&id="+id+"&iuv="+ClaseValues+"&aiv="+associations_insert_values;
		alert(str);
		return;
		var URL	 = "/cgi-bin/"+app+"/bin/mdwss.pl";
		xmlHttp  = GetXmlHttpObject(SC_Save);
		xmlHttp.open('POST',URL,true) ;
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.send(str);
	}else{alert("Fields Neeeded");}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 2
function GetXmlHttpObject(handler){
	var objXmlHttp=null;	
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 		
	}
	if(window.ActiveXObject){// code for IE6, IE5
		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		objXmlHttp.onreadystatechange=handler 		
	}
	if(objXmlHttp==null){alert("Your Browser Does Not Suppport AJAX")}
	return objXmlHttp;
}
function SC_GetClase(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		xmlHttpStream = xmlHttp.responseText;
		//document.write(xmlHttpStream);		
		var Data   = xmlHttpStream.split("^^");
		if(Data!=""){
			id 			   = Data[0];
			//alert("1."+id);
			select_fields 		   = Data[1];
			//alert("2."+select_fields);
			labels_fields 		   = Data[2];
			//alert("3."+labels_fields);
			needed_fields 		   = Data[3];
			//alert("4."+needed_fields);
			insert_update_fields 	   = Data[4];
			//alert("5."+insert_update_fields);
			select_values 		   = Data[5];
			//alert("6."+select_values);			
			labels_values 		   = Data[6];
			//alert("7."+labels_values);
			association_fields 	   = Data[7];
			//alert("8."+association_fields);
			association_labels_fields  = Data[8];
			//alert("9."+association_labels_fields);
			associations_needed_fields = Data[9];
			//alert("10."+associations_needed_fields);
			association_values 	   = Data[10];
			//alert("11."+association_values);
			association_labels_values  = Data[11];
			//alert("12."+association_labels_values);
			ConstructClase();
		}
	} 
}
function CheckClaseNeededFields(){
	var clase_needed_fields = needed_fields.split(',');
	var needed = true;
	for (i=0;i<clase_needed_fields.length;i++){
		if (eval("document.rcrd."+clase_needed_fields[i]+"[0]")){		
			valor=eval("document.rcrd."+clase_needed_fields[i]+"[document.rcrd."+clase_needed_fields[i]+".selectedIndex].value");
		}else if(eval("document.rcrd."+clase_needed_fields[i])){
			valor=eval("document.rcrd."+clase_needed_fields[i]+".value");
		}
		if(valor==""){needed = false}
	}
	return needed;
}
function PassClaseToContainer(){
	var miv  = "";
	var clase_needed_fields = insert_update_fields.split(',');
	for (i=0;i<clase_needed_fields.length;i++){
		if(document.getElementsByName(clase_needed_fields[i])[0]) {
			if (eval("document.rcrd."+clase_needed_fields[i]+"[0]")){
				valor=eval("document.rcrd."+clase_needed_fields[i]+"[document.rcrd."+clase_needed_fields[i]+".selectedIndex].value");
			}else if(eval("document.rcrd."+clase_needed_fields[i])){
				valor=eval("document.rcrd."+clase_needed_fields[i]+".value");
			}
			miv+=valor+"[]";
		}else{
			alert(clase_needed_fields[i]+" Does Not Exist");
			return;
		}
	}
	ClaseValues=miv;
}
function PassAssociationsToContainer(){
	var aiv  = "";
	var associations_needed_fields = associations_needed_fields.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(document.getElementsByName(fields[j])[0]) {			
				if (eval("document.rcrd."+fields[j]+"[0]")){
					valor = eval("document.rcrd."+fields[j]+"[document.rcrd."+fields[j]+".selectedIndex].value");
				}else if(eval("document.rcrd."+fields[i])){
					valor = eval("document.rcrd."+fields[j]+".value");
				}
				aiv+=valor+"|";
			}else{
				alert(fields[i]+" Does Not Exist");
				return;
			}
		}
		aiv = members[0]+"|"+aiv+"~~";
	}
	associations_insert_values = aiv;
}
function SC_Save(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		xmlHttpStream = xmlHttp.responseText;
		//alert(xmlHttpStream);
		var Data   = xmlHttpStream.split("^^");		
		if(Data!=""){
			id= Data[0]; 			
			GetClase(id);
		}
	} 
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 3
function ConstructClase(){
	var msf = select_fields.split(',');
	var msv = select_values.split('|');
	for (i=0;i<msf.length-1;i++){
		//alert(msf[i]+"<=>"+msv[i]);
		if(document.getElementsByName(msf[i])[0]) {
			if (eval("document.rcrd."+msf[i]+".selectedIndex == 0")){			
				listlength = eval("document.rcrd."+msf[i]+".options.length");
				idx=0;
				for (j=0;j<listlength;j++){
					opt=eval("document.rcrd."+msf[i]+".options["+j+"].value");
					if ( opt== msv[i]){idx = j;}
				}
				eval("document.rcrd."+msf[i]+"["+idx+"]"+".selected=true");
			}else{
				//alert("document.rcrd."+msf[i]+".value=\""+msv[i]+"\"")
				eval("document.rcrd."+msf[i]+".value=\""+msv[i]+"\"");
			}
		}
	}
	tmlf = labels_fields.split('~~');
	tmlv = labels_values.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(document.getElementsByName(mlf[i])[0]) {
				if (eval("document.rcrd."+mlf[i]+".selectedIndex == 0")){
					listlength = eval("document.rcrd."+mlf[i]+".options.length");
					idx=0;
					for (j=0;j<listlength;j++){
						opt=eval("document.rcrd."+mlf[i]+".options["+j+"].value");
						if ( opt== msv[i]){idx = j;}
					}			
					eval("document.rcrd."+mlf[i]+"["+idx+"]"+".selected=true");
				}else{
					eval("document.rcrd."+mlf[i]+".value=\""+mlv[i]+"\"");
				}
			}
		}
	}
	tmlf = association_fields.split('~~');
	tmlv = association_values.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(document.getElementsByName(mlf[i])[0]) {
				if (eval("document.rcrd."+mlf[i]+".selectedIndex == 0")){
					listlength = eval("document.rcrd."+mlf[i]+".options.length");
					idx=0;
					for (j=0;j<listlength;j++){
						opt=eval("document.rcrd."+mlf[i]+".options["+j+"].value");
						if ( opt== msv[i]){idx = j;}
					}			
					eval("document.rcrd."+mlf[i]+"["+idx+"]"+".selected=true");
				}else{
					eval("document.rcrd."+mlf[i]+".value=\""+mlv[i]+"\"");
				}
			}
		}
	}
	tmlf = association_labels_fields.split('~~');
	tmlv = association_labels_values.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(document.getElementsByName(mlf[i])[0]) {
				if (eval("document.rcrd."+mlf[i]+".selectedIndex == 0")){
					listlength = eval("document.rcrd."+mlf[i]+".options.length");
					idx=0;
					for (j=0;j<listlength;j++){
						opt=eval("document.rcrd."+mlf[i]+".options["+j+"].value");
						if ( opt== msv[i]){idx = j;}
					}			
					eval("document.rcrd."+mlf[i]+"["+idx+"]"+".selected=true");
				}else{
					eval("document.rcrd."+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';}}}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Auxiliar Sub-Routines
function GetBack(component,clase,getback,opener_form,left,top,id_field,field_focus){
	var id_value	= "";
	if(id_field!=""){
		id_value = eval("document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('/cgi-bin/"+app+"/bin/"+component+".exe?lan="+lan+"&app="+app+"&clase="+clase+"&getback="+getback+"&opener_form="+opener_form+"&id="+id_value+"&field_focus="+field_focus+"&id_field="+id_field+"','"+component+clase+"','left="+left+",top="+top+",width=100,height=100,scrollbars=yes,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function SystemPrintPreview(OLECMDID){ 
	parent.form.document.all.save.style.visibility='hidden';	
	parent.form.document.all.preview.style.visibility='hidden';
	parent.form.document.all.nuevo.style.visibility='hidden';	
	 //var OLECMDID = 10;/* OLECMDID values: *6- print *7- print preview *8- page setup (for printing) *1- open window  *4- Save As *10- properties  */
	 try{
			var PROMPT = 1; // 1 PROMPT & 2 DONT PROMPT USER 
			var oWebBrowser = document.getElementById("WebBrowser1");
			if(oWebBrowser == null)
			{
				var sWebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
				document.body.insertAdjacentHTML('beforeEnd', sWebBrowser); 
				oWebBrowser = document.getElementById("WebBrowser1");
			}
			oWebBrowser.ExecWB(OLECMDID,PROMPT);
	}
	catch(e){alert("Printing failed! " + e.message);}
	parent.form.document.all.save.style.visibility='visible';	
	parent.form.document.all.preview.style.visibility='visible';
	parent.form.document.all.nuevo.style.visibility='visible';	
}
function ShowCalendar(form,datetype,separator){
	if (!openwindow || openwindow.closed){
		win ="window.open('/"+app+"/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){
	if (!openwindow || openwindow.closed){
		win ="window.open('/"+app+"/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    = select_fields.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){
	if(id!=""){
		var pbf		 = "";
		var psback_fields= PassBack.split(",");
		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')}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////