/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * Modified by Paul Kretek, Santora Media, http://www.santora.de/
 */

$(document).ready(JT_init);
var timerID;
function JT_init(){
	$(".jTip").hover(function(){JT_show(this.id,$(this).attr("tip"), "150")},function(){$('#JT').remove()});	   
}


function alertme(text) {
		clearTimeout(timerID);
		$('#alert').remove();
		$("body").append("<div id='alert'><div class='text'><img src='http://static.santora.de/santora.de/i/emblem-important.png' alt='' /> "+text+"</div></div>");
		$("#alert").click(function () { $(this).slideUp().queue(function () {$(this).remove();}); });
		$("#alert").slideDown();
		timerID  = setTimeout("$('#alert').slideUp().queue(function () {$('#alert').remove();});", 10000);
}

function JT_show(linkId,title, width){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 20; //set y position
	
	$("body").append("<div id='JT' style='width:"+width*1+"px'><div id='JT_arr'></div><div id='JT_text'>"+title+"</div></div>");
	var arrowOffset = getElementWidth(linkId) + 11;
	var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();

}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft
		while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

function getAbsoluteTop(objectId) {
	o = document.getElementById(objectId);
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}




$(document).ready( function() {
	$("#contactForm").submit(function() {
		return checkForm(this);
    });
} );


	function checkForm(form) {
		var valid = true;
		var alerttext = "";
		
		jQuery.each($("#" + form.id + " :text, :checkbox, textarea"), function() {
			$(this).css("background-color", "#ffffff");
			$(this).parent().css("background-color", "transparent");	
			if ( $(this).attr("required") ) {
				if ($(this).val() == "") {
						$(this).css("background-color", "#ffcccc");
						alerttext+="* Bitte f&uuml;llen sie das Feld &raquo;"+ $(this).attr("required") +"&laquo; aus!<br/>";
						valid = false;
				}
			}

			if ( $(this).hasClass("valemail") && $(this).val() !== "") { 
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test( $(this).val() )) {
						$(this).css("background-color", "#ffcccc");
						alerttext+="* Bitte geben Sie eine g&uuml;ltige eMail Addresse ein!<br/>";
						valid = false;
				}
			
			 }
			if ( $(this).hasClass("valphone") && $(this).val() !== "") { 
				var filter  = /^([0-9\.\-\\\/\+\ \(\)])+$/;
				if (!filter.test( $(this).val() )) {
						$(this).css("background-color", "#ffcccc");
						alerttext+="* Bitte geben Sie eine g&uuml;ltige Telefonnummer ein!<br/>";
						valid = false;
				}
			
			 }
                        if ( $(this).hasClass("valcheckbox") && !$(this).attr("checked")) {
	                        $(this).parent().css("background-color", "#ffcccc");
	                        alerttext+="* Bitte stimmen Sie der Datenschutzerkl&auml;rung zu.<br/>";
	                        valid = false;
                        }

		});
		if(valid)
			return true;
		else {
			alertme(alerttext);
			return false;
		}
	
		return false;
	/*
		var message = document.getElementById("message");
		if (message.value == "") {
			alert ("Message is required.");
			return false;
		}
		var firstName = document.getElementById("firstName");
		if (firstName.value == "") {
			alert ("First Name is required.");
			return false;
		}
		var lastName = document.getElementById("lastName");
		if (lastName.value == "") {
			alert ("Last Name is required.");
			return false;
		}

		var organization = document.getElementById("organization");
		if (organization.value == "") {
			alert ("Organization is required.");
			return false;
		}
		var email = document.getElementById("email");
		if (email.value == "") {
			alert ("Email address are required.");
			return false;
		} else {
			if(email.value.indexOf("@") == -1) {
				alert ("Invalid email address format.");
				return false;
			}
		}
		var from = document.getElementById("from");
		from.value = email.value;
		return true;
		*/
	}
