/*

*/

function licz_plat_trans(felgi, felgi2, opony, felga_opona, alcar){
          var c_pl=0.0;
          /*raty_zagiel, platnosc_odbior, platnosc24, przelew*/
          var platnosc= get_checked_value(document.forms['prod_koszyk'].elements['platnosc']);
          /*ups, poczta*/
          var transport= get_checked_value(document.forms['prod_koszyk'].elements['transport']);
          //alert(felga_opona);//alert(platnosc);
           
          
          if (felgi2+felga_opona+felgi+alcar>0&&opony<=0){
              var mnoz=1.0;
              if (platnosc=='przelew'||platnosc=='raty_zagiel'){
                      mnoz=50;
              }else if (platnosc=='platnosc_odbior'){
                      mnoz=70;
              }else if (platnosc=='platnosc24'){
                      mnoz=80;
              }
              c_pl+= mnoz*( felga_opona+felgi+alcar+felgi2);
          }else if ( (felgi2+felga_opona+felgi+alcar)<=0 && opony>0){
               var skl=0;
              if (platnosc=='przelew'||platnosc=='raty_zagiel'){
                      skl=20;
              }else if (platnosc=='platnosc_odbior'){
                      skl=30;
              }else if (platnosc=='platnosc24'){
                      skl=40;
              }
              c_pl+= (opony-1)*10+skl;
          }else if ( (felgi2+felga_opona+felgi+alcar)>0 && opony>0){
              var skl=0; var mnoz=1.0; mnoz_op=1.0;
              if (platnosc=='przelew'||platnosc=='raty_zagiel'){
                      mnoz= 40; mnoz_op=10.0;
              }else if (platnosc=='platnosc_odbior'){
                      mnoz= 50; mnoz_op=10.0;
              }else if (platnosc=='platnosc24'){
                      mnoz= 50; mnoz_op=20.0;
              }
              c_pl+= opony*mnoz_op+mnoz*(felga_opona+felgi+alcar+felgi2);
          }else if ( (felgi2+felga_opona+felgi+alcar)<=0 && opony<=0){
              if (transport=='ups'){
                  if (platnosc=='przelew'){
                     c_pl= 20.00;
                  }else{
                     c_pl= 40.00;
                  }
              }else if (transport=='poczta'){
                  if (platnosc=='przelew'){
                     c_pl= 10.00;
                  }else{
                     c_pl= 20.00;
                  }
              }
          }

          

/*
          if (platnosc!='przelew' ){
              //c_pl= 50.00;
              if (felga_opona>0){
                   c_pl+= 100.00*(felga_opona);
              }
              if (felgi>0||alcar>0){
                  c_pl+= 50.00*(felgi+alcar);
              }
              if (opony>0){
                  if (felgi>0||alcar>0){
                     c_pl+= 10.0*opony+0.00;
                  }else{
                     c_pl+= 10.0*opony+30.00;
                  }
              }
          }
           //alert(felgi);
          if (felgi<=0&&opony<=0&&felga_opona<=0&&alcar<=0){
              if (transport=='ups'){
                  if (platnosc=='przelew'){
                     c_pl= 20.00;
                  }else{
                     c_pl= 40.00;
                  }
              }else if (transport=='poczta'){
                  if (platnosc=='przelew'){
                     c_pl= 10.00;
                  }else{
                     c_pl= 20.00;
                  }
              }
          }
*/
         
          //document.getElementById('TEST').value=(c_pl) ;
          document.getElementById('c_plat_trans').value=(cena_f(c_pl)) ;
          document.getElementById('c_plat_trans_html').innerHTML= document.getElementById('c_plat_trans').value ;

          return c_pl;
    }




function czysc_faktura(){
       $j('#fak_firma').val("");
        $j('#fak_ulica').val("");
        $j('#fak_kod').val("");
        $j('#fak_miasto').val("");
}

function copy_platnik(){
        $j('#fak_firma').val($j('#pl_firma').val());
        $j('#fak_ulica').val($j('#pl_ulica').val());
        $j('#fak_kod').val($j('#pl_kod').val());
        $j('#fak_miasto').val($j('#pl_miasto').val());
/*            pl_firma
            pl_ulica
            pl_kod
             pl_miasto

             fak_firma
             fak_ulica
             fak_kod
             fak_miasto*/
}

   function licz_cene2(){
        var brutto= 1;
        //var kids= $j("#prod_koszyk").children("input");
        //alert(kids[0].value);
        var cena= 0;
        var FELGI=0;
        var FELGA_OPONA=0;
        var OPONY=0;
        var ALCAR= 0;
        var FELGI2=0;
        var form= document.forms['prod_koszyk'];
    		var len=form.length;
    		var i;
    		for (i= 0; i<len; i++){
    			var el= form.elements[i];
          var ar= el.name.split("_");
          if ( ar.length==3&&ar[0].slice(0,4)=='cena'){
                 //return ();
                //typecat_
               //alert('ile_'+ar[1]+'_'+ar[2]);
                var temp= document.forms['prod_koszyk'].elements['cena_'+ar[1]+'_'+ar[2]].value *
                document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value;
                document.getElementById('cena_b_'+ar[1]+'_'+ar[2]).innerHTML= cena_f(temp);
                cena+= temp;
                if (document.forms['prod_koszyk'].elements['typecat_'+ar[1]+'_'+ar[2]].value=='alcar')
                  ALCAR+= parseInt((document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value),10);
                if (document.forms['prod_koszyk'].elements['typecat_'+ar[1]+'_'+ar[2]].value=='felga')
                  FELGI+= parseInt((document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value),10);
                if (document.forms['prod_koszyk'].elements['typecat_'+ar[1]+'_'+ar[2]].value=='felga2')
                  FELGI2+= parseInt((document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value),10);
                if (document.forms['prod_koszyk'].elements['typecat_'+ar[1]+'_'+ar[2]].value=='opona')
                  OPONY+= parseInt((document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value),10);
                if (document.forms['prod_koszyk'].elements['typecat_'+ar[1]+'_'+ar[2]].value=='felga_opona')
                  FELGA_OPONA+= parseInt((document.forms['prod_koszyk'].elements['ile_'+ar[1]+'_'+ar[2]].value),10);
          }
    		}
         if (FELGI2){
//alert('jest');
          }
        if (document.getElementById('poczta_outer') ){
            if (FELGI2==0&&FELGI==0&&OPONY==0&&FELGA_OPONA==0&&ALCAR==0){
                document.getElementById('poczta_outer').display='block';
            }else{
                document.forms['prod_koszyk'].elements['transport'].value='ups';
                document.getElementById('poczta_outer').style.display='none';
            }
        }

        if (0&&c_t<=0){
           document.getElementById('c_plat_trans_html_outer').style.display='none';
        }else{
           document.getElementById('c_plat_trans_html_outer').style.display='block';
        }

        var c_plat_trans= licz_plat_trans(FELGI, FELGI2, OPONY, FELGA_OPONA, ALCAR);
        var cena_all =  cena + c_plat_trans;
        document.getElementById('suma_prod').innerHTML = cena_f(cena_f(cena) );
        CENA_RATA_ZAGIEL= cena_f(cena_all) ;

        return cena_f(cena);
    }





function get_checked_value(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function set_checked_value(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function dhtmlselect(selectid, selected,type, cena){
	var selectbox=document.getElementById(selectid);
	document.write('<div id="dhtml_'+selectid+'" class="dhtmlselect"><span class="dhtml_text" id="dsel_'+selectid+'">'+selectbox.title+'</span> '+'<div class="dropdown">')
  var anch_item= new Object();
	for (var i=0; i<selectbox.options.length; i++){
    var t='normal';
    if ( selectbox.options[i].value==selected ){
        t= 'selected';
        document.getElementById('dsel_'+selectid).innerHTML= selectbox.options[i].text;
    }

	  if (typeof type!="undefined" && type=='nr'){
		   document.write('<div  class="'+ t +'" id="dhtml_'+selectid+'_'+i+'" class="item"  >'+selectbox.options[i].text+'</div>');
    }else{
		   document.write('<a  class="'+ t +'" id="dhtml_'+selectid+'_'+i+'" href="'+selectbox.options[i].value+'"  >'+selectbox.options[i].text+'</a>');
    }


		//document.write('<a href="'+selectbox.options[i].value+'"  >'+selectbox.options[i].text+'</a>');

    anch_item[i]= document.getElementById('dhtml_'+selectid+'_'+i) ;
    anch_item[i].onclick=function(){
        document.getElementById('dsel_'+selectid).innerHTML= this.innerHTML;
        if (typeof cena!="undefined"  && typeof type!="undefined" && type=='nr'){
                  if (document.getElementById('cena_'+selectid)){
                       document.getElementById('cena_'+selectid).innerHTML= cena_f(cena*this.innerHTML);
                 }
        }
    }
  }
	document.write('</div><div class="dhtml_arr"></div></div>')
  //alert('co');
	selectbox.style.display="none" ;
	var dhtmlselectbox=document.getElementById("dhtml_"+selectid) ;
	//dhtmlselectbox.style.zIndex=combozindex  //dla wielu kombo
	//combozindex--
	if (typeof selwidth!="undefined"){
		dhtmlselectbox.style.width=selwidth;
  }
	if (typeof optionwidth!="undefined"){
		dhtmlselectbox.getElementsByTagName("div")[0].style.width=optionwidth;
  }

  //dhtmlselectbox.getElementsByTagName("div")[0].style.top=dhtmlselectbox.offsetHeight+"px";

	dhtmlselectbox.onmouseover=function(){
		this.getElementsByTagName("div")[0].style.display="block" ;
	}

	dhtmlselectbox.onmouseout=function(){
		this.getElementsByTagName("div")[0].style.display="none" ;
	}
	dhtmlselectbox.onclick=function(){
    if (this.getElementsByTagName("div")[0].style.display=="none"){
		   this.getElementsByTagName("div")[0].style.display= "block" ;
    }else{
       this.getElementsByTagName("div")[0].style.display= "none" ;
    }

		//this.getElementsByTagName("div")[0].style.display="none" ;
	}

}




/*
name - nazwa cookie
value - wartosc cookie
expire - długość życia cookie
*/
function setDaysCookie(name,value,expire){
	setCookie(name,value,expire*24*3600*1000)
}
function setMinutesCookie(name,value,expire){
	setCookie(name,value,expire*60*1000)
}

function setCookie(name,value,expire) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expire ));
    document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/";
}

/*
function setCookie(name,value,expire) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expire ));
    document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}*/

function getCookie(name){
    if (document.cookie.length > 0) {
		begin = document.cookie.indexOf(name+"=");
		if (begin != -1) {
			begin += name.length+1;
		    end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
				return unescape(document.cookie.substring(begin, end));
		}
    }
    return false;
}
function delCookie (name) {
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function isEnabledCookies(){
	setMinutesCookie("isEnabled", "true", 1);
	if (!(getCookie("isEnabled")=="true")){
		return false;
	}
	return true;
}

var basket_op_fel="";

function basket_ile( id, type){
   //koszyk= trim(koszyk);
   var ar= basket_op_fel.split(",");
   
   if (ar.length>0){  //usuniecie pierwszego pustego elementu.
      var t= ar[0].split("_");
      if (t.length<3){
        // alert(ar.length);
         ar.shift();
         //alert(ar.length);
      }
      if (ar.length>0){
        //for(var k in ar){
        for(var i=0; i<ar.length; i++){
         //   alert(ar.length);
            var t= ar[i].split("_");
            if (t.length==3){
              if (t[0]==id&&t[2]==type){
                  return t[1];
              }
            }
        }
      }
   }
   return 1;
}

function basket_del_all(){
   basket_op_fel="";
}

function basket_ile_cookie( id, type){
    var a= 1;
    var t= getCookie("basket_op_fel");
    if (t !=false ){
       basket_op_fel= t;
       a= basket_ile(id, type);
    }
    return a; //domyślnie 4 koła
}

function basket_add_cookie( id, ile, type){
      var t= getCookie("basket_op_fel");
      if (t !=false ){ basket_op_fel= t; } else { basket_op_fel="" }
      basket_add( id, ile, type);
      setMinutesCookie("basket_op_fel", basket_op_fel,240);
}


function basket_add( id, ile, type){
   //koszyk= trim(koszyk);
   var ar= basket_op_fel.split(",");
//   alert(ar.toString());
   var bylo=0;
   if (ar.length>0){  //usuniecie pierwszego pustego elementu.
      var t= ar[0].split("_");
      if (t.length<3){
         //alert(ar.length);
         ar.shift();
        // alert(ar.length);
      }
      //alert(ar.length);
//alert(ar.toString());
      if (ar.length>0){
        //for (var k in ar){
        for(var i=0; i<ar.length; i++){
            //alert(k);
            var t= ar[i].split("_");
            if (t.length==3){
              
              if (t[0]==id&&t[2]==type){  //wyszukanie i nadpisanie.
                  bylo =1;
                  t[0]=id; t[2]= type; t[1]= ile;
              }
              ar[i]= t.join("_");
              if (ile<=0&&bylo){
                   ar[i]= "";
                   var t ;
                   t= ar.slice(0,i);
                   t.push(ar.slice(i+1, ar.length));
                   ar= t;
                   break;
              }
            }
        }
        //alert(i);
      }
   }
   if (!bylo){
     ar.push(id+"_"+ile+"_"+type);
   }
//alert(ar.toString());
   basket_op_fel= ar.join(",");



   if ( document.getElementById('basket_id') ){
       document.getElementById('basket_id').innerHTML= basket_op_fel;
   }
}


function to_int(liczba){
   var temp= parseInt(liczba,10);
   if ( temp!=liczba){
     return 0;
   }
   return temp;
}

function robi(){
	alert('robi');
}

function is_int(ob, oldValue){
	str= parseInt(ob.value,10);
	if (ob.value!=str||str<0){
		ob.value=oldValue;
	}
}


    function show_hide(id){
      if (document.getElementById(id) ){
          if (document.getElementById(id).style.display=='block')
              document.getElementById(id).style.display='none';
          else
              document.getElementById(id).style.display='block';
      }
    }


    function cena_f(cena){
       var cenaf= parseFloat(cena)
       if (cena!="NaN"){
          return cenaf.toFixed(2);
       }
       else{
         alert("Bląd skontaktuj się z sewisem.");
         return false;
       }
    }


    function set_int(ob, oldValue, max){
    	str= parseInt(ob.value,10);
//alert(str );

    	if (ob.value!=str){
    		ob.value=oldValue;
    	}
      if (str<1 ){
        ob.value=oldValue;
      }

      if (max!='undefined'&&str>max ){
        ob.value=max;
      }
    }


function trim(str){
    return lTrim(rTrim(str));
}
function lTrim(str){
    return str.replace(/^\s */, '');
}
function rTrim(str){
    return str.replace(/\s *$/, '');
}

function is_required(ob, kom){
	if (ob!=null && (trim(ob.value))==''){
    if (kom!=null) alert(kom);
		ob.focus();
		return false;
	}
	return true;
}

