window.addEvent('domready', function () {
	
	var form = 'myForm';
	var info = 'mas-info';
	var radio_sel = 'radio';
	var importe = $('Ds_Merchant_Amount');
	
	if($(form)) {
		$(form).addEvent('submit', function(e) {
			
			new Event(e).stop();
			
			if(valida_formulario() == true) {
				
				if(importe.value != "") {
			
					var signature = $('signature');
					var merchantData = $('Ds_Merchant_MerchantData');
					var url = "http://www.sitsms.com/envio-sms/validation.asp";
					
					merchantData.value = $('Ds_Merchant_Titular').value + ":" + $('password').value + ":" + $('telefono').value + ":" + $('email').value + ":" + $('radio').value;
					
					var data = this.toQueryString();
					
					var myAjax = new XHR({
										 method: 'post',
										 onSuccess: function(response) {
											 signature.value = response;
											 $('myForm').submit();
										}
					}).send(url, data);
					
				} else {
					alert("Seleccione un producto");
				}
			
			}
			
		});
	}
	
	if($(info)) {
		$(info).addEvent('click', function() {
			
			if(valida_formulario() == true) {
			
				var mail_ok = $('mail-ok').value;
				var send_mail = $('send-mail').value;
				var data = $(form).toQueryString();
				
				var myAjax = new XHR({
									 method: 'post',
									 onSuccess: function(response) {
										alert("Hemos recibido sus datos correctamente.");
										window.location = mail_ok;
									}, 
									onFailure: function() {
										alert("Ha habido un error, por favor vuelva a intentarlo.")
									}
				}).send(send_mail, data);
				
			}
			
		});
	}
	
	if($(radio_sel)) {
		$(radio_sel).addEvent('change', function() {
			var pack = $('radio').getValue();
			var urlOK = $('Ds_Merchant_UrlOK');
			var importe = $('Ds_Merchant_Amount');
			var order = $('Ds_Merchant_Order');
			var precio = '';
			
			if (pack == 128) {
				precio = 10000;
			} else if (pack == 129) {
				precio = 25000;
			} else if (pack == 130) {
				precio = 50000;
			} else if (pack == 131) {
				precio = 30000;
			} else if (pack == 132) {
				precio = 90000;
			} else if (pack == 133) {
				precio = 180000;
			}
			
			if (pack == "") {
				importe.value = null;
			} else {
				importe.value = precio * 1.16;
				urlOK.value = urlOK.getValue() + "&pack=" + pack + "&order=" + order.getValue();
			}
		});
	}
});

function valida_formulario(){
    //valido el nombre
	var msg = "";
	
    if (document.fvalida.Ds_Merchant_Titular.value.length == "") {
       msg += "Por favor, escriba su nombre.\r\n";
    }
	
    //valido empresa
    if (document.fvalida.empresa.value.length == "") {
       msg += "Por favor, especifique su empresa.\r\n";
    }
	
    //valido el telefono
	if (document.fvalida.telefono.value.length == "") {
		msg += "Por favor, introduzca un numero de telefono.\r\n";
	} else if (document.fvalida.telefono.value.length != 9) {
		msg += "El numero de telefono, debe contener 9 digitos.\r\n";
	}
	if (isNaN(document.fvalida.telefono.value)) {
		msg += "El telefono debe componerse solo de numeros.\r\n";
	}
	
	//valido el email
	if (document.fvalida.email.value.length == "") {
		msg += "Por favor, introduzca un e-mail.\r\n";
	} else if (validateEmail(document.fvalida.email.value) == false) {
		msg += "Por favor, introduzca un e-mail correcto.\r\n";
	}

	
	if (msg != "") {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function validateEmail(email) {
	var regExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return regExp.test(email);
}