﻿/***********************************************************
*** (C) Antonio Olmedo Soler, 2002 (All rights reserved) ***
************************************************************
*** File____: fu_uitools.js                              ***
*** Project_: VirtOf                                     ***
*** Language: JavaScript                                 ***
*** Tier____: Presentation-services                      ***
*** Description: Value Input functions                   ***
***********************************************************/


//==========================================================
// CALENDAR TOOL (DATE VALUES)
//==========================================================

//--------------------
//- Modified: ??-???-????, Antonio Olmedo Soler
//- Modified: 25-Oct-2006, Antonio Olmedo Soler
//- Syntax__: Input_DateTime(e,[bEditTime=false],[cEvent],[cMin],[cMax],[oRef],[cRel])
//- Description: 
//--------------------
function Input_DateTime (e) {
	//·Debug_Function('Input_DateTime',null,arguments);
	//--- Optional arguments
	var bEditTime=false;
	var cEvent='';
	var cMin='';
	var cMax='';
	var oRef=null;
	var cRel='';
	var nI=0;
	if (arguments.length>nI++) if (arguments[nI]!=null) bEditTime=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cEvent=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cMin=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cMax=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) oRef=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cRel=arguments[nI];
	if (oRef) if (!cRel) cRel='>';
	//---
	var cPreValue=e.value;
	//var nHeight=300;
	var nHeight=320;
	var cOut='';
	var cLink=DIR_ROOT+'ui_datetime.asp?EditTime='+(bEditTime?'Y':'N') ;
	if (bEditTime) nHeight+=24;
	if ((cMin+''+cMax)!='') {
		cLink+='&Min='+cMin+'&Max='+cMax;
		nHeight+=40;
	}
	//alert(cLink);
	if (!e.value) if (oRef) if (oRef.value) e.value=oRef.value;
	if (!e.value) if (cMin) e.value=cMin;
	window.dateField=e;
	if (document.all) {
		var cFeatures='dialogWidth:280px;dialogHeight:'+nHeight+'px;center:yes;border:thin';
		cFeatures+='status:no;location:no;';
		cOut=showModalDialog(cLink,e,cFeatures);
	} else {
		window.open(cLink,'dlgDateTime','Width=200,Height=280');
	}	
	if (e.value!=cPreValue) if (cEvent) eval(cEvent);
	try{ e.onchange(); } catch(oErr2) {}
	return cOut;
}


//==========================================================
// CLOCK TOOL (TIME VALUES)
//==========================================================

//--------------------
//- Modified: 23-May-2001, Antonio Olmedo Soler
//- Syntax__: Input_Clock(e,[cEvent],[cMin],[cMax])
//- Description: 
// NOTE: Pending to develop: Validate that the value is between cMin and cMax 
//       (Idea: Use duration functions to convert to numeric value -Minutes-)
//       Allow to configure onfiguratio of hours/minutes (reduced/complete)
//--------------------
function Input_Clock (e) {
	//--- Optional arguments
	var cEvent='';
	var cMin='';
	var cMax='';
	if (arguments.length>1) if (arguments[1]) cEvent=arguments[1];
	if (arguments.length>2) if (arguments[2]) cMin=arguments[2];
	if (arguments.length>3) if (arguments[3]) cMax=arguments[3];
	//---
	var cLink=DIR_ROOT+'ui_clock.asp';
	var cOut='';
	//---
	window.timeField=e;
	if (document.all) {
		var cFeatures='dialogWidth:260px;dialogHeight:300px;center:yes;border:thin;';
		cFeatures+='location:no;status:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,e,cFeatures);
	} else {
		window.open(cLink,'dlgClock','Width=200,Height=430');
	}
	if (cEvent) eval(cEvent);
	return cOut;
}


//==========================================================
// TEXT ZOOM TOOL (MEMO VALUES)
//==========================================================

//--------------------
//- Modified: 24-Mar-2003, Antonio Olmedo Soler
//- Syntax__: Input_ZoomText(e,[cTitle],[cParameters])
//- Description: 
//--------------------
function Input_ZoomText (e) {
	var cLink=DIR_ROOT+'ui_zoomtext.asp';
	//---
	var cTitle		='';
	var cParameters='';
	if (arguments.length>1) if (arguments[1]) cTitle=arguments[1];
	if (arguments.length>2) if (arguments[2]) cParameters=arguments[2];
	//---
	if (cTitle)			cLink+='?Title='+escape(cTitle);
	if (cParameters)	cLink+=cParameters;
	//---
	var cOut='';
	window.oTarget=e;
	if (document.all) {
		//alert('Input_ZoomText: '+e.length);
		var cFeatures='dialogWidth:700px;dialogHeight:500px;center:yes;border:thin;';
		cFeatures+='location:no;status:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,e,cFeatures);
	} else {
		window.open(cLink,'dlgZoomText','Width=400,Height=400');
	}	
	return cOut;
}

//--------------------
//- Created_: 06-Nov-2001, Antonio Olmedo Soler
//- Modified: 23-Oct-2006, Antonio Olmedo Soler
//- Syntax__: Input_EditHtml(e,[cHtmlField='HtmlEditable'],[cTitle],[cParameters],[cMainPath=location.href])
//- Description: 
//--------------------
function Input_EditHtml (e) {
	//--- Optional arguments
	var cHtmlField	='HtmlEditable';
	var cTitle		='Editar texto con formato';
	var cParameters='';
	var cMainPath	=location.href;
	if (arguments.length>1) if (arguments[1]) cHtmlField=arguments[1];
	if (arguments.length>2) if (arguments[2]) cTitle=arguments[2];
	if (arguments.length>3) if (arguments[3]) cParameters=arguments[3];
	if (arguments.length>4) if (arguments[4]) cMainPath=arguments[4];
	//alert('cHtmlField='+cHtmlField);
	//--- If Browser is lower than IE 5.5, use Raw Text Zoom Editor
	//<<<var cVersion=navigator.appVersion;
	//<<<if ((cVersion.indexOf('MSIE 5.5')<0) && (cVersion.indexOf('MSIE 6.')<0)) {
	//<<<	return Input_ZoomText(e,cTitle);
	//<<<}
	//---
	//var cLink=DIR_ROOT+'ui_htmledit.asp?Title='+cTitle;
	//if (cParameters)	cLink+=cParameters;
	//if (cMainPath)		cLink+='&MainPath='+escape(cMainPath);
	//---
	var cLink=DIR_ROOT+'ui_wizard.asp?Page=ui_htmledit.asp&Title='+escape(cTitle);
	if (cParameters)	cLink+=cParameters;
	if (cMainPath)		cLink+='&MainPath='+escape(cMainPath);
	//alert(cLink);
	//---
	var cOut='';
	window.oTarget=e;
	if (document.all) {
		var cFeatures='dialogWidth:800px;dialogHeight:560px;center:yes;border:thin;scroll:no;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=window.showModalDialog(cLink,e,cFeatures);
		//alert('Input_EditHtml:  ['+cOut+']');
		if ((''+cOut)!='undefined') {
			var e2	=null;
			if (!e2) e2=document.all[cHtmlField];
			//if (e2) alert('1 cHtmlField:'+cHtmlField+'\n'+e2);
			if (!e2) e2=document.all('aux_'+e.name);
			//if (e2) alert('2 cHtmlField:'+cHtmlField+'\n'+e2);
			if (e2) {
				//alert('3 '+e2.innerHTML)
				//e2.innerHTML='<P class="NoValue">[ Cargando nuevo valor... ]</P>';
				cOut=HTML_CleanForm(cOut);
				e2.innerHTML=(cOut?cOut:MSG_NOVALUE);
			//} else {
			//	location.reload();
			}
			//if (oXForm) oXForm.onEditField(e.name);
		}
	} else {
		window.open(cLink,'dlgEditHtml','Width=500,Height=400');
	}
	return cOut;
}

//--------------------
//- Created_: 13-Jul-2004, Antonio Olmedo Soler
//- Modified: 05-Sep-2004, Antonio Olmedo Soler
//- Syntax__: Input_EditPage(e,cHtmlField,cTitle,cParameters)
//- Description: 
//--------------------
function Input_EditPage (e,cHtmlField,cTitle,cParameters) {
	var cOut=Input_EditHtml(e,cHtmlField,cTitle,cParameters);
	return void(null);
}

//==========================================================
// COLOR PICKER TOOL
//==========================================================

//--------------------
//- Created_: 28-Mar-2001, Antonio Olmedo Soler
//- Modified: 08-Feb-2002, Antonio Olmedo Soler
//- Syntax__: Input_Color(e,[cTitle])
//- Description: 
//--------------------
function Input_Color (e) {
	var cLink=DIR_ROOT+'ui_colorpick.asp';
	var cOut='';
	//---
	if (arguments.length>1) if (arguments[1]) cLink+='?Title='+escape(arguments[1]);
	//---
	window.oTarget=e;
	if (document.all) {
		var cFeatures='dialogWidth:620px;dialogHeight:250px;center:yes;border:thin;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,e,cFeatures);
		//if (oXForm) oXForm.onEditField(e.name);
	} else {
		window.open(cLink,'dlgColorPick','Width=400,Height=400');
	}	
	return cOut;
}

//==========================================================
// BOX LAYOUT EDITOR TOOL
//==========================================================
//--------------------
//- Created_: 27-Mar-2001, Antonio Olmedo Soler
//- Modified: 08-Feb-2002, Antonio Olmedo Soler
//- Syntax__: Input_BoxLayout(e,[cTitle])
//- Description: 
//--------------------
function Input_BoxLayout (e) {
	var cLink=DIR_ROOT+'ui_boxlayout.asp';
	var cOut='';
	//---
	if (arguments.length>1) if (arguments[1]) cLink+='?Title='+escape(arguments[1]);
	//---
	window.oTarget=e;
	//alert('Before: '+e.value);
	if (document.all) {
		var cFeatures='dialogWidth:450px;dialogHeight:330px;center:yes;border:thin;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,e,cFeatures);
		//if (oXForm) oXForm.onEditField(e.name);
	} else {
		window.open(cLink,'dlgBoxLayout','Width=400,Height=410');
	}	
	return cOut;
}


//==========================================================
// OBJECT SELECTION TOOL (OBJECT VALUES)
//==========================================================

//--------------------
//- Modified: ??-???-2001, Antonio Olmedo Soler
//- Modified: 21-Mar-2002, Antonio Olmedo Soler
//- Syntax__: Input_ObjectChoose(nClassId,[e],[cSelected],[bMultiple=false],[cParams],[cFoo])
//- Description: 
//--------------------
function Input_ObjectChoose(nClassId) {
	//·alert('Input_ObjectChoose('+nClassId+','+e+','+cSelected+','+bMultiple+','+cParams+')');
	//--- Optional arguments	
	var e=null;
	var cSelected='';
	var bMultiple=false;
	var cParams='';
	var cFoo='';
	var nI=0;
	if (arguments.length>nI++) if (arguments[nI]) e=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cSelected=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]!=null) bMultiple=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cParams=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cFoo=arguments[nI];
	//---
	var cTitle='Select '+KB_Class(nClassId,'name');
	var cIcon=String_Replace(KB_Class(nClassId,'icon'),'16','32');
	var cLink;
	var cId='';
	//---
	//alert('Input_ObjectChoose('+nClassId+')');
	cLink=DIR_ROOT+'wiz_object_select.asp?Class='+nClassId;
	if (cSelected)	cLink+='&Selected='+escape(cSelected);
	if (bMultiple) cLink+='&Multiple=Y'
	if (cParams)	cLink+='&'+escape(cParams);
	try { if (cFoo)		cLink+='&'+escape(eval(cFoo)); } catch(oErr2) {}
	var cFeatures='dialogWidth:800px;dialogHeight:600px;border:thick;center:yes;';
	cFeatures+='status:no;location:no;';
	var aSelection=Open_MicroWizard(cLink,cTitle,'',cIcon,null,cFeatures);
	if (!aSelection) return null; //- If the user cancels the selection, return null
	//	alert(aSelection[0]+'\n\n'+BANNER1+'\n\n'+aSelection[1]);
	if (aSelection) if (aSelection[0]) cId=aSelection[0];
	//--- If a form element was given update its contents
	if (e) {
		if (e.tagName) { //--- If the argument is a form field, update its associated label
			e.value=(aSelection?aSelection[0]:'');
			var eAux=document.all['aux_'+e.name];
			if (eAux) {
				var aId=new Array();
				var aLabel=new Array();
				if (aSelection) {
					if (aSelection[0]) {
						cId=aSelection[0];
						aId=aSelection[0].split(',');
						aLabel=aSelection[1].split('\n');
						//alert(aId+'\n\n'+BANNER1+'\n\n'+aLabel);
					}
				}
				eAux.innerHTML=UI_XList(nClassId,aId,aLabel,e.name);
				//if (oXForm) oXForm.onEditField(e.name);
			} else {
				alert('Error in function Input_ObjectChoose:\nCannot find form element aux_'+e.name); 
			}
		}
	}
	//--- Return the selected Id or list of Ids
	return cId; 
}



//==========================================================
// OBJECT SELECTION TOOL (OBJECT VALUES)
//==========================================================

//--------------------
//- Modified: 06-Feb-2003, Antonio Olmedo Soler
//- Modified: 06-Oct-2004, Antonio Olmedo Soler
//- Syntax__: Input_EditRelations(e,[cDbConn],[cOwner],[cTitle],[cTable1],[cTable2],[nId1],[cRole],[cType1],[cType2],[cMainPath=location.href])
//- Description: 
//--------------------
function Input_EditRelations(e) {
	//·alert('Input_EditRelation('+e+')');
	//--- Optional arguments
	var cDbConn	='';
	var cOwner	='';
	var cTitle	='';
	var cTable1	='';
	var cTable2	='';
	var nId1		='';
	var cRole	='';
	var cType1	='';
	var cType2	='';
	var cMainPath=location.href;
	var nI=0;
	if (arguments.length>nI++) if (arguments[nI]) cDbConn=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cOwner=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cTitle=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cTable1=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cTable2=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) nId1=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cRole=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cType1=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cType2=arguments[nI];
	if (arguments.length>nI++) if (arguments[nI]) cMainPath=arguments[nI];
	//---
	var cLink=DIR_ROOT+'ui_relations.asp?Title='+escape(cTitle);
	if (cDbConn)	cLink+='&Conn='+cDbConn;
	if (cOwner)		cLink+='&Owner='+cOwner;
	if (cTable1)	cLink+='&Rel_Table1='+cTable1;
	if (cTable2)	cLink+='&Rel_Table2='+cTable2;
	if (nId1)		cLink+='&Rel_Id='+nId1;
	if (cRole)		cLink+='&Rel_Role='+cRole;
	if (cType1)		cLink+='&Rel_Type1='+cType1;
	if (cType2)		cLink+='&Rel_Type2='+cType2;
	if (cMainPath) cLink+='&MainPath='+escape(cMainPath);
	//---
	var cOut='';
	window.oTarget=e;
	if (false) { //(document.all) {
		var cFeatures='dialogWidth:800px;dialogHeight:560px;center:yes;border:thin;scroll:no;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		//cOut=showModalDialog(cLink,e,cFeatures);
		if ((''+cOut)!='undefined') {
			var e2=document.all('aux_'+e.name);
			if (e2) e2.innerHTML=(cOut?cOut:MSG_NOVALUE);
			//if (oXForm) oXForm.onEditField(e.name);
		}
	} else {
		var cFeatures='toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=YES,resizable=YES,width=800,height=560,top=0,left=0,screenX=5,screenY=5';
		window.open(cLink,'dlgRelations',cFeatures);
	}		
	//--- Return the selected Id or list of Ids
	return cOut; 
}


//--------------------
//- Created_: 01-Oct-2004, Antonio Olmedo Soler
//- Modified: 15-Apr-2007, Antonio Olmedo Soler
//- Syntax__: Show_Help(cDatabase,cWebCode,cHelpCode,[cTitle],[nUserId],[cMainPath=location.href])
//- Description: 
//--------------------
function Show_Help (cDatabase,cWebCode,cHelpCode) {
	var cTitle		='Ayuda on-line';
	var nUserId		=''; //oThisUser.Id;
	var cMainPath	=location.href;
	if (arguments.length>3) if (arguments[3]) cTitle=arguments[3];
	if (arguments.length>4) if (arguments[4]) nUserId=arguments[4];
	if (arguments.length>5) if (arguments[5]) cMainPath=arguments[5];
	//---
	//var cLink=DIR_ROOT+'ui_help.asp';
	//cLink+='?Database='+cDatabase+'&WebCode='+cWebCode+'&HelpCode='+cHelpCode;
	//if (cTitle)		cLink+='&Title='+escape(cTitle);
	//if (nUserId)	cLink+='&User='+nUserId;
	//if (cMainPath)	cLink+='&MainPath='+escape(cMainPath);
	//alert(cLink);	
	var cLink=DIR_ROOT+'ui_wizard.asp?Page=ui_help.asp&Title='+escape(cTitle);
	cLink+='&Database='+escape(cDatabase)+'&WebCode='+escape(cWebCode)+'&HelpCode='+escape(cHelpCode);
	if (nUserId)	cLink+='&User='+nUserId;
	if (cMainPath)	cLink+='&MainPath='+escape(cMainPath);
	//alert(cLink);	
	//---
	var cOut='';
	if (document.all) {
		var cFeatures='dialogWidth:700px;dialogHeight:500px;center:yes;border:thin;resizable:no;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,null,cFeatures);
		//window.showHelp(cLink);
	} else {
		alert(2)
		window.open(cLink,'dlgHelp','Width=400,Height=400');
	}	
	return cOut;
}

//--------------------
//- Created_: 25-Oct-2006, Antonio Olmedo Soler
//- Modified: 17-Apr-2007, Antonio Olmedo Soler
//- Syntax__: Show_VirtofHelp(cTopic)
//- Description: 
//--------------------
function Show_VirtofHelp (cTopic) {
	//>>> Open Virtof.com page

	//<<< Detectar si hay conexion a Internet. Si no, mostrar ayuda de localhost
	var cLink='http://www.virtof.com/Virtof2/ui_help.asp';
	cLink+='?WebCode=VirtOf';
	cLink+='&HelpCode='+escape(cTopic);
	cLink+='&MainPath=http://www.virtof.com/'; 
	//if (nUserId)	cLink+='&User='+nUserId;
	//if (cMainPath)	cLink+='&MainPath='+escape(cMainPath);
	//>>> Pass Selected language
	//cLink+='&Lang='+...;
	cLink+='&Database=ok-systems.com.virtof2_virtof';
	//alert(cLink);	
	//---
	var cOut='';
	if (document.all) {
		var cFeatures='dialogWidth:700px;dialogHeight:500px;center:yes;border:thin;resizable:no;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		window.showModalDialog(cLink,null,cFeatures);
		//window.showModelessDialog(cLink,null,cFeatures);
		//window.showHelp(cLink,cTopic);
	} else {
		window.open(cLink,'dlgHelp','Width=600,Height=450');
	}	
	return cOut;
}

//--------------------
//- Created_: 06-Oct-2004, Antonio Olmedo Soler
//- Modified: 27-Jun-2006, Antonio Olmedo Soler
//- Syntax__: Input_Clipart(e,[cTitle],[cParameters],[cSelectedFile],[cImagesFolder],[cMainPath=location.href])
//- Description: 
//--------------------
function Input_Clipart (e) {
	//--- Optional arguments
	var cTitle			='Image selector' //Selector de imágenes'; 
	var cParameters		='';
	var cSelectedFile	='';
	var cImagesFolder			='';
	var cMainPath		=location.href;
	if (arguments.length>1) if (arguments[1]) cTitle=arguments[1];
	if (arguments.length>2) if (arguments[2]) cParameters=arguments[2];
	if (arguments.length>3) if (arguments[3]) cImagesFolder=arguments[3];
	if (arguments.length>4) if (arguments[4]) cSelectedFile=arguments[4];
	if (arguments.length>5) if (arguments[5]) cMainPath=arguments[5];
	//---
	var cLink=DIR_ROOT+'ui_wizard.asp?Page=ui_clipart.asp&Title='+escape(cTitle);
	if (cParameters)	cLink+=cParameters;
	if (cSelectedFile)	cLink+='&ImgFile='+escape(cSelectedFile);
	if (cImagesFolder)	cLink+='&ImagesFolder='+escape(cImagesFolder);
	if (cMainPath)		cLink+='&MainPath='+escape(cMainPath);
	//alert('Input_Clipart() '+String_Replace(cLink,'&','\n\t- '));
	//---
	var cOut='';
	window.oTarget=e;
	if (document.all) {
		var cFeatures='dialogWidth:800px;dialogHeight:560px;center:yes;border:thin;scroll:no;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=window.showModalDialog(cLink,e,cFeatures);
		if ((''+cOut)!='undefined') {
			//alert('Input_Clipart:  ['+cOut+']');
			//document.execCommand('InsertImage',false,cOut);
			return cOut;
		}
	} else {
		window.open(cLink,'dlgEditHtml','Width=500,Height=400');
		//>>>
	}	
	return cOut;
}

//--------------------
//- Created_: 17-May-2006, Antonio Olmedo Soler
//- Modified: 20-Jul-2011, Antonio Olmedo Soler
//- Syntax__: Input_UploadFile(cTargetFolder,[cTitle],[cMessage],[cLang],[cAltLang])
//- Description: 
//- Returns_: String (File name, if successfully loaded).
//--------------------
function Input_UploadFile (cTargetFolder) {
   var cOut='';
	//alert('Input_UploadFile('+cTargetFolder+')');
	try { 
		//---
		var cTitle		='Cargar archivo';
		var cMessage   	='';
		var cLang		='';
		var cAltLang	='';
		if (arguments.length>1) if (arguments[1]) cTitle=arguments[1];
		if (arguments.length>2) if (arguments[2]) cMessage=arguments[2];
		if (arguments.length>3) if (arguments[3]) cLang=arguments[3];
		if (arguments.length>4) if (arguments[4]) cAltLang=arguments[4];
		//---
		var cLink=DIR_ROOT+'ui_upload_file.asp?TargetFolder='+escape(cTargetFolder);
		if (cTitle)    	cLink+='&Title='+escape(cTitle);
		if (cMessage)  	cLink+='&Message='+escape(cMessage);
		if (cLang) 		cLink+='&Lang='+cLang;
		if (cAltLang) 	cLink+='&AltLang='+cAltLang;
		cLink+='&MainPath='+escape(location.href);
		//alert(cLink);	
		//---
		var cFeatures='dialogWidth:600px;dialogHeight:340px;center:yes;border:thin;';
		cFeatures+='status:no;location:no;minimize:no;maximize:no;';
		cOut=showModalDialog(cLink,null,cFeatures);
	} catch(oError) {
	   //Debug_Function('Input_UploadFile',arguments,oError); }
	}
	return cOut;
}

