var dtExpireDate = new Date();
var iDuration = 180;
var sDomain = 'lace-tapestries.com';
var sPath = '/';

dtExpireDate.setTime(dtExpireDate.getTime() + (iDuration*86400000));

function InitCookie() {
   // sUserName = prompt("Please enter your id: ");
   WriteToCookie('UserName', '--visitor--', dtExpireDate,sPath,sDomain);
   WriteToCookie('NumberOfVisits',0,dtExpireDate,sPath,sDomain);
   WriteToCookie('Language','en',dtExpireDate,sPath,sDomain);
   //WriteToCookie('DisplayFormat','2x10',dtExpireDate,sPath,sDomain);
   WriteToCookie('CntInBasket',0,dtExpireDate,sPath,sDomain);
   WriteToCookie('sid',0,null,sPath,sDomain);  // geen expire !
   WriteToCookie('sid2',1,dtExpireDate,sPath,sDomain);
   history.go(0);
}

function GetUserName() {
   var CookieValue = ReadFromCookie('UserName')
   if (CookieValue == null) {
      CookieValue = '--visitor--';
      WriteToCookie('UserName', CookieValue, dtExpireDate,sPath,sDomain);
   }
   return CookieValue;
}

function GetNumberOfVisits() {
   var i = ReadFromCookie('NumberOfVisits')
   if (i == null)
      i = 0;
   else
      i++;
   WriteToCookie('NumberOfVisits',i,dtExpireDate,sPath,sDomain);
   return i;
}

function GetLanguage() {
   var s = ReadFromCookie('Language')
   if (s == null)
      s = '--';
   WriteToCookie('Language',s,dtExpireDate,sPath,sDomain);
   return s;
}

function GetDisplayFormat() {
   var i = ReadFromCookie('DisplayFormat')
   if (i == null)
      i = "2x10";
   WriteToCookie('DisplayFormat',i,dtExpireDate,sPath,sDomain);
   return s;
}

function GetCntInBasket() {
   var i = ReadFromCookie('CntInBasket')
   if (i == null)
      i = 0;
   WriteToCookie('CntInBasket',i,dtExpireDate,sPath,sDomain);
   return i;
}
function WriteCntInBasket(){ 
   var iCnt = GetCntInBasket();
   document.write("(" + iCnt + ") ");
}      

function GetIsLoggedOn() {
   var i = ReadFromCookie('IsLoggedOn')
   if (i == null)
      i = 0;
   WriteToCookie('IsLoggedOn',i,null,sPath,sDomain);
   return i;
}


function GetSid() {
   var i = ReadFromCookie('sid')
   if (i == null)
      i = 0;
   WriteToCookie('sid',i,null,sPath,sDomain);
   return i;
}

function GetSid2() {
   var i = ReadFromCookie('sid2')
   if (i == null)
      i = 1;
   WriteToCookie('sid2',i,dtExpireDate,sPath,sDomain);
   return i;
}


function ValueOfCookie (ItemPosition) {
   var iLen = document.cookie.indexOf(";", ItemPosition)
   if (iLen == -1)
      iLen = document.cookie.length;
   return unescape(document.cookie.substring(ItemPosition, iLen)); 
}


function ChangeLanguage(psValue) {
   var s = ReadFromCookie("Language");
   if (s != psValue){
      WriteToCookie('Language',psValue,dtExpireDate,sPath,sDomain);
                // document.location.replace("cust_overzicht.asp");
      // history.go(0);
   }
}

function WriteLanguageSwitch(){ 
   var sLanguage = GetLanguage();
   document.write("| ");
   if (sLanguage != 'en')
      document.write("<A href=\"javascript:ChangeLanguage('en')\">English</A>&nbsp;| ");      
   if (sLanguage != 'fr') 
      document.write("<A href=\"javascript:ChangeLanguage('fr')\">Fran&ccedil;ais</A>&nbsp;| ");      
   if (sLanguage != 'nl') 
      document.write("<A href=\"javascript:ChangeLanguage('nl')\">Nederlands</A>&nbsp;| ");
   if (sLanguage != 'jp') 
      document.write("<A href=\"javascript:ChangeLanguage('jp')\"><IMG SRC='http://www.adinimages.com/lace-tapestries/images/japangif.gif' BORDER='0'></A>&nbsp;| ");
}

function DoLogout(psLogoutRef) {
   WriteToCookie('UserName', '--visitor--', dtExpireDate,sPath,sDomain);
   WriteToCookie('NumberOfVisits',0,dtExpireDate,sPath,sDomain);
   WriteToCookie('CntInBasket',0,dtExpireDate,sPath,sDomain);
   document.location=psLogoutRef;
}

function WriteWelcome(psWelcomeMsg, psLoginRef, psLogoutRef){
   var sName = GetUserName();
   var iNrVisits = GetNumberOfVisits();
   var sid = GetSid();
   var sid2 = GetSid2();
   var IsLoggedOn = GetIsLoggedOn();

   if (sName == "--visitor--") {
      document.write("<a href='" + psLoginRef + "'><img src='http://www.adinimages.com/lace-tapestries/images/loginbutton.jpg' width='36' height='25' border='0' align='left'></a>");
         //document.write("Sorry, " + sName);
         //document.write(IsLoggedOn);
   }
   else {
      if (sid == sid2) {
         document.write("<a href='" + psLogoutRef + "'><img src='http://www.adinimages.com/lace-tapestries/images/logoutbutton.jpg' width='36' height='25' border='0' align='left'></a>");
         //document.write("Welcome, " + sName);
      }
      else {
         document.write("<a href='" + psLoginRef + "'><img src='http://www.adinimages.com/lace-tapestries/images/loginbutton.jpg' width='36' height='25' border='0' align='left'></a>");
         //document.write("Welcome, " + sName);
      }
   }
}

function SaveSearchValues(){
   WriteToCookie('ArtOC', document.frmShopSearch.edt_ArtOC.value,null,sPath,sDomain);
   WriteToCookie('ArtPC', document.frmShopSearch.edt_ArtPC.value,null,sPath,sDomain);
   WriteToCookie('ArtYC', document.frmShopSearch.edt_ArtYC.value,null,sPath,sDomain);
 //WriteToCookie('ArtTI', document.frmShopSearch.edt_ArtTI.value,null,sPath,sDomain);
   WriteToCookie('ArtCI', document.frmShopSearch.edt_ArtCI.value,null,sPath,sDomain);
   WriteToCookie('ArtCITEST', document.frmShopSearch.edt_ArtCI.value,dtExpireDate,sPath,sDomain);
   return true
}

function SaveIdValue(){
   WriteToCookie('UserName', document.frmCustInfo.edt_KltLN.value,dtExpireDate,sPath,sDomain);
   return true
}

function EmptyCntInBasket(){
   WriteToCookie('CntInBasket',0,dtExpireDate,sPath,sDomain);
   return true
}

function CookieLogon(piSessionId){
   WriteToCookie('LoggedIn',piSessionId,dtExpireDate,sPath,sDomain);
   return true
}

function CookieLogOff(){
   WriteToCookie('LoggedIn',0,dtExpireDate,sPath,sDomain);
   return true
}

function WriteOption(psCookieValue,psSelection,psText){
   document.write("<option ");
   if ( psCookieValue == psSelection ){
      document.write(" SELECTED ");
   }
   document.write("value='" + psSelection + "'>" + psText + "</option>");
}

function WriteSearchField(){
   var sTI = ReadFromCookie('searchtext');
   if (sTI == null) sTI = ""; 
   document.write("<input type='text' name='edt_ArtTI' size='14' MaxLength='30' class='entryform'");
   document.write(" value='" + sTI + "'>");
}

function ResetValues(){
   document.frmShopSearch.edt_ArtOC.value = "*";
   document.frmShopSearch.edt_ArtPC.value = "*";
   document.frmShopSearch.edt_ArtYC.value = "*";
   document.frmShopSearch.edt_ArtTI.value = "";
   SaveSearchValues();
}

function ReadFromCookie(ItemName) {
   var RetVal = null;
   var ItemRef = ItemName + "=";
   var iPosLastChar = document.cookie.length - ItemRef.length;
   var i = 0;
   while (i < iPosLastChar) {
      var j = i + ItemRef.length;
      if (document.cookie.substring(i,j) == ItemRef) {
         RetVal = ValueOfCookie(j);
         break;
      }
      i = document.cookie.indexOf(" ",i) + 1;
      if (i == 0) break;
   }
   return RetVal;
}

function WriteToCookie ( ArgName, ArgValue ) {
   var ArgumentValues = WriteToCookie.arguments;
   var iNrArguments = WriteToCookie.arguments.length;
   var dtExpireDate = (iNrArguments > 2) ? ArgumentValues[2] : null;
   var Path = (iNrArguments > 3) ? ArgumentValues[3] : null;
   var Domain = (iNrArguments > 4) ? ArgumentValues[4] : null;
   var Secure = (iNrArguments > 5) ? ArgumentValues[5] : false;
   document.cookie = ArgName + "=" + escape(ArgValue)  +
      ((dtExpireDate == null) ? "" : ("; expires="+dtExpireDate.toGMTString())) +
      ((Path == null) ? "" : ("; path="+Path)) +
      ((Domain == null) ? "" : ("; domain="+Domain)) +
      ((Secure == true) ? "; secure" : "" );
}

