function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function sendRequest(r,ie,responser) {
  if (ie == 1) {
    document.getElementById(responser).innerHTML = '<select name="port_dest" style="width:100px;"><option value=0>loading...</option></select>';
  } else {
    document.getElementById(responser).innerHTML = '<option value=0>loading...</option>';
  }
  var xmlhttp = getXmlHttp()
  xmlhttp.open('GET', r, true);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
       if(xmlhttp.status == 200) {
         document.getElementById(responser).innerHTML = xmlhttp.responseText;
       }
    }
  };
  xmlhttp.send(null);
}

function sendRequestGoogle(r,ie) {
  if (ie == 1) {
    document.getElementById("google_here").innerHTML = 'IE';
  } else {
    document.getElementById("google_here").innerHTML = 'FFox';
  }
  var xmlhttp = getXmlHttp()
  xmlhttp.open('GET', r, true);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
       if(xmlhttp.status == 200) {
         document.getElementById("google_here").innerHTML = xmlhttp.responseText;
       }
    }
  };
  xmlhttp.send(null);
}

function sendMapRequest(myparam) {
  var rand_no = Math.random();
  rand_no = rand_no * 100;
  var ver = navigator.appVersion;
  if (ver.indexOf("MSIE") != -1) {
    sendRequestGoogle('/cgi-bin/u/gmaps.cgi?' + myparam);
  } else {
    sendRequestGoogle('/cgi-bin/u/gmaps.cgi?' + myparam);
  }
}

function sendSearchRequest(selector,responser) {
  var rand_no = Math.random();
  rand_no = rand_no * 100;
  var ver = navigator.appVersion;
  if (ver.indexOf("MSIE") != -1) {
    sendRequest('/cgi-bin/ports.cgi?' + document.getElementById(selector).value + '&0&' + rand_no,1,responser);
  } else {
    sendRequest('/cgi-bin/ports.cgi?' + document.getElementById(selector).value + '&1&' + rand_no,0,responser);
  }
}

function addNewPort() {
  var o = document.getElementById('countrysel');
  var t = o.options[o.selectedIndex].text;
  var v = o.value;
  if (v != 0) {
    document.getElementById('addPortHere').innerHTML = '<div style="background-color:#ffffdd;padding: 4px 2px 2px 6px;">Please enter new port in <b>' + t + '</b>:<br><form method=post>Port of <input type=hidden name=ccode value=' + v +'><input type=hidden name=action value=addport><input type=text name=newport> <input type=submit class=fleetbutton value="add port"></form></div>';
  }  else {
    document.getElementById('addPortHere').innerHTML = '<div style="background-color:#ffffdd;padding: 4px 2px 2px 6px; color:#770000; font-weight:bold;">Please select country first!</div>';
  }
}

function showMessage(id) {
  var ver = navigator.appVersion;
  var rand_no = Math.random();
  rand_no = rand_no * 100;
  if (ver.indexOf("MSIE") != -1) {
    sendRequest('/cgi-bin/u/showmsg.cgi?' + id + '&' + rand_no,1,'responseHTML');
  } else {
    sendRequest('/cgi-bin/u/showmsg.cgi?' + id + '&' + rand_no,0,'responseHTML');
  }
}

function SubmitForm()
  {
    var pd = document.forms.voyageform.port_dest;

    if (pd == undefined || pd.value == 0)
    {
      alert('Please select port of destination!');
      return false;
    }

    return true;
}
