// JavaScript Document
window.addEvent('domready', function(){
	$('fade').addEvent('click', function(e) { hideBox() });
	document.addEvent('keydown', function(e){ if (e.key == 'esc' && openbox) { hideBox(); }});
});
function getAccesos() {
	var selpais = $("idpais");
	if (selpais.value != "") {
		var path = "json.acceso.php?id=" + selpais.value;
		var json = new Request.JSON({url: path, onComplete: function(obj) {													 
			var results = obj.data;
			var txttmp = "";
			if (obj.length > 0) {
				txttmp+= "<table width=\"309\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"grid\">";
 				for (j=0; j<obj.length; j++) {
					txttmp+= "<tr><td>"+obj[j].ciudad+"</td><td class=\"right\">"+obj[j].numero+"</td></tr>";
				}
				txttmp+= "</table>";
				$("accesos").set("html", txttmp);
				scracce.update();
			}
		}});
		json.get();
	}
}
function getRates(idpais) {
	var seltipo = $("idtipo");
	var seldest = $("destino");
	
	var path = "json.rates.php?idtipo=" + seltipo.value + "&idpais=" + idpais + "&destino=" + seldest.value;
	var json = new Request.JSON({url: path, onComplete: function(obj) {													 
		var results = obj.data;
		var txttmp = "";
		if (obj.length > 0) {
			txttmp+= "<table width=\"309\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"grid\">";
			for (j=0; j<obj.length; j++) {
				txttmp+= "<tr><td>"+obj[j].destino+"</td><td class=\"right_rate\">"+obj[j].precio+"</td></tr>";
			}
			txttmp+= "</table>";
			$("rates").set("html", txttmp);
			scrrate.update();
		}
	}});
	json.get();
}

var openbox = false;
function showBox() {
	w = 290;
	h = 400;
	openbox = true;
	wsize = $(document.body).getSize();
	wleft = (wsize.x - w)/2;
	wtop = (wsize.y - h)/2;
	$('light').setStyles({display: 'block', width: w, height: h, left: wleft, top: wtop });
	$('fade').setStyle('display', 'block');	
}
function hideBox() {
	openbox = false;
	$('light').setStyle('display', 'none');
	$('lightp').setStyle('display', 'none');
	$('fade').setStyle('display', 'none');
}
function showLogin() {
	w = 290;
	h = 400;
	openbox = true;
	wsize = $(document.body).getSize();
	wleft = (wsize.x - w)/2;
	wtop = (wsize.y - h)/2;
	$('lightp').setStyles({display: 'block', width: w, height: h, left: wleft, top: wtop });
	$('fade').setStyle('display', 'block');	
}

function sendForm() {
	// validate form
	var error = 0;
	if ($("txtnombre").value == "") {
		error++;
	}
	if ($("txtapellido").value == "") {
		error++;
	}
	if ($("txtdocu").value == "") {
		error++;
	}
	if ($("txtemail").value == "" || !(isMail($('txtemail').value))) {
		error++;
	}
	if ($("txtmensaje").value == "") {
		error++;
	}
	if ($("asunto").selectedIndex == 0) {
		error++;	
	}
	if (error == 0) {
		$('contact').submit();	
	} else {
		$('rpta').set('html', 'Error en el formulario.')	
	}
}
function clearForm() {
	$("txtnombre").value = "";
	$("txtapellido").value = "";
	$("txtdocu").value = "";
	$("txtdocu").value = "";
	$("txtmensaje").value = "";
	selectMe("asunto", 0, 0);
}
function clearFormLogin() {
	$("us").value = "";
	$("ps").value = "";
}
function isMail(value) {
	return /^[\w\-]+(\.[\w\-]+)*@[\w\-]+\.([\w\-]+\.)*[a-z]{2,}$/i.test(value);
}
function sendFormLogin() {
	if ($("us").value != "" && $("ps").value != "") {
		hideBox();
		win = window.open('','myWin','menubar=0,resizable=0,width=638,height=510');
		document.myForm.target = 'myWin';
		document.myForm.submit();
	}
}