$().ready(function() {
	
	$("#slider").slides({
		container: 'slider-content',
		generatePagination: true,
		randomize: true,
		play: 5000
	});
	
	// inputi title kuvamine, kui input on tühi
	$('input[title]').each(function() {
		if(!$(this).val()) $(this).val($(this).attr('title')).addClass('example');
				
		$(this).bind('click', function() {
			if($(this).val() == $(this).attr('title')) $(this).val('').removeClass('example');
		})
		$(this).bind('blur', function() {
			if($(this).val() == '') $(this).val($(this).attr('title')).addClass('example');
		})
	})
	
	// tabeli ridade värvimine
	$('.zebra tbody tr:odd').addClass('odd');
	
	// e-posti aadressi mask
	$('[rel~="mail"]').each(function() {
		var str = $(this).text();
		var at = /\(at\)/;
		$(this).attr("href", "mailto:" + str.replace(at,"@"));
	});
	
	//lisainfo 
	$('small.info').hide()
	$('.leasing input').live('focus', function() {
		$(this).next('small.info').delay(1500).slideDown();
	});
	$('.leasing input').live('blur', function() {
		$(this).next('small.info').slideUp();
	});
	
});

var IE = isIE();
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;

function get_selected(object){
	for (var i = 0; i < object.length; i++) {
		if (object[i].selected) return(object[i].value);
	}
}
	
function set_selected(object, t_value){
	for (var i = 0; i < object.length; i++) {
		if (object[i].value==t_value) object[i].selected="TRUE";
	}
}

function isIE() {
	var verStr=navigator.appVersion, app=navigator.appName;	
	if ((app.indexOf('Microsoft') == -1))
		return false;
	else
		return true;
}
	
function clearCombo(input) {
	input.options.length=0;
}
	
function addOption(input, opt_name, opt_value) {
	if (IE) {
		var newEl = document.createElement("OPTION");
		newEl.text=opt_value;
		newEl.value=opt_name;
		input.options.add(newEl);
	} else {
		with (input) {
			olen=options.length;
			options[olen] = new Option(opt_value, opt_name, false, false);
		}
	}
}

function showHelp(id) {
	if (IE4) {
		document.all["help0"].style.visibility = "hidden";
		document.all["help" + id].style.visibility = "visible";
	} else if (!NS4) {
		document.getElementById("help0").style.visibility="hidden";
		document.getElementById("help"+id).style.visibility="visible";
	}
}

function hideHelp(id) {
	if (IE4) {
		document.all["help" + id].style.visibility = "hidden";
		document.all["help0"].style.visibility = "visible";
	} else if (!NS4) {
		document.getElementById("help"+id).style.visibility="hidden";
		document.getElementById("help0").style.visibility="visible";
	}
}


function makeNum(input) {
	str=input.value;
	if (str.indexOf(',')!=-1) {
		re= /,/gi;
		input.value=str.replace(re,'.');
	}
	str=input.value;
	if (str.indexOf(' ')!=-1) {
		re= / /gi;
		input.value=str.replace(re,'');
	}
	if (isNaN(input.value) || input.value=='') {
		input.focus();
		return false;
	} else {
		return true;
	}
}
	
function calcPayment(price, advanceSum, periodMonths, interest, residualValue) {
	if (interest == 0) {
		return ( price - advanceSum - (price * residualValue / 100) ) / periodMonths; 
	}	else {
		return (((interest / 100 / 12) * ((price - advanceSum)-((price * residualValue / 100) / (Math.pow((interest / 100 / 12) + 1, periodMonths)))) / (1 - (1 / Math.pow((interest / 100 / 12) + 1, periodMonths)))));
	}
}

function calcPaymentQt(price, advanceSum, periodQts, interest, residualValue) {
	if (interest == 0) {
		return ( price - advanceSum - (price * residualValue / 100) ) / periodQts; 
	}	else {
		return (((interest / 100 / 4) * ((price - advanceSum)-((price * residualValue / 100) / (Math.pow((interest / 100 / 4) + 1, periodQts)))) / (1 - (1 / Math.pow((interest / 100 / 4) + 1, periodQts)))));
	}
}


function currency(anynum) {
	if (isNaN(anynum) || anynum == '') {
		if (anynum == 0) {
			return ("0.00");
		} else {
			return (anynum);
		}
	} else {   
		anynum=eval(anynum);
		workNum=Math.abs(Math.round(anynum));
		workStr=""+workNum;
		dStr=workStr;
		dNum=dStr-0;
		/*
		pStr=workStr.substr(workStr.indexOf("."));
		while (pStr.length<3) {
			pStr+="0";
		}
		*/
		if (dNum>=1000) {
    			dLen=dStr.length;
    			dStr=parseInt(""+(dNum/1000))+" "+dStr.substring(dLen-3,dLen);
		}
		if (dNum>=1000000) {
			dLen=dStr.length;
			dStr=parseInt(""+(dNum/1000000))+" "+dStr.substring(dLen-7,dLen);
			//alert(dStr.substring(dLen-7,dLen));
		}
		//retval = dStr + pStr ;
		retval = dStr;
		//-- Put numbers in parentheses if negative.
		if (anynum<0) {retval="-"+retval};
		return retval;
	}
}

function currencyWCents(anynum) {
	if (isNaN(anynum) || anynum == '') {
		if (anynum == 0) {
			return ("");
		} else {
			return (anynum);
		}
	} else {   
		anynum=eval(anynum)
		workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
		if (workStr.indexOf(".")==-1){workStr+=".00"}
		dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
		pStr=workStr.substr(workStr.indexOf("."))
		while (pStr.length<3){pStr+="0"}
		if (dNum>=1000) {
	  			dLen=dStr.length
	  			dStr=parseInt(""+(dNum/1000))+" "+dStr.substring(dLen-3,dLen)
		}
		if (dNum>=1000000) {
			dLen=dStr.length
			dStr = dStr.substring(dLen-12,dLen);
		}
		retval = dStr + pStr 
		//-- Put numbers in parentheses if negative.
		if (anynum<0) {retval="-"+retval}
		return retval
	}
}

function calcReturnwYear(yearInterest, year)  {
	if (year == 2005) {
		taxRate = 0.24;
	} else if (year == 2006) {
		taxRate = 0.23;
	} else if (year == 2007) {
		taxRate = 0.22;
	} else if (year == 2008) {
		taxRate = 0.21;		
	} else if (year == 2009) {
		taxRate = 0.21;		
	} else if (year == 2010) {
		taxRate = 0.21;		
	} else if (year == 2011) {
		taxRate = 0.21;
	} else if (year > 2011) {
		taxRate = 0.21;
	} else {
		taxRate = 0.26;
	}
	
	maxReturn = taxRate * 3196;
	
	returnPayment = Math.round(yearInterest * taxRate * 100) / 100;
  	if (returnPayment > maxReturn)
  		returnPayment = maxReturn;
	return returnPayment;
}




function reCalcPayment() {
	if (checkPrice() && checkAdvanceSum() && checkInterest()) {
		payment = calcPayment(document.getElementById('ARTICLE_PRICE').value, document.getElementById('LEASING_ADVANCE_SUM').value, document.getElementById('LEASING_PERIOD').value, document.getElementById('LEASING_INTEREST').value, 0);
		
		if (isNaN(payment)) {
			clearRes();
		} else {
			document.getElementById('MONTH_PAYMENT').innerHTML = currency(payment);
		
		
		if (payment < parseFloat(6.5)) {
			alert("Kuumakse peab olema üle 6,5 EUR! Lühendage järelmaksu perioodi.");
		}
		document.getElementById('INTEREST_TOTAL').innerHTML = currency(payment*document.getElementById('LEASING_PERIOD').value-(document.getElementById('ARTICLE_PRICE').value-document.getElementById('LEASING_ADVANCE_SUM').value));}
	}
}
function changeInterest() {
	checkInterest();
	clearRes();
}
function checkInterest() {
	if (makeNum(document.getElementById('LEASING_INTEREST'))) {
		num=1*document.getElementById('LEASING_INTEREST').value;
		if (num < 0 || num > 50) {
			alert("Intress peab olema vahemikus 0% - 20%!");
			return false;
		} else {
			return true;
		}
	} else {
		alert("Intress on sisestamata või valesti sisestatud!");
		return false;
	}
}
function changePrice() {
	if(checkPrice()) {
		reCalcAdvanceSum();
		setInterest();
		clearRes();
	} else {
		clearAll();
	}
}
function setInterest() {
		num=1*document.getElementById('ARTICLE_PRICE').value;
		if (num < 1300) {
			//newinterest=19.9;
			newinterest=21.9;
		}  else if(num >= 1300) {
			newinterest=19.9;
		} 
		document.getElementById('LEASING_INTEREST').value = newinterest;
}
function changeAdvance() {
	if (checkAdvance() && document.getElementById('ARTICLE_PRICE').value != '') {
		reCalcAdvanceSum();
		clearRes();
	} else {
		clearAll();
	}
}
function changeAdvanceSum() {
		if (checkAdvanceSum()) {
			reCalcAdvance();
			clearRes();
		} else {
			clearAll();
		}
}
function reCalcAdvanceSum() {
	document.getElementById('LEASING_ADVANCE_SUM').value = Math.round(document.getElementById('LEASING_ADVANCE').value * document.getElementById('ARTICLE_PRICE').value / 100);
}
function reCalcAdvance() {
	document.getElementById('LEASING_ADVANCE').value = Math.round(document.getElementById('LEASING_ADVANCE_SUM').value / document.getElementById('ARTICLE_PRICE').value * 100);
}
function checkAdvance() {
	if (makeNum(document.getElementById('LEASING_ADVANCE'))) {
		num=1*document.getElementById('LEASING_ADVANCE').value;
		if (num < 0 || num > 70) {
			alert("Sissemakse peab olema vahemikus 5% - 70%!");
			return false;
		} else {
			return true;
		}
	} else {
		if (document.getElementById('LEASING_ADVANCE').value=="") {
			alert("Sissemakse väljad on sisestamata või valesti sisestatud!");
			return false;
		} else {
			alert("Sissemakse väljad on sisestamata või valesti sisestatud!");
			return false;
		}
	}
}

function checkAdvanceSum() {
	if (makeNum(document.getElementById('LEASING_ADVANCE_SUM'))) {
		num = 1*document.getElementById('LEASING_ADVANCE_SUM').value;
		if (num < 0 || num > 10000) {
			alert("Sissemakse summa peab olema vahemikus 6,5 - 10 000 EUR!");
			return false;
		} else if ((num > document.getElementById('ARTICLE_PRICE').value * .70) && !(document.getElementById('ARTICLE_PRICE').value == '' && document.getElementById('LOAN_SUM').value == '')) {
			alert("Soetava vara maksumus puudub või sissemakse on sellest suurem");
			return false;
		} else {
			return true;
		}
	} else {
		if (document.mainForm.LEASING_ADVANCE_SUM.value=="") {
			alert("Sissemakse summa on sisestamata või valesti sisestatud!");
			return false;
		} else {
			alert("Sissemakse summa on sisestamata või valesti sisestatud!");
			return false;
		}
	}
}
function checkPrice() {
	if (makeNum(document.getElementById('ARTICLE_PRICE'))) {
		num = 1*document.getElementById('ARTICLE_PRICE').value;
		if (num < 125 || num > 10000) {
			alert("Soetatava vara maksumus peab olema vahemikus 125  – 10 000 EUR!");
			return false;
		} else {
			return true;
		}
	} else {
		alert("Soetatava vara maksumus on sisestamata või valesti sisestatud!");
		return false;
	}
}
function clearAll() {
		document.getElementById('LEASING_ADVANCE').value = 10;
		document.getElementById('LEASING_ADVANCE_SUM').value = '';
		document.getElementById('ARTICLE_PRICE').value = '';
		//document.getElementById('LEASING_INTEREST').value = 15.9;
		document.getElementById('LEASING_INTEREST').value = 21.9;

	document.getElementById('MONTH_PAYMENT').innerHTML = '0';
	document.getElementById('INTEREST_TOTAL').innerHTML = '0';
}
function clearRes() {
	document.getElementById('MONTH_PAYMENT').innerHTML='0';
	document.getElementById('INTEREST_TOTAL').innerHTML='0';
}
