$(document).ready(function(){
	$("#lead > a").hover(
	  function () {
		$("#contact_pic").attr({src: 'images/contact_over.gif'});
	  },
	  function () {
		$("#contact_pic").attr({src: 'images/contact.gif'});
	  }
	).click(
	  function () {
		$("#claim").slideUp();
		$("#lead").animate({
		  height: "0px",
		  marginTop: "175px"
		});
		$("#contact_pic").animate({
		  marginTop: "-145px"
		});
		return false;
	})
	$("form").submit(function() {
		if (checkForm()) {
			var copy = document.getElementById('copy').checked ? 1 : 0;
			$.ajax({
			   type: "POST",
			   url: "contact.php",
			   data: "mode=ajax&name=" + encodeURIComponent($("#name").val()) + "&email=" + encodeURIComponent($("#email").val()) + "&subject=" + encodeURIComponent($("#subject").val()) + "&text=" + encodeURIComponent($("#text").val()) + "&copy=" + copy,
			   success: function(msg){
				 if (msg == 1) {
					$('#name').val(''); 
					$('#subject').val(''); 
					$('#email').val(''); 
					$('#text').val(''); 
					$('#error').html("");
					$('#contact').fadeOut("slow");
					$('#sent').fadeIn("slow");
				 }
				 else {
					 switch (msg) {
					 case 'flood':
						 $('#error').html("No flooding please. Try again later.");
						 break;
					 default:
						 $('#error').html("Send error. Please try again later.");
						 break;
					 }
				 }
			   }
			 });			
			return false;
		}
		else {
			return false;
		}
	})
	$("#send").hover(
	  function () {
		$(this).css("color", "#000000");
	  },
	  function () {
		$(this).css("color", "#737173");
	  }
	);
});


function checkForm() {
    var err = '';
	var errEnter = new Array;
    var focus = '';
	var BorderErr = '#ff0000';
	var BorderStd = '#BDBABD';

	$('#name').css("borderColor", BorderStd);
    if ($('#name').val() == "") {
		errEnter[errEnter.length] ='name';
		focus = 'name';
		$('#name').css("borderColor", BorderErr);
    }
    if ($('#name').val().length > 255) {
		err = 'Your name is too long (maximum 255 characters)';
		focus = 'name';
		$('#name').css("borderColor", BorderErr);
    }

	$('#email').css("borderColor", BorderStd);
    if ($('#email').val() == "") {
		focus = 'email';
		errEnter[errEnter.length] = 'your e-mail address';
		$('#email').css("borderColor", BorderErr);
    }
    if (! $('#email').val().match(/[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[a-zA-Z]{2,4}/) && $('#email').val() != "") {   
		errEnter[errEnter.length] = 'a valid e-mail address';
		focus = 'email';
		$('#email').css("borderColor", BorderErr);
    }
    if ($('#email').val().length > 255) {
		err = 'Your e-mail address is too long (maximum 255 characters)';
		focus = 'email';
		$('#email').css("borderColor", BorderErr);
    }

	$('#text').css("borderColor", BorderStd);
    if ($('#text').val() == "") {
		errEnter[errEnter.length] = 'a message';
		focus = 'text';
		$('#text').css("borderColor", BorderErr);
    }
    if ($('#text').val().length > 10000) {
		err = 'Your message may not be longer than 10000 characters';
		focus = 'text';
		$('#text').css("borderColor", BorderErr);
    }
	
	$('#subject').css("borderColor", BorderStd);
    if ($('#subject').val() == "") {
		$('#subject').val('www.tobiashartmann.com');
    }
    if ($('#subject').val().length > 255) {
		err = 'The subject is too long (maximum 255 characters)';
		focus = 'subject';
		$('#subject').css("borderColor", BorderErr);
    }

    if (err != "" || errEnter.length > 0) {
		errMsg = "";
		for (var i = 0; i < errEnter.length; i++) {
			if (i == 0) {
				errMsg += "Please enter "; 
			}
			if (i == errEnter.length - 1 && errEnter.length > 1) {
				errMsg += " and ";
			}
			if (i > 0 && i < errEnter.length - 1) {
				errMsg += ", ";
			}
			errMsg += errEnter[i];
		}
		$('#error').html(errMsg + "<br/>" + err);
		$("#" + focus).focus();
		return false;
    }
    else {
		return true;
    }
}



function switchImages()
{
	if (document.images && (preloaded == true)) 
	{
		for (var i = 0; i < switchImages.arguments.length; i += 2) 
		{
			document.getElementById(switchImages.arguments[i]).src = switchImages.arguments[i+1];
		}
	}
}

function loadImage(url)
{
	if (document.images) 
	{
		result = new Image();
		result.src = url;
		return result;
	}
}

var preloaded = false;
function preloadImages() 
{
	if (document.images) 
	{
		overImages = new Array;
		for (var i = 0; i < preloadImages.arguments.length; i++)
		{
			overImages[i] = loadImage(preloadImages.arguments[i]);
		}
		preloaded = true;
	}
}