// JavaScript Document
var currentpage;
var browser = Browser.Engine.name + Browser.Engine.version;

function domReady() {
 var scrollBox1 = new MooScroll({selector:'#box1'});
 
 if(currentpage == 'CONTACT') {
  hideErrors();
 }
 
 if(currentpage == 'INDEX') {
  if(browser != 'trident4') {
   effect();
  }
 }
}

function effect() {
	var barTop = $('bar_top').setStyles({
					'marginTop':-300,
					'background-image':'url(./images/top_balk.png)'
				});
	
	var titleImage = $('title').setStyles({
					'opacity': 0
				});
	
	var barLeft = $('barLeft').setStyles({
					'opacity': 0
				});
	
	new Fx.Morph(barTop, {duration:1500, transition: Fx.Transitions.Expo.easeInOut, onComplete: function() {
			new Fx.Morph(titleImage, {duration:2000}).start({opacity:1}), 
			new Fx.Morph(barLeft, {duration:1000}).start({opacity:1});
	}}).start({'marginTop':0}); 	
}

function setPage(page) {
	currentpage = page;
}

function setErrorField(field, error) {
	//$(error).setOpacity(1);	
	$(error).fade('in');
	$(field).highlight('#ff0000');
	//$(field).setStyles({'background': '#ffb7d5', 'border': '2px solid #5a5959'});
}

function setNoErrorField(field, error) {
	//$(error).setOpacity(0);
	$(error).fade('out');
	$(field).setStyles({'background': 'white', 'border': '1px solid #5a5959'});	
}

function sendForm() {
	var errorCount = 0;
	var inputName = getInputName();
	var inputEmail = getInputEmail();
	var inputQuestion = getInputQuestion();

	// Validate name field
	if (isEmpty(inputName)) {
		setErrorField('name', 'error_name');
		errorCount++;
	} else {
		setNoErrorField('name', 'error_name');
	}

	// Validate e-mail field
	if (isEmpty(inputEmail) || !validEmail(inputEmail)) {
		setErrorField('email', 'error_email');
		errorCount++;
	} else {
		setNoErrorField('email', 'error_email');
	}

	// Validate question field
	if (isEmpty(inputQuestion)) {
		setErrorField('message', 'error_question');
		errorCount++;
	} else {
		setNoErrorField('message', 'error_question');
	}

	// Check if there are no errors
	if(errorCount == 0) {
		$('contact').submit();
	}
}

function hideErrors() {
	$('error_name').setOpacity(0);
	$('error_email').setOpacity(0);
	$('error_question').setOpacity(0);	
}

function getInputName() {
	return $('contact').name.value;
}

function getInputEmail() {
	return $('contact').email.value;
}

function getInputQuestion() {
	return $('contact').message.value;
}

// Set the domReady handler
window.addEvent('domready', domReady);