﻿//Appends a course to the cookie 'courseQue'
//courseType: "4" = long course, "5" = short course, "6" = distance course
//courseId: the courses´ nav_id
function appendCourse(schoolId, courseType, courseId)
{
    if(schoolId == null || schoolId == '' || courseType == null || courseType == '' || courseId == null || courseId == '')
    {
        return;
    }
    if ( confirm('Kursen sparas nu i din egen mapp som\n du hittar i menyraden till vänster.') == true  )
    {   
        if ( typeof(WM_readCookie('courseQue').length) == 'number' )  
        {     
            xold = WM_readCookie('courseQue');     
            WM_killCookie('courseQue');     
            WM_setCookie('courseQue', xold + schoolId + ',' + courseType + ',' + courseId + ';', '', '/');   
        } 
        else  
        {     
            WM_killCookie('courseQue');     
            WM_setCookie('courseQue', schoolId + ',' + courseType + ',' + courseId + ';', '', '/');  
        }   
    }
}

function clearQuestionForm()
{
    try
    {
        document.getElementById('strName').value = '';
        document.getElementById('strMail').value = '';
        document.getElementById('txtQuestion').value = '';
    }
    catch(ex)
    {
        return;
    }
}

function clearOrderForm()
{
    try
    {
        document.getElementById('strName').value = '';
        document.getElementById('strCOAddress').value = '';
        document.getElementById('strAddress').value = '';
        document.getElementById('strPostalAddress').value = '';
        document.getElementById('drpType').selectedIndex = 0;
        document.getElementById('drpQuantity').selectedIndex = 0;
    }
    catch(ex)
    {
        return;
    }
}

function validateOrderForm()
{
    try
    {
        if(document.getElementById('strName').value == '')
        {
            alert('Du måste ange ditt namn.');
            document.getElementById('strName').focus();
            return;
        }
        if(document.getElementById('strAddress').value == '')
        {
            alert('Du måste ange din adress.');
            document.getElementById('strAddress').focus();
            return;
        }
        if(document.getElementById('strPostalAddress').value == '')
        {
            alert('Du måste ange din postadress.');
            document.getElementById('strPostalAddress').focus();
            return;
        }
        if(document.getElementById('drpQuantityFhsk').value == '0' && document.getElementById('drpQuantitySummer').value == '0')
        {
            alert('Du måste välja minst en katalog.');
            return;
        }
        document.getElementById('submitButton').click();
    }
    catch(ex)
    {
        return;
    }
}

function setFontSize(size)
{
    var content = document.getElementById('content');
    content.style.fontSize = size + 'em';
    if (WM_acceptsCookies == true)  {
		WM_setCookie("fntSizeFIN", size, "1000", "/")
	}
	setContentHeight();
}

function sayit()
{
           if (document.getSelection) // older Mozilla versions
           {
                      var selectedString = document.getSelection();
           }
           else if (document.all) // MSIE 4+
           {
                      var selectedString = document.selection.createRange().text;
           }
           else if (window.getSelection) // recent Mozilla versions
           {
                      var selectedString = window.getSelection();
           }
           document.rs_form.rstext.value = selectedString;
           if (window.location.href)
           {
                      document.rs_form.url.value = window.location.href;
           }
           else if (document.location.href)
           {
                      document.rs_form.url.value = document.location.href;
           }
}

function copyselected()
{
setTimeout("sayit()",50);
return true;
}

// Set the height of the content div to minimum the left menus height. This will make the
// background image to span all the way down to the footer.
function setContentHeight()
{
	var docContent = document.getElementById('content');
	if (docContent == null)
	{
		return;
	}

	docContent.style.height = 'auto';
	var contentHeight = docContent.offsetHeight;
	var leftmenuHeight = document.getElementById('leftmenucontainer').offsetHeight;
	if(contentHeight < leftmenuHeight)
	{
		docContent.style.height = leftmenuHeight + 'px';
	}
	else
	{
		docContent.style.height = contentHeight + 'px';
	}
}

// Set both elements (with the id's passed as parameters) to the height
// of the highest of them.
function SynchHeights(firstElementId, secondElementId)
{
    try
    {
        var firstElement = document.getElementById(firstElementId);
        var secondElement = document.getElementById(secondElementId);
        if(firstElement == null || secondElement == null)
        {
            return;
        }
        
//        firstElement.style.height = 'auto';
//        secondElement.style.height = 'auto';
        
        var firstElementHeight = firstElement.offsetHeight;
        var secondElementHeight = secondElement.offsetHeight;
        
        var maxHeight;
        if(firstElementHeight > secondElementHeight)
        {
            maxHeight = firstElementHeight;
        }
        else
        {
            maxHeight = secondElementHeight;
        }        
        if(maxHeight == null)
        {
            return;
        }
        
        firstElement.style.height = maxHeight + 'px';        
        secondElement.style.height = maxHeight + 'px';
        
        return;        
    }
    catch(err)
    {
        return;
    }
}

//function that highlights the words searched for
//(stolen, with some minor adaptions, from the non-serious page: http://www.kryogenix.org/
// original source: http://www.kryogenix.org/code/browser/searchhi/searchhi.js ) 
function highlightWord(node,word) 
{
    // Iterate into this nodes childNodes
    if (node.hasChildNodes) 
    {
        var hi_cn;
        for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) 
        {
	        highlightWord(node.childNodes[hi_cn],word);
        }
    }
	
    // And do this node itself
    if (node.nodeType == 3) 
    { // text node
        tempNodeVal = node.nodeValue.toLowerCase();
        tempWordVal = word.toLowerCase();
        if (tempNodeVal.indexOf(tempWordVal) != -1) 
        {
	        pn = node.parentNode;
	        // check if we're inside a "nosearchhi" zone
	        checkn = pn;
	        
	        //while (checkn.nodeType != 9 && checkn.nodeName.toLowerCase() != 'body') 
	        while (checkn.nodeType != 9 && checkn.id != 'content') 
	        { // 9 = top of doc			            
		        checkn = checkn.parentNode;
	        }
	        if (pn.className != "searchResultHighlight") 
	        {
	            // word has not already been highlighted!
	            nv = node.nodeValue;
	            ni = tempNodeVal.indexOf(tempWordVal);
	            // Create a load of replacement nodes
	            before = document.createTextNode(nv.substr(0,ni));
	            docWordVal = nv.substr(ni,word.length);
	            after = document.createTextNode(nv.substr(ni+word.length));
	            hiwordtext = document.createTextNode(docWordVal);
	            hiword = document.createElement("span");
	            hiword.className = "searchResultHighlight";
	            hiword.appendChild(hiwordtext);
	            pn.insertBefore(before,node);
	            pn.insertBefore(hiword,node);
	            pn.insertBefore(after,node);
	            pn.removeChild(node);
            }
        }
    }
}

function checkForSearchString()
{
    var value = getQueryVariable('highlight');
	if(value == false)
{
	return;
}
    //var arrValues = value.split(' ');
    var arrValues = value.split('+');
    for(var i = 0 ; i < arrValues.length ; i++)
    {
        highlightWord(document.getElementById('content'),arrValues[i]);
    }
}

function getQueryVariable(key) 
{
    var lsRegExp = /\+/g;
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) 
    {
        var pair = vars[i].split("=");
        if (pair[0] == key) 
        {
            return utf8Decode(pair[1]);
        }
    }
    return false;
} 

function utf8Decode (utftext) 
{
	utftext = unescape(utftext);
	 var string = "";
	 var i = 0;
	 var c = c1 = c2 = 0;

	while ( i < utftext.length ) {
		 c = utftext.charCodeAt(i);
		if (c < 128) {
				string += String.fromCharCode(c);
				i++;
		 } else if((c > 191) && (c < 224)) {
			   c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
		 } else {
			   c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			 i += 3;
		  }
	  }
	return string;
}

function checkHiddenHighlight()
{
    try
    {
        var elemHighlight = document.getElementById('hiddenHighlight');
        if(elemHighlight != null)
        {
            var highlightString = elemHighlight.value;
            var highlightStrings = highlightString.split(' ');
            for(var i = 0 ; i < highlightStrings.length ; i++)
            {
                highlightWord(document.getElementById('content'),highlightStrings[i]);
            }
        }
    }
    catch(ex)
    {
        return;
    }
}