function loadingAjax(isStart, idCombo) {
	if (isStart) {	
		var lista = document.getElementById(idCombo);
		var op = lista[0];
		var lop = new Option('Cargando Info...',-1);
		while(lista.length>0) {
			lista.remove(0); 
		}
		try { 
			lista.add(op,null);
			lista.add(lop,null);
			lista.selectedIndex = 1;
		} catch (ex) {
			lista.add(op);
			lista.add(lop);
			lista.selectedIndex = 1;
		}
	}

}

function crearMensaje(div,mensaje) {
	if (div != null) {
		while (div.hasChildNodes()){
			div.removeChild(div.firstChild);
		}
		var message = document.createTextNode(mensaje);
		div.appendChild(message);
	}	
}

function cambiarLista(elementos,idLista) {
	if (elementos.length > 0) {
		var lista = document.getElementById(idLista);
		var op = lista[0];
		while(lista.length>0) {
			lista.remove(0); 
		}
		try { lista.add(op,null);} catch (ex) {lista.add(op);}					
		for (i=0;i<elementos.length;i++) {
			var opcion = elementos[i];
			var codigoOpcion = opcion.getElementsByTagName("id")[0];
			var nombreOpcion = opcion.getElementsByTagName("nombre")[0];
			op = new Option(nombreOpcion.childNodes[0].nodeValue/*value*/,codigoOpcion.childNodes[0].nodeValue/*option*/);
			try {
				lista.add(op,null);
			} catch (ex) {
				lista.add(op);
			}
		}
		lista.focus();
	}
}


function cambiarCombo(id,idCombo,paramName) {
	if (id == "") {
		return false;
	}
	loadingAjax(true,idCombo);
	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				cambiarLista(elementos, idCombo);
				loadingAjax(false,idCombo);
			} 
		}
	}
	var str  = paramName+"="+id;
	req.send(str);

}

function cambiarPais(idCurso) {
	cambiarCombo(idCurso,'paises','curso');
}

function cambiarCiudad(idPais) {
	var idCombo = 'ciudades';
	loadingAjax(true,idCombo);
	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				cambiarLista(elementos, idCombo);
				loadingAjax(false, idCombo);
			} 
		}
	}
	var str  = "pais="+idPais+"&tipo="+document.getElementById('cursos').value;
	req.send(str);

}

function cambiarCentro(id) {
	var paramName = 'ciudad';
	var idCombo = 'centros';
	var path = "ajaxcotizador.php";
	loadingAjax(true,idCombo);
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				cambiarLista(elementos, idCombo);
				loadingAjax(false,idCombo);
			} 
		}
	}
	var str  = paramName+"="+id+"&tipo="+document.getElementById('cursos').value;;
	req.send(str);
	
}

function cambiarLecciones(idCurso) {
	if (idCurso == "") {
		return false;
	}
	var idCombo = 'lecciones';
	loadingAjax(true, idCombo);
	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				cambiarLista(elementos, idCombo);
				loadingAjax(false, idCombo);
			} 
		}
	}
	var str = "lecciones=1&c1="+idCurso;
	req.send(str);
}


function cambiarEspecificos() {
	var id1 = document.getElementById('cursos').value;
	var id2 = document.getElementById('paises').value;
	var id3 = document.getElementById('ciudades').value;
	var id4 = document.getElementById('centros').value;
	if (id1 == "" || id2 == "" || id3 == "" || id4 == "") {
		return false;
	}
	var idCombo = 'cursoEsp';
	loadingAjax(true, idCombo);
	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				cambiarLista(elementos, idCombo);
				loadingAjax(false, idCombo);
			} 
		}
	}
	var str = "cursoEsp=1&cursos="+id1+"&paises="+id2+"&ciudades="+id3+"&centros="+id4;
	req.send(str);
}

function cotizar(idTipProp) {
	var forma = document.getElementById('forma');
	var eles = forma.elements;
	var str = "";
	var total = 0;
	var sumTmp = 0;
	var moneda = "";
	for (i = 0; i<eles.length; i++) {
		if (eles[i].type == "select-one") {
			if (eles[i].id.substring(0,6) == "combo_") {
				if (eles[i].value != "") {
					var tmp = parseFloat(eles[i].value.split('_')[1]);
					var moneda = eles[i].value.split('_')[3];
					total = total + tmp;
					if (idTipProp == eles[i].value.split('_')[2]) {
						var div = document.getElementById('res_'+idTipProp);
						sumTmp = sumTmp + tmp;
						crearMensaje(div,sumTmp);
					}
				} 
			}
		}
	}
	document.getElementById('ccfrom').value = moneda;
	crearMensaje(document.getElementById('total'),total+" "+moneda);
	try {
		document.getElementById('total1').value = total;	
		crearMensaje(document.getElementById('totalmoneda'),moneda);
	} catch (err) {
	}
	
}

function nuevaCotizacion() {
	var combos = document.getElementsByTagName('select');
	for (i=0;i<combos.length;i++) {
		if (combos[i].id != "cursos" && combos[i].id != "ccfrom" && combos[i].id != "ccto") {
			while(combos[i].length>0) {
				combos[i].remove(0); 
			}
			var op = new Option('Seleccionar...','');
			try { combos[i].add(op,null);} catch (ex) {combos[i].add(op);}
		}
	}
	document.getElementById('cursos').value = '';
	crearMensaje(document.getElementById('totalConv'), "");
	iniciarLabels();
	try {
		document.getElementById('total1').value = 0;	
		crearMensaje(document.getElementById('totalmoneda'),'');
	} catch (err) {
	}
}

function iniciarCombos() {
	var combos = document.getElementsByTagName('select');
	for (i=0;i<combos.length;i++) {
		if (combos[i].id.substring(0,6) == "combo_") {
			var op = combos[i][0];//new Option('Seleccionar...','');
			while(combos[i].length>0) {
				combos[i].remove(0); 
			}
			try { combos[i].add(op,null);} catch (ex) {combos[i].add(op);}
		}
	}
	iniciarLabels();
}

function iniciarLabels() {
	var divs = document.getElementsByTagName('div');
	for (i=0;i<divs.length ; i++) {
		if (divs[i].id.substring(0,4) == 'res_' || divs[i].id == 'total') {
			crearMensaje(divs[i],'0.00');
		}
	}
}

function cargarCombos() {	
	iniciarCombos();
	var pais = document.getElementById('paises').value;
	var ciudad = document.getElementById('ciudades').value;
	var curso = document.getElementById('cursoEsp').value;
	var centro = document.getElementById('centros').value;
	var lecciones = document.getElementById('lecciones').value;
	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var elementos = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("elemento");
				for (i=0;i<elementos.length;i++) {
					var opcion = elementos[i];
					var codigoOpcion = opcion.getElementsByTagName("id")[0].childNodes[0].nodeValue;
					var nombreOpcion = opcion.getElementsByTagName("nombre")[0].childNodes[0].nodeValue;
					var idPropiedad = opcion.getElementsByTagName("propiedad")[0].childNodes[0].nodeValue;
					var moneda = opcion.getElementsByTagName("moneda")[0].childNodes[0].nodeValue;
					var combo = document.getElementById('combo_'+idPropiedad);
					if (combo != null) {	
						var costo = opcion.getElementsByTagName("costo")[0].childNodes[0].nodeValue;
						var tipo = opcion.getElementsByTagName("tipo")[0].childNodes[0].nodeValue;
						var op = new Option(nombreOpcion/*value*/,codigoOpcion+"_"+costo+"_"+tipo+"_"+moneda/*option*/);
						try {
							combo.add(op,null);
						} catch (ex) {
							combo.add(op);
						}
					}
				}
			} 
		}
	}
	var str = "cc=1&c="+curso+"&l="+lecciones;
	req.send(str);
}

function convertirTotal() {
	var total = document.getElementById('total').innerHTML;
	var ccfrom = document.getElementById('ccfrom').value;
	var ccto = document.getElementById('ccto').value;

	crearMensaje(document.getElementById('totalConv'), "Un momento por favor, convirtiendo valor...");

	var totalNum = "";
	for (i=0; i<total.length; i++ ) {
		if ("0123456789.".indexOf(total.charAt(i))>-1) {
			totalNum = totalNum + total.charAt(i);
		}
	}

	var path = "ajaxcotizador.php";
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", path, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				var valor = req.responseXML.getElementsByTagName("resp")[0].getElementsByTagName("valor")[0].childNodes[0].nodeValue;
				crearMensaje(document.getElementById('totalConv'), totalNum+" "+ccfrom+" = "+valor+" "+ccto);
			}
		}
	}

	var str = "conv=1&v="+totalNum+"&c0="+ccfrom+"&c1="+ccto;
	req.send(str);
}

/* FIN FUNCIONES DEL COTIZADOR */


//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 1000;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

 function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display == "none") ? "block" : "none";
	}
 }


