// JavaScript Document

//subscribe, unsubscribe to newsletter
function afmeldTilmeld(paramAction){
var name = document.forms.afmeldTilmeldForm.umbSubscribeName.value;
var email = document.forms.afmeldTilmeldForm.umbSubscribeEmail.value;
var emailRegexp = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$/;
var isValid = emailRegexp.test(email) && name!="";
//remove if MailChimp will be needed
isValid = false;
if (isValid){
	if (window.XMLHttpRequest)
	{
		xhttp=new XMLHttpRequest();
	}
	else // Internet Explorer 5/6
	{
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	var result;  
	var params = "action=" + paramAction + "&name=" + name + "&mail=" + email;
	xhttp.open("POST","/umbraco/SubUnsub.aspx",true);
	xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhttp.setRequestHeader("Content-Length", params.length);
	xhttp.setRequestHeader("Connection", "close");  

	xhttp.onreadystatechange = function() {
	if(xhttp.readyState == 4 && xhttp.status == 200) {
		document.forms.afmeldTilmeldForm.umbSubscribeMemberAction.value = paramAction;
		document.forms.afmeldTilmeldForm.submit();
		result = xhttp.responseXML;
		}
	}
	xhttp.send(params);
}
else if (!isValid){
	document.forms.afmeldTilmeldForm.umbSubscribeMemberAction.value = paramAction;
	document.forms.afmeldTilmeldForm.submit();
}

}

function subscribeUser(){
	if (document.MM_returnValue == false) return;
	var currentForm = document.forms.bestilKatalog;
	var test = currentForm.modtageinfo;
	if (currentForm.modtageinfo[0].checked==false)
	{
		return;
	}
	var name = currentForm.navn.value;
	var email = currentForm.email.value;
	var emailRegexp = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$/;
	var isValid = emailRegexp.test(email) && name!="";
	if (isValid){
		if (window.XMLHttpRequest)
		{
			xhttp=new XMLHttpRequest();
		}
		else // Internet Explorer 5/6
		{
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		var result;  
		var params = "action=subscribe&name=" + name + "&mail=" + email;
		xhttp.open("POST","/umbraco/SubUnsub.aspx",true);
		xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhttp.setRequestHeader("Content-Length", params.length);
		xhttp.setRequestHeader("Connection", "close");  
		xhttp.send(params);
	}
}


//Jump menu in b2b section
function MM_jumpMenuGo(objId,targ,restore){ //v9.0
  var selObj = null;  with (document) { 
  if (getElementById) selObj = getElementById(objId);
  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0; }
}

//Hide and show medarbejder image
function hideCoworkerImage (theImage){
	//alert ('hi');
	document.getElementById(theImage).src = '/images/medarbejder_over.gif';
}

function showCoworkerImage (theImage, theSrc){
	//alert ('there');
	document.getElementById(theImage).src = theSrc;
}


//Builds form for byggegrund calculator
function populateList(paramSelectedIndex, housetype1Array, housetype2Array, housetype1IDArray, housetype2IDArray){
	
	var housetypeArrayM2 = new Array();
	var housetypeArrayId = new Array();
	
	if (paramSelectedIndex == 1){
		housetypeArrayM2 = housetype1Array.split(',');
		housetypeArrayId = housetype1IDArray.split(',');
	}
	if (paramSelectedIndex == 2){
		housetypeArrayM2 = housetype2Array.split(',');
		housetypeArrayId = housetype2IDArray.split(',');
	}
	
	document.forms['hustypeDropdown'].m2.options.length = 0;
	
	document.forms['hustypeDropdown'].m2.options[0] = new Option('V\346lg','');
	
	for (i=0; i<housetypeArrayM2.length; i++){
		document.forms['hustypeDropdown'].m2.options[i+1] = new Option(housetypeArrayM2[i],housetypeArrayId[i]);
	}
}



//Photo gallery for hustype
var currentImage = 1;
//variables 'imageCount' and 'path' are written in xslt file

function changeImage(paramValue){
		currentImage = Number(currentImage) + Number(paramValue);
		if(currentImage>imageCount){
			currentImage = 1;
		}else if(currentImage<=0){
			currentImage = imageCount;
		}
		document.getElementById('imagePlaceholder').src = path + "/main" + currentImage + ".jpg";
		
		for (i=1; i<imageCount+1; i++){
			document.getElementById('thumbPlaceholder' + i).className  = 'deselectedThumb';	
		}
		document.getElementById('thumbPlaceholder' + currentImage).className  = 'selectedThumb';
}


function changeImageByThumbnail(paramValue){
		currentImage = paramValue;
		document.getElementById('imagePlaceholder').src = path + "/main" + currentImage + ".jpg";
		for (i=1; i<imageCount+1; i++){
			document.getElementById('thumbPlaceholder' + i).className  = 'deselectedThumb';	
		}
		document.getElementById('thumbPlaceholder' + currentImage).className  = 'selectedThumb';	
}


//Rollovers for hustype icons
function hustypeRollover (paramID){
	if(document.getElementById(paramID)) {
		var theHustype = document.getElementById(paramID);
		theHustype.style.background = 'none';
	}
}

function hustypeRollout (paramID){
	if(document.getElementById(paramID)) {
		var theHustype = document.getElementById(paramID);
		if (paramID == 'hustype1anchor'){
			theHustype.style.background = 'url(/images/hustype1.gif) -92px 0 no-repeat';	
		}
		if (paramID == 'hustype2anchor'){
			theHustype.style.background = 'url(/images/hustype2.gif) -133px 0 no-repeat	';
		}
	}
}


//Rollovers for byggegrund maps
function mapRollover (paramID){
	var theMap = document.getElementById(paramID);
	if (paramID == 'byggegrundjyllandanchor'){
		theMap.style.background = 'url(/images/byggegrundKort.gif) -710px 0 no-repeat';	
	}
	if (paramID == 'byggegrundsjaellandanchor'){
		theMap.style.background = 'url(/images/byggegrundKort.gif) -520px -150px no-repeat';	
	}
}

function mapRollout (paramID){
	var theMap = document.getElementById(paramID);
	if (paramID == 'byggegrundjyllandanchor'){
		theMap.style.background = 'url(/images/byggegrundKort.gif) 0 0 no-repeat';	
	}
	if (paramID == 'byggegrundsjaellandanchor'){
		theMap.style.background = 'none';	
	}
}


//Rollovers for medarbejdere
function medarbejderRollover (paramID){
	//alert (document.getElementById(paramID));
	var theMedarbejder = document.getElementById(paramID);
	theMedarbejder.src = '/images/medarbejder_over.gif';	
	//alert (theMedarbejder.src);
	
}

function medarbejderRollout (paramID, correctURL){
	var theMedarbejder = document.getElementById(paramID);
	theMedarbejder.src = correctURL;
}


function mainMenuRollover( id, obj){
	if( document.getElementById( "submenu" + id ) ) {
		document.getElementById( "submenu" + id ).style.background = "url(/images/dropdown/trans-dd-mid.png) repeat-y 0 0";
		document.getElementById( "submenu" + id ).style.width = "200px"
		document.getElementById( "submenu" + id ).style.border = "1px solid #BABABA";
		document.getElementById( "submenu" + id ).style.paddingTop = "5px";
		document.getElementById( "submenu" + id ).style.marginTop = "8px";
	}
}

function mainMenuRolloverIE6( obj ){
	var getULObj = obj.getElementsByTagName( "UL" )
	for (var i=0; i<getULObj.length; i++) {
		// getULObj[i].style.background = "url(/images/dropdown/trans-dd-mid.png) repeat-y 0 0";
		getULObj[i].style.width = "200px"
		getULObj[i].style.border = "1px solid #BABABA";
		getULObj[i].style.paddingTop = "5px";
		getULObj[i].style.marginTop = "8px";
	}
}

function mainMenuRollout( id ){
	// NOTHING
}

//Rollovers for submenus from hustype and map (sommerhuse og byggegrunde)
function remoteSubmenuRollover (paramID, theTriggerObject){

	
	if (theTriggerObject == 'hustype1anchor' || 'hustype2anchor'){
		document.getElementById(theTriggerObject).style.background = 'none';	
	}
	if (theTriggerObject == 'byggegrundjyllandanchor'){
		document.getElementById(theTriggerObject).style.background = 'url(/images/byggegrundKort.gif) -710px 0 no-repeat';	
	}
	if (theTriggerObject == 'byggegrundsjaellandanchor'){
		document.getElementById(theTriggerObject).style.background = 'url(/images/byggegrundKort.gif) -520px -150px no-repeat';	
	}
	
	var theMenuPoint = document.getElementById(paramID);
	theMenuPoint.className = 'selectedBlue';
	
	//alert ('helloo... '+theMenuPoint.className);
}

function remoteSubmenuRollout (paramID, theTriggerObject){
	if (theTriggerObject == 'hustype1anchor'){
		document.getElementById(theTriggerObject).style.background = 'url(/images/hustype1.gif) -92px 0 no-repeat';	
	}
	if (theTriggerObject == 'hustype2anchor'){
		document.getElementById(theTriggerObject).style.background = 'url(/images/hustype2.gif) -133px 0 no-repeat	';
	}
	if (theTriggerObject == 'byggegrundjyllandanchor'){
		document.getElementById(theTriggerObject).style.background =  'url(/images/byggegrundKort.gif) 0 0 no-repeat';	
	}
	if (theTriggerObject == 'byggegrundsjaellandanchor'){
		document.getElementById(theTriggerObject).style.background = 'none';
	}
	
	var theMenuPoint = document.getElementById(paramID);
	theMenuPoint.className = null;
}




//Show and hide video (swf)
function showVideo(){
	var theVideoDiv = document.getElementById("videoPlayer");
	var theMainContentImage = document.getElementById("mainContentImage");
	var theMainContentParagraph = document.getElementById("mainContentParagraph");
	
	//hide the image and paragraphs in the mainContent div
	theMainContentImage.style.visibility = 'hidden';
	theMainContentImage.style.display = 'none';
	
	theMainContentParagraph.style.visibility = 'hidden';
	theMainContentParagraph.style.display = 'none';
	
	//and then show the video div
	theVideoDiv.style.visibility = 'visible';
	theVideoDiv.style.display = 'block';
}

function hideVideo(){
	var theVideoDiv = document.getElementById("videoPlayer");
	var theMainContentImage = document.getElementById("mainContentImage");
	var theMainContentParagraph = document.getElementById("mainContentParagraph");

	//show image and paragraph
	theMainContentImage.style.visibility = 'visible';
	theMainContentImage.style.display = 'block';
	
	theMainContentParagraph.style.visibility = 'visible';
	theMainContentParagraph.style.display = 'block';	

	//and then hide the video div
	theVideoDiv.style.visibility = 'hidden';
	theVideoDiv.style.display = 'none';
	
}



//calculates size of window and puts bottom bar element at bottom of page, or below content if scroll
function calculateBottomBar() {
	
	//gets height and width of inner space of window, crossbrowser
	var theInnerWidth = 0, theInnerHeight = 0, theInnerHalfWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		theInnerWidth = window.innerWidth;
		theInnerHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		theInnerWidth = document.documentElement.clientWidth;
		theInnerHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		theInnerWidth = document.body.clientWidth;
		theInnerHeight = document.body.clientHeight;
	}
	
	//half the width of the inner window
	theInnerHalfWidth = (theInnerWidth)/2;
  
  	//gets the height of the scrollable content on the current page
	var theBodyHeight = 0;
	theBodyHeight = document.getElementById('skanluxBody').offsetHeight;
	
  	//defines the bottom bar in an element
	var theBottomBar = document.getElementById('bottomBar');
	
	//and compares heights of scrollable content vs height of inner screen, and either sets position to relative or fixed to the bottom  
	if (theBodyHeight > theInnerHeight){
		theBottomBar.style.position = 'relative';
		theBottomBar.style.left = null;
		theBottomBar.style.bottom = null;
		theBottomBar.style.top = null;
		theBottomBar.style.visibility = 'visible';
	} else {
		//sets the position of bottombar to absolute to bottom minus 20px (the height of the bottom bar)
		//this method was used instead of position:fixed to comply with ie6
		//Note: bottombar has a fixed width of 886px, half size 443px
		theBottomBar.style.position = 'absolute';
		var theCorrectPosition = theInnerHalfWidth - 443;
		theBottomBar.style.left = theCorrectPosition + 'px';
		theBottomBar.style.top = (theInnerHeight - 20) + 'px'; 
		theBottomBar.style.visibility = 'visible';
	}
	//alert (theInnerHeight +  ' : ' + theBodyHeight + ' : ' + theBottomBar.style.top);
}





function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
if(!document.bestilKatalog.classic.checked && !document.bestilKatalog.aqua.checked){
	errors+='- du skal v\346lge et katalog\n'
}
	for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' Skal v\346re en gyldig email adresse.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' skal v\346re et tal.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' skal udfyldes.\n'; }
  } if (errors) alert('Der opstod fejl:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}


function MM_validateHelaarsForm() {
	message='';
	if (!document.bestilKatalog.navn.value){
		message += 'Navn skal udfyldes.\n';
	}

	if (!document.bestilKatalog.postnummer.value){
		message += 'Postnummer skal udfyldes.\n';
	}

	if (!document.bestilKatalog.telefon.value){
		message += 'Telefon skal udfyldes.\n';
	}

	if (!document.bestilKatalog.email.value){
		message += 'E-mail skal udfyldes.\n';
	}else{
		if(!isEmail(document.bestilKatalog.email.value)){
			message += 'E-mailadressen er udfyldt forkert.\n';
		}
	}

	if (!document.bestilKatalog.adresse.value){
		message += 'Adresse skal udfyldes.\n';
	}

	if (!document.bestilKatalog.by.value){
		message += 'By skal udfyldes.\n';
	}
	if (!document.bestilKatalog.mobil.value){
		message += 'Mobil skal udfyldes.\n';
	}

	if(message==''){
		document.bestilKatalog.submit();
	}else{
		alert(message);
		return false;
	}
}