
/**
 * FIELDS
 */
var validateFields = new Hash();
validateFields.set('welcome', new Array('application_as'));
validateFields.set('personal', new Array('firstname', 'lastname', 'address', 'zip', 'city', 'email', 'datejoining_day', 'datejoining_month', 'datejoining_year'));
validateFields.set('school', new Array('type_1', 'from_month_1', 'from_year_1', 'to_month_1', 'to_year_1', 'qualification_1', 'grade_1'));
validateFields.set('study', new Array());
validateFields.set('internship', new Array());
validateFields.set('job', new Array());
validateFields.set('foreign', new Array());
validateFields.set('other', new Array('mothertongue', 'foreign_language_oral_1', 'foreign_language_oral_2', 'foreign_language_oral_3', 'foreign_language_written_1', 'foreign_language_written_2', 'foreign_language_written_3', 'itprogram_use_1', 'itprogram_use_2', 'itprogram_use_3', 'itprogram_program_1', 'itprogram_program_2', 'itprogram_program_3'));
validateFields.set('finish', new Array());
/**
 * GENERAL FUNCTIONS
 */
function isEmpty(value) {
	if (value == null || value.blank()) {
		return true;
	}
	return false;
}

function isEmail (value) {
	if (value.strip().match("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$")) {
		return true;
	}
	return false;
}

function isInteger (value) {
	if (value.strip().blank() || value.strip().match("^[0-9]*$")) {
		return true;
	}
	return false;
}

function isNumeric (value) {
	if (value.strip().blank() || value.strip().match("^([0-9]*)(,|\.|)([0-9]+)$")) {
		return true;
	}
	return false;
}

function isPhoneNr(value) {
	if (value.stip().blank() || value.strip().match("^[+-/0-9]*$")) {
		return true;
	}
	return false;
}

function showJSErrorWindow(errors) {
	// hide all errors
	$$('#jsWindow-errors ul.errors li').each( function (item) {
		item.hide();
	});

	//unhide errors contained in errors-array
	errors.each(function (item) {
		$('jsWindow-errors').getElementsByClassName('error_'+item).first().show();
	});

	popup = $('jsPopup');
	popup.getElementsBySelector(".wrapper .content").first().innerHTML = $('jsWindow-errors').innerHTML;

	popupDimensions = popup.getDimensions();
	popupPosition = Position.cumulativeOffset(popup);
	topPosition = ((document.viewport.getHeight() - popupDimensions.height) / 2);
	popup.style.top = ( (topPosition < 0) ? '0' : topPosition ) + 'px';
	popup.style.left = ((document.viewport.getWidth() - popupDimensions.width) / 2) + 'px';

	popup.getElementsByClassName('jsWindow-button').first().observe('click', function(event) {
		new Effect.Fade("jsPopup");
	});

	//popup.show();
	new Effect.Appear(popup);
}

/**
 * NAVIGATION
 */
function application_navigate(event) {
	//console.log("Navigation clicked: " + this.id);

	var activeItem;
	var activeIndex = 0;
	var clickedItem = $(this);
	var clickedIndex = 0;

	$(this).up('ul').childElements().each(function (item, index) {
		if (item.hasClassName('active')) {
			activeItem = item;
			activeIndex = index;
		}
		if (item.id == clickedItem.id) {
			clickedIndex = index;
		}
	});

	var activeFormId = activeItem.firstDescendant().readAttribute('rel');
	var nextFormId = clickedItem.firstDescendant().readAttribute('rel');

	/*console.log("Navigation: activeIndex=" + activeIndex+ " clickedIndex=" + clickedIndex);
	console.log("Navigation: activeItem: " + activeItem);
	console.log("Navigation: clickedItem: " + clickedItem);
	console.log("Navigation: current form: " + activeFormId);
	console.log("Navigation: next form: " + nextFormId);*/

	//do nothing if activeItem and clickedItem are the same
	if (activeIndex == clickedIndex) {
		//console.log("Navigation: active and clicked Items are the same");
		event.stop();
		return false;
	}

	if (activeIndex < clickedIndex) {
		//validate active form and forms between
		var children = $(this).up('ul').childElements();
		for (i = activeIndex; i < clickedIndex; i++) {
			//console.log(children[i]);
			if (! validateForm( event, children[i].id.substr(children[i].id.lastIndexOf('_')+1))) {
				nextFormId = children[i].firstDescendant().readAttribute('rel');
				clickedItem = children[i];
				break;
			}
		}
	}

	//call form-validate-function
	if ((activeIndex > clickedIndex) || validateForm( event, activeItem.id.substr(activeItem.id.lastIndexOf('_')+1))) {
		Effect.Fade(activeFormId, {
			duration: 0.3,
			afterFinish: function() {
				activeItem.removeClassName('active');
				clickedItem.addClassName('active');
				Effect.Appear(nextFormId, {
					duration: 0.3
				});
			}
		});
	}
}

/*
function application_validate(event) {
	validateFields.each (function (form) {
		if (! validateForm(event, form.key)) {
			console.log(form.key);
		} 
	});
	event.stop();
}*/

/**
 * FORM-VALIDATION-FUNCTION
 */
function validateForm(event, form) {
	var errors = new Array();
	var prefix = form + '_';

	validateFields.get(form).each(function (item) {
		if (! eval('check_' + (prefix+item).dasherize().camelize() + '();')) {
				errors.push(prefix+item);
		}
	});

	if (errors.length > 0) {
		showJSErrorWindow(errors);
		event.stop();
		return false;
	} else {
		return true;
	}
}


/* Welcome */
function check_welcomeApplicationAs() {
	return ! isEmpty($F('welcome_application_as'));
}


/* Personal */
function check_personalFirstname() {
	return ! isEmpty($F('personal_firstname'));
}
function check_personalLastname() {
	return ! isEmpty($F('personal_lastname'));
}
function check_personalAddress() {
	return ! isEmpty($F('personal_address'));
}
function check_personalZip() {
	return ! isEmpty($F('personal_zip'));
}
function check_personalCity() {
	return ! isEmpty($F('personal_city'));
}
function check_personalEmail() {
	return (! isEmpty($F('personal_email'))) && (isEmail($F('personal_email')));
}
function check_personalDatejoiningDay() {
	return (! isEmpty($F('personal_datejoining_day'))) && (isInteger($F('personal_datejoining_day')));
}
function check_personalDatejoiningMonth() {
	return (! isEmpty($F('personal_datejoining_month'))) && (isInteger($F('personal_datejoining_month')));
}
function check_personalDatejoiningYear() {
	return (! isEmpty($F('personal_datejoining_year'))) && (isInteger($F('personal_datejoining_year')));
}

/* School */
function check_schoolType1() {
	return ! isEmpty($F('school_type_1'));
}
function check_schoolFromMonth1() {
	return (! isEmpty($F('school_from_month_1'))) && (isInteger($F('school_from_month_1')));
}
function check_schoolFromYear1() {
	return (! isEmpty($F('school_from_year_1'))) && (isInteger($F('school_from_year_1')));
}
function check_schoolToMonth1() {
	return (! isEmpty($F('school_to_month_1'))) && (isInteger($F('school_to_month_1')));
}
function check_schoolToYear1() {
	return (! isEmpty($F('school_to_year_1'))) && (isInteger($F('school_to_year_1')));
}
function check_schoolQualification1() {
	return ! isEmpty($F('school_qualification_1'));
}
function check_schoolGrade1() {
	return (! isEmpty($F('school_grade_1'))) && (isNumeric($F('school_grade_1')));
}

/* Study */

/* Internship */

/* Job */

/* Foreign */

/* Other */
function check_otherMothertongue() {
	return ! isEmpty($F('other_mothertongue'));
}

function check_otherForeignLanguageOral1() {
	return ! ( (! isEmpty($F('other_foreign_language_1')))
				&& isEmpty($F('other_foreign_language_oral_1_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_oral_1_fortgeschritten'))
				&& isEmpty($F('other_foreign_language_oral_1_verhandlungssicher')));
}
function check_otherForeignLanguageOral2() {
	return ! ( (! isEmpty($F('other_foreign_language_2')))
				&& isEmpty($F('other_foreign_language_oral_2_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_oral_2_fortgeschritten'))
				&& isEmpty($F('other_foreign_language_oral_2_verhandlungssicher')));
}
function check_otherForeignLanguageOral3() {
	return ! ( (! isEmpty($F('other_foreign_language_3')))
				&& isEmpty($F('other_foreign_language_oral_3_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_oral_3_fortgeschritten'))
				&& isEmpty($F('other_foreign_language_oral_3_verhandlungssicher')));
}
function check_otherForeignLanguageWritten1() {
	return ! ( (! isEmpty($F('other_foreign_language_1')))
				&& isEmpty($F('other_foreign_language_written_1_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_written_1_sicher')));
}
function check_otherForeignLanguageWritten2() {
	return ! ( (! isEmpty($F('other_foreign_language_2')))
				&& isEmpty($F('other_foreign_language_written_2_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_written_2_sicher')));
}
function check_otherForeignLanguageWritten3() {
	return ! ( (! isEmpty($F('other_foreign_language_3')))
				&& isEmpty($F('other_foreign_language_written_3_grundkenntnisse'))
				&& isEmpty($F('other_foreign_language_written_3_sicher')));
}

function check_otherItprogramUse1() {
	return ! ( (! isEmpty($F('other_itprogram_1')))
				&& isEmpty($F('other_itprogram_use_1_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_use_1_fortgeschritten')));
}
function check_otherItprogramUse2() {
	return ! ( (! isEmpty($F('other_itprogram_2')))
				&& isEmpty($F('other_itprogram_use_2_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_use_2_fortgeschritten')));
}
function check_otherItprogramUse3() {
	return ! ( (! isEmpty($F('other_itprogram_3')))
				&& isEmpty($F('other_itprogram_use_3_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_use_3_fortgeschritten')));
}
function check_otherItprogramProgram1() {
	return ! ( (! isEmpty($F('other_itprogram_1')))
				&& isEmpty($F('other_itprogram_program_1_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_program_1_fortgeschritten')));
}
function check_otherItprogramProgram2() {
	return ! ( (! isEmpty($F('other_itprogram_2')))
				&& isEmpty($F('other_itprogram_program_2_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_program_2_fortgeschritten')));
}
function check_otherItprogramProgram3() {
	return ! ( (! isEmpty($F('other_itprogram_3')))
				&& isEmpty($F('other_itprogram_program_3_grundkenntnisse'))
				&& isEmpty($F('other_itprogram_program_3_fortgeschritten')));
}

/* Finish */

Event.observe(window, 'load', function (event) {

	//Navigation
	$$('#intedis_application_menu li').each( function (element) {
		element.observe('mouseover', function() {
			this.addClassName('hover');
		}.bindAsEventListener(element));

		element.observe('mouseout', function() {
			this.removeClassName('hover');
		}.bindAsEventListener(element));


		element.observe('click', application_navigate.bindAsEventListener(element));
	});

	//Remove default-classes if fields are preset
	if ($('welcome_application_as').present()) {
		$('welcome_application_as').removeClassName('input-required');
	}

	// Add validationhandlers
	validateFields.each(function (pair) {
		pair.value.each (function (item) {
			var prefix = pair.key + '_';

			if ($(prefix + item)) {
				$(prefix + item).observe('blur', function() {
					if (eval('check_' + (prefix+item).dasherize().camelize() + '();')) {
						$(prefix + item).removeClassName('input-required')
					} else {
						$(prefix + item).addClassName('input-required');
					}
				});
		}
		});
	});
	
	//$('applicationForm').observe('submit', application_validate);
	$('applicationForm').observe('submit', function(event) {
		var element = Event.element(event);
		if (! $('intedis_application_menu_other').hasClassName('active')) {
			event.stop();
		}
	});
	
});


