﻿var WinOnload = window.onload;
window.onload = WinOnloadWrapper;

function WinOnloadWrapper() {
    if (WinOnload) {
        WinOnload();
    }

    try {
        FormOnload();
    } catch (e) {
    }

}

var XmlHttpReadyState = {
    "UNSENT" : 0,
    "OPENED" : 1,
    "HEADERS_RECEIVED" : 2,
    "LOADING" : 3,
    "DONE" : 4
};

function GetXmlHttpObject() {
  var xmlHttp = null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      // Internet Explorer 5.5+
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
      }
    }
  }
  
  return xmlHttp;
}

function LoadControl(selectID, taxType, parentID, parentType, reload, disableControl) {
  var xmlHttp = GetXmlHttpObject();
  if (xmlHttp == null) { return; }

  var url = "";
  
  if (siteRoot) {
    url += siteRoot;
  }
  
  url += "/includes/js/DropDownList.aspx";
  
  url += "?taxType=" + taxType;
  url += "&parentId=" + parentID;
  
  if (parentType) {
    url += "&parentType=" + parentType;
  }
  
  if (reload) {
    url += "&reload=" + reload;
  }

  xmlHttp.onreadystatechange=function () {LoadControl_StateChanged(xmlHttp, selectID, disableControl);};
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  
//  if(!xmlHttp.onreadystatechange) {
//    LoadControl_StateChanged(xmlHttp, selectID);
//  }
}

function LoadControl_StateChanged(xmlHttp, selectID, disableControl) {
  var ddl = document.getElementById(selectID);
  
  ClearOptions(ddl);

  if (xmlHttp.readyState == XmlHttpReadyState.DONE
  && xmlHttp.status == 200) {
    var objReceived = eval("(" + xmlHttp.responseText + ")");

    if (objReceived) {
        ddl.disabled = (objReceived.length <= 1 || disableControl);

        for (var i=0; i<objReceived.length; i++) {
            AddOption(ddl, objReceived[i].Text, objReceived[i].Value, objReceived[i].Selected);
        }
    }
    
  } else {    
        AddOption(ddl, "loading...");
        ddl.disabled = true;
  }
}

function AddOption(select, text, value, selected) {
    if (select == null) { return; }
    
    var newOption = document.createElement('option');
    newOption.text = text;
    
    if (value) {
        newOption.value = value;
    } else {
        newOption.value = "";
    }
    
    if (selected) {
        newOption.selected = selected;
    }
    
    try {
        select.add(newOption, null); // standards compliant
    } catch (e) {
        select.add(newOption);
    }
}

function ClearOptions(select) {
    if (select == null) { return; }
    while(select.length > 0) {
        select.remove(select.length - 1);
    }
}

function GetSelectedValue(select) {
    if (select == null 
    || select.length == 0) { return ""; }

    return select.options[select.selectedIndex].value;
}

function DisableControl(select) {
    if (select == null) { return; }
    
    select.selectedIndex = 0;
    select.disabled = true;
}