$(function() {
	/*
	number of fieldsets
	*/
	var fieldsetCount = $('#formElem').children().length;

	/*
	current position of fieldset / navigation link
	*/
	var current 	= 1;

	/*
	sum and save the widths of each one of the fieldsets
	set the final sum as the total width of the steps element
	*/
	var stepsWidth	= 0;
    var widths 		= new Array();
	$('#steps .step').each(function(i){
        var $step 		= $(this);
		widths[i]  		= stepsWidth;
        stepsWidth	 	+= $step.width();
    });
	$('#steps').width(stepsWidth);

	/*
	to avoid problems in IE, focus the first input of the form
	*/
	//$('#formElem').children(':first').find(':input:first').focus();	

	/*
	show the navigation bar
	*/
	$('#navigation').show();
	$("#application_comment").attr("tabindex", "-1");

	/*
	when clicking on a navigation link
	the form slides to the corresponding fieldset
	*/
    $('#navigation a').bind('click',function(e){
		var $this	= $(this);
		var prev	= current;
		/*
		we store the position of the link
		in the current variable
		*/
		if ($this.parent().index() == 0 & current > 1){
			current = current - 1;
			if (current < fieldsetCount){
				$("a#nav_next").text('NÄSTA');
			}
		}
		if ($this.parent().index() == 1){
			if (current < fieldsetCount){
				current = current + 1;
			}
			else{
				validateSteps();
				if($('#formElem').data('errors')){
					return false;
				}
				else{
					$('#formElem').submit();
				}
			}
			if (current == fieldsetCount){
				$this.text("SKICKA");
			} 
		}
		 $('#navigation_step').text("Steg "+current+"/3");

		/*
		animate / slide to the next or to the corresponding
		fieldset. The order of the links in the navigation
		is the order of the fieldsets.
		Also, after sliding, we trigger the focus on the first
		input element of the new fieldset
		If we clicked on the last link (confirmation), then we validate
		all the fieldsets, otherwise we validate the previous one
		before the form slided
		*/
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
		});
        e.preventDefault();
    });

	/*
	validates errors on all the fieldsets
	records if the form has errors in $('#formElem').data()
	*/
	function validateSteps(){
		var FormErrors = false;
		var ErrorPage = 0;
		
		// Sida 3
		if(!regExpTest(jQuery.trim($("#application_student").val()), RE_NAME)){
			$("#application_student").css('background-color','#fcc');
			FormErrors = true;
			ErrorPage = 3;
		}
		else{
			$("#application_student").css('background-color','#fff');
		}
		
		// Sida 2
		if(!regExpTest(jQuery.trim($("#application_contact").val()), RE_NAME)){
			$("#application_contact").css('background-color','#fcc');
			FormErrors = true;
			ErrorPage = 2;
		}
		else{
			$("#application_contact").css('background-color','#fff');
		}
		if(!regExpTest(jQuery.trim($("#application_phone").val()), RE_PHONE)){
			$("#application_phone").css('background-color','#fcc');
			FormErrors = true;
			ErrorPage = 2;
		}
		else{
			$("#application_phone").css('background-color','#fff');
		}
		if(!regExpTest(jQuery.trim($("#application_mail").val()), RE_EMAIL)){
			$("#application_mail").css('background-color','#fcc');
			FormErrors = true;
			ErrorPage = 2;
		}
		else{
			$("#application_mail").css('background-color','#fff');
		}
		
		// Sida 1
		$("#application_city option:selected").each(function () {
			if($(this).val() <= 0){
				$("#application_city").css('background-color','#fcc');
				FormErrors = true;
				ErrorPage = 1;
			}
			else{
				$("#application_city").css('background-color','#fff');
			}
		});
		$("#application_age option:selected").each(function () {
			if($(this).val() <= 0){
				$("#application_age").css('background-color','#fcc');
				FormErrors = true;
				ErrorPage = 1;
			}
			else{
				$("#application_age").css('background-color','#fff');
			}
		});
		$("#application_product option:selected").each(function () {
			if($(this).val() <= 0){
				$("#application_product").css('background-color','#fcc');
				FormErrors = true;
				ErrorPage = 1;
			}
			else{
				$("#application_product").css('background-color','#fff');
			}
		});
		
		$('#formElem').data('errors',FormErrors);
		
		current = ErrorPage;
		$('#navigation_step').text("Steg "+current+"/3");
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
		});

	}
});

function submitApplication() {
	var FormErrors = false;
	
	if(!regExpTest(jQuery.trim($("#application_student").val()), RE_NAME)){
		$("#application_student").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_student").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#application_contact").val()), RE_NAME)){
		$("#application_contact").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_contact").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#application_phone").val()), RE_PHONE)){
		$("#application_phone").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_phone").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#application_mail").val()), RE_EMAIL)){
		$("#application_mail").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_mail").css('background-color','#fff');
	}
	$("#application_city option:selected").each(function () {
		if($(this).val() <= 0){
			$("#application_city").css('background-color','#fcc');
			FormErrors = true;
		}
		else{
			$("#application_city").css('background-color','#fff');
		}
	});
	$("#application_age option:selected").each(function () {
		if($(this).val() <= 0){
			$("#application_age").css('background-color','#fcc');
			FormErrors = true;
		}
		else{
			$("#application_age").css('background-color','#fff');
		}
	});
	$("#application_product option:selected").each(function () {
		if($(this).val() <= 0){
			$("#application_product").css('background-color','#fcc');
			FormErrors = true;
		}
		else{
			$("#application_product").css('background-color','#fff');
		}
	});
	
	if (FormErrors) {
		document.getElementById('subpage_part_error').innerHTML = "Vänligen korrigera de markerade fälten ovan";
	}
	else {
		document.getElementById('subpage_part_error').innerHTML = "";
		document.application_form.submit();
	}
}

function submitContactForm() {
	var FormErrors = false;
	
	if(!regExpTest(jQuery.trim($("#application_contact").val()), RE_NAME)){
		$("#application_contact").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_contact").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#application_phone").val()), RE_PHONE)){
		$("#application_phone").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_phone").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#application_mail").val()), RE_EMAIL)){
		$("#application_mail").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#application_mail").css('background-color','#fff');
	}
	
	if (FormErrors) {
		document.getElementById('subpage_part_error').innerHTML = "Vänligen korrigera de markerade fälten ovan";
	}
	else {
		document.getElementById('subpage_part_error').innerHTML = "";
		document.application_form.submit();
	}
}

function submitRecruitForm() {
	var FormErrors = false;
	
	if(!regExpTest(jQuery.trim($("#recruit_first_name").val()), RE_NAME)){
		$("#recruit_first_name").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#recruit_first_name").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#recruit_last_name").val()), RE_NAME)){
		$("#recruit_last_name").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#recruit_last_name").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#recruit_phone").val()), RE_PHONE)){
		$("#recruit_phone").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#recruit_phone").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#recruit_email").val()), RE_EMAIL)){
		$("#recruit_email").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#recruit_email").css('background-color','#fff');
	}
	$("#recruit_city option:selected").each(function () {
		if($(this).val() <= 0){
			$("#recruit_city").css('background-color','#fcc');
			FormErrors = true;
		}
		else{
			$("#recruit_city").css('background-color','#fff');
		}
	});
	$("#recruit_tasks option:selected").each(function () {
		if($(this).val() <= 0){
			$("#recruit_tasks").css('background-color','#fcc');
			FormErrors = true;
		}
		else{
			$("#recruit_tasks").css('background-color','#fff');
		}
	});
	if(!regExpTest(jQuery.trim($("#recruit_cv").val()), RE_FILENAME)){
		$("#fakefilefield0").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#fakefilefield0").css('background-color','#fff');
	}
	if(!regExpTest(jQuery.trim($("#recruit_cover_letter").val()), RE_FILENAME)){
		$("#fakefilefield1").css('background-color','#fcc');
		FormErrors = true;
	}
	else{
		$("#fakefilefield1").css('background-color','#fff');
	}
	
	if (FormErrors) {
		document.getElementById('subpage_part_error').innerHTML = "Vänligen korrigera de markerade fälten ovan";
	}
	else {
		document.getElementById('subpage_part_error').innerHTML = "";
		document.recruit_form.submit();
	}
}


var W3CDOM = (document.createElement && document.getElementsByTagName);
function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('div');
	image.className='fakefilebutton';
	image.innerHTML = "Bläddra...";
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	var idNum = 0;
	for (var i=0;i<x.length;i++) {
		if (x[i].parentNode.className == 'fakefile') {
			x[i].className = 'fakefilefield';
			x[i].id = 'fakefilefield'+idNum;
			idNum++;
		}
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value.replace(/^.*\\/, '');
		}
	}
}
