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

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

function isEmpty(value) {
	if (value == null || value.blank()) {
		return true;
	}
	return false;
}

function checkContactViaTelephone(event) {
	//manage hidden-field
	$('contact_via_telephone').value = $F('contact_via_telephone_checkbox') ? $('contact_via_telephone_value_on').innerHTML : $('contact_via_telephone_value_off').innerHTML;

	//manage required-fields
	if ($F('contact_via_telephone_checkbox')) {
		phone = $F('contact_telephone');
		mobile = $F('contact_mobile');
		if ( (isEmpty(phone) || (! isPhoneNr(phone))) &&
			 (isEmpty(mobile) || (! isPhoneNr(mobile))) ) {
			$('contact_telephone').addClassName('input-required');
			$('contact_mobile').addClassName('input-required');
			return false;
		} else {
			$('contact_telephone').removeClassName('input-required');
			$('contact_mobile').removeClassName('input-required');
			return true;
		}
	} else {
		$('contact_telephone').removeClassName('input-required');
		$('contact_mobile').removeClassName('input-required');
		return true;
	}
}

function checkSendInformation(event) {
	//manage hidden-field
	$('send_information').value = $F('send_information_checkbox') ? $('send_information_value_on').innerHTML : $('send_information_value_off').innerHTML;

	formFields = ['information_address', 'information_city'];
	if ($F('send_information_checkbox')) {
		formFields.each(function (name) {
			if (isEmpty($F(name))) {
				$(name).addClassName('input-required');
			} else {
				$(name).removeClassName('input-required');
			}
		});

		if (isEmpty($F('information_zip'))) {
			$('information_zip').addClassName('input-required');
		} else {
			$('information_zip').removeClassName('input-required');
		}
	} else {
		$('information_address').removeClassName('input-required');
		$('information_zip').removeClassName('input-required');
		$('information_city').removeClassName('input-required');
	}
}

Event.observe(window, "load", function() {
	document.getElementsByClassName('input-required').each(function (input) {
		//CASE: not null
		if (input.hasClassName('input-required-notNull')) {
			input.observe('blur', function(event) {
				if ( isEmpty($F(this)) ) {
					this.addClassName('input-required');
				} else {
					this.removeClassName('input-required');
				}
			}.bindAsEventListener(input));
		}

		//CASE: email required
		if (input.hasClassName('input-required-email')) {
			input.observe('blur', function(event) {
				if ( isEmpty($F(this)) || (! isEmail($F(this)))) {
					this.addClassName('input-required');
				} else {
					this.removeClassName('input-required');
				}
			}.bindAsEventListener(input));
		}

		// CASE contact by telephone
		$('contact_via_telephone_checkbox').observe("click", checkContactViaTelephone);
		$('contact_telephone').observe("blur", checkContactViaTelephone);
		$('contact_mobile').observe("blur", checkContactViaTelephone);

		// CASE send information
		$('send_information_checkbox').observe("click", checkSendInformation);
		$('information_address').observe("blur", checkSendInformation);
		$('information_zip').observe("blur", checkSendInformation);
		$('information_city').observe("blur", checkSendInformation);
	});

	$('contactform').observe('submit', function (event) {
		errors = $('jsWindow-errors');
		hasError = false;
		//check name
		if (isEmpty($F('personal_name'))) {
			errors.getElementsByClassName('error_personal_name').first().show();
			hasError = true;
		} else {
			errors.getElementsByClassName('error_personal_name').first().hide();
		}

		//check email
		if ( isEmpty($F('personal_email')) || (! isEmail($F('personal_email')))) {
			errors.getElementsByClassName('error_personal_email').first().show();
			hasError = true;
		} else {
			errors.getElementsByClassName('error_personal_email').first().hide();
		}

		//check contact_via_telephone_checkbox
		if (! checkContactViaTelephone()) {
			errors.getElementsByClassName('error_contact_via_telephone').first().show();
			hasError = true;
		} else {
			errors.getElementsByClassName('error_contact_via_telephone').first().hide();
		}

		//check Send Information
		if ($F('send_information_checkbox')) {
			//street
			if (isEmpty($F('information_address'))) {
				errors.getElementsByClassName('error_information_address').first().show();
				hasError = true;
			} else {
				errors.getElementsByClassName('error_information_address').first().hide();
			}

			//zip
			if (isEmpty($F('information_zip'))) {
				errors.getElementsByClassName('error_information_zip').first().show();
				hasError = true;
			} else {
				errors.getElementsByClassName('error_information_zip').first().hide();
			}

			//city
			if (isEmpty($F('information_city'))) {
				errors.getElementsByClassName('error_information_city').first().show();
				hasError = true;
			} else {
				errors.getElementsByClassName('error_information_city').first().hide();
			}
		}

		if (hasError) {
			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);
			event.stop();
			return false;
		} else {
			return true;
		}
	});
});