﻿//-------------------------------------
// styleInputBoxes
//-------------------------------------
function styleInputBoxes() {
	//
	var inputBoxHeight = 15;
	//
	var $InputBox = $(this);
	var $span = $("<span />");
	var $label = $InputBox.parent("label");
	$span.addClass($InputBox.attr("type"));
	$span.css(inputBoxGetBgPosition(this.checked, 1));
	// bind inputBox events
	$InputBox.before($span);
	$InputBox.change(inputBoxClear);
	$InputBox.bind("error", inputBoxError);
	// hide inputBox
	$InputBox.css("display", "none");
	// bind span events
	// In Firefox the label tag gets the mouseup event !!
	if ($.browser.msie || !$span.parent().is("label")) {
		$span.mouseup(inputBoxCheck);
	}
	$span.hover(inputBoxOver, inputBoxClear);
	$span.mousedown(inputBoxDown);
	// bind document event
	$(document).mouseup(inputBoxClear);

	function inputBoxGetBgPosition(checked, state) {
		var multi = state + ((checked) ? 3 : 0) - 1;
		return {
			"background-position": "0 -" + (inputBoxHeight * multi) + "px"
		}
	}
	//-------------------------------------
	// inputBox methods
	//-------------------------------------
	function inputBoxClear() {
		var $InputBoxes = $(":checkbox, :radio");
		$InputBoxes.map(function() {
			var $InputBox = $(this);
			var $span = $InputBox.prev();
			$span.css(inputBoxGetBgPosition($InputBox[0].checked, 1));
		});
	}
	function inputBoxOver() {
		var $span = $(this);
		var $InputBox = $span.next();
		$span.css(inputBoxGetBgPosition($InputBox[0].checked, 2));
	}
	function inputBoxDown() {
		var $span = $(this);
		var $InputBox = $span.next();
		$span.css(inputBoxGetBgPosition($InputBox[0].checked, 3));
	}
	function inputBoxError() {
		var $InputBox = $(this);
		var $span = $InputBox.prev();
		$span.css(inputBoxGetBgPosition(false, 6));
	}
	function inputBoxCheck() {
		var $span = $(this);
		var $InputBox = $span.next();
		$InputBox[0].checked = !$InputBox[0].checked;
	}
}
//-------------------------------------
// styleSelectBoxes
//-------------------------------------
var selectWidth = 0;
var selectHeight = 25;
function styleSelectBoxes() {
	var $SelectBox = $(this);
	var $label = $SelectBox.parent("label");

	var styleWidth = $SelectBox.css("width").substr(0,$SelectBox.css("width").length-2);
	var width = $SelectBox.width();	
	
	selectWidth = (!isNaN(styleWidth)) ? Number(styleWidth)+25 : width+25;

	$SelectBox.addClass("styled");
	$SelectBox.css("width", selectWidth + "px");
	$SelectBox.css("height", selectHeight + "px");

	$Options = $SelectBox.children("option");
	var ActiveString = $Options.filter(":selected").text();
	var $Outer = $("<div />");
	$Outer.addClass("selectOuter");
	var $Inner = $("<div />");
	$Inner.addClass("selectInner");
	$Inner.css("height", selectHeight + "px");
	
	var $Text = $('<span />');
	$Text.addClass("selectText");
	$Text.text(ActiveString);

	$Inner.append($Text);
	$Outer.append($Inner);
	$SelectBox.before($Outer);
	$Outer.append($SelectBox);
	//
	$Outer.css("width", selectWidth + "px");
	$Outer.css("height", selectHeight + "px");
	//
	$SelectBox.focus(selectFocus);
	$SelectBox.blur(selectBlur);
	$SelectBox.mouseover(selectOver);
	$SelectBox.mouseout(selectOut);
	$SelectBox.change(selectChange);

	function selectFocus() {
		var $SelectBox = $(this);
		var $Text = $SelectBox.prev().children("span");
		$Text.attr("class", "selectText_focus");
		$Options = $SelectBox.children("option");
		var index = this.selectedIndex;
		$SelectBox.keypress(function(event) {
			if (event.keyCode == 37 || event.keyCode == 38) {
				index = (index > 0) ? index - 1 : 0;
			} else if (event.keyCode == 39 || event.keyCode == 40) {
				index = (index + 1 < $Options.length) ? index + 1 : index;
			}
			$Text.text($Options.eq(index).text())
		});
	}
	function selectBlur() {
		var $SelectBox = $(this);
		var $Text = $SelectBox.prev().children("span");
		$Text.attr("class", "selectText");
		$SelectBox.mouseout();
	}
	function selectOver() {
		var $SelectBox = $(this);
		$SelectBox.parent("div").attr("class", "selectOuter_over");
	}
	function selectOut() {
		var $SelectBox = $(this);
		$SelectBox.parent("div").attr("class", "selectOuter");
	}
	function selectChange() {
		var $SelectBox = $(this);
		var $Text = $SelectBox.prev().children("span");
		$Text.text($SelectBox.children("option").filter(":selected").text());
	}
}


//-------------------------------------
// styleFileInput
//-------------------------------------

var fileWidth = 287;
var fileHeight = 25;

function styleFileInput() {
	var $FileInput = $(this);
	var $label = $FileInput.parent("label");

	var styleWidth = $FileInput.outerWidth()
	selectWidth = (styleWidth > fileWidth) ? styleWidth: fileWidth;

	$FileInput.addClass("styled");
	$FileInput.css("width", fileWidth + "px");
	$FileInput.css("height", fileHeight + "px");

	var $Outer = $("<div />");
	$Outer.addClass("fileOuter");
	var $Inner = $("<div />");
	$Inner.addClass("fileInner");
	$Inner.css("height", fileHeight + "px");
	
	var $Text = $('<span />');
	$Text.addClass("fileText");
	
	var fileText = (String($FileInput.val()) != "") ? $FileInput.val() : "\r \n Vælg fil";
	$Text.text(fileText);

	$Inner.append($Text);
	$Outer.append($Inner);
	$FileInput.before($Outer);
	$Outer.append($FileInput);
	//
	$Outer.css("width", fileWidth + "px");
	$Outer.css("height", fileHeight + "px");

	
	$FileInput.focus(fileFocus);
	$FileInput.blur(fileBlur);
	$FileInput.mouseover(fileOver);
	$FileInput.mouseout(fileOut);
	$FileInput.change(fileChange);

	function fileFocus() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span");
		$Text.attr("class", "fileText_focus");
	}
	function fileBlur() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span");
		$Text.attr("class", "fileText");
		$FileInput.mouseout();
	}
	function fileOver() {
		var $FileInput = $(this);
		$FileInput.parent("div").attr("class", "fileOuter_over");
	}
	function fileOut() {
		var $FileInput = $(this);
		$FileInput.parent("div").attr("class", "fileOuter");
	}
	function fileChange() {
		var $FileInput = $(this);
		var $Text = $FileInput.prev().children("span");
		$Text.text($FileInput.val());
	}
}
//-------------------------------------
// styleFileInput
//-------------------------------------

var inputWidth = 287;
var inputHeight = 25;

function styleTextInput() {
	var $TextInput = $(this);
	var $label = $TextInput.parent("label");
	$TextInput.focus(textFocus);
	$TextInput.blur(textBlur);

	function textFocus() {
		$(this).toggleClass("inputFocus");
	}
	function textBlur() {
		$(this).toggleClass("inputFocus");
	}
}


//-------------------------------------
// Check and submit form
//-------------------------------------
function checkAndSubmitForm($Container, formFunction, $Div) {
	var errorCount = 0;		
	var $TextInputs = $("input:text, input:password, textarea", $Container)
	$TextInputs.each( function(index) {
		displayErrorText($Div, "")
		displayError($(this), false)
		//
		if(checkForm($(this), $Div)){			
			displayError($(this), true);
			errorCount++;
		}
		if(errorCount>0){
			return false;
		}
	});
	if(errorCount==0){
		eval(formFunction);
	}	
}
function displayError($Input, error){
	if (error) {
		$Input[0].focus();
		$Input[0].select()
		$Input.addClass("error")
		$Input.parent().addClass("labelError")
	} else {
		$Input.removeClass("error")
		$Input.parent().removeClass("labelError")
	}
}
function displayErrorText($Div, error_str){
	if (error_str != "") {
		$Div.text(error_str)
		$Div.show()
	} else {
		$Div.hide()
	}
}
function checkForm($Input, $Div) {
	if ($Input.parent().find("em").length > 0 && $Input.val() == "") {		
		displayErrorText($Div, "Det markerede inputfelt skal udfyldes")
		return true;
	}	
	var RegExEmail= new RegExp("email");
	if (RegExEmail.test($Input.attr("id"))) {
		if (checkEmail($Input.val())) {
			displayErrorText($Div, "Dette er en ugyldig e-mail adresse")
			return true;
		}
	}	
	return false;
}
function checkEmail(txt, msg) {
	var RegExFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (RegExFilter.test(txt)) {
		if (txt.length >= 7) {
			if (txt.indexOf("@") >= 0) {
				if ((txt.indexOf("@") + 2) < txt.lastIndexOf(".")) {
					if (txt.lastIndexOf(".") < (txt.length - 2)) {
						return false;
					}
				}
			}
		}
	}
	return true;
}
//-------------------------------------
// setContactEvents
//-------------------------------------
function setContactEvents(){
	if($('.Contact').length == 0) return
	//
	var $Input = $('.Contact input:text, .Contact textarea');

	var $Button = $(".Contact a");
	$Button.map(function(){
		$(this).attr("mshref",$(this).attr("href").split(":")[1]);
		$(this).attr("href","javascript:void(0)")
	});
	$Button.click(function(event) {
		var $Div = $(".Contact .ErrorText");
		checkAndSubmitForm($(this).parent(), $(this).attr("mshref"), $Div);
	});
}
//-------------------------------------
// setSearchEvents
//-------------------------------------
function setHeaderSearchEvents(){
	var $headerSearch = $('#headerSearch');
	if($headerSearch.length == 0) return
	var $Input = $('#headerSearchInput input', $headerSearch);
	$Input.keypress(function(event) {
		if (event.keyCode == 13) {
			$(this).parent().next().children("a").click();
			return true
		} 
	});
 $("#headerSearchButton a", $headerSearch).click(function(event) {
		window.location.href = "/Soegning/Sider/soeg.aspx?k="+encodeURIComponent($Input.val());
	});
}
//-------------------------------------
// setSearchEvents
//-------------------------------------
function setFindBankEvents(){
	var $Find = $('#findBank');
	if($Find.length == 0) return
	var $Input = $('input', $Find);
	$Input.keypress(function(event) {
		if (event.keyCode == 13) {
			if(GMap2 != null){
				$(this).next().click();
			}else{
				$(this).next().next().click();
			}
			return true
		} 
	});
	var $Button = $("a", $Find);
	$Button.map(function(){		
		$(this).attr("mshref",$(this).attr("href").split(":")[1]);
		$(this).attr("href","javascript:void(0)")
	});
	$Button.click(function(event) {
		eval($(this).attr("mshref"));
	});
}
//-------------------------------------
// setSearchEvents
//-------------------------------------
function setSearchEvents(){
	var $Search = $('#search');
	if($Search.length == 0) return
	var $Input = $('#searchInput input', $Search);
	$Input.keypress(function(event) {
		if (event.keyCode == 13) {
			$(this).parent().next().children("a").click();
			return true
		} 
	});
	var $Button = $("#searchButton a", $Search);
	$Button.map(function(){
		$(this).attr("mshref",$(this).attr("href").split(":")[1]);
		$(this).attr("href","javascript:void(0)")
	});
	$Button.click(function(event) {
		eval($(this).attr("mshref"));
	});
}
//-------------------------------------
// setSearchEvents
//-------------------------------------
function setSearchPaging(){
	var $Next = $('#SRP_Next');
	var $Prev = $('#SRP_Prev')
	if($Next.length > 0){
		$Next.text($Next.text().replace(/>/i, ""));
		$Next.text($Next.text().replace(/&gt;/i, ""));
	}
	if($Prev.length > 0){
		$Prev.text($Prev.text().replace(/</i, ""));
		$Prev.text($Prev.text().replace(/&lt;/i, ""));
	}
}
//-------------------------------------
// styleFormFields
//-------------------------------------
function styleFormFields() {
	// if MOSS console open do nothing
	if($(".ms-consoleframe").length == 0){	
		$("input:checkbox").each(styleInputBoxes);
		$("input:radio").each(styleInputBoxes);
		$("select").each(styleSelectBoxes);	
		$("input:file").each(styleFileInput);
		$("input:text, input:password, textarea").each(styleTextInput);
		//
	}
	setSearchPaging();
	setContactEvents();
	setHeaderSearchEvents();
	setSearchEvents();
	setFindBankEvents();
}






