function showLogin(){
    wwidth = document.body.clientWidth;
    wleft=(wwidth/2)-(250/2);
    document.getElementById('login_div').style.left = wleft + "px";
    document.getElementById('login_div').style.display = 'block';
}

function submitform(id){
	if (id != "") document.getElementById(id).submit();
}

function hide_element(id,value){
	if (value == "0" && document.getElementById(id)) document.getElementById(id).style.display = "none";
}
function set_icons(id,value){
	if (value == "1" && document.getElementById(id)) document.getElementById(id).style.display = "inline";
}

    function maxLength(obj,i) {
        if (obj.value.length >= i) {
            obj.value = obj.value.slice(0,i);
        }
    }

function sfHover() {
    var menu_id=Array("nav");
    for(k=0;k<menu_id.length;k++){
        var sfEls = document.getElementById(menu_id[k]).getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            if(navigator.appName!="Netscape"){
                sfEls[i].onmouseover=function() {
                    this.className+=" sfhover";
                }
                sfEls[i].onmouseout=function() {
                    this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
                }
            }
        }
    }
}
function addtobasket(id,cat_id){
	document.location="?action=products.view&id="+id+"&cat_id="+cat_id+"&addtobasket="+document.getElementById('amount'+id).value;
}

function addtobasket_list(id,cat_id){
	document.location="?action=products.browse&id="+cat_id+"&addtobasketid="+id+"&addtobasket="+document.getElementById('amount'+id).value;
}

function deletefrombasket(id){
	var a = confirm('Kustutan?',"Jah","Ei");
	if (a==true) document.location="?action=basket.delete&id="+id+"&amount="+document.getElementById('amount_'+id).value;
}

function navigate(link,id){
document.write("<form id=\"nav_form\" method=\"POST\" action=\"?\"><input type=\"hidden\" name=\"action\" value=\""+link+"\"><input type=\"hidden\" name=\"id\" value=\""+id+"\"></form>");

document.getElementById('nav_form').submit();
}


function cust_confirm(link,text){
var a = confirm(text,"Jah","Ei");
if (a==true) parent.location.href=link;
}

function change_edit_language(obj,url){
	parent.location="?alang="+obj.value+"&"+url;
}

function editwin(obj,wh,ww,wintitle){
	window.name='mainwin';
	wleft=(screen.width/2)-(ww/2);
	wtop=(screen.height/2)-(wh/2);
	newwin=window.open(obj, wintitle, "width="+ww+",height="+wh+",top="+wtop+",left="+wleft+",status=yes,toolbar=no,menubar=no,scrollbars=yes");		
	newwin.focus();
}

function helpwin(param){
	ww=500;
	wh=400;
	wleft=(screen.width/2)-(ww/2);
	wtop=(screen.height/2)-(wh/2);
	newwin=window.open("help.php?"+param,"helpWin", "width="+ww+",height="+wh+",top="+wtop+",left="+wleft+",status=yes,toolbar=no,menubar=no,scrollbars=yes");		
	newwin.focus();
}
	function calculate_loan(obj,obj2,periood,intress){
		var summa = obj.value;
		intress=parseInt(intress)/100;
		ssumm=parseFloat(summa);
		if (isNaN(ssumm)) return 0;
		var vast=summa/periood;
		
		var pohiosa=vast;
		
		if (!isNaN(ssumm)) document.getElementById(obj2).value = parseInt(pohiosa);

		//maksegraafiku arvutamine
		var ca=0;
		var cb=summa;
		var cc=0;
		var cd=0;
		var ce=0;
		var i=0;
		var isumm=0;
		var psumm=0;
		var tsumm=0;
		var a=new Array();
		var b=new Array();
		var c=new Array();
		var d=new Array();
		var e=new Array();
		while (i<=periood){
			a[i]=(i==0)?"Algus":i;
			b[i]=Math.abs(cb);
			b[i]=b[i]<0.15?0:b[i];
			c[i]=cc;
			d[i]=(i==0)?0:parseFloat(pohiosa);
			//alert (d[i]);
			e[i]=ce;
			ca++;
			cd=pohiosa;
			cb=b[i]-pohiosa;
			cc=intress/12*b[i];
			ce=parseFloat(cc)+parseFloat(pohiosa);
			lim=cb;
			isumm+=parseFloat(c[i]);
			psumm+=parseFloat(d[i]);
			tsumm+=parseFloat(e[i]);
			i++;
		}
	}

var data_ref = "";
var data_obj = "";
var bb_objectlist;

function readdata(){
	var DataValue = top.frames['dataconnection'].document.documentElement.innerHTML;
	var DataValue = DataValue.replace("<HEAD></HEAD>\r\n<BODY>","");
	var DataValue = DataValue.replace("</BODY>","");
	var DataValue = DataValue.replace("<head><title></title></head><body></body>","");
	var DataValue = DataValue.replace("<head></head><body>","");
	var DataValue = DataValue.replace("</body>","");
	if (DataValue != "") {
		if (data_ref == ""){
			var data_array = DataValue.split(";");
			var current_price = 0;
			var transport = 0;
			var dealer = 0;
			var base = 0;

			if (document.getElementById("data_price_own").value != "") current_price = parseFloat(document.getElementById("data_price_own").value);
			if (data_array[0] != "") transport = parseFloat(data_array[0]);
			if (data_array[1] != "") dealer = parseFloat(data_array[1]);
			if (data_array[2] != "") base = parseFloat(data_array[2]);
			var warranty = data_array[3];			

			document.getElementById("data_prules_dealer").value = dealer;
			document.getElementById("data_prules_base").value = base;
			document.getElementById("data_prules_transport").value = transport;

			var price = current_price + transport;
			price = price * (1+(dealer / 100));
			document.getElementById("data_dealer").value = Math.round(price*100)/100;
			price = price * (1+(base / 100));
			document.getElementById("data_price").value = Math.round(price*100)/100;
			document.getElementById("data_warranty").value = warranty;
			calculate_loan(document.getElementById("data_price"),'data_price_loan',12,12);
		}
		if (data_ref == "bb"){
			var prod = document.getElementById(data_obj);
			if (DataValue == "0") {
				prod.options.length = 0;
				prod.options[0] = new Option("Tooteid pole","-1");
				prod.disabled = true;
			} else {
				prod.options.length = 0;
				prod.disabled = false;
				var data_array = DataValue.split(";");
				for (var i=0; i<data_array.length; i++){
					var tmp = data_array[i].split("|");
					if (tmp[0] != "") prod.options[prod.options.length] = new Option(tmp[1],tmp[0]);
				}
			}
		}
		if (data_ref == "bb_default"){
			var prod = document.getElementById(data_obj);
			if (DataValue == "0") {
				prod.options.length = 0;
				prod.options[0] = new Option("---","0");
				prod.disabled = false;
			} else {
				prod.options.length = 0;
				prod.options[0] = new Option("---","0");
				prod.disabled = false;
				var data_array = DataValue.split(";");
				for (var i=0; i<data_array.length; i++){
					var tmp = data_array[i].split("|");
					if (tmp[0] != "") prod.options[prod.options.length] = new Option(tmp[1],tmp[0]);
				}
			}
		}		
	};
}

function getPRules(cat_obj,manu_obj){
	var cat = document.getElementById(cat_obj).options[document.getElementById(cat_obj).selectedIndex].value;
	var manu = document.getElementById(manu_obj).options[document.getElementById(manu_obj).selectedIndex].value;
	data_ref = "";
	document.getElementById("dataconnection").src="";		
	document.getElementById("dataconnection").src="?action=prules.data&set_mode=edit&var1="+cat+"&var2="+manu;
}

function setPRules(){
	var current_price = 0;
	var transport = 0;
	var dealer = 0;
	var base = 0;
	if (document.getElementById("data_price_own").value != "") current_price = parseFloat(document.getElementById("data_price_own").value);
	if (document.getElementById("data_prules_transport").value != "") transport= parseFloat(document.getElementById("data_prules_transport").value);
	if (document.getElementById("data_prules_dealer").value != "") dealer = parseFloat(document.getElementById("data_prules_dealer").value);
	if (document.getElementById("data_prules_base").value != "") base = parseFloat(document.getElementById("data_prules_base").value);

	var price = current_price + transport;
	price = price * (1+(dealer / 100));
	document.getElementById("data_dealer").value = Math.round(price*100)/100;
	price = price * (1+(base / 100));
	document.getElementById("data_price").value = Math.round(price*100)/100;
	calculate_loan(document.getElementById("data_price"),'data_price_loan',12,12);
}

function add_selected_items(from_obj,to_obj){
	var from_o = document.getElementById(from_obj);
	var to_o = document.getElementById(to_obj);
	for (var i = 0; i < from_o.options.length; i++){
		if (from_o.options[i].selected) to_o.options[to_o.options.length] = new Option(from_o.options[i].text,from_o.options[i].value);
	}
}

function del_selected_items(from_obj){
	var from_o = document.getElementById(from_obj);
	for (var i = from_o.options.length-1; i >= 0; i--){
		if (from_o.options[i].selected) from_o.options[i] = null;
	}
}

function set_all_selected(){
  	for (var a = 0; a < set_all_selected.arguments.length; a++){
		if (document.getElementById('data_'+set_all_selected.arguments[a]) && set_all_selected.arguments[a] != ""){
			var from_o = document.getElementById('data_'+set_all_selected.arguments[a]);
			for (var i = 0; i < from_o.options.length; i++){
				from_o.options[i].selected = true;
			}
		}
	}
}

function bb_get_products(cat_obj,prod_obj){
	var cat = cat_obj.options[cat_obj.selectedIndex].value;
	var prod = document.getElementById(prod_obj);
	prod.options.length = 0;
	prod.options[0] = new Option("Laen andmeid ..","-1");
	prod.disabled = true;
	data_ref = "bb";
	data_obj = prod_obj;
	document.getElementById("dataconnection").src="";		
	document.getElementById("dataconnection").src="?action=products.data&set_mode=edit&var1="+cat;
}

function bb_get_products_default(cat_obj,prod_obj){
	var cat = cat_obj.options[cat_obj.selectedIndex].value;
	var prod = document.getElementById(prod_obj);
	prod.options.length = 0;
	prod.options[0] = new Option("Laen andmeid ..","-1");
	prod.disabled = true;
	data_ref = "bb_default";
	data_obj = prod_obj;
	document.getElementById("dataconnection").src="";		
	document.getElementById("dataconnection").src="?action=products.data&set_mode=edit&var1="+cat;
}

function bb_calculate_price(){
	var total = 0;
	for (var i=1; i<objectlist.length; i++){
		var obj = document.getElementById(objectlist[i]);
		var prod_id = obj.options[obj.selectedIndex].value;
		total += bb_getProductPrice(prod_id);			
	}
	document.getElementById('bb_component_price').innerHTML = total;
	document.getElementById('bb_total_price').innerHTML = parseFloat(bb_base_price) + total;
}

function bb_getProductPrice(prod_id){
	for (var i=0; i<price_array.length-1; i++){
		if (price_array[i][0] == prod_id) return parseFloat(price_array[i][1]);
	}
	return 0;
}

function select_moveup(obj_name){
	var obj = document.getElementById(obj_name);
	var sel = obj.selectedIndex;
	if (sel > 0){
		var opt = obj.options[sel];
		var opt2 = obj.options[sel-1];
		obj.options[sel] = new Option(opt2.text,opt2.value);
		obj.options[sel-1] = new Option(opt.text,opt.value);
		obj.selectedIndex = sel - 1;
	}
}

function select_movedown(obj_name){
	var obj = document.getElementById(obj_name);
	var sel = obj.selectedIndex;
	if (sel < obj.options.length-1 && sel > -1){
		var opt = obj.options[sel];
		var opt2 = obj.options[sel+1];
		obj.options[sel] = new Option(opt2.text,opt2.value);
		obj.options[sel+1] = new Option(opt.text,opt.value);
		obj.selectedIndex = sel + 1;
	}
}

function calculate_vatprice(obj,obj2){
	if (document.getElementById(obj2)) document.getElementById(obj2).value = Math.round(parseFloat(obj.value) * 1.18 * 100) / 100;
}

function calculate_pricefromvat(obj,obj2){
	if (document.getElementById(obj2)) document.getElementById(obj2).value = Math.round(parseFloat(obj.value) / 1.18 * 100) / 100;
}


function set_model(cat_obj,url){
	var cat = cat_obj.options[cat_obj.selectedIndex].value;
	document.location=url+".view&id="+cat;
}

function set_type(cat_obj,url,param){
	var cat = cat_obj.options[cat_obj.selectedIndex].value;
	document.location=url+"&"+param+"="+cat;
}

function set_model2(cat_obj,url){
	var cat = cat_obj.options[cat_obj.selectedIndex].value;
	document.location=url+".view&model="+cat;
}