function showMudel(param, mis){
	document.getElementById("muddif").style.display="";
	document.getElementById("h"+param).value=mis;
	/*window.location.reload();*/
}

function sortTable(wht){
	if(document.getElementById(wht+"asc").style.display=="none" && document.getElementById(wht+"desc").style.display=="none"){
		document.getElementById("oSortDir").value=wht+"asc";
	}else if(document.getElementById(wht+"asc").style.display==""){
		document.getElementById("oSortDir").value=wht+"desc";
	}else{
		document.getElementById("oSortDir").value=wht+"esc";
	}
	document.getElementById("oSort").value=wht;
}

function toggle(div_id) {
	var el = $(div_id);
	if ( el.style.display == 'none' ) {el.style.display = 'block';}
	else {el.style.display = 'none';}
}

function switchdiv(div1_id,div2_id) {
    	toggle(div1_id);
    	toggle(div2_id);
}

function checkChange(id) {
	var sum;
	var obj = document.getElementById(id);
	if ((sum = parseInt(obj.value))<0 || isNaN(sum))
	{
		obj.value = 0;
	}
}

function checkAll(theElement){
	var theForm = theElement;
	for(z=0; z<theForm.length;z++){
      		if(theForm[z].type == 'checkbox' && theForm[z].checked == false){
	  		theForm[z].checked = true;
	  	}
     	}
}

function checkAmount(siffer,amount){
	var url = 'popup.php?act=checkAmount&siffer='+siffer+'&amount='+amount;
	var ajaxOptions = {
		method: 	'get',
		update: 	$('alert_'+siffer)
		};
	this.ajaxRequest = new Ajax(url, ajaxOptions).request();
			
	return false;
}

function getNotifycation(siffer){
	
	var elem = $('muudatused_'+siffer);
	
	elem.setHTML('Palun oodake...');
	
	if ( elem.style.display == 'none' ) {elem.style.display = 'block';}
	else {elem.style.display = 'none';}
	
	//AJAX
	var url = 'popup.php?act=getNotify&siffer='+siffer;
	var ajaxOptions = {
		method: 	'get',
		update: 	elem
		};
	this.ajaxRequest = new Ajax(url, ajaxOptions).request();
			
	return false;
}

function setMaxAmount(siffer,amount){
	$('siffer['+siffer+']').value=amount;
}

function SetNull(siffer){
	$('siffer['+siffer+']').value="0";
}

function setNotify(siffer,checked){
	var url = 'popup.php?act=setNotification&siffer='+siffer+'&checked='+checked;
	var ids = $('muudatused_'+siffer);
	
	var ajaxOptions = {
		method: 	'get',
		update: 	ids
		};
	this.ajaxRequest = new Ajax(url, ajaxOptions).request();
	
	$('alert_'+siffer).style.display="none";	
	
	return false;
}

function SetMyPage(page){
	location.href = "index.php?"+page;
}

function makeSearch(event,actValue){
	if(event && event.which == 13){
		$('act').value=actValue; document.form.submit();
	}else{
		return true;
	}
}

function updateKogus(siffer,value,price,name){
	var url = 'popup.php?act=updateSavedAmount&siffer='+siffer+'&value='+value+'&price='+price+'&saveName='+name;
	var ids = $('summa_'+siffer);
	
	var ajaxOptions = {
		method: 	'get',
		update: 	ids
		};
	this.ajaxRequest = new Ajax(url, ajaxOptions).request();
	
	return false;
}

function setStatistics(userid,company,toode){
	var info = $('too_'+toode);
	var comm = $('komm_'+toode);
	
	if (info.style.display == 'none' && comm.style.display == 'none') {
		var url = 'exsists.php?act=showItem&siffer='+toode+'&user='+userid+'&company='+company;
		var ajaxOptions = {
					method: 	'get'
				  };
		this.ajaxRequest = new Ajax(url, ajaxOptions).request();
	}
	return false;
}


function kysi(klient,loc){
	Oname=AskPrompt('Tellimuse nimi','Sisesta siia tellimuse nimi');
	/* Kui on olemas mingi nimi ja ei vajutatud cancel */
	if(Oname!='null' && Oname){
		ahah('exsists.php?SavedName='+Oname+'&klient='+klient,klient,loc,Oname);
	}
}

function kysi2(klient,loc,Oname){
	Ask=fAsk('Sellise nimega salvestatud tellimus on juba olemas. Vajuta OK, et tellimus olemasolevale liita vői Cancel, et salvestada tellimus teise nimega.');
	
	/* Kui on olemas mingi nimi ja ei vajutatud cancel */
	if(Ask){
		window.location='index.php?'+loc+'&act=SaveCart&ordName='+Oname+'&todo=update';
	}else{
		kysi(klient,loc);
	}
}

function ahah(url,klient,loc,Oname) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDone(klient,loc,Oname);};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
} 

function ahahDone(klient,loc,Oname) {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200 || req.status == 304) {
	   if(req.responseText=="1"){
		kysi2(klient,loc,Oname);
	   }else{
	   	window.location='index.php?'+loc+'&act=SaveCart&ordName='+Oname;
	   }
       } else {
	   if(req.responseText=="1"){
		kysi2(klient,loc,Oname);
	   }else{
	   	window.location='index.php?'+loc+'&act=SaveCart&ordName='+Oname;
	   }
       }
   }
}


 //Google map load   
function initialize() {
	if (GBrowserIsCompatible()) {
        	map = new GMap2($("map_canvas"));
		map.setUIToDefault();
		over = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=103018188531422293361.00046e54f02097c58775a&z=7&output=kml"); 
		map.addOverlay(over);
        	map.setCenter(new GLatLng(58.642653, 25.3), 7);
      	}
}
    
function animate(l1,l2,z) {    
	map.setCenter(new GLatLng(l1, l2), z);
}

window.addEvent('load', function() {
	if($chk($("map_canvas"))){	
		initialize();
	}
});

window.addEvent('unload', function() {
	if($chk($("map_canvas"))){	
		GUnload();
	}
});

