var COLOR_LISTA = "#C5DBD3";
var COLOR_MENU = "#FCCC88";
var TAMANO_MAXIMO_DET = 90;

function cargarBoton(control, imagen) {
	control.background=imagen;
}
function quitarBoton(control, imagen) {
	control.background=imagen;
}
function cambiaColor(control) {
	control.bgColor = COLOR_LISTA;
}
function devuelveColor(control) {
	control.bgColor = "";
}

function cambiaColorMenu(control) {
	control.bgColor = COLOR_MENU;
}

function cambiaColorMenu2(control, color) {
	control.bgColor = color;
}
function devuelveColorMenu2(control, color) {
	control.bgColor = color;
}

function cargarMenu(id) {
	document.location.href = "index2.php?c=" + id;
}

function filtrar() {
	if (document.getElementById("p")) {
		document.getElementById("p").value = "1";
	}
	document.formulario.submit();
}
	
function verMenu(id) {
	if (id.substr(0,4) == 'menu') {
		vValoresMenu = document.getElementById("valoresMenu").value.split("-");
		valorMenuPadreID = id.substr(0,id.length-3);
		
		for (i=0;i<vValoresMenu.length;i++) {
			if (vValoresMenu[i] != '') {
				if (vValoresMenu[i].length != 4) {
					document.getElementById('menu' + vValoresMenu[i]).style.display = "none";
				}
			}
		}
		
		for (i=0;i<vValoresMenu.length;i++) {
			if (vValoresMenu[i] != '') {
				vValoresMenu[i] = 'menu' + vValoresMenu[i];
				valorMenuPadre = vValoresMenu[i].substr(0,vValoresMenu[i].length-3);
				
				if (valorMenuPadreID == valorMenuPadre) {
					// SUS HERMANOS
					document.getElementById(vValoresMenu[i]).style.display = "block";
				} else {
					if (id == valorMenuPadre) {
						// SUS HIJOS
						document.getElementById(vValoresMenu[i]).style.display = "block";
					} else {
						if (valorMenuPadreID == vValoresMenu[i]) {
							// SUS PADRES: MUESTRO SU DESCENDENCIA Y LOS HERMANOS DE SU DESCENDENCIA
							for (var t=5;t<30;t+=3) {
								if (id.length > t) {
									inicio = id.substr(0,t);
									for (var k=1;k<100;k++) {
										var sAux = k + '';
										if (sAux.length == 1) { sAux = "00" + sAux; }
										if (sAux.length == 2) { sAux = "0" + sAux; }
										if (document.getElementById(inicio + sAux)) {
											document.getElementById(inicio + sAux).style.display = "block";
										}
									}
								}
							}							
						}
					}
				}
			}
		}
	}
}

function devuelveColor2(control, COLOR_ANTIGUO) {
	control.bgColor = COLOR_ANTIGUO;
}
function borrarSeleccionados() {
	if (document.getElementById("para_borrar").value == "") {
		alert("No ha seleccionado ningún registro para eliminar.");
		return false;
	}	
	if (!confirm("¿Está seguro de querer borrar los registros seleccionados?")) {
		return false;
	}
	document.getElementById("accion").value = "BORRAR";
	document.formulario.submit();
}
function borrar_agenda() {
	if (!confirm("¿Está seguro de querer borrar el registro seleccionado?")) {
		return false;
	}
	document.getElementById("i").value = "";
	document.getElementById("accion").value = "BORRAR";
	document.formulario.submit();
}
function cargarParaBorrar(control) {
	if (document.getElementById(control.id).checked) {
		document.getElementById("para_borrar").value = document.getElementById("para_borrar").value + control.id + ",";
	}
	if (!document.getElementById(control.id).checked) {
		document.getElementById("para_borrar").value = document.getElementById("para_borrar").value.replace(control.id + ",", "");
	}
}
function replaceAll_X(cadena, valor, valor_nuevo) {
	 while(cadena.indexOf(valor) >= 0) {
	 	cadena = cadena.replace(valor, valor_nuevo); 
	} 
	return cadena; 
}

function cambioFichero1(sValor, sValor2) {
	sValor2.value = sValor.value.replace('C:\\fakepath\\','');
}
function replaceAll( str, from, to ) {
	var idx = str.indexOf( from );
	while ( idx > -1 ) {
		str = str.replace( from, to ); 
		idx = str.indexOf( from );
	}
	return str;
}
	
function HeightMiddle(altoElemento) {
	/*
	altoElemento = altoElemento.replace("px","");
	if(window.innerHeight) { altoActual = window.innerHeight; } else { altoActual = document.body.offsetHeight; }
	alto = (altoActual/2)-(altoElemento/2);
	*/
	alto = 50;
	return alto;
}
function WithMiddle(anchoElemento) {
	anchoElemento = anchoElemento.replace("px","");
	if(window.innerWidth) { anchoActual = window.innerWidth; } else { anchoActual = document.body.offsetWidth; }
	ancho = (anchoActual/2)-(anchoElemento/2);
	return ancho;
}
function fbs_click(red) {
		u=location.href;
		t = 'DIVULGACION DINAMICA';
		if (red == 'FACEBOOK') {
			Destination = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u); // +'&t='+encodeURIComponent(t);
		}
		if (red == 'TWITTER') {
			Destination = 'http://twitter.com/home?status='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'GOOGLE') {
			Destination = 'http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'DIGGIT') {
			Destination = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'MYSPACE') {
			Destination = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(u) + '&t=' + t;
		}
		if (red == 'LINKEDIN') {
			Destination = 'http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(u) + '&title=' + t + '&summary=' + t;
		}
		OpenNewWindow(Destination,'VENTANA_FACEBOOK',800,600,'MC','yes','yes');
		return true;
}
function fbs_click2(red) {
		u=location.href;
		t = 'PROGRAMA DE FORMACION CONTINUA';
		if (red == 'FACEBOOK') {
			Destination = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u); // +'&t='+encodeURIComponent(t);
		}
		if (red == 'TWITTER') {
			Destination = 'http://twitter.com/home?status='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'GOOGLE') {
			Destination = 'http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'DIGGIT') {
			Destination = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(u) + '&title=' + t;
		}
		if (red == 'MYSPACE') {
			Destination = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(u) + '&t=' + t;
		}
		if (red == 'LINKEDIN') {
			Destination = 'http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(u) + '&title=' + t + '&summary=' + t;
		}
		OpenNewWindow(Destination,'VENTANA_FACEBOOK',800,600,'MC','yes','yes');
		return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//N = (document.all) ? 0 : 1;
var ob;
var N;
var formu;
function MD(e) {
	if (N) {
		ob = document.layers[e.target.name];
		X=e.x;
		Y=e.y;
		return false;
	} else {
		if (formu == "datos") {
			ob = document.getElementById("div_formulario").style;
		} else {
			if (formu == "filtro") {
				ob = document.getElementById("div_formulario_filtro").style;
			} else {
				if (formu == "columnas") {
					ob = document.getElementById("formularioColumnas").style;
				} else {
					if (formu == "excel") {
						ob = document.getElementById("formularioExcel").style;
					} else {
						ob = document.getElementById("formularioMailing").style;
					}
				}
			}
		}	
		//ob = event.srcElement.parentElement.style;
		if (navigator.appName == "Microsoft Internet Explorer")
		X=event.offsetX;
		Y=event.offsetY;
	}
}

function MM(e) {
	if (ob) {
		if (N) {
			ob.moveTo((e.pageX-X), (e.pageY-Y));
		}
		else {
			ob.pixelLeft = event.clientX-X + document.body.scrollLeft-10;
			ob.pixelTop = event.clientY-Y + document.body.scrollTop-10;
			return false;
		}
	}
}

function MU() {
	ob = null;
}

if (N) {
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
function mover1(control) {
	if (control == "datos") {
		N = document.div_formulario;
	} else {
		if (control == "filtro") {
			N = document.div_formulario_filtro;
		} else {
			if (control == "columnas") {
				N = document.formularioColumnas;
			} else {
				if (control == "excel") {
					N = document.formularioExcel;
				} else {
					N = document.formularioMailing;
				}
			}
		}
	}
	//alert(control);
	formu = control;
	document.onmousedown = MD;
	document.onmousemove = MM;
	document.onmouseup = MU;
}
function mover2() {
	document.onmousedown = "";
	document.onmousemove = "";
	document.onmouseup = "";
}
function OpenNewWindow(Destination,WindowName,Width,Height,Position,Scrollbars,Resizable,Left,Top)	{
/*	var features = "resizable:"+Resizable+";scroll:"+Scrollbars+";status:no;center:yes;help:no;dialogWidth:"+Width+"px;dialogHeight:"+Height+"px;";
	window.showModalDialog(Destination, window, features);
*/
//   Position where the window will open
//   --------------------
//   |   TL   TC   TR   |
//   |   ML   MC   MR   |
//   |   BL   BC   BR   |
//   --------------------
	DefaultPosition = 'MC';
	DefaultWidth = 200;
	DefaultHeight = 150;
	DefaultMargin = 10;
	
	if (!Position)
		{ Position = DefaultPosition; }

	if (!Height)
		{ Height = DefaultHeight; }

	if (!Width)
		{ Width = DefaultWidth; }

	if (!Scrollbars && !Resizable)
		{
		Scrollbars = 'no';
		Resizable = 'no';
		}

	if (!Left && !Top)
		{
		Left = DefaultMargin;
		Top = DefaultMargin;
		}

	if (Position == 'TC')
		{ Left = (screen.width - Width) / 2; }
	else if (Position == 'TR')
		{ Left = screen.width - Width - (Left * 2); }
	else if (Position == 'ML')
		{ Top = (screen.height - Height) / 2; }
	else if (Position == 'MC')
		{
		Left = (screen.width - Width) / 2;
		Top = (screen.height - Height) / 2;
		}
	else if (Position == 'MR')
		{
		Left = screen.width - Width - (Left * 2);
		Top = (screen.height - Height) / 2;
		}
	else if (Position == 'BL')
		{ Top = screen.height - (Height + (Top * 2) + 45); }
	else if (Position == 'BC')
		{
		Left = (screen.width - Width) / 2;
		Top = screen.height - (Height + (Top * 2) + 45);
		}
	else if (Position == 'BR')
		{
		Left = screen.width - Width - (Left * 2);
		Top = screen.height - (Height + (Top * 2) + 45);
		}

 	WindowName = window.open(Destination,WindowName,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + Scrollbars + ",resizable=" + Resizable + ",copyhistory=yes,top=" + Top + ",left=" + Left + ",width=" + Width + ",height=" + Height+",dialog=yes,modal=yes")
	WindowName.focus();

/*	if (ErrorOpeningPopups && !Window)
		{ alert(ErrorOpeningPopups); }
*/
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

function asociarDetalle(sDetalle) {
	if (document.getElementById("sel_detalle").value == "") {
		alert("Debe de seleccionar el " + sDetalle + ".");
		document.getElementById("sel_detalle").focus();
		return false;
	}
	// Comprobamos si existe
	if (document.getElementById("modcap").value != "") {
		if (document.getElementById("modcap").value.indexOf("/C/" + document.getElementById("sel_detalle").value + "/C/") != -1) {
			alert("Este " + sDetalle + " ya lo ha añadido.");
			document.getElementById("sel_detalle").focus();
			return false;
		}
	}
	
	// AÑADO EL DETALLE
	if (document.getElementById("sel_detalle").options[document.getElementById("sel_detalle").selectedIndex].text.length > TAMANO_MAXIMO_DET) {
		sel_detalle = document.getElementById("sel_detalle").options[document.getElementById("sel_detalle").selectedIndex].text.substr(0,TAMANO_MAXIMO_DET) + "...";
	} else {
		sel_detalle = document.getElementById("sel_detalle").options[document.getElementById("sel_detalle").selectedIndex].text;
	}		
	document.getElementById("modcap_des").value += "/C/" + sel_detalle + "/C/";
	document.getElementById("modcap").value += "/C/" + document.getElementById("sel_detalle").value + "/C/";
	cargarLista();		
}
function cargarLista() {
	sSel_detalles = "";
	document.getElementById("lab_detalle").innerHTML = "";
	if (document.getElementById("modcap").value != "") {
		vSel_detalles_des = document.getElementById("modcap_des").value.split("/C/");
		vSel_detalles = document.getElementById("modcap").value.split("/C/");
		for (i=0;i<vSel_detalles_des.length;i++) {
			if (vSel_detalles_des[i] != "") {
				imagenes = '';
				if (i>1 && i<vSel_detalles_des.length-2) {
					imagenes += '&nbsp;<img src="../ddimages/flecha_abajo.gif" height="10px" border="0" style="cursor:pointer;" onclick="bajarDetalle(\'' + vSel_detalles[i] + '\', \'' + vSel_detalles[i+2] + '\')"/>';
					imagenes += '&nbsp;<img src="../ddimages/flecha_arriba.gif" height="10px" border="0" style="cursor:pointer;" onclick="subirDetalle(\'' + vSel_detalles[i] + '\', \'' + vSel_detalles[i-2] + '\')"/>';
				} else {
					if (vSel_detalles_des.length > 3) {
						if (i==1) {
							imagenes += '&nbsp;<img src="../ddimages/flecha_abajo.gif" height="10px" border="0" style="cursor:pointer;" onclick="bajarDetalle(\'' + vSel_detalles[i] + '\', \'' + vSel_detalles[i+2] + '\')"/>';
						} else {
							if (i==vSel_detalles_des.length-2) {
								imagenes += '&nbsp;<img src="../ddimages/flecha_arriba.gif" height="10px" border="0" style="cursor:pointer;" onclick="subirDetalle(\'' + vSel_detalles[i] + '\', \'' + vSel_detalles[i-2] + '\')"/>';
							}
						}
					}
				}				
				sSel_detalles += '&nbsp;->&nbsp;' + vSel_detalles_des[i] + '&nbsp;<img src="../ddimages/borrar.gif" height="10px" border="0" style="cursor:pointer;" onclick="quitarDetalle(\'' + vSel_detalles[i] + '\')"/>' + imagenes + '<BR>';
			}
		}
	}
	document.getElementById("lab_detalle").innerHTML = sSel_detalles;
}
function quitarDetalle(codDetalle) {
	if (!confirm("¿Está seguro de querer quitarlo?")) {
		return false;
	}
	vSel_detalles = document.getElementById("modcap").value.split("/C/");
	vSel_detalles_des = document.getElementById("modcap_des").value.split("/C/");		
	document.getElementById("modcap").value = "";
	document.getElementById("modcap_des").value = "";
	for (i=0;i<vSel_detalles.length;i++) {
		if (vSel_detalles[i] != "") {
			if (vSel_detalles[i] != codDetalle) {			
				document.getElementById("modcap").value += '/C/' + vSel_detalles[i] + '/C/';
				document.getElementById("modcap_des").value += '/C/' + vSel_detalles_des[i] + '/C/';
			}
		}
	}
	cargarLista();
}
function subirDetalle(codDetalle, codDetalleAnterior) {
	nomDetalle = "";
	nomDetalleAnterior = "";
				
	detalles = document.getElementById("modcap").value;
	detalles_des = document.getElementById("modcap_des").value;
		
	vSel_detalles = document.getElementById("modcap").value.split("/C/");
	vSel_detalles_des = document.getElementById("modcap_des").value.split("/C/");		
	for (i=0;i<vSel_detalles.length;i++) {
		if (vSel_detalles[i] != "") {
			if (vSel_detalles[i] == codDetalle) {
				nomDetalle = vSel_detalles_des[i];
				nomDetalleAnterior = vSel_detalles_des[i-2];
			}
		}
	}
	detalles_des = detalles_des.replace("/C/" + nomDetalle + "/C/", "/C/@" + nomDetalleAnterior + "@/C/");
	detalles_des = detalles_des.replace("/C/" + nomDetalleAnterior + "/C/", "/C/@" + nomDetalle + "@/C/");
	detalles_des = replaceAll_X(detalles_des,"@","");
	document.getElementById("modcap_des").value = detalles_des;

	detalles = detalles.replace("/C/" + codDetalle + "/C/", "/C/@" + codDetalleAnterior + "@/C/");
	detalles = detalles.replace("/C/" + codDetalleAnterior + "/C/", "/C/@" + codDetalle + "@/C/");
	detalles = replaceAll_X(detalles,"@","");
	document.getElementById("modcap").value = detalles;

	cargarLista();
}
function bajarDetalle(codDetalle, codDetalleSiguiente) {
	nomDetalle = "";
	nomDetalleSiguiente = "";
				
	detalles = document.getElementById("modcap").value;
	detalles_des = document.getElementById("modcap_des").value;
		
	vSel_detalles = document.getElementById("modcap").value.split("/C/");
	vSel_detalles_des = document.getElementById("modcap_des").value.split("/C/");		
	for (i=0;i<vSel_detalles.length;i++) {
		if (vSel_detalles[i] != "") {
			if (vSel_detalles[i] == codDetalle) {
				nomDetalle = vSel_detalles_des[i];
				nomDetalleSiguiente = vSel_detalles_des[i+2];
			}
		}
	}
	detalles_des = detalles_des.replace("/C/" + nomDetalle + "/C/", "/C/@" + nomDetalleSiguiente + "@/C/");
	detalles_des = detalles_des.replace("/C/" + nomDetalleSiguiente + "/C/", "/C/@" + nomDetalle + "@/C/");
	detalles_des = replaceAll_X(detalles_des,"@","");
	document.getElementById("modcap_des").value = detalles_des;

	detalles = detalles.replace("/C/" + codDetalle + "/C/", "/C/@" + codDetalleSiguiente + "@/C/");
	detalles = detalles.replace("/C/" + codDetalleSiguiente + "/C/", "/C/@" + codDetalle + "@/C/");
	detalles = replaceAll_X(detalles,"@","");
	document.getElementById("modcap").value = detalles;

	cargarLista();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
