//************************************************************************************
//** Cookie.js Version 1.0                                  				** 
//** Copyright 2000 Ing. B. Heutz  	Berry@LiSAR.com                      		**
//** Created 30/03/00              	Last Modified --/--/--                        	**
//** Scripts used for:           	Online shop WWW.LiSAR.COM		        **
//************************************************************************************
//** COPYRIGHT NOTICE                                                           	**
//** Copyright 2000 Ing. B. Heutz All Rights Reserved.                     		**
//**                                                                            	**
//** Cookie.js may not be used, modified or sell free of charge by anyone without 	**
//** prior written conset is expressly forbidden    					**
//** Using this this code you agree to indemnify Ing. B. Heutz from any     		**
//** liability that might arise from it's use.                                  	** 	 
//**                                                                            	**
//** Selling the code for this program without prior written consent is         	**
//** expressly forbidden. 								**
//**                                                                            	**
//** Obtain permission before redistributing this software over the Internet or 	**
//** in any other medium.  In all cases copyright and header must remain intact.	**
//** 											**
//** More information: Berry@lisar.com							**		
//****************************************************************************************
	
	var Art   = 'xAx';
	var Desc  = 'xDx';	//Constanten voor het zoeken van informatie
	var Price = 'xPx';
	var Porto = 'xOx';

	var today = new Date();
	var expiry = new Date(today.getTime() + 730 * 24 * 60 * 60 * 1000); // plus 2 jaar  
	var NrRecords=0;
	
//************************************************************************************
//** WriteCookie controleert de te nemen actie afhankelijk van de formulierstatus	**
//** t.a.v. het artikel											**
//** 1. naam van het artikel en het aantal te bestellen (identificatie = xAx + naam)**
//** 2. naam van het artikel en een korte omschrijving (identificatie = xDx + naam)	**
//** 3. naam van het artikel en de prijs (identificatie= xPx + naam)             	**
//** 4. Prijs inclusief Porto (identificatie= xOx + naam)		             	**
//************************************************************************************
function PlaceOrder(ID, name, description, Prijs, PTT )
{
       	SetCookie('Artikel', ID);
		SetCookie(Art + ID, name);
		SetCookie(Desc + ID, description);
		SetCookie(Price + ID, Prijs);
		SetCookie(Porto + ID, PTT);
		parent.location = 'Online.htm';
}


//************************************************************************************
//** Definitief wegschrijven van een cookie record.				    **
//************************************************************************************
function SetCookie(name, value)
{
	var CookieVal, CookError;

	CookieVal = CookError = "";
	if (name)
   	{
		CookieVal = CookieVal + escape(name) + '=';
		if (value)
       	{
			CookieVal += escape(value);
			CookieVal += '; expires=' + expiry.toGMTString();
		}
		else	{ CookError = 'LiSAR ' + CookError + 'Value failure';	}
	}
	else	{ CookError = 'LiSAR ' + CookError + 'Name failure';	}

	if (!CookError)
	{
		document.cookie = CookieVal;  	// sets the cookie
		if (value != GetCookie(name)) 	{ CookError = 'Write failure';	}
	}
	return CookError;
}

//************************************************************************************
//** Teruglezen van een Cookie record op basis van de naam.			    **
//************************************************************************************
function GetCookie(name)
{
	var allCookie, cookieVal, length, start, end;

	cookieVal = "";
	name = name + "=";
	allCookie = document.cookie;
	length = allCookie.length;
	if (length > 0)
	{
		start = allCookie.indexOf(name, 0);
		if (start != -1)
		{
			start += name.length;
			end = allCookie.indexOf(";",start);
			if (end == -1)	{ end = length; }
			cookieVal = unescape(allCookie.substring(start,end));
		}
	}
	return(cookieVal);
}


//************************************************************************************
//** Actualiseren van een Cookie record op basis van de naam.			    **
//************************************************************************************
function UpdateCookie(name, value)
{
	if (value != null && value != "") 
       	SetCookie(name, value);
    	else
     	     	DeleteCookie(name);  
}


//************************************************************************************
//** Verwijderen van een Cookie record op basis van de naam.			    **
//** Als alle entrie verwijderd zijn wordt de gehele Cookie automatische ook 	    **
//** verwijderd.								    **
//************************************************************************************
function DeleteCookie (name) 
{
	    var exp = new Date();
	    var cval = GetCookie(name);

	    exp.setTime (exp.getTime() - 1);  // This cookie is history!
	    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


//************************************************************************************
//** Tellen van het aantal geregistreerde artikelen.				    **
//************************************************************************************
function CountRecords()
{
	var allCookie, length, start, intCounter;

	intCounter = start = NrRecords = 0;
	allCookie = document.cookie;
	length = allCookie.length;
	if (length > 0)
	{
		while (start != -1)
		{
			start = allCookie.indexOf(Art, start);
			if (start != -1) 
			{
				intCounter += 1;
				allCookie = allCookie.substring(start+ 3, length);
			}
		}
	}
	NrRecords = intCounter;
	return(NrRecords);
}


//************************************************************************************
//** Weergeven van een decimaal getal met correct opmaak.			    **
//************************************************************************************
function decimal(num) 
{
	string = "" + num;
  	if (string.indexOf('.') == -1)	return string + '.00';

	seperation = string.length - string.indexOf('.');
	if (seperation > 3) 
		return string.substring(0,string.length-seperation+3);
	else if (seperation == 2)
                return string + '0';
    return string;
}

//************************************************************************************
//** Teruglezen van een specifiek Cookie record t.a.v. de order			    **
//************************************************************************************
function GetOrderPart(intPart)
{
	var result, ArtID;
	
	ArtID = GetCookie('Artikel') || 'Artikel niet gevonden!';
	result = ArtID;
	if (intPart == 2) result = GetCookie(Art + ArtID)  || 0;
	if (intPart == 3) result = GetCookie(Desc + ArtID) || '';
	if (intPart == 4) result = GetCookie(Price + ArtID) || 0;
	if (intPart == 5) result = GetCookie(Porto + ArtID) || 0;
	return(result);
}

//************************************************************************************
//** Ophalen van alle orderinformatie en eventueel beschikbaar NAW-gegevens.	    **
//************************************************************************************
function GetOrder()
{
	var rbSexe, intPrice, intPorto;

	dispNr.innerHTML 		= GetOrderPart(1);
	dispAr.innerHTML 		= GetOrderPart(2);
 	dispOm.innerHTML 		= GetOrderPart(3);
	intPrice 			= GetOrderPart(4) || 0;
	intPorto			= GetOrderPart(5) || 0;
 	dispPr.innerHTML 		= decimal(intPrice) + ' euro';
	NewTotal(intPrice, intPorto);
	with (document.Orderform)
	{
		Bestelling.value = GetOrderPart(1) + '/' + GetOrderPart(2);
		naam.value = GetCookie('naam') || '';
		rbSexe = GetCookie('sexe') || '';
		if (rbSexe != '')
		{
			if (rbSexe == 'man')   sexe(1).checked = true;
			if (rbSexe == 'vrouw') sexe(0).checked = true;
		}	
		organisatie.value = GetCookie('organisatie') || '';
 		straat.value = GetCookie('straat') || '';
		nummer.value = GetCookie('nummer') || '';
		postcode.value = GetCookie('postcode') || '';
		woonplaats.value = GetCookie('woonplaats') || '';
		telefoonnummer.value = GetCookie('telefoonnummer') || '';
		faxnummer.value = GetCookie('faxnummer') || '';
		email_adres.value = GetCookie('email_adres') || '';
	}
	Orderform.Aantal.focus();
}


//************************************************************************************
//** Berekenen van nieuw subtotaal als het aantal artikelen wijzigd		    **
//************************************************************************************
function NewTotal(intPrice, intPorto)
{
		var Price, strPorto;
		if (Orderform.Aantal.value != '')
		{
			Price = Orderform.Aantal.value * intPrice;
			if (intPorto == 0) strPorto = ' (incl. BTW, excl. Porto)';
			if (intPorto == 1) strPorto = ' (incl. BTW, incl. Porto)';
			dispTo.innerHTML = 'Totaal: ' + decimal(Price) + ' euro' +  strPorto;
		}
}


//************************************************************************************
//**				         E N D			    		    **
//************************************************************************************
