/****************************************************************
*
*	@filesource: common.js
*	@version: 0.3.3
*
*	@author: Alejandro D. Carraretto (ceo@logicpack.net)
*	@copyright: Agosto/2005
*
****************************************************************/


var agt = navigator.userAgent.toLowerCase() ;
//var is_major = parseInt( navigator.appVersion ) ;
//var is_minor = parseFloat( navigator.appVersion ) ;

var is_win 		= ( agt.indexOf( "win" ) != -1 ) ;
var is_os2 		= ( agt.indexOf( "os/2" ) != -1 ) ;
var is_mac 		= ( agt.indexOf( "mac" ) != -1 ) ;
var is_sun 		= ( agt.indexOf( "sunos" ) != -1 ) ;
var is_irix 	= ( agt.indexOf( "irix" ) != -1 ) ;
var is_hpux 	= ( agt.indexOf( "hp-ux" ) != -1 ) ;
var is_aix 		= ( agt.indexOf( "aix" ) != -1 ) ;
var is_linux 	= ( agt.indexOf( "inux" ) != -1 ) ;
var is_sco 		= ( agt.indexOf( "sco" ) != -1 ) ;
var is_freebsd	= ( agt.indexOf( "freebsd" ) != -1 ) ;
var is_bsd 		= ( agt.indexOf( "bsd" ) != -1 ) ;

var is_ie 		= ( agt.indexOf( "msie" ) != -1 ) ;
var is_seamk 	= ( agt.indexOf( "seamonkey" ) != -1 ) ;
var is_firefox 	= ( agt.indexOf( "firefox" ) != -1 ) ;
var is_opera 	= ( agt.indexOf( "opera" ) != -1 ) ;
var is_safari 	= ( agt.indexOf( "safari" ) != -1 ) ;
var is_khtml 	= ( agt.indexOf( "khtml" ) != -1 ) ;
var is_aol 		= ( agt.indexOf( "aol" ) != -1 ) ;
var is_gecko 	= ( agt.indexOf( "gecko" ) != -1 ) ;
var is_webtv 	= ( agt.indexOf( "webtv" ) != -1 ) ;


// ***************************************************************


Object.extend = function( destination, source ) {
	for ( var property in source ) {
		destination[ property ] = source[ property ] ;
	}
	return destination ;
}


$ = function( id ) {

	if ( typeof id == 'string' ) {
		return document.getElementById( id ) ;
	}
	else if ( typeof id == 'object' ) {
		return id ;
	}
	else {
		return false ;
	}
}


empty = function( value ) {
	return ( typeof value == 'undefined' || value == '' || value == null ) ;
}


// ***************************************************************


Array.prototype.search = function( regexp ) {
	var n = 0 ;
	for ( var i = 0 ; i <= this.length ; i ++ ) {
		if ( regexp.test( this[ i ] ) ) n ++ ;
	}
	return n ;
}

Array.prototype.first = function() {
	return this[ 0 ] ;
}

Array.prototype.last = function() {
	return this[ this.length - 1 ] ;
}

Array.prototype.implode = function( sep ) {

	var cadena = "" ;

	if ( this.length > 0 ) {
		for ( var i = 0 ; i <= this.length - 2 ; i ++ ) {
			cadena += this[ i ] + sep ;
		}
	}
	return cadena + this[ this.length - 1 ] ;
}


// ***************************************************************


var fragmentJS = '<script[^>]*>([\\S\\s]*?)<\/script>' ;


__pad = function( largo, caracter ) {
	var insert = '' ;
	for ( var i = 1 ; i <= largo ; i ++ ) insert += caracter ;
	return insert ;
}


String.prototype.padl = function( largo, caracter ) {
	return __pad( largo - this.length, caracter ) + this ;
}


String.prototype.padr = function( largo, caracter ) {
	return this + __pad( largo - this.length, caracter ) ;
}


String.prototype.trim = function() {
	return this.replace( /^(\s)*|(\s)*$/, '' ) ;
}


String.prototype.stripTags = function() {
	return this.replace( /<\/?[^>]+>/gi, '' ) ;
}


String.prototype.stripJS = function() {
	return this.replace( new RegExp( fragmentJS, 'img' ), '' ) ;
}


String.prototype.getJS = function() {
	var matchAll = new RegExp( fragmentJS, 'img' ) ;
	var matchOne = new RegExp( fragmentJS, 'im' ) ;
	return ( this.match( matchAll ) || [] ).map( function( scriptTag ) {
		return ( scriptTag.match( matchOne ) || [ '', '' ] )[ 1 ] ;
	} ) ;
}


String.prototype.evalJS = function() {
	return this.getJS().map( function( script ) { return eval( script ) } ) ;
}


String.prototype.encodeUTF8 = function() {

	var str = this.replace( /\r\n/g, "\n" ) ;
	var utf8text = '' ;

	for ( var n = 0 ; n < str.length ; n ++ ) {

		var c = str.charCodeAt( n ) ;

		if ( c < 128 ) {
			utf8text += String.fromCharCode( c ) ;
		}
		else if ( ( c > 127 ) && ( c < 2048 ) ) {
			utf8text += String.fromCharCode( ( c >> 6 ) | 192 ) ;
			utf8text += String.fromCharCode( ( c & 63 ) | 128 ) ;
		}
		else {
			utf8text += String.fromCharCode( ( c >> 12 ) | 224 ) ;
			utf8text += String.fromCharCode( ( ( c >> 6 ) & 63 ) | 128 ) ;
			utf8text += String.fromCharCode( ( c & 63 ) | 128 ) ;
		}
	}
	return utf8text ;
}


String.prototype.decodeUTF8 = function() {

	var str = '' ;
	var i = c = c1 = c2 = 0 ;

	while ( i < this.length ) {

		c = this.charCodeAt( i ) ;

		if ( c < 128 ) {
			str += String.fromCharCode( c ) ;
			i ++ ;
		}
		else if ( ( c > 191 ) && ( c < 224 ) ) {
			c2 = this.charCodeAt( i + 1 ) ;
			str += String.fromCharCode( ( ( c & 31 ) << 6 ) | ( c2 & 63 ) ) ;
			i += 2 ;
		}
		else {
			c2 = this.charCodeAt( i + 1 ) ;
			c3 = this.charCodeAt( i + 2 ) ;
			str += String.fromCharCode( ( ( c & 15 ) << 12 ) | ( ( c2 & 63 ) << 6 ) | ( c3 & 63 ) ) ;
			i += 3 ;
		}
	}
	return str ;
}


// ***************************************************************


Number.prototype.numRound = function( places ) {

	if ( places > 0 ) {
		if ( ( this.toString().length - this.toString().lastIndexOf( '.' ) ) > ( places + 1 ) ) {
			var rounder = Math.pow( 10, places ) ;
			return Math.round( this * rounder ) / rounder ;
		} else {
			return this ;
		}
	} else {
		return Math.round( this ) ;
	}
}


Number.prototype.numComa = function() { // p.ej. 132141.2 -> 132,141.2

	var str = this.toString() ;
	var punto = str.indexOf( '.' ) ;
	var dec = nuevo = "" ;

	if ( punto > 0 ) {
		dec = str.substr( punto, 3 ) ;
		str = str.substr( 0, punto ) ;
	}

	var num = str.length % 3 ;

	num = ( num == 0 ) ? 3 : num ;

	while ( str.length > 0 ) {
		nuevo += str.substr( 0, num ) + "," ;
		str = str.substr( num ) ;
		num = 3 ;
	}

	return nuevo.substr( 0, nuevo.length - 1 ) + dec ;
}


// ***************************************************************


implode = function( obj, sep ) {

	var cadena = "" ;

	if ( obj.length >= 1 ) {
		for ( var i = 0 ; i <= obj.length - 1 ; i ++ ) {
			if ( obj.options[ i ].selected ) cadena += obj.options[ i ].value + sep ;
		}
	}
	return cadena ;
}


// ***************************************************************


keyCode = function( e ) {
	var key = ( is_ie ) ? event.keyCode : e.which ;
	return key ;
}


// ***************************************************************


eventStop = function( e ) {

	if ( e.preventDefault ) {
		e.preventDefault() ;
		e.stopPropagation() ;
	} else {
		window.event.returnValue = false ;
		window.event.cancelBubble = true ;
	}
	return false ;
}

eventAdd = function( id, type, listener, useCapture ) {

	id = $( id ) ;

	if ( id.removeEventListener ) {
		id.addEventListener( type, listener, useCapture ) ;
	} else {
		id.attachEvent( 'on' + type, listener ) ;
	}
}

eventRemove = function( id, type, listener, useCapture ) {

	id = $( id ) ;

	if ( id.removeEventListener ) {
		id.removeEventListener( type, listener, useCapture ) ;
	} else {
		id.detachEvent( 'on' + type, listener ) ;
	}
}


// ***************************************************************


checkForm = function( form, elementos ) {

	var i, n ;
	var aElementos ;

	aElementos = elementos.split( '-' ) ;

	for ( var i = 0 ; i < aElementos.length ; i ++ ) {
		n = aElementos[ i ] ;
		if ( empty( form.elements[ n ].value ) ) {
			alert( "Faltan más datos importantes para poder continuar !!!" ) ;
			form.elements[ n ].focus() ;
			return false ;
		}
	}

	return true ;
}


// ***************************************************************


popup = function( x, y, w, h, url ) {
	var param = 'dependent=yes,hotkeys=no,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no' ;
	param = param + ',left=' + x + ',top=' + y + ',width=' + w + ',height=' + h ;
	window.open( url, null, param ) ;
	//param = 'dialogLeft:' + x + 'px; dialogTop:' + y + 'px; dialogWidth:' + w + 'px; dialogHeight:' + h + 'px; edge: Sunken; center: Yes; resizable: No; status: No' ;
	//win2 = window.showModalDialog( url, null, param ) ;
}


// ***************************************************************


checkBrowser = function() {
	return true ;
}


// ***************************************************************


checkResolucion = function( width, height ) {

	var width = ( width > screen.availWidth ) ? screen.availWidth : width ;
	var height = ( height > screen.availHeight ) ? screen.availHeight : height ;

	if ( screen.availWidth != width || screen.availHeight != height ) {
		//window.moveTo( 0, 0 ) ;
		window.resizeTo( width, height ) ;
		return false ;
	}

	return true ;
}


// ***************************************************************


checkFecha = function( fecha ) {

	if ( fecha.length <= 0 ) return true ;

	var check = /^[0-9]{1,2}[-./]{1}[0-9]{1,2}[-./]{1}[0-9]{4}$/ ;

	if ( ! check.test( fecha ) ) {
		alert( "El formato de fecha NO es correcto. ej: 01/04/2003" ) ;
		return false ;
	}

	var dia = parseFloat( fecha.substr( 0, 2 ) ) ;
	var mes = parseFloat( fecha.substr( 3, 2 ) ) ;
	var anio = parseFloat( fecha.substr( 6, 4 ) ) ;
	var dias = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ) ;

	if ( ( ( anio % 4 == 0 ) && ( anio % 100 != 0 ) ) || ( anio % 400 == 0 ) ) { // si es biciesto
		dias[ 1 ] = 29 ;
	}

	if ( fecha.length != 10 ) {
		alert( "La fecha NO es Correcta, el día/mes deben tener 2 dígitos, el año 4 dígitos. ej: 01/04/2001" ) ;
		return false ;
	}
	else if ( dia > 31 || dia < 1 ) {
		alert( "La fecha NO es Correcta, el día NO puede ser mayor que 31 ni menor que 1" ) ;
		return false ;
	}
	else if ( mes > 12 || mes < 1 ) {
		alert( mes + fecha + "La fecha NO es Correcta, el mes NO puede ser mayor que 12 ni menor que 1" ) ;
		return false ;
	}
	else if ( dia > dias[ mes - 1 ] ) {
		alert( "La fecha NO es correcta, el mes " + mes + " NO puede tener mas de " + dias[ mes - 1 ] + " días" ) ;
		return false ;
	}

	return true ;
}


// ***************************************************************


checkEmail = function( email ) {

	if ( email.length <= 0 ) return true ;

	var check = /^((?:\w+[^\w\s@]?)+)@((?:[^\.@\s]+\.)+[a-z]{2,}|(?:\d{1,3}\.){3}\d{1,3})$/ ;

	if ( ! check.test( email ) ) {
		alert( "Cuenta de E-Mail Incorrecta!" ) ;
		return false ;
	}

	return true ;
}


// ***************************************************************


objOff = function( obj ) {
	obj.disabled = true ;
}

objOn = function( obj ) {
	obj.disabled = false ;
}


// ***************************************************************


styleClass = function( src, newclass ) {
	src.className = newclass ;
}


// ***************************************************************


stylesChange = function( path_css ) {
	document.getElementsByTagName( "link" ).item( 0 ).setAttribute( "href", path_css ) ;
}


// ***************************************************************


clickLink = function( src ) {
	location.replace( src.firstChild.href ) ;
}


// ***************************************************************


fecha = function() {

	var objDate = new Date() ;

	var dia = objDate.getDate().toString().padl( 2, "0" ) ;
	var mes = objDate.getMonth() + 1 ;
	var anio = objDate.getFullYear() ;

	return dia + "/" + mes.toString().padl( 2, "0" ) + "/" + anio ;
}


// ***************************************************************


fechaFull = function() {

	var objDate = new Date() ;
	var months = new Array( "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" ) ;
	var days = new Array( "Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado" ) ;

	return days[ objDate.getDay() ] + " " + objDate.getDate() + " de "+ months[ objDate.getMonth() ] + " de " + objDate.getFullYear() ;
}


// ***************************************************************


printElement = function( id, auto ) {

	if ( document.getElementById != null ) {

		var html = '<HTML>\n<HEAD>\n' ;

		if ( document.getElementsByTagName != null ) {
			var headTags = document.getElementsByTagName( "HEAD" ) ;
			if ( headTags.length > 0 ) html += headTags[ 0 ].innerHTML ;
		}

		html += '\n</HEAD>\n<BODY>\n' ;
		var printReadyElem = $( id ) ;

		if ( printReadyElem != null ) {
			html += printReadyElem.innerHTML ;
		} else {
			return ;
		}

		html += '\n</BODY>\n</HTML>' ;

		var printWin = window.open( "", "printElement" ) ;
		printWin.document.open() ;
		printWin.document.write( html ) ;
		printWin.document.close() ;

		if ( auto ) printWin.print() ;

	} else {
		alert( "NOTA: Esta función no está disponible para este tipo de browser." ) ;
	}
}


// ***************************************************************


clipboard = function( id ) {

	//var copied = document.selection.createRange() ;
	var copied = $( id ) ;
	//copied = copied.innerHTML.createTextRange() ;
	copied.execCommand( "Copy" ) ;
}


// ***************************************************************


aAjax = function( url, id, values ) {

	var obj = $( id ) ;

	if ( obj.style.display != 'none' ) {

		obj.innerHTML = "<img src='images/loading5.gif'>" ;

		//$( "#" + id ).load( url ) ; // jQuery.js
		//new Ajax.Updater( id, url, { method: 'get', evalScripts: true } ) ; // prototype.js
		new Ajax( url, values, { update: id, evalJS: true } ) ; // common.js
	}
}


// ***************************************************************


var dragObj = new Object() ;  // Mi gran orgullo 2 :)

dragStart = function( event, el, effects ) {

	var evtObj = window.event ? window.event : event ;

	dragObj.elNode = $( el ) ;
	dragObj.effects = ( typeof effects == 'number' ) ? effects : 0 ;

	eventStop( evtObj ) ; // preventDefault.

	if ( isNaN( parseInt( dragObj.elNode.style.left ) ) ) dragObj.elNode.style.left = 0 ;
	if ( isNaN( parseInt( dragObj.elNode.style.top ) ) ) dragObj.elNode.style.top = 0 ;

	dragObj.offsetx = parseInt( dragObj.elNode.style.left ) ;
	dragObj.offsety = parseInt( dragObj.elNode.style.top ) ;

	dragObj.x = evtObj.clientX ;
	dragObj.y = evtObj.clientY ;

	eventAdd( document, "mousemove", __dragGo, true ) ;
	eventAdd( document, "mouseup", __dragStop, true ) ;
}


__dragGo = function( event ) {

	var evtObj = window.event ? window.event : event ;

	if ( dragObj.offsetx + evtObj.clientX - dragObj.x > 0 ) {
		dragObj.elNode.style.left = dragObj.offsetx + evtObj.clientX - dragObj.x + "px" ;
	} else {
		dragObj.elNode.style.left = 0 + "px" ;
	}

	if ( dragObj.offsety + evtObj.clientY - dragObj.y > 0 ) {
		dragObj.elNode.style.top = dragObj.offsety + evtObj.clientY - dragObj.y + "px" ;
	} else {
		dragObj.elNode.style.top = 0 + "px" ;
	}

	if ( dragObj.effects ) { // effects on.
		dragObj.elNode.style.opacity = 0.8 ;
		dragObj.elNode.style.cursor = 'move' ;
	}

	return false ;
}


__dragStop = function( event ) {

	var targetObj = window.event ? event.srcElement : event.target ;

	eventRemove( document, "mousemove", __dragGo, true ) ;
	eventRemove( document, "mouseup", __dragStop, true ) ;

	if ( dragObj.effects ) { // effects off.
		dragObj.elNode.style.opacity = 1 ;
		dragObj.elNode.style.cursor = 'default' ;
	}

	targetObj.focus() ;
}


// ***************************************************************


mousePosX = function( e ) {

	var posX = 0 ;

	if ( ! e ) var e = window.event ;

	if ( e.pageX ) {
		posX = e.pageX ;
	}
	else if ( e.clientX ) {
		posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft ;
	}

	return posX ;
}


mousePosY = function( e ) {

	var posY = 0 ;

	if ( ! e ) var e = window.event ;

	if ( e.pageY ) {
		posY = e.pageY ;
	}
	else if ( e.clientY ) {
		posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop ;
	}

	return posY ;
}


// ***************************************************************


callToServer = function( URL, id ) {

	var oFrame, name, rand_id ;

	name = 'RSIFrame' ;
	rand_id = name + Math.random() ;

	// Si se pasa un ID le metemos el HTML "loading...", sino cancelamos.
	if ( ! empty( id ) ) {
		var container = $( id ) ;

		if ( container.style.display != 'none' ) {
			container.innerHTML = "<img src='media/loading3.gif'>" ;
		} else {
			return true ;
		}
	}

	// Generamos el IFRAME desde aca mismo y lo agregamos al body.
	oFrame = document.createElement( 'IFRAME' ) ;
	oFrame.id = rand_id ;
	oFrame.name = rand_id ;
	oFrame.className = name ;
	oFrame.style.width = '0px' ;
	oFrame.style.height = '0px' ;
	oFrame.style.border = '0px' ;
	oFrame.src = URL ;

	document.body.appendChild( oFrame ) ;

	// Si se pasa un ID le metemos el HTML del container.
	if ( ! empty( id ) ) setTimeout( function() { __getIframeContent( rand_id, id ) }, 13 ) ;
}

__getIframeContent = function( frame_id, update_id ) {

	var objIFrame = window.frames[ frame_id ].document.documentElement ;
	var objUpdate = $( update_id ) ;

	// Metemos el contenido HTML.
	objUpdate.innerHTML = objIFrame.innerHTML ;
}


// ***************************************************************


Ajax = function( url, values, options ) {

	var oAjax = ( window.ActiveXObject ) ? new ActiveXObject( 'Microsoft.XMLHTTP' ) : new XMLHttpRequest() ;

	var method = ( empty( values ) ) ? 'get' : 'post' ;

	if ( empty( options.async ) )  options.async  = true  ;
	if ( empty( options.evalJS ) ) options.evalJS = false ;

	oAjax.open( method, url, options.async ) ;

	if ( method == 'post' ) {
		oAjax.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ) ;
		oAjax.send( values ) ;
	} else {
		oAjax.send( null ) ;
	}

	oAjax.onreadystatechange = function() {
		if ( oAjax.readyState == 4 ) {
			if ( options.update ) {
				$( options.update ).innerHTML = oAjax.responseText.stripJS() ;
				if ( options.evalJS ) oAjax.responseText.evalJS() ;
			}
		}
	}

	return oAjax ;
}


// ***************************************************************

