
function GotoNavSearch() {

	if (document.getElementById("search-field").value != '')
		location.href = webRoot + "/Archief/Zoek/Index.aspx?trefwoord=" + encodeURI(document.getElementById("search-field").value); 	
	else
		alert('Trefwoord is niet gespecifieerd');
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function isDateFromString(value) {
    return (!isNaN(new Date(value).getYear()));
}

function isDate(dd,mm,yyyy) 
{
	if (yyyy.length != 4)
		return false;
	var d = new Date();
	d.setYear(yyyy);
	d.setMonth(mm-1);
	d.setDate(dd);      
	return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
}

function Right(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function GoToInnerSearch() 
{ 
	GotoAdvancedSearch( true );
}

function GotoAdvancedSearch( isInner ) { 

	var url = "";
	var trefwoord = "";
	
	var wanneer;
	var oVan, oTot;

	for(i = 0; i < document.searchFrm.elements.length; i++)
	{
		var control = document.searchFrm.elements[i];
	
		if (Right(control.name, 12).toLowerCase() == 'txttrefwoord')
			trefwoord = control.value;
		else if (Right(control.name, 12).toLowerCase() == 'chkfindexact') {
			if (control.checked)
				url = url + '&zoekExact=1';
		} 
		else if (Right(control.name, 3).toLowerCase() == 'wat' && control.checked)
			url = url + '&wat=' + control.value;	
		else if (Right(control.name, 4).toLowerCase() == 'waar' && control.checked)
			url = url + '&waar=' + control.value;
        else if (Right(control.name, 10).toLowerCase() == 'ddlsection')
			url = url + '&sektie=' + control.value;	
		else if (Right(control.name, 7).toLowerCase() == 'wanneer' && control.checked)
			wanneer = control.value;
        else if (Right(control.name, 6).toLowerCase() == 'txtvan')
            oVan = control.value;
		else if (Right(control.name, 6).toLowerCase() == 'txttot')
		    oTot = control.value;
		else if (Right(control.name, 7).toLowerCase() == 'cbintro')
			url = url + '&intro=' + ( control.checked ? '1' : '0' );
	}

	if (wanneer == 4) {
	    if (isDateFromString(oVan))
			url = url + '&datumVan=' + oVan;
		else
		{
			alert("Ongeldige 'van' datum voor tijdsperiode.");
			return;			
		}

		if (isDateFromString(oTot))
			url = url + '&datumTot=' + oTot;
		else
		{
			alert("Ongeldige 'tot' datum voor tijdsperiode.");
			return;			
		}
	}

//	if (isInner)
//	{
//		var prev = document.getElementById( 'parentSearchKey' ).value + '|' + document.getElementById( 'parentSearchTerm').value;
//				
//		url = url + '&inner=' + encodeURIComponent( encode64(prev) );
//	}

	if (trefwoord != '')
		location.href = webRoot + "/Archief/Zoek/index.aspx?trefwoord=" + encodeURIComponent(trefwoord) + "&wanneer=" + wanneer + url; 
	else
		alert( 'Trefwoord is niet gespecifieerd.' );
}

function toggle( element )
{
	if ( element != null )
	{
		if ( element.style.display == 'none' )
			element.style.display = 'inline';
		else
			element.style.display = 'none';
	}
}

function toggleSearchIntros()
{
	var isVisible = true;
	var list = document.getElementsByTagName( "p" );
	
	for ( i = 0; i < list.length; i++ )
	{
		var search = list[i].getAttribute( 'search' );
		
		if ( search == 'intro' )
		{
			if ( list[i].style.display == 'none' ) {
				isVisible = true;
				list[i].style.display = 'block';
			}
			else {
				isVisible = false;
				list[i].style.display = 'none';
			}
		}	
	}
	
	var pagers = document.getElementById( 'searchPagers' );
	
	if ( pagers != null )
	{
		for ( i = 0; i < pagers.childNodes.length; i++ ) {
			if ( pagers.childNodes[i].tagName == 'A' ) {
				if ( isVisible )
					pagers.childNodes[i].href = pagers.childNodes[i].href.replace( /intro=0/, "intro=1" );
				else
					pagers.childNodes[i].href = pagers.childNodes[i].href.replace( /intro=1/, "intro=0" );
			}
		}
	}
}


function toggleSearchOptions()
{
	toggle( document.getElementById( 'searchOptions' ) );
	toggle( document.getElementById( 'searchOptionsQuick' ) );
	
	var lnk = document.getElementById( 'searchOptionsLink' );
	
	if ( lnk.innerText == 'uitgebreid zoeken' )
		lnk.innerHTML = 'verberg opties';
	else
		lnk.innerHTML = 'uitgebreid zoeken';
}
