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 currencyEgo(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;
		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;
		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 > 2008) {
		taxRate = 0.20;
	} else {
		taxRate = 0.26;
	}
	returnPayment = Math.round(yearInterest * taxRate * 100) / 100;
  if (returnPayment > 50000)
  	returnPayment = 50000;
	return returnPayment;
}
