var popUp;
var popUpFamilias;
var popUpTexto;

function ValidaNIF(source, arguments) {
	var expresion = /^([0-9]{1,8}[a-zA-Z])$/
	if (!expresion.test(arguments.Value)) {
		arguments.IsValid = false
		return;
	}

	cadena = "TRWAGMYFPDXBNJZSQVHLCKET"
	letra_nif = arguments.Value.charAt(arguments.Value.length - 1);
	numeros = arguments.Value.substring(0, arguments.Value.length - 1);
	posicion = numeros % 23
	letra = cadena.substring(posicion, posicion + 1)
	if (letra_nif.toUpperCase() != letra) {
		arguments.IsValid = false;
		return;
	}
	arguments.IsValid = true;
}

function Open_ImgDG(url_imagen) {
	if (popUp != null) { try { popUp.close(); } catch (e) { ; } }
	popUp = window.open(url_imagen, 'PopUpImg', 'resizable=yes, status=no,toolbar=no,scrollbars=yes,menubar=false,width=600, height=500, left=0, top=0');
}

function Open_TxtRTF(param_idorigen, idorigen, param_campoorigen, campoorigen, param_tablaorigen, tablaorigen, idname) {
	if (popUpTexto != null) { try { popUpTexto.close(); } catch (e) { ; } }
	popUpTexto = window.open("_EditTexto.aspx?" + param_idorigen + "=" + idorigen + "&" + param_campoorigen + "=" + campoorigen + "&" + param_tablaorigen + "=" + tablaorigen + "&req_IdCliente=" + idname, 'PopUpRTF', 'resizable=no, status=no,toolbar=no,scrollbars=yes,menubar=false,width=900, height=650, left=50, top=50');
}

function SetTxtRTF(idcliente, closePopUp, postBack) {
	eval('var theform = document.Form1;');
	if (postBack) { __doPostBack('btnGuardar', ''); }
}

function Open_Word(sesionid, campossalida, tipolistado, idcongreso, estado, idpreferencia, idcomunicaciones, ordenadopor) {
	if (popUp != null) { try { popUp.close(); } catch (e) { ; } }
	popUp = window.open('Word/Salida_Word.aspx?WordParams_Sesion=' + sesionid + '&WordParams_CamposSalida=' + campossalida +
		'&WordParams_TipoListado=' + tipolistado + '&WordParams_IdCongreso=' + idcongreso + '&WordParams_Estado=' + estado +
		'&WordParams_PreferenciaGenerica=' + idpreferencia + '&WordParams_IdComunicaciones=' + idcomunicaciones + '&WordParams_OrdenadoPor=' + ordenadopor,
		'PopUpWord', 'resizable=yes, status=no,toolbar=no,scrollbars=yes,menubar=yes,width=600, height=500, left=0, top=0');
}

function Open_ArbolFamilias(_strhref) {
	if (popUpFamilias != null) { try { popUpFamilias.close(); } catch (e) { ; } }
	popUpFamilias = window.open(_strhref, 'PopUpArbolFamilias', 'resizable=yes, status=no,toolbar=no,scrollbars=yes,menubar=yes,width=400, height=500, left=0, top=0');
}

function Open_Impresion_CartasSistema(_strhref) {
	if (popUp != null) { try { popUp.close(); } catch (e) { ; } }
	popUp = window.open(_strhref, 'PopUpImpresion', 'resizable=yes, status=no,toolbar=no,scrollbars=yes,menubar=yes,width=800, height=600, left=0, top=0');
}

function OpenCalendar(idname, postBack) {
	popUp = window.open('Calendario.aspx?formname=' + document.forms[0].name +
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack,
		'popupcal',
		'width=165,height=208,left=0,top=0');
}

function OpenCuboColores(idname, postBack) {
	popUp = window.open('_DSRgbSlide.aspx?formname=' + document.forms[0].name +
		'&id=' + idname + '&postBack=' + postBack + '&selected=' + document.forms[0].elements[idname].value,
		'popupccol',
		'width=600,height=400,left=0,top=0');
}

function OpenDetalle_CalcularInscripcion_TotalCalculado() {
	eval('var theform = document.Form1;');

	var idinscripcion = theform.selId.options[theform.selId.selectedIndex].value;
	var idfechainscripcion = theform.selIdFechaInscripcionEvento.options[theform.selIdFechaInscripcionEvento.selectedIndex].value;

	if (idinscripcion != 0 && idfechainscripcion != 0) {
		popUp = window.open('_MostrarCalculo_InscripcionesConceptos.aspx?req_Id=' + idinscripcion +
			'&req_IdFechaInscripcionEvento=' + idfechainscripcion + '&Mrm_Listar=1',
			'popup_calcularinscripcion',
			'width=400,height=500,left=0,top=0');
	}
}

function OpenDetalle_CalcularInscripcion_CantidadAbonada() {
	eval('var theform = document.Form1;');

	var idinscripcion = theform.selId.options[theform.selId.selectedIndex].value;

	if (idinscripcion != 0) {
		popUp = window.open('_MostrarCalculo_InscripcionesConceptos.aspx?req_Id=' + idinscripcion + '&Mrm_Listar=1',
			'popup_calcularinscripcion',
			'width=400,height=500,left=0,top=0');
	}
}

function OpenDetalle_CalcularComunicacionGrupoCorreccion_NotaTotal() {
	eval('var theform = document.Form1;');

	var idcomunicacion = theform.selIdComunicacion.options[theform.selIdComunicacion.selectedIndex].value;

	if (idcomunicacion != 0) {
		popUp = window.open('_MostrarCalculo_Evaluaciones.aspx?req_IdComunicacion=' + idcomunicacion + '&Mrm_Listar=1',
			'popup_evaluacion',
			'width=400,height=500,left=0,top=0');
	}
}

function OpenDetalle_CalcularComunicacionGrupoCorreccion_NotaTotal_DG(idcomunicacion) {
	if (idcomunicacion != 0) {
		popUp = window.open('_MostrarCalculo_Evaluaciones.aspx?req_IdComunicacion=' + idcomunicacion + '&Mrm_Listar=1',
			'popup_evaluacion',
			'width=400,height=500,left=0,top=0');
	}
}

function SetDate(formName, id, newDate, postBack) {
	eval('var theform = document.' + formName + ';');

	popUp.close();
	theform.elements[id].value = newDate;

	/*
	if (postBack)
	__doPostBack(id,'');
	*/
}

function SetColor(formName, id, newColor, postBack) {
	eval('var theform = document.' + formName + ';');

	popUp.close();
	theform.elements[id].value = newColor;

	/*
	if (postBack)
	__doPostBack(id,'');
	*/
}

function OpenArchivoDescarga(fichero) {
	popUp = window.open('../archivos/' + fichero, 'Descarga', 'width=640,height=480,left=0,top=0');
}

function calcCharLeft(_idtexto, _idcontador, _maxcaracteres) {
	var _cajatexto;
	var _contadortexto;

	_cajatexto = eval("document.Form1." + _idtexto);
	_contadortexto = eval("document.Form1." + _idcontador);

	if (_cajatexto.value.length > _maxcaracteres) {
		_cajatexto.value = _cajatexto.value.substring(0, _maxcaracteres);
		_contadortexto.value = 0;
		// TODO: i18n
		alert("El texto ha sobrepasado el máximo de caracteres permitidos.");
	}
	else {
		_contadortexto.value = _maxcaracteres - _cajatexto.value.length;
	}
}

function OpenAutores(idname, sesion_name) {
	popUp = window.open('AddAutor.aspx?formname=' + document.forms[0].name + '&id=' + idname + '&sname=' + sesion_name,
		'popupautor',
		'width=530,height=650,left=0,top=0');
}

function SetAutores(formName, id) {
	popUp.close();
	__doPostBack(id, '');
}

function ClaveAjenaFromDataGrid(valor, texto, postBack) {
	return;
}

function __Nuevo() {
	return true;
}

function __Situarse() {
	return true;
}

function __Siguiente() {
	return true;
}

function __Anterior() {
	return true;
}

function __Ultimo() {
	return true;
}

function __Primero() {
	return true;
}

function __Cancelar() {
	return true;
}

function __getL10nTexto(key) {
	switch (key) {
		case "texto.confirm.insertar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, doy de alta estos datos?" : "Confirmation required, insert this data?";
		case "texto.confirm.alta":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, doy de alta estos datos?" : "Confirmation required, insert this data?";
		case "texto.confirm.modificar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, modifico estos datos?" : "Confirmation required, update this data?";
		case "texto.confirm.borrar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, elimino estos datos?" : "Confirmation required, delete this data?";
		case "texto.confirm.enviar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, envio estos datos?" : "Confirmation required, send this data?";
		case "texto.confirm.importar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, importo estos datos?" : "Confirmation required, import this data?";
		case "texto.confirm.exportar":
			return typeof (__reqlang) == "undefined" || __reqlang == null || __reqlang == "es" ? "Requerida confirmacion, exporto estos datos?" : "Confirmation required, export this data?";
	}
	return null;
}

function __Insertar() {
	if (confirm(__getL10nTexto("texto.confirm.insertar"))) {
		return true;
	}
	return false;
}

function __Alta() {
	if (confirm(__getL10nTexto("texto.confirm.alta"))) {
		return true;
	}
	return false;
}

function __Modificar() {
	if (confirm(__getL10nTexto("texto.confirm.modificar"))) {
		return true;
	}
	return false;
}

function __Borrar() {
	if (confirm(__getL10nTexto("texto.confirm.borrar"))) {
		return true;
	}
	return false;
}

function __Enviar() {
	if (confirm(__getL10nTexto("texto.confirm.enviar"))) {
		return true;
	}
	return false;
}

function __Enviar_Correo() {
	// TODO: i18n
	if (confirm("Requerida confirmacion, esta usted seguro/a de que desea enviar estos datos?")) {
		if (confirm("Requerida confirmacion, si usted no ha rellenado el campo 'Correo electronico' los datos se enviaran a sus destinatarios, continuar?")) {
			return true;
		}
	}
	return false;
}

function __Importar() {
	if (confirm(__getL10nTexto("texto.confirm.importar"))) {
		return true;
	}
	return false;
}

function __Exportar() {
	if (confirm(__getL10nTexto("texto.confirm.exportar"))) {
		return true;
	}
	return false;
}

function ValidaCombos(source, arguments) {
	if (arguments.Value == 0)
		arguments.IsValid = false;
	else
		arguments.IsValid = true;
}

function IrAVolver(_redireccion) {
	var _xreg = /@/gi;
	var _xreg2 = /&amp;/gi;

	_redireccion = _redireccion.replace(_xreg, "&");
	_redireccion = _redireccion.replace(_xreg2, "&");

	window.location.href = _redireccion;
}

function __ValidaTextAreas(source, arguments) {
	// Necesito maximo de caracteres	
	var _maxsize = source.id.substring(source.id.lastIndexOf("_") + 1, source.id.length);

	if (!isNaN(_maxsize)) {
		if (arguments.Value.length > _maxsize) {
			arguments.IsValid = false;
		}
		else {
			arguments.IsValid = true;
		}
	}
	else {
		arguments.IsValid = true;
	}
}

function __ValidaTextAreasDG(source, arguments) {
	// Necesito maximo de caracteres
	alert("a");

	var _maxsize = source.id.substring(source.id.lastIndexOf("_") + 1, source.id.length);
	_maxsize = _maxsize.substring(0, _maxsize.indexOf("DG"));

	alert(_maxsize);

	if (!isNaN(_maxsize)) {
		if (arguments.Value.length > _maxsize) {
			arguments.IsValid = false;
		}
		else {
			arguments.IsValid = true;
		}
	}
	else {
		arguments.IsValid = true;
	}
}

function Load_Presupuesto_ImportExport(idvalue, limitadora_id, limitadora_value, modalidad) {
	if (popUpLoad != null) { try { popUpLoad.close(); } catch (e) { ; } }
	if (String(limitadora_id) == 'undefined' || String(limitadora_id) == 'null') { limitadora_id = 0; }
	if (String(limitadora_value) == 'undefined' || String(limitadora_value) == 'null') { limitadora_value = 0; }
	popUpLoad = window.open('PTO_Import/ImportPresupuesto.aspx?CT=' + idvalue + '&LID=' + limitadora_id + '&LVA=' + limitadora_value + '&M=' + modalidad, 'PopUpImportExportPresupuesto', 'resizable=no, status=no,toolbar=no,scrollbars=yes,menubar=no,width=800, height=500, left=0, top=0');
}

function Load_EnvioCorreo(direccion, enviar) {
	if (direccion != "") {
		if (popUpLoad != null) { try { popUpLoad.close(); } catch (e) { ; } }
		if (enviar) {
			popUpLoad = window.open(direccion + '&frmEnviar=' + enviar, 'PopUpEnviarCorreo', 'resizable=no, status=no,toolbar=no,scrollbars=yes,menubar=no,width=680, height=680, left=0, top=0');
		}
		else {
			popUpLoad = window.open(direccion, 'PopUpMostrarResumen', 'resizable=no, status=no,toolbar=no,scrollbars=yes,menubar=no,width=680, height=680, left=0, top=0');
		}
	}
}
