//NEWSLETTER
function checkNews() {
    var bfr="";
    if(document.newsf.mail_news.value=="") {
        bfr+="Podaj mail!";
    }
    else if(document.newsf.mail_news.value!="") {
        if(!document.newsf.mail_news.value.match(/^([a-z0-9_.-]+)@[a-z0-9-_]+([.][a-z0-9-_]+)+$/i)) {
            bfr+="Musisz podać prawidłowy adres e-mail!\n";
        }
    }
    if(bfr!="") {
        alert(bfr);
        return false;
    }
    return true;
}
function sendnews(){
	document.newsf.submit();
}
//NEWSLETTER


//SEARCH
function checkSearch(){
	var bfr="";
	if(document.f.searchf.value=="") {
        bfr+="Musisz wpisać hasło do wyszukania!";
   }
    if(bfr!="") {
        alert(bfr);
        return false;
    }
    return true;
}
function cmd2(){
   document.fs.submit();
}
//SEARCH

//POPUP
function popup(openfile,id){
	fullImg=window.open('/_page/popup_'+openfile+'.php?id='+id,'full','width=600,height=650,resizable=yes,scrollbars=yes,menubar=no,left=200,top=100' );
	fullImg.focus();
}

function popup2(){
	fullImg=window.open('/_page/popup_printkoszyk.php','full','width=650,height=650,resizable=yes,scrollbars=yes,menubar=no,left=0,top=0' );
	fullImg.focus();
}
//POPUP

//KONTAKT
function checkKontakt(frm) {
    var bfr="";
    if(frm.email.value=="") {
        bfr+="Podaj e-mail!";
    }
    else if(frm.email.value!="") {
        if(!frm.email.value.match(/^([a-z0-9_.-]+)@[a-z0-9-_]+([.][a-z0-9-_]+)+$/i)) {
            bfr+="Musisz podać prawidłowy adres e-mail!\n";
        }
    }
    if(frm.tresc.value=="") {
        bfr+="Podaj treść wiadomości!";
    }
    if(bfr!="") {
        alert(bfr);
        return false;
    }
    return true;
}
//KONTAKT

//ARTYKULY
function readart(openfile,id) {
	fullImg=window.open('/_page/popup_'+openfile+'.php?id='+id,'','width=500,height=400,resizable=yes,scrollbars=yes,menubar=no,left=200,top=100' );
	fullImg.focus();
}
//ARTYKULY

//KOSZYK
function setPopup(nId){
    //alert('sprawdzam powiazania dla id: '+nId);
    crossSetStyle('relsell', 'display', 'block');
    getRelsell(nId);
}
function getRelsell( nr )
{
	//var id = '';
	//id = document.getElementById("id_prod").value;
	
    advAJAX.post({
        
        url : "/_page/popup_getrellsell.php",
        id:nr,
        onInitialization : function()
        {
           //alert('inicjalizacja');
           document.getElementById('relsell_ajax').innerHTML = '<img src="/_img/page/ajax.gif"   style="float:none;" /><br /><span style="font-weight: bold">Trwa pobieranie danych... Proszę czekać.</span>';
        },
        
        onSuccess : function(obj)
        {
            if(obj.responseText == 'brak' )
            {
                document.getElementById('relsell_ajax').innerHTML = 'Brak produktów';
            }
            else
            {
              document.getElementById('relsell_ajax').innerHTML = obj.responseText;
            }
        },
        
        onError : function(obj)
        {
            document.getElementById('obrazki').innerHTML = '<span style="font-weight: bold;color:#Ff0000;">Wystąpil nieoczekiwany bląd, spróbuj ponownie.</span>';
        }
        
    });
}
function zamow(name_file){
  popup=window.open('/_page/popup_'+name_file+'.php','full','width=300,height=230,resizable=no,scrollbars=no,menubar=no,left=200,top=100' );
  popup.focus();
}
function checkfirm(frm)
{
		var checkedButoon = false;
		for(var i=0; i<frm.firma.length; i++) 
		{
			if(frm.firma[i].checked == true)
				checkedButoon = true;
		}
	   if(!checkedButoon)
		{

	      alert('Proszę wybrać sposób dostarczenie przesyłki.');

   	   return false;

	   }

}
//KOSZYK

//PRODUKT
function cmd(){
   document.f.submit();
}
function showorhide(id){
  if (crossGetStyle(id, 'display') == 'none')
  {
      crossSetStyle(id, 'display', 'block');
  }
        else
        {
            crossSetStyle(id, 'display', 'none');
        }
}
function MM_jumpMenu(targ,selObj,restore,param){
	if(param =='reg'){
	   if(selObj.options[selObj.selectedIndex].value!=0){
	      popup('download', selObj.options[selObj.selectedIndex].value);
	   }
	  if (restore) selObj.selectedIndex=0;
	}
	else
		document.location = '/logowanie.html';
} 
//PRODUKT


function przelicz(){
	var j=0;
	var suma=0;
	
	while( document.f.elements['prmh'+'['+j+']'] )
	{
		var a="";
		a=document.f.elements['prmh'+'['+j+']'].value;
		
		var temp=a.indexOf( "]" );
		var b = a.substring(1,temp);		
		a=a.substring(temp+2);
		var temp=a.indexOf("]");
		var c=a.substring(0,temp);
		a=a.substring(temp+2);
		var temp=a.indexOf("]");
		var d=a.substring(0,a.indexOf("]"));
		a=a.substring(temp+2);
		
		var f=parseFloat(a.substring(0,a.indexOf("]")));
		
		var ile=parseFloat(document.f.elements['prom'+'['+b+']'+'['+c+']'].value);
		
		if(isNumber(ile)==true)
		{
			document.f.elements['prom'+'['+b+']'+'['+c+']'].value=ile;
		}
		else {
			ile=0;
			document.f.elements['prom'+'['+b+']'+'['+c+']'].value='';
		}
		suma += ile*parseFloat(promocje[ b ][ c ]);
		j++;		
	}
	
	////////////////////////////////////////////////
	var j=0;
	var id = 0;
	while( document.f.elements['prh'+'['+j+']'] )
	{
		var id = parseInt( document.f.elements['prh'+'['+j+']'].value ); 
		
		var ile= parseFloat( document.f.elements['prod['+id+']'].value );

		if(isNumber(ile)==true)
		{
			document.f.elements['prod['+id+']'].value=ile;
		}
		else
		{
			ile=0;
			document.f.elements['prod['+id+']'].value='';
		}
		
		if(ile)
		{

			if( ile < produkty[ id ][ 'ile' ] )
				cena = produkty[ id ][ 'cena_jed' ];
			else
				cena = produkty[ id ][ 'cena_op' ];

      	suma += ile*parseFloat(cena);
	   }
		j++;
	}
	///////////////////////////////////////////////
	document.f.suma2.value = document.f.suma.value=printf("%01.2f",suma);
}

var sumaFirst = 0;
var firstSuma = true;
function przelicz_koszyk(){
    var j=0;
    var suma=0;
    
    while( document.f.elements['prmh'+'['+j+']'] )
    {
        var a="";
        a=document.f.elements['prmh'+'['+j+']'].value;
        
        var temp=a.indexOf( "]" );
        var b = a.substring(1,temp);        
        a=a.substring(temp+2);
        var temp=a.indexOf("]");
        var c=a.substring(0,temp);
        a=a.substring(temp+2);
        var temp=a.indexOf("]");
        var d=a.substring(0,a.indexOf("]"));
        a=a.substring(temp+2);
        
        var f=parseFloat(a.substring(0,a.indexOf("]")));
        
        var ile=parseFloat(document.f.elements['prom'+'['+b+']'+'['+c+']'].value);
        
        if(isNumber(ile)==true)
        {
            document.f.elements['prom'+'['+b+']'+'['+c+']'].value=ile;
        }
        else {
            ile=0;
            document.f.elements['prom'+'['+b+']'+'['+c+']'].value='';
        }
        suma += ile*parseFloat(promocje[ b ][ c ]);
        j++;        
    }
    
    ////////////////////////////////////////////////
    var j=0;
    var id = 0;
    while( document.f.elements['prh'+'['+j+']'] )
    {
        var id = parseInt( document.f.elements['prh'+'['+j+']'].value ); 
        var id_prod = produkty[ id ][ 'id' ];
        var ile= parseFloat( document.f.elements['prod['+id_prod+']['+id+']'].value );

        if(isNumber(ile)==true)
        {
            document.f.elements['prod['+id_prod+']['+id+']'].value=ile;
        }
        else
        {
            ile=0;
            document.f.elements['prod['+id_prod+']['+id+']'].value='';
        }
        
        if(ile)
        {

            if( ile < produkty[ id ][ 'ile' ] )
                cena = produkty[ id ][ 'cena_jed' ];
            else
                cena = produkty[ id ][ 'cena_op' ];

          suma += ile*parseFloat(cena);
       }
        j++;
    }
	 if( firstSuma )
	 {
	 	firstSuma = false;
		sumaFirst = suma;
	 }
	 if( suma != sumaFirst )
	 {
	 		$("#przeliczKoszyk").show();
			$("#td3").css({backgroundColor:"#D1EDF1"});
	 }
    ///////////////////////////////////////////////
    document.f.suma.value=printf("%01.2f",suma);
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
/* Function printf(format_string,arguments...)
 * Javascript emulation of the C printf function (modifiers and argument types 
 *    "p" and "n" are not supported due to language restrictions)
 *
 * Copyright 2003 K&L Productions. All rights reserved
 * http://www.klproductions.com 
 *
 * Terms of use: This function can be used free of charge IF this header is not
 *               modified and remains with the function code.
 * 
 * Legal: Use this code at your own risk. K&L Productions assumes NO responsibility
 *        for anything.
 ********************************************************************************/
function printf(fstring)
  { var pad = function(str,ch,len)
      { var ps='';
        for(var i=0; i<Math.abs(len); i++) ps+=ch;
        return len>0?str+ps:ps+str;
      }
    var processFlags = function(flags,width,rs,arg)
      { var pn = function(flags,arg,rs)
          { if(arg>=0)
              { if(flags.indexOf(' ')>=0) rs = ' ' + rs;
                else if(flags.indexOf('+')>=0) rs = '+' + rs;
              }
            else
                rs = '-' + rs;
            return rs;
          }
        var iWidth = parseInt(width,10);
        if(width.charAt(0) == '0')
          { var ec=0;
            if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) ec++;
            if(rs.length<(iWidth-ec)) rs = pad(rs,'0',rs.length-(iWidth-ec));
            return pn(flags,arg,rs);
          }
        rs = pn(flags,arg,rs);
        if(rs.length<iWidth)
          { if(flags.indexOf('-')<0) rs = pad(rs,' ',rs.length-iWidth);
            else rs = pad(rs,' ',iWidth - rs.length);
          }    
        return rs;
      }
    var converters = new Array();
    converters['c'] = function(flags,width,precision,arg)
      { if(typeof(arg) == 'number') return String.fromCharCode(arg);
        if(typeof(arg) == 'string') return arg.charAt(0);
        return '';
      }
    converters['d'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,arg); 
      }
    converters['u'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,Math.abs(arg)); 
      }
    converters['i'] =  function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = ((Math.abs(arg)).toString().split('.'))[0];
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        return processFlags(flags,width,rs,arg); 
      }
    converters['E'] = function(flags,width,precision,arg) 
      { return (converters['e'](flags,width,precision,arg)).toUpperCase();
      }
    converters['e'] =  function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toExponential(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs.replace(/^(.*)(e.*)$/,'$1.$2');
        return processFlags(flags,width,rs,arg);        
      }
    converters['f'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toFixed(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs + '.';
        return processFlags(flags,width,rs,arg);
      }
    converters['G'] = function(flags,width,precision,arg)
      { return (converters['g'](flags,width,precision,arg)).toUpperCase();
      }
    converters['g'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        absArg = Math.abs(arg);
        rse = absArg.toExponential();
        rsf = absArg.toFixed(6);
        if(!isNaN(iPrecision))
          { rsep = absArg.toExponential(iPrecision);
            rse = rsep.length < rse.length ? rsep : rse;
            rsfp = absArg.toFixed(iPrecision);
            rsf = rsfp.length < rsf.length ? rsfp : rsf;
          }
        if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) rse = rse.replace(/^(.*)(e.*)$/,'$1.$2');
        if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) rsf = rsf + '.';
        rs = rse.length<rsf.length ? rse : rsf;
        return processFlags(flags,width,rs,arg);        
      }  
    converters['o'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = Math.round(Math.abs(arg)).toString(8);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0'+rs;
        return processFlags(flags,width,rs,arg); 
      }
    converters['X'] = function(flags,width,precision,arg)
      { return (converters['x'](flags,width,precision,arg)).toUpperCase();
      }
    converters['x'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        arg = Math.abs(arg);
        var rs = Math.round(arg).toString(16);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0x'+rs;
        return processFlags(flags,width,rs,arg); 
      }
    converters['s'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = arg;
        if(rs.length > iPrecision) rs = rs.substring(0,iPrecision);
        return processFlags(flags,width,rs,0);
      }
    farr = fstring.split('%');
    retstr = farr[0];
    fpRE = /^([-+ #]*)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/;
    for(var i=1; i<farr.length; i++)
      { fps=fpRE.exec(farr[i]);
        if(!fps) continue;
        if(arguments[i]!=null) retstr+=converters[fps[4]](fps[1],fps[2],fps[3],arguments[i]);
        retstr += fps[5];
      }
    return retstr;
  }
/* Function printf() END */
