var DONATE = (function() 
{
    function initGroups()
    {
        $$('div.groups').each(function(n)
        {
            var inputs = n.getElementsBySelector('input[name="' + n.id + '[]"]');
            var i1 = inputs[inputs.length-1] || {type: null};
            var i2 = inputs[inputs.length-2] || {type: null};
                
            if (i1.type != i2.type)
            {
                i1.observe('focus', function(){i2.checked = 'checked'});        
            }
        });
    }

	function verify()
	{
		if (window.resultRequest != undefined)
		{
			var message, reply = window.resultRequest;
			var id = 'donate';
			if (reply)
			{
				if (reply.verify)
				{
					if (FORM.addIContact)
					{
						FORM.addIContact(id);
					}
					message = reply.message;
					$(id).disable();
				}
				else
				{
					if (reply.message)
					{
						message = reply.message;
					}
					else
					{
						var errors = '';
						var labels = $H({});
						$$('label[for]').each(function(label){labels[label.htmlFor] = label});

						$H(reply.errors).each(function(nn)
						{
							var node = $(nn.key);
							if (nn.key in labels)
							{
								var label = $(labels[nn.key]).addClassName('label_warning');
								if (node.getStyle('display') == 'block' && node.hasClassName('groups'))
								{
									node.getElementsBySelector('input[name="' + node.id + '[]"]').each(function(n)
									{
										$(n).observe('focus', function()
										{
											if (!isNaN(parseInt(label.htmlFor.slice(-1))))
											{
												var id = label.htmlFor.slice(0, -1).toString();
												$R(0, 10).each(function(index)
												{
													if (labels[id + index])
													{
														$(labels[id + index]).removeClassName('label_warning');
													}
												});
											}
											else
											{
												label.removeClassName('label_warning');
											}
											node.stopObserving('focus', arguments.callee)
										})
									})
								}
								else
								{
									node.observe('focus', function()
									{
										label.removeClassName('label_warning');
										node.stopObserving('focus', arguments.callee) 
									});
								}
							}
							
							switch (node.type)
							{
								case 'text': case 'password':
									node.addClassName('warning').observe('focus', function()
									{
										node.removeClassName('warning').stopObserving('focus', arguments.callee) 
									});
									break;
							}
							
							errors +=  '<span class="MB_errors_left">' + ((typeof(label) == 'undefined') ? nn.key : labels[nn.key].lastChild.data.truncate(18)) + '</span>' + ': ' + nn.value + '<br />'; 
						});
					}
				}
			}
			else
			{
				message = 'Error processing request';
			}
				
			if (errors)
			{
				Modalbox.outError(errors, false);
			}
			else
			{
				Modalbox.outMessage(message, true);
			}			
		}		
	}

    function init()
    {
		initGroups();
		verify();
		//FORM.activateSubmitButton('submit', 'donate');

		$('submit').observe('click', function()
		{
			new Ajax.Request('index.php?c=donate.logging', 
			{
				parameters : {
					request : $('donate').serialize()
				}
			});
		});
	}
    
    return {
		init 		: init
    }	
})();


Event.observe(window, 'load', DONATE.init);
