//var OrderSlist = "/php/order/slist.php";
var defaultResetShopListMessage = 'U gaat de winkelwagen legen, wilt u doorgaan?';
function InShopList(product_id) {
  var found = 0;
  for(var i=1;i<=CustomerNbOfProducts && ShopListId[i] != '' && found == 0;i++){
   if (ShopListId[i] == product_id) {
    found = 1;
  }}
  if (found == 0) { return false;}
  else return true;
}

var MaxProducts = 500;
var CustomerNbOfProducts = 0;
var ProductWindow = null;
var RefWindow = null;
var even=false;

function MakeShopListId(MaxProducts) {
 this.length = MaxProducts;
 for(var i=1;i<=MaxProducts;i++){this[i]='';}
 return this;
}
function MakeShopListTitle(MaxProducts) {
 this.length = MaxProducts;
 for(var i=1;i<=MaxProducts;i++){this[i]='';}
 return this;
}
function MakeShopListPrize(MaxProducts) {
 this.length = MaxProducts;
 for(var i=1;i<=MaxProducts;i++){this[i]='';}
 return this;
}
function MakeShopListNb(MaxProducts) {
 this.length = MaxProducts;
 for(var i=1;i<=MaxProducts;i++){this[i]='';}
 return this;
}
function ChangeInNumber(i,Number){
 if (isNaN(Number)) {
  alert("Aantal moet een getal zijn");
  return false;
 }
 else {
  if (Number >= 1) {
   ShopListNb[i] = eval(parseInt(Number));
   top.main.location =  top.main.URL_Slist;
  }
  else {
   alert("Aantal moet groter zijn dan 0");
   return false;
  }
 }
}
function WriteFooterShopList(TotalPrize,LabelTotalPrice) {
 TotalPrize = CorrectPrize(TotalPrize);
  top.main.document.writeln("<TR");
  if (even)  top.main.document.writeln(" class=\"l_tbl_even\"");
  else top.main.document.writeln(" class=\"l_tbl_odd\"");
  top.main.document.writeln(">");
  even=!even;
 top.main.document.writeln("<TD COLSPAN=\"9\">&nbsp;</TD>");
 top.main.document.writeln("</TR>");
  top.main.document.writeln("<TR valign=\"TOP\"");
  if (even)  top.main.document.writeln(" class=\"l_tbl_even\"");
  else top.main.document.writeln(" class=\"l_tbl_odd\"");
//  top.main.document.writeln(" class=\"l_ftr\"");
  top.main.document.writeln(">");
 top.main.document.writeln("<TD class=\"l_tbl_id\">&nbsp;</TD>");
 top.main.document.writeln("<TD class=\"l_tbl_total_price_label\" COLSPAN=\"3\">"+LabelTotalPrice+"</TD>");
 top.main.document.writeln("<TD class=\"l_tbl_quantity_price\">&euro; "+DisplayPrize(TotalPrize)+"<INPUT TYPE=\"HIDDEN\" NAME=\"TOTAALPRIJS\" VALUE="+DisplayPrize(TotalPrize)+"></TD>");
 top.main.document.writeln("<TD class=\"l_tbl_delete\">&nbsp;</TD>");
 top.main.document.writeln("</TR>");
}
function DisplayPrize(Prize){
 if (Prize>=1){
  PrizeInt = parseInt(Prize);
  PrizeDec = parseInt(Math.round(Prize*100)-(parseInt(Prize)*100));
 }
 else{
  PrizeInt=0;
  PrizeDec = parseInt(Math.round(Prize*100));
 }
 if (PrizeDec < 10){PrizeDec = '0' + PrizeDec;}
 if (PrizeDec == 100){PrizeInt = PrizeInt + 1; PrizeDec = 0;}
 return PrizeInt + ',' + PrizeDec;
}
function CorrectPrize(Prize){
 if (Prize>=1){
  PrizeInt = parseInt(Prize);
  PrizeDec = parseInt(Math.round(Prize*100)-(parseInt(Prize)*100));
 }
 else {
  PrizeInt = 0;
  PrizeDec = parseInt(Math.round(Prize*100));
 }
 if (PrizeDec < 10){PrizeDec = '0' + PrizeDec;}
 if (PrizeDec == 100){PrizeInt = PrizeInt + 1; PrizeDec = 0;}
 return eval(PrizeInt + '.' + PrizeDec);
}
function WriteShopListChange(LabelTotalPrice) {
 var l_URL_deselect_picture = top.main.URL_deselect_picture;
 even=false;
 var TotalPrize = 0;
 for(var i=1;i<=ShopListId.length && ShopListId[i] != '';i++){
  TotalProductPrize = parseFloat(ShopListNb[i]) * parseFloat(ShopListPrize[i]);
  TotalProductPrize = CorrectPrize(TotalProductPrize);
  TotalPrize += TotalProductPrize
  top.main.document.writeln("<TR");
  if (even)  top.main.document.writeln(" class=\"l_tbl_even\"");
  else top.main.document.writeln(" class=\"l_tbl_odd\"");
  top.main.document.writeln(">");
  top.main.document.writeln("<TD class=\"l_tbl_id\">"+ShopListId[i]+"<INPUT TYPE=\"hidden\" NAME=\"Id"+i+"\" VALUE=\""+ShopListId[i]+"\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_description\">"+ShopListTitle[i]+"<INPUT TYPE=\"hidden\" NAME=\"Title"+i+"\" VALUE=\""+ShopListTitle[i]+"\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_item_price\">&euro; "+DisplayPrize(ShopListPrize[i])+"<INPUT TYPE=\"hidden\" NAME=\"Prize"+i+"\" VALUE=\""+ShopListPrize[i]+"\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_quantity\"><INPUT TYPE=\"text\" NAME=\"Number"+i+"\" SIZE=\"1\" MAXLENGTH=\"1\" VALUE=\""+ShopListNb[i]+"\" ONCHANGE=\"return top.topbar.ChangeInNumber("+i+",Number"+i+".value)\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_quantity_price\">&euro; "+DisplayPrize(TotalProductPrize)+"<INPUT TYPE=\"HIDDEN\" NAME=\"TotalProductPrize"+i+"\" SIZE=\"9\" MAXLENGTH=\"9\" VALUE=\"EURO "+DisplayPrize(TotalProductPrize)+"\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_delete\"><A HREF=\"javascript:top.topbar.RemoveFromShopList("+i+")\" ONMOUSEOVER=\"javascript:window.status=mdel;return true;\" ONMOUSEOUT=\"javascript:window.status='';return true;\"><img src=\"" + l_URL_deselect_picture + "\" width=\"20\" height=\"20\" border=\"0\"></A></TD>");
  top.main.document.writeln("</TR>");
  even=!even;
 }
 WriteFooterShopList(TotalPrize,LabelTotalPrice);
}
function WriteProductIdList() {
 top.main.document.writeln("<TABLE><TR><TD><INPUT TYPE=\"hidden\" NAME=\"PRODUCT_ID_LIST\" VALUE=\"");
 for(var i=1;i<=ShopListId.length && ShopListId[i] != '';i++){
  top.main.document.writeln(ShopListId[i]);
 } 
 top.main.document.writeln("\"></TD></TR></TABLE>");
}
function WriteShopListNoChange(LabelTotalPrice) {
 even=false;
 var TotalPrize = 0;
 for(var i=1;i<=ShopListId.length && ShopListId[i] != '';i++){
  TotalProductPrize = ShopListNb[i] * ShopListPrize[i];
  TotalProductPrize = CorrectPrize(TotalProductPrize);
  TotalPrize += TotalProductPrize;
  top.main.document.writeln("<TR");
  if (even)  top.main.document.writeln(" class=\"l_tbl_even\"");
  else top.main.document.writeln(" class=\"l_tbl_odd\"");
  top.main.document.writeln(">");
  top.main.document.writeln("<TD class=\"l_tbl_id\">"+ShopListId[i]+"<INPUT TYPE=\"hidden\" NAME=\"PRODUCT"+i+"\" VALUE=\";ID="+ShopListId[i]+";AANTAL="+ShopListNb[i]+";STUKPRIJS="+DisplayPrize(ShopListPrize[i])+";PRODUCTPRIJS="+DisplayPrize(TotalProductPrize)+";OMSCHRIJVING="+ShopListTitle[i]+"\"></TD>");
  top.main.document.writeln("<TD class=\"l_tbl_description\">"+ShopListTitle[i]+"</TD>");
  top.main.document.writeln("<TD class=\"l_tbl_item_price\">&euro; "+PuntToKomma(ShopListPrize[i])+"</TD>");
  top.main.document.writeln("<TD class=\"l_tbl_quantity\">"+ShopListNb[i]+"</TD>");
  top.main.document.writeln("<TD class=\"l_tbl_quantity_price\">&euro; "+DisplayPrize(TotalProductPrize)+"</TD>");
  top.main.document.writeln("<TD class=\"l_tbl_delete\">&nbsp;</TD>");
  top.main.document.writeln("</TR>");
  even=!even;
 }
 WriteFooterShopList(TotalPrize,LabelTotalPrice);
}
function PuntToKomma(Prize) {
  KommaPos = Prize.indexOf('.');
  if (KommaPos != -1) {
   PrizeA = Prize.substring(0,KommaPos);
   PrizeB = Prize.substring(KommaPos+1,Prize.length);
   Prize = PrizeA + "," + PrizeB;
  } 
  return Prize;
}
function KommaToPunt(Prize) {
  KommaPos = Prize.indexOf(',');
  if (KommaPos != -1) {
   PrizeA = Prize.substring(0,KommaPos);
   PrizeB = Prize.substring(KommaPos+1,Prize.length);
   Prize = PrizeA + "." + PrizeB;
  } 
  return Prize;
}
function AddToShopList(Id,Title,Prize) {
 var found = 0;
 if (CustomerNbOfProducts == MaxProducts) {
  alert("Het is helaas niet mogelijk om meer dan "+MaxProducts+" producten per keer te bestellen");
 }
 else {
  for(var i=1;i<=ShopListId.length && ShopListId[i] != '' && found == 0;i++){
   if (ShopListId[i] == Id) {
    RemoveFromShopListB(i);
    SetImgWinkelwagen(Id,2);
    found = 1;
   }
  }
  if (found == 0) {
   CustomerNbOfProducts += 1;
   ShopListId[CustomerNbOfProducts] = Id;
   ShopListTitle[CustomerNbOfProducts] = Title;
   ShopListPrize[CustomerNbOfProducts] = KommaToPunt(Prize);
   ShopListNb[CustomerNbOfProducts] = 1;
   SetImgWinkelwagen(Id,1);
  }
 }
}
function RemoveFromShopList(i) {
 RemoveFromShopListB(i);
 top.main.location = top.main.URL_Slist;
}
function RemoveFromShopListB(i) {
 ShopListId[i]=ShopListTitle[i]=ShopListPrize[i]=ShopListNb[i]='';
 CustomerNbOfProducts--;
 i++;
 for(;i<=ShopListId.length && ShopListId[i] != '';i++){
  ShopListId[i-1] = ShopListId[i];
  ShopListTitle[i-1] = ShopListTitle[i];
  ShopListPrize[i-1] = ShopListPrize[i];
  ShopListNb[i-1] = ShopListNb[i];
 } 
 ShopListId[i-1]=ShopListTitle[i-1]=ShopListPrize[i-1]=ShopListNb[i-1]='';
}
function ResetShopList() {
 var msg = defaultResetShopListMessage;
 if (top.main.mResetShopListMessage) msg = top.main.mResetShopListMessage;

 var conf;
 conf = window.confirm(msg);
 if (conf) { 
     InitShopList();
     top.main.location = top.main.URL_Slist;
 }
}
function InitShopList() {
 ShopListId = new MakeShopListId(MaxProducts);
 ShopListTitle = new MakeShopListTitle(MaxProducts);
 ShopListPrize = new MakeShopListPrize(MaxProducts);
 ShopListNb = new MakeShopListNb(MaxProducts);
 CustomerNbOfProducts = 0
}
function DisplayShopList(){
 top.main.location = top.main.URL_Slist;
}
function OrderShopList(empty_shoppinglist){
 if (CustomerNbOfProducts == 0) {
  alert(empty_shoppinglist);
  return false;
 }
 else {
  top.main.location = top.main.URL_Order;
 }
}
function ProductsSelected(empty_shoppinglist) {
 if (CustomerNbOfProducts == 0) {
  alert(empty_shoppinglist);
  return false;
 }
 else {
  return true;
 }
}

function SendShopList(Voorletters,Achternaam,Adres,Postcode,Woonplaats,Telefoon,email,Land
                     ,error_voorletters,error_achternaam,error_adres,error_postcode
                     ,error_woonplaats,error_land,error_email,error_incorrect_email
                     ,order_message_send,empty_shoppinglist,message_choose_free_comic){
 if (!ProductsSelected(empty_shoppinglist)) {return false;}

 if (Voorletters == ''){
  alert(error_voorletters);
  top.main.document.shoppingcard.Voorletters.focus();
  return false;
 }
 if (Achternaam == ''){
  alert(error_achternaam);
  top.main.document.shoppingcard.Achternaam.focus();
  return false;
 }
 if (Adres == ''){
  alert(error_adres);
  top.main.document.shoppingcard.Adres.focus();
  return false;
 }
 if (Postcode == ''){
  alert(error_postcode);
  top.main.document.shoppingcard.Postcode.focus();
  return false;
 }
 if (Woonplaats == ''){
  alert(error_woonplaats);
  top.main.document.shoppingcard.Woonplaats.focus();
  return false;
 }
 if (Land == ''){
  alert(error_land);
  top.main.document.shoppingcard.Land.focus();
  return false;
 }
 if (email == ''){
  alert(error_email);
  top.main.document.shoppingcard.email.focus();
  return false;
 }
 else {
   if (!emailCheck(email)) {
    alert(error_incorrect_email)
    top.main.document.shoppingcard.email.focus();
    top.main.document.shoppingcard.email.select();
    return false;
   }
 }
 if (confirm(order_message_send)){
  top.main.document.shoppingcard.submit();
 }
 return true;
}

function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
 return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
 return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
 for (var i=1;i<=4;i++) {
  if (IPArray[i]>255) {
   return false
  }
 }
 return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
 return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
 return false
}
if (len<2) {
 return false
}
return true;
}

InitShopList();

function SetAllImgWinkelwagen(){
 var l_URL_remove_picture = top.main.URL_remove_picture;
 for(n=0;n<top.main.document.images.length;n++){
  var found = 0;
  for(var i=1;i<=CustomerNbOfProducts && ShopListId[i] != '' && found == 0;i++){
   if (top.main.document.images[n].name == "WW_" + ShopListId[i]) {
    top.main.document.images[n].src = l_URL_remove_picture;
    found = 1;
}}}}
function SetImgWinkelwagen(Id,Flg){
 var l_URL_remove_picture = top.main.URL_remove_picture;
 var l_URL_buy_picture = top.main.URL_buy_picture;
 var found = 0;
 for(n=0;n<top.main.document.images.length;n++){
  if (top.main.document.images[n].name == "WW_" + Id) {
   if (Flg==1) {top.main.document.images[n].src = l_URL_remove_picture;}
   else { if (Flg==2) {top.main.document.images[n].src = l_URL_buy_picture;}}
}}}
