﻿
window.onload = function() {
	if(document.getElementById("ctl02_ctl04_form_current")) {
		setFormHeight("ctl02_ctl04_form_current")
	}
	changeInputBtSrc();	
  	
	var orgBtSrc;
}

function openWin(url, wname, width, height) {
	window.open(url, wname, 'scrollbars=1, width='+width+',height='+height);
}


//Fixes all the button styling issues
function changeInputBtSrc() {    
    var allInput = document.getElementsByTagName("input");   
    
	if (!allInput) {return;}
	for (var i = 0; i < allInput.length; i++) {
		//All input buttons
		if(allInput[i].type =="image" && allInput[i].src.indexOf("GoToBasket")==-1 && allInput[i].className == "")  {
		    //addEvent(allInput[i], "mouseover", function() {this.src = "/gfx/netbutik/button-laeg-i-indkobskurv-hover.png"});
		    //addEvent(allInput[i], "mouseout", function() {this.src = "/gfx/netbutik/button-laeg-i-indkobskurv.png"});  
		   
		}
		else if(allInput[i].type =="image" && allInput[i].className == ""){
		    addEvent(allInput[i], "mouseover", function() {this.src = "/gfx/netbutik/button-vis-indkobskurv-hover.png"});
		    addEvent(allInput[i], "mouseout", function() {this.src = "/gfx/netbutik/button-vis-indkobskurv.png"});
		}
		else {
		    switch (allInput[i].className) {
		        case "WSloginBt":
		            addEvent(allInput[i], "mouseover", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-logind-hover.png')"});
		            addEvent(allInput[i], "mouseout", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-logind.png')"});
		            break;
		        case "WSnextBt wrapperR":
		            addEvent(allInput[i], "mouseover", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-next-hover.png')"});
		            addEvent(allInput[i], "mouseout", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-next.png')"});
		            break;
		        case "WSupdateBt":
		            addEvent(allInput[i], "mouseover", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-opdater-hover.png')"});
		            addEvent(allInput[i], "mouseout", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-opdater.png')"});
		            break;
		        case "WSbasketInputBt":
		            break;	
		        case "WSapproveOrderBt":	
		            addEvent(allInput[i], "mouseover", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-godkend-betaling-hover.png')"});
		            addEvent(allInput[i], "mouseout", function() {this.style.backgroundImage = "url('/gfx/netbutik/button-godkend-betaling.png')"});
		            break;		
		        default:
            	    if (allInput[i].name){
                        if (allInput[i].name.indexOf("ctl02$ctl0") != -1 && allInput[i].type == "submit" && allInput[i].name.indexOf("Search")==-1) {
                             allInput[i].className = "btStyle";
                             //allInput[i].style.backgroundImage = "url('/gfx/button-red.png')";
                             addEvent(allInput[i], "mouseover", function() {this.style.backgroundImage = "url('/gfx/button-red-hover.png')"});
                             addEvent(allInput[i], "mouseout", function() {this.style.backgroundImage = "url('/gfx/button-red.png')"});
            		   
                        }
                    }                              
		    }
		}
	}
}


//Fix the height bug in all SC forms
function setFormHeight (id) {
	var startDiv = document.getElementById(id);
	var divHeight = 0;
	var divLength = startDiv.childNodes.length
	var contatinerDiv;
	var lastElem;
	var lastPos = 0;

	//Loop through all the childNodes of the start node (this number differ between browsers)
	for (var i=0; i < divLength; i++) {
		//If the node is an HTML item
		if(startDiv.childNodes[i].nodeType == 1) {		
			//Set the containerDiv start node for the next loop
			contatinerDiv = startDiv.childNodes[i]			
			//Loop through the div that contains all the form items
			for (var x=0; x < contatinerDiv.childNodes.length; x++) {
				//Again we check for the HTML elements
				if(contatinerDiv.childNodes[x].nodeType == 1) {
					//Check if the element top is greater than lastPos					
					if (Number(contatinerDiv.childNodes[x].style.top.substring(0,contatinerDiv.childNodes[x].style.top.indexOf("px"))) > lastPos) {
					    //Set the lastPos to the element top
					    lastPos = Number(contatinerDiv.childNodes[x].style.top.substring(0,contatinerDiv.childNodes[x].style.top.indexOf("px")))
					    //Set the element with the highest top to be the lastElem
					    lastElem = contatinerDiv.childNodes[x]
				    }
				}
			}
		}
	}
   	//Set the divHeight to be the sum of the last elements offsetHeight and the last elements top style (which is in px and needs to be truncated)
	divHeight = Number(lastElem.offsetHeight) + Number(lastElem.style.top.substring(0,lastElem.style.top.indexOf("px")));
	//Set the height of the overall div to our calcutaled divHeight (plus 10px padding for safari)
	startDiv.style.height=divHeight + 10+ "px";
}

function changetype(dtype) {
    document.getElementById(dtype).checked=true;
    document.getElementById('typeform').submit();
}

function changeyear(url, dtype) {
    location.href=url + '?year=' + document.getElementById('selectyear').value;
}

function openMailLink(url) {

	window.open(url, "Tipenven", "width=405, height=335");
 	return false;
}

jQuery(document).ready(function(){
    var version = jQuery.browser.version; 
    if (version instanceof Object) version=version.number(); 
    // Fix PNG bugs on IE6/5 
    if (jQuery.browser.msie && version < 7) { 
         $("img[@src$=png]").pngfix();
    }   
});

function searchrecipe(id) {
    location.href = "/Velvaerekokkenet/sog.aspx?searchrecipe=" + urlencode(document.getElementById(id).value); 

}

function searchadvice(id) {
    location.href = "/Raadgivning/sog.aspx?searchadvice=" + urlencode(document.getElementById(id).value); 

}
function urlencode(str) {
str = str.replace(/å/gi, '%c3%a5');
str = str.replace(/æ/gi, '%c3%a6');
str = str.replace(/ø/gi, "%c3%b8");
str = str.replace(/Å/gi, '%c3%85');
str = str.replace(/Æ/gi, '%c3%86');
str = str.replace(/Ø/gi, '%c3%98');
return str;
}


//Adds an event on an object
function addEvent( obj, type, fn ) { 

	if ( obj.attachEvent ) { 
		obj['e'+type+fn] = fn; 
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
		obj.attachEvent( 'on'+type, obj[type+fn] ); 
	} 
	else 
		obj.addEventListener( type, fn, false ); 
} 

//Removes an event on an object
function removeEvent( obj, type, fn ) { 
	if ( obj.detachEvent ) { 
		obj.detachEvent( 'on'+type, obj[type+fn] ); 
		obj[type+fn] = null;
	} 
	else 
    		obj.removeEventListener( type, fn, false ); 
} 

function removeClassName (elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}

function addCSSClass (elem, className) {
	removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
}

function AddToCart(url, id, quantity, query)
{
        location.href= url + "?id=" + id + "&quantity=" + quantity + "&query=" + query;
}

function submitenter(myfield,e)
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {

   searchrecipe(myfield.id);
   return false;
   }
else
   return true;
}

function submitEnterAdvice(myfield,e)
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {

   searchadvice(myfield.id);
   return false;
   }
else
   return true;
}

function getFormElements(id) {
    if (document.getElementById(id)) {
        var container = document.getElementById(id);
        var allInput = container.getElementsByTagName("input");  
        var qs; 
    
	        if (!allInput) {return;}
            for (var i = 0; i < allInput.length; i++) {
                if(allInput[i].type == "radio" && allInput[i].checked == true) {	                 
                    qs = allInput[i].value
                }
                else if (allInput[i].type == "text" && allInput[i].value != ""){
                    qs = allInput[i].value
                }
            }  
           if (qs == undefined) {
                alert("Husk at vælge/indtaste et beløb");
           }
           else {
                window.location= "/global/giv_et_bidrag.aspx?amount=" +  qs;
           }
    }
}


function sendForm() {
      var formElem = document.forms[0]
      var qs= document.forms[0].from.value
      var qs2= document.forms[0].til.value
      window.location= "/global/eksport.aspx?from=" +  qs + "&til=" + qs2;
    }
