$(document).ready(function(){
	/* h1 onclick */
	$("h1").click( function(){ window.location.href='home.html'; } );

	/* nach oben scrollen */
	$("[href='#top']").click( function(){ $('html, body').animate({scrollTop:0}, 'slow'); return false; } );

	/* dl-form hover */
	$('#download_form').find('a:first').mouseover(function(){
		if((navigator.appName == "Microsoft Internet Explorer") && (browser.indexOf('MSIE 6.0') != '-1')){
			$('#download_form').css('background-position', '0 -137px');
		}else{
			$('#download_form').css('background-position', '0 -138px');
		}
	});
	$('#download_form').find('a:last').mouseover(function(){
		$('#download_form').css('background-position', '0 0');
	});

	/* dl-form hover */
	$('#download_form_camp').find('a:first').mouseover(function(){
		if((navigator.appName == "Microsoft Internet Explorer") && (browser.indexOf('MSIE 6.0') != '-1')){
			$('#download_form_camp').css('background-position', '0 -137px');
		}else{
			$('#download_form_camp').css('background-position', '0 -138px');
		}
	});
	$('#download_form_camp').find('a:last').mouseover(function(){
		$('#download_form_camp').css('background-position', '0 0');
	});


	/* lang hover info */
	$('#sprachwahl').find('li').hover(
		function(){
			$('#langinfo').html($(this).attr('title')).fadeIn('fast');
			$(this).attr('title','');
		},
		function(){
			$('#langinfo').hide();
			$(this).attr('title',$('#langinfo').html());
		}
	);
	$('#sprachwahl').find('li').mousemove(function(e){
		$('#langinfo').css("left", e.pageX + 18);$('#langinfo').css("top", e.pageY - 5);
	});

	/* ie6 css anpassung via js */
	browser = navigator.appVersion;
	if(navigator.appName == "Microsoft Internet Explorer"){
		$('#siteinfo').find('li').css('display','inline');

		if(browser.indexOf('MSIE 6.0') != '-1'){
			$('#siteinfo').find('li').append(' |');
			/* form fixes */
			$('textarea').css('width','300px');
			$('input[type="text"]').css('width','300px');
			$('form').find('div').next('div').css('margin-top','5px');
			/* p fixes */
			$('p').css('padding-bottom','10px');
			$('p').css('margin-bottom','0');
			$('p').next('p').css('margin-top','0');
			$('p').next('p').css('padding-top','0');
			$('h4').next('p').css('padding-top','10px');
			$('h4').next('ul').css('padding-top','10px');
			$('ul').next('h4').css('padding-top','10px');
			/* dl + p fix */
			$('dl').find('p:next').css('clear', 'both');
			/* main-div fixes */
			$('#content').find('ol').next().css('margin-top','10px');
			/* nav fix */
			$('#nav ul.main li:first').css('background', 'none');
			/* mood fix */
			$('#mood').attr('src', 'img/mood.png');
			$('#mood').attr('class', 'png');
			/* content hight fix */
			if($('#content').height() < ($('#nav').find('.main').height()+205)){
				$('#content').css('margin-top','-2px');
			}
		}
		if(browser.indexOf('MSIE 7.0') != '-1'){
		/*	if($('#content').height() < ($('#nav').find('.main').height()+205)){
				$('#content').css('margin-top','-2px');*/
			if($('#content').height() > (400)){
				//		$('#content').css({'marginTop':'-15px','paddingTop':'30px'});
						$('#content').css('marginTop','-16px');
						$('#content h2').css('margin-top','16px');

					}
			/*}*/
		}
	}

	/* content-height anpassung */
	if($('#content').height() < ($('#nav').find('.main').height()+225)){
		$('#content').css('height', ($('#nav').find('.main').height()+225));
	}
});

/* email validierung */
function checkMail(s){
	var a = false;
	var res = false;
	if(typeof(RegExp) == 'function') {
		var b = new RegExp('abc');
		if(b.test('abc') == true){a = true;}
	}
	if(a == true){
		reg = new RegExp('^([a-zA-Z0-9\-\.\_]+)'+'(\@)([a-zA-Z0-9\-\.]+)'+'(\.)([a-zA-Z]{2,4})$');
		res = (reg.test(s));
	}else{
		res = (s.search('@') >= 1 && s.lastIndexOf('.') > s.search('@') && s.lastIndexOf('.') >= s.length-5);
	}
	return(res);
}

/* cookie daten erfragen */
function cookieData(n){
	a = document.cookie;
	//alert(a);
	res = '';
//	alert(n);
	all = a.split(";");
	for(i=0;i<all.length;i++)
	{	
//	while(a != ''){
		helper = all[i].split("=");
		cookiename = helper[0];
		cookiewert = helper[1];
	//	alert("n =" + n + "=");
	//	alert("cname =" + cookiename + "=");
		n = n.replace(/ /, "");
		cookiename = cookiename.replace(/ /, "");
		if(n == cookiename){
		//	alert("inschleife " + cookiename + " " + cookiewert);
			hwert = cookiewert.split(" ");
			res = hwert[0].replace(/;/, "");
		}
	}
	return(res);
}

function cookieData2(n){
	a = document.cookie;
	res = '';
	while(a != ''){
		cookiename = a.substring(0,a.search('='));
		cookiewert = a.substring(a.search('=')+1,a.search(';'));
		if(cookiewert == ''){
			cookiewert = a.substring(a.search('=')+1,a.length);
		}
		if(n == cookiename){
			res = cookiewert;
		}
		i = a.search(';')+1;
		if(i == 0){
			i = a.length
		}
		a = a.substring(i,a.length);
	}
	return(res);
}

/* form-submit-check + ajax */
function checkForm(){
	if($('#vorname').val() == ""){
		alert('vorname');
		$('#vorname').focus();
	}
	else if($('#nachname').val() == ""){
		alert('nachname');
		$('#nachname').focus();
	}
	else if($('#birth').val() == ""){
		alert('birth');
		$('#birth').focus();
	}
	else if($('#email').val() == ""){
		alert('email');
		$('#email').focus();
	}
	else if(checkMail($('#email').val()) == false){
		alert('valid mail');
		$('#email').focus();
	}
	else if($('#frage').val() == ""){
		alert('frage');
		$('#frage').focus();
	}
	else if($('#captcha').val() == ""){
		alert('captcha');
		$('#captcha').focus();
	}
	else if(cookieData('cm_ass_captcha') != $('#captcha').val()){
		alert('captcha2');
		$('#captcha').focus();
	}
	else{
		sex		= ($('#sexa:checked').length) ? 'm' : 'w';
		zone	= ($('#zonea:checked').length) ? '1' : '2';
		data	= '&sex=' + sex
			+ '&zone=' + zone
			+ '&vorname=' + $('#vorname').val()
			+ '&nachname=' + $('#nachname').val()
			+ '&birth=' + $('#birth').val()
			+ '&email=' + $('#email').val()
			+ '&policy=' + $('#policy').val()
			+ '&wform=' + $('#wform').val()
			+ '&frage=' + $('#frage').val();
		$.ajax({
			type:		"POST",
			url:		"tools.php",
			data:		"action=sendMail" + data,
			success:	function(a){
				//document.contact.reset();
				alert(a);
			},
			error:		function(a){ alert(a.getResponseHeader("Status")); }
		});
	}
	return false;
}

/* provider iframe + floater */
function provider(){
	$('#langinfo').hide();
	$("body").append(
		 '<div id="floater">'
		+'<iframe width="1" height="1" src="http://www.hygeia.net/customer_tools/customers/caremed/main.asp"></iframe>'
		+'</div>'
	);
	$('#floater').find('iframe').animate({
		marginTop:	($(window).height()/2)-246,
		marginLeft:	($(window).width()/2)-250,
        height:	494,
        width:	600
	}, 1500, function(){
		$("#floater").append('<a href="javascript:providerClose();" id="close" class="png">Close</a>');
		$('#close').css('display', 'block');
		$('#close').css('margin-top', ($(window).height()/2)-253),
		$('#close').css('margin-left', ($(window).width()/2)+345),
		$('#close').fadeIn('fast');
	});
	$("#floater").fadeIn('slow');
	$('#langinfo').hide();
}

/* floater schließen */
function providerClose(){
	$('#floater').find('iframe').fadeOut('fast', function(){
		$('#floater').remove();
	});
}

/* contact-form-check + ajax */
function contactMail(){
	if($('#vorname').val() == ""){
		alert('vorname');
		$('#vorname').focus();
	}
	else if($('#nachname').val() == ""){
		alert('nachname');
		$('#nachname').focus();
	}
	else if($('#email').val() == ""){
		alert('email');
		$('#email').focus();
	}
	else if(checkMail($('#email').val()) == false){
		alert('valid mail');
		$('#email').focus();
	}
	else if($('#frage').val() == ""){
		alert('frage');
		$('#frage').focus();
	}
	else if($('#captcha').val() == ""){
		alert('captcha');
		$('#captcha').focus();
	}
	else if(cookieData('cm_captcha') != $('#captcha').val()){
		alert('captcha2');
		$('#captcha').focus();
	}
	else{
		sex		= ($('#sexa:checked').length) ? 'm' : 'w';
		data	= '&sex=' + sex
			+ '&vorname=' + $('#vorname').val()
			+ '&nachname=' + $('#nachname').val()
			+ '&email=' + $('#email').val()
			+ '&frage=' + $('#frage').val()
			+ '&empfaenger=' + $('#empfaenger').val();
		$.ajax({
			type:		"POST",
			url:		"tools.php",
			data:		"action=contactSend" + data,
			success:	function(a){
				alert(a);
				document.contact.reset();
			},
			error:		function(a){ alert(a.getResponseHeader("Status")); }
		});
	}
	return false;
}