function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
/* prevent default action */
_preventDefault = function(_event){
	if(_event && _event.preventDefault) _event.preventDefault();
	else if(window.event && window.event.returnValue) window.eventReturnValue = false;
};
/* center element */
function pageWidth(){
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight(){
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}
function posLeft(){
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function posRight(){
	return posLeft()+pageWidth();
}
function posBottom(){
	return posTop()+pageHeight();
}

/* center the popup div */
centerScreen = function(loaded){
	_x = "400";
	_y = "100";
	_top = (pageHeight()/2)-(_y/2);
	_left = (pageWidth()/2)-(_x/2);
	document.body.style.overflow = "hidden";

	if(!loaded){
		_text = "Vennligst vent.";
		$.get("pages/downloadpdf.html", function(_html){
			$(_html).appendTo("body");
			$("#downloadcont h2").text(_text);
			$("#overflowdiv, #confirm_div").width(pageWidth()+"px");
			$("#overflowdiv").height(pageHeight()+"px");
			$("#overflowdiv").css({"top":posTop()+"px", "left":posLeft()+"px"});
			$("#confirm_div").css({"top":_top+"px"});
			$("#downloadcontainer").width(_x+"px");
			$("#downloadcontainer").height(_y+"px");
			$("#downloadcontainer").css({"left":_left+"px"});
			$("#closedownload").width(_x-10+"px");
		});
		$(window).resize(function(){ centerScreen(!loaded); });
	}
	else{
		$("#overflowdiv").stop();
		$("#overflowdiv").css({width: pageWidth(), height: pageHeight(), top: posTop(), left: posLeft()});
		$("#confirm_div").css({width: pageWidth(), top: _top});
		$("#closedownload").width(_x-10+"px");
		$("#downloadcontainer").animate({left: _left, width: _x, height: _y}, 200, "linear");
	}
};

/* close the popup div */
_cancel = function(_event){
	_preventDefault(_event);
	$("#closedownload, #downloadcontainer, #confirm_div, #overflowdiv").remove();
	document.body.style.overflow = "visible";
};

/* animated close */
_closeOverflow = function(){
	$("#closedownload, #downloadcontainer, #confirm_div, #overflowdiv").animate({ opacity: "hide" }, "slow");
	setTimeout("$('#closedownload, #downloadcontainer, #confirm_div, #overflowdiv').remove()", 1000);
	document.body.style.overflow = "visible";
}

/* start functions */
$(document).ready(function(){
	/* house select/search */
	$("#land, #size, #bedroom").change(function(){
		_land = $("#land").attr("value");
		_size = $("#size").attr("value");
		_bedroom = $("#bedroom").attr("value");

		$.get("do_ajax_load.php", { land:_land, size:_size, bedroom:_bedroom }, function(data){
			$("#quickselect").html(data);
		});
	}),
	/* show/hide hidden divs */
	$("#land2").click(function(){
		if($("#want_landtype").is(":hidden")){
			$("#want_landtype").show();
			$("#has_landtype").hide();
		}
	}),
	$("#land").click(function(){
		if($("#has_landtype").is(":hidden")){
			$("#has_landtype").show();
			$("#want_landtype").hide();
		}
	}),
	/* check required */
	$("#sendmaillink").click(function(){
		$errormsg = "";
		$tmp = "";
		$either = "0";

		$("#sendform td").each(function(){
			if($(this).html().indexOf("*") > "0"){
				if($(this).next().children().val() == ""){
					$errormsg += "'"+$(this).text()+"' er påkrevd.\r\n";
				}
			}
			if($(this).html().indexOf("°") > "0"){
				if($(this).next().children().val() == ""){
					$either++;
					$tmp += "'"+$(this).text()+"' eller ";
				}
				else{
					$either--;
				}
			}
		});

		if($either > "0"){
			$tmp = rtrim($tmp, " eller");
			$errormsg += $tmp+"  må fylles ut.\r\n";
		}

		/* custom errors */
		if($("#sendform_middle").length > "0"){
			if($("input:radio[name=send_land]:checked").length == "0")
				$errormsg += "'* Har tomt' er påkrevd.\r\n";
			else{
				if($("#want_landtype").is(":visible")){
					if($("input:radio[name=send_want_land]:checked").length == "0"){
						$errormsg += "'* Interessert i tomt' er påkrevd.\r\n";
					}
					if($("#send_wantland_city").val() == ""){
						$errormsg += "'* Kommune' er påkrevd.\r\n";
					}
				}
				if($("#has_landtype").is(":visible")){
					if($("#send_landtype").val() == ""){
						$errormsg += "'* Type tomt' er påkrevd.\r\n";
					}
					if($("#send_land_city").val() == ""){
						$errormsg += "'* Kommune' er påkrevd.\r\n";
					}
				}
			}
		}

		// submit or on error, cancel submit
		if($errormsg != ""){
			alert($errormsg);
			return false;
		}
		else{
			$("#sendmail").submit();
		}
	}),
	/* print to pdf link action */
	$("#print2pdf").click(function(event){
		_preventDefault(event);
		centerScreen();
		$.ajax({
			type: "GET",
			url: $(this).attr("href"),
			processData: false,
			dataType: "json",
			success: function(json){
				if(json.error == "0"){
					$("#confirm_div h2").text("Filen er klar til nedlasting.");
					$("#downloadpdf_a").attr({"href":json.text});
					$("#error").css({"display":"none"});
					$("#downloadpdf").css({"display":"block"});
				}
				else{
					$("#confirm_div h2").text("Det har skjedd en feil med filen. Vennligst prøv igjen.");
					$("#error").html(json.text);
					$("#error").css({"display":"block"});
					$("#downloadpdf_a").attr({"href":""});
					$("#downloadpdf").css({"display":"none"});
					setTimeout("_closeOverflow();", 3000);
				}
			}
		});
	}),
	$(window).keypress(function(event){
		if(event.keyCode == "27")
			_cancel(event);
	}),
	$("#printpage").click(function(event){
		_x = "650";
		_y = "600";
		_top = (pageHeight()/2)-(_y/2);
		_left = (pageWidth()/2)-(_x/2);
		_preventDefault(event);
		window.open(this.href, "printpage", "width="+_x+",height="+_y+",scrollbars=yes,left="+_left+",top="+_top+",toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes");
		return false;
	});
});
