function showWait(disp)
{
 if(top.document.getElementById("wait_pic"))
 {
  var st = top.document.getElementById("wait_pic").style;
 
  if(disp)
  {
   st.display = "block";
   st.visibility = "visible";
   st.zIndex = 11000;
   st.left = (((top.document.body.clientWidth || innerWidth) / 2) - 40) +"px";
   st.top = (((top.document.body.clientHeight || innerHeight) / 2) - 40) +"px";
  }
  else
  {
   st.display = "none";
   st.visibility = "hidden";
  }
 }
}

function isWait()
{
 if(top.document.getElementById("wait_pic"))
 {
  var st = top.document.getElementById("wait_pic").style;
 
  if(st.display == "none" || st.visibility == "hidden")
   return false;
  else
   return true; 
 }
}

function showProgress(disp,tmout)
{
 var st = top.document.getElementById("progress_pic");
 
 if(disp)
 {
  st.style.display = "block";
  st.style.visibility = "visible";
  
  st.style.left = (((top.document.body.clientWidth || innerWidth) / 2) - 55) +"px";
  st.style.top = (((top.document.body.clientHeight || innerHeight) / 2) - 55) +"px";
  
  top.document.getElementById("progress_pic").src = "progress_pic.php" + (tmout != undefined && tmout>0?"?to="+tmout:"");
 }
 else
 {
  st.style.display = "none";
  st.style.visibility = "hidden";
 }
}


function getISODt(dt,ref_dt)
{
 var ret_val = "";

 if(dt != "")
 {
  var date_array = trim(dt).match(/^(\d{1,2})\.(\d{1,2})\.(\d{2,4})?\s?((\d{1,2})\:(\d{1,2})(\:(\d{1,2}))?)?$/);
  
  if(date_array)
  {
   var day = date_array[1];
   var month = date_array[2];
   var year = date_array[3];
   
   if(year == "" || year == undefined)
   {
    if(typeof(ref_dt) == "undefined" || ref_dt == undefined)
    {
     today=new Date();

     year = today.getFullYear();
     
     prev_year = new Date();
     prev_year.setFullYear(year-1,month-1,day);
     
     next_year = new Date();
     next_year.setFullYear(year+1,month-1,day);
     
     this_year = new Date();
     this_year.setFullYear(year,month-1,day);
     
     if(!(Math.abs(today.getTime()-this_year.getTime())<Math.abs(today.getTime()-prev_year.getTime())
      && Math.abs(today.getTime()-this_year.getTime())<Math.abs(today.getTime()-next_year.getTime())))
     {
      if(Math.abs(today.getTime()-prev_year.getTime())<Math.abs(today.getTime()-next_year.getTime()))
       year -= 1;
      else
       year += 1;
     }
    }
    else
    {
     year = ref_dt.getFullYear();
    
     if(sprintf("%04s%02s%02s",year,month,day)<sprintf("%04s%02s%02s",year,ref_dt.getMonth()+1,ref_dt.getDate()))
      year++;
    }
   }
   
   var hr;
   var min;
   var sec;
   
   if(date_array[4]!="")
   {
    hr = date_array[5];
    min = date_array[6];
    sec = date_array[8];
    
    if(sec=="" || sec==undefined)
     sec = "0";
   }
   else
   {
    hr = "0";
    min = "0";
    sec = "0";
   }
  
//  alert(year + "," + month  + "," + day + "," + hr + "," + min  + "," + sec);
  
   ret_val = (year<100?"20" + sprintf("%02s",year):year) + sprintf("%02s",month) + sprintf("%02s",day) + sprintf("%02s",hr) + sprintf("%02s",min) + sprintf("%02s",sec);
  }
 }
 
 return ret_val; 
}

function isDateCorrect(dt,skip_hr)
{
 if(dt != "")
 {
  var date_array = trim(dt).match(/^(\d{1,2})\.(\d{1,2})\.(\d{2,4})?\s?((\d{1,2})\:(\d{1,2})(\:(\d{1,2}))?)?$/);
  
  if(date_array)
  {
   var day = date_array[1];
   var month = date_array[2];
   var year = date_array[3];
   
   if(year == "" || year == undefined)
   {
    dt=new Date();
    
    year = dt.getFullYear();
   }
   
   if(date_array[4]!="" || skip_hr)
   {
    var hr;
    var min;
    var sec;
   
    if(date_array[4]!="")
    {
     hr = date_array[5];
     min = date_array[6];
     sec = date_array[8];
    
     if(sec=="" || sec==undefined)
      sec = "0";
    }
    else if(skip_hr)
    {
     hr = "0";
     min = "0";
     sec = "0";
    }
    
    source_date = new Date(year,month-1,day,hr,min,sec,0);
   
    if(year != source_date.getFullYear() || month != source_date.getMonth()+1 || day != source_date.getDate() || hr != source_date.getHours() || min != source_date.getMinutes() || sec != source_date.getSeconds())
     return false;
   }
   else
    return false;
  }
  else
   return false;
 }
	
 return true;
} 

function getStringFromDate(dt, skip_sec)
{
 return dt.getDate() + "." + (dt.getMonth()+1) + "." + dt.getFullYear() + " " + dt.getHours() + ":" + dt.getMinutes() + ((skip_sec==undefined || skip_sec==false)?":" + dt.getSeconds():"");
}

function getDtFromString(dt,ref_dt,local)
{
 var iso_dt = getISODt(dt,ref_dt);   
 
 if(local)
  return new Date(iso_dt.substr(0,4),iso_dt.substr(4,2)-1,iso_dt.substr(6,2),iso_dt.substr(8,2),iso_dt.substr(10,2),iso_dt.substr(12,2),0);
 else
  return UTCDate(iso_dt.substr(0,4),iso_dt.substr(4,2)-1,iso_dt.substr(6,2),iso_dt.substr(8,2),iso_dt.substr(10,2),iso_dt.substr(12,2),0);
}

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

function reformatCoord(coord,other_ctrl,is_lat,ret_array)
{
 if(typeof(coord)=="number")
  coord = new String(coord);

 var ret_val = coord.toUpperCase();

 if(ret_val != "")
 {
  ret_val = ret_val.replace(/,/g,".");
  ret_val = ret_val.replace(/^\s+|\s+$/g,'');
  
  var vysl;

  if((vysl = ret_val.match(/^(-)?([0|1]?\d{1,2})((\.)(\d*))?\s*([NEWS])?$/)) != null)
  {   
   if(vysl[6] == "S" || vysl[6] == "W")  
    vysl[1] = "-";
    
   if(vysl[5] == "" || vysl[5] == undefined)
    vysl[5] = 0;
   
   var tmp = ("0." + vysl[5]) * 60.0;

   var min = parseInt(tmp);
   var sec = Math.round((tmp-min)*600000.0,4)/10000;
   
   var format_string = "%s";
   
   if(!is_lat || vysl[6] == "E" || vysl[6] == "W") 
    format_string += "%03s";
   else
    format_string += "%02s";
   
   format_string += ":%02d:%05.2f";

   for(var i=0; i<3; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   ret_val = sprintf(format_string,vysl[1],vysl[2],min,sec);
  }
  else if((vysl = ret_val.match(/^(-)?([0|1]?\d{6})((\.)(\d*))?\s*([NEWS])?$/)) != null)
  {
   if(vysl[6] == "S" || vysl[6] == "W")  
    vysl[1] = "-";
    
   var format_string = "%s";
   
   if(!is_lat || vysl[6] == "E" || vysl[6] == "W") 
    format_string += "%03s";
   else
    format_string += "%02s";
   
   format_string += ":%02s:%02s";
   
   for(var i=0; i<vysl.length; i++)
   {
    if(vysl[i] == undefined)
     vysl[i] = "";
   }
   
   if(vysl[5]!="")
    format_string += "."+vysl[5];
   
   ret_val = sprintf(format_string,vysl[1],vysl[2].substr(0,vysl[2].length-4),vysl[2].substr(vysl[2].length-4,2),vysl[2].substr(vysl[2].length-2,2));
  }
  else if((vysl = ret_val.match(/^(-)?([0|1]?\d{1,2})[\:\.]([0-5]?\d)[\:\.]([0-5]?\d)(\.\d*)?\s*([NEWS])?$/)) != null)
  {
   if(vysl[6] == "S" || vysl[6] == "W")  
    vysl[1] = "-";
    
   var format_string = "%s";
   
   if(!is_lat || vysl[6] == "E" || vysl[6] == "W") 
    format_string += "%03s";
   else
    format_string += "%02s";
   
   format_string += ":%02s:%02s%s";
   
   for(var i=0; i<6; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   ret_val = sprintf(format_string,vysl[1],vysl[2],vysl[3],vysl[4],vysl[5]);
  }
  else if((vysl = ret_val.match(/^([NEWS])?\s?(-)?\s?([0|1]?\d{1,2})°?\s?((\d*(\.\d*)?)\'?)?$/)) != null)
  {
   if(vysl[1] == "S" || vysl[1] == "W")  
    vysl[2] = "-";
    
   if(vysl[5] == "" || vysl[5] == undefined) 
    vysl[5] = "0";
    
   var min = parseInt(vysl[5]);
   var sec = Math.round((vysl[5]-min)*600000.0,4)/10000;

   var format_string = "%s";
   
   if(!is_lat || vysl[1] == "E" || vysl[1] == "W") 
    format_string += "%03s";
   else
    format_string += "%02s";
   
   format_string += ":%02d:%05.2f";
   
   for(var i=0; i<4; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   ret_val = sprintf(format_string,vysl[2],vysl[3],min,sec);
  }
  else if((vysl = ret_val.match(/(LOC:\s?)?(-)?\s?([0|1]?\d{1,2})[°\s\:]?([0-5]?\d)[\'\s\:]?([0-5]?\d)(\.\d*)?\"?\s*([NEWS])?[,\s\.]*(-)?\s?([0|1]?\d{1,2})[°\s\:]?([0-5]?\d)[\'\s\:]?([0-5]?\d)(\.\d*)?\"?\s*([NEWS])?/)) != null)
  {
   if(vysl[7] == "S" || vysl[7] == "W")  
    vysl[2] = "-";

   if(vysl[13] == "S" || vysl[13] == "W")  
    vysl[8] = "-";

   var format_string_lat = format_string_lon = "%s";
   
   format_string_lon += "%03s";
   format_string_lat += "%02s";
   
   format_string_lat += ":%02s:%02s%s";
   format_string_lon += ":%02s:%02s%s";
   
   for(var i=0; i<13; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   var lat, lon;
   
   if(vysl[7] == "E" || vysl[7] == "W") 
   {
    lon = sprintf(format_string_lon,vysl[2],vysl[3],vysl[4],vysl[5],vysl[6]);
    lat = sprintf(format_string_lat,vysl[8],vysl[9],vysl[10],vysl[11],vysl[12]);
   }
   else
   {
    lat = sprintf(format_string_lat,vysl[2],vysl[3],vysl[4],vysl[5],vysl[6]);
    lon = sprintf(format_string_lon,vysl[8],vysl[9],vysl[10],vysl[11],vysl[12]);
   }
   
   
   if(ret_array)
   {
    ret_val = new Array(lat,lon);
   }
   else
   {
    if(is_lat)
     ret_val = lat;
    else
     ret_val = lon;
    
    
    if(other_ctrl != null) 
    {
     if(is_lat)
      other_ctrl.value = lon;
     else
      other_ctrl.value = lat;
    }
   }
  }
  else if((vysl = ret_val.match(/^\d{0,3}\.?\s?(-)?\s?([0|1]?\d{2})[\:\s]?([0-5]\d)[\:\s]?([0-5]\d)(\.\d*)?\s*([NEWS])?[,\s]*(-)?\s?([0|1]?\d{2})[\:\s]?([0-5]\d)[\:\s]?([0-5]\d)(\.\d*)?\s*([NEWS])?$/)) != null)
  {
   if(vysl[6] == "S" || vysl[6] == "W")  
    vysl[1] = "-";

   if(vysl[12] == "S" || vysl[12] == "W")  
    vysl[7] = "-";

   var format_string_lat = format_string_lon = "%s";
   
   format_string_lon += "%03s";
   format_string_lat += "%02s";
   
   format_string_lat += ":%02s:%02s%s";
   format_string_lon += ":%02s:%02s%s";
   
   for(var i=0; i<12; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   var lat, lon;
   
   if(vysl[6] == "E" || vysl[6] == "W") 
   {
    lon = sprintf(format_string_lon,vysl[1],vysl[2],vysl[3],vysl[4],vysl[5]);
    lat = sprintf(format_string_lat,vysl[7],vysl[8],vysl[9],vysl[10],vysl[11]);
   }
   else
   {
    lat = sprintf(format_string_lat,vysl[1],vysl[2],vysl[3],vysl[4],vysl[5]);
    lon = sprintf(format_string_lon,vysl[7],vysl[8],vysl[9],vysl[10],vysl[11]);
   }
    
   if(ret_array)
   {
    ret_val = new Array(lat,lon);
   }
   else
   {
    if(is_lat)
     ret_val = lat;
    else
     ret_val = lon;
   
    if(other_ctrl != null) 
    {
     if(is_lat)
      other_ctrl.value = lon;
     else
      other_ctrl.value = lat;
    }
   }
  }
  else if((vysl = ret_val.match(/([NEWS])([0|1]?\d{1,2})[°\s]?([0-5]\d)[\’\s]?([0-5]\d)[\”\t\s]?([NEWS])([0|1]?\d{1,2})[°\s]?([0-5]\d)[\’\s]?([0-5]\d)[\”\s]?/)) != null)
  {
   var zn1 = "";
   var zn2 = "";
   
   if(vysl[1] == "S" || vysl[1] == "W")  
    zn1 = "-";

   if(vysl[5] == "S" || vysl[5] == "W")  
    zn2 = "-";

   var format_string_lat = format_string_lon = "%s";
   
   format_string_lon += "%03s";
   format_string_lat += "%02s";
   
   format_string_lat += ":%02s:%02s";
   format_string_lon += ":%02s:%02s";
   
   for(var i=0; i<9; i++)
    if(vysl[i]==undefined)
     vysl[i] = "";
   
   var lat, lon;
   
   if(vysl[1] == "E" || vysl[1] == "W") 
   {
    lon = sprintf(format_string_lon,zn1,vysl[2],vysl[3],vysl[4]);
    lat = sprintf(format_string_lat,zn2,vysl[6],vysl[7],vysl[8]);
   }
   else
   {
    lat = sprintf(format_string_lon,zn1,vysl[2],vysl[3],vysl[4]);
    lon = sprintf(format_string_lat,zn2,vysl[6],vysl[7],vysl[8]);
   }
    
   if(ret_array)
   {
    ret_val = new Array(lat,lon);
   }
   else
   {
    if(is_lat)
     ret_val = lat;
    else
     ret_val = lon;
    
    
    if(other_ctrl != null) 
    {
     if(is_lat)
      other_ctrl.value = lon;
     else
      other_ctrl.value = lat;
    }
   }
  }
 }
 
 return ret_val;
}

function isCoordCorrect(coord,is_lat)
{
 if(coord != "")
 {
  coord = coord.replace(/^\s+|\s+$/g,'');
  
  var vysl;
 
  if((vysl = coord.match(/^(-)?([0|1]?\d{1,2})[\:\.]([0-5]?\d)[\:\.]([0-5]?\d)(\.\d*)?\s*([NEWS])?$/)) == null)
   return false;
  else
  {
   var tmp = parseFloat(vysl[2]) + (parseFloat(vysl[3])/60.0) + (parseFloat(vysl[4] + vysl[5])/3600.0); 
   
   if((is_lat && tmp>90) || (!is_lat && tmp>180))
    return false;
  }
 }

 return true;
}

function isAltitudeCorrect(alt,alt_typ)
{
 if(alt != "" && /^\d{1,5}$/.test(alt) && (alt_typ != 2 || parseInt(alt) <= 660))
  return true;
 else
  return false;
}

function trim(str, chars) 
{
 return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
 if(str != null && str != undefined)
 {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"),"");
 }
 else
  return "";
}

function rtrim(str, chars) 
{
 if(str != null && str != undefined)
 {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"),"");
 }
 else
  return "";
}

function collectionToArray(col) {
    a = new Array();
    for (i = 0; i < col.length; i++)
        a[a.length] = col[i];
    return a;
}

function isEmailCorrect(val)
{ 
 val = trim(val);
 
 if(val=="" || /^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/i.test(val))
  return true;
 else
  return false;
}

function selectNahoru(ctrl,ctrl_hidden)
{
 if(ctrl.selectedIndex > 0)
 {
  var sel = ctrl.selectedIndex;
  switchOptions(ctrl,sel,sel-1)
  
  if(ctrl_hidden)
   switchOptions(ctrl_hidden,sel,sel-1)
 }
}

function selectDolu(ctrl,ctrl_hidden)
{
 if(ctrl.selectedIndex >= 0 && ctrl.selectedIndex<ctrl.options.length-1)
 {
  var sel = ctrl.selectedIndex;
  switchOptions(ctrl,sel,sel+1)
  
  if(ctrl_hidden)
   switchOptions(ctrl_hidden,sel,sel+1)
 }
}

function switchOptions(ctrl,opt1,opt2)
{
 var txt = ctrl.options[opt1].text;
 var id = ctrl.options[opt1].value;
     
 ctrl.options[opt1] = new Option(ctrl.options[opt2].text,ctrl.options[opt2].value);
 ctrl.options[opt2] = new Option(txt,id);
     
 ctrl.selectedIndex = opt2;
}

function reformatHour(hr)
{
 ret_val = trim(hr.toUpperCase());

 if(ret_val != "")
 {
  ret_val = ret_val.replace(/,/g,".");
  
  var vysl;
  
  if((vysl = ret_val.match(/^\d{1,2}$/)) != null)
  {
   ret_val = sprintf("%02s:00",ret_val);
  }
  else if((vysl = ret_val.match(/^\d{3,4}$/)) != null)
  {
   ret_val = sprintf("%02s:%02s",ret_val.substr(0,ret_val.length-2),ret_val.substr(ret_val.length-2));
  }
  else if((vysl = ret_val.match(/^(\d{1,2})\:(\d{1,2})$/)) != null)
  {
   ret_val = sprintf("%02s:%02s",vysl[1],vysl[2]);
  }
 } 
 
 return ret_val;
}

function isHourCorrect(hr,use_sssr)
{
 ret_val = true;
 hr = trim(hr);

 if(hr != "")
 {
  ret_val = false;
  
  if((vysl = hr.match(/^(\d{2})\:(\d{2})$/)) != null)
  {
   if(parseInt(vysl[1])<24 && parseInt(vysl[2])<60)
    ret_val = true;
  }
  
  if(!ret_val && use_sssr && (hr.match(/^SS$/i) || hr.match(/^SR$/i)))
   ret_val = true;
 }
 
 return ret_val;
}

Array.prototype.compare = function(testArr) 
{    
 if(this.length != testArr.length) 
  return false;    
 
 for(var i = 0; i < testArr.length; i++) 
 {        
  if(this[i].compare) 
  {             
   if(!this[i].compare(testArr[i])) 
    return false;        
  }        
  
  if(this[i] !== testArr[i]) 
   return false;    
 }    
 
 return true;
}

Array.prototype.search = function(val, sub) 
{    
 var ret_val = -1;
  
 for(var i = 0; i < this.length; i++) 
 {        
  if(((sub==undefined || sub==null || sub==false) && this[i] == val) 
   || (sub==true && this[i].indexOf(val) >= 0))
  {             
   ret_val = i;
   break;        
  }        
 }    
 
 return ret_val;
}

Date.prototype.getDOY = function() 
{
 var onejan = new Date(this.getFullYear(),0,1);
 return Math.ceil((this - onejan) / 86400000);
} 

function changeSaveClassName(doc,not_change)
{
 var docu;
 
 if(doc)
  docu = doc;
 else
  docu = document;

 var lists = collectionToArray(docu.getElementsByTagName("INPUT"));
 lists = lists.concat(collectionToArray(docu.getElementsByTagName("SELECT")));
 lists = lists.concat(collectionToArray(docu.getElementsByTagName("TEXTAREA")));
    
 var changed = 0;
 
 for (var i = 0; i < lists.length; i++) 
 {
  if(lists[i].className == "input_changed")
  {
   if(!not_change)
    lists[i].className = "input_saved";
    
   changed++;
  }
 }
 
 return changed;
}

function convertAlt(alt,typ,unit)
{
 alt = parseInt(alt);
 
 switch(parseInt(typ))
 {
  case 2:
   alt *= 100;
   break;
  case 3:
   alt = 0;
   break;
 }
 
 if(unit == 1)
  alt = Math.round(3.281*alt,0);
 
 return alt;
}

function dateDiff(dt1, dt2, unit)
{
 var ret_val = false;
 
 if(!dt1)
  dt1 = new Date();
 else if(typeof(dt1)=='string')
  dt1 = getDtFromString(dt1);
  
 if(!dt2)
  dt2 = new Date();
 else if(typeof(dt2)=='string')
  dt2 = getDtFromString(dt2);
  
 switch(unit)
 {
  case "s":
  case "S":
   ret_val = (dt2.getTime()-dt1.getTime())/1000;
   break;
  case "I":
  case "i":
   ret_val = (dt2.getTime()-dt1.getTime())/60000;
   break;
  case "h":
  case "H":
   ret_val = (dt2.getTime()-dt1.getTime())/3600000;
   break;
  case "d":
  case "D":
   ret_val = (dt2.getTime()-dt1.getTime())/86400000;
   break;
  case "m":
  case "M":
   var tmp1 = new Date(2000,0,dt1.getDate(),dt1.getHours(),dt1.getMinutes(),dt1.getSeconds());
   var tmp2 = new Date(2000,0,dt2.getDate(),dt2.getHours(),dt2.getMinutes(),dt2.getSeconds());
   var d_diff = dateDiff(tmp1, tmp2, "d");
   var m_diff = (dt2.getFullYear() - dt1.getFullYear())*12;
   m_diff += dt2.getMonth() - dt1.getMonth();
   m_diff += d_diff/31;
   
   ret_val = m_diff;
   break;
  case "Y":
  case "y":
   ret_val = dateDiff(dt1, dt2, "m")/12.0;
   break;
 }
  
 return ret_val;
}

function UTCDate(year,mon,mday,hour,min,sec)
{
 var d = new Date();
 
 if(year != undefined && year != null)
 {
  d.setUTCFullYear(year,mon,mday);
  d.setUTCHours(hour,min,sec,0);
  return d;
 } 
 else
 {
  var localTime = d.getTime();
  var localOffset = d.getTimezoneOffset() * 60000;
  var utc = localTime + localOffset;
 
  return new Date(utc);
 }
}

function UTCDateStr(year,mon,mday,hour,min,sec)
{
 var dt = UTCDate(year,mon,mday,hour,min,sec);   
 
 var ret_val = dt.getDate() + "." + (dt.getMonth()+1) + "." + dt.getFullYear() + " " + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
 
 return ret_val;
}

function removeOption(ctrl,txt)
{
 if(ctrl && txt != "" && ctrl.options)
 {
  for(var i=0; i<ctrl.options.length;i++)
  {
   if(ctrl.options[i].text == txt)
   {
    ctrl.options[i] = null;
   }
  }
 }
}

function searchOption(ctrl,id)
{
 ret_val = -1;
 if(ctrl && id >0 && ctrl.options)
 {
  for(var i=0; i<ctrl.options.length;i++)
  {
   if(ctrl.options[i].value == id)
   {
    ret_val = i;
    break;
   }
  }
 }
 
 return ret_val;
}


function searchOptionText(ctrl,txt)
{
 ret_val = -1;
 if(ctrl && txt!="" && ctrl.options)
 {
  for(var i=0; i<ctrl.options.length;i++)
  {
   if(ctrl.options[i].text == txt)
   {
    ret_val = i;
    break;
   }
  }
 }
 
 return ret_val;
}

function selectCount(thefield) 
{
    for( var i=0,count=0; i<thefield.length; i++ ) {
        count += ( thefield.options[i].selected ? 1 : 0 )
    }    
    
    return count;
} 

function unselectAll(ctrl)
{
 if(ctrl)
 {
  for(var i = 0; i<ctrl.options.length; i++)
   ctrl.options[i].selected = false;
 }
}

function getSelectedOptions(ctrl)
{
 var ret_val = "";
 
 if(ctrl)
 {
  for(var i=0; i<ctrl.length; i++ ) 
  {
   if(ctrl.options[i].selected)
   {
    if(ret_val != "")
     ret_val += ",";
      
    ret_val += ctrl.options[i].value;
   }
  }
 }
 
 return ret_val;
}

function reformatPhone(val,use_international)
{
 var ret_val = "";
 
 val = trim(val);
 var a_num = val.match(/\d/g);
 
 //alert(a_num);
 if(a_num != null && a_num.length>8)
 {
  var nbr_start;
  var ctry_start;
  
  if(val.indexOf("00")==0)
  {
   nbr_start=5;
   ctry_start=2;
  }
  else if(val.indexOf("+")==0)
  {
   nbr_start=3;
   ctry_start=0;
  }
  else
  {
   nbr_start=0;
   ctry_start=-1;
  }
  
  for(var i=nbr_start;i<nbr_start+9 && i<a_num.length;i++)
  {
   if(ret_val.length>0 && ((i-nbr_start) % 3) == 0)
    ret_val += " ";
   
   ret_val += a_num[i];
  }
  
  if(use_international && ctry_start>=0)
   ret_val = "(" + a_num[ctry_start] + a_num[ctry_start+1] + a_num[ctry_start+2] + ") " + ret_val;
 }
    
 return ret_val;   
}

function reformatPSC(val)
{
 var ret_val = "";
 
 val = trim(val);
 var a_num = val.match(/\d/g);
 
 //alert(a_num);
 if(val != "" && a_num != null && a_num.length==5)
 {
  for(var i=0;i<a_num.length;i++)
  {
   if(ret_val.length==3)
    ret_val += " ";
   
   ret_val += a_num[i];
  }
 }
 else
  ret_val = val;
    
 return ret_val;   
}

function isPSCCorrect(val)
{
 val = trim(val);
 
 return (val == "" || /^\d{3}\s\d{2}$/.test(val));	
}

function isPhoneCorrect(val)
{ 
 val = trim(val);
 
 if(val=="" || /^((\+|00)\d{3}\s?)?((\d{9})|(\d{2}\s\d{2}\s\d{2}\s\d{2})|(\d{3}\s\d{3}\s\d{3})|(\d{3}\s\d{2}\s\d{2}\s\d{2}))$/.test(val))
  return true;
 else
  return false;
}

function htmlspecialchars (string, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: get_html_translation_table
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
 
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (histogram = this.get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {
        return false;
    }
    
    histogram["'"] = '&#039;';
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

function get_html_translation_table(table, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco
    // +   bugfixed by: madipta
    // +   improved by: KELAN
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js. Meaning the constants are not
    // %          note: real constants, but strings instead. integers are also supported if someone
    // %          note: chooses to create the constants themselves.
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
 
    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
 
    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }
 
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
    }
 
    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    else
     entities['38'] = '&amp;';

    entities['60'] = '&lt;';
    entities['62'] = '&gt;';
 
    // ascii decimals for better compatibility
 
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        histogram[symbol] = entities[decimal];
    }
    
    return histogram;
}

function openWindowWithPost(url,name,keys,values)
{
 var newWindow = window.open(url, name); 
 
 if (!newWindow) 
  return false;
 
 var html = "";

 html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
 
 if (keys && values && (keys.length == values.length))
  for (var i=0; i < keys.length; i++)
   html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";

 html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
 newWindow.document.write(html);
 
 return newWindow;
}

function getFormData(doc)
{
 var docu;
 
 if(doc)
  docu = doc;
 else
  docu = document;

 var lists = collectionToArray(docu.getElementsByTagName("INPUT"));
 lists = lists.concat(collectionToArray(docu.getElementsByTagName("SELECT")));
 lists = lists.concat(collectionToArray(docu.getElementsByTagName("TEXTAREA")));
    
 var ret_val = "";
 
 for (var i = 0; i < lists.length; i++) 
 {
//  if(lists[i].id == "trasa")
//   alert(lists[i].value);
   
  switch(lists[i].type)
  {
   case "text":
   case "textarea":
   case "hidden":
   case "password":
   case "select-one":
    ret_val += (ret_val == ""?"":"&") + lists[i].id + "=" + encodePost(lists[i].value);
    break;  
   case "checkbox":
    ret_val += (ret_val == ""?"":"&") + lists[i].id + "=" + (lists[i].checked?"1":"0");
    break;  
  }
 }
 
 return ret_val;
}

function encodePost(str) 
{
 if(typeof(str)=='string')
 {
  var ret_val = str.replace(/\&/g,"*|*").replace(/\</g,"*~*");
  return ret_val;
 }
 else
  return str;
} 

String.prototype.decodePost = function()
{
 var ret_val = this.replace(/\*~\*/g,"<").replace(/\*\|\*/g,"&");
 return ret_val;
}

function copy_to_clipboard(text)   
{   
    if(window.clipboardData)   
	    window.clipboardData.setData('text',text);   
	    
    return false;   
}  

function focusNext(el)
{
 var f = document.body;
 var els = f.document.documentElement.all;
 var x, nextEl;
 for (var i=0, len=els.length; i<len; i++)
 {
  x = els[i];
  if (el == x)
  {
   var j=i+1;
   
   if(j>len)
    j=0;
   
   while(true) 
   {
	j++;   
    if(j>len)
     j=0;
     
    if(j==i)
     break;

    if(els[j].nodeName == "INPUT")
    {
		try
		{
         els[j].focus();
         break;
		}
		catch(err)
		{
			
		}
    }
   }
  	  
   break;
  }
 }
}

function isAFTNCorrect(val)
{
 val = trim(val);
 
 if(val=="" || /^\w{8}$/.test(val))
  return true;
 else
  return false;
}
