// JavaScript Document
// FUNCIONES DE VALIDACION

// VALIDAR CORREO ELECTRONICO
function ValidateEmail(theElement, elementName, errorMsg, errorDiv)
{
	
	var elementValue = theElement.value;
	var msg = null;
	
	var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;


	if (elementValue.length == 0 ){
		printValidateError("El campo es requerido.", errorDiv, "false");
		return false;
	}
	
	if (filter.test(elementValue)){
		printValidateError(" ", errorDiv, "true");
		return true;
	}else{
		theElement.value = "";
		printValidateError(errorMsg, errorDiv, "false");
	}
	
	//theElement.focus();
	return false;
}



function Require(theElement, elementName, errorMsg, errorDiv)
{
	
	var elementValue = theElement.value;
	var msg = null;
	

	if (elementValue.length == 0 ){
		printValidateError(errorMsg, errorDiv, "false");
		return false;
	}else{
		printValidateError(" ", errorDiv, "true");
		return true;			
	}
	
	//theElement.focus();
	return false;
}




// AJAX
function newAjax(){
  var xmlhttp=false;
  try {
   // Creación del objeto ajax para navegadores diferentes a Explorer
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   // o bien
   try {
     // Creación del objet ajax para Explorer
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) {
     xmlhttp = false;
   }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
   xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
} 

// Valida los campos de formulario y e imprime el error en pantalla
function printValidateError(errorMsg, errorDiv, type){

	var container;
	container = document.getElementById(errorDiv); // errorDiv es el id del div que contiene la respuesta del php
	ajax=newAjax();
	ajax.open("GET", "/academia/inc/process.php?error="+ errorMsg + "&type="+type);
	ajax.onreadystatechange=function() 	{
		if (ajax.readyState==4) {
		   container.innerHTML = ajax.responseText;
		}
	}

	ajax.send(null);
	
}


// Envia de un ListBox los valores seleccionados a otro listBox
// El primer parametro es el listBox de origen y el segundo de destino
function move(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
   		}
	}
		arrFbox.sort();
		arrTbox.sort();
		fbox.length = 0;
		tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	
	for(c = 0; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
   }
}

// Confirma que realmente se desee borrar un registro
function msgDelete(CategoriaID){

	if(confirm("Desea eliminar este registro definitivamente?")){
		window.location= "index.php?url=categoria&action=delete&CategoriaID=" + CategoriaID;
	}
}

function msgDeletesc(SubcategoriaID){

	if(confirm("Desea eliminar este registro definitivamente?")){
		window.location= "index.php?url=subcategoria&action=delete&SubcategoriaID=" + SubcategoriaID;
	}
}

function msgDeletes(SitioID){

	if(confirm("Desea eliminar este registro definitivamente?")){
		window.location= "index.php?url=sitio&action=delete&SitioID=" + SitioID;
	}
}

function msgDeletets(TipoSitioID){

	if(confirm("Desea eliminar este registro definitivamente?")){
		window.location= "index.php?url=tipositio&action=delete&TipoSitioID=" + TipoSitioID;
	}
}
// Selecciona todos los valores del los listBox Modulos y paquetes
function selectListBox(listName, sent){
	id=document.getElementById(listName);
	for (i=0; ele = id.options[i]; i++)
	{
		ele.selected = true;
	}
	
	if(sent == true)
		document.frmSitio.submit();
}
