/*
	function checkUncheckAll
	Copyright 2006
	http://www.shawnolson.net
*/
function checkUncheckAll(theElement)
{
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
		{
			theForm[z].checked = theElement.checked;
		}
	}
}

/*
	function checkUncheckSome
	Copyright 2006
	http://www.shawnolson.net
*/
function checkUncheckSome(controller,theElements) {
	//theElements is an array of objects designated as a comma separated list of their IDs
	//If an element in theElements is not a checkbox, then it is assumed
	//that the function is recursive for that object and will check/uncheck
	//all checkboxes contained in that element
	// examples: http://www.shawnolson.net/a/1302/select-some-checkboxes-javascript-function.html
	
var formElements = theElements.split(',');
var theController = document.getElementById(controller);
for(var z=0; z<formElements.length;z++){
theItem = document.getElementById(formElements[z]);
if(theItem){
if(theItem.type){
     if(theItem.type == 'checkbox' && theItem.id != theController.id){
   theItem.checked = theController.checked;
  }
} else {

  var nextArray = '';
   for(var x=0;x <theItem.childNodes.length;x++){
    if(theItem.childNodes[x]){
      if (theItem.childNodes[x].id){
        nextArray += theItem.childNodes[x].id+',';
   }
    }
   }
   checkUncheckSome(controller,nextArray);
 
 }

}
  }
 }


/*
*	Descripcion : Crea url con los objetos del formulario para pasar como parametro.
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 7 de Julio de 2008
*/
creaParametrosURL = function(frm)
{
	var formParametros = $(frm).serialize();
	formParametros = formParametros.replace(/=&/g, "/0/");
	formParametros = formParametros.replace(/&/g, "/");
	formParametros = formParametros.replace(/=/g, "/");
	formParametros = formParametros.replace(/ /g, ",");

	return formParametros;	
}

/*
*	Descripcion : Reemplaza acentos.
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 20 de Septiembre de 2008
*/
reemplazaAcentos = function(palabra)
{

	stringF = palabra;
	
	for(i=0;i<(stringF.length-1);i++)
	{

		stringF = stringF.replace("\u00C1","A")
		stringF = stringF.replace("\u00E1","a")
		stringF = stringF.replace("\u00C9","E")
		stringF = stringF.replace("\u00E9","e")
		stringF = stringF.replace("\u00CD","I")
		stringF = stringF.replace("\u00ED","i")
		stringF = stringF.replace("\u00D3","O")
		stringF = stringF.replace("\u00F3","o")
		stringF = stringF.replace("\u00DA","U")
		stringF = stringF.replace("\u00FA","u")
		stringF = stringF.replace("\u00D1","N")
		stringF = stringF.replace("\u00F1","n")
		stringF = stringF.replace("\u00DC","U")
		stringF = stringF.replace("\u00FC","u")
		stringF = stringF.replace("\*","")
		
	}
	
	return stringF;	
}


/*
*	Descripcion : Valida NIF (universia).
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 30 de Septiembre de 2008
*/
validaNIF = function(dni) 
{
	numero = dni.substr(0,dni.length-1);
	let = dni.substr(dni.length-1,1);
	numero = numero % 23;
	letra='TRWAGMYFPDXBNJZSQVHLCKET';
	letra=letra.substring(numero,numero+1);
	if (letra!=let) 
		resultado = false;
	else
		resultado = true;
  
	return resultado;	  
}


/*
*	Descripcion : Valida CIF (universia).
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 30 de Septiembre de 2008
*/
validaCIF = function(texto)
{
	var pares = 0;
	var impares = 0;
	var suma;
	var ultima;
	var unumero;
	var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
	var xxx;
	
	texto = texto.toUpperCase();
	
	var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
	 if (!regular.exec(texto)) return false;
		 
	 ultima = texto.substr(8,1);

	 for (var cont = 1 ; cont < 7 ; cont ++){
		 xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0";
		 impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
		 pares += parseInt(texto.substr(cont,1));
	 }
	 xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0";
	 impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
	 
	 suma = (pares + impares).toString();
	 unumero = parseInt(suma.substr(suma.length - 1, 1));
	 unumero = (10 - unumero).toString();
	 if(unumero == 10) unumero = 0;
	 
	 if ((ultima == unumero) || (ultima == uletra[unumero]))
		 return true;
	 else
		 return false;

}



/*
*	Descripcion : Valida Identificadores Universia.
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 30 de Septiembre de 2008
*/
validaIdentificador = function(vcTipoIdentificador, vcIdentificador)
{
	if(vcTipoIdentificador == 1)
	{
		return validaCIF(vcIdentificador);		
	}
	else if(vcTipoIdentificador == 2)
	{
		return validaNIF(vcIdentificador);		
	}
	else
	{
		return true;
	}
}

/*
*	Descripcion : Mueve la opción (<option>) de un combobox a otro.
*	Parametros  : 	'valora', value de la opción a ser movida. 
					'origen', objeto combobox donde esta la opción a ser movida.
					'destino', objeto combobox que recibe la opción seleccionada (indicada en el primer parametro).
*	Autor		: Jacob Gutierrez Romero
*	Fecha		: 4 de Octubre de 2008
*/
function selecciona(valora,origen,destino) {
	var ok=false; 
	if (valora !='Cualquiera') {
		i=destino.length;
		if (i==1 && destino.options[0].text=='Cualquiera') {
				destino.options[0].text=destino.options[destino.length-1].text;
				destino.options[0].value=destino.options[destino.length-1].value;
				destino.length--;
				i--;
		}
		
		valor=origen.selectedIndex ;
		if (valor>=0) {
			texto=origen.options[valor].text;
		
			for (var e=0; e< i; e++) {
				if (texto==destino.options[e].text) {
					ok=true;
					break;
				}
				else
					ok=false;
			}
			if (!ok) {
				var el = new Option(texto,valora);
				destino.options[i] = el;
			}
				
			for (var i=0;i<origen.length;i++) {
				if (origen.options[i].value==valora) {
					for (j=i;j<origen.length-1;j++){
						origen.options[j].text=origen.options[j+1].text;
						origen.options[j].value=origen.options[j+1].value;
					}
					origen.length--;
//					if (origen.length ==0) {
//						var el = new Option('Cualquiera',0);
//						origen.options[0] = el;
//					}
					break;
				}
			}
		} // FIN: if (valor>=0)
		else
			alert("Seleccione una opción primero");
	} //FIN: if (valora !='Cualquiera')
}


/*
*	Descripcion : Cambia de color y estilo el elemento (una fila <tr> generalmente) cuando el Mouse esta encima (Mouse Over). Normalmente se llama así: 
*	Parametros  : 	'src', objeto 
					'clrOver', Color que se asigna
*	Autor		: Jacob Gutierrez Romero
*	Fecha		: 4 de Octubre de 2008
*/
function mOvr(src,clrOver) {
	src.style.cursor = 'hand';
	src.bgColor = clrOver;
}
					  
function mOut(src,clrIn) {
	src.style.cursor = 'default';
	src.bgColor = clrIn;
}

/*
*	Descripcion : Abrir una nueva ventana/popup con las dimensiones indicadas y ciertas caracteristicas de la ventana predefinidas:
					- toolbar: Sin barra de herramientas
					- location: No se despliega la direccion de la pagina
					- directories:
					- status: sin la barra inferior que indica el estatus
					- menubar: Sin la barra de menu
					-.... etc :)
*	Parametros  : 	'archivo', pagina que se llama/invoca
					'nombre', Nombre que se da a la ventana/pop-up. Recordar que si se trata de crear una ventana con el mismo nombre de otra (ya abierta), se reutiliza esa ventana y no habre otra.
					'ancho', ancho de la nueva ventana en pixeles.
					'alto', alto de la nueva ventana en pixeles.
*	Autor		: Jacob Gutierrez Romero
*	Fecha		: 10 de Octubre de 2008
*/
function ventanaFija(archivo,nombre,ancho,alto) {
	LeftPosition=(screen.width)?(screen.width-ancho)/2:100;
	TopPosition=(screen.height)?(screen.height-alto)/2:100;
	anexosayuda = window.open(archivo,nombre,"left="+LeftPosition+",top="+TopPosition+",height="+alto+",width="+ancho+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,maximized");
}


/*
*	Descripcion : Las siguientes funciones permiten el intercambio de valores entre Select`s.
*                 Permite la selección y traspaso de multiples registros.
*	Autor		: Rodrigo Marin Saxton
*	Fecha		: 17 de Octubre de 2008
*/
function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
	theSel.options[theIndex] = null;
  }
}

function intercambiarSelect(selectDesde, selectHasta)
{
  
  var selLength = selectDesde.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  for(i=selLength-1; i>=0; i--)
  {
	if(selectDesde.options[i].selected)
	{
	  selectedText[selectedCount] = selectDesde.options[i].text;
	  selectedValues[selectedCount] = selectDesde.options[i].value;
	  deleteOption(selectDesde, i);
	  selectedCount++;
	}
  }
  
  for(i=selectedCount-1; i>=0; i--)
  {
	addOption(selectHasta, selectedText[i], selectedValues[i]);
  }
}

