//
// Copyright 2006 Nelson Daza. nelson.daza@gmail.com. All rights reserved.
// DOM HTML 
// ----------------------------------------------------
//
// Functions.js
//
//	version 2.2.8.4 major.minor[.revision[.build]]
//
	
	/* START Pop-up Windows Script */

	function popup ( url, name, width, height, posx, posy, features )	{
		if ( typeof( posx ) == 'undefined' )
			posx = 'center';
		if ( typeof( posy ) == 'undefined' )
			posy = 'middle';
		if ( typeof( features ) == 'undefined' )
			features = '';

		if ( posx.toLowerCase ( ) == 'center' )
			posx = ( screen.width - width ) / 2;
		else if ( posx.toLowerCase ( ) == 'right' )
			posx = (screen.width - width - 30);
		else if ( posx < 0 )
			posx = screen.width - width + posx;
		else 
			posx = 0;
	
		if ( posy.toLowerCase ( ) == 'middle' )
			posy = ( screen.height - height ) / 2;
		else if ( posy.toLowerCase ( ) == 'bottom' )
			posy = ( screen.height - height - 60 );
		else if ( posy < 0 )
			posx = screen.height - height - posy - 30;
		else 
			posy = 0;
	
		var func = window.open;
		if( features.indexOf( "dependent=yes" ) >= 0 && window.showModalDialog )
			func = window.showModalDialog;
		
		if ( typeof( name ) == 'undefined' || typeof( width ) == 'undefined' || typeof( height ) == 'undefined' )
			return func( url );
		else
			return func( url, name, 'dialogHeight:' + height + 'px; dialogWidth:' + width + 'px; dialogLeft:' + posx + 'px; dialogTop:' + posy + 'px;,' + 'width=' + width + ',height=' + height + ',screenX=' + posx + ',left=' + posx + ',screenY=' + posy + ',top=' + posy + ',' + features );
	}

	function showSWF( filePath, id, width, height, target, bgcolor, align, quality, flashvars, wmode, allowScriptAccess )	{
		
		if( !bgcolor )
			bgcolor = '#FFFFFF';
		if( !align )
			align = 'center';
		if( !quality )
			quality = 'high';
		if( !flashvars )
			flashvars = '';
		if( !wmode )
			wmode = 'transparent';
		if( !allowScriptAccess )
			allowScriptAccess = 'sameDomain';
		
		var sHTML = '' +
			'<object type="application/x-shockwave-flash" ' + 
			'	data="' + filePath + '" ' + 
			'	width="' + width + '" ' + 
			'	height="' + height + '" ' + 
			'	id="' + id + '" ' + 
			'	align="' + align + '">' + 
			'		<param name="flashvars" value="' + flashvars + '">' + 
			'		<param name="allowScriptAccess" value="' + allowScriptAccess + '" />' + 
			//'		<param name="scale" value="' + scale + '">' + 
			'		<param name="movie" value="' + filePath + '" />' + 
			'		<param name="quality" value="' + quality + '" />' + 
			'		<param name="bgcolor" value="' + bgcolor + '" />' + 
			'		<param name="wmode" value="' + wmode + '">' + 
			'</object>';

		if ( target )
			target.innerHTML = sHTML;
		else
			document.write( sHTML );
	}

	/* Interval script */
	function callInterval( func , obj, time, interval )	{
		if( typeof( func ) != 'function'  )
			func = new Function( func );
		if( typeof( interval ) == 'undefined'  )
			interval = 'interval';

		obj = toObject( obj );
		window.clearInterval( obj[interval] );
		obj[interval] = window.setInterval( func, time );
	}
	
	function stopInterval( obj, interval )	{
		if( typeof( interval ) == 'undefined'  )
			interval = 'interval';

		obj = toObject( obj );
		window.clearInterval( obj[interval] );
	}
	
	function trace( text, clear )	{
		var elem = document.getElementById( 'trace_element' );
		if( !elem )	{
			elem = document.createElement( 'div' );
			elem.id = 'trace_element';
			elem.className = 'text table_border';
			elem.style.backgroundColor = '#EEEEEE';
			elem.style.padding = '5px';
			elem.style.width = '50%';
			elem.style.height = '30px';
			elem.style.position = 'absolute';
			elem.style.top = "0px";
			elem.style.left = "0px";
			elem.style.overflow = "auto";
			document.body.appendChild( elem );
		}
		elem.innerHTML = ( clear ? '' : elem.innerHTML ) + '<br />' + text;
	}

	function isMouseOut ( e, obj, action )	{
		obj = toObject( obj );
		if( obj.outAction )
			return;

		obj.outAction = true;
		if( typeof( action ) != 'function'  )
			action = new Function( action );

		var relTarget;
		if ( !e )
			e = window.event;
		if ( e.relatedTarget )
			relTarget = e.relatedTarget;
		else if ( e.toElement )
			relTarget = e.toElement;
		while( relTarget && relTarget != obj )
			relTarget = relTarget.parentNode;
		if ( !relTarget )
			action( );
		obj.outAction = false;
	}
	/* END Pop-up Windows Script */
	
	var font_size = 9;
	var MAX = 17;
	var MIN = 11;
	
	function changeFont( num )	{
		font_size += num;
		if( font_size > MAX )
			font_size = MAX;
		if( font_size < MIN )
			font_size = MIN;
		 document.getElementsByTagName( 'body' )[0].style.fontSize = font_size + 'px';
	}
	
	function validateKey ( evt, key )	{
		if ( !evt )
			evt = window.event;

		if ( evt.keyCode && evt.keyCode == key )
			return true;
		else if ( evt.charCode && evt.charCode == key )
			return false;

		return false;
	}

	function objectToString ( obj, ident, maxlevel )	{
		var string = "";
		var tab = "";
		ident = ( ident ? ident + 1 : 0 );
		maxlevel = ( maxlevel ? maxlevel -1 : 1 );
		
		if ( maxlevel == -1 )
			return;
		
		for ( var c = 0; c < ident; c ++ )
			tab += "  ";
		string += tab + "[" + typeof(obj) + "]" + obj;
		
		ident ++;
		tab += "  ";
		
		for ( subObj in obj )	{
			string += "\n" + tab + "[" + subObj + "] => " + obj[subObj];
			if( typeof( obj[subObj] ) == 'object' && obj[subObj] && confirm ( typeof(obj[subObj]) + obj[subObj] ) )
				string += "\n" + objectToString ( subObj, ident, maxlevel );
		}
		return string;
	}
	
	function toObject ( something )	{
		if ( typeof( something ) == 'string' )
			return document.getElementById ( something );
		if ( typeof( something.nodeName ) != 'undefined' )
			return something;
		return null;
	}
	
	function jumpTo ( url )	{
		document.location.href = url;
	}
	
	function hideElement ( something )	{
		something = toObject ( something );
		if ( something )	{
			something.style.visibility = 'hidden';
			something.style.display = 'none';
		}
	}
	
	function showElement ( something )	{
		something = toObject ( something );
		if ( something )	{
			something.style.visibility = 'visible';
			something.style.display = 'block';
		}
	}
	
	function changeVisibility ( something )	{
		something = toObject ( something );
		if ( something )	{
			if ( something.style.visibility == 'visible' )
				hideElement ( something );
			else
				showElement ( something );
		}
	}
	
	function setElementPosition( something, posx, posy )	{
		something = toObject( something );
		something.style.position = 'absolute';
		something.style.left = posx + 'px';
		something.style.top = posy + 'px';
	}
	
	function getElementRec ( something )	{
		something = toObject( something );
		var tmp = something;
		var nLeft = 0;
		var nTop = 0;
		while ( tmp && typeof( tmp.offsetLeft ) != 'undefined' && typeof( tmp.offsetTop ) != 'undefined' )	{
			nLeft += Number( tmp.offsetLeft );
			if( tmp.nodeName.toLowerCase( ) != 'td' )
				nTop += Number( tmp.offsetTop );
			tmp = tmp.parentNode;
		}
		
		return { left: nLeft, top: nTop, right: (nLeft + Number( something.offsetWidth )), bottom: (nTop + Number( something.offsetHeight )), width : something.offsetWidth, height: something.offsetHeight };
	}
	
	function setElementOpacity ( something, opacity )	{
		something = toObject( something );
		if( document.all )
			something.style.filter = 'alpha(opacity=' + ( opacity * 100 ) + ')';
		else
			something.style.MozOpacity = opacity;
	}
	
	function isDate ( year, month, day )	{
		if ( year.toString().length == 0 )
			return false;
		if ( day == null )	{
			month = ( month == null ) ? '-' : month;
			if ( year.toString().indexOf( month ) == -1 )
				return false;
			
			var arr = year.toString().split( month );
			if ( arr.length != 3 )
				return false;
			year = arr[0];
			month = arr[1];
			day = arr[2];
		}
		month = month - 1;  // javascript month range : 0- 11
		var tempDate = new Date ( year, month, day );
		var nyear = ( tempDate.getYear ( ) < 1000 ) ? tempDate.getYear ( ) + 1900 : tempDate.getYear ( )
		
		return ( ( nyear == year ) && ( month == tempDate.getMonth ( ) ) && ( day == tempDate.getDate ( ) ) );
	}
	
	function imagenChange ( image, source )	{
		image = toObject ( image );
		if ( image )
			image.src = source;
	}
	
	function checkMaxChars( something, maxChars ){
		something = toObject ( something );
		if ( something && something.value.length > maxChars )
			something.value = something.value.substring( 0, maxChars );
	}
	
	function innerChange ( something, child )	{
		something = toObject ( something );
		if ( something )	{
			while ( something.hasChildNodes( ) )
				something.removeChild( something.firstChild );
			if ( typeof ( text ) == 'string' )
				something.appendChild( document.createTextNode( child ) )
			else
				something.appendChild( child )
		}
	}
	
	function bookMark ( value )	{
		var bookData = new Array ( );
		bookData = value.split ( '|' );
		if ( document.all )
			window.external.AddFavorite ( bookData[0], bookData[1] );
		else
			alert( 'Lo Sentimos, los usuarios de Netscape o Mozilla deben agregar a \nfavoritos manualmente desde el menu o haciendo uso de <Ctrl-D>' );
	}

	function moveElement ( elem, nX, nY, nXStepIn, nYStepIn, bPercent, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style;
		var nDirX = 0;
		var nDirY = 0;
		var nXStep = nXStepIn;
		var nYStep = nYStepIn;
		
		if ( nXStep <= 0 )
			nXStep = 1;
	
		if ( nYStep <= 0 )
			nYStep = 1;
			
		var left = Number ( style.left.replace( 'px', '' ) );
		var top = Number ( style.top.replace( 'px', '' ) );
		
		var deficit = Math.abs( nX - left );
		if ( left != nX && deficit > 0 )	{
			if ( bPercent )
				nXStep = Math.ceil ( nXStep * deficit / 100 ) + 2;
			
			if ( deficit < nXStep )
				nXStep = deficit;
			
			nDirX = ( nX - left >= 0 ? 1 : -1 );
			
			left += ( nXStep * nDirX );
			style.left = left + 'px';
		}

		deficit = Math.abs( nY - top );
		if ( top != nY && deficit > 0 )	{
			if ( bPercent )
				nYStep = Math.ceil ( nYStep * deficit / 100 ) + 2;

			if ( deficit < nYStep )
				nYStep = deficit;
			
			nDirY = ( nY - top >= 0 ? 1 : -1 );
			
			top += ( nYStep * nDirY );
			style.top = top + 'px';
		}
		
		if ( nDirX == 0 && nDirY == 0 )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				var func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "moveElement ( '" + elem.id + "', " + nX + ", " + nY + ", " + nXStepIn + ", " + nYStepIn + ", " + bPercent + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}


	var nSizeLimit = 1;
	var testing = 0;

	function resizeElement ( elem, nW, nH, nWStepIn, nHStepIn, bPercent, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style;
		var nDirW = 0;
		var nDirH = 0;
		var nWStep = nWStepIn;
		var nHStep = nHStepIn;
		
		if ( nWStep <= 0 )
			nWStep = 1;
	
		if ( nHStep <= 0 )
			nHStep = 1;
			
		var width = ( style.width ? style.width : elem.width );
		var height = ( style.height ? style.height : elem.height );
		
		width = ( width ? Number ( width.replace( 'px', '' ).replace( '%', '' ) ) : 0 );
		height = ( height ? Number ( height.replace( 'px', '' ).replace( '%', '' ) ) : 0 );
		
		if ( Math.abs( nW - width ) > 0 )	{
			if ( bPercent )
				nWStep = Math.ceil ( ( nWStepIn ) * Math.abs ( nW - width ) / 100 );
			
			if ( Math.abs( nW - width ) < nWStep || nWStep <= nSizeLimit )	{
				nDirW = 0;
				style.width = nW + 'px';
			}
			else	{
				nDirW = ( nW - width ) >= 0 ? 1 : -1;
				width += nWStep * nDirW;
				style.width = width + 'px';
			}
		}

		if ( Math.abs( nH - height ) > 0 )	{
			if ( bPercent )
				nHStep = ( nHStep ) * Math.abs( nH - height ) / 100;

			if ( Math.abs( nH - height ) < nHStep || nHStep <= nSizeLimit )	{
				nDirH = 0;
				style.height = nH + 'px';
			}
			else	{
				nDirH = ( nH - height ) >= 0 ? 1 : -1;
				height += nHStep * nDirH;
				style.height = height + 'px';
			}
		}

		if ( nDirW == 0 && nDirH == 0 )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				var func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "resizeElement ( '" + elem.id + "', " + nW + ", " + nH + ", " + nWStepIn + ", " + nHStepIn + ", " + bPercent + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}

	function fadeElement ( elem, nAlpha, nStep, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style;
		var actualAlpha = ( style.MozOpacity ? style.MozOpacity : 1 );
		var nDir = 0;
		var nTotal = 0;
		
		if( nStep <= 0 )
			nStep = 0.05;
		else if( nStep > 1 )
			nStep /= 100;
		
		if( nAlpha < 0 )
			nAlpha = 0;
		else if( nAlpha > 1 )
			nAlpha /= 100;
		
		nTotal = Math.abs( Number( nAlpha ) - Number ( actualAlpha ) );
		if ( nTotal > 0 )	{
			if ( nTotal < Math.abs( nStep ) + 0.01 )
				nStep = nTotal;
			
			nDir = ( Number( nAlpha ) - Number ( actualAlpha ) ) >= 0 ? 1 : -1;
			
			actualAlpha = ( Number( actualAlpha ) + Number( nStep * nDir ) );
			style.MozOpacity = actualAlpha;
			style.filter = 'alpha(opacity=' + parseInt( actualAlpha * 100 ) + ')';
		}

		if ( actualAlpha == nAlpha )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				var func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "fadeElement ( '" + elem.id + "', " + nAlpha + ", " + nStep + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}

	/* START SortTable Script */

	function sortTable ( column )	{
		column = toObject ( column );
		if ( column )	{
			var table = findParentNode ( column, 'table' );
			if ( table == null )
				return;
			var groups = table.getElementsByTagName ( 'tbody' );
			for(var i = 0; i < groups.length; i++)	{
				sortTableRows ( groups[i], column.parentNode.cellIndex );
			}
		}
	}
	
	function sortTableRows ( group, col )	{
		var oRows = new Array ( );	//set the rows to be removed as an array of cloneNodes
		var iRows = new Array ( );	//set those rows' indexes as array

		for ( var c = 0; c < group.rows.length; c++ )	{
			oRows[c] = group.rows[c].cloneNode ( true );
			oRows[c].onmouseover = group.rows[c].onmouseover;
			oRows[c].onmouseout = group.rows[c].onmouseout;
			oRows[c].onclick = group.rows[c].onclick;
			oRows[c].ondblclick = group.rows[c].ondblclick;
			
			oRows[c].cells[col].onmouseover = group.rows[c].cells[col].onmouseover;
			oRows[c].cells[col].onmouseout = group.rows[c].cells[col].onmouseout;
			oRows[c].cells[col].onclick = group.rows[c].cells[col].onclick;
			oRows[c].cells[col].ondblclick = group.rows[c].cells[col].ondblclick;
			
			iRows[c] = group.rows[c].sectionRowIndex;
		}
		var oCol = new Array ( );			//set the string content of column cells as array
		var vCol = new Array ( );			//set the "compare" array for a future sort/reverse

		for ( c = 0; c < iRows.length; c++ )	{
			if ( group.rows[c].cells[col] && group.rows[c].cells[col].firstChild )	{
				if( group.rows[c].cells[col].firstChild.nodeName.toLowerCase( ) == 'img' )
					vCol[c] = oCol[c] = [group.rows[c].cells[col].firstChild.nextSibling.nodeValue,iRows[c]];
				else
					vCol[c] = oCol[c] = [group.rows[c].cells[col].firstChild.nodeValue,iRows[c]];
			}
			else
				vCol[c] = oCol[c] = ['',iRows[c]];
		}

		oCol.sort ( compareAsString );	//sorts the content array

		if ( vCol.toString ( ) == oCol.toString ( ) )
			oCol.reverse ( );	//if the content was already sorted, reverse

		for ( c = 0; c < group.rows.length; c++ )	{
			group.replaceChild ( oRows[oCol[c][1]], group.rows[c] );	//writes the rows in a sorted/reversed order
		}
	}

	/* END SortTable Script */

	function compareAsString ( obj1, obj2 )	{
		var str1 = obj1.toString( ).toLowerCase( );
		var str2 = obj2.toString( ).toLowerCase( );
		
		if ( str1.lastIndexOf ( ',' ) > 0  && str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( ) && !isNaN ( str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( ) ) )
			str1 = str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( );

		if ( str2.lastIndexOf ( ',' ) > 0  && str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( ) && !isNaN ( str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( ) ) )
			str2 = str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( );
		
		if ( str1 == str2 )
			return 0;

		if ( str1 && !isNaN ( str1 ) && str2 && !isNaN ( str2 ) )	{
			return Number( str1 ) - Number ( str2 );
		}
		return ( str1 > str2 ) ? 1 : -1;
	}
	
	function findParentNode ( something, parentName )	{
		var elem = toObject ( something );
		if ( elem )	{
			while ( elem.parentNode.nodeName.toLowerCase ( ) != parentName.toLowerCase ( ) && elem.parentNode.nodeName.toLowerCase ( ) != 'body' )
				elem = elem.parentNode;
	
			if ( elem.parentNode.nodeName.toLowerCase ( ) != parentName.toLowerCase ( ) )
				return null;
			else
				return elem.parentNode;
		}
	}
	
	function validateEmail ( email )	{
		var pos = 0;
		email = email.trim( );
		if ( email.trim( ).length > 7 && email.indexOf ( ' ', 0 ) == -1 )	{
			pos = email.indexOf ( '@', 0 );
			if ( pos > 2 && email.indexOf ( '@', pos ) && email.indexOf ( '.', pos ) > ( pos + 3 ) )	{
				pos = email.indexOf ( '.', pos );
				if ( pos > -1 && pos < email.length - 2 )
					return true;
			}
		}			
		return false;
	}
	
	/* START String general purpouse */
	
	String.prototype.trim = function ( )	{
		return this.replace( ' ', ' ' );
	}

