var today = new Date();
var expires = new Date(today.getTime() + (56 * 86400000));
currencyCode = new Array(1); currencyName = new Array(1); currencyDecimals = new Array(1); currencyPre = new Array(1); currencyMiddle = new Array(1); currencyPost = new Array(1);
currencyCode[1]='HUF'; currencyName[1]='Hungary Forint'; currencyDecimals[1]='0'; currencyPre[1]=''; currencyMiddle[1]=''; currencyPost[1]=',-Ft';
today=new Date();
today.setTime(today.getTime() + (3600000));
today=today.toGMTString();
currentdate=new Date();
currentdate=currentdate.toLocaleString();
index = document.cookie.indexOf('AllprintName');

if (index > -1) {
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);
   if(countend==-1) {
      fulllist='';
   } else {
      fulllist=document.cookie.substring(countbegin,countend);
   }
} else {
   fulllist='';
}

if(fulllist.length < 1) {
   document.cookie='AllprintCode=.; expires='+today;
   document.cookie='AllprintName=.; expires='+today;
   document.cookie='AllprintWeight=.; expires='+today;
   document.cookie='AllprintTaxable=.; expires='+today;
   document.cookie='AllprintPrice=.; expires='+today;
   document.cookie='AllprintExtraFields=.; expires='+today;
   document.cookie='AllprintAdvanced=.; expires='+today;
   document.cookie='AllprintQuantity=.; expires='+today;
   document.cookie='TheCurrency=1; expires=Friday, 31-Dec-2010 08:00:00 GMT';
   document.cookie='JShopReferrer=.; expires='+today;
}



currencyCode = new Array(1); currencyName = new Array(1); currencyDecimals = new Array(1); currencyPre = new Array(1); currencyMiddle = new Array(1); currencyPost = new Array(1); 
currencyCode[1]='HUF'; currencyName[1]='Hungary Forint'; currencyDecimals[1]='0'; currencyPre[1]=''; currencyMiddle[1]=''; currencyPost[1]=',-Ft';


function goBuy() {
        ilist='';
        ilist=ilist+retrieveCookie('AllprintCode',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintName',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintWeight',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintTaxable',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintPrice',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintExtraFields',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintAdvanced',0,0)+'^';
        ilist=ilist+retrieveCookie('AllprintQuantity',0,0)+'^';
        ilist=ilist+retrieveCookie('TheCurrency',0,0)+'^';
        ilist=ilist+retrieveCookie('JShopReferrer',0,0)+'^';
        ilist=changeUnwanted(ilist);
        ilist=escape(ilist);
        ilist=changePercent(ilist);

        if (top.name != '') {
             window.open('buy.html',ilist);
        } else {
             top.name=ilist;
             top.location='buy.html';
        }
    }



function changeUnwanted(tstring) {
    nstring='';
    for (var i=0; i <= tstring.length; i++) {
        nchar=tstring.charAt(i);
        if (nchar=='*') { nchar='!&1'; }
        if (nchar=='+') { nchar='!&2'; }
        if (nchar=='@') { nchar='!&3'; }
        if (nchar=='.') { nchar='!&4'; }
        if (nchar=='/') { nchar='!&5'; }
        if (nchar=='-') { nchar='!&6'; }
        nstring=nstring+nchar;
    }
    return nstring;
}





function changePercent(tstring) {
    nstring='';
    for (var i=0; i <= tstring.length; i++) {
        nchar=tstring.charAt(i);
        if (nchar=='%') { nchar='_'; }
        nstring=nstring+nchar;
    }
    return nstring;
}





function showPrice(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    thisitem=0;
    itemstart=0;
    theprice=0;
    for (var i=0; i<pricestring.length; i++) {
       if (pricestring.substring(i,i+1)=='~') {
          thisitem++;
          if (thisitem==eval(theCurrency)) {
             theprice=pricestring.substring(itemstart,i);
          }
          itemstart=i+1;
       }
    }
    return presentValue(theprice,currencyDecimals[theCurrency],currencyPre[theCurrency],currencyMiddle[theCurrency],currencyPost[theCurrency]);
}





function showPriceSingle(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    // return presentValue(pricestring,currencyDecimals[theCurrency],currencyPre[theCurrency],currencyMiddle[theCurrency],currencyPost[theCurrency]);
       return presentValue(pricestring,currencyDecimals[theCurrency],'',currencyMiddle[theCurrency],'');
}





function showPricePlain(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    return presentValue(pricestring,currencyDecimals[theCurrency],'',currencyMiddle[theCurrency],'');
}





function getPrice(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    thisitem=0;
    itemstart=0;
    theprice=0;
    for (var i=0; i<pricestring.length; i++) {
       if (pricestring.substring(i,i+1)=='~') {
          thisitem++;
          if (thisitem==eval(theCurrency)) {
             theprice=pricestring.substring(itemstart,i);
          }
          itemstart=i+1;
       }
    }
    return theprice;
}





function presentValue(value,dp,pt,mt,at) {
        if(value<=0.9999) {
            newPounds='0';
        } else {
            newPounds=parseInt(value);
        }
        dec='1';
        for (var i=1; i<=dp;i++) {
            dec=dec+'0';
        }
        if (value>0) {
            newPence=Math.round((eval(value)+.000008 - newPounds)*(eval(dec)));
        } else {
            newPence=0;
        }
        compstring='9';
        for (var i=1; i <=dp-1;i++) {
            if (eval(newPence) <= eval(compstring)) newPence='0'+newPence;
            compstring=compstring+'9';
        }
        if (dp>0) {
            if (newPence==eval(dec)) { newPounds++; newPence=0; }
            newString=pt+newPounds+mt+newPence+at;
        } else {
            newString=pt+newPounds+at;
        }
        return (newString);
    }





function retrieveCookie(thecookie,thenumber,theadditional) {
    index = document.cookie.indexOf(thecookie);
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    fulllist=document.cookie.substring(countbegin,countend);
    if (thenumber==0) { return fulllist; }
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
           itemvalue=fulllist.substring(itemstart,i);
           if (thisitem==thenumber) {
                if (theadditional==0) {
                    return itemvalue;
                } else {
                    itemstarttwo=0;
                    thisitemtwo=0;
                    for (var j=0; j<itemvalue.length; j++) {
                        if (itemvalue.substring(j,j+1)=='~') {
                            thisitemtwo++;
                            itemvaluetwo=itemvalue.substring(itemstarttwo,j);
                            if (thisitemtwo==theadditional) {
                                return itemvaluetwo;
                            }
                            itemstarttwo=j+1;
                        }
                    }
                }
            }
            itemstart=i+1;
        }
    }
    return '';
}





function addToCookie(thecookie,thevalue) {
    fulllist=retrieveCookie(thecookie,0,0);
    if (fulllist=='.') { fulllist=''; }
    fulllist=fulllist+thevalue+'|';
    document.cookie=thecookie+'='+fulllist+'; expires='+today;
}





function removeFromCookie(thecookie,thenumber) {
    fulllist=retrieveCookie(thecookie,0,0);
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
            itemvalue=fulllist.substring(itemstart,i);
            if (thisitem!=thenumber) {
                newcookie=newcookie+itemvalue+'|';
            }
            itemstart=i+1;
        }
    }
    if (newcookie=='') { newcookie='.'; }
    document.cookie=thecookie+'='+newcookie+'; expires='+today;
}





function changeCookieValue(thecookie,thenumber,thevalue) {
    fulllist=retrieveCookie(thecookie,0,0);
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
            itemvalue=fulllist.substring(itemstart,i);
            if (thisitem==thenumber) {
                itemvalue=thevalue;
            }
            newcookie=newcookie+itemvalue+'|';
            itemstart=i+1;
        }
    }
    document.cookie=thecookie+'='+newcookie+'; expires='+today;
}





function retrieveCookieCount(thecookie) {
   fulllist=retrieveCookie(thecookie,0,0);
   thisitem=0; newcookie=''; itemstart=0;
   for(var i=0; i<fulllist.length;i++) {
    if (fulllist.substring(i,i+1)=='|') {
        thisitem++;
        itemstart=i+1;
    }
   }
   return thisitem;
}





function showCurrency() {
   index = document.cookie.indexOf('TheCurrency');
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);
   if(countend==-1) {countend=document.cookie.length; }
   theCurrency=document.cookie.substring(countbegin,countend);
   document.basketform.selectcurrency.selectedIndex=theCurrency-1;
}





function getAdvanced(adv,qty) {
     newAdv='';advlist=0;applicable=true;advnewprice='';
    for (var j=0;j<=adv.length;j++) {
            if (adv.substring(j,j+1) == '{') {
            thisadv=1;
            advstart=j+1;
        } else if (adv.substring(j,j+1) == '}') {
            advend=j;
            advPrice=adv.substring(advstart,advend);
            advlist++; applicable=true;
            if (applicable==true) {
                if (advFrom > 0 && advFrom > eval(qty)) applicable=false;
                if (advTo > 0 && advTo < eval(qty)) applicable=false;
                if (applicable==true) advnewprice=advPrice;
            }
        } else if (adv.substring(j,j+1)=='!') {
            if (thisadv== 1) advFrom=adv.substring(advstart,j);
            if (thisadv== 2) advTo=adv.substring(advstart,j);
            thisadv++;advstart=j+1;
        }
    }
    return advnewprice;
}





alltheitems=0;
megrendeles='';
taxfield='';
singletax='';
singleshipping='';
taxfield2='';
shippingfile='';
primaryfield='';
secondaryfield='';
singlefield='';





function showBasket() {
   sepchar='"';
   totprice=0;

   document.writeln("<table align='center' border=0 width=450 cellspacing=0 cellpadding=0>");
   document.writeln("<tr>");
   document.writeln("<td width=200></td>");
   document.writeln("<td></td>");
   document.writeln("<td>");
   document.writeln("<p class=kis_szovegek align=right>egységár</td>");
   document.writeln("<td>");
   document.writeln("<p class=kis_szovegek align=right>összesen</td>");
   document.writeln("<td></td>");
   document.writeln("</tr>");




   for (var i=1;i<=retrieveCookieCount('AllprintCode');i++) {
       theCode=retrieveCookie('AllprintCode',i,0);
       theName=retrieveCookie('AllprintName',i,0);
       theWeight=retrieveCookie('AllprintWeight',i,0);
       theTaxable=retrieveCookie('AllprintTaxable',i,0);
       thePrice=retrieveCookie('AllprintPrice',i,0);
       theAP=retrieveCookie('AllprintAdvanced',i,0);
       theQuantity=retrieveCookie('AllprintQuantity',i,0);

       if (theAP!='none') {
           tempprice=getAdvanced(theAP,theQuantity);
           if (tempprice>'') { thePrice=tempprice; }
       }

       itemtotal=eval(theQuantity)*eval(getPrice(thePrice));
       totprice=totprice+itemtotal;


	megrendeles=megrendeles+""+theQuantity+"db "+theName+" "+showPricePlain(itemtotal)+",-Ft\n<br>";


  document.writeln("<tr>");
	document.writeln("<td width=200>");
	document.writeln("<p class=kis_szovegek>"+theName+"</td>");
	document.writeln("<td class=kis_szovegek><input type=text name='quant"+i+"' value='"+theQuantity+"' size=1></td>");
	document.writeln("<td class=kis_szovegek><p align=right>"+showPrice(thePrice)+"</td>");
	document.writeln("<td class=kis_szovegek><p align=right>"+showPriceSingle(itemtotal)+"</td>");
	document.writeln("<td class=kis_szovegek><p align=right><a href='#' onClick='removeItem("+i+");'>törlés</a></td>");
	document.writeln("</tr>");

   }

  document.writeln("<tr>");
 	document.writeln("<td colspan=5><img border=0 src='images/spacer.gif' width=20 height=20></td>");
 	document.writeln("</tr>");
 	document.writeln("<tr>");
 	document.writeln("<td width=200><p class=kis_szovegek>Összesen</td>");
 	document.writeln("<td></td>");
 	document.writeln("<td></td>");
 	document.writeln("<td><p align=right class=konyv_cim><b>"+showPriceSingle(totprice)+"</b> <span class=kis_szovegek>(+postaköltség)</span></td>");
 	document.writeln("<td></td>");
 	document.writeln("</tr>");
 	document.writeln("</table>");

	document.writeln("<input type=hidden name='Megrendelt kiadvány(ok)' value='"+megrendeles+"'>");
 	document.writeln("<input type=hidden name='Megrendelés összértéke (forintban)' value='"+showPriceSingle(totprice)+"'>");

}






function flipImage(imgname,imgsrc) {
    document.images[imgname].src=imgsrc;
}





function removeItem(itemno) {
        removeFromCookie('AllprintCode',itemno);
        removeFromCookie('AllprintName',itemno);
        removeFromCookie('AllprintWeight',itemno);
        removeFromCookie('AllprintTaxable',itemno);
        removeFromCookie('AllprintPrice',itemno);
        removeFromCookie('AllprintExtraFields',itemno);
        removeFromCookie('AllprintAdvanced',itemno);
        removeFromCookie('AllprintQuantity',itemno);
        self.location = 'kosar.php';
    }





function clearBasket() {
            index=document.cookie.indexOf('Allprint');
            document.cookie='AllprintCode=.; expires='+today;
            document.cookie='AllprintName=.; expires='+today;
            document.cookie='AllprintWeight=.; expires='+today;
            document.cookie='AllprintTaxable=.; expires='+today;
            document.cookie='AllprintPrice=.; expires='+today;
            document.cookie='AllprintExtraFields=.; expires='+today;
            document.cookie='AllprintAdvanced=.; expires='+today;
            document.cookie='AllprintQuantity=.; expires='+today;
            self.location='kosar.php';
    }





function changeCurrency(num) {
         index=document.cookie.indexOf('TheCurrency');
         document.cookie='TheCurrency='+(num+1)+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
         self.location='kosar.php';
    }





function updateBasket() {
        newItemList='';
        itemlist=0;
        for (var i=1;i<=retrieveCookieCount('AllprintCode');i++) {
            if (!isNaN(document.basketform.elements['quant'+i].value)) {
                changeCookieValue('AllprintQuantity',i,eval(document.basketform.elements['quant'+i].value));
            }
        }
        self.location='kosar.php';
    }





function szamlazasi_adatok() {
document.getElementById("szamlazashoz").innerHTML="<table border='0' width='400' cellspacing='0' cellpadding='0' STYLE='border-collapse: collapse' BORDERCOLOR='#111111'><tr><td width='200' class='tah11'><p style='margin-bottom: 5'>Név</td><td CLASS='tah11'><p style='margin-bottom: 5'><input type='text' value name='szamla_NEV' size='30' STYLE='background-color: #E1EEF0'></td></tr><tr><td width='200' class='tah11'><p style='margin-bottom: 5'>Irányítószám</td><td CLASS='tah11'><p style='margin-bottom: 5'><input type='text' value name='szamla_IRANYITOSZAM' size='10' STYLE='background-color: #E1EEF0'></td></tr><tr><td width='200' class='tah11'><p style='margin-bottom: 5'>Város</td><td CLASS='tah11'><p style='margin-bottom: 5'><input type='text' value name='szamla_VAROS' size='30' STYLE='background-color: #E1EEF0'></td></tr><tr><td width='200' class='tah11'><p style='margin-bottom: 10'>Cím</td><td CLASS='tah11'><p style='margin-bottom: 10'><input type='text' value name='szamla_CIM' size='30' STYLE='background-color: #E1EEF0'></td></tr></TABLE>";

	}	





function checkPurchaseFields() {
var flag=0;

if ((document.basketform.EGY.value == "") || (document.basketform.KETTO.value == "") || (document.basketform.HAROM.value == "") || (document.basketform.HAROM.value.indexOf('@')==-1)){
document.getElementById("Megrendelő adatai").innerHTML="<font color='#ff0000'><b>- minden adatot meg kell adni!</b></font>";
flag++;}else{document.getElementById("Megrendelő adatai").innerHTML="- minden adatot meg kell adni!";}

if ((document.basketform.NEGY.value == "") || (document.basketform.OT.value == "") || (document.basketform.HAT.value == "")){
document.getElementById("Szállítási cím").innerHTML="<font color='#ff0000'><b>- minden adatot meg kell adni!</b></font>";
flag++;}else{document.getElementById("Szállítási cím").innerHTML="- minden adatot meg kell adni!";}


if (flag!="0"){return false;}
 return true;
}


function goSubmit() {
 if (checkPurchaseFields()) {document.basketform.submit();}
}





function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") + 
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}





function Get_Cookie(name) {
var start = document.cookie.indexOf(name+"=");
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(";",len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

