var stationinput=null;
var list = null;
var cal = null;

function keyDown(e) { 
    if(e)   { // if the event object is present (NN only)
        e = e // var e = event
    } else { 
        e = window.event // else e = window.event for IE
    }
    var keycode=null;
    if(e.which) {
        keycode = e.which // e.which is stored in variable "keycode"
    } else {
        keycode = e.keyCode // otherwise for IE, var keycode stores e.keyCode syntax
    }
    if (keycode == 13) {
        //popTrainInfo(document.getElementById('trainnum').value, document.getElementById('date_info').value);
    }
}
function hideAllPops() {
    if(list != null) list.hidePopup();
    if(cal != null) cal.hidePopup();
}
function sendValue(s){
    document.getElementById(stationinput).value = s;
    list.hidePopup();
}
function popStationsList(divName, input) {
    list = new PopupWindow(divName);
    list.offsetX=150;
    list.offsetY=-180;
    list.autoHide();
    list.showPopup(input);
    stationinput = input;
    document.getElementById(divName).style.display = 'block';
}
function showInfo() {
    hide('tab1');
    hide('tab2');
    show('tab3');
    hide('tab4');
    hide('tab5')
    hide('search_content');
}
function showSearch() {
    show('tab1');
    hide('tab2');
    hide('tab3');
    hide('tab4');
    hide('tab5')
    show('search_content');
    hideExtras();
    document.getElementById('via_station').value = '';
    //document.getElementById('via_min_time').value = '';
}
function showAdvanced() {
    hide('tab1');
    show('tab2');
    hide('tab3');
    hide('tab4');
    hide('tab5');
    hide('tab6')
    show('search_content');
    showExtras();
}

function showOffers() {
    hide('tab1');
    hide('tab2');
    hide('tab3');
    show('tab4')
    hide('tab5');
    hide('search_content');
    hideExtras();
}

function showInfoGara() {
    hide('tab1');
    hide('tab2');
    hide('tab3');
    hide('tab4');
    show('tab5');
    hide('search_content');
    hideExtras();
}



function showExtras() {
    hide('link_add_via');
    hide('link_more_options');
    document.getElementById('via_options1').style.display='';
    document.getElementById('via_options2').style.display='';
    document.getElementById('more_options').style.display='';
}
function hideExtras() {
    document.getElementById('link_add_via').style.display='';
    document.getElementById('link_more_options').style.display='';
    hide('via_options1');
    hide('via_options2');
    hide('more_options');
}
function toggleShow(which) {
    if(document.getElementById(which).style.display=='block') {
        document.getElementById(which).style.display = 'none';
    } else {
        document.getElementById(which).style.display = 'block';
    }                                                   
}  
function show(which) {
    document.getElementById(which).style.display = 'block';
}
function hide(which) {
    document.getElementById(which).style.display = 'none';
}
function initSearch() {
    var station = Get_Cookie('fromStation');
    if (station != null) document.getElementById('from_station').value = station;
    window.onresize = hideAllPops;
    document.getElementById("search").from_station.focus();
    document.getElementById('via_station').value = '';
    //document.getElementById('via_min_time').value = '';
    //document.getElementById("search").date.value=formatDate(new Date(),'dd/MM/yyyy');
}



/*POPUP FUNCTIONS*/

function PopWindow(url) { newWindow =
    window.open(url,'pop','width=800,height=600,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes,top=0,left=0');
}     
function PopupCenter(pageURL, title,w,h) {
                var left = (screen.width/2)-(w/2);
                var top = (screen.height/2)-(h/2);
                var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}



function pr(dt, st) {
    nod=document.getElementById(dt);
    nod2=document.getElementById(st);
    w=open("", "", "");
    w.document.open();
    w.document.write("<HEAD><link href='" + nod2.href + "' rel='stylesheet' type='text/css'>");
    w.document.write("</HEAD>");
    w.document.write("<BODY>");
    w.document.write(nod.innerHTML);
    w.document.write("<br>");
    w.document.write("<input class=button type=button value='Печат' onclick='print()'> ");
    w.document.write("<input class=button type=button value='Затвори' onclick='window.close()'>");
    w.document.write("</BODY>");
    w.document.close();
    //w.print();
    //w.close();
}
function setAll() {
    document.forms['search'].elements['aft'].checked = false;
    document.forms['search'].elements['ft'].checked = false;
    document.forms['search'].elements['ot'].checked = false;
}
function rmAll() {
    document.forms['search'].elements['all_cats'].checked = false;
}
  function Set_Cookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) expires = expires * 1000 * 60 * 60 * 24;
    var expires_date = new Date( today.getTime() + (expires) );    
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
  }
  function Get_Cookie( name ) {	
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
      return null;
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
  }
  function setFromStation() {
    var query = document.getElementById('from_station').value;
    if (query != null) Set_Cookie( 'fromStation', query, 365, '/', 
    document.domain, '' );
  }
