//--------------------------------------------------------------------
//------------------------LA CUEVA - SITE-----------------------------
//--------------------------------------------------------------------

//Navigation functions---------------------------------------------------------------------

function goBack() {
	history.back();
}

function mostrar (cual) {
	abrirVentana(cual+".php", cual, 610, 300);
}

//Seleccionadores-------------------------------------------------------------------------

function elegirPagina (numPag) {
	theForm = document.seleccionador;	
	theForm.pag.value = numPag;
	theForm.submit();
}

function seleccionarSocioPadrino () {
	abrirVentana ("seleccion.php", "selectSocio", 450, 400);
}

function asignarSocioPadrino (itemNumber) {
	window.close();
	window.opener.focus();
	txtField = window.opener.document.getElementById('txtNumSocio');
	txtField.value = itemNumber;
	txtField.focus();
	button = window.opener.document.getElementById('cmdSocioPadrino');
	button.focus();
}

function seleccionarSocioJugador(id) {
	abrirVentana ("seleccion.php?idUsuario="+id, "selectSocio", 450, 400);
}

function asignarSocioJugador (itemNumber) {
	window.close();
	window.opener.focus();
	txtField = window.opener.document.getElementById('txtNumSocio');
	txtField.value = itemNumber;
	txtField.focus();
	button = window.opener.document.getElementById('cmdSocioJugador');
	button.focus();
}

//Recogedores de información--------------------------------------------------------------

function buscarInfoSocio (cod, where) {
	ifrm = document.getElementById('retriever');
	ifrm.src = "socioInfo.php?where="+where+"&codIngresado="+cod;
}

//Agenda / Reservacion de canchas y ranchos------------------------------------------------------

function refrescarAgenda(cod) {
	if (cod > 0) {
		ifrm = document.getElementById('agenda');
		ifrm.src = "agendaCanchas.php?which="+cod;
		ifrm.width = 500;
		ifrm.height = 700;
	}
}

function refrescarAgendaRanchos() {
	fechaField = document.getElementById('txtFecha');
	fecha = fechaField.value;
	array = fecha.split("/");
	fullDate = new Date(array[2], array[1], array[0]);

	today = new Date();
	cday = today.getDate();
	cmonth = today.getMonth()+1;
	if (cmonth.toString().length == 1) {
		cmonth = "0"+cmonth;
	}
	cyear = today.getFullYear();
	currentFullDate = new Date(cyear, cmonth, cday);

	if (fecha != "" && fullDate.getTime() >= currentFullDate.getTime()) {
		ifrm = document.getElementById('agenda');
		ifrm.src = "agendaRanchos.php?fecha="+fecha;
		ifrm.width = 450;
		ifrm.height = 570;
	}
	else {
		vbMsgError("Solamente puede reservar ranchos en fechas posteriores al dia de hoy (inclusive)");
		ifrm = document.getElementById('agenda');
		ifrm.src = "agendaRanchos.php";
		ifrm.width = 0;
		ifrm.height = 0;
	}
}

function incluirSegundaHora() {
	checkbox = document.getElementById("chkOtroJugador");
	row = document.getElementById("secondPlayer");
	if (checkbox.checked) {
		row.style.display = 'block';
	}
	else {
		row.style.display = 'none';
	}
}

//Hijos de los socios-----------------------------------------------------------------------------

function addChild(which) {
	currentForm = document.formRegistro;
	allOK = true;

	currentForm.nombreHijo.value = "";
	currentForm.fechaNacHijo.value = "";
	switch (which) {
		case "hijos18":
			currentForm.nombreHijo.value = currentForm.nombreHijo18.value;
			currentForm.fechaNacHijo.value = currentForm.fechaNacHijo18.value;	
		break;
		case "hijos25":
			currentForm.nombreHijo.value = currentForm.nombreHijo25.value;
			currentForm.fechaNacHijo.value = currentForm.fechaNacHijo25.value;	
		break;
		case "hijos":
			currentForm.nombreHijo.value = currentForm.nombreHijo1825.value;
			currentForm.fechaNacHijo.value = currentForm.fechaNacHijo1825.value;	
		break;
	}//switch
	errorMsg = "Problemas con la información del hijo(a) a incluir:";
	if (currentForm.nombreHijo.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe indicar el nombre completo";
	}
	today = new Date();
	day = today.getDate();
	month = today.getMonth()+1;
	if (month.toString().length == 1) {
		month = "0"+month;
	}
	year = today.getFullYear();
	currentDate = day+"/"+month+"/"+year;
	if (currentForm.fechaNacHijo.value == "" || currentForm.fechaNacHijo.value == currentDate) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar la fecha de nacimiento";
	}
	if (allOK) {
		currentForm.whichKind.value = which;
		currentForm.cmdAddChild.value = 1;
		currentForm.cmdRemoveChild.value = 0;
		currentForm.submit();
	}
	else {
		vbMsgError(errorMsg);
	}
}

function removeChild(pos, which) {
	currentForm = document.formRegistro;
	currentForm.whichKind.value = which;
	currentForm.cmdAddChild.value = 0;
	currentForm.cmdRemoveChild.value = 1;
	currentForm.posRemover.value = pos;
	currentForm.submit();
}

//Formulario de socios-------------------------------------------------------------------

function checkWebForm() {
	allOK = true;
	errorMsg = "Problemas con la información ingresada:";
	
	currentForm = document.formularioWeb;
	if (currentForm.txtNumSocio.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su número de socio";
	}
	if (currentForm.txtNombreSocio.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su nombre completo";
	}
	if (currentForm.txtMailSocio.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar un correo electrónico válido";
	}
	else {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		strEmail = currentForm.txtMailSocio.value;
		if (strEmail.search(validRegExp) == -1) {
			allOK = false;
			errorMsg = errorMsg + "\n-El correo electrónico proporcionado parece no ser válido";
		}
	}
	if (!allOK) {
		vbMsgError(errorMsg);
	}
	return (allOK);
}

function checkSocioInfo() {
	allOK = true;
	errorMsg = "Problemas con la información ingresada:";

	currentForm = document.formRegistro;
	if (currentForm.txtNombre.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su nombre completo";
	}
	if (currentForm.cboNacionalidad.value == "" || currentForm.cboNacionalidad.value == -1) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar su nacionalidad";
	}
	if (currentForm.txtCedula.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su cédula física para su registro";
	}
	today = new Date();
	day = today.getDate();
	month = today.getMonth()+1;
	if (month.toString().length == 1) {
		month = "0"+month;
	}
	year = today.getFullYear();
	currentDate = day+"/"+month+"/"+year;
	if (currentForm.txtFechaNacimiento.value == "" || currentForm.txtFechaNacimiento.value == currentDate) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar/seleccionar su fecha de nacimiento";
	}
	if (currentForm.cboEstadoCivil.value == "" || currentForm.cboEstadoCivil.value == -1) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar su estado civil";
	}
	if (currentForm.txtTelefono.value == "" && currentForm.txtCelular.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar al menos un número telefónico (particular/oficina o celular) donde contactarle";
	}
	/*
	if (currentForm.txtApartado.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar un apartado postal";
	}
	*/
	if (currentForm.txtCorreo.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar un correo electrónico válido";
	}
	else {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		strEmail = currentForm.txtCorreo.value;
		if (strEmail.search(validRegExp) == -1) {
			allOK = false;
			errorMsg = errorMsg + "\n-El correo electrónico proporcionado parece no ser válido";
		}
	}
	if (currentForm.txtDireccion.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su dirección física exacta";
	}
	if (currentForm.txtProfesion.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar su profesión actual";
	}
	if (currentForm.txtNombreTrabajo.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar el nombre de su lugar de trabajo";
	}
	if (currentForm.txtDireccionTrabajo.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar la dirección de su lugar de trabajo";
	}
	/*
	if (currentForm.txtConyuge.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar el nombre de su cónyuge";
	}
	if (currentForm.txtProfesionConyuge.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar la profesión actual de su cónyuge";
	}
	*/
	elegida = -1;
	valorElegido = "";
	for (i = 0; i < currentForm.rdoCC.length; i++){
		if (currentForm.rdoCC[i].checked == true) {
			elegida = i;
			valorElegido = currentForm.rdoCC[i].value;
		}
	}//for
	if (elegida == -1) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe indicar si tiene tarjeta de débito/crédito o no";
	}
	else {
		if (valorElegido == 1) {
			if (currentForm.cboBanco.value == "" || currentForm.cboBanco.value == -1) {
				allOK = false;
				errorMsg = errorMsg + "\n-Debe seleccionar un banco emisor de la tarjeta de débito/crédito";
			}
		}
	}
	/*
	if (currentForm.txtCodSocio.value == "" || currentForm.txtNomSocio.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar/indicar el nombre del socio que lo apadrina";
	}
	*/

	if (!allOK) {
		vbMsgError(errorMsg);
	}
	return (allOK);
}

function enviarFormulario() {
	currentForm = document.formRegistro;
	if (checkSocioInfo()) {
		currentForm.cmdEnviarFormulario.value = 1;
		currentForm.submit();
	}
}

function askChange() {
	check = document.getElementById("errorTipoSocio");
	if (check.value == 0) {
		result = callVBMsgConfirm("Si cambia el tipo de socio deberá reingresar toda la información que haya escrito hasta el momento. ¿Desea continuar?");
		if (result == 6) {
			return(true);
		}
		else {
			return(false);
		}
	}
}

//Particular de formulario y empleados/autorizados de los socios corporativos------------------------------------------------------

function checkDatosCorp() {
	currentForm = document.formRegistro;
	allOK = true;
	errorMsg = "Problemas con la información del socio corporativo a incluir:";

	if (currentForm.txtNombreEmpresa.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe indicar el nombre de la empresa";
	}	
	if (currentForm.cantEmpleados.value == "" || parseInt(currentForm.cantEmpleados.value) < 5) {
		allOK = false;
		errorMsg = errorMsg + "\n-Deben incluirse un mínimo de cinco (5) empleados de la empresa";
	}

	if (!allOK) {
		vbMsgError(errorMsg);
	}
	return (allOK);

}

function enviarFormularioCorp() {
	currentForm = document.formRegistro;
	if (checkDatosCorp()) {
		currentForm.cmdEnviarFormulario.value = 1;
		currentForm.submit();
	}
}

function addPartner() {
	currentForm = document.formRegistro;
	allOK = true;
	errorMsg = "Problemas con la información del empleado(a) a incluir:";
	if (currentForm.txtNombre.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar el nombre completo del empleado";
	}
	if (currentForm.cboNacionalidad.value == "" || currentForm.cboNacionalidad.value == -1) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar la nacionalidad del empleado";
	}
	if (currentForm.txtCedula.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar la cédula física del empleado";
	}
	today = new Date();
	day = today.getDate();
	month = today.getMonth()+1;
	if (month.toString().length == 1) {
		month = "0"+month;
	}
	year = today.getFullYear();
	currentDate = day+"/"+month+"/"+year;
	if (currentForm.txtFechaNacimiento.value == "" || currentForm.txtFechaNacimiento.value == currentDate) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar/seleccionar la fecha de nacimiento del empleado";
	}
	if (currentForm.cboEstadoCivil.value == "" || currentForm.cboEstadoCivil.value == -1) {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe seleccionar el estado civil del empleado";
	}
	if (currentForm.txtTelefono.value == "" && currentForm.txtCelular.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar al menos un número telefónico (particular/oficina o celular) del empleado";
	}
	/*
	if (currentForm.txtApartado.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar el apartado postal del empleado";
	}
	*/
	if (currentForm.txtCorreo.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar un correo electrónico válido del empleado";
	}
	else {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		strEmail = currentForm.txtCorreo.value;
		if (strEmail.search(validRegExp) == -1) {
			allOK = false;
			errorMsg = errorMsg + "\n-El correo electrónico proporcionado parece no ser válido";
		}
	}
	if (currentForm.txtDireccion.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar la dirección física exacta del empleado";
	}
	if (currentForm.txtProfesion.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe ingresar la profesión o puesto actual del empleado";
	}
	if (allOK) {
		currentForm.cmdAddPartner.value = 1;
		currentForm.cmdRemovePartner.value = 0;
		currentForm.submit();
	}
	else {
		vbMsgError(errorMsg);
	}
}

function removePartner(pos) {
	currentForm = document.formRegistro;
	currentForm.cmdAddPartner.value = 0;
	currentForm.cmdRemovePartner.value = 1;
	currentForm.posRemover.value = pos;
	currentForm.submit();
}

//Login----------------------------------------------------------------------------------

function checkLoginForm() {
	currentForm = document.loginForm;
	allOK = true;
	errorMsg = "Imposible ingresar al sistema debido a los siguientes problemas:";
	
	if (currentForm.txtLoginUsr.value == "") {
		errorMsg = errorMsg + "\n-Debe digitar su número de socio para ingresar";
		allOK = false;
	}
	if (currentForm.txtLoginPwd.value == "") {
		allOK = false;
		errorMsg = errorMsg + "\n-Debe proporcionar su contraseña correspondiente";
	}
	
	if (!allOK) {
		vbMsgError (errorMsg);
	}
	return (allOK);
}


function doLogin() {
	if (checkLoginForm()) {
		document.loginForm.startLogin.value = 1;
		document.loginForm.submit();
	}
}

function logout () {
	document.loginForm.logoutProcess.value = 1;
	document.loginForm.submit();
}

//Funciones globales----------------------------------------------------------------------

function activateField (valor, nombreForm, cual) {
	eval("document."+nombreForm+"."+cual+".disabled = "+!valor+";");
	eval("document."+nombreForm+"."+cual+".value = '';");
}

function activateCombo (valor, nombreForm, cual) {
	eval("document."+nombreForm+"."+cual+".disabled = "+!valor+";");
	eval("document."+nombreForm+"."+cual+".value = -1;");
}

//Ventanas Pop-Up ------------------------------------------------------------------------

function abrirVentana (pagina, nombre, width, height) {
	var address = pagina; //dirección de la página
	var op_tool = 0;  //barra de herramientas
	var op_loc_box = 0; //barra de direcciones
	var op_dir =  0; //vinculos/links
	var op_stat = 1;  //barra de estado  
	var op_menu = 0; //barra de menú
	var op_scroll = 1; //visibilidad de scrolls
	var op_resize = 0; //permitir cambiar el tamaño de la ventana
	if (width == "" || width == undefined) {
		var op_wid = 500; //ancho de la ventanta
	}
	else {
		var op_wid = width; //ancho de la ventanta
	}
	if (height == "" || height == undefined) {
		var op_heigh = 500; //alto de la ventana
	}
	else {
		var op_heigh = height; //alto de la ventana
	}
	var option = "toolbar=" + op_tool + ",location=" + op_loc_box + ",directories=" + op_dir + ",status=" + op_stat 
	+ ",menubar=" + op_menu + ",scrollbars="  + op_scroll + ",resizable=" + op_resize 
	+ ",width=" + op_wid + ",height=" + op_heigh;
	//var win3 = window.open("", "FotoGaleria", option);  
	//alert(address);

	var newWindow = window.open(address, nombre, option);
	window.opener = self;
	newWindow.moveTo(screen.width/2 - (op_wid/2), screen.height/2-(op_heigh/2));
	newWindow.focus();
}

//Javascript & VBScript compatibility--------------------------------------------------

function callVBMsgError (msg) {
	vbMsgError (msg);
}

function callVBMsgInfo (msg) {
	vbMsgInfo (msg);
}

function callVBMsgWarning (msg) {
	vbMsgWarning (msg);
}

function callVBMsgConfirm (msg) {
	return(vbMsgConfirm (msg));
}
