/**
* Archivo js creado por EVO I.T.
* 
* Este archivo contiene funciones generales de JavaScript
*
* @package EVOIT
* @author {@link http://www.evoit.com/ EVO I.T.}
* @version 14-04-2009
* @copyright {@link http://www.evoit.com/ EVO I.T.}
*/

/**
* Función para mostrar y ocultar una capa
* 
* Estas funciones se utilizan para cambiar el estilo (CSS) de una capa, haciéndola invisible o visible según sea el caso
* 
* @param object id_ob Id del objeto que se quiere ocultar o mostrar
*/
function viewlayer(id_ob)
{
	var ob_layer = document.getElementById(id_ob);
	if (ob_layer.style.display == 'none')
	{
		ob_layer.style.display = '';
	}
	else
	{
		ob_layer.style.display = 'none';
	}
}

/**
* Función para mostrar y ocultar los items del menú en el administrador
* 
* Esta función se utiliza para mostrar u ocultar los items del menú en el administrador y setear una variable de $_SESSION
* mediante AJAX para mantener el estado del menú (abierto o cerrado)
* 
* @param object id_ob Id del objeto que se quiere ocultar o mostrar
*/
function showhide_menu(id_ob)
{
	var ob_layer = document.getElementById('menu_' + id_ob);
	var ob_btn   = document.getElementById('btn_menu_' + id_ob);
	
	if (ob_layer.style.display == 'none')
	{
		ob_layer.style.display = '';
		ob_btn.className = 'btn_menu_open';		
		loadPage('btnmenu.php', '', 's=' + id_ob + '&v=open');
	}
	else
	{
		ob_layer.style.display = 'none';
		ob_btn.className = 'btn_menu_closed';
		loadPage('btnmenu.php', '', 's=' + id_ob + '&v=closed');
	}	
}

/**
* Función para mostrar y ocultar el formulario de búsqueda
* 
* Esta función se utiliza para mostrar u ocultar el formulario de búsqueda y setear una variable de $_SESSION
* mediante AJAX para mantener el estado del formulario (abierto o cerrado)
* 
* @param object id_ob Id del objeto que se quiere ocultar o mostrar
*/
function showhide_search(id_ob)
{
	var ob_layer = document.getElementById('main_tr_search_' + id_ob);
	var ob_btn   = document.getElementById('btn_search_' + id_ob);
	
	if (ob_layer.style.display == 'none')
	{
		ob_layer.style.display = '';
		ob_btn.className = 'btn_search_open';		
		loadPage('btnsearch.php', '', 's=' + id_ob + '&v=open');
	}
	else
	{
		ob_layer.style.display = 'none';
		ob_btn.className = 'btn_search_closed';
		loadPage('btnsearch.php', '', 's=' + id_ob + '&v=closed');
	}	
}

/**
* Función para cambiar el estilo de las filas de una consulta
* 
* Estas funciones se utilizan para cambiar el estilo (CSS) de las filas de una tabla
* que se crea llamando al método show_query()
* 
* @param object _obj objeto row (tr) de una tabla en HTML
* @param string _class clase CSS de la fila
*/
function tr_over(_obj, _class)
{
	if (_obj.className == 'query_tr_' + _class + '_click')
	{
		_obj.className = 'query_tr_' + _class + '_click';
	}
	else
	{
		_obj.className = 'query_tr_' + _class + '_hover';
	}
}

/**
* Función para cambiar el estilo de las filas de una consulta
* 
* Estas funciones se utilizan para cambiar el estilo (CSS) de las filas de una tabla
* que se crea llamando al método show_query()
* 
* @param object _obj objeto row (tr) de una tabla en HTML
* @param string _class clase CSS de la fila
*/
function tr_out(_obj, _class)
{
	if (_obj.className == 'query_tr_' + _class + '_click')
	{
		_obj.className = 'query_tr_' + _class + '_click';
	}
	else
	{
		_obj.className = 'query_tr_' + _class + '';
	}
}

/**
* Función para cambiar el estilo de las filas de una consulta
* 
* Estas funciones se utilizan para cambiar el estilo (CSS) de las filas de una tabla
* que se crea llamando al método show_query()
* 
* @param object _obj objeto row (tr) de una tabla en HTML
* @param string _class clase CSS de la fila
* @param boolean _click_check indica si se debe chequear el checkbox de la fila
* @param string _id id del checkbox de la fila
* @param string _table tabla (de la clase)
*/
function tr_click(_obj, _class, _click_check, _id, _table)
{
	if (_obj.className != 'query_tr_' + _class + '_click')
	{
		_obj.className = 'query_tr_' + _class + '_click';
	}
	else
	{
		_obj.className = 'query_tr_' + _class + '';
	}
	
	if (_click_check)
	{
		var ob_chkb = document.getElementById('cb_' + _table + '_' + _id);
		if (ob_chkb.checked == true)
		{
			ob_chkb.checked = false;
		}
		else
		{
			ob_chkb.checked = true;
		}
		
		checked_select_all('form_query_' + _table, _table + '_select_all');
	}	
}

/**
* Esta función la utilizo para que el click del checkbox no interfiera con el click de la fila
*
* Esta función se utiliza en conjunto con las funciones tr_over, tr_out y tr_click
*
* @param object _obj objeto checkbox
*/
function checkbox_click(_obj)
{
	_obj.checked = ! _obj.checked;
}

/**
* Función para seleccionar o deseleccionar el checkbox de seleccion grupal
* 
* Esta función se utiliza para seleccionar o deseleccionar el checkbox de selección grupal cuando se seleccionan
* todos los checkbox de una consulta o se deselecciona al menos uno.
* 
* @param string _form id del formulario que contiene los checkbox
* @param string _checkbox id del checkbox que se utiliza para seleccionar todos los otros
*/
function checked_select_all(_form, _checkbox)
{
	var ob_form = document.getElementById(_form);
	var ob_chkb = document.getElementById(_checkbox);
	
	cantidad = ob_form.elements.length;
	checked_all = true;
	
	for (i = 0; i < cantidad; i++)
	{ 
		if (ob_form.elements[i].type == 'checkbox')
		{
			if (ob_form.elements[i].value)
			{
				if (!ob_form.elements[i].checked)
				{
					checked_all = false;
				}
			}
		}
	}
	
	ob_chkb.checked = checked_all;
}

/**
* Función para seleccionar o deseleccionar todos los checkbox de un formulario
* 
* Esta función se utiliza para seleccionar o deseleccionar todos los checkbox de las filas de una tabla
* que se crea llamando al método show_query()
* 
* @param string _table tabla (de la clase)
*/
function query_select_all(_table)
{
	var ob_form = document.getElementById('form_query_' + _table);
	var ob_chkb = document.getElementById(_table + '_select_all');
	
	cantidad = ob_form.elements.length;
		
	for (i = 0; i < cantidad; i++)
	{ 
		if (ob_form.elements[i].type == 'checkbox')
		{
			ob_form.elements[i].checked = ob_chkb.checked;
			
			if (ob_form.elements[i].value)
			{
				var ob_tr = document.getElementById(_table + '_tr_' + ob_form.elements[i].value);
				
				/**
				* determino si es clase 1 o clase 2 (query_tr_1, query_tr_1_click o query_tr_2, query_tr_2_click)
				*/
				aux_class = ob_tr.className.substr(9, 1);
				
				if (ob_chkb.checked)
				{
					ob_tr.className = 'query_tr_' + aux_class + '_click';
				}
				else
				{
					ob_tr.className = 'query_tr_' + aux_class;
				}
			}
		}
	}
}

/**
* Función para realizar el submit del formulario de las acciones grupales
* 
* Esta función se utiliza para realizar el submit del formulario para realizar las acciones grupales
* que se crean llamando al método show_query()
* 
* @param string _form id del formulario
* @param string _action archivo al que redirecciona el formulario
* @param string _params parámetros que se envían
* @param boolean _confirm indica si la acción necesita confirmación antes de ser realizada
* @param string _msg mensaje de la confirmación
*/
function form_query_submit(_form, _action, _params, _confirm, _msg)
{
	var ob_form = document.getElementById(_form);
	
	ob_form.action = _action;
	ob_form.p.value = _params;
	if (_confirm)
	{
		if (confirm(_msg))
		{
			ob_form.submit();
		}
	}
	else
	{
		ob_form.submit();
	}
}

/**
* Función para abrir un popup con la imagen seleccionada
* 
* @param string myimage URL de la imagen
*/
function display(url_img, width, height)
{
	var scrollbars = '0';
	
	if (width)
	{
		width = width + 4;
	}
	else
	{
		width = screen.width / 2;
		scrollbars = '1';
	}
	
	if (height)
	{
		height = height + 4;
	}
	else
	{
		height = screen.height / 2;
		scrollbars = '1';
	}
	
	if (width > (screen.width - 30))
	{
		width = screen.width - 30;
		scrollbars = '1';
	}	
	if (height > (screen.height - 150))
	{
		height = screen.height - 150;
		scrollbars = '1';
	}

	var left = (screen.width - width) / 2;
	var top  = (screen.height - height) / 4;

	html = '<html>';
	html+= '<head>';
	html+= '<title>Photo</title>';
	html+= '</head>';
	html+= '<body style="margin: 0;">';
	html+= '<div align="center" style="padding: 2px 0 2px 0;">';
	html+= '<a href="javascript:window.close()">';
	html+= '<img src="' + url_img + '" border="0" name="image_popup" id="image_popup" alt="" title="Click to close" />';
	html+= '</a>';
	html+= '<div>';
	html+= '</body></html>';
		
	popup = window.open('', 'imagen', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',channelmode=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=' + scrollbars + ',status=0,titlebar=0,toolbar=0');
	popup.document.open();
	popup.document.write(html);
	popup.focus();
	popup.document.close();
}

/**
* Corta cadenas de caracteres y les agrega un "title"
* 
* Esta función se utiliza para cortar cadenas de caracteres muy largas.
* Es muy común usarla cuando se muestran los resultados de una consulta en forma tabulada.
* 
* @param string string cadena de caracteres que se corta
* @param string lenght largo con el que queda la cadena
* @param string last_string [opcional] caracteres que se muestran al final de la cadena si la misma es cortada
*/
function getcut_string(string, length, last_string)
{		
	length = parseInt(length);

	var aux = '';
	
	var myString = string;
	
	var myLastString = last_string;
	
	if (myString.length <= length || length == 0)
	{
		aux = myString;
	}
	else
	{
		if (myString.length < myLastString.length + length)
		{
			aux = myString;
		}
		else
		{
			aux = myString.substr(0 , length) + myLastString;
		}
	}	
	
	return '<span title="' + htmlentities(myString) + '">' + aux + '</span>';
}

/**
* Función para evitar el submit de un formulario cuando se presiona Enter en alguno de sus campos
* 
* @param event e Evento
*/
function noSubmit(e)
{
	var keynum;
	
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if (keynum == 13)
	{
		return false;	
	}
}

/**
* Función para transformar caracteres en sus entidades HTML correspondientes
* 
* @param string str cadena de caracteres a transformar
*/
function htmlentities(str)
{
	return str.replace(/"/g, '&quot;');
}

/**
* Función para darle el foco a un elemento determinado
*
* @param string id identificador del elemento
*/
function setfocus(id)
{
	var ob_form = document.getElementById(id);
	ob_form.focus();
}

/**
* Funciones para ejecutar el form de suscripción de usuarios
*
*/
function sign_up()
{
	$.ajax({
		type: 'POST', 
		url: 'sign_up.php', 
		data: $('#sign_up_form').serialize(), 
		dataType: 'html', 
	   	success: function (data) {
			$('#cont_sign_up').html('<div class="sign_up_success">' + data + '</div>');
		}, 
		beforeSend: function () {
					
			$('#cont_sign_up').html('<div class="sign_up_beforeSend">Sending data...</div>');		
		}
	 });
}
function sign_up_back()
{
	$.ajax({
		type: 'POST', 
		url: 'sign_up.php', 
		data: $('#sign_up_form').serialize(), 
		dataType: 'html', 
	   	success: function (data) {
			$('#cont_sign_up').html(data);
		}, 
		beforeSend: function () {
			$('#cont_sign_up').html('<div class="sign_up_beforeSend">Sending data...</div>');		
		}
	 });
}

/**
* Funcines para ejecutar el form de contacto del sitio
*
*/
function contact()
{
	$.ajax({
		type: 'POST', 
		url: 'send_contact.php', 
		data: $('#contact_form').serialize(), 
		dataType: 'html', 
	   	success: function (data) {
			$('#cont_contact_form').html('<div class="contact_success">' + data + '</div>');
		}, 
		beforeSend: function () {
					
			$('#cont_contact_form').html('<div class="contact_beforeSend">Sending data...</div>');		
		}
	 });
}
function contact_back()
{
	$.ajax({
		type: 'POST', 
		url: 'send_contact.php', 
		data: $('#contact_form').serialize(), 
		dataType: 'html', 
	   	success: function (data) {
			$('#cont_contact_form').html(data);
		}, 
		beforeSend: function () {
			$('#cont_contact_form').html('<div class="contact_beforeSend">Sending data...</div>');
		}
	 });
}

/**
* Función para realizar scroll de la ventana
* 
* El parámetro w puede ser un número, string, etc. Ej. 'max': al final de todo; '100px'; '.unaclase'; '#unid'; etc.
*
* @param string w a donde va
* @param integer s velocidad del scroll
*/
function scroll(w, s)
{
	//si no está definida la velocidad, pongo por defecto 1000
	if (!s)
	{
		s = 1000;
	}

	$.scrollTo(w, {speed:s});
}

/**
* Función que muestra el detalle de los servicios y las políticas de privacidad
*
* @param string id del servicio/política a mostrar
* @param array others array con los otros id de los servicios/políticas (para ocultarlos)
*/
function show_chart(type, id, others)
{	
	var amount = others.length;	
	for (i = 0; i < amount; i++)
	{
		$('#' + type + '_chart_content' + others[i]).slideUp('fast');
		
		$('#' + type + '_chart_arrow' + others[i]).attr('src', 'images/content_chart_arrow1.jpg');
	}
	
	if ($('#' + type + '_chart_content' + id).is(":hidden")) 
	{
		$('#' + type + '_chart_content' + id).slideDown('fast');
		
		$('#' + type + '_chart_arrow' + id).attr('src', 'images/content_chart_arrow2.jpg');
	} 
	else
	{
		$('#' + type + '_chart_content' + id).slideUp('fast');
		
		$('#' + type + '_chart_arrow' + id).attr('src', 'images/content_chart_arrow1.jpg');
	}
}

/**
* Función que muestra el detalle del physician
*
* @param string id del physician a mostrar
*/
function show_physician(id)
{		
	$('#physicians').hide('fast');
	
	$('#physician' + id).show('fast');
	
	$.scrollTo('#physicians_begin', {speed:1000});
}

/**
* Función que muestra la lista de los physicians
*
* @param string id del physician a ocultar
*/
function show_physicians(id)
{		
	$('#physician' + id).hide('fast');
	
	$('#physicians').show('fast');
	
	$.scrollTo('#physicians_begin', {speed:1000});
}

/**
* Función que muestra el detalle de la investigación
*
* @param string id de la investigación a mostrar
*/
function show_research(id)
{		
	$('#researches').hide('fast');
	
	$('#research' + id).show('fast');
	
	$.scrollTo('#researches_begin', {speed:1000});
}

/**
* Función que muestra la lista de las investigaciones
*
* @param string id de la investigación a ocultar
*/
function show_researches(id)
{		
	$('#research' + id).hide('fast');
	
	$('#researches').show('fast');
	
	$.scrollTo('#researches_begin', {speed:1000});
}

/**
* Función que muestra el detalle de la investigación
*
* @param string id de la investigación a mostrar
*/
function show_news(id)
{		
	$('#news').hide('fast');
	
	$('#news' + id).show('fast');
		
	$.scrollTo('#news_begin', {speed:1000});
}

/**
* Función que muestra la lista de las investigaciones
*
* @param string id de la investigación a ocultar
*/
function show_list_news(id)
{		
	$('#news' + id).hide('fast');
	
	$('#news').show('fast');
	
	$.scrollTo('#news_begin', {speed:1000});
}

/**
* Función para controlar los checkbox del estado del newsletter en el admin 
*/
function check_status()
{
	var ob_status_send = document.getElementById('status_send');
	var ob_status_post = document.getElementById('status_post');
	
	var ob_text_status_post = document.getElementById('text_status_post');
	var ob_text_status_post_desc = document.getElementById('text_status_post_desc');
	var ob_text_status_send = document.getElementById('text_status_send');
	var ob_text_status_send_desc = document.getElementById('text_status_send_desc');
	
		
	if (ob_status_send.checked == true)
	{
		ob_status_post.checked = true;
				
		ob_text_status_send.className = 'text_status_selected';
		ob_text_status_send_desc.className = 'text_status_selected';
	}
	else
	{
		ob_text_status_send.className = 'text_status';
		ob_text_status_send_desc.className = 'text_status';			
	}
	
	if (ob_status_post.checked == true)
	{
		ob_text_status_post.className = 'text_status_selected';
		ob_text_status_post_desc.className = 'text_status_selected';			
	}
	else
	{
		ob_text_status_post.className = 'text_status';
		ob_text_status_post_desc.className = 'text_status';
	}
}