﻿$(document).ready(function() {
	init_sidebar();
	init_contactValidation();
});

function init_sidebar() {
	// initialize animated sidebar
	var sb = $('div.content_navi_container');
	sb.hide();
	if(sb.children('ul').length >= 1) {		
		// color variation of lines
		var animateIt = true;
		sb.find('ul li').each(function(i, obj) {
			$(obj).find('div.linie').addClass('linie'+ i);
			
			if($(obj).hasClass('active')) {
				animateIt = false;
				$(obj).find('a').attr('id', 'activeLink');
			}
		});
		
		if(animateIt) {
			var effect;
			if(is_ie6()) {
				sb.show();
			} else {
				effect = {
				'width' : 'show',
				'opacity' : 'show'
				};
				sb.animate(effect, 1000);
			}
		} else {
			sb.show();
		}
		
		// hover animation
		var oldWidth = '7px';
		
		sb.find('ul li a').hover(function() {
			var newWidth = this.offsetWidth + 3;
			$(this).parent('li').find('div.linie')
				.animate({
					'width' : newWidth
				}, 400);
		}, function() {
			if($(this).parent('li').hasClass('active') == false) {
				$(this).parent('li').find('div.linie').stop()
					.animate({
						'width' : oldWidth
					});
			}
		});
		
		var activeLink = document.getElementById('activeLink');
		$(activeLink).parent().find('div.linie').css('width', activeLink.offsetWidth + 3);		
	}
}

function init_contactValidation() {
	$('table.contact input.submit').click(function() {
		$('label, input, textarea, select').removeClass('text_error').removeClass('inp_error');
		
		var anrede 	= $('select.slct_anrede').val();
		var name 	= $('input.txt_name').val();
		var vorname	= $('input.txt_vorname').val();
		var strasse	= $('input.txt_strasse').val();
		var hausnr	= $('input.txt_hausnr').val();
		var plz 	= $('input.txt_plz').val();
		var ort		= $('input.txt_ort').val();
		var tel		= $('input.txt_tel').val();
		var email	= $('input.txt_email').val();	
		var text	= $('textarea.txta_nachricht').val();	
		var error	= new Array();
		
		if(anrede == 0)
			error['anrede'] = true;
			
		if(name == '')
			error['name'] = true;
		
		if(vorname == '')
			error['vorname'] = true;
			
		if(plz != '' && isNaN(plz))
			error['plz'] = true;
			
		if(tel == '')
			error['tel'] = true;
			
		if(email == '' || !echeck(email))
			error['email'] = true;
			
		if(text == '')
			error['nachricht'] = true;			
		
		var errorLength = 0;
		for(var i in error) {
			$('label.'+ i).addClass('text_error');
			$('.txt_'+ i +', .slct_'+ i + ', .txta_'+ i).addClass('inp_error');
			errorLength++;
		}
		
		if(errorLength == 0) {
			$('form.contactForm').submit();
			return true;
		} else {
			var errorMsg = '<div class="contact error">Bitte füllen Sie alle notwendigen Felder korrekt aus.</div>';
			$('.msgOutput').html(errorMsg);
			return false;
		}
	});
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 return true;				
}

// ****************************************** //
// * returns 'true' if browser is IE6 * //
// ****************************************** //
function is_ie6() {
	var ie6 = false;
	if($.browser.msie) {
		var version = $.browser.version;
		if(version == '6.0') {
			ie6 = true;
		}
	}
	return ie6;
}







