function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

//-------------------------------------------------------------------------------

function validarLlenado(campo, mensaje){
	if(campo.value==""){
		campo.value=mensaje;
	}
}

//-------------------------------------------------------------------------------

function validarSelect(campo, mensaje){
	if(campo.value==mensaje){
		campo.value = "";
	}
}

//-------------------------------------------------------------------------------

function seleccionarCodigo(pais){
	var codigo = pais.value;
	
	if(codigo!=""){
		codigo = codigo.split(",");
		
		document.getElementById("country").value = codigo[0];
		document.getElementById("country_code").value = codigo[1];
	}else{
		document.getElementById("country").value = "";
		document.getElementById("country_code").value = "Country Code";
	}
}

//-------------------------------------------------------------------------------

function validarEnvio(){
	if(document.getElementById("nom").value=="" || document.getElementById("nom").value=="First Name"){
		alert("Field First Name is required!");
		return;
	}
	
	if(document.getElementById("last").value=="" || document.getElementById("last").value=="Last Name"){
		alert("Field Last Name is required!");
		return;
	}
	
	if(document.getElementById("email").value=="" || document.getElementById("email").value=="E-mail address"){
		alert("Field E-mail address is required!");
		return;
	}
	
	if(document.getElementById("email_repeat").value=="" || document.getElementById("email_repeat").value=="Repeat email address"){
		alert("Field Repeat email address address is required!");
		return;
	}
	
	if(document.getElementById("email").value!=document.getElementById("email_repeat").value){
		alert("Field Email and Repeat Email must be equal!");
		return;
	}	
	
	if(document.getElementById("country").value==""){
		alert("Field Country is required!");
		return;
	}
	
	if(document.getElementById("country_code").value=="" || document.getElementById("country_code").value=="Country code"){
		alert("Field Country code is required!");
		return;
	}
	
	if(document.getElementById("mobile").value=="" || document.getElementById("mobile").value=="Phone number"){
		alert("Field Phone Number is required!");
		return;
	}
	if(document.getElementById("consent").checked == false){		
		alert("You must agree with the consent of UBIS");
		return;
	}
	
	if(document.getElementById("choose").value=="" || document.getElementById("choose").value=="choose"){
		alert("Field Choose one of our International Programs is required!");
		return;
	}
	
	document.getElementById("form1").submit();
}

//-------------------------------------------------------------------------------

function validarEnvioCompleto(){
	if(document.getElementById("Nationality").value=="" || document.getElementById("Nationality").value=="-Select-"){
		alert("Field Nationality is required!");
		return;
	}
	
	if(document.getElementById("Financial_Needs").value=="" || document.getElementById("Financial_Needs").value=="-Select-"){
		alert("Field Financial Needs is required!");
		return;
	}
	
	if(document.getElementById("Zip_Code").value==""){
		alert("Field Zip Code is required!");
		return;
	}
	
	if(document.getElementById("City").value==""){
		alert("Field City is required!");
		return;
	}
	
	if(document.getElementById("Country").value=="" || document.getElementById("Country").value=="-Select-"){
		alert("Field Country is required!");
		return;
	}
	
	if(document.getElementById("Interested_in").value=="" || document.getElementById("Interested_in").value=="-Select-"){
		alert("Field Interested in is required!");
		return;
	}
	
	if(document.getElementById("Year").value=="" || document.getElementById("Year").value=="-Select-"){
		alert("Field Year is required!");
		return;
	}
	
	if(document.getElementById("Period").value=="" || document.getElementById("Period").value=="-Select-"){
		alert("Field Period is required!");
		return;
	}
	
	if(document.getElementById("Attendance").value=="" || document.getElementById("Attendance").value=="-Select-"){
		alert("Field Attendance is required!");
		return;
	}
	
	if(document.getElementById("enrollment").checked==false){
		alert("You must agree with the content enrollment!");
		return;
	}
	
	if(document.getElementById("cupon").value!=""){
		var ajax=nuevoAjax();
		ajax.open("POST", "validarCupon.php?", false);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("numero="+document.getElementById("cupon").value);
		
		if (ajax.readyState==4){
			if(ajax.responseText){
				var mensaje = ajax.responseText;
				
				if(mensaje!="OK"){
					alert("The promotional code is invalid or has already been used, please confirm.");
					return;
				}
			}
		}
	}
	
	document.getElementById("form2").submit();
}

//-------------------------------------------------------------------------------

function validarCupon(){
	
	if(document.getElementById("cupon").value!=""){
		var ajax=nuevoAjax();
		ajax.open("POST", "validarCupon.php?", false);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("numero="+document.getElementById("cupon").value);
		
		if (ajax.readyState==4){
			if(ajax.responseText){
				var mensaje = ajax.responseText;
				
				if(mensaje!="OK"){
					document.getElementById("message_coupon").innerHTML="Invalid code, please check again";
				}else{
					document.getElementById("message_coupon").innerHTML="";
				}
			}
		}
	}else{
		document.getElementById("message_coupon").innerHTML="";
	}
}
