// Begin global variables
var expdate = new Date ();
var expdatepast = new Date ();
var expdateHomePage = new Date ();
// Set expdate to Current date plus 1 day
expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 1));
// Set expdatepast to Current date minus 1 year		
expdatepast.setTime (expdatepast.getTime() - (31536000000));
// Set expdateHomePage to Current date plus 1 hour
expdateHomePage.setTime (expdateHomePage.getTime() + (1000 * 60 * 30 * 1 * 1));

var strID;
var strIDCont;
//De uitzonderingen worden niet meegeteld bij de controle van het aantal van de variabele maxPakket. Duitsland wordt nl. niet meegeteld.
arrayUitzonderingen = new Array("", "");
//Soms hebben vvv's een combinatie van meerdere folders, maar telt het gezamenlijke pakket als 1 kruis.
//Onderstaande array is in de functies setCounter en delCookie ingebouwd, zodat een gezamenlijk pakket van meerder couponnrs in de website als 1 kruisje wordt gezien.
//code groepskruis
arrayGroepsKruis1 = new Array("C0022","C0023","C0024","C0025");
//arrayGroepsKruis2 = new Array("C0011","C0012", "C0013");
//arrayGroepsKruis3 = new Array("C0021","C0022", "C0023");
maxPakket = 9;
// End global variables


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i>d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}


MM_reloadPage(true);

function in_array(stringToSearch) {
	    arrayToSearch = arrayUitzonderingen;
	    
            for (s = 0; s < arrayToSearch.length; s++) {
                        thisEntry = arrayToSearch[s].toString();
                        if (thisEntry == stringToSearch) {
                                   return true;
                        }
            }
            return false;
}

function in_arrayGroepskruis(stringToSearch, array) {
	    arrayToSearch = array;
	    
            for (s = 0; s < arrayToSearch.length; s++) {
                        thisEntry = arrayToSearch[s].toString();
                        if (thisEntry == stringToSearch) {
                                   return true;
                        }
            }
            return false;
}

function setCookie (name, value, expires) {
		if (!expires) 
		{
			expires = new Date();
		}
			document.cookie = name + "=" + escape (value) +  "; expires=" + expires.toGMTString() +  "; path=/"; 
        }                                                       

function getCookie (name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
    }
    
function addGroepsKruis(counterGroepsKruis)
{
	//alert(counterGroepsKruis);
	var getid = null;
	var getid = getCookie(counterGroepsKruis);
	//alert(getid);
	if (!getid)
	{
		counter = 1;  //the value for the new cookie if value does not exist
		setCookie(counterGroepsKruis, counter , expdate);
		//alert(counterGroepsKruis + ' = ' + counter + 'xx');
		return true;
	}
	else
	{
		// increment the counter 
		if (getid) 
		{
			counter = parseInt(getid) + 1;
			setCookie(counterGroepsKruis, counter , expdate);	
			//alert(counterGroepsKruis + ' = ' + counter + 'YY');
		}	
		return false;
	}
}

function extractGroepsKruis(counterGroepsKruis)
{
		// decrement the counter 
		var getid = null;
		var getid = getCookie(counterGroepsKruis);
		if (getid) 
		{
			var counter = parseInt(getid);
			if (counter > 1)
			{
				counter = counter -1;
				setCookie(counterGroepsKruis, counter , expdate);
				//alert( 'counterGroepsKruis ' + counterGroepsKruis + ' = ' + counter);
				return false;

			}
			else
			{
				setCookie(counterGroepsKruis, null , expdatepast);
				//alert( 'counterGroepsKruis ' + counterGroepsKruis + ' = ' + counter);				
				return true;
			}
		}	

}

function setCounter() 
{
	var counter = getCookie("kruisjes");
	//alert('mijncounter = ' + counter);	
	
//Code groepskruis
	var groepsKruis1 = null;
	if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis1))
	{
		addGroepsKruis("GroepsKruis1");
		var _tmp = getCookie("GroepsKruis1");
		var groepsKruis1 = parseInt(_tmp);	
		//alert('_tmp = ' + groepsKruis1);
	}
	//var groepsKruis2 = null;
	//if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis2))
	//{
	//	addGroepsKruis("GroepsKruis2");
	//	var _tmp = getCookie("GroepsKruis2");
	//	var groepsKruis2 = parseInt(_tmp);	
	//		alert('_tmp = ' + groepsKruis2);
	//	 
	//}
	//var groepsKruis3 = null;
	//if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis3))
	//{
	//	addGroepsKruis("GroepsKruis3");
	//	var _tmp = getCookie("GroepsKruis3");
	//	var groepsKruis3 = parseInt(_tmp);	
	//		alert('_tmp = ' + groepsKruis3);
	//	 
	//}
	
	
	//alert('xgroepsKruis1 =' + groepsKruis1 );
	//alert('xgroepsKruis2 =' + groepsKruis2 );
	//alert('xgroepsKruis3 =' + groepsKruis3 );
	//
	//alert('xcounter = ' + counter);
	
	var bMaxPakketGroepsKruis = null;
//Code groepskruis	
	//if ((counter == maxPakket && groepsKruis1 > 1) || (counter == maxPakket && groepsKruis2 > 1)|| (counter == maxPakket && groepsKruis3 > 1))
	if (counter == maxPakket && groepsKruis1 > 1)
	{
		bMaxPakketGroepsKruis = true;
	}
	
	if (counter < maxPakket || bMaxPakketGroepsKruis  ) 
	{
		//alert('dd' + counter);
		    
		    if (!counter) 
			{
				counter = 1; // the value for the new cookie if value does not exist
			} 
	    		else 
			{
				// increment the counter 
				var getid = null;
				var getid = getCookie(strID);
	
	
				if (! getid) 
				{
					counter = parseInt(counter) + 1;
				}	
	
			}		
			
			setCookie(strID,strIDCont, expdate);
			//Only increment counter if getid does not contain German advertenties
			//In arrayUitzonderingen are the exceptions which are not included in the total.
		    //alert(strID);
		    //alert(strIDCont);
		    if(!(in_array(strIDCont))) 
		    {
//Code groepskruis
			//Alleen verlagen als het een eerste aanvraag is van een combinatie van folders die tellen als 1	    	
			if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis1) && (groepsKruis1>1))
			{
				//alert('counter -1 bij groepskruis1');
				counter = counter -1 ;
			}
//			if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis2) && (groepsKruis2>1))
//			{
//				//alert('counter -1 bij groepskruis2');
//				counter = counter -1 ;
//			}
//			if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis3) && (groepsKruis3>1))
//			{
//				//alert('counter -1 bij groepskruis3');
//				counter = counter -1 ;
//			}						
			
			setCookie("kruisjes", counter , expdate);		
			//alert('kruisjes is nu = ' + counter);
		    }
		  } 
	  	else 
	    {
	     alert ("U kunt maximaal " + maxPakket + " informatiepakketten selecteren.\nUw laatste keuze wordt ongedaan gemaakt.\n U kunt naar de coupon gaan om uw aanvraag te versturen.");
//Code groepskruis
	     if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis1))
	     {
	     	extractGroepsKruis("GroepsKruis1");
	     }
//	     if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis2))
//	     {
//	     	extractGroepsKruis("GroepsKruis2");
//	     }
//	     if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis3))
//	     {
//	     	extractGroepsKruis("GroepsKruis3");
//	     }     
	     var check = "document.coupon." + strIDCont + ".checked = false";
	     eval(check);
	    }
}

function delCookie() 
{
	var delid = getCookie (strID);
	if (delid) 
	{
		setCookie(strID,null,expdatepast);

		if(!(in_array(strIDCont))) 
	    {
			var delkruis = getCookie("kruisjes");
//Code groepskruis
			if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis1) && (! extractGroepsKruis("GroepsKruis1")))
			{
				//alert('groepskruis1 = 1');
			}
//			else if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis2)&& (! extractGroepsKruis("GroepsKruis2")))
//			{
//				//alert('groepskruis2 = 1');
//			}	
//			else if (in_arrayGroepskruis(strIDCont, arrayGroepsKruis3)&& (! extractGroepsKruis("GroepsKruis3")))
//			{
//				//alert('groepskruis3 = 1');
//			}
			else
			{
				//alert('check');
				delkruis = delkruis -1;
			}						
			
			setCookie("kruisjes", delkruis , expdate);
			//alert(delkruis);
		}
	}
	if (delkruis==0) 
	{
		//alert('delkruis cookie weg');
		setCookie("kruisjes", null , expdatepast);
	}
}  

function delCookieAll() 
{
    var aantal = 1;
    while (aantal < 70) 
	{
		if(aantal <10)                       
       	{
			var delid = getCookie ("id0" +aantal);
			if (delid) 
			{
			    setCookie("id0" +aantal, null,expdatepast);			
    		}
		}
		else
		{
	   		var delid = getCookie ("id" +aantal);
			if (delid) 
			{
			    setCookie("id" +aantal, null,expdatepast);			
    		}
	   	}
		++aantal;
	}
	var delkruis = getCookie("kruisjes");
    	if (delkruis) 
		{
			setCookie("kruisjes", null , expdatepast);
    	}
	delCookiehomepage();
	delCookieGroepsKruis();
    
}

function delCookieGroepsKruis()
{
	if (getCookie("GroepsKruis1"))
	{
		setCookie("GroepsKruis1", null, expdatepast);
	}
	if (getCookie("GroepsKruis2"))
	{
		setCookie("GroepsKruis2", null, expdatepast);
	}	
	if (getCookie("GroepsKruis3"))
	{
		setCookie("GroepsKruis3", null, expdatepast);
	}	
}

function delCookiehomepage()
	{
		var delhomepage = getCookie("homepage")
		if (delhomepage)
		{
			setCookie("homepage",null,expdatepast);
		}
	}
function setCookiehomepage()
	{
		setCookie("homepage",true,expdateHomePage);
	}

function testAanvraag()
{
	var delkruis = getCookie("kruisjes");
	if (delkruis) 
		{
				window.open('http://www.vakantie-informatie.nl/htm_diverse/uwaanvraag.htm', 'popup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=575,height=350,left = 512.5,top = 425');
				//window.open('http://www.vakantie-informatie.nl/test/htm_diverse/uwaanvraag.htm', 'popup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=575,height=350,left = 512.5,top = 425');				
				
		}

}

//Deze functie wordt aangeroepen binnen uwaanvraag.htm
//Indien er geen kruisjes meer zijn, omdat de aanvraag is verstuurd, wordt de popup gesloten.
//De popup is alleen aanwezig, indien er op refresh is geklikt. De andere clicks worden afgevangen met leaving=false
//De functie wordt aangeroepen bij de body onload en bij de onFocus. Na de onLoad wordt om de 10 seconden gecontroleerd of er nog kruisjes bestaan, anders wordt het window gesloten.
//Indien de popup de focus krijgt, vindt er direct een controle plaats en niet na 10 seconden.
function testAanvraagPopup()
{
		var delkruis = getCookie("kruisjes");
		//alert(delkruis);
		if (!delkruis)
		{
			window.close();
		}
		else
		{
			//Check om de 10 seconden of de popup nog nodig is....
			setTimeout('testAanvraagPopup()',10000)
		}
}		


