/**
* Archivo js creado EVO I.T.
* 
* Este archivo contiene funciones generales de JavaScript que pueden utilizar las
* clases creadas con O-creator
*
* @package EVOIT
* @author {@link http://www.evoit.com/ EVO I.T.}
* @copyright {@link http://www.evoit.com/ EVO I.T.}
*/

/**
* Funciones 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 metodo show_query()
* 
* @param object obj objeto row (tr) de una tabla en HTML
*/
function tr_over_1(_obj)
{
	if (_obj.className == 'query_tr_1_click')
	{
		_obj.className = 'query_tr_1_click';
	}
	else
	{
		_obj.className = 'query_tr_1_hover';
	}
}
function tr_out_1(_obj)
{
	if (_obj.className == 'query_tr_1_click')
	{
		_obj.className = 'query_tr_1_click';
	}
	else
	{
		_obj.className = 'query_tr_1';
	}
}
function tr_click_1(_obj)
{
	if (_obj.className != 'query_tr_1_click')
	{
		_obj.className = 'query_tr_1_click';
	}
	else
	{
		_obj.className = 'query_tr_1';
	}
}
function tr_over_2(_obj)
{
	if (_obj.className == 'query_tr_2_click')
	{
		_obj.className = 'query_tr_2_click';
	}
	else
	{
		_obj.className = 'query_tr_2_hover';
	}
}
function tr_out_2(_obj)
{
	if (_obj.className == 'query_tr_2_click')
	{
		_obj.className = 'query_tr_2_click';
	}
	else
	{
		_obj.className = 'query_tr_2';
	}
}
function tr_click_2(_obj)
{
	if (_obj.className != 'query_tr_2_click')
	{
		_obj.className = 'query_tr_2_click';
	}
	else
	{
		_obj.className = 'query_tr_2';
	}
}

/**
* Funcion para seleccionar o deseleccionar todos los checkbox de un formulario
* 
* Esta funcion 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 _form id del formulario que contiene los checkbox
* @param string _checkbox id del checkbox que se utiliza para seleccionar todos los otros
*/
function query_select_all(_form, _checkbox)
{
	var ob_form = document.getElementById(_form);
	var ob_chkb = document.getElementById(_checkbox);
	
	cantidad = ob_form.elements.length;
		
	if (ob_chkb.checked == true)
	{
		for (i = 0; i < cantidad; i++)
		{ 
			if (ob_form.elements[i].type == 'checkbox')
			{
				if (ob_form.elements[i].disabled == false)
				{
					ob_form.elements[i].checked = true;
				}
			}
		} 
	}
	else
	{
		for (i = 0; i < cantidad; i++)
		{ 
			if (ob_form.elements[i].type == 'checkbox')
			{
				ob_form.elements[i].checked = false;
			}
		} 
	}
}

/**
* Funcion para seleccionar o deseleccionar todos los checkbox del formulario de envio de newsletter
* 
* Esta funcion se utiliza para seleccionar o deseleccionar todos los checkbox de los elementos que son tipos de
* publicaciones (Cpublication_type) o tipo de investigaciones (Cresearch_type).
* 
* @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 newsletter_select_all(_form, _checkbox)
{
	var ob_form = document.getElementById(_form);
	var ob_chkb = document.getElementById(_checkbox);
	
	cantidad = ob_form.elements.length;
	
	var str = _checkbox;
	var str_type = "";
		
	if (ob_chkb.checked == true)
	{
		for (i = 0; i < cantidad; i++)
		{ 
			if (ob_form.elements[i].type == 'checkbox')
			{
				str_type = ob_form.elements[i].name;
				if (str_type.match(str) != null)
				{
					if (ob_form.elements[i].disabled == false)
					{
						ob_form.elements[i].checked = true;
					}					
				}
			}
		} 
	}
	else
	{
		for (i = 0; i < cantidad; i++)
		{ 
			if (ob_form.elements[i].type == 'checkbox')
			{
				str_type = ob_form.elements[i].name;
				if (str_type.match(str) != null)
				{	
					ob_form.elements[i].checked = false;
				}
			}
		} 
	}
}

/**
* Funcion para realizar el submit del formulario de las acciones grupales
* 
* Esta funcion se utiliza para realizar el submit del formulario para realizar las acciones grupales
* que se crean llamando al metodo show_query()
* 
* @param string _form id del formulario
* @param string _action archivo al que redirecciona el formulario
* @param string _params parametros que se envian
* @param boolean _confirm indica si la accion necesita confirmacion antes de ser realizada
* @param string _msg mensaje de la confirmacion
*/
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();
	}
}

/**
* Funcion para abrir un popup con la imagen seleccionada
* 
* @param string myimage URL de la imagen
*/
function display(myimage)
{
	html = '<html><head><title>Imagen<\/title> ';
	html+= '<script language="javascript" type="text\/javascript">';
	html+= 'var arrTemp = self.location.href.split(\'?\'); ';
	html+= 'var picUrl = (arrTemp.length > 0) ? arrTemp[1] : \'\'; ';
	html+= 'var NS = (navigator.appName == \'Netscape\') ? true : false; ';
	html+= 'function FitPic() ';
	html+= '{ ';
	html+= 'var obj_img = document.getElementById(\'image\'); ';
	html+= 'iWidth = (NS) ? window.innerWidth:document.body.clientWidth; ';
	html+= 'iHeight = (NS) ? window.innerHeight:document.body.clientHeight; ';
	html+= 'iWidth = obj_img.width - iWidth; ';
	html+= 'iHeight = obj_img.height - iHeight; ';
	html+= 'window.resizeBy(iWidth, iHeight); ';
	html+= 'var X = (screen.width - obj_img.width) / 2; ';
	html+= 'var Y = (screen.height - obj_img.height) / 4; ';	
	html+= 'window.moveTo(X, Y); ';
	html+= 'self.focus(); ';
	html+= '} ';
	html+= '<\/script> ';
	html+= '<\/head>';
	html+= '<body leftmargin="0"';
	html+= 'marginwidth="0" topmargin="0" marginheight="0"><center>';
	html+= '<a href="javascript:window.close()">';
	html+= '<img src="' + myimage + '" border="0" name="image" id="image" alt="Cerrar" title="Cerrar" onload="FitPic()" \/>';
	html+= '<\/a><\/center>';
	html+= '<\/body><\/html>';
	
	var NS = (navigator.appName == 'Netscape') ? true : false;
	if (NS)
	{
		iWidth = screen.width;
		iHeight = screen.height;
	}
	else
	{
		iWidth = 1;
		iHeight = 1;
	}
	
	popup = window.open('', 'image', 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,width=' + iWidth + ',height=' + iHeight);
	popup.document.open();
	popup.document.write(html);
	popup.focus();
	popup.document.close();
}

/**
* Funcion para confirmar el submit de un formulario
* 
* @param string msg Mensaje de la confirmacion
* @return boolean
*/
function confirm_form(msg)
{
	if (confirm(msg))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
* Funcion para agregar opciones a las encuestas
* 
* @param string id div donde se cargan las opciones
* @param string str Opcion cargada
*/
function addchoice(id, id_text)
{
	var ob_div = document.getElementById(id);
	var ob_txt = document.getElementById(id_text);
	
	var aux = ob_txt.value;
	aux = aux.replace(/"/g, "&quot;");
	
	if (ob_txt.value != '')
	{
		ob_div.innerHTML+= '<div class="item_choice"> - ' + aux + '<input type="hidden" name="choice_group[]" value="' + aux + '" \/><\/div>';
		ob_txt.value = '';	
	}
	else
	{
		ob_txt.focus();
	}
}

/**
* Funcion para realizar la votacion
*/
function vote(id_form, survey)
{
	var ob_form = document.getElementById(id_form);
	
	//determino el valor de la opcion que eligio
	for (i=0; i < ob_form.elements['survey_' + survey].length; i++)
	{
		if (ob_form.elements['survey_' + survey][i].checked == true)
		{
			opcion = ob_form.elements['survey_' + survey][i].value;	
		}	
	}
	
	//llamo al ajax
	loadPage('surveyvote.php', 'cont_survey_' + survey, 'svs=' + survey + '&svc=' + opcion);
}
