﻿/*********************************************************************
*** (C) OK-Systems,2007 (All rights reserved)                      ***
*** Don't make any changes without reporting to oks@ok-systems.com ***
**********************************************************************
*** File: fu_ui.js                                                 ***
*** Project: Virtual Office                                        ***
*** Language: JavaScript                                           ***
*** Description: User Interface Functions                          ***
*********************************************************************/

//--------------------
//- Created_: 01-Jun-2002, Antonio Olmedo Soler
//- Modified: 01-Jun-2002, Antonio Olmedo Soler
//- Syntax__: Window_Resize([nWidth=800],[nHeight=600])
//- Description: 
//--------------------
function Window_Resize() {
	//--- Optional arguments
	var nWidth	=800;
	var nHeight	=600;
	if (arguments.length>0) if (arguments[0]) nWidth=arguments[0];
	if (arguments.length>1) if (arguments[1]) nHeight=arguments[1];
	//---
	//external.menuArguments.top.resizeTo(nWidth,nHeight);
	//external.menuArguments.top.moveTo(0,0);
	window.resizeTo(nWidth,nHeight);
	window.moveTo(0,0);
	//---
	return void(null);
}

//--------------------
//- Created_: 19-Nov-2002, Antonio Olmedo Soler
//- Modified: 10-Oct-2004, Antonio Olmedo Soler
//- Syntax__: Page_Preview ()
//- Description: Hides toolbars and auxiliar buttons, and removes backgrouns, to allow a clean print of any page
//--------------------
function Page_Preview () {
	//alert('Page_Preview');
	try {
		var oElement,cClassName;
		//var cList='';
		//---
		document.body.style.background='white';
		for (var i in document.all) {
			oElement=document.all[i];
			cClassName=oElement.className;
			if (cClassName) {
				//cList+=' '+cClassName;		
				switch (cClassName.toLowerCase()) {
				case 'toolbar':		oElement.style.display='none';     break;
				case 'footerbar':		oElement.style.display='none';     break;
				//case 'menu':			oElement.style.display='none';     break;
				case 'adds':			oElement.style.display='none';     break;
				case 'marcoderecho':	oElement.style.display='none';     break;
				case 'noprint':		oElement.style.display='none';     break;
				case 'menuback':		oElement.style.display='none';     oElement.style.border='0px'; break;
				case 'pagebody':		oElement.style.background='white'; oElement.style.border='0px'; break;
				case 'pageheader':	oElement.style.background='white'; oElement.style.border='0px'; break;
				case 'pagefooter':	oElement.style.background='white'; oElement.style.border='0px'; break;
				}
			}
		}
		window.print();
		//alert(cList);
	} catch(oError) { 
		//alert(oError.description); 
	}
}


//--------------------
//- Created_: 27-Oct-2007, Antonio Olmedo Soler
//- Modified: 27-Oct-2007, Antonio Olmedo Soler
//- Syntax__: Page_Change(nPag)
//- Description: 
//--------------------
function Page_Change(nPag) {
	try {
		if (nPag<=0) nPag=prompt('Introduce el número de página: ','');
		if (nPag) location.href='?pag='+nPag;
	} catch(ex) { }
	return void(null);
}

//--------------------
//- Created_: 14-Dec-2005, Antonio Olmedo Soler
//- Modified: 14-Dec-2005, Antonio Olmedo Soler
//- Syntax__: VirtofMenu_Show ()
//- Description: 
//--------------------
var bMenuInvisible;
function VirtofMenu_Show () {
	try {
		var oMenu=document.getElementById('VirtofMenu');
		if (!oMenu) return void(null);
		bMenuInvisible=!bMenuInvisible;
		//alert(oMenu+' bMenuInvisible='+bMenuInvisible);
		//---
		var oCell;
		var cList='';
		//---
		for (var c in oMenu.cells) {
			oCell=oMenu.cells[c];
			//cList+=oCell.tagName+'.'+oCell.className+' ['+oCell.parentElement+']'+'\n';
			switch (oCell.className) {
			case 'Item':
			case 'ItemSelected':
			case 'ItemPushed':
				oCell.parentElement.style.display=(bMenuInvisible?'none':'');
				break;
			case 'MenuHeader':
			case 'MenuFooter':
				oCell.width='16px;'
				break;
			}	
		}
		if (cList) alert(cList);
	} catch(oError) { 
		//alert(oError.description); 
	}
	return void(null);
}


//--------------------
//- Created_: 20-Dec-2005, Antonio Olmedo Soler
//- Modified: 26-Dec-2005, Antonio Olmedo Soler
//- Syntax__: Div_ScrollSwap(cElementName)
//- Description: 
//--------------------
function Div_ScrollSwap(cElementName) {
	var oElement=document.getElementById(cElementName);
	if (oElement) oElement.style.overflow=(oElement.style.overflow!='visible'?'visible':'auto');
	return void(null);
}

//--------------------
//- Created_: 19-May-2009, Antonio Olmedo Soler
//- Modified: 19-May-2009, Antonio Olmedo Soler
//- Syntax__: Element_SetClass(oElement,cClassName)
//- Description: 
//--------------------
function Element_SetClass(oElement,cClassName) {
	try {
		if (typeof(oElement)=='string') oElement=document.getElementById(oElement);
		if (oElement) oElement.className=cClassName;
	} catch(oError) { alert('Element_SetClass()\nError:\n\n'+oError.message); }
	return void(null);
}

//--------------------
//- Created_: 19-May-2009, Antonio Olmedo Soler
//- Modified: 19-May-2009, Antonio Olmedo Soler
//- Syntax__: Element_SwapClass(oElement,cClass1,cClass2)
//- Description: 
//--------------------
function Element_SwapClass(oElement,cClass1,cClass2) {
	try {
		if (typeof(oElement)=='string') oElement=document.getElementById(oElement);
		if (oElement) oElement.className=((oElement.className==cClass1)?cClass2:cClass1);
//alert('Element_SwapClass\noElement='+oElement+'\noElement.className='+oElement.className);
	} catch(oError) { alert('Element_SwapClass()\nError:\n\n'+oError.message); }
	return void(null);
}

//--------------------
//- Created_: 19-May-2009, Antonio Olmedo Soler
//- Modified: 19-May-2009, Antonio Olmedo Soler
//- Syntax__: Element_Hover(oTR,bSelect)
//- Description: 
//--------------------
function Element_Hover(oElement,bSelect) {
	try {
		if (!oElement) return false;
		oElement.style.background=(bSelect?'#ffe':'');		
		oElement.style.color=(bSelect?'#900':'');		
		//oElement.className=(bSelect?'Hover':'');		
	} catch(oError) { alert('Element_Hover()\nError:\n\n'+oError.message); }
	return false;
}

//--------------------
//- Created_: 14-Jan-2006, Antonio Olmedo Soler
//- Modified: 14-Jan-2006, Antonio Olmedo Soler
//- Syntax__: Checkbox_SelectAll(cElementName,bChecked)
//- Description: 
//--------------------
function Checkbox_SelectAll(cElementName,bChecked) {
	var aElements=document.getElementsByName(cElementName);
	//alert('aElements: '+aElements.length);
	var i=0;
	if (aElements) {
		for (var i=0; i<aElements.length; i++) {
			e=aElements[i];
			if (e.tagName=='INPUT') e.checked=bChecked;
		}		
	}
	return void(null);
}




//==================================================
// IMAGE ELEMENT FUNCTIONS
//==================================================

//--------------------
//- Created_: 26-Oct-2001, Antonio Olmedo Soler
//- Modified: 01-May-2003, Antonio Olmedo Soler
//- Syntax__: Image_Error(e,[cImage])
//- Description: This function can be assigned to the onerror event of any IMG
//--------------------
/*
function Image_Error(e) {
	try {
		var cImage='';
		if (arguments.length>1) if (arguments[1]) cImage=arguments[1];
		//---
		if (cImage) if (e.src!=cImage) { e.src=cImage; return void(null);	} 
		//---
		//alert('Image_Error()\n'+e.name+'.src = '+e.src);
		if (e.src!=DIR_IMG+'ico32_error.gif') e.src=DIR_IMG+'ico32_error.gif';
	} catch(oError) {}
	return void(null);
}
*/

//--------------------
//- Created_: 22-Apr-2006, Antonio Olmedo Soler
//- Modified: 27-Dec-2006, Antonio Olmedo Soler
//- Syntax__: IMG_Error(eIMG,[cAlternativeImage])
//- Description: Show alternative image, or hide image if there is an error loading the file
//--------------------
function IMG_Error(eIMG) {
   try {
		//--- Optional arguments
		var cAlternativeImage=DIR_IMG+'ico32_nofoto.gif';
		if (arguments.length>1) if (arguments[1]!=null) cAlternativeImage=arguments[1];
		//---
		var cOriginal=eIMG.src;
		//alert('IMG_Error cOriginal');
		if (cOriginal.match(cAlternativeImage)) {
			return void(null);
		} else if (eIMG.alt.substr(0,6)=='ERROR:') {
			return void(null);
		} else if (cOriginal!=cAlternativeImage) {
			eIMG.alt='ERROR: File not found:\n'+cOriginal;
			eIMG.src=cAlternativeImage;
		}
	} catch(oError) { Debug_Function('IMG_Error',oError,arguments); }
	return void(null);
}

//--------------------
//- Created_: 27-Dec-2006, Antonio Olmedo Soler
//- Modified: 27-Dec-2006, Antonio Olmedo Soler
//- Syntax__: IMG_Zoom(eIMG,[cFile],[cTarget])
//- Description: 
//--------------------
function IMG_Zoom(eIMG) {
   try {
		//--- Optional arguments
		var cFile	='';
		var cTarget	='Zoom';
		if (arguments.length>1) if (arguments[1]!=null) cFile=arguments[1];
		if (arguments.length>2) if (arguments[2]!=null) cTarget=arguments[2];
		//--- 
		if (!cFile) cFile=String_Replace(eIMG.src,'p_','');
		//alert('IMG_Zoom '+cFile);
		//---
		var cFeatures = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600,top=0,left=0,dependent=yes';
		var oWindow=window.open(cFile,cTarget,cFeatures);
	} catch(oError) {}
	return void(null);
}

//--------------------
//- Created_: 27-Dec-2006, Antonio Olmedo Soler
//- Modified: 27-Dec-2006, Antonio Olmedo Soler
//- Syntax__: IMG_AlbumLoad()
//- Description: 
//--------------------
function IMG_AlbumLoad() {
   try {
		//alert('IMG_AlbumLoad()');
		var eAlbum=document.getElementById('Album');
		if (!eAlbum) return void(null);
		//---
		var aIMG=eAlbum.getElementsByTagName('IMG');
		//alert('aIMG.length='+aIMG.length);
		var cOut='';
		var eIMG;
		//for (var i in aIMG) {
		for (var i = 0; i < aIMG.length; i++) {
			eIMG=aIMG[i];
			eIMG.onerror=function() { IMG_Error(this); }
			//eIMG.style.border='8px dashed red';
			eIMG.style.margin='8px';
			if (eIMG.src.match('ico32_nofoto.gif')) {
				eIMG.onclick=function() { }
				//eIMG.onerror=function() { }
				eIMG.style.cursor='';
			} else {
				eIMG.onclick=function() { IMG_Zoom(this); }
				eIMG.style.cursor='pointer';
				eIMG.alt=String_SubStrAfter(eIMG.src,'/').replace('p_','');
				eIMG.alt=String_SubStrBefore(eIMG.alt,'.').replace('_',' ');
			}
			//eIMG.onerror=function() { alert(999); }
			//cOut+=eIMG.src+'\n';
		}
		//alert(cOut);
	} catch(oError) { Debug_Function('IMG_AlbumLoad',oError,arguments); }
	return void(null);
}



//==================================================
// UNOBTRUSIVE FUNCTIONS
//==================================================

//--------------------
//- Created_: 27-Dec-2006, Antonio Olmedo Soler
//- Modified: 27-Dec-2006, Antonio Olmedo Soler
//- Syntax__: addEvent(obj, evType, fn)
//- Description: 
//- Reference: http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html
//--------------------
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent('on'+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
