
function submitContact(event) {
	Event.stop(event);
	
	return submitForm('contactform');
}


function submitNewsletter(event) {
	Event.stop(event);
	
	return submitForm('newsletterform');
}

 
function submitRegister(event) {
	Event.stop(event);
	
	return submitForm('registerform');
}


function submitFAQ(event) {
	Event.stop(event);
	
	return submitForm('faqform');
}

function submitLogin(event) {
	Event.stop(event);
	
	return submitForm('loginform', '/login/', submitLoginError);
}

function submitLoginError(errors) {
	try {
		if (errors.activacao) {
			$('login_error').hide();
			$('login_activate').show();
			return;
		}
	
		if (errors.error) {
			$('login_activate').hide();
			showErrors([errors.error]);
			return;
		}
		
		throw "";
	} catch (e) {
		$('login_activate').hide();
		showErrors();
	}
}


function submitPassword(event) {
	Event.stop(event);
	
	return submitForm('lostpassword', 
			          '/password/', 
			          undefined, 
			          $$('#lostpassword .enviar').first(), 
			          $$('.lostpassword_error').first(),
			          $('lostpassword_msg'));
}

function submitNewPassword(event) {
    
	Event.stop(event);
	
	return submitForm('passwordform');
}

function submitGame(event) {
    
	Event.stop(event);
	
	return submitForm('gameform', location.pathname, submitGameError);
}

function submitGameError(errors) {
	try {
		if (errors.redirect) {
			location.href = errors.redirect;
			return;
		}
	
		if (errors.error) {
			showErrors([errors.error]);
			return;
		}
		
		throw "";
	} catch (e) {
		showErrors();
	}
}


function submitForm(form, url, callbackerror, sendbutton, errorhtml, msghtml) {
	
	if (typeof(window.submited) != 'undefined' && window.submited == true)
		return false;
	
	if (typeof(sendbutton) == 'undefined')
		sendbutton = $$('a.enviar').first();

	sendbutton.setStyle({'color': '#3390ff'});
	
	new Ajax.Request(((typeof(url) == 'undefined') ? location.pathname : url), {
		method: 'post',
		parameters: $(form).serialize(),
		onSuccess: function(transport) {
		    
			window.submited = false;
			sendbutton.setStyle({'color': '#D6D6D7'});
			
			try {
				if (transport.responseJSON == null)
					throw "";
				
				if (transport.responseJSON.msg) {					
					$(form).up().fade({duration: 0.5,
												   queue: 'front'});
					
					if (typeof(msghtml) == 'undefined')
						msghtml = $('msg');

					msghtml.appear({duration: 0.5,
									   queue: 'end'});
					
					return;
				}
	
				if (transport.responseJSON.errors) {
					
					if (typeof(callbackerror) != 'undefined')
						callbackerror(transport.responseJSON.errors);
					else
						showErrors(transport.responseJSON.errors, errorhtml);
					return;
				}
				
				throw "";
			} catch (e) {
				showErrors(undefined, errorhtml);
			}
		},
		onFailure: function() {
			window.submited = false;
			sendbutton.setStyle({'color': '#D6D6D7'});

			showErrors(undefined, errorhtml);
		}	
	});
	
	return false;
}


function showErrors(errors, errorhtml) {
	if (typeof(errors) == 'undefined' || errors.length == 0)
		errors = ['Falha no sistema. Tente novamente.'];
		 
	var str = '';
	errors.each(function(s, i) {
		if (i > 0)
			str += ', ';
		str += s;
	});

	if (typeof(errorhtml) == 'undefined') {
		$$('.error span').first().innerHTML = str;
		$$('.error').first().show();
	} else {
		errorhtml.down('span').innerHTML = str;
		errorhtml.show();
	}
}


function resendActivation(event) {
	Event.stop(event);

	if (typeof(window.submited) != 'undefined' && window.submited == true)
		return false;
	
	if (typeof(sendbutton) == 'undefined')
		sendbutton = $$('a.enviar').first();

	var element = Event.element(event);
	sendbutton.setStyle({'color': '#CCCCCC'}); 

	parameters = $('loginform').serialize(true);
	
	//ARREBENTA nos IE's
	//parameters.do = 'resend_activation';
	
	parameters['do'] = 'resend_activation';
	
	new Ajax.Request('/login/', {
		method: 'post',
		parameters: parameters,
		onSuccess: function(transport) {
		    
			window.submited = false;
			sendbutton.setStyle({'color': '#FFFFFF'});
			
			try {
				if (transport.responseJSON == null)
					throw "";
				
				if (transport.responseJSON.msg) {
					$$('.error').first().hide();
					
					$$('#login_activate a').first().hide();
					$('reactivation_email_sent').show();
					return;
				}
	
				if (transport.responseJSON.errors) {
				    	
					alert(transport.responseJSON.errors);
					
					showErrors(transport.responseJSON.errors);
					return;
				}
				
				throw "";
			} catch (e) {
				showErrors(undefined);
			}
		},
		onFailure: function() {
			window.submited = false;
			sendbutton.setStyle({'color': '#FFFFFF'});

			showErrors(undefined);
		}	
	});
	
	return false;
	
	
}


function openCloseLostPassword(event) {
	Event.stop(event);
	var e = Event.element(event);
	var d = e.next();
	
	if(typeof(d._moving) == 'undefined')
		d._moving = false;
	if (d._moving == true)
		return;
	
	d._moving = true;
	
	if (d.visible())
		Effect.BlindUp(d, { duration: 0.3, 
			            afterFinish : function(effect) {
									      effect.element._moving = false;
										  effect.element.setStyle({minHeight: '0'});
		                              }
		              });
	else
		Effect.BlindDown(d, { duration: 0.3, 
				          afterFinish : function(effect) {
											effect.element._moving = false;
											effect.element.setStyle({minHeight: '30px'});
				                        }
		              });
}


function allowMessage(e, msg) {
    try {
        e.up().select('span.allowerror').first().remove();
    } catch (error) {
    }
    if (typeof (msg) != 'undefined')
        e.up().insert({bottom: '<p class="error spc1">' + msg + '</p>'}); 
}


function allowExtensions(e, extensions) {
    allowMessage(e);

    var v = e.value;
    if (v.length == 0)
        return;
    
    v = v.toLowerCase();
    ext = v.substring(v.lastIndexOf('.') + 1,v.length);

    var result = false;
    extensions.each(function(allow) {
        if (allow == ext)
            result = true;
    });

    if (! result) {
        var tmp = '';
        for (var i= 0; i < extensions.length; ++i) {
            if (i > 0) tmp += ', ';
            tmp += extensions[i];
        }

        e.value = '';
        allowMessage(e, 'Extensões permitidas: ' + tmp);
        return;
    }
}