var foto = 1;
function OBJETO(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function enviaGET(URL, PARAMS, http) {
	myRand=parseInt(Math.random()*99999999);
	http.open("GET", URL+'?'+PARAMS+'&rand='+myRand, true);
	http.send(null);
}

function execute(FUNCION, item) {
	eval(FUNCION + '(item)');
}

function error(numero){
	var err='';
	numero = parseInt(numero);
	switch(numero){
		case 404: err = 'Página no encontrada';	
	}
	alert("Ha habido un problema al acceder al servidor: " + err);
}

function AJAX(URL, PARAMS, FUNCION, METOD){
	var http = OBJETO();

	http.onreadystatechange = function(){
		if(http.readyState == 4){
   			if(http.status == 200){
      			if(FUNCION != ''){
					execute(FUNCION, http.responseText);
				}
				http = null;
    		}else{	
				error(http.status);
				http = null;
			}
  		}
	}
	enviaGET(URL, PARAMS, http);
}
function SEND(PAGE, TXT){
	location.href = PAGE;
}
function pass(tipo){
	if(tipo == 0){
		if(foto == 1) foto = 28;
		else if(foto > 1) foto -= 1;
	}else{
		if(foto == 28) foto = 1;
		else if(foto < 28) foto += 1;	
	}
	document.getElementById('num').innerHTML = 'Fotograf&iacute;a ' + foto + ' de 28';
	document.getElementById('galeria').innerHTML = '<img src="galeria/max/'+ foto +'.JPG" width="350" height="300">';
}
function valor(obj){
	var str = new String();
	str = document.getElementById(obj).value;
	str = str.replace(/&/g,'y'); 
	return "&" + obj + "=" + str;
}
function enviar(){
	if(mal(5, document.getElementById('nombre').value)){
		alert("Debe especificar su nombre");
		return;
	}
	var s = document.getElementById('email').value;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (!filter.test(s)){
		alert("Ingrese una dirección de correo válida");
		return;
	}
	if(mal(5, document.getElementById('comentario').value)){
		alert("Debe especificar su comentario");
		return;
	}
	if(confirm('Desea enviar la queja o sugerencia?')){
		document.getElementById('temp').innerHTML = "<center>Enviando Correo...<br><img align='absmiddle' src='images/progress.gif' /></center>";
		var str = "ID=1";
		str += valor('placa');
		str += valor('chofer');
		str += valor('hora');
		str += valor('cedula');
		str += valor('nombre');
		str += valor('telefono');
		str += valor('fax');
		str += valor('direccion');
		str += valor('email');
		str += valor('comentario');
		AJAX('_envia.php', str, '_enviar', 'get');
	}
}
function _enviar(X){
	switch(X){
		case "1": alert("Correo enviado Exitosamente"); break;
		case "0": alert("El correo no puso ser enviado, intente más tarde"); break;
		default: alert("El correo no pudo ser enviado"); break;
	}
	document.getElementById('temp').innerHTML = "";
}
function mal(cantidad, valor){
	if (valor.length < cantidad){
		return true;
	}
	var p1 = valor;
  	var espacios = true;
  	var cont = 0;
	while (espacios && (cont < p1.length)) {
   		if (p1.charAt(cont) != " ") {
    		espacios = false;
   		}
   	cont++;
  	}
 
  	if (espacios) {
   		return true;
  	}
	return false;
}
