// JavaScript Document

	var ortalamaTime=0;	
	var year=0;
	var month=0;
	var day=0;
	var basetarih = new Date ();
	basetarih.setYear(2000);
	basetarih.setMonth(1);
	basetarih.setDate(1);
	basetarihTime = basetarih.getTime();
		
	var decpoint=".";
	var sep=",";

function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}
function parakurusu(num, decpoint, sep){
  var x=FormatNumberBy3(num, decpoint, sep);
  $("gtutar").value=x;     
}

function Ekle(){
	
	if((!$("tutar").value && $("tutar").value=="") || (!$("tarih").value && $("tarih").value=="")) {
		alert("Değer giriniz");
		return false;
	}
	
	var opt=new Option();
	opt.value=$("tarih").value+'__'+$("tutar").value;
	opt.text=$("tarih").value+"__"+$("tutar").value;
	$("ortalama").options[$("ortalama").options.length]=opt;		
	return true;
}

function Cikar(sec){

	var rmv=$("ortalama");
	if(sec=="temizle" && rmv.options.length>0){
		rmv.options[0].selected="selected";			
		rmv.remove(rmv.selectedIndex);
		if(rmv.options.length>0) cikar(sec);
	}
	else{
		if(rmv.selectedIndex==-1){
			alert("Seçim yapınız");
			return false;
		}
		rmv.remove(rmv.selectedIndex);
	}	
	return true;
}

function Hesapla(){

	if($("ortalama").options.length==0) 	{
		alert("Değer giriniz");
		return false;
	}	
	
	var deger=0;
	var toplamtutar=0;	
	for(var i=0;i<$("ortalama").options.length;i++){
		year=0;
		month=0;
		day=0;
		year=$("ortalama").options[i].value.substr(6,4);
		month=$("ortalama").options[i].value.substr(3,2);
		day=$("ortalama").options[i].value.substr(0,2);
		var tarih = new Date ();
		tarih.setYear(year);
		tarih.setMonth(month);
		tarih.setDate(day);
		tarihTime = tarih.getTime();		
		deger=deger+(tarihTime-basetarihTime)*parseFloat($("ortalama").options[i].value.substr($("ortalama").options[i].value.search("__")+2).replace(",","."));
		toplamtutar=toplamtutar+parseFloat($("ortalama").options[i].value.substr($("ortalama").options[i].value.search("__")+2).replace(",","."));
	}
	ortalamaTime=deger/toplamtutar;
	var ortalamatarih=new Date(basetarihTime+ortalamaTime);
	var toplamtutar=FormatNumberBy3(toplamtutar, decpoint, sep);
  	$("ortalamatarihdegeri").innerHTML="Ortalama Tarih = "+ortalamatarih.getDate()+"/"+ortalamatarih.getMonth()+"/"+ortalamatarih.getFullYear()+"<br>"+"Toplam tutar = "+toplamtutar;
}