﻿function gotoURL(pUrl,indexNum) {
		if (pUrl != '') { 
			url = new String(pUrl);
			window.location.href = url;
	}
}

/**/

function openTipEnVenPopup(pUrl, pTitle)
{
    var winl = (screen.width-400)/2;
    var wint = (screen.height-300)/2;
    var settings = "width=450,height=300" + ",top=" + wint + ",left=" + winl;
    window.open(pUrl, '', settings);

}

/*Search */

function getSearch(){
    document.forms[0].__VIEWSTATE.disabled = true;
    document.forms[0].quickSearchBtn.disabled = true;
    document.mainform.method="get";
    document.mainform.submit();



/*
    document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
    document.forms[0].__VIEWSTATE.value = '';
    document.forms[0].NOVIEWSTATE.disabled = true;
    document.forms[0].quickSearchBtn.disabled = true;
    document.mainform.method="get";
    document.mainform.submit();
    */
}

function doSearch(){

    document.forms[0].__VIEWSTATE.disabled = true;
    document.forms[0].quickSearchBtn.disabled = true;
    document.forms[0].btnG.disabled = true;
    document.mainform.method="get";
    
   

    
    var q = '?q=';
    if(qBox != null){
        q += encodeURI(qBox.value);
    }
    var url = '/search.aspx';
    
    // check if it's advanced search or not
    var el = document.getElementById("advancedSearch");
    var display = el.style.display;
    // simple search
    if(display == "none"){
        // the query words
        var qBox = document.getElementById('q');
        
        if(qBox != null){
            q += encodeURI(qBox.value);
        }
       
        url += q;
        // HACK, dont show files in normal search
       //url += '&as_filetype=pdf&as_filetype=doc&as_filetype=ps&as_filetype=ppt&as_filetype=xls&as_filetype=rtf'
        
        document.location = url;
        return;
    }
    
    // advanced search takes search words from as_q
    var as_q = document.getElementById('as_q');
    
    q += encodeURI(as_q.value);
    url += q;

    
    // partial fields area, infomationtype, category, 
    var pf = '';
    pf = addFilterParam(pf, 'mField', '.');
    pf = addFilterParam(pf, 'mInformationType', '.');
    pf = addFilterParam(pf, 'mCategory', '.');
    
    /*
    // partial fields keywords
    var mKeys = document.mainform.mKeywords;    
    if(mKeys != null){
        for(i = 0; i < mKeys.length; i++){
            if(mKeys[i].checked){
                pf += encodeURI((pf != '') ? '.' + mKeys[i].value : mKeys[i].value);
            }
        }
    }
    */
    
    if(pf != ''){
        pf = '&partialfields=' + pf;
    }   

    var rf = '';
    rf = addFilterParam(rf, 'mInstitutions', '.');
    if(rf != ''){
        rf = '&requiredfields=' + rf;
    }
    

        
    url += rf + pf;
    
    // add exclusion options if any
    var getFields = '';
    // area (mField)
    var as_Area = document.getElementById('as_ar');
    if(as_Area != null && as_Area.options[1].selected){
        url += '&as_ar=' + encodeURI(as_Area.value);
        getFields = 'field';
    }
    // information type
    var as_it = document.getElementById('as_it');
    if(as_it != null && as_it.options[1].selected){
        url += '&as_it=' + encodeURI(as_it.value);
        getFields = (getFields == '') ? 'informationtype' : getFields + '.informationtype';
    }
    if(getFields != ''){
        url += '&getfields=' + getFields;
    }
    
    // exact words
    var as_epq = document.getElementById('as_epq');
    if(as_epq != null && as_epq.value != ''){
        url += '&as_epq=' + encodeURI(as_epq.value);
    }
    
    
    // one of the words at least
    var as_oq = document.getElementById('as_oq');
  
    if(as_oq != null && as_oq.value != ''){
        url += '&as_oq=' + encodeURI(as_oq.value);
        
    }
    
    // and without the words
    var as_eq = document.getElementById('as_eq');
    if(as_eq != null && as_eq.value != ''){
        url += '&as_eq=' + encodeURI(as_eq.value);
    }
    
    // filetype filtering
    var as_ft = document.getElementById('as_ft');
    if(as_ft != null && as_ft.value != ''){
        url += '&as_ft=' + encodeURI(as_ft.value);
    }
   
    
    var as_filetype = document.getElementById('as_filetype');
    if(as_filetype != null && as_filetype.value != ''){
        url += '&as_filetype=' + encodeURI(as_filetype.value);
    }
  
    
    // the number of results in page
    var num = document.getElementById('num');
    if(num != null){
        url += '&num=' + num.value;
    }
    
    
    
   
    
    document.location = url;
    
//    document.mainform.submit();
}

function addFilterParam(qs, objId, separator){
    var obj = document.getElementById(objId);
    if(obj != null && obj.value != ''){
        if(qs != ''){
            qs += separator;
        }
        return qs + encodeURI(obj.value);
    }
    else{
        return qs;
    }
}

function appendParam(input, str, separator){
    return (pf != '') ? pf + separator + str : str;
}

function quickSearch(query){
    var query = encodeURIComponent(query);

    /*
    if(category != undefined){
    var cat = encodeURIComponent("&category="+category);
    }
    */
    if(query != 'Søgeord'){
        location.href = '/search.aspx?q=' + query;
    }
    
    
}
function quickSearchInstitution(query,site){
    var query = encodeURIComponent(query);
    var site = encodeURIComponent(site);
    if(query != 'S%C3%B8geord'){
        location.href = '/search.aspx?q=' + query+"&requiredfields=site:"+site+"&layout=institution";
    }
}
function quickSearchNews(query,keyword1,keyword2){
    

    var q = query;
    var pf = '';
    if(keyword1 !=''){
        pf = 'field:'+keyword1;
    }
    if(keyword2 !=''){
        if(pf != ''){pf += '|';}
        pf += 'informationtype:'+keyword2;
    }    
    
    var category = 'Category:Nyheder';
    var query = encodeURIComponent(q+"&requiredfields="+category+"&partialfields="+pf)+"&sort=date:D:S:d1&layout=news";
    location.href = '/search.aspx?q=' + query;
}
function checkEnter(e,caller) //e is event object passed from function invocation
{

	var characterCode //literal character code will be stored in this variable

	if(e && e.which ||e.which == 0)
	{ //if which property of event object is supported (NN4)
	
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)//if generated character code is equal to ascii 13 (if enter key)
	{ 
	    if(document.all){
		    e.returnValue=false;
            e.cancel = true;
        }
        else{
         e.preventDefault();
        }
        var obj = document.getElementById(caller);
        if(obj){        
            
            if(obj.click){
		        obj.click();
		    }
		}
		return false
	}
	else
	{
		return true
	}
		
}

// clear form fields
function clearDefault(el) {
    var defaultValue = "Søgeord";
    if (defaultValue==el.value){el.value = "";}
}

function toggleAdvancedSearch() {
    var el = document.getElementById("advancedSearch");
    var searchFormNormal = document.getElementById('searchFormNormal');
    var lnkSimpleSearch = document.getElementById('lnkSimpleSearch');
    var lnkAdvancedSearch = document.getElementById('lnkAdvancedSearch');
    
    var display = el.style.display;
    
    if (el.style.display != "none"){
       el.style.display="none";
       searchFormNormal.style.display = "block";
       
       lnkSimpleSearch.style.display = "none";
       lnkSimpleSearch.style.visibility = "hidden";
       
       lnkAdvancedSearch.style.display = "block";
       lnkAdvancedSearch.style.visibility = "visible";
    }
    else{
        el.style.display="block";
        searchFormNormal.style.display = "none";
        
       lnkSimpleSearch.style.display = "block";
       lnkSimpleSearch.style.visibility = "visible";
        
       lnkAdvancedSearch.style.display = "none";
       lnkAdvancedSearch.style.visibility = "hidden";

    }
}

function switchInstitutions(index){
if(index != 0){
    document.forms[0].requiredfields.disabled = true;
    for (i=0;i<document.forms[0].partialfields.length;i++) {
	    document.forms[0].partialfields[i].disabled = true;
	}    
    document.forms[0].institutions.name = "requiredfields";
}
else{
    document.forms[0].requiredfields.name = "institutions";
    document.forms[0].requiredfields.disabled = false;
    for (i=0;i<document.forms[0].partialfields.length;i++) {
	    document.forms[0].partialfields[i].disabled = false;
	}    
   
}
}
