function setCookie(name,value,ndays){
  var today = new Date();
  var expire = new Date();
  if (ndays==null || ndays==0) ndays=1;
  expire.setTime(today.getTime() + 3600000*24*ndays);
  document.cookie = name+"="+escape(value)
                 + ";expires="+expire.toGMTString();
}

function readCookieByName(name){
  name+="="; 
  startCookie=document.cookie.indexOf(name); //sprawdza czy istnieje cookie o podanej nazwie

  if (startCookie==-1) {return ""} //jeżeli nie istnieje
  startCookie+=name.length; //początek treści cookie
  
  if (document.cookie.indexOf(";",startCookie)==-1) //jeżeli jest więcej cookie
    endCookie=document.cookie.length; //jeżeli nie ma podstawia jako koniec cookie, koniec całego tekstu
  else
    endCookie=document.cookie.indexOf(";",startCookie); // podstawia pod koniec miejsce występowania ";" (czyli początek następnego cookie)

  textCookie=document.cookie.substring(startCookie,endCookie); //kopiuje treść cookie
  return textCookie; //zwraca wartość
}


function readCookieFundType(name){
  
  fundtype=readCookieByName(name);

  switcher=0;
  if(fundtype=='A') {
      document.formtab.type.value='1';
       switcher=1;
  }
  else if(fundtype=='S') {
      document.formtab.type.value='2';
       switcher=2;
  }
  else if(fundtype=='T') {
      document.formtab.type.value='3';
       switcher=3;
  }
 
      if( switcher>0&&name=='tabDuzaFundType') {
          switchOmTab(switcher);
      }
      else if(switcher>0&&name=='tabMiniFundType') {
        switchMiniTab(switcher);
      }
      
}

function readCookieCalc(nr){
    fund=readCookieByName('calcFund');

    if(fund!=null&&fund>0) document.kalkulator.fund.value=fund;
    
    if(nr==1){
        fundtype = readCookieByName('calcFundType');
        if(fundtype=='A')      document.kalkulator.type.value='1';
        else if(fundtype=='S') document.kalkulator.type.value='2';
        else if(fundtype=='T') document.kalkulator.type.value='3';
    }
    daty = readCookieByName('calcZakresDat');

    if(daty!=null&&daty!='') {
      selectDates(daty);
    }   
}


function to_float(snumber) {
        snumber = snumber.replace(".",",");
    		if (snumber.match(/^[0-9]+$/)==null && snumber.match(/^[0-9]+\,[0-9]*$/)==null && snumber!="") ret = parseFloat(snumber);
    		else ret = snumber;	
    		if(!ret&&ret!="") ret="";
    		return ret;
}

function to_ilosc(snumber) {
        snumber = snumber.replace(".",",");
        coma=snumber.indexOf(",",0);
        if(coma!=-1) snumber=snumber.substring(0,coma)+snumber.substring(coma,coma+4);
    		return to_float(snumber);
}

function to_integer(snumber){
		  if (snumber.match(/^[0-9]+$/)==null && snumber!="") ret = parseInt(snumber);
    	else ret = snumber;	
    	if(!ret&&ret!="") ret="";
    	return ret;
}

function switchOmTab(type){

      for(i=0;i<15;i++){
        for(j=1;j<4;j++){
          if(j==type) {
              document.getElementById("td1_"+i+"_"+j).style.display="block";
              document.getElementById("td2_"+i+"_"+j).style.display="block";
              document.getElementById("td3_"+i+"_"+j).style.display="block";
          }
          else {
              document.getElementById("td1_"+i+"_"+j).style.display="none";
              document.getElementById("td2_"+i+"_"+j).style.display="none";
              document.getElementById("td3_"+i+"_"+j).style.display="none";
          }
          
        }
      }
      cookie='';
      if(type==1) cookie='A';
      else if(type==2) cookie='S';
      else if(type==3) cookie='T';
      if(cookie!='') setCookie('tabDuzaFundType',cookie,90);
}
    
function switchMiniTab(type){
      for(i=0;i<nfunds;i++){
        for(j=1;j<4;j++){
        //alert(tabactive+"_"+i+"_"+j);
          if(j==type) {
              document.getElementById(tabactive+"_"+i+"_"+j).style.display="block";
          }
          else {
              document.getElementById(tabactive+"_"+i+"_"+j).style.display="none";
          }
          document.getElementById(tabhidden+"_"+i+"_"+j).style.display="none";
          
        }
      }
      cookie='';
      if(type==1) cookie='A';
      else if(type==2) cookie='S';
      else if(type==3) cookie='T';
      if(cookie!='') setCookie('tabMiniFundType',cookie,90);
   pubType=type;   
}

function changeday(day,color){
    if(day==2) {
      tabactive='td';
      tabhidden='tpd';

    }
    else if(day==1){
      tabactive='tpd';
      tabhidden='td';
    }
    document.getElementById("menu").style.borderBottom="3px solid "+color;
    switchMiniTab(pubType);
}

function oblicz1(url) {
        var ilosc = document.kalkulator.ilosc.value;
        var fund = document.kalkulator.fund.options[document.kalkulator.fund.selectedIndex].value;
        
        var type = document.kalkulator.type.options[document.kalkulator.type.selectedIndex].value;
      
        var tstart = 
        document.kalkulator.year_start.options[document.kalkulator.year_start.selectedIndex].value
        +document.kalkulator.month_start.options[document.kalkulator.month_start.selectedIndex].value
        +document.kalkulator.day_start.options[document.kalkulator.day_start.selectedIndex].value;
        
        var tend = 
        document.kalkulator.year_start.options[document.kalkulator.year_end.selectedIndex].value
        +document.kalkulator.month_start.options[document.kalkulator.month_end.selectedIndex].value
        +document.kalkulator.day_start.options[document.kalkulator.day_end.selectedIndex].value;
      
        if(!fund||fund==0) alert("Proszę wybrać fundusz z listy");
        else if(!ilosc) alert("Proszę podać ilość nabytych jednostek");
        else if(tstart>=tend) alert("Data nabycia musi być wcześniejsza niż data umorzenia");
        else {
        
              setCookie('calcFund',fund,90);  
              setCookie('calcZakresDat',tstart+'-'+tend,1);   
              cookie='';
              if(type==1) cookie='A';
              else if(type==2) cookie='S';
              else if(type==3) cookie='T';
              if(cookie!='') setCookie('calcFundType',cookie,90);
      
            	new Ajax.Updater("ajax_result", url+"utils/ajax_kalkulator.php", {
		              method: "get",
		              evalScripts: true,
		              parameters: { "count": ilosc, "fund": fund, "type": type,"start": tstart, "end": tend }
	                 });
        }
}

function checkCalendar(suffix){
  //zmiania dzien na ostatni w miesiacu
  day=document.kalkulator["day_"+suffix].options[document.kalkulator["day_"+suffix].selectedIndex].value;

  if(day>28){
    month=document.kalkulator["month_"+suffix].options[document.kalkulator["month_"+suffix].selectedIndex].value;
    
    if(month==2) {
        year=document.kalkulator["year_"+suffix].options[document.kalkulator["year_"+suffix].selectedIndex].value;
          if(year%4==0) document.kalkulator["day_"+suffix].value=29;
          else document.kalkulator["day_"+suffix].value=28;
    }
    else if(day>30&&(month==4||month==6||month==9||month==11)) document.kalkulator["day_"+suffix].value=30;
  }
}

function selectDates(daty){

    document.kalkulator.year_start.value=daty.substring(0,4);
      document.kalkulator.month_start.value=daty.substring(4,6);
      document.kalkulator.day_start.value=daty.substring(6,8);
      
      document.kalkulator.year_end.value=daty.substring(9,13);
      document.kalkulator.month_end.value=daty.substring(13,15);
      document.kalkulator.day_end.value=daty.substring(15,17);
}
function loadDates(url,today){
      fund = document.kalkulator.fund.options[document.kalkulator.fund.selectedIndex].value;
      if(fund>0){
          new Ajax.Updater("ajax_result", url+"utils/ajax_kalkulator.php", {
		              method: "get",
		              evalScripts: true,
		              parameters: { "loadfund": fund }
	                 });
	    }              
}
function loadDates3(url,today){
      fund = document.kalkulator.fund.options[document.kalkulator.fund.selectedIndex].value;
      if(fund>0){
          var start = document.kalkulator.year_start.value + document.kalkulator.month_start.value + document.kalkulator.day_start.value;
          var end = document.kalkulator.year_end.value + document.kalkulator.month_end.value + document.kalkulator.day_end.value;
          new Ajax.Updater("ajax_result", url+"utils/ajax_kalkulator3.php", {
		              method: "get",
		              evalScripts: true,
		              parameters: { "loadfund": fund ,
                                "start" : start,
                                "end"   : end
                               }
	                 });
	    }              
}

function dispResult(tstart,fstart,tend,fend,prc,change,total,currency){
          document.kalkulator.wart_start.value=fstart+" "+currency;
          document.kalkulator.wart_end.value=fend+" "+currency;
          document.kalkulator.prc.value=prc;
          document.kalkulator.change.value=change+" "+currency;
          document.kalkulator.total.value=total+" "+currency;
          document.getElementById("start_info").innerHTML="<b>wycena z dnia: "+tstart+"</b>";
          document.getElementById("start_info").style.display="block";
          document.getElementById("start_input").style.display="none";
          document.getElementById("end_info").innerHTML="<b>wycena z dnia: "+tend+"</b>";
          document.getElementById("end_info").style.display="block";
          document.getElementById("end_input").style.display="none";
}
      
function resetForm(nr){
          document.kalkulator.reset();
          readCookieCalc(nr);
          if(nr==1){
          document.getElementById("start_info").style.display="none";
          document.getElementById("start_input").style.display="block";
          document.getElementById("end_info").style.display="none";
          document.getElementById("end_input").style.display="block";
          }
}

function round(num) {
	str = num.toString();
	dotInd = str.indexOf(".", 0);
	num = parseFloat(str.substr(0, dotInd+3));
	rest = parseFloat("0."+str.substr(dotInd+3, str.length-dotInd+3));
	if (rest>=0.5) {
		num = num+0.01;
	}
	return num;
}
      
function oblicz2(){
        var W = document.kalkulator.in_by_month.value;
        W=W.replace(",",".");
        var L = document.kalkulator.rates.value;
        var SR = document.kalkulator.percent.options[document.kalkulator.percent.selectedIndex].value;
    
    if(!W) {
        alert("Podaj wartość miesięcznej wpłaty");
        return;
    }
    if(!L) {
        alert("Podaj liczbę lat");
        return;
    }
	
    var suma = W * L*12;
	
    prc = SR / 100;
    nawias = 1 + prc / 12;
    potega = Math.pow(nawias, 12 * L);
    total = W * nawias * (1 - potega) / (1 - nawias);
    total = round((total*100)/100);
    document.kalkulator.suma.value=number_format(suma,2,',',' ')+' PLN';
    document.kalkulator.kapital.value=number_format(total,2,',',' ')+' PLN';
}
function oblicz3(url) {

        var fund = document.kalkulator.fund.options[document.kalkulator.fund.selectedIndex].value;
        var W = document.kalkulator.in_by_month.options[document.kalkulator.in_by_month.selectedIndex].value;

        var tstart = 
        document.kalkulator.year_start.options[document.kalkulator.year_start.selectedIndex].value
        +document.kalkulator.month_start.options[document.kalkulator.month_start.selectedIndex].value;
        
        var tend = 
        document.kalkulator.year_start.options[document.kalkulator.year_end.selectedIndex].value
        +document.kalkulator.month_start.options[document.kalkulator.month_end.selectedIndex].value;
      
        if(!fund||fund==0) alert("Proszę wybrać fundusz z listy");
        else if(!W) alert("Proszę kwotę miesięcznej wpłaty");
        else if(tstart>=tend) alert("Data nabycia musi być wcześniejsza niż data umorzenia");
        else {
        
              setCookie('calcFund',fund,90);  
              setCookie('calcZakresDat',tstart+'-'+tend,1);   

            	new Ajax.Updater("ajax_result", url+"utils/ajax_kalkulator3.php", {
		              method: "get",
		              evalScripts: true,
		              parameters: { "fund": fund, "fin_by_month": W,  "start": tstart, "end": tend }
	                 });
        }
}
function number_format( number, decimals, dec_point, thousands_sep ) {
    // Format a number with grouped thousands
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
    // +       version: 809.522
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}// }}}


