// DropDown Select Box

var dropdownDivID = "myDropDown";
var ddiFrameDivID = "myDropDownIframe";
var dditems = new Array();
dditems[0] = "Standart";
dditems[1] = "Engelli";
dditems[2] = "Suite";
dditems[3] = "Balayı";
dditems[4] = "Executive";
dditems[5] = "Kral";

/**
This is the main function you'll call from the onClick event of a button.
*/
function displayDropDown(FieldName, displayBelowThisObject)
{
  var targetField = document.getElementsByName (FieldName).item(0);
 
  // if we weren't told what node to display the dropdown beneath, just display it
  // beneath the field we're updating
  if (!displayBelowThisObject)
    displayBelowThisObject = targetField;
	
  var x = displayBelowThisObject.offsetLeft;
  var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight ;
 
  // deal with elements inside tables and such
  var parent = displayBelowThisObject;
  while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop ;
  }
 
  drawDropDown(targetField, x, y);
}

/**
Draw the dropdown object (which is just a table with selection elements) at the
specified x and y coordinates, using the targetField object as the input tag
that will ultimately be populated with the selected value.

This function will normally be called by the displayDropDown function.
*/
function drawDropDown(targetField, x, y)
{
  var fieldValue = targetField.value;
 
  // the dropdown table will be drawn inside of a <div> with an ID defined by the
  // global dropdownDivID variable. If such a div doesn't yet exist on the HTML
  // document we're working with, add one.
  if (!document.getElementById(dropdownDivID)) {
    // don't use innerHTML to update the body, because it can cause global variables
    // that are currently pointing to objects on the page to have bad references
    //document.body.innerHTML += "<div id='" + datePickerDivID + "' class='dpDiv'></div>";
    var newNode = document.createElement("div");
    newNode.setAttribute("id", dropdownDivID);
    newNode.setAttribute("class", "ddDiv");
    newNode.setAttribute("style", "visibility: hidden;");
    document.body.appendChild(newNode);
  }
 
  // move the dropdown div to the proper x,y coordinate and toggle the visiblity
  var ddDiv = document.getElementById(dropdownDivID);
  ddDiv.style.position = "absolute";
  ddDiv.style.left = x + "px";
  ddDiv.style.top = y + "px";
  ddDiv.style.visibility = (ddDiv.style.visibility == "visible" ? "hidden" : "visible");
  ddDiv.style.display = (ddDiv.style.display == "block" ? "none" : "block");
  ddDiv.style.zIndex = 10000;
 
  // draw the dropdown table
  refreshDropDown(targetField.name, fieldValue);
}


/**
This is the function that actually draws the datepicker calendar.
*/
function refreshDropDown(targetFieldName, fieldValue)
{
  // the selection box will be drawn as a table
  // you can customize the table elements with a global CSS style sheet,
  // or by hardcoding style and formatting elements below
  var targetField = document.getElementsByName (targetFieldName).item(0);
  var width = targetField.offsetWidth;
  var crlf = "\r\n";
  var TABLE = "<table class='ddTable' cellspacing='0' cellpadding='1'>" + crlf;
  var xTABLE = "</table>" + crlf;
  var TR = "<tr class='ddTR'>";
  var xTR = "</tr>" + crlf;
  var TD = "<td class='ddTD' width='" + width +"' onMouseOut='this.className=\"ddTD\";' onMouseOver=' this.className=\"ddTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
  var TD_selected = "<td class='ddHighlightTD' onMouseOut='this.className=\"ddHighlightTD\";' onMouseOver='this.className=\"ddTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
  var xTD = "</td>" + crlf;
  
  var TD_onclick = "";
 
  // start generating the code for the calendar table
  var html = TABLE;
  
  for (i = 0; i < dditems.length; i++) {
	 TD_onclick = " onclick=\"ddupdateField('" + targetFieldName + "', '" + dditems[i] + "');\">"; 
	 html += TR;
	 if (dditems[i] == fieldValue) {
		 html += TD_selected + TD_onclick + dditems[i] + xTD;
	 } else {
	 	html += TD + TD_onclick + dditems[i] + xTD;
	 }
	 html += xTR;
  }
  
  // and finally, close the table
  html += xTABLE;
 
  document.getElementById(dropdownDivID).innerHTML = html;
  // add an "iFrame shim" to allow the datepicker to display above selection lists
  ddadjustiFrame();
}

/**
Update the field with the given dateFieldName with the dateString that has been passed,
and hide the datepicker. If no dateString is passed, just close the datepicker without
changing the field value.

Also, if the page developer has defined a function called datePickerClosed anywhere on
the page or in an imported library, we will attempt to run that function with the updated
field as a parameter. This can be used for such things as date validation, setting default
values for related fields, etc. For example, you might have a function like this to validate
a start date field:

function datePickerClosed(dateField)
{
  var dateObj = getFieldDate(dateField.value);
  var today = new Date();
  today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
 
  if (dateField.name == "StartDate") {
    if (dateObj < today) {
      // if the date is before today, alert the user and display the datepicker again
      alert("Please enter a date that is today or later");
      dateField.value = "";
      document.getElementById(datePickerDivID).style.visibility = "visible";
      adjustiFrame();
    } else {
      // if the date is okay, set the EndDate field to 7 days after the StartDate
      dateObj.setTime(dateObj.getTime() + (7 * 24 * 60 * 60 * 1000));
      var endDateField = document.getElementsByName ("EndDate").item(0);
      endDateField.value = getDateString(dateObj);
    }
  }
}

*/
function ddupdateField(targetFieldName, selectedString)
{
  var targetField = document.getElementsByName (targetFieldName).item(0);
  if (selectedString)
    targetField.value = selectedString;
 
  var dropdownDiv = document.getElementById(dropdownDivID);
  dropdownDiv.style.visibility = "hidden";
  dropdownDiv.style.display = "none";
 
  ddadjustiFrame();
  targetField.focus();
 
  // after the datepicker has closed, optionally run a user-defined function called
  // datePickerClosed, passing the field that was just updated as a parameter
  // (note that this will only run if the user actually selected a date from the datepicker)
  if ((selectedString) && (typeof(dropdownClosed) == "function"))
    dropdownClosed(targetField);
}

/**
Use an "iFrame shim" to deal with problems where the datepicker shows up behind
selection list elements, if they're below the datepicker. The problem and solution are
described at:

http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
*/
function ddadjustiFrame(dropdownDiv, iFrameDiv)
{
  // we know that Opera doesn't like something about this, so if we
  // think we're using Opera, don't even try
  var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
  if (is_opera)
    return;
  
  // put a try/catch block around the whole thing, just in case
  try {
    if (!document.getElementById(ddiFrameDivID)) {
      // don't use innerHTML to update the body, because it can cause global variables
      // that are currently pointing to objects on the page to have bad references
      //document.body.innerHTML += "<iframe id='" + iFrameDivID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
      var newNode = document.createElement("iFrame");
      newNode.setAttribute("id", ddiFrameDivID);
      newNode.setAttribute("src", "javascript:false;");
      newNode.setAttribute("scrolling", "no");
      newNode.setAttribute ("frameborder", "0");
      document.body.appendChild(newNode);
    }
    
    if (!dropdownDiv)
      dropdownDiv = document.getElementById(dropdownDivID);
    if (!iFrameDiv)
      iFrameDiv = document.getElementById(ddiFrameDivID);
    
    try {
      iFrameDiv.style.position = "absolute";
      iFrameDiv.style.width = dropdownDiv.offsetWidth;
      iFrameDiv.style.height = dropdownDiv.offsetHeight ;
      iFrameDiv.style.top = dropdownDiv.style.top;
      iFrameDiv.style.left = dropdownDiv.style.left;
      iFrameDiv.style.zIndex = dropdownDiv.style.zIndex - 1;
      iFrameDiv.style.visibility = dropdownDiv.style.visibility ;
      iFrameDiv.style.display = dropdownDiv.style.display;
    } catch(e) {
    }
 
  } catch (ee) {
  }
 
}
