var bPageInitStage = true;
var xmlhttp=null;
var xmlhttpReady = true;
var bXmlhttpSupported = isXmlhttpSupported();
var iframeReady = false;
var curFunc = "";
var FNGetDestPoints = "getDestPoints";
//bXmlhttpSupported = false;

function getXmlHttp()
{
  var oXmlHttp = null;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try
  {
      oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
      try
      {
          oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (E)
      {
          oXmlHttp = null;
      }
  }
@end @*/
  if (!oXmlHttp && typeof XMLHttpRequest != 'undefined') {
    try {
      oXmlHttp = new XMLHttpRequest();
    } catch (e) {
      oXmlHttp = null;
    }
  }
  return oXmlHttp;
}

function isXmlhttpSupported()
{
  if (getXmlHttp() == null) return false;
  return true;
}

function onOriginPointChange()
{
//  if (!xmlhttpReady)
  if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady)) return;
  curFunc = FNGetDestPoints;
  var selOriginPoint = getElem('TmtblFrm_selTravelFrom');
  var idOriginPoint = parseInt(selOriginPoint[selOriginPoint.selectedIndex].value, 10);
  if (typeof(idOriginPoint) == "undefined" || isNaN(idOriginPoint)) return;
  if (idOriginPoint == -1)
  {
    selOriginPoint.selectedIndex++;
    idOriginPoint = parseInt(selOriginPoint[selOriginPoint.selectedIndex].value, 10);
    if (typeof(idOriginPoint) == "undefined" || isNaN(idOriginPoint)) return;
  }
  if (idOriginPoint > 0 && id_prov > 0)
  {
    sendRequest("AjaxLibrary.aspx?func=" + curFunc + "&idOrigin=" + idOriginPoint + "&id_prov=" + id_prov + "&lng=" + lng + "&sbname=1");
  }
  else
  {
    destPoints = null;
    procDestPointsResponseData();
  }
}

function sendRequest(url)
{
//  if (!xmlhttpReady)
  if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady)) return;
  try
  {
    if (bXmlhttpSupported)
    {
      xmlhttp = getXmlHttp();
      xmlhttp.open("GET", url, true);
      xmlhttp.onreadystatechange = responseHandler;
      try
      {
        if (xmlhttpReady == true) xmlhttp.send(null);
      }
      catch(ex)
      {
          //alert("error");
      }
    }
    else
    {
      if (iframeReady == true)
      {
        //setIFrameReadyState(false);
        //window.ifr.location.href = url;
      }
      else
      {
        //alert("Frame is not ready.");
      }
    }
  }
  catch (e)
  {
  }
}

function responseHandler()
{
  if(xmlhttp.readyState == 4)
  {
    if (xmlhttp.status != 200 || xmlhttp.responseText == "")
    {
      xmlhttpReady = true;
      procDestPointsResponseData();
      return;
    }
    switch(curFunc)
    {
      case FNGetDestPoints:
      {
        //eval(responseData);
        // parse destinations
        destPoints = null;
        destPoints = new Object();
        var destinations = xmlhttp.responseText.split("|");
        for(var i=0; i < destinations.length; i++)
        {
          var point = destinations[i].split(";");
          eval("destPoints[\""+i+"\"] = new ListItem(\""+point[0]+"\", \""+point[1]+"\");");
        }

        procDestPointsResponseData();
        curFunc = "";
        xmlhttpReady = true;
        break;
      }
    }
  }
  else
  {
    if (xmlhttp.readyState >= 0 && xmlhttp.readyState <= 3)
    {
      switch(curFunc)
      {
        case FNGetDestPoints:
        {
          showTravelToWaitMessage();
          break;
        }
      }
    }
    xmlhttpReady = false;
  }
}

function procDestPointsResponseData()
{
  var selDestPoint = getElem('TmtblFrm_selTravelTo');
  if (getArrayLength(destPoints) > 0)
  {
    clearSelect(selDestPoint);
    setupSelect(selDestPoint, destPoints, selectLocationText);

    SetOriginPointValue();
    if (!SetDestPointValue()) selDestPoint.selectedIndex = 0;
    selDestPoint.className = "inpCtrl";

    UpdateHiddenOriginPointValue(true);
    UpdateHiddenDestPointValue(true);
    UpdateBtnSearch(false, true, false);
  }
  else
  {
    clearSelect(selDestPoint);
    setupSelect(selDestPoint, null, selectLocationText);
    selDestPoint.className = "inpCtrlDisbl";

    UpdateHiddenOriginPointValue(false);
    UpdateHiddenDestPointValue(false);
    UpdateBtnSearch(true, false, false);
  }
}

function showTravelToWaitMessage()
{
  var arr = new Array();
  arr[0] = new ListItem("0", msgLoading);

  var selDestPoint = getElem('TmtblFrm_selTravelTo');
  clearSelect(selDestPoint);
  setupSelect(selDestPoint, arr);
  selDestPoint.disabled = true;
}

function callInProgress(xmlhttp)
{
  switch ( xmlhttp.readyState )
  {
    case 1, 2, 3:
      return true;
    break;
    // Case 4 and 0
    default:
      return false;
    break;
  }
}

function updateXmlHttpStatus()
{
  xmlhttpReady = !callInProgress(xmlhttp);
  //return callInProgress(xmlhttp);
}

function setIFrameReadyState(state)
{
  if (bXmlhttpSupported) return;
}

function UpdateHiddenOriginPointValue(update)
{
  var selOriginPoint = getElem('TmtblFrm_selTravelFrom');
  var hdnIdOriginPoint = frm.hdnIdOriginPoint;
  hdnIdOriginPoint.value = "0";
  if (!update) return;
  hdnIdOriginPoint.value = "" + selOriginPoint[selOriginPoint.selectedIndex].value;
}

function UpdateHiddenDestPointValue(update)
{
  var selDestPoint = getElem('TmtblFrm_selTravelTo');
  var hdnIdDestPoint = frm.hdnIdDestPoint;
  hdnIdDestPoint.value = "0";
  if (!update) return;
  hdnIdDestPoint.value = "" + selDestPoint[selDestPoint.selectedIndex].value;
}

function SetOriginPointValue()
{
  var selOriginPoint = getElem('TmtblFrm_selTravelFrom');
  var strOriginPoint = selOriginPoint[selOriginPoint.selectedIndex].value;
  var hdnIdOriginPoint = frm.hdnIdOriginPoint;
  if (strOriginPoint == "0") return false;

  hdnIdOriginPoint.value = "0";
  var idOriginPoint = parseInt(strOriginPoint, 10);
  if (typeof(idOriginPoint) == "undefined" || isNaN(idOriginPoint) || idOriginPoint <= 0) return false;
  hdnIdOriginPoint.value = strOriginPoint;
  setSelected(selOriginPoint, strOriginPoint);

  return true;
}

function SetDestPointValue()
{
  var selDestPoint = getElem('TmtblFrm_selTravelTo');
  var strDestPoint = selDestPoint[selDestPoint.selectedIndex].value;
  var hdnIdDestPoint = frm.hdnIdDestPoint;
  if (strDestPoint == "0") return false;

  hdnIdDestPoint.value = "0";
  var id_destination = parseInt(strDestPoint, 10);
  if (typeof(id_destination) == "undefined" || isNaN(id_destination) || id_destination <= 0) return false;
  hdnIdDestPoint.value = strDestPoint;
  setSelected(selDestPoint, strDestPoint);

  return true;
}