// files object
var Files = {

	Add : function() {

		// calculate amount
		if(!this.amount) { this.amount = 1; }
		else { this.amount += 1; }

		// create node
		var fileHtml = Builder.node('div', { className: "order_fileholder", id: "fileholder" + this.amount, style: "display: none;" }, [ 
			Builder.node('input', { className: "fileInput", type: "file", name: "file" + this.amount })
		]);
		$('order_files_ajaxed').appendChild(fileHtml);
		Effect.Appear($('fileholder' + this.amount));
	}
}

// Form animation and status
var OrderDOM = {

	// FORM submission
	'#order_send_button' : function(button) {

		button.onclick = function(click) {

			// validate fields
			if($F('company_name') && $F('person_name') && $F('email') && $F('phone')) {

				// validate email
				var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				//var emailRegEx = ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}+$;
				var emailUser = $F('email');

				if(emailUser.match(emailRegEx)) {

					// Check if we got files attached	// 2010-06-01
					if($F($('order_files').select('input[name="file0"]')[0])) $('order_form_form').submit();
					else if(confirm('Nie załączyłeś żadnych faktur. Nie będziemy mieli możliwości dokonania analizy. Czy na pewno chcesz wysłać zapytanie bez faktur?')) $('order_form_form').submit();
					else return false;
				}
				else {

					// TODO: show neat error message here
					alert('Proszę podaj poprawny adres e-mail');
				}
			}
			else {

				// TODO: show neat error message here
				alert('Proszę podaj wszelkie dane kontaktowe');
			}

		}
	},

	// BILL
	'#company_bill' : function(input) {

		input.onchange = function(focus) {

			if($F(input) != "0") {

				if($('order_form_wrapper').style.display == 'none') {

					Effect.BlindDown('order_form_wrapper', { afterFinish: function() { Effect.ScrollTo('slide_here'); }});
				}
			}
			else {
				if($('order_form_wrapper').style.display != 'none') {

					Effect.ScrollTo('main', { afterFinish: function() { Effect.BlindUp('order_form_wrapper', { duration: 0.5 }); } });
				}
			}
		}
	},

	// FILE INPUTS
	'#add_file' : function(input) {


		input.onclick = function(change) {

			Files.Add();
		}
	
	},


	// global startup
	'body' : function(body) {

		if($F('company_bill') != "0") {

			$('order_form_wrapper').style.display = 'block';
		}
	}
}
Behaviour.register(OrderDOM);

