
 function setCookie(cookieName, cookieValue) {
//  var today = new Date();
  if (cookieValue=="")
//    document.cookie = cookieName+"=;expires="+today.toGMTString()
    document.cookie = cookieName+"=; path=/"
  else
  {
//    var expire = new Date();
//    expire.setTime(today.getTime() + 3600000 * 24 * 365); // eno leto
//    document.cookie = cookieName+"="+escape(cookieValue) + "; path=/; expires="+expire.toGMTString();
    document.cookie = cookieName+"="+escape(cookieValue) + "; path=/";
  };
 }

 function getCookie(cookieName, cookieDefault) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") return cookieDefault;
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) ind1=theCookie.length;
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
 }

 function allCookie(cookieName, cookieDefault) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") {setCookie(cookieName, cookieDefault); return cookieDefault; };
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) ind1=theCookie.length;
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
 }

 function lockField(S)
{

  //alert("Alert: " + S);
  var elem = document.getElementById(S)
  if (elem.readOnly) {
    elem.disabled = false;
    elem.readOnly = false;
    setCookie ("discountAvail", "true");
  } else {
    elem.disabled = true;
    elem.readOnly = true;
    setCookie ("discountAvail", "");
  }
  saveDiscount();
  getQuickInfo();

}

 function saveDiscount ()
 {
   var chkDiscount = document.getElementById('idCheckDiscount');
   var edtDiscount = document.getElementById('idEditDiscount');

   if (chkDiscount.checked) {
     var ss = edtDiscount.value;
     if(ss == "") ss = "0.0";
     ss = ss.replace(/,/, '.');
     setCookie ("discount", ss);
   }

 }

// ----------

function getNewHttpObject() {
 var objType = false;
 try {
  objType = new ActiveXObject('Msxml2.XMLHTTP');
 } catch(e) {
  try {
   objType = new ActiveXObject('Microsoft.XMLHTTP');
  } catch(e) {
   objType = new XMLHttpRequest();
  }
 }
 return objType;
}

function trim(ss) {return ss.replace(/^\s+|\s+$/g, "")};
function get(s) {return document.getElementById(s)};

function mouseCoords(ev){
 if(ev.pageX || ev.pageY){
  // FF ne dela, zato:
  return {x:ev.pageX-4, y:ev.pageY-40};
 }
// IE
 return {
   x:ev.x, y:ev.y
//  x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
//  y:ev.clientY + document.body.scrollTop  - document.body.clientTop
 };
}



function getData(url) {
 var theHttpRequest = getNewHttpObject();
 theHttpRequest.onreadystatechange = function() {setData();};
 theHttpRequest.open("GET", url+'&counter='+Math.random());
 theHttpRequest.send(false);
 function setData() {
  if (theHttpRequest.readyState == 4) {
   if ((theHttpRequest.status == 200)||(theHttpRequest.status == 304)) {
    setCookie('list', trim(theHttpRequest.responseText));
    getMap();
    getList1();
    getList2();
    getQuickInfo();
   };
  };
 };
}

function getDataLite(url) {
 var theHttpRequest = getNewHttpObject();
 theHttpRequest.onreadystatechange = function() {setData();};
 theHttpRequest.open("GET", url+'&counter='+Math.random());
 theHttpRequest.send(false);
 function setData() {
  if (theHttpRequest.readyState == 4) {
   if ((theHttpRequest.status == 200)||(theHttpRequest.status == 304)) {
    setCookie('list2', trim(theHttpRequest.responseText));
   };
  };
 };
}



function getList(url, par) {
 var theHttpRequest = getNewHttpObject();
 theHttpRequest.onreadystatechange = function() {setList(par);};
 theHttpRequest.open("GET", url+'&counter='+Math.random());
 theHttpRequest.send(false);
 function setList(par) {
  if (theHttpRequest.readyState == 4) {
   if ((theHttpRequest.status == 200)||(theHttpRequest.status == 304)) {
    par.innerHTML=theHttpRequest.responseText;
   };
  };
 };
}

function getList1() {
 getList('system/function.dll?function=wsSelect&action=parent1', get('Parent1'));
}

function getList2() {
 getList('system/function.dll?function=wsSelect&action=parent2', get('Parent2'));
}

function getQuickInfo() {
 getList('system/function.dll?function=wsQuickInfo', get('Parent5'));
}

// ----------

function getMap() {
 c=get('cc')
 c.style.cursor="wait";
 c.src="map.gif?command=wsPaintMap&counter="+Math.random();
}

// ----------

var ymin=368000;
var ymax=628000;
var xmin= 15000;
var xmax=210000;

function navigate(s) {

 s1=getCookie('position', '');
 s2=s1.split(',');
 y1=Number(s2[2]);
 x1=Number(s2[3]);
 y2=Number(s2[4]);
 x2=Number(s2[5]);
 
 yc=(y1+y2)/2;
 xc=(x1+x2)/2;
 yd=(y2-y1);
 xd=(x2-x1);

 if (s=='za') {
  y1=ymin;
  y2=ymax;
  x1=xmin;
  x2=xmax;
 };
 if (s=='zi') {
  if (yd<5000) return;
  y1=y1+yd/4;
  y2=y2-yd/4;
  x1=x1+xd/4;
  x2=x2-xd/4;
 };
 if (s=='zo') {
  if (yd>250000) return;
  y1=y1-yd/2;
  y2=y2+yd/2;
  x1=x1-xd/2;
  x2=x2+xd/2;
 };

 if (s=='nw') {
  y1=y1-yd/4;
  y2=y2-yd/4;
  x1=x1+xd/4;
  x2=x2+xd/4;
 };
 if (s=='nc') {
  x1=x1+xd/4;
  x2=x2+xd/4;
 };
 if (s=='ne') {
  y1=y1+yd/4;
  y2=y2+yd/4;
  x1=x1+xd/4;
  x2=x2+xd/4;
 };
 if (s=='cw') {
  y1=y1-yd/4;
  y2=y2-yd/4;
 };
 if (s=='ce') {
  y1=y1+yd/4;
  y2=y2+yd/4;
 };
 if (s=='sw') {
  y1=y1-yd/4;
  y2=y2-yd/4;
  x1=x1-xd/4;
  x2=x2-xd/4;
 };
 if (s=='sc') {
  x1=x1-xd/4;
  x2=x2-xd/4;
 };
 if (s=='se') {
  y1=y1+yd/4;
  y2=y2+yd/4;
  x1=x1-xd/4;
  x2=x2-xd/4;
 };

 setCookie('position', '800,600,'+y1+','+x1+','+y2+','+x2);
 getMap();
 getList1();

};

function navigateYX(s, y, x) {
 s1=getCookie('position', '');
 s2=s1.split(',');
 yp=Number(s2[0]);
 xp=Number(s2[1]);
 y1=Number(s2[2]);
 x1=Number(s2[3]);
 y2=Number(s2[4]);
 x2=Number(s2[5]);

 yc=(y1+y2)/2;
 xc=(x1+x2)/2;
 yd=(y2-y1);
 xd=(x2-x1);

 if (s=='cp') {
  y0=-y/yp*yd;
  x0= x/xp*xd;
  y1=y1+y0;
  y2=y2+y0;
  x1=x1+x0;
  x2=x2+x0;
  setCookie('position', '800,600,'+y1+','+x1+','+y2+','+x2);
  getMap();
  getList1();
  return;
 };

 y0=y1+   (y)/yp*yd;
 x0=x1+(xp-x)/xp*xd;

 if (s=='ci') {
  if (yd<5000) return;
  y1=y1+(y0-y1)/2;
  y2=y2-(y2-y0)/2;
  x1=x1+(x0-x1)/2;
  x2=x2-(x2-x0)/2;
  setCookie('position', '800,600,'+y1+','+x1+','+y2+','+x2);
  getMap();
  getList1();
 };
 if (s=='co') {
  if (yd>250000) return;
  y1=y1-(y0-y1);
  y2=y2+(y2-y0);
  x1=x1-(x0-x1);
  x2=x2+(x2-x0);
  setCookie('position', '800,600,'+y1+','+x1+','+y2+','+x2);
  getMap();
  getList1();
 };
 if (s=='cs') {
  getData('system/function.dll?function=wsInfo&action=switch&y='+y0+'&x='+x0);
 };

};

// ----------

function ChangeNumDeliveries (S) {

  // getDataLite('system/function.dll?function=wsGetDetailedInfo');
  //alert (getCookie('list2'));
  window.open('system/function.dll?function=wsGetDetailedInfo', '_blank');
 // location.href = '/mannumentry.htm';
  /*
  var selCtl = document.getElementById(S)
  var i = selCtl.selectedIndex;
  if (i < 0) {
    alert ("Najprej Izberi dostavno pošto");
  }
  var s1 = selCtl.options[i].value;
  var option_user_selection = selCtl.options[i].text
  var stpos = prompt("Število pošiljk za občino '"+option_user_selection+"': ","100");
  if (stpos != null) {
    stpos = parseInt(stpos);
    if (isNaN(stpos)) {
       alert("Vpisano število ni veljavno.");
    } else {
         alert ("Izbranih pošiljk: " + stpos);
      location.href = 'system/function.dll?function=wsOffer&selectedoffice=' + Math.random();
    }
  }
  */
}


// ----------

 function onDblClick(a, b)
 {
  i = a.selectedIndex;
  if (i>-1) {
   s1 = a.options[i].value;
   if (i==a.length-1) {
     if (a.length==1) {
	   s2 = ""
	 } else {
	   s2 = a.options[i-1].value;
	 }
   } else {
     s2 = a.options[i+1].value;
   };
   if (b==1) getData('system/function.dll?function=wsInfo&action=insert&n='+s1+'&m='+s2);
   if (b==0) getData('system/function.dll?function=wsInfo&action=remove&n='+s1+'&m='+s2);
  }
 };

function onChange(a) {
  if (a.id=="Select3") {setCookie("type", a.selectedIndex); getQuickInfo(); };
  if (a.id=="Select4") {setCookie("mass", a.value); getQuickInfo(); };
  if (a.id=="Select6") {setCookie("cust", a.value); getQuickInfo(); };
  if (a.id=="idEditDiscount") { saveDiscount(); getQuickInfo(); };
  if (a.id=="idCheckCalcDiscount") {
    if (a.checked) {
      setCookie("calcDiscountAvail", "True");
    } else {
      setCookie("calcDiscountAvail", "");
    }
    getQuickInfo();
  }
 };



 function SetNavButton(s) {
  a=get(s);
//  a.width=32;
//  a.height=32;
  a.onmouseover=function(e) {
   if (window.event) e = window.event;
   b=e.srcElement ? e.srcElement : e.target; 
   b.style.backgroundColor="#FFBA36";
  };
  a.onmouseout=function(e) {
   if (window.event) e = window.event;
   b=e.srcElement ? e.srcElement : e.target;
   b.style.backgroundColor="";
  };
  a.onclick=function(e) {
   if (window.event) e = window.event;
   b=e.srcElement ? e.srcElement : e.target; 
   if (b.id=="za") navigate('za');
   if (b.id=="zi") navigate('zi');
   if (b.id=="zo") navigate('zo');
   if (b.id=="nw") navigate('nw');
   if (b.id=="nc") navigate('nc');
   if (b.id=="ne") navigate('ne');
   if (b.id=="cw") navigate('cw');
   if (b.id=="ce") navigate('ce');
   if (b.id=="sw") navigate('sw');
   if (b.id=="sc") navigate('sc');
   if (b.id=="se") navigate('se');
  };
 };

 var mp;
 
 function SetCC() {
  c=get('cc');
  c.onload=function() {
   c=get('cc');
   c.style.cursor='default';
  };
  c.onmousedown=function(e) {
   if (window.event) e = window.event;
   mp = mouseCoords(e);
   c=get('cc');
   c.style.cursor='auto';
  };
  c.onmouseup=function(e) {
   if (window.event) e = window.event;
   c=get('cc');
   c.style.cursor='default';
   dp = mouseCoords(e);
   dx=dp.x-mp.x;
   dy=dp.y-mp.y;
   if ((Math.abs(dx)>3)||(Math.abs(dy)>3)) navigateYX('cp', dx, dy);
  };
  c.onmouseout=function(e) {
   c=get('cc');
   c.style.cursor='default';
  };
  c.ondblclick=function(e) {
   if (window.event) e = window.event;
   dp = mouseCoords(e);
   navigateYX('cs', dp.x, dp.y);
  };
  c.onmousewheel=wheel;
  if (c.addEventListener) c.addEventListener('DOMMouseScroll', wheel, false);
 };

function wheel(e) {
 if (window.event) e = window.event;
 dp = mouseCoords(e);
 if (e.wheelDelta>0) navigateYX('ci', dp.x, dp.y);
 if (e.wheelDelta<0) navigateYX('co', dp.x, dp.y);
 if (e.detail<0) navigateYX('ci', dp.x, dp.y);
 if (e.detail>0) navigateYX('co', dp.x, dp.y);
}

 function IsLogged() {
  s = getCookie('loginUserName', '');
  if (s=='') return false;
  if (s=='0') return false;
  if (s=='0.0') return false;
  return true;
 };

 function Login() {
  location.href='login.htm';
 };

 function Logout() {
  setCookie('loginUserName','');
  location.href='default.htm';
  //divKI.innerHTML='';
 };

 function ShowLang(s) {
  location.href = 'default'+ s + '.htm';
 };
 
 function MakePdf() {
  //saveDiscount ();
  window.open('system/function.dll?function=wsReport', '_blank');
 };

 function MakePop() {
  location.href= 'system/function.dll?function=wsPopust';
 };
 
 function UpdateData() {
  window.open('system/function.dll?function=wsUvoz', '_blank');
 };
 
 function ShowHelp(s) {
  window.open('help-' + s + '.htm', '_blank');
 };
 
 function ShowData() {
  window.open('Navodila-za-sklepanje-poslov.pdf', '_blank');
 };
 
 function ShowInfo() {
  window.open('info-' + lang + '.htm', '_blank');
 };

function ShowList(s) {
 location.href = 'system/function.dll?function=wsFromExcel';
};

function SelectOffer() {
  location.href = 'system/function.dll?function=wsOffer&counter=' + Math.random();
 };



 function ClearAll() {
  setCookie("position", "800,600,368000,15000,628000,210000");
  setCookie("list", "");
  setCookie("list2", "");
  setCookie("listVO", "");
  setCookie("type", "0");
  setCookie("mass", "0");
  setCookie("cust", "0");

  get('Select3').selectedIndex=getCookie("type", "0");
  get('Select4').value=getCookie("mass", "0");
  if (get('Select6')) get('Select6').value=getCookie("cust", "0");

  getMap();
  getList1();
  getList2();
  getQuickInfo();
 };


function postToUrl(url, params, newWindow)
{
    var form = $('<form>');
    form.attr('action', url);
    form.attr('method', 'POST');
    if(newWindow){ form.attr('target', '_blank'); }

    var addParam = function(paramName, paramValue){
        var input = $('<input type="hidden">');
        input.attr({ 'id':     paramName,
                     'name':   paramName,
                     'value':  paramValue });
        form.append(input);
    };

    // Params is an Array.
    if(params instanceof Array){
        for(var i=0; i<params.length; i++){
            addParam(i, params[i]);
        }
    }

    // Params is an Associative array or Object.
    if(params instanceof Object){
        for(var key in params){
            addParam(key, params[key]);
        }
    }

    // Submit the form, then remove it from the page
    form.appendTo(document.body);
    form.submit();
    form.remove();
}



 setCookie("lang", lang);

 allCookie("position", "800,600,368000,15000,628000,210000");
 allCookie("list", "");
 allCookie("type", "0");
 allCookie("mass", "0");
 allCookie("cust", "0");
 allCookie("id", 10000000 + Math.round(Math.random()*10000000));


 SetNavButton('za');
 SetNavButton('zi');
 SetNavButton('zo');

 SetNavButton('nw');
 SetNavButton('nc');
 SetNavButton('ne');
 SetNavButton('cw');
 SetCC();
 SetNavButton('ce');
 SetNavButton('sw');
 SetNavButton('sc');
 SetNavButton('se');

 get('Select3').selectedIndex=getCookie("type", "0")
 get('Select4').value=getCookie("mass", "0")
 if (get('Select6')) get('Select6').value=getCookie("cust", "0");

 getMap();
 getList1();
 getList2();
 getQuickInfo();





