function ExchangeDirection() {
  var str = document.getElementById("stat1").value;
  document.getElementById("stat1").value = document.getElementById("stat2").value;
  document.getElementById("stat2").value = str;
  document.getElementById("stat1sel").swapNode(document.getElementById("stat2sel"));
  document.getElementById("stat1sel").id="stat2sel_tmp";
  document.getElementById("stat2sel").id="stat1sel";
  document.getElementById("stat2sel_tmp").id="stat2sel";
  var str = document.getElementById("stat1sel").onchange;
  document.getElementById("stat1sel").onchange = document.getElementById("stat2sel").onchange;
  document.getElementById("stat2sel").onchange = str;
}

function StationBetweenKeyDown(e) {
  if (e.keyCode) keyCode=e.keyCode; else keyCode=e.which;
  if (keyCode == 46) document.getElementById("StationBetween").value=-1;
}

function TimeKeyPress(e) {
  if (e.keyCode) keyCode=e.keyCode; else keyCode=e.which;
  if ((keyCode < 48 || keyCode > 57) && keyCode!=8 && keyCode!=58 && keyCode!=13 && keyCode!=9 && (keyCode < 35 || keyCode > 40)) return false;
  return true;
}

function TimeBlur(obj) {
  var str = obj.value;
  var arr = str.split(":");
  if (arr[0]) if (arr[0].length < 2) arr[0] = "0" + arr[0];
  if (arr[1]) if (arr[1].length < 2) arr[1] = "0" + arr[1];
  var nn=0, hh=0;
  str = arr.join("");
  if (str.length == 5) str=str.substring(0,2)+str.substring(3,5);
  if (str.length > 2) {
    nn = parseInt(str.substring(str.length-2,str.length),10);
  	hh = parseInt(str.substring(0,str.length-2),10);
  } else {
	if (str != "") nn = parseInt(str,10); else nn = 0;
	hh = 0;
  }
  if (nn > 59) nn=59;
  if (hh > 23) hh=23;
  obj.value=String(100+hh).substring(1,3)+":"+String(100+nn).substring(1,3);
}

function DateBlur(obj) {
  var str = obj.value;
  var arr = str.split(".");
  var N=0, dd="", mm="", yy="";
  for (var i=0; i<arr.length; i++)
    if (arr[i] != "") {
      switch (N) {
        case 0: dd=parseInt(arr[i],10).toString(); break
		case 1: mm=parseInt(arr[i],10).toString(); break
		case 2: yy=parseInt(arr[i],10).toString(); break
      }
	  N++;
    }
  
  if (dd.length > 2 && mm == "") {
    if (dd.length % 2 != 0) dd = "0"+dd;
	mm = dd.substring(2,dd.length);
	dd = dd.substring(0,2);
  }
  if (mm.length > 2 && yy == "") {
    if (mm.length % 2 != 0) mm = "0"+mm;
    yy = mm.substring(2, mm.length);
	mm = mm.substring(0,2);
  }
  
  if (dd == "") dd=0;
  if (mm == "") mm=0;
  if (yy == "") yy=0;

  dd = parseInt(dd,10);
  mm = parseInt(mm,10);
  yy = parseInt(yy,10);
    
  var curdate = new Date();
  if (dd == 0) dd = curdate.getDate();
  if (mm == 0) mm = curdate.getMonth()+1;
  if (yy == 0) yy = curdate.getFullYear();
  else if (yy < 1000) yy+=2000;
  else if (yy > 2069) yy=2069;
  if (mm > 12) mm=12;
  if (dd > 31 && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)) dd=31;
  if (dd > 30 && (mm==4 || mm==6 || mm==9 || mm==11)) dd=30;
  if (dd > 29 && mm==2 && yy%4==0) dd=29;
  if (dd > 28 && mm==2 && yy%4!=0) dd=28;
  var oldvalue = obj.value;
  obj.value=String(100+dd).substring(1,3)+"."+String(100+mm).substring(1,3)+"."+yy;
  if (obj.value != oldvalue && obj.onchange) obj.onchange();
}

function DateKeyPress(e) {
  if (e.keyCode) keyCode=e.keyCode; else keyCode=e.which;
  if ((keyCode < 48 || keyCode > 57) && keyCode!=8 && keyCode!=46 && keyCode!=13 && keyCode!=9 && (keyCode < 35 || keyCode > 40)) return false;
  return true;
}

function RemoveAnchors(str) {
  do {
    b = str.toLowerCase().indexOf("<a ");
	if (b != -1) {
      e = b + 1;
      ot = 1; ct = 0;
      while (ot != ct) {
  	    if (str.toLowerCase().charAt(e) == '<') ot++; 
		else if (str.toLowerCase().charAt(e) == '>') ct++;
	    e++;
      }
      str = str.substring(0,b) + str.substring(e,str.length);
	}
  }	while (b != -1)
  return str;
}

function check(type, obj) {
	if(obj.value == '')
		obj.value = type;
}

function checkfree(type, obj) {
	if(type == obj.value)
		obj.value = '';
}

function exchangeDirections() {
  var tmp = document.getElementById("stationIn").selectedIndex;
  document.getElementById("stationIn").selectedIndex = document.getElementById("stationOut").selectedIndex;
  document.getElementById("stationOut").selectedIndex = tmp;
}

function TestString(E) {
  var S = E.value
  checking = /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(S);
  if (checking == false)  E.focus();
  return checking;
}
		
function TestRange(forma) {
  if (forma.TimeOut.value < forma.TimeIn.value) {
    // alert('TimeOut < TimeIn');
    forma.TimeOut.focus();
    return false;
  } else {
    return true;
  }
}
		
function ValidForm(forma) {
  if (TestString(forma.TimeIn) && TestString(forma.TimeOut) && TestRange(forma)) {
    return true;
  } else {
	alert('Jūsu ievadītais laiks nav pareizs. Laika formātām jābūt "ss:mm" un laukā "līdz" ievadītais laiks nedrīkst būt mazāks par ievadīto laiku laukā "no".');
	//forma.TimeIn.focus();
	return false;
  }
}

function PasKeyPress(e) {
  if (e.keyCode) keyCode=e.keyCode; else keyCode=e.which;
  if ((keyCode < 48 || keyCode > 57) && keyCode!=8 && keyCode!=13 && keyCode!=9 && (keyCode < 35 || keyCode > 40)) return false;
  return true;
}