korv_tooted = new Array();


function calculateLeadTime(){
	
	if($("#quantityForLeadTime").val()=='' || $("#quantityForLeadTime").val()<1){
		$('#leadTimes').html('<span>Sisesta palun kogus!</span>');
		return;		
	}	
	$('#leadTimes').html('<img src="Images/loader.gif">');
	$.post('StockAPI/LeadTime.php', $("#tarneajaKalkulaator").serialize(), function(data){
	   $('#leadTimes').html(data);
	 });
}	

function ClearSearchBox()
{
	if(document.getElementById('SearchBox').value == 'Sisesta otsisõna siia...')
		{
			document.getElementById('SearchBox').value = '';
		}
}

function showTarne(id, status){
	if (document.getElementById(id) != null) document.getElementById(id).style.display=status;
}

function inc_x(siffer)
{
	return korv_muuda(siffer, 1);
}

function joonista_otsingu_t2pid()
{
	if(!window.mitmes_t2pp) mitmes_t2pp = 0;
	if(!window.kokku_t2ppe) kokku_t2ppe = 5;

	t2pid_html = '';
	for(i = 0; i < mitmes_t2pp; i++)
	{
		t2pid_html += '.';
	}
	t2pid_html += '<font color="red">.</font>';

	for(i = mitmes_t2pp + 1; i < kokku_t2ppe; i++)
	{
		t2pid_html += '.';
	}

	document.getElementById('otsingu_t2pid_tekst').innerHTML = t2pid_html;

	mitmes_t2pp++;
	mitmes_t2pp %= kokku_t2ppe;

	setTimeout('joonista_otsingu_t2pid();', 700);

}

function dec_x(siffer)
{
	return korv_muuda(siffer, -1);
}

function korv_muuda(siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta)
{
	// vaatame, kas on võimalik tellitavat toodet valida laost
	korv_valik_item_id = 'korv_t[k2ttesaamine]';
	if(document.o_form[korv_valik_item_id])
	{
		if(document.o_form[korv_valik_item_id][0].checked) // on valitud laohind
		{
			yhik_mitu_tk = 1;
			//alert(typeof(toote_laohind));
		}
	}

	
	
	if(!yhik_mitu_tk) yhik_mitu_tk = 1;

	item_id = 'korv_x[' + siffer + ']';
	v22rtus = 0;
	if(document.o_form[item_id].value)
	{
		v22rtus = parseInt(document.o_form[item_id].value);
		if(!v22rtus) v22rtus = 0;
	}

	
	v22rtus += palju_muuta * yhik_mitu_tk;

	if(yhik_mitu_tk > 1)
	{
		palju_muuta = v22rtus % yhik_mitu_tk;
		if(palju_muuta != 0)
		{
			v22rtus += yhik_mitu_tk - palju_muuta;
		}
	}
	


	if(v22rtus < 0) v22rtus = 0;
	if(v22rtus > 9999) v22rtus = 9999;

	document.o_form[item_id].value = v22rtus;
	document.o_form[item_id].focus();
	//document.o_form[item_id].select();
	if(!vaja_summat_arvutada) vaja_summat_arvutada = false;
	if(!vaja_hinda_muuta) vaja_hinda_muuta = false;

	arvuta_summa(vaja_summat_arvutada, siffer, vaja_hinda_muuta);

	return false;
}

function korv_muuda_timer_alusta(siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta)
{
	korv_muuda_timer_seadista(500, siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta);
}

function korv_muuda_timer(siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta)
{
	korv_muuda_timer_seadista(100, siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta);
}

function korv_muuda_timer_seadista(ajavahemik, siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta)
{
	korv_muuda(siffer, palju_muuta, yhik_mitu_tk, vaja_summat_arvutada, vaja_hinda_muuta);
	korv_muuda_timer_ID = setTimeout('korv_muuda_timer("' + siffer + '", ' + palju_muuta + ', ' + yhik_mitu_tk + ', ' + vaja_summat_arvutada + ', ' + vaja_hinda_muuta + ')', ajavahemik);
}

function klahv_alla(event, siffer, vaja_summat_arvutada, vaja_hinda_muuta, yhik_mitu_tk)
{
	if(!event) event = window.event;
	
	if(!yhik_mitu_tk) yhik_mitu_tk = 1;
	
	klahv = event.keyCode;

	if(klahv == 38) // nool üles
	{
		korv_muuda(siffer, 1, yhik_mitu_tk);
	}
	else if(klahv == 40) // nool alla
	{
		korv_muuda(siffer, -1, yhik_mitu_tk);
	}
	else // kui pole nooleklahv
	{
		// siis kindlasti ei arvuta
		return true;
	}

	arvuta_summa(vaja_summat_arvutada, siffer, vaja_hinda_muuta);
}

function klahv_yles_kontrolli(event, vaja_summat_arvutada, siffer, vaja_hinda_muuta)
{
	if(!event) event = window.event;

	klahv = event.keyCode;

	if(klahv == 38 // nool üles
		|| klahv == 40) // nool alla
	{
		return; // siis ei ole enam vaja arvutada, arvutatakse enne
	}

	arvuta_summa(vaja_summat_arvutada, siffer, vaja_hinda_muuta)
}


function arvuta_summa(vaja_summat_arvutada, muuda_siffer, vaja_hinda_muuta)
{
	// vaatame, kas on võimalik tellitavat toodet valida laost
	valitud_laohind = 0;
	korv_valik_item_id = 'korv_t[k2ttesaamine]';
	if(document.o_form[korv_valik_item_id])
	{
		if(document.o_form[korv_valik_item_id][0].checked
			&& typeof(toote_laohind) != "undefined") // on valitud laohind
		{
			valitud_laohind = toote_laohind;
		}
	}


	summa = 0;
	for(i = 0; i < korv_tooted.length; i++)
	{
		toode_siffer = korv_tooted[i][0];
		if(!vaja_summat_arvutada && vaja_hinda_muuta && muuda_siffer != toode_siffer) continue;

		toode_kogus_id = 'korv_x[' + toode_siffer + ']';

		kogus = 0;
		if(document.o_form[toode_kogus_id].value)
		{
			kogus = parseInt(document.o_form[toode_kogus_id].value);
			if(!kogus) kogus = 0;
		}

		uus_hind = korv_tooted[i][1];
		for(h = 1; h < korv_tooted[i].length; h += 2)
		{
			if(h + 1 < korv_tooted[i].length)
			{
				temp_kogus = korv_tooted[i][h + 1];
				if(temp_kogus > kogus) break;
			}

			uus_hind = korv_tooted[i][h];
		}

		// kui on valitud laohind
		if(valitud_laohind) uus_hind = valitud_laohind;

		if(vaja_hinda_muuta && muuda_siffer == toode_siffer)
		{
			toode_hind_id = 'toode_hind_' + toode_siffer;
			if(document.getElementById(toode_hind_id))
			{
				document.getElementById(toode_hind_id).innerHTML = hind(uus_hind);
			}
		}

		if(!vaja_summat_arvutada) continue;
			

		toote_summa = kogus * uus_hind;

		toode_summa_id = 'korv_summa[' + toode_siffer + ']';
//				alert(toode_summa_id);

		if(document.getElementById(toode_summa_id))
		{
			document.getElementById(toode_summa_id).innerHTML = hind(toote_summa);
		}

		summa += toote_summa;
	}

	if(!vaja_summat_arvutada) return;
/*
	k2ibemaks = 1.18;
	
	summa_ilma_id = 'korv_summa_ilma';
	if(document.getElementById(summa_ilma_id))
	{
		document.getElementById(summa_ilma_id).innerHTML = hind(summa / k2ibemaks);
	}
	
	summa_km_id = 'korv_summa_km';
	if(document.getElementById(summa_km_id))
	{
		document.getElementById(summa_km_id).innerHTML = hind(summa - (summa / k2ibemaks));
	}
*/
	

	
	summa_kokku_id = 'korv_summa_kokku';
	if(document.getElementById(summa_kokku_id))
	{
		document.getElementById(summa_kokku_id).innerHTML = hind(summa);
		$('#liisi_id').attr('title',parseFloat(summa).toFixed(2));
		liisi_kuumakse(parseFloat(summa).toFixed(2));
		uuenda_dpd(parseFloat(summa).toFixed(2));
		uuenda_jab(parseFloat(summa).toFixed(2));

	}

}

function leia_farnelli_k2sitlustasu()
{
	farnelli_k2sitlustasu_valik_id = 'korv_sisestus[farnell_kiirtellimus][v22rtus]';
	farnelli_k2sitlustasu = 0;

	if(document.o_form[farnelli_k2sitlustasu_valik_id] && document.o_form[farnelli_k2sitlustasu_valik_id].checked)
	{
		farnelli_k2sitlustasu = parseInt(document.o_form[farnelli_k2sitlustasu_valik_id].value);
	}

	return farnelli_k2sitlustasu;
}

function arvuta_farnelli_k2sitlustasu(mitmes_tarne)
{
	farnelli_k2sitlustasu_valik_id = 'korv_sisestus[farnell_kiirtellimus][v22rtus]';
	farnelli_k2sitlustasu = leia_farnelli_k2sitlustasu();
	display = 'none';

	if(farnelli_k2sitlustasu)
	{
		display = '';
	}
	
	korv_farnelli_k2sitlustasu_id = 'korv_farnell_kiirtellimus_k2sitlustasu';
	korv_farnelli_k2sitlustasu_rida_id = 'korv_farnell_kiirtellimus_k2sitlustasu_rida';
	
	if(document.getElementById(korv_farnelli_k2sitlustasu_id))
	{
		document.getElementById(korv_farnelli_k2sitlustasu_id).innerHTML = hind(farnelli_k2sitlustasu);
	}
	if(document.getElementById(korv_farnelli_k2sitlustasu_rida_id))
	{
		document.getElementById(korv_farnelli_k2sitlustasu_rida_id).style.display = display;
	}


	if(window.farnell_tarne)
	{
		arvuta_teenustasu(farnell_tarne, viimane_lisatasu, viimane_toodete_summa)
	}

}


function arvuta_teenustasu(mitmes_tarne, lisatasu, toodete_summa)
{
	farnelli_k2sitlustasu = 0;

	if(window.farnell_tarne && farnell_tarne == mitmes_tarne)
	{
		viimane_lisatasu = lisatasu;
		viimane_toodete_summa = toodete_summa;

		farnelli_k2sitlustasu = leia_farnelli_k2sitlustasu();
	}

	teenustasu_id = 'korv_teenustasu_transport_' + mitmes_tarne;
	if(document.getElementById(teenustasu_id))
	{
		document.getElementById(teenustasu_id).innerHTML = hind(lisatasu);
		
	}

	summa = toodete_summa + lisatasu + farnelli_k2sitlustasu;

	summa_id = 'korv_teenustasu_summa_' + mitmes_tarne;
	if(document.getElementById(summa_id))
	{
		document.getElementById(summa_id).innerHTML = hind(summa);
	} 
}

function emptydiv(id){
	document.getElementById(id).innerHTML = '';
}


function hind(summa)
{
	if(summa.toFixed(4) != 0) hind_summa = summa.toFixed(4);
	/*
	else if(summa.toFixed(3) != 0) hind_summa = summa.toFixed(3);
	else if(summa.toFixed(3) != 0) hind_summa = summa.toFixed(3);
	*/
	else return '&euro;0,00';
	// teeme eestikeelseks
	hind_summa = '&euro;'+hind_summa.replace('.', ',');

	return hind_summa;
}


/******************************************************************************/

function OpenWindow(url) {
	window.open(url, '', 'width=800,height=600,toolbar=0,status=0,menubar=0,directories=0,location=0,scrollbars=1,copyhistory=0,resizable=yes,screenX=10,screenY=10,left=10,top=10');
}

/******************************************************************************/

// !!!Uus kirjelduste piltide vahetaja!!!

function switchPicture(newPicSrc,openUri){
	
	var picture = document.getElementById('pic_url');
	var link = document.getElementById('pic_link');
	
	if (picture && newPicSrc) picture.src = newPicSrc;
	if (link && openUri) link.href = openUri;
	
	return false;
}

function renewPicture(src,href){
	$('#pic225px').attr('src',src);
	$('#pic225px_link').attr('href',href);
	return false;
}

// MUUDETUD: lisatud funktsion, avab akna
function openwindow2(url, pilt)
{
	aken = window.open("", "_blank", 'resizable=yes,toolbar=' +
		(pilt ? 'no,status=no,scrollbars=no,width=370, height=300' : 'yes,status=yes,scrollbars=yes,width=770, height=500'));
	
	aken.document.write("<title>Oomipood</title>");
	
	if(pilt == 1)
	{
		aken.document.write("<center><table height=\"100%\"><tr><td valign=middle><a href=\"javascript:window.close();\"><img border=\"0\" src=\"" + url + "\"></a></td></tr></table>");
	}
	else
	{
		aken.document.write("<FRAMESET ROWS=\"0,*\" border=0><FRAME><FRAME SRC=\"" + url + "\" SCROLLING=AUTO NORESIZE></FRAMESET>");
	}

	aken.document.close();
}

// kui nupule vajutatakse kaks korda
function topeltklikk(nupp)
{
	if(navigator.appName == "Microsoft Internet Explorer") nupp.onclick();
}

function korv_uus_samm(uus_samm)
{
	document.o_form['korv_sisestus[uus_samm]'].value = uus_samm;
	document.o_form.submit();
}

// kui n2ita == -1
function peida_tekst(id, n2ita)
{
	if(!document.getElementById(id)) return;
	if(!document.getElementById(id).style) return;

	if(n2ita == null || n2ita == -1)
	{
		n2ita = true;
		if(document.getElementById(id).style.display != "none")
		{
			n2ita = false;
		}
	}

	document.getElementById(id).style.display = n2ita ? "" : "none";
}


// Kaardid
	function loadKaardid() {
		var latlngR = new google.maps.LatLng(59.434577, 24.767824);
		var latlngJ = new google.maps.LatLng(59.393607, 24.720351);
		var latlngT = new google.maps.LatLng(58.358221, 26.677292);
		var latlngRa = new google.maps.LatLng(59.364312, 26.338542);
		var latlngP = new google.maps.LatLng(58.386603, 24.503106);
		
		var settingsR = {
			zoom: 13,
			center: latlngR,
			mapTypeControl: false,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var settingsJ = {
			zoom: 13,
			center: latlngJ,
			mapTypeControl: false,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var settingsT = {
			zoom: 13,
			center: latlngT,
			mapTypeControl: false,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var settingsRa = {
			zoom: 13,
			center: latlngRa,
			mapTypeControl: false,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var settingsP = {
			zoom: 13,
			center: latlngP,
			mapTypeControl: false,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var mapR = new google.maps.Map(document.getElementById("map_raadiomaja"), settingsR);
    var mapJ = new google.maps.Map(document.getElementById("map_jarve"), settingsJ);
    var mapT = new google.maps.Map(document.getElementById("map_tartu"), settingsT);
    var mapRa = new google.maps.Map(document.getElementById("map_rakvere"), settingsRa);
    var mapP = new google.maps.Map(document.getElementById("map_parnu"), settingsP);
  	
  	var companyLogo = new google.maps.MarkerImage('/Images/Maps/OomipoeKaardiMarkerH100.png',
			new google.maps.Size(85,100),
			new google.maps.Point(0,0),
			new google.maps.Point(50,100)
		);
		
		var companyMarkerR = new google.maps.Marker({
			position: latlngR,
			map: mapR,
			icon: companyLogo,
			title:"Raadiomaja Oomipood"
		});
		
		var companyMarkerJ = new google.maps.Marker({
			position: latlngJ,
			map: mapJ,
			icon: companyLogo,
			title:"Järve keskuse Oomipood"
		});
		
		var companyMarkerT = new google.maps.Marker({
			position: latlngT,
			map: mapT,
			icon: companyLogo,
			title:"Lõunakeskuse Oomipood"
		});
		
		var companyMarkerRa = new google.maps.Marker({
			position: latlngRa,
			map: mapRa,
			icon: companyLogo,
			title:"Põhjakeskuse Oomipood"
		});
		
		var companyMarkerP = new google.maps.Marker({
			position: latlngP,
			map: mapP,
			icon: companyLogo,
			title:"Port Arturi Oomipood"
		});
		
  }
 google.maps.event.addDomListener(window, 'load', loadKaardid)
/* Script by: www.jtricks.com 
 * Version: 20100515 
 * Latest version: 
 * www.jtricks.com/javascript/window/box.html 
 */  
// Moves the box object to be directly beneath an object.  
function move_box(an, box)  
{  
    var cleft = 0;  
    var ctop = 0;  
    var obj = an;  
  
    while (obj.offsetParent)  
    {  
        cleft += obj.offsetLeft;  
        ctop += obj.offsetTop;  
        obj = obj.offsetParent;  
    }  
  
    box.style.left = (cleft-230) + 'px';  
    

  
    ctop += an.offsetHeight + 3;  
  
    // Handle Internet Explorer body margins,  
    // which affect normal document, but not  
    // absolute-positioned stuff.  
    if (document.body.currentStyle &&  
        document.body.currentStyle['marginTop'])  
    {  
        ctop += parseInt(  
            document.body.currentStyle['marginTop']);  
    }  
  
    box.style.top = ctop + 'px';  
}  

function applyCode(regno,name) {
	$("#regcode").val(regno);
	$("#company").val(name);
	$('div.popUp').fadeOut("slow");
}

function findCompanies() {
$.ajax({
    url: "inc/company.php",
    type: "POST",   
    data: { 
			company: $("#firma_id").val()
		},
    cache: false,
    beforeSend: function() {
    $("#div_result").fadeIn("slow");
    $('#div_result').text('Otsin firmat. Palun oodake...');
    },
    success: function (data) {       
				$('#div_result').html(data);
    } 
});
}
  
// Shows a box if it wasn't shown yet or is hidden  
// or hides it if it is currently shown  
function show_hide_box(an, id, width, height, borderStyle,mode)  
{  

    var href = an.href; 

    var boxdiv = document.getElementById(href);  
  
    if (boxdiv != null)  
    {  
        if (boxdiv.style.display=='none')  
        {  
            // Show existing box, move it  
            // if document changed layout  
            move_box(an, boxdiv);  
            boxdiv.style.display='block';  
  
            bringToFront(boxdiv);  
            
            if (mode==0 || mode==2) {
  
            // Workaround for Konqueror/Safari  
            if (!boxdiv.contents.contentWindow)  
                boxdiv.contents.src = href;  
            }
        }  
        else  
            // Hide currently shown box.  
            boxdiv.style.display='none';  
        return false;  
    }  
  
    // Create box object through DOM  
    boxdiv = document.createElement('div');  
    boxdiv.setAttribute('id', href);  
    boxdiv.setAttribute('class', 'popUp');  
		boxdiv.setAttribute('style', '-moz-box-shadow: 3px 3px 4px #6C6C6C;-webkit-box-shadow: 3px 3px 4px #6C6C6C;box-shadow: 3px 3px 4px #6C6C6C;	-moz-box-shadow: 3px 3px 4px #6C6C6C;-webkit-box-shadow: 3px 3px 4px #6C6C6C;box-shadow: 3px 3px 4px #6C6C6C;filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color=\'#6C6C6C\')');  
    boxdiv.style.display = 'block';  
    boxdiv.style.position = 'absolute';  
    boxdiv.style.width = width + 'px';  
    boxdiv.style.height = height + 'px';  
    boxdiv.style.border = borderStyle;  
    boxdiv.style.textAlign = 'right';  
    boxdiv.style.padding = '4px';  
    boxdiv.style.background = '#FFFFFF';  
    document.body.appendChild(boxdiv);  
    bringToFront(boxdiv);  
  
  	

  
    var offset = 0;  
  
    // Remove the following code if 'Close' hyperlink  
    // is not needed.  
    
    var close_href = document.createElement('a'); 

		var img = new Image(18,18); // width, height values are optional params 
		img.src = 'images/close.gif';
		img.alt = 'Sulge';
		img.border = '0';
		img.title = 'Sulge';
		img.id = 'img_close_'+id;

		
		
    close_href.setAttribute('style', 'float:right; font-weight:bold; padding-top: 5px;padding-right:10px;font-size:12pt; font-family:Arial; color:gray;');
    close_href.href = 'javascript:void(0);';  
    close_href.onclick = function()  
        { show_hide_box(an, width, height, borderStyle); }  
    //close_href.appendChild(document.createTextNode('X'));  
    boxdiv.appendChild(close_href);   
    offset = close_href.offsetHeight;  
    // End of 'Close' hyperlink code.  
  	close_href.appendChild(img);
 
 

  	
    var contents = document.createElement('iframe');  
    //contents.scrolling = 'no';  
    contents.overflowX = 'hidden';  
    contents.overflowY = 'scroll';  
    
    contents.frameBorder = '0';  
    contents.style.width = width + 'px';  
    contents.style.height = (height - offset - 50) + 'px';  
  
    boxdiv.contents = contents;  
    if ((mode==0) || (mode==2)) boxdiv.appendChild(contents);  
  
  	

    move_box(an, boxdiv);  
  
  	if (mode==0) {
  
	    if (contents.contentWindow)  {
	        var div = "";
					div = document.getElementById('ajalugu_'+id);
	       	div.visibility = true;
	        contents.contentWindow.document.write(div.innerHTML);				
	        
	
	
	    } else  {
	        contents.src = href;  
	    }
    
    } else if (mode==2) {
		    if (contents.contentWindow)  {
		        var div = "";
						div = document.getElementById('liisi_taotlus_'+id);
		       	div.visibility = true;
		        contents.contentWindow.document.write(div.innerHTML);				
		    } else  {
		        contents.src = href;  
		    }		
		}

		if (mode==1) {
		
		// Otsingu DIV
		
		var searchDiv = document.createElement('div');  
		//searchDiv.setAttribute('style', '-moz-box-shadow: 3px 3px 4px #6C6C6C;-webkit-box-shadow: 3px 3px 4px #6C6C6C;box-shadow: 3px 3px 4px #6C6C6C;	-moz-box-shadow: 3px 3px 4px #6C6C6C;-webkit-box-shadow: 3px 3px 4px #6C6C6C;box-shadow: 3px 3px 4px #6C6C6C;filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color=\'#6C6C6C\');');  
		searchDiv.setAttribute('style','overflow:auto');
		searchDiv.style.display = 'block';  
		searchDiv.style.position = 'relative';  
		if (mode==1) {
			searchDiv.style.width = '330px'; 
		} else {
		searchDiv.style.width = '200px'; 
		}
		//searchDiv.style.height = '300px';  
		searchDiv.style.textAlign = 'center';  
		searchDiv.style.padding = '10px';  
		searchDiv.style.background = '#FFFFFF';  						
		
		
		// Otsingu tekst
		
		var p = document.createElement("span");
		p.setAttribute('style','padding-right: 8px;');
		var text = document.createTextNode("Firma");
		p.appendChild(text);
		searchDiv.appendChild(p);
		
		// Otsingu lahter
		var txtBox = document.createElement('input');  
		txtBox.name = 'nimetus';
		txtBox.type = 'text';
		txtBox.id = 'firma_id';
		txtBox.size = '26';	
		txtBox.setAttribute('style','border: 1px solid #DCDCDC; height:23px;');
		var firma = document.getElementById('company').value;
		txtBox.value = firma;
		searchDiv.appendChild(txtBox);  
		
		// Otsingu nupp

		var p2 = document.createElement("span");
		p2.setAttribute('style','padding-left: 4px;');		
		
		var button = document.createElement('input');  
		button.name = 'btn';
		button.setAttribute('type','button');
		button.setAttribute('style',' background: url("../Images/button-search.gif") no-repeat scroll left top #F0E38B;border: 1px solid #D9CB66;color: #666666;float: none;font-weight: bold;height: 26px;margin-right: 0;padding: 0 5px 0 33px;text-align: left;width: 70px;');
		button.size = '20';
		button.value = 'OTSI';
		button.setAttribute('onclick','findCompanies();');
		p2.appendChild(button);
		searchDiv.appendChild(p2);
		
		
		
		//Otsingu tulemused
		var resultDiv = document.createElement('div');  
		resultDiv.setAttribute('id','div_result');
		resultDiv.setAttribute('style','display:none;padding-top:20px;overflow:auto;height:180px;');
		searchDiv.appendChild(resultDiv);	
    boxdiv.appendChild(searchDiv);
    document.getElementById('firma_id').focus();
    
    
    }
    
		var browserName=navigator.appName; 
		if (browserName=="Netscape")
		{ 
		 window.stop();
		}
		else 
		{ 
		 if (browserName=="Microsoft Internet Explorer")
		 {
		  document.execCommand('Stop');
		 }
		 else
		  {
		     window.stop();
		   }
		}
	
		
    return false;  
}  
  
function getAbsoluteDivs()  
{  
    var arr = new Array();  
    var all_divs = document.body.getElementsByTagName("DIV");  
    var j = 0;  
  
    for (i = 0; i < all_divs.length; i++)  
        if (all_divs.item(i).style.position=='absolute')  
        {  
            arr[j] = all_divs.item(i);  
            j++;  
        }  
  
    return arr;  
}  
  
function bringToFront(obj)  
{  
    if (!document.getElementsByTagName)  
        return;  
  
    var divs = getAbsoluteDivs();  
    var max_index = 0;  
    var cur_index;  
  
    // Compute the maximal z-index of  
    // other absolute-positioned divs  
    for (i = 0; i < divs.length; i++)  
    {  
        var item = divs[i];  
        if (item == obj ||  
            item.style.zIndex == '')  
            continue;  
  
        cur_index = parseInt(item.style.zIndex);  
        if (max_index < cur_index)  
        {  
            max_index = cur_index;  
        }  
    }  
  
    obj.style.zIndex = max_index + 1;  
}  

function str_replace (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function reloadImg(id) {
   var obj = document.getElementById(id);
   if (obj) {
	   var src = obj.src;
	   var pos = src.indexOf('?');
	   if (pos >= 0) {
	      src = src.substr(0, pos);
	   }
	
	   obj.src = src;
	   return false;
   }
   return false;
}

function printDiv(id) {
	var div = document.getElementById('ajalugu_'+id);
	var newWindow = window.open('http://test.opood.ee/?t=u_tell#'+id,'mywindow','width=600,height=800,scrollbars=auto');
	//div.visibility = true;
	newWindow.document.write('<html><body onload="window.print()">'+div.innerHTML+'</div></html>');
	newWindow.document.close();
	return false;
	
}

function liisi_data(price) {

$("#sissemaksu_suurus").val((price*0.1).toFixed(2));
$("#t_hind").val(parseFloat(price).toFixed(2));

var x = parseFloat(price) - (price*0.1);

var periood = 0;

if (x < 130) {
	periood = 16;
} else if ((x>=130) && (x<=640)) {
	periood = 24;
} else if (x > 640) {
	periood = 48;
}


$("#s_periood").val(periood).attr('selected',true);


}


function loadPic(thumb,video,aspect,id){
	var string = 'thumb='+thumb+'&video='+video+'&aspect='+aspect;
	var imageID = '#' + id;
	$.ajax({
		type: "GET",
		url: "/inc/imageresize.php",
		data: string,
    beforeSend: function() {
				alert('1');
    },		
		success: function(msg){
			var img_src = $(imageID).attr('src');
     		var timestamp = new Date().getTime();
     		$(imageID).attr('src',img_src+'?'+timestamp);
  		}
	});
}


function liisi_link(href) {
	
	var position_href = $(href).position();
	var position_div = $('#liisi_calculator').position();


	var price = $(href).attr('title');
			price = str_replace("EUR","",price);
	if (price) {
			liisi_data(price);
			arvuta_kuumaksu('');
			if (position_href.top > 750) {
			
				//$("body").animate({scrollTop:position_div.top}, "slow");//IE, FF
				//$("html").animate({scrollTop:position_div.top}, "slow");
				$("body").animate({scrollTop:(position_href.top-90)}, "slow");//IE, FF
				$("html").animate({scrollTop:(position_href.top-90)}, "slow");				
				$('#liisi_calculator').css('top',(position_href.top-90)+'px').css('position','absolute').css('box-shadow','3px 3px 4px #6C6C6C').css('left','-40px');

				
				
			} else {
        $('#liisi_calculator').css('top','0px').css('position','relative').css('left','0').css('box-shadow','');
      }
			
					
	}
}
function uuenda_jab(price) {

	if ((price >= 60)) {
			$('tr.jablotron_link').fadeIn("slow");
	} else if (price < 60){
		// 
		$('tr.jablotron_link').fadeOut("slow");
	}
}

function uuenda_dpd(price) {

	if ((price >= 70) && (price <125)) {
			$('div.dpd_info').show();
			$('#dpd_txt').html('Tasuta kullerteenuseni &euro;'+parseFloat(125-price).toFixed(2).replace('.',',')); 
	} else if (price < 70){
		// 
		$('div.dpd_info').hide();
	} else if (price >= 125){
		//
		$('div.dpd_info').show();
		$('#dpd_txt').html('Kuller tasuta');
	
	}
}

function liisi_kuumakse(hind) {
		if (hind < 100) {
				$('div.liisi_link').hide();
				return false;
		} 
		$('div.liisi_link').show();
		var sissemaks = hind * 0.1;
		var intress = 0.20;
		var x = hind - sissemaks;
		
		if (x < 130) {
			intress = 0.25;
			periood = 16;
		} else if ((x>=130) && (x<=640)) {
			intress = 0.22;
			periood = 24;
		} else if (x > 640) {
			intress = 0.20;
			periood = 48;
		}
		
		var sissee = ((hind * ( 1 + intress * periood / 12 ) ) / ( periood + 1)).toFixed(2);
  	if (sissemaks < sissee && sissemaks != 0){
  		sissemaks = sissee;
  	}
		 
   	if(sissemaks > hind) {
    	sissemaks = sissee = hind;
    }
    
		var kuumakse = (( (hind - sissemaks) * ( 1 + intress * periood / 12) ) / (periood));

		$('#liisi_kmakse').html('&euro;'+parseFloat(kuumakse).toFixed(2).replace('.',','));

}


function arvuta_kuumaksu(id) {

var sissemaks = $("#sissemaksu_suurus"+id).val();
var hind = $("#t_hind"+id).val();
var periood = $("#s_periood"+id).val();
var span_txt = $("#k_makse"+id);
sissemaks = sissemaks > 0 ? sissemaks : 0;
sissemaks = parseFloat(sissemaks);
if (id!='') {
	var min = $("#t_min"+id).val();
}

	if (sissemaks>=min || sissemaks<Math.round(hind)) {
		var x = hind - sissemaks;
		var intress = 0.20;

		if (x < 130) {
			intress = 0.25;
		} else if ((x>=130) && (x<=640)) {
			intress = 0.22;
		} else if (x > 640) {
			intress = 0.20;
		}
		
		var sissee = ((hind * ( 1 + intress * periood / 12 ) ) / ( periood + 1)).toFixed(2);
  	if (sissemaks < sissee && sissemaks != 0){
  		sissemaks = sissee;
  	}
		 
   	if(sissemaks > hind) {
    	sissemaks = sissee = hind;
    }
    
		var kuumakse = (( (hind - sissemaks) * ( 1 + intress * periood / 12) ) / (periood)) . toFixed(2);

		$("#k_makse"+id).html('&euro;'+kuumakse);
		$("#k_id"+id).val(kuumakse);
	}

}

function LoadImage(obj,src) {
$(obj).attr('src',"ImageResize.php?src="+src+"&w=80&h=80").css('width',"80");  
}

function printPage() {

  var text = "<div class=\"addbox\">"+$('div.addbox').html()+"</div>";
  		text += "<div class=\"usual\">"+$('div.usual').html()+"</div>";
  var name = document.getElementById('toode_nimi').value;
  printwin = open("", "printwin");
  printwin.document.open();
  printwin.document.writeln("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"CSS/style.css\" /><title></title></head><body onload=print();>");
	printwin.document.writeln("<h1>"+name+"</h1>");
	printwin.document.writeln("<div class=\"special\" style=\"width:450px;\">"+text);
  printwin.document.writeln("</div></body></html>");
 // printwin.document.close();
  
  
}

function formSubmit(id) {
	$('#'+id).submit();
}

function confirmDelete(msg) {
  if (confirm(msg+"?")) {
    return true;
  }
  return false;
}


// Password generator

function GeneratePassword() {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

	
    var length=8;
    var sPassword = "";
    var noPunction = true;
    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }

    //document.aForm.passField.value = sPassword
    $('#password').val(sPassword);

    return true;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}


