var BOX = {
	dialogBox:	function()
	{
    		var hd, ft, bd, cn, s, c, d, colour;
    		var o = document.getElementsByTagName('div');

    		var box = [];
    		for (var i = 0, L = o.length; i < L; i++)
    		{
        		if(o[i].className.substr(0, 9) == 'DialogBox' && o[i].hasChildNodes())
        		{
            			box[box.length] = o[i];
        		}
    		}
    		for (var i = 0, L = box.length; i < L; i++)
    		{
        		d = box[i];
        		colour = d.className.substr(10);
        		cn = d.cloneNode(true);
        		cn.className = 's';

        		s = document.createElement('div');
        		s.className = 'dialog_' + (colour ? colour : 'black');

        		hd = document.createElement('div');
        		hd.className = 'hd';
        		c = document.createElement('div');
        		c.className = 'c';
        		hd.appendChild(c);

        		ft = document.createElement('div');
        		ft.className = 'ft';
        		c = document.createElement('div');
        		c.className = 'c';
        		ft.appendChild(c);

        		bd = document.createElement('div');
        		bd.className = 'bd';
        		c = document.createElement('div');
        		c.className = 'c';
        		c.appendChild(cn);
        		bd.appendChild(c);

        		s.appendChild(hd);
        		s.appendChild(bd);
        		s.appendChild(ft);

        		d.parentNode.replaceChild(s, d);
    		}
	},

	cornerBox: 	function()
	{
    		var c, d, cbox, ctop, xbottom, cb1, cb2, cb3, cb4;
    		var o = document.getElementsByTagName('div');

    		var box = [];
    		for (var i = 0, L = o.length; i < L; i++)
    		{
        		if(o[i].className.substr(0, 9) == 'cornerBox' && o[i].hasChildNodes())
        		{
            			box[box.length] = o[i];
        		}
    		}
    		for (var i = 0, L = box.length; i < L; i++)
    		{
        		d = o[i];
        		c = d.cloneNode(true);
        		c.className = 'cboxcontent';

        		cbox = document.createElement('div');
        		cbox.className = 'cbox';

        		ctop = document.createElement('span');
        		ctop.className = 'ctop';
        		cb1 = document.createElement('span');
        		cb1.className = 'cb1';
        		ctop.appendChild(cb1);
        		cb2 = document.createElement('span');
        		cb2.className = 'cb2';
        		ctop.appendChild(cb2);
        		cb3 = document.createElement('span');
        		cb3.className = 'cb3';
        		ctop.appendChild(cb3);
        		cb4 = document.createElement('span');
        		cb4.className = 'cb4';
        		ctop.appendChild(cb4);

        		cbottom = document.createElement('span');
        		cbottom.className = 'cbottom';
        		cb4 = document.createElement('span');
        		cb4.className = 'cb4';
        		cbottom.appendChild(cb4);
        		cb3 = document.createElement('span');
        		cb3.className = 'cb3';
        		cbottom.appendChild(cb3);
        		cb2 = document.createElement('span');
        		cb2.className = 'cb2';
        		cbottom.appendChild(cb2);
        		cb1 = document.createElement('span');
        		cb1.className = 'cb1';
        		cbottom.appendChild(cb1);

        		cbox.appendChild(ctop);
        		cbox.appendChild(c);
        		cbox.appendChild(cbottom);

        		d.parentNode.replaceChild(cbox, d);
    		}
	}
}


var LINK = {
	setLinkToPDF: 		function()
	{
    		var d, o = document.getElementsByTagName('a');
    		for (var i = 0, L = o.length; i < L; i++)
    		{
        		d = o[i];
        		if(d.href.substr(d.href.lastIndexOf('.'), 4) == '.pdf')
        		{
            			d.title = 'open document "' + d.href.substr(d.href.lastIndexOf('/')+1) + '"';
            			d.target = '_blank';
        		}
    		}
	},

	setExternalLink: 	function()
	{
    		var d, o = document.getElementsByTagName('a');
    		for (var i = 0, L = o.length; i < L; i++)
    		{
        		d = o[i];
        		if(d.target == '_blank')
        		{
				d.style.background = 'url(../images/link/external.gif) no-repeat right top';
				d.style.paddingRight = '10px';
        		}
    		}
	}
}


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 timer;
	
	var sendForm = function(id)
	{
		var check = $($$('input[type="checkbox"].check_button').last());

		if ((submitButtonEnable === false) || (check && check.checked === false)) return;

		submitButtonEnable 	= false;
		startWaiting();
		timer = setTimeout(requestTimeout, 50000);

		$(id).request({
			onSuccess: function(ret)
			{
				clearTimeout(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.truncate(20)) + '</span>' + ': ' + nn.value + '<br />'; 
                            });							
                        }
                    }
                }
				else
				{
					message = 'Error processing request';
				}
				
				if (errors)
				{
					Modalbox.outError(errors, false);
				}
				else
				{
					Modalbox.outMessage(message, true);
				}
			},
			
			onFailure: function()
			{
				clearTimeout(timer);
				submitButtonEnable = true;
				stopWaiting();	
				Modalbox.alert('Error. Server temporarily unavailable');
			}
		});
	}

    
	function requestTimeout()
	{
		submitButtonEnable = true;
		stopWaiting();
		Modalbox.alert('Error. Check your Internet connection');			
	}


	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
    }
})();