  var start=0;
  var splatka=0;
  var spotreba=0;
  var inflace=0;
  var urok=0;
  var zhodnoceni=0;
  var roky = 0;
  var celkovyprijem=0;

function preservedincome(onlyfirst){
  incm=Math.round((parseInt($("#capital").attr('value'))*(parseInt($('#assumedreturn').attr('value'))-parseInt($('#inflation').attr('value')) )/100)/12);
  if (!onlyfirst){
    $('#initialincome').attr('value',incm);
  }
  $('#res1').text(addCommas(incm));
}

function vypocitejrok(soucasna_hodnota,platba,urok){
  urok = 1+((urok/12)/100);
  kz=0;
  for (i=1;i<=12;i++){
    if (i==1){
      pz = (soucasna_hodnota * urok) - soucasna_hodnota;
      aktplatba = soucasna_hodnota + platba;
      kz = pz + aktplatba;
    } else {
      aktplatba = platba;
      pz = kz * urok;
      kz = pz + aktplatba;
    }
  }
  start = kz;
  return kz;
}
  
function vypocitejcele(starth,splatkah,urokh,zhodnocenih,starih){
  start=parseInt(starth);
  splatka=-parseInt(splatkah);
  urok=parseInt(urokh);
  zhodnoceni=parseInt(zhodnocenih);
  roky = 0;
  celkovyprijem = 0;
  starih=parseInt(starih);
  var d1=[];
  var d2=[];
  for (j=starih;j<=(starih+40);j++){
    d1.push([j,start]);
    d2.push([j,-(splatka*12)]);
    vypocitejrok(start,splatka,urok);
    if (start<0){
      start=0;
      break;
    } else {
       roky ++;
    }
    celkovyprijem = celkovyprijem + (splatka * 12);
    splatka = splatka * (1+(zhodnoceni/100));
  }
  if (roky>40){
    roky='40>';
  }
  $('#result').show();
  //$('#res1').text(Math.round((parseInt(starth)*0.06)/12));
  preservedincome(true);
  $('#totalcapital').text('R'+addCommas(Math.round(Math.abs(celkovyprijem))));
  $('#years').text(roky+' years');
  $.plot($('#placeholder'),[{label: 'Capital', color: '#4d4d4d', data: d1, lines: {show: true,fill: true, steps: false}},{label: 'Annual Income Needed', color: '#87180f', data: d2, lines: {show: true,fill: true, steps: false}}],{yaxis: {tickFormatter: function (v, axis) { return "R"+addCommas(v.toFixed(axis.tickDecimals)) }}});
  $('#contactus').show();
  $('#email2').attr('value','Hello World');
  $('#thank_you').attr('value','thank-you-3');
  $('#details').attr('value','Retirement age: '+starih+', Capital: '+starth+', Years Capital Lasts: '+roky+', Total Income Capital Generates: '+Math.round(Math.abs(celkovyprijem)));
}

function vypocitejrok2(soucasna_hodnota){
	aktzhodnoceni=1+((zhodnoceni)/100);
	akturok = 1+((urok/12)/100);
    kz=0;
    for (i=1;i<=12;i++){
      if (i==1){
        pz = (soucasna_hodnota * akturok) - soucasna_hodnota;
        aktplatba = soucasna_hodnota + spotreba;
        kz = pz + aktplatba;
      } else {
        aktplatba = spotreba;
        pz = kz * akturok;
        kz = pz + aktplatba;
      }
    }
	spotreba=spotreba*aktzhodnoceni; 
    start = kz;
	//alert(kz);
  }
  
function vypocitejcele2(starth,spotrebah,veknyni,vekpotom,aktrok,urokh,zhodnocenih,inflaceh,lump){
  if (!(veknyni && vekpotom)){
     alert("You have to fill current and retirement ages.");
     return false;
  }
  if (veknyni>=vekpotom){
    alert("Retirement age have to be higher than your current age");
    return false;
  }
  var d1=[];
	start=starth;
	spotreba=spotrebah;
	zhodnoceni=zhodnocenih;
	urok=urokh;
	inflace=inflaceh;
	roky=veknyni-vekpotom;
	for (j=veknyni;j<vekpotom;j++){
    d1.push([j,start]);
		vypocitejrok2(start);
	}
  kz+=lump;
  d1.push([vekpotom,kz]);
  $('#totalsum').text(addCommas(Math.round(kz)));
  $('#todaysum').text(addCommas(Math.round(kz*Math.pow(1+(inflace/100),roky))));
  $('#realisticsum').text(addCommas(Math.round(kz*((urok-inflace)/100)/12)));
  $('#total2sum').text(addCommas(Math.round((kz*((urok-inflace)/100)/12)*Math.pow(1+(inflace/100),roky))));
  $.plot($('#placeholder'),[{label: 'Retirement Capital', color: '#87180f', data: d1, lines: {show: true,fill: true, steps: false}}],{yaxis: {tickFormatter: function (v, axis) { return "R"+addCommas(v.toFixed(axis.tickDecimals))  }}});
  $('#resulttab').show();
  $('#contactus').show();
  $('#email2').attr('value','Hello World');
  $('#thank_you').attr('value','thank-you-2');
  $('#details').attr('value','Current age: '+veknyni+', Retirement age: '+vekpotom+', Capital: '+starth+', Contribution: '+spotrebah+', Total Retirement Capital at Retirement: '+Math.round(kz));
  createCookie('retcapital',Math.round(kz),60);
  createCookie('retage',vekpotom,60);
  createCookie('retinflace',inflaceh,60);
  createCookie('retassumed',urokh,60);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(document).ready(function(){

              $('#smap>li:eq(9)').remove();
              $('#smap a[href*=thank]').closest('ul').remove();
              $('#smap>li:eq(1) li a').attr('class','cbox').click(function(){$("#tab3").hide();});
              $('#smap>li:nth-child(5n+1)').css('clear','left');

              $('#indicators>div:eq(1)').load('footer/indicators');
              $('#fundprices>div:eq(1)').load('footer/fund-prices');
              $('#quicklinks a:first').ceebox({htmlWidth: 200, htmlHeight: 350, titles: false});
              //$('#graph1,#graph2,.cbox,a[href$=invest-with-us]').ceebox({htmlWidth: 710, htmlHeight: 560, titles: false});
          /* ==== campain only ====*/
              $('#graph1,#graph2,.cbox').click(function(){
                tb_show($(this).text(),$(this).attr('href')+'/?KeepThis=true&height=560&width=710');
                return false;
              });
              $('a[href$=who-says---form]').click(function(){
                tb_show('','who-says---form/?KeepThis=true&height=500&width=750');
                return false;
              });
              
              $('#contactform').submit(function(){
                alert('test');
                return false;
              });
						   
				/* ----------------  Fade effet here --------------------- */		   
						   /*$(".latest_img").fadeTo("slow", 1); // This sets the opacity of the thumbs to fade down to 30% when the page loads
						   $(".latest_img").hover(function(){
						   $(this).fadeTo("slow", 0.9); // This should set the opacity to 100% on hover
						   },function(){
						   $(this).fadeTo("slow", 1); // This should set the opacity back to 30% on mouseout
						   });*/
						   
						   
						   $(".grp1").hover(function(){
						   $(this).css("background", "#ccc"); 
						   },function(){
						   $(this).css("background", "#fff"); 
						   });
						   
				/* -----------------------------------------------------------*/		   
				
						   $(".features>ul>li>div>.caption").addClass("cap-bg");
						   $(".features>ul>li>div>.caption1").addClass("cap-bg");
						   $(".features>ul>li>div>.caption2").addClass("cap-bg");
						   $(".features>ul>li>div>.caption3").addClass("cap-bg-hover");
						  
						  
						   
						   $("img.g").animate({"opacity": "0"}, 0);

						   $("img.a").hover(
							function() {
							$(this).animate({"opacity": "0"}, "slow");
							$(".features>ul>li>div>.caption").removeClass("cap-bg");
						   $(".features>ul>li>div>.caption").addClass("cap-bg-hover");
							},
							function() {
							$(this).animate({"opacity": "1"}, "slow");
							$(".features>ul>li>div>.caption").addClass("cap-bg");
						   $(".features>ul>li>div>.caption").removeClass("cap-bg-hover");
							});
						   
						   
						   
						   $("img.c").hover(
							function() {
							$(this).animate({"opacity": "0"}, "slow");
							$(".features>ul>li>div>.caption1").removeClass("cap-bg");
						   $(".features>ul>li>div>.caption1").addClass("cap-bg-hover");
							},
							function() {
							$(this).animate({"opacity": "1"}, "slow");
							$(".features>ul>li>div>.caption1").addClass("cap-bg");
						   $(".features>ul>li>div>.caption1").removeClass("cap-bg-hover");
							});
						   
						   
						   $("img.e").hover(
							function() {
							$(this).animate({"opacity": "0"}, "slow");
							$(".features>ul>li>div>.caption2").removeClass("cap-bg");
						   $(".features>ul>li>div>.caption2").addClass("cap-bg-hover");
							},
							function() {
							$(this).animate({"opacity": "1"}, "slow");
							$(".features>ul>li>div>.caption2").addClass("cap-bg");
						   $(".features>ul>li>div>.caption2").removeClass("cap-bg-hover");
							});
						   
						   
						   
						   
						   

						   
						   
						   
						   $("img.g").hover(
							function() {
							$(this).animate({"opacity": "0"}, "slow");
							$(".features>ul>li>div>.caption3").removeClass("cap-bg");
						    $(".features>ul>li>div>.caption3").addClass("cap-bg-hover");
							},
							function() {
							$(this).animate({"opacity": "1"}, "slow");
							$(".features>ul>li>div>.caption3").addClass("cap-bg");
						    $(".features>ul>li>div>.caption3").removeClass("cap-bg-hover");
							});

						   
						   
						   /* ------------------------- LIGHT BOX ----------------------*/
						   //	$('#gallery a').lightBox();
						   /* -----------------------------------------------------------*/
						   
						
						
							// toggle slide
									$('.funds').click(function(){
									// by calling sibling, we can use same div for all demos
									
									$("#tab1").slideToggle("slow");
									$("#tab2").hide();
									$("#tab3").hide();
									
									});
									
									
									$('.ind').click(function(){
									// by calling sibling, we can use same div for all demos
									
									$("#tab2").slideToggle("slow");
									$("#tab1").hide();
									$("#tab3").hide();
									
									});
									
									
									$('.site').click(function(){
									// by calling sibling, we can use same div for all demos
									
									$("#tab3").slideToggle("slow");
									$("#tab1").hide();
									$("#tab2").hide();
									
									});
									
									
									
									
									/*$('.tabs>a').click(function(){
									// by calling sibling, we can use same div for all demos
									
									$("#footer").slideToggle("slow");
									
									});*/
									
									
									
									
									/* -------------------------------- CODE FOR SLIDER --------------------------*/
										
										/* **** code for slider ******* */				   
											$('#myslides').cycle({
												fx: 'fade',
												speed: 1000,
												timeout: 5000,
												random: 1
											});
											
											
		
									///////////////////////////	


/* ------------------------------------------------------------------------*/
									
						   
});

