	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		hideModalWindow();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		hideModalWindow();
	});	


function showModal(id) {	

	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set height and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	$('#mask').fadeIn(200);	
	$('#mask').fadeTo("fast",0.5);	
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();

	$(id).css('top',  "10px");
	$(id).css('left', "10px"); 	 //     		alert(winW); return;
	$(id).css('display' , "block");
	$(id).css('width',  (winW-50) + "px");
	//$(id).css('height',  (winH-40) + "px");
	var table = id + "_table";
	var padding = ((winH-40 - $(table).height()) / 2);
//var padding = 10;
	if($(table).height() != null && padding > 1)
	{
			$(id).css('padding-top',  padding  + "px" );
			$(id).css('padding-bottom',  padding  + "px" );
	}
//	if($(table).height() > 600)
//	{
		$(id).css('position' , "absolute");
//	}
//	else
//	{
//		$(id).css('position' , "fixed");
//	}
	$(id).fadeIn(1000); 	
}

function hideModalWindow(id) {
	var fadeout_time = 1000;
	$(id).fadeOut(fadeout_time);
	setTimeout("$('#mask, .window').hide()",fadeout_time);

}

	function getY( oElement )
	{
		var iReturnValue = 0;
		while( oElement != null ) {
			iReturnValue += oElement.offsetTop;
			oElement = oElement.offsetParent;
		}
		return iReturnValue;
	}

	function getX( oElement )
	{
		var iReturnValue = 0;
		while( oElement != null ) {
			iReturnValue += oElement.offsetLeft;
			oElement = oElement.offsetParent;
		}
		return iReturnValue;
	}

	function Poststuff(strURL , div , query_str,doafter) {
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.xmlHttpReq.open('POST', strURL, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(div).innerHTML = self.xmlHttpReq.responseText;
				setTimeout(doafter,500);
			}
		}
		self.xmlHttpReq.send(query_str);
	}



	function login()
	{
		uname = document.getElementById("username").value;
		pass = document.getElementById("password").value;
		if(!uname.length )
		{
			set_error("Kasutajanimi sisestamata", document.getElementById("username"));
			return false;
		}
		if(!pass.length)
		{
			set_error("Parool sisestamata" , document.getElementById("password"));
			return false;
		}
		str = 'user=' + uname + '&password=' + pass;

		Poststuff(u("users/login") , "log_section" , str,"manage_login()");
	}

	function validate(address) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(address) == false) {
		  return false;
	   }
	   return true;
	}



	function logout()
	{
		str = "";
		xmlhttpPost(u("users/logout") , "log_section" , str);

		if (window.after_logout ) {
			after_logout();
		}
		
		hide_user_link();
	}

	function hide_user_link()
	{
		$("td[name=customer_zone]").css("display",  "none");
	}

	function register()
	{
		uname = document.getElementById("reg_username").value;
		pass = document.getElementById("reg_password").value;
		pass_again = document.getElementById("password_again").value;
		var email = document.getElementById("email").value;
		var phone = document.getElementById("phone").value;
		if(!uname.length )
		{
			set_error("Kasutajanimi sisestamata", document.getElementById("reg_username"));
			return false;
		}
		if(!pass.length)
		{
			set_error("Parool sisestamata" , document.getElementById("reg_password"));
			return false;
		}
		if(!pass_again.length)
		{
			set_error("Parool uuesti sisestamata" , document.getElementById("password_again"));
			return false;
		}
		if(!(pass == pass_again))
		{
		//	set_error("Paroolid on erinevad" , document.getElementById("password_again"));
			set_popup_error("Paroolid on erinevad" , "#register_window");
			return false;
		}
		if(!email.length)
		{
			set_error("E-maili aadress on kohutstuslik" , document.getElementById("email"));
			return false;
		}
		if(!validate(email))
		{
			set_error("Pole toimiv e-maili aadress" , document.getElementById("email"));
			return false;
		}

		if(!phone.length)
		{
			set_error("Telefon on kohustulsik" , document.getElementById("phone"));
			return false;
		}
		str = 'user=' + uname +'&password=' + pass+ '&password_again=' + pass_again+ '&email=' + email+ '&phone=' + phone;
		Poststuff(u("users/register") , "log_section" , str,"show_register_error()");

	}


	function show_register_error()
	{
		var error = document.getElementById("register_error");
		if(error != null)
		{
			set_popup_error(error.value , "#register_window");
		//	set_error(error.value , document.getElementById("login"));
		}
		else
		{
			show_user_link();
			hideModalWindow("#register_window");
		}
	}		

	function set_popup_error(text,id)
	{
		$(id + "_popup_error_message").html(text);
	}

	function show_user_link()
	{
		$("td[name=customer_zone]").css("display",  "");
	}

	function redirect()
	{
		var redirect = document.getElementById("redirect");
		if(redirect != null)
		{
			window.location = redirect.value;
		}
	}

	function manage_login()
	{
		redirect();
		var error = document.getElementById("login_error");
		if(error != null)
		{
			set_popup_error(error.value , "#login_window");
			//set_error(error.value , document.getElementById("login"));
		}
		else
		{
			if (window.after_login ) {
				after_login();
			}
			show_user_link();
			hideModalWindow("#login_window");
		}
	}

	function xmlhttpPost(strURL , div , query_str) {
		var xmlHttpReq = false;
		var self = this;
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.xmlHttpReq.open('POST', strURL, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(div).innerHTML = self.xmlHttpReq.responseText;
			}
		}
		self.xmlHttpReq.send(query_str);
	}

	function getquerystring(elementid) {
		word = document.getElementById(elementid).value;
		qstr = elementid + '=' + escape(word);  
		return qstr;
	}

	function set_error(message , element)
	{
		err = document.getElementById("error");
		err.style.display="block";
		err.innerHTML = message;
		err.style.position = "absolute";
		err.style.top = (getY(element) - 30) + "px";
		err.style.left = getX(element) + "px";
		clickedmoment = new Date();
	}	

	function show_tickets_to_buy()
	{
		div = "#tickets_to_buy_button";
		div2 = "#tickets_to_buy_junn";
		topdiv = "#page_top";
		var pos = $(topdiv).offset();
		if(pos != null)
		{
			$(div).css('top',  (pos.top + 56));
			$(div).css('left', (pos.left + 765)); 
			$(div).css('display', 'block'); 
			$(div2).css('top',  (pos.top + 15));
			$(div2).css('left', (pos.left + 876)); 
			$(div2).css('display', 'block'); 
		}
var productElement = $(div2);
if (productElement != null)
{
		 $("#langue_bar").css('margin-right', '140px'); 
}

	}
	
	function delete_tickets_to_buy()
	{
		div = "#tickets_to_buy_button";
		div2 = "#tickets_to_buy_junn";
		$(div).html(""); 
		$(div2).html("");
	}

	function show_errors()
	{
		var text = $("#error_content").html();
		if(text.length)
		{
			showModal('#error_window');
		}
	}

	function bodyonload()
	{
		var ie6 = false;
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		 var ieversion=new Number(RegExp.$1);
		 if (ieversion>=6 && ieversion<8)
		 {
			 ie6 = true;
		 }
		}
		if(!ie6)
		{
			show_tickets_to_buy();
		}
		else
		{
			delete_tickets_to_buy();
		}
		show_errors();
		if (window.start_page_function ) {
			start_page_function();
		}
		position_elements();
	}

	function position_elements()
	{
		var ie6 = false;
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		 var ieversion=new Number(RegExp.$1);
		 if (ieversion>=6 && ieversion<7)
		 {
			 ie6 = true;
		 }
		}



		$("#content_bottom_image").css("padding-bottom" , 25 + "px");
		var height = document.getElementById('page_body').offsetHeight;
		var bottomheight = document.getElementById('content_bottom_image').offsetHeight;
		var position = $("#content_bottom_image").offset().top;

//alert(height); alert(bottomheight); alert(position);
		$("#infosys_link").css("top" , (position + bottomheight -20) + "px");
		$("#infosys_link").css("display" , "block");
		$("#infosys_link").css("left" , ($("#content_bottom_image").offset().left + 880) + "px");

		var new_value = (height - position - 94);

		if(ie6)
		{
new_value = new_value + 32;//alert(new_value);
		}

		if(new_value > 25)
		{
			$("#content_bottom_image").css("padding-bottom" , new_value + "px");
		}		
	}

	function bodyonclick()
	{
		end = new Date();
		var difference = end.getTime() - clickedmoment.getTime();
		if(difference > 20)
		{
			$("#error").css("display" , "none");
		//$("#datepicker").css("display" , "none");
		}
		$("#Result_div").css("display" , "none");

	}
	
	function hide_hover()
	{
		div = "#hover";
		$(div).css('display', 'none');
	}

	function set_hover(id , text , x, y)
	{
		div = "#hover";
		$(div).css('display', 'block');
		div2 = "#hover_content";
		var pos = $(id).offset();

		if(pos != null)
		{
			$(div).css('top',  (pos.top + y - 20));
			$(div).css('left', (pos.left + x));
			$(div2).html(text);
		}
	}

function showhide(contentbit , imagebit)
{
	el=document.getElementById(contentbit);
	im=document.getElementById( imagebit);
	if (el.style.display=="none") {
		el.style.display="block";
		im.src=u("img/open.png");
		im.alt=im.title="Kinni";
	} else {
		el.style.display="none";
		im.src=u("img/closed.png");
		im.alt=im.title="Lahti";
	}
	position_elements();
	return false;
}



function show_info(id)
{
	var query_str = 'id=' + id;
	var strURL = u("users/info");
	$.ajax({
	   type: "POST",
	   url: strURL,
	   data: query_str,
	   success: function(msg){
			document.getElementById("info_box_content").innerHTML = msg;
			showModal('#info_box');
	   }
	 });



}

