function msgpregunta(mensaje){
		if (confirm (mensaje)){
			return true;
		} else{
			return false;
		}
		return false;
}

function foco(control){
	control = document.getElementById(control);
	if(control != null) {
		control.focus();
	}
}

function checkEmail(emailStr) {
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (emailStr.length == 0 ) {
		alert("Ingrese una dirección de correo válida");
		return false;
	}
	
	if (filter.test(emailStr)) {
		return true;
	}else {
		alert("Ingrese una dirección de correo válida");
		//theElement.focus();
	}
	
	return false;
}


function esVacio(campo) {
	var obj=$(campo)
	var cadena=obj.value;
	if (cadena.length == 0 ) {
		return true;
	}
	else{
		return false;
	}
}


function trim(str) {
	var resultado = "";
	for(i=0; i < str.length; i ++) {
		if(str.charAt(i) == " ") {
				str = str.substring(i+1, str.length);
		} else {
			break;
		}
	}
	
	for(i=str.length-1 ;  i >= 0 ; i=str.length-1 )	{
		if(str.charAt(i)==" ") {
			str = str.substring(0,i);
		} else {
			break;
		}
	}
	return str;
}

function obtenerDatosFormulario(nombreForm) {
		var obj = $(nombreForm);
	  	var resultado = '';
	  	this.add = function(nombre , valor) {
	  			resultado += nombre + '=' + escape(valor);
	  		 	if(resultado != '') {
	  		 	    resultado +='&';
	  		 	}
	  	}
	  		
  		 if(obj.length > 0) {
	  		for(i=0;i<obj.length;i++) {
	  			var tipo=obj[i].type;
				var nombre=obj[i].name;
				
				if(tipo=='select-multiple') { 
					var n=0;
					for(j=0;j<obj[i].options.length;j++) {
						if(obj[i].options[j].selected) {
							this.add(nombre,obj[i].options[j].value);
						}
					}
				}else if(tipo=='checkbox') {
					
					if(obj[i].checked) {
						this.add(nombre,obj[i].value);
					}else{
						this.add(nombre,"off");
					}
				}else if(tipo =='radio') {
					if(obj[i].checked) {
						this.add(nombre,obj[i].value);
					}
				}else {
					this.add(nombre,obj[i].value);
				}
	  		}
  		}
  		
  		return resultado;
}

/**
 * 
 * @return Boolean Falso si el valor no es numerico, verdadero si el valor es numerico
 */
function validarSoloNumero(nombreCampo) {
	var valor = $(nombreCampo).value;
	//alert('entro')
	
	if(isNumeric(valor) == false) {
		//alert("El campo: " + nombreCampo + " solo acepta valores numericos.");
    	$(nombreCampo).style.backgroundColor = 'red';
    	retorno = false;
	}else {
		$(nombreCampo).style.backgroundColor = '';
    	retorno = true;
	}
//	return true;
//	var charpos = valor.search('[^0-9]');
//	var retorno = false;
//	//valor.length > 0 &&
//    if( charpos >= 0 || valor.length == 0) {
//    	alert("El campo: " + nombreCampo + " solo acepta valores numericos.");
//    	$(nombreCampo).style.backgroundColor = 'red';
//    	retorno = false;
//    }else {
//    	$(nombreCampo).style.backgroundColor = '';
//    	retorno = true;
//    }
    return retorno;
}

function isNumeric(valor) {
	if(isNaN(valor) || valor == "") {
		return false;
	} else {
		return true;
	}
}

function validarValorMayorQueCero(nombreCampo) {
	var valor = $(nombreCampo).value;
	//alert('entro')
	if(valor<=0) {
    	$(nombreCampo).style.backgroundColor = 'red';
    	retorno = false;
	}else {
		$(nombreCampo).style.backgroundColor = '';
    	retorno = true;
	}
    return retorno;
}

/**
 * arrDatos debe ser un array que en cada posicion contenga un array("value" => 0 , "text" => 111)
 */
function llenarUnCuadroDeLista(nombreControl , arrDatos) {
	var objDDL = $(nombreControl)
	
	objDDL.options.length = 0;
	
	for(i = 0 ; i < arrDatos.length ; i++){
		var text	= unescape(arrDatos[i].text);
		var value	= unescape(arrDatos[i].value);
		var option	= new Option(text,value);
		try{
			objDDL.add(option,null);
		}catch (e){
			objDDL.add(option,-1);
		}
	}
}

/**
 *
 * @param Date fecha  
 * @param separador
 */

function getFechaFormatoDDMMAAAA(fecha, separador) {
	
	if(separador == null) {
		separador = "/";
	}
	
	var year = fecha.getYear();
	
	if (year < 1000) {
		year = 1900 + year;		
	}
	
	var day = fecha.getDate();
	var month = fecha.getMonth();
	var daym = fecha.getDate();
	if (day < 10) {
		daym="0"+daym;
	}
	if(month < 10) {
		month = "0" + month;
	}
	
	return day + separador + month + separador + year;
	
	//var dayarray = new Array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado");
	//var montharray = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
	
	//document.write("<small><font color='000000' face='Arial'>"+dayarray[day]+" "+daym+" de "+montharray[month]+" de "+year+"</font></small>")
}


/*******************************
 * FUNCIONES PARA VALIDAR FECHA *
 ********************************/

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebbyn.com/dhtml/)
 */

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

/**
 * recibe un string con la fecha y devuelve un valor que indica si la fecha esta correcta o no
 * @param String dtStr  La fecha debe estar en el formato "mm/dd/aaaa"
 */
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
//	alert(strMonth);
//	alert(strDay);
//	alert(strYear);
	
	strYr =strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if ( pos1==-1 || pos2==-1 ){
		alert("El formato de la fecha debe ser: mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


 /*********FIN FUNCIONES PARA VALIDAR FECHA **********************/
 
 
 
 function getValuesCheckbox(nombre, cont) {
 	var str = "";
 	continucarCiclo = true;
 	cont = 0;
 	while(continucarCiclo) {
 		nombreElemento = nombre + "["+ cont +"]"
 		elemento = $(nombreElemento);
 		
 		if(elemento == null) {
 			continucarCiclo = false;	
 		}else {
 			if(str != "") {
 				str += "&";
 			}
 			if(elemento.checked) {
 				str += nombreElemento + "=" + elemento.value;
 			}else {
 				str += nombreElemento + "=off";
 			}
 		}
 		cont ++;
 	}
 	
 	return str;
}


/**
 * 
 * param String formulario
 * param String url
 * param String parametros 
 * param String functcionRegreso
 * para Boolean enviarDatosForm
 */
function serviceOAjax(formulario, url, parametros,funcionRegreso ,  enviarDatosForm) {
	
	oAjax = new OAjax(formulario,'');
	var datosFormulario = "";
	if (enviarDatosForm == true) {
		datosFormulario = obtenerDatosFormulario(formulario) + "&";
	}
	
	
	datosFormulario = datosFormulario + parametros;
	oAjax.abrirUrl_Funcion(url,datosFormulario , funcionRegreso );
}


function recorrerCheckbox(nombreFormulario,nombreCheckbox,seleccionar) {
	var variable;
	var form = document.forms[nombreFormulario];
	var elementos = form.elements.length;
	var len1 = nombreCheckbox.length;
	
	 for (var i=0;i < elementos ;i++){
		var elemento = form.elements[i];
		
		if (elemento.type == "checkbox" &&  elemento.name.substring(0,len1) == nombreCheckbox){
		    elemento.checked = seleccionar;
		}
	 }
	
}


function OTabla (nombre) {
	this.nombre = nombre;
	this.columnas = Array();
	this.filas = null;
	
	this.objTabla = document.createElement("table");
	this.objTabla.setAttribute('id',this.nombre);
	this.objTabla.setAttribute('name',this.nombre);
	
	
	function Columna(titulo,ancho,alineacion) {
		this.titulo = titulo;
		this.ancho = ancho;
		this.alineacion = alineacion;
	}
	
	this.addColumna = function(titulo,ancho,alineacion) {
		columna = new Columna(titulo,ancho,alineacion);
		this.columnas[this.columnas.length] = columna;
	}
	
	this.setFilas = function (filas) {
		this.filas = filas;
	}
	
	this.getTabla = function() {
		
		
		/**
		  * Creando el titulo
		  */
		var  oTR= this.objTabla.insertRow(0);
		for(var i = 0; i < this.columnas.length ; i++ ) {
			var  oTD= oTR.insertCell(i);      
			oTD.innerHTML = this.columnas[i].titulo;
		}
		
		for(var i = 0 ; i < this.filas.length ; i++) {
			var  oTR= this.objTabla.insertRow(i+1);
			for(var campo in datos[i]) {
				var j=0;
				var  oTD= oTR.insertCell(j);      
				oTD.innerHTML = this.filas[i][campo];
				j++;
			}
		}
		
		return this.objTabla;
	}
	
	this.setAtributo = function(nombre,valor) {
		this.objTabla.setAttribute(nombre,valor);
	}
}


function Pair() {
	this.values = [];
	this.text = [];
	
	this.add = function (value, text) {
		this.values.push(value);
		this.text.push(text);
	}
	
	this.length = function() {
		return this.values.length;
	}
	
	this.findText = function(value) {
		var pos = this.values.indexOf(value);
		if(pos != -1) {
			return this.text[pos];	
		}else {
			return null;
		}
		
	}
}

function buscarObjectInArray(array , id) {
	var returnObject = null;
	array.each(function(object) {
		//alert(object.id);
		if(object.id == id) {
			returnObject = object;
		}
	});
	return returnObject;
}


function cambiarImagen(control,nombreImagen) {
	
	control.src = nombreImagen;
	/*arg = cambiarImagen.arguments;
	
	for(i = 0 ; i < arg.length; i++) {
		$(arg[i]).src = 
	}*/
}


function mostrarCapa(nombreCapa,event){
	//alert(nombreCapa);
	var IE = document.all?true:false;
	if (!IE) {
		document.captureEvents(Event.MOUSEMOVE)
	}
	
	var tempX = 0;
	var tempY = 0;
	if(IE){
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else{
		tempX = event.pageX;
		tempY = event.pageY;
	}
	
	tempX = tempX - 20;
	tempY = tempY - 20;
	$(nombreCapa).style.left = tempX + "px";
	$(nombreCapa).style.top = tempY+ "px";
	$(nombreCapa).style.display = "block";
	//alert( tempY + "-" +  tempX);
}

function ocultarCapa(nombreCapa){
	$(nombreCapa).style.display = "none";
} 


function findLeftObj(obj) {  
	var curleft = 0;  
	if (obj.offsetParent) {  
	   while (obj.offsetParent) {  
	    curleft += obj.offsetLeft  
	    obj = obj.offsetParent;  
	    }  
	}  
	else {  
	 if(obj.x) {  
	   curleft += obj.x;  
	  }  
	}  
	return(curleft);  
}  
           
function findTopObj(obj) {  
	var curtop = 0;  
	if (obj.offsetParent) {  
	    while (obj.offsetParent) {  
	        curtop += obj.offsetTop  
	        obj = obj.offsetParent;  
	    }  
	}  
	else {  
	  if (obj.y) {  
	    curtop += obj.y;  
	   }  
	}  
	return(curtop);  
}  
   
function posicionImagen(imagen) {  
   
    posXImagen = findLeftObj(imagen);  
    posYImagen = findTopObj(imagen);  
       
    alert('La imagen está en la posición:\nLeft(X) ' + posXImagen +   
    '\nTop(Y) ' + posYImagen)  
   
} // Fin de la función posicionImagen()


/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item]; 
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function getKeyCode(e){
	
	e= (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;
	return intKey;
}