function ajaxobj() {
	try {
		_ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			_ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			_ajaxobj = false;
		}
	}

	if (!_ajaxobj && typeof XMLHttpRequest != 'undefined') {
		_ajaxobj = new XMLHttpRequest();
	}

	return _ajaxobj;
}

function sinc(URL, DIV) {

	var check = "";
	check = document.getElementById("aceptar");

	if (check.checked == false) {

		alert('Debes aceptar la política de privacidad');
		return null;
	}

	ajax = ajaxobj();

	ajax.open("GET", URL, true);

	ajax.onreadystatechange = function() {
		switch (ajax.readyState) {
		case 1:
			document.getElementById(DIV).innerHTML = "Cargando...";
			break;

		case 4:
			document.getElementById(DIV).innerHTML = ajax.responseText;
			/* --- */
			document.getElementById(DIV).style.position = "absolute";
			// document.getElementById(DIV).style.top= "70px";
			// document.getElementById(DIV).style.left= "170px";
			document.getElementById(DIV).style.display = "block";
			centrarventana(DIV);
			/* --- */

			break;
		}
	}
	ajax.send(null);
}

function centrarventana(id) {
	document.getElementById(id).style.position = "absolute";
	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.visibility = "visible";
	// document.write(document.getElementById(id).offsetWidth);

	auxaltura = document.documentElement.scrollTop;
	limitealtura = (document.documentElement.clientHeight - document
			.getElementById(id).offsetHeight) / 2;

	document.getElementById(id).style.top = limitealtura + auxaltura + "px";
	auxaltura = (document.documentElement.clientWidth - document
			.getElementById(id).offsetWidth) / 2;
	document.getElementById(id).style.left = auxaltura + "px";

	/*--- modificado*/
	document.getElementById(id).style.top = ((document.getElementById(id).offsetHeight))
			+ 20 / 2 + "px";
	document.getElementById(id).style.left = (document.getElementById(id).offsetWidth)
			/ 2 + "px";

}
// //////////////////////////////////
// verificar el telefono de llamame
// //////////////////////////////////
function verificar(FORM, DIV) {
	var telefono = "";
	var asunto = "";
	var idioma = "";
	var hacer = 0;
	var url = "";

	telefono = document.getElementById("fono_llamame").value;
	asunto = document.getElementById("subject").value;
	idioma = document.getElementById("idioma_x").value;

	var url = "../ficha/callformenviado.php?idio=" + idioma + "&asunto="
			+ asunto + "&fono_llamame=" + telefono;

	// document.write(document.getElementById("fono").value);
	// document.write(document.getElementById("subject").value);
	// document.write(url);

	if (telefono.length > 0) {
		if (comprobartelefono(telefono) == 0) {
			alert('You must enter a valid phone');
			hacer = 1
		}
	} else {
		hacer = 1;
		alert('You must enter your phone');
	}

	if (hacer == 0) {
		sinc(url, DIV);
	}
}
// end

// //////////////////////////////////
// verificar datos del form quiero
// //////////////////////////////////
function verificar_form(FILE, DIV) {
	var telefono = "";
	var asunto = "";
	var nombre = "";
	var email = "";
	var hacer = 0;
	var comment = "";
	var url = "";
	var check = "";

	telefono = document.getElementById("phone_user").value;
	asunto = document.getElementById("subject_user").value;
	email = document.getElementById("email_user").value;
	nombre = document.getElementById("name_user").value;
	comment = document.getElementById("comment_user").value;
	check = document.getElementById("aceptar");

	// document.write(document.getElementById("name_user").value);
	// document.write(nombre);
	// document.write(email);

	var url = FILE + "&asunto=" + asunto + "&telefono_u=" + telefono
			+ "&email_u=" + email + "&nombre_u=" + nombre + "&comment_u="
			+ comment;

	if (check.checked == false) {
		hacer = 1;
		alert('Debes aceptar la politica de privacidad');

	}

	// comprobar nombre
	if (nombre == null || nombre == "") {
		hacer = 1;
		alert('You must enter your name');
	}

	// comprobar telefono
	if ((hacer == 0) && telefono.length > 0) {
		if (comprobartelefono(telefono) == 0) {
			alert('You must enter a valid phone');
			hacer = 1
		}
	}

	if ((hacer == 0) && (telefono == null || telefono == "")) {
		hacer = 1;
		alert('You must enter your phone');
	}

	// comprobar email
	if ((hacer == 0) && email.length > 0)
		hacer = compruebaemail(email);

	if (hacer == 0 && (email == null || email == "")) {
		alert('You must enter a valid email');
		return "";
	}

	if (hacer == 0) {
		sinc(url, DIV);

		document.getElementById("email_user").value = "";
		document.getElementById("name_user").value = "";
		document.getElementById("phone_user").value = "";
		document.getElementById("comment_user").value = "";
	}

}
// end

function verificar_form_contactar(datoofer, soyinmo, idio, DIV) {
	var telefono = "";
	var asunto = "";
	var nombre = "";
	var email = "";
	var hacer = 0;
	var url = "";
	var comentario = "";

	telefono = document.getElementById("phone_user").value;
	asunto = document.getElementById("subject_user").value;
	email = document.getElementById("email_user").value;
	nombre = document.getElementById("name_user").value;
	comentario = document.getElementById("comment_user").value;

	// document.write(telefono.length);
	// document.write(nombre);

	var url = "fichapisoenviar.php?idio=" + idio + "&datoofe=" + datoofer
			+ "&soyinmo=" + soyinmo + "&subject=" + asunto + "&telefono="
			+ telefono + "&textarea=" + comentario + "&txtemail=" + email
			+ "&txtnombre=" + nombre + "&contactar=1";

	// comprobar nombre
	if (nombre == null) {
		hacer = 1;
		alert('Debe ingresar su nombre');
	}

	// comprobar telefono
	if (telefono.length > 0) {
		if (comprobartelefono(telefono) == 0) {
			alert('Debe introducir un teléfono válido');
			hacer = 1
		}
	} else {
		hacer = 1;
		alert('Debe de introducir su teléfono');
	}

	// comprobar email
	if (email.length > 0)
		hacer = compruebaemail(email);
	else
		hacer = 1;

	if (hacer == 1) {
		alert('Debe ingresar un email Válido');
		return "";
	}

	if (hacer == 0) {
		sinc(url, DIV);
	}

	document.getElementById("email_user").value = "";
	document.getElementById("name_user").value = "";
	document.getElementById("phone_user").value = "";
	document.getElementById("comment_user").value = "";
}
// end

function cerrarventana(id) {
	document.getElementById(id).style.display = 'none';
	document.getElementById(id).style.visibility = "hidden";
}

function enviarsolicitud(id, idioma) {
	var url = "callformusuario.php";
	var telefono = "";
	var email = "";
	var hacer = 0;
	var nombre = "";
	var asunto = "";
	var from = "";
	telefono = document.getElementById("telefono").value;
	asunto = document.getElementById("asunto").value;
	email = document.getElementById("email").value;
	nombre = document.getElementById("nombre").value;
	from = document.getElementById("from").value;

	if (telefono.length > 0) {
		if (comprobartelefono(telefono) == 0) {
			alert('Debes de introducir un telefono valido');
			hacer = 1
		}
	} else {
		hacer = 1;
		alert('Debes de introducir un telefono valido');
	}

	if ((hacer == 0) && (email.length > 0))
		hacer = compruebaemail(email);

	if (hacer == 0) {
		ajax = ajaxobj();
		ajax.open("GET", url + "?idio=" + idio + "&asunto=" + asunto
				+ "&telefono=" + telefono + "&email=" + email + "&nombre="
				+ nombre, true);
		ajax.onreadystatechange = function() {

			if (ajax.readyState == 4) {

				document.getElementById(id).innerHTML = ajax.responseText;
				document.getElementById(id).style.position = "absolute";
				document.getElementById(id).style.display = "block";
				document.getElementById(id).style.visibility = "visible";
				centrarventana(id);
			}
		}
		ajax.send(null)

		document.getElementById("telefono").value = "";
		document.getElementById("email").value = "";
		document.getElementById("nombre").value = "";
	}

}

function comprobartelefono(telefono) {
	var si = 0;

	if (telefono.length < 9)
		return 0;
	if (telefono == "999999999")
		return 0;
	if (telefono == "888888888")
		return 0;
	if (telefono == "666666666")
		return 0;
	if (telefono == "000000000")
		return 0;
	if (telefono.indexOf('00000') > 0)
		return 0;
	if (telefono.indexOf('11111') > 0)
		return 0;
	if (telefono.indexOf('22222') > 0)
		return 0;
	if (telefono.indexOf('33333') > 0)
		return 0;
	if (telefono.indexOf('44444') > 0)
		return 0;
	if (telefono.indexOf('55555') > 0)
		return 0;
	if (telefono.indexOf('66666') > 0)
		return 0;
	if (telefono.indexOf('77777') > 0)
		return 0;
	if (telefono.indexOf('88888') > 0)
		return 0;
	if (telefono.indexOf('99999') > 0)
		return 0;
	if (telefono.indexOf('00000') > 0)
		return 0;
	if (telefono.indexOf('12345') > 0)
		return 0;
	if (telefono.indexOf('23456') > 0)
		return 0;
	if (telefono.indexOf('34567') > 0)
		return 0;
	if (telefono.indexOf('45678') > 0)
		return 0;
	if (telefono.indexOf('56789') > 0)
		return 0;
	if (telefono.indexOf('67890') > 0)
		return 0;
	if (telefono.indexOf('01234') > 0)
		return 0;
	if (telefono.indexOf('09876') > 0)
		return 0;
	if (telefono.indexOf('98765') > 0)
		return 0;
	if (telefono.indexOf('87654') > 0)
		return 0;
	if (telefono.indexOf('76543') > 0)
		return 0;
	if (telefono.indexOf('65432') > 0)
		return 0;
	if (telefono.indexOf('54321') > 0)
		return 0;

	if (telefono.length == 9) {

		if (telefono.substr(0, 1) == "9")
			si = 1;

		if (telefono.substr(0, 1) == "8")
			si = 1;

		if (telefono.substr(0, 1) == "6")
			si = 1;

	}

	return si;
}

function compruebaemail(mxa) {
	var cade
	var nohacer
	nohacer = 0;

	if ((mxa.indexOf('@') < 1) && (nohacer == 0))
		nohacer = 1;
	if ((mxa.indexOf('.') < 1) && (nohacer == 0))
		nohacer = 1;
	if ((mxa.indexOf(' ') > 0) && (nohacer == 0))
		nohacer = 1;
	if ((mxa.length < 5) && (nohacer == 0))
		nohacer = 1;

	if (nohacer == 1) {
		alert("Error in your E-mail");
		return 1;
	} else
		return 0;

}
