
$(document).ready(function(){
	if ($("form.validate").length > 0) startValidation();
	
	if ($(".search").length > 0) {
		$(".search input[type='text']").each(function() {
			$(this).attr("oldval", $(this).val());
			$(this).focus(function(){
				$(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "")
					$(this).val($(this).attr("oldval"));
			});
		});
	}
	
	var thelocation = $(document).attr("location").href;
	
	
	if (thelocation.indexOf("/hss/") != -1) {
		$("#header ul.tc-over").css("backgroundPosition", "0 -75px");
		$("#header ul.tc-over a.tc").mouseout(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -75px");
		});		
		$("#header ul.tc-over a.tc").mouseover(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -125px");
		});		
		
	} else if (thelocation.indexOf("totalcourt.nl") != -1) {	
		$("#header ul.tc-over").css("backgroundPosition", "0 -100px");
		
		$("#header ul.tc-over a.hs").mouseout(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -100px");
		});		
		$("#header ul.tc-over a.hs").mouseover(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -150px");
		});	
	} else {
		$("#header ul.tc-over a.hs").mouseover(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -50px");
		});	
		
		$("#header ul.tc-over a.hs").mouseout(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 0");
		});	

		$("#header ul.tc-over a.tc").mouseover(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 -25px");
		});

		$("#header ul.tc-over a.tc").mouseout(function(){
			$("#header ul.tc-over").css("backgroundPosition", "0 0");
		});	
	}	
	
	var rh = $("#rightrail").height();
	var ch = $("#centerrail").height();
	var lh = $("#leftrail").height();
	
	if (rh > lh) {
		$("#centerrail").css("height", rh + "px");
		$("#leftrail").css("height", rh + "px");
	} else if (ch > lh) {
		$("#rightrail").css("height", ch + "px");
		$("#leftrail").css("height", ch + "px");
	} else {
		$("#centerrail").css("height", lh + "px");
		$("#rightrail").css("height", lh + "px");
	}

});


function bookmark(){
	title = $(document).attr("title");
	url = $(document).attr("location");
	
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
}


function formsubmit() {
	if (validateChildren(this)) {
		frmurl = $(this).attr("action");
		frmdata = $(this).serialize();
		myform = $(this);
		$.ajax({
			type: "POST",
			url: frmurl,
			data: frmdata,
			success: function(msg) {
				if (msg == "success") {
					myform.fadeOut("normal", function(){myform.parent().find("#sentmessage").html("Thank you!<br/>Message Sent! <a href=\"#\">Send again</a>").fadeIn();});
				} else {
					myform.fadeOut("normal", function(){
						myform.parent().find("#sentmessage").html("Error sending! <a href=\"#\">Try again</a>.").fadeIn();
						myform.parent().find("#sentmessage a").click(function(){
							myform.parent().find("#sentmessage").fadeOut("normal", function(){myform.fadeIn("normal");});
							return false;
						});
					});									
				}
			}
		});
	} 
	
	return false;	
}

function startValidation() {
	$("form.validate").each(
		function (i, val) {			
			$(this).find(":text").each(
				 function () {
					if ($(this).val() != "") $(this).attr("oldval", $(this).val());
					
					$(this).blur(function() {
						if ($(this).hasClass("required")) {
							if ($(this).hasClass("valid")) $(this).removeClass("valid");
							if ($(this).hasClass("invalid")) $(this).removeClass("invalid");
							
							if ($(this).val() == "") { 
								$(this).addClass("invalid");
							} else if ($(this).hasClass("emailadd")) {
								var tEmail = new REgExp("\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$");
								if (!tEmail.test($(this).text)) $(this).addClass("invalid");
							} else {
								$(this).addClass("valid");
							}
						}
						
						if ($(this).attr("oldval") != "") {
							if ($(this).val() == "") {
								$(this).val($(this).attr("oldval"));
							}
						}
					});
					
					$(this).focus(function() {
						if ($(this).val() == $(this).attr("oldval")) {
							$(this).val("");
						}
					});					
				}
			);
			
			myform = $(this);
	
			$(this).submit(function() { 
				if ($(this).hasClass("callback")) {
					if (validateChildren(this)) {
						frmurl = $(this).attr("action");
						frmdata = $(this).serialize();
						$.ajax({
							type: "POST",
							url: frmurl,
							data: frmdata,
							success: function(msg) {
								if (msg == "success") {
									myform.fadeOut("normal", function(){$("#sentmessage").html("Thank you!<br/>Message Sent!").fadeIn();});
								} else {
									myform.fadeOut("normal", function(){
										$("#sentmessage").html("Error sending! <a href=\"#\">Try again</a>.").fadeIn();
										$("#sentmessage a").click(function(){
											$("#sentmessage").fadeOut("normal", function(){myform.fadeIn("normal");});
											return false;
										});
									});									
								}
							}
						});
					} 
					return false;
				} else {				
					return validateChildren(this);
				}
			});		
		}
	);
}

function validateChildren(myForm) {
	if ($(myForm).find(".required").length > 0) {
		if ($(myForm).find(".valid").length == $(myForm).find(".required").length) {
			return true;
		} else {
			$(myForm).find(".required").each(
				function() {
					if ($(this).val() == "") $(this).addClass("invalid");
				}
			);
			return false;
		}
	} else {
		retval = true;
		$(myForm).find("input[type='text']").each(function(){
			if ($(this).val() == $(this).attr("oldval")) retval = false;			
		});		
		return retval;
	}
}
/* --------------------------------------------------------------------------- */
