function formValid(theForm)
{
	firstname = theForm.first_name;
	surname = theForm.surname;
	country = theForm.country;
	press = theForm.press;
	email = theForm.email;
	comments = theForm.comments;
	
	errorelement = null;
	if (firstname.value == "")
	{
		errorelement = firstname;
	} else
	if (surname.value == "")
	{
		errorelement = surname;
	} else
	if (email.value == "" || email.value.indexOf('@') == -1 || email.value.length<6)
	{
		errorelement = email;
	} else
	if (country.selectedIndex==0)
	{
		errorelement = country;
	} else
	if (press != null && press.selectedIndex==0)
	{
		errorelement = press;
	} else	
	if (comments.value == "")
	{
		errorelement = comments;
	}
	if(errorelement)
	{
		showError(errorelement,true);
		return false;
	}
	return (true);
}

function showError(errorelement, state){
	$('div.formerror').each( function(element) {
		if(state)
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}

	});
	$(errorelement).parents().find('tr').each(function(element){
		if(state)
		{
			$(this).addClass('error');
		}
		else
		{
			$(this).removeClass('error');
		}
	});
}

function initContactForm(){
	$( '.nojs' ).each(function( intIndex ){
		$(this).remove();
	});
	
	$('button[type=submit]').each( function(element) {
		$(this).click(function() { 
		
			for(i=0; i < document.contactForm.elements.length; i++){
				element = document.contactForm.elements[i];
				showError(element,false);
			}
			
			document.contactForm.woher.value = document.contactForm.country.options[document.contactForm.country.selectedIndex].text;
			
			return formValid(document.contactForm);
		})
	});
}

$(document).ready(function(){
	initContactForm();
});