<!--

var idcounter = 6;

function sendPromocion()
{
	var forma = $('form_promocion_send');
	var pars = Form.serialize(forma);
	var busyElement = $('sendPromocionBusy');
		
	Element.show(busyElement);
	if( !Form.chequearCamposObligatorios(forma,'<span class="warning">Debe llenar los campos obligatorios</span>') )
	{
		Element.hide(busyElement);
		return;
	}
	
	var emailField  = $F('email');
	if( !emailField.match(mailRegex))
	{
		Element.hide(busyElement);
		alert('El correo ingresado es incorrecto'); 	 	
		return;
	}
	
	var url = URL_BASE + '/promociones/enviar/';
	new Ajax.Request(url, 	{
					 		parameters:pars,
							onComplete:function(r){
									Element.hide(busyElement);
									var response = r.responseText;
									if( response == '1')
									{
										Element.hide('fl_formulario');
										Element.show('fl_confirmacion');
										/*location.href = URL_BASE + '/promociones/fanny_enviado';*/
									}
									else
									{
										showErrorPromoResponse();
									}									
								}							
							});
}

function sendTacticas()
{
	var forma = $('form_tacticas_send');
	var pars = Form.serialize(forma);
	var busyElement = $('sendTacticasBusy');
		
	Element.show(busyElement);
	if( !Form.chequearCamposObligatorios(forma,'<span class="warning">Debe llenar los campos obligatorios</span>') )
	{
		Element.hide(busyElement);
		return;
	}
	
	var mail_ok = validate_emails('form_tacticas_send');
	
	if( mail_ok == false )
	{
		Element.hide(busyElement);
		alert('Error en algunos(s) de los correos ingresados');
		return;
	}
	
	var required_ok = required_mails('form_tacticas_send');
	
	if( required_ok == false )
	{
		Element.hide(busyElement);
		alert('Debes ingresar al menos 5 correos');
		return;
	}
	
	var duplicate_ok = duplicate_mails('form_tacticas_send');
	
	if( duplicate_ok == true )
	{
		Element.hide(busyElement);
		alert('No puedes ingresar correos repetidos');
		return;
	}
	
	var url = URL_BASE + '/promociones/tacticas_guardar/';
	new Ajax.Request(url, 	{
					 		parameters:pars,
							onComplete:function(r){
									Element.hide(busyElement);
									var response = r.responseText;
									if( response > '0')
									{
										location.href = URL_BASE + '/promociones/fannylu/' + response;
									}
									else
									{
										showErrorPromoResponse();
									}									
								}							
							});
}

function showErrorPromoResponse()
{
	var message = "Hubo un error al procesar los datos. Por favor intente de nuevo.";
	var divUpdate = $('divPromoResponse');
	divUpdate.innerHTML = message;
}

function add_email()
{
	var div = $('div_correos');
	var url = URL_BASE + '/promociones/tacticas_nodo/' + idcounter;
	idcounter = idcounter + 1;
	new Ajax.Updater(div, url,{ insertion:Insertion.Bottom } );
}

function remove_email(id)
{
	var div = $('correo_' + id);
	Element.remove(div);
}

function required_mails(forma)
{
	var forma = $(forma);
	var fields = forma.getElementsByClassName('email_field');
	var response = true;
	var counter = 0;
	fields = $A(fields);
		
	fields.each(function(field)
	{
		var value = field.value;
		
		if( !value.blank() )
		{
			counter++;	
		}
	});
	
	if( counter >= 5)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function duplicate_mails(forma)
{
	var forma = $(forma);
	var fields = forma.getElementsByClassName('email_field');
	var response = true;
	var counter = 0;
	fields = $A(fields);
	var original = fields.pluck('value');
	
	var duplicate = original.uniq();
	
	if( fields.size() == duplicate.size() )
	{
		return false;
	}
	
	return true;
}

function ismaxlength(obj)
{
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";
	
	if (obj.getAttribute && obj.value.length>mlength)
	{
		obj.value=obj.value.substring(0,mlength);
	}
}

function checkTest()
{
	var pars = $('form_test_send').serialize(true);
	
	if(pars['data[preg1]'] && pars['data[preg2]'] && pars['data[preg3]'] && pars['data[preg4]'] && pars['data[preg5]'])
	{
		$('form_test_send').submit();
	}
	else
	{
		alert('Debe contestar todas las preguntas')
	}
}					  

function sendTest()
{
	var forma = $('form_test_send');
	var pars = Form.serialize(forma);
	var busyElement = $('sendTestBusy');
		
	Element.show(busyElement);
	if( !Form.chequearCamposObligatorios(forma,'<span class="warning">Debe llenar los campos obligatorios</span>') )
	{
		Element.hide(busyElement);
		return;
	}
	
	var mail_ok = validate_emails('form_test_send');
	
	if( mail_ok == false )
	{
		Element.hide(busyElement);
		alert('Error en algunos(s) de los correos ingresados');
		return;
	}
	
	var required_ok = required_mails('form_test_send');
	
	if( required_ok == false )
	{
		Element.hide(busyElement);
		alert('Debes ingresar al menos 5 correos');
		return;
	}
	
	var duplicate_ok = duplicate_mails('form_test_send');
	
	if( duplicate_ok == true )
	{
		Element.hide(busyElement);
		alert('No puedes ingresar correos repetidos');
		return;
	}
	
	var url = URL_BASE + '/promociones/test_guardar/';
	new Ajax.Request(url, 	{
					 		parameters:pars,
							onComplete:function(r){
									Element.hide(busyElement);
									var response = r.responseText;
									if( response > '0')
									{
										location.href = URL_BASE + '/promociones/fannylu/' + response;
									}
									else
									{
										showErrorPromoResponse();
									}									
								}							
							});
}

-->
