/*----------------------------------------------------------------------------------------------------------------------------*/

function checkSearchBoxContent()
{
	if (document.getElementById('searchquery').value !=
		document.getElementById('initsearchquery').value) {
		return true;
	}
	return false;
}


function locationChange(variable, value)
{
	var variables = getQueryVariables();
	var queryString = '?';
	var found = false;
	for (i = 0; i < variables.length; i++) {
		if ( variables[i][0] == variable ) {
			variables[i][1] = value;
			found = true;
		}
		if (i == 0) {
			queryString += variables[i][0] + '=' + variables[i][1];
		} else {
			queryString += '&' + variables[i][0] + '=' + variables[i][1];
		}
	}
	if (found == false) {
		if (variables.length > 0) {
			queryString = queryString + '&' + variable + '=' + value;
		} else {
			queryString = queryString + variable + '=' + value;
		}
	}
	window.location = queryString;
}

/* Get something from GET query */

function getQueryVariable(variable) {
  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] == variable) {
      return pair[1];
    }
  }
}

function getQueryVariables() {
  var query = window.location.search.substring(1);
 var pairs = new Array();
  if (query != '') {
	  var vars = query.split("&");
	  for (var i=0; i < vars.length; i++) {
	    var pair = vars[i].split("=");
	    pairs[pairs.length] = pair;
	  }
  }
  return pairs;
}

function initContactType() {
	var val = getGETVar('contact_over');
	if (val != '') {
		document.getElementById('contact_over').value = val;
	}
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}


/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle mouseover for images in topmenu */

function topMenuOver(oImg)
{
	var newSrc = oImg.src.replace('.gif','_act.gif');
	return(newSrc);
}

/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle mouseout for images in topmenu */

function topMenuOut(oImg)
{
	var newSrc = oImg.src.replace('_act.gif','.gif');
	return(newSrc);

}


/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle AJAX requests for newsletters */

function handleNewsletter(vlag,nbid)
{
	if(nbid != 'undefined')
	{
		/* Specific newsletter */
		var url = '/modules/newsletter/newsletter.php?action=newsletter&vlag='+vlag+'&nbid='+nbid;
	}
	else
	{
		/* Overview */
		var url = '/modules/newsletter/newsletter.php?action=overview&vlag='+vlag;

	}

	new Ajax.Updater('newslettercontent', url);
}

/*----------------------------------------------------------------------------------------------------------------------------*/
/* Handle AJAX requests for searches */

function handleSearch(vlag,query,start,target,skipbackcheck)
{

	if(query != '')
	{

	if(!skipbackcheck)
	{

		/* Check if user has pressed back button on browser, to make sure the correct AJAX call is made */
		var currentUrl = document.location.href;
		var currentUrlElements = currentUrl.split('#');
		if ((!isNaN(currentUrlElements[1])) && (!currentUrlElements[1].length == 0))
		{
			var start = currentUrlElements[1];
		}

	} else {

		/* check if target is Down then slide up and return */
		if ($("#" + target).is(":visible") && ( $("#" + target).height() > 0) ) {

			$("#" + target).slideUp();
			return;
		}

	}


	$.ajax({
	   type: "GET",
	   url: "/modules/search/search.php",
	   data: "query="+encodeURIComponent(query)+"&vlag="+vlag+"&start="+start+"&target="+target,
	   success: function(msg){

		 		$("#" + target).html(msg).slideDown();

	   }
	 });


	}

}

function getCookie(Name)
{
	var search = Name + "=";
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

function tjek()
{
	var t = new Date().getTime();

	if (document.inlogformulier.onthoud.checked == true)
	{
		var m = document.inlogformulier.email.value;
		var p = document.inlogformulier.password.value;

		var Verval = new Date();
		Verval.setMonth(Verval.getMonth()+6);

		document.cookie = "onthoudn="+m+"; expires=" + Verval.toGMTString();
		document.cookie = "onthoudp="+p+"; expires=" + Verval.toGMTString();
	}else{
		var Verval = new Date();
		Verval.setMonth(Verval.getMonth()-6);
		document.cookie = "onthoudn=; expires=" + Verval.toGMTString();
		document.cookie = "onthoudp=; expires=" + Verval.toGMTString();
	}

	return true;

}

function laden()
{
	var on = getCookie("onthoudn");
	var op = getCookie("onthoudp");

	if (op && on)
	{
		if(testIsValidObject(document.inlogformulier)){
			document.inlogformulier.emailLogin.value = on;
			document.inlogformulier.passwordLogin.value = op;
			document.inlogformulier.onthoud.checked = true;
		}
		if(testIsValidObject(document.inlogformulier_main)){
			document.inlogformulier_main.emailLogin.value = on;
			document.inlogformulier_main.passwordLogin.value = op;
		}

	}

	// foutmelding tonene in simplemodalbox
	 var fout = getGETVar("fout");

	 if (fout != '')
	 {
	 	 showLoginError('klantLoginFoutMeldingen');
	 }


}

function testIsValidObject(objToTest) {
	if (null == objToTest) {
		return false;
	}
	if ("undefined" == typeof(objToTest) ) {
		return false;
	}
	return true;

}

function getGETVar( name )
{
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+name+"=([^&#]*)";
   var regex = new RegExp( regexS );
   var results = regex.exec( window.location.href );
   if( results == null )
                  return "";
   else    return results[1];
}
