if (window.Modalbox)
{
	if (!Modalbox.outWarning) 
	{
		Modalbox.outWarning = function(msg, redirect)
		{
			var param = {
				title			: ' ', //'Warning',
				width			: 380,
				overlayClose	: false
			}
			
			if (redirect)
			{
				param.afterHide = function()
				{
					setTimeout("location.assign('index.php')", 2000);
				}
			}
			Modalbox.show('<div class="MB_contentFrame">' + msg + '<p class="MB_button"><input type="button" onclick="Modalbox.hide()" value="&nbsp;OK&nbsp;" /></p></div>', param);
		}
	}
	
	if (!Modalbox.outMessage)
	{
		Modalbox.outMessage = function(msg, redirect)
		{
			Modalbox.outWarning('<p class="MB_messages">' + msg + '</p>', redirect);	
		};
	}

	if (!Modalbox.outError)
	{
		Modalbox.outError = function(msg, redirect)
		{
			Modalbox.outWarning('<p class="MB_errors">' + msg + '</p>', redirect);	
		};
	}
}


var FORM = (function() 
{	
	var submitButton;
	var submitButtonEnable;
	var request;
	
	var sendForm = function(id)
	{
		var check = $($$('input[type="checkbox"].check_button').last());

		if ((submitButtonEnable === false) || (check && check.checked === false)) return false;

		submitButtonEnable 	= false;
		startWaiting();

        var params = {
			onSuccess: function(ret)
			{
				clearTimeout(request.timer);
				stopWaiting();	
				
                var message, reply = ret.responseJS;
                if (reply)
                {
                    if (reply.verify)
                    {
						if (FORM.addIContact)
						{
							FORM.addIContact(id);
						}
                        message = reply.message;
                        $(id).disable();
                    }
                    else
                    {
						submitButtonEnable = true;
                        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.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) + '</span>' + ': ' + nn.value + '<br />'; 
                            });							
                        }
                    }
                }
				else
				{
					message = 'Error processing request';
				}
				
				if (errors)
				{
					Modalbox.outError(errors, false);
				}
				else
				{
					Modalbox.outMessage(message, true);
				}
			},
			
			onFailure: function()
			{
				clearTimeout(request.timer);
				submitButtonEnable = true;
				stopWaiting();	
				Modalbox.alert('Error. Server temporarily unavailable');
			}
		};

        //params.method = (Prototype.Browser.IE) ? 'get' : 'post';

		if (window.DONATE == undefined)
		{
			request = $(id).request(params);
			request.timer = setTimeout(
		  	function()
		  	{
		    	try
		    	{
                	request.transport.abort();
            	}
            	catch(e){}
            	submitButtonEnable = true;
		    	stopWaiting();
            	Modalbox.alert('Error. Check your Internet connection');			
		  	}, 
          	50000
			);			
		}
		else
		{
			return true;
		}
	}

	function startWaiting()
	{
		if (!Prototype.Browser.IE && !Prototype.Browser.Opera) 
		{
			submitButton.startWaiting();
		}		
	}
	
	function stopWaiting()
	{
		if (!Prototype.Browser.IE && !Prototype.Browser.Opera)
		{
			submitButton.stopWaiting();	
		}		
	}
	
    function activateSubmitButton(id, formID)
    {
        if ($(id) && $(formID))
        {
			submitButton 			= $(id);
			submitButtonEnable 		= true;
			submitButton.observe('click', FORM.sendForm.bind(FORM, formID));
        }
    }
	
    return {
        activateSubmitButton 	: activateSubmitButton,
        sendForm 				: sendForm
    }
})();