
// Attach load event
xAttachEventOnLoad("load", cornerInt);

function cornerInt(){
  // Get all DIV elements
  var divElements = document.getElementsByTagName("DIV");
  var divCount = divElements.length;
  var t = 0;
  objsArray = new Array();

  // Loop through all DIVs
  for(var i = 0; i < divCount; i++){
    var currentDiv = divElements[i];
    if(radius = currentDiv.getAttribute("radius")){
      if(!(corners = currentDiv.getAttribute("corners")))
        corners = "TR,TL,BR,BL";
      var newObj = new curvyCorners(currentDiv, radius, corners);
      objsArray[t] = newObj;
      t++;
    }
  }

  //alert(objsArray);
  var arrayLength = objsArray.length;
  for(var x = 0; x < arrayLength; x++){
      objsArray[x].doCorners();
  }
}

// ------------- curvyCorners OBJECT

function curvyCorners(boxObj, radiusInt, cornersCSVStr) {
  // Set objects properties
  this.box      = boxObj;
  this.radius   = (Math.round(parseInt(radiusInt)/2))*2;
  this.corners  = cornersCSVStr.split(","); // Array

  // Setup global variables
  this.masterCorner     = null;
  this.masterCornerType = null;
  this.cornersAvailable = new Array("TR", "TL", "BR", "BL");

  // Get box formatting details and set properties
  this.boxHeight    = parseInt(((this.box.style.height != "")? this.box.style.height.substring(0, this.box.style.height.indexOf("px")) : this.box.scrollHeight));
  this.boxWidth     = parseInt(((this.box.style.width != "")? this.box.style.width.substring(0, this.box.style.width.indexOf("px")) : this.box.scrollWidth));
  this.borderWidth  = parseInt(((this.box.style.borderWidth != "")? this.box.style.borderWidth.slice(0, this.box.style.borderWidth.indexOf("px")) : 0));
  this.boxColour    = ((this.box.style.backgroundColor.substr(0, 3) == "rgb")? rgb2Hex(this.box.style.backgroundColor) : this.box.style.backgroundColor);
  this.borderColour = ((this.box.style.borderColor != "" && this.borderWidth > 0)? ((this.box.style.borderColor.substr(0, 3) == "rgb")? rgb2Hex(this.box.style.borderColor) : this.box.style.borderColor) : this.boxColour);
  this.borderRadius = parseInt(this.radius - this.borderWidth);
  this.borderString = this.borderWidth + "px" + " solid " + this.borderColour;
  this.posStart = (0 - this.radius);
  this.posAdjust = (Math.floor(Math.sqrt(Math.pow((this.radius - this.borderWidth),2)/2)));

  // Make box relative if not already absolute
  if(this.box.style.position != "absolute") this.box.style.position = "relative";

  // Builds the corners
  this.doCorners = function()
  {
    // Loop once for each corner specfied
    for(var i in this.cornersAvailable)
    {
      // Get current corner type from array
      var currentCorner = this.cornersAvailable[i];

      if(inArray(this.corners, currentCorner) === false)
      {
        // No corner is to be rounded so we generated a square to fill the gap
        var newCorner = document.createElement("DIV");

        // Setup corners properties
        newCorner.style.height   = this.radius - this.borderWidth + "px";
        newCorner.style.width    = this.radius - this.borderWidth + "px";
        newCorner.style.position = "absolute";
        newCorner.style.fontSize = "1px";
        newCorner.style.overflow = "hidden";
        newCorner.style.backgroundColor = this.boxColour;

        switch(currentCorner)
        {
          case "TL":
            newCorner.style.borderLeft = this.borderString;
            newCorner.style.borderTop  = this.borderString;
            break;
          case "TR":
            newCorner.style.borderRight = this.borderString;
            newCorner.style.borderTop   = this.borderString;
            break;
          case "BL":
            newCorner.style.borderLeft   = this.borderString;
            newCorner.style.borderBottom = this.borderString;
            break;
          case "BR":
            newCorner.style.borderRight  = this.borderString;
            newCorner.style.borderBottom = this.borderString;
            break;
        }
      }
      else{
        /*
        To increase performace we only ever generate one corner.
        This corner is the bottom right corner. If more that one corner is requested
        then the other corners will be a cloned version of the first corner.
        */
        if(this.masterCorner != null)
        {
          // Create clone of the master corner
          var newCorner = this.masterCorner.cloneNode(true);
        }
        else
        {
          // First time round so generate the master corner
          var newCorner = document.createElement("DIV");

          // Setup corners properties
          newCorner.style.height   = this.radius + "px";
          newCorner.style.width    = this.radius + "px";
          newCorner.style.position = "absolute";
          newCorner.style.fontSize = "1px";
          newCorner.style.overflow = "hidden";

          // Cycle the x-axis
          for(var intx = 0; intx < this.radius; intx++)
          {
            // Calculate the value of y1 which identifies the pixels inside the border
            if((intx +1) >= this.borderRadius)
              var y1 = -1;
            else
              var y1 = (Math.floor(Math.sqrt(Math.pow(this.borderRadius, 2) - Math.pow((intx+1), 2))) - 1);

            // Only calculate y2 and y3 if there is a border defined
            if(this.borderRadius != this.radius)
            {
               if((intx) >= this.borderRadius)
                 var y2 = -1;
               else
                 var y2 = Math.ceil(Math.sqrt(Math.pow(this.borderRadius,2) - Math.pow(intx, 2)));
               if((intx+1) >= this.radius)
                 var y3 = -1;
               else
                 var y3 = (Math.floor(Math.sqrt(Math.pow(this.radius,2) - Math.pow((intx+1), 2))) - 1);
            }

            // Calculate y4
            if((intx) >= this.radius)
              var y4 = -1;
            else
              var y4 = Math.ceil(Math.sqrt(Math.pow(this.radius,2) - Math.pow(intx, 2)));

            // Draw bar on inside of the border with foreground colour
            if(y1 > -1) this.drawPixel(intx, 0, this.boxColour, 100, (y1+1), newCorner);

            // Only draw border/foreground antialiased pixels and border if there is a border defined
            if(this.borderRadius != this.radius)
            {
                // Cycle the y-axis
                for(var inty = (y1 + 1); inty < y2; inty++)
                {
                  // For each of the pixels that need anti aliasing between the foreground and border colour draw single pixel divs
                  var pixelcolour = BlendColour(this.boxColour, this.borderColour, pixelFraction(intx, inty, this.borderRadius));
                  this.drawPixel(intx, inty, pixelcolour, 100, 1, newCorner);
                }

                // Draw bar for the border
                if(y3 >= y2)
                {
                  if (y1 == -1)
                    y1 = 0;
                  this.drawPixel(intx, y2, this.borderColour, 100, (y3 - y2 + 1), newCorner);
                }

                // Set the colour for the outside curve
                var outsideColour = this.borderColour;
              }
              else
              {
                // Set the coour for the outside curve
                var outsideColour = this.boxColour;
                var y3 = y1;
              }

              // Cycle the y-axis and draw the anti aliased pixels on the outside of the curve
              for(var inty = (y3 + 1); inty < y4; inty++)
              {
                // For each of the pixels that need anti aliasing between the foreground/border colour & background draw single pixel divs
                this.drawPixel(intx, inty, outsideColour, (pixelFraction(intx, inty ,this.radius) * 100), 1, newCorner);
              }
            }
            // Store corner as master corner
            this.masterCorner = newCorner.cloneNode(true);
            this.masterCornerType = "BR";
        }
      }

      /*
      Now we have a new corner we need to reposition all the pixels unless
      the current corner is the bottom right/
      */
      if(currentCorner != "BR")
      {
        // Loop through all children (pixel bars)
        var pixelCount = newCorner.childNodes.length;
        for(var t = 0; t < pixelCount; t++)
        {
          // Get current pixel bar
          var pixelBar = newCorner.childNodes[t];

          // Get current top and left properties
          var pixelBarTop    = parseInt(pixelBar.style.top.substring(0, pixelBar.style.top.indexOf("px")));
          var pixelBarLeft   = parseInt(pixelBar.style.left.substring(0, pixelBar.style.left.indexOf("px")));
          var pixelBarHeight = parseInt(pixelBar.style.height.substring(0, pixelBar.style.height.indexOf("px")));

          // Reposition pixels
          if(currentCorner == "TL" || currentCorner == "BL"){
              pixelBar.style.left = this.radius -pixelBarLeft -1 + "px"; // Left
          }
          if(currentCorner == "TR" || currentCorner == "TL"){
              pixelBar.style.top =  this.radius -pixelBarHeight -pixelBarTop + "px"; // Top
          }
        }
      }

      // Position the container
      switch(currentCorner)
      {
        case "TL":
          newCorner.style.top  = (this.posStart + this.posAdjust) + "px";
          newCorner.style.left = (this.posStart + this.posAdjust) + "px";
          break;

        case "TR":
          newCorner.style.top  = (this.posStart + this.posAdjust) + "px";
          newCorner.style.left = (this.boxWidth - this.posAdjust) + "px";
          break;

        case "BL":
          newCorner.style.top = (this.boxHeight - this.posAdjust) + "px";
          newCorner.style.left = (this.posStart + this.posAdjust) + "px";
          break;

        case "BR":
          newCorner.style.top = (this.boxHeight - this.posAdjust)+ "px";
          newCorner.style.left = (this.boxWidth - this.posAdjust)+ "px";
          break;
      }

      // Append new corner
      this.box.appendChild(newCorner);

      /*
      We have now drawn all the corner required so we now need to
      put on the finishing touches.
      */

      // Draw bars ----------------------------------------------

      // Turn off current borders
      this.box.style.borderWidth = "0px";

      for(var s = 0; s < 4; s++)
      {
        // Create bar
        var bar = document.createElement("DIV");

        // Set the bars properties
        bar.style.height   = this.boxHeight + "px";
        bar.style.width    = this.boxWidth + "px";
        bar.style.position = "absolute";
        bar.style.fontSize = "1px";
        bar.style.overflow = "hidden";
        bar.style.backgroundColor = this.boxColour;
        //bar.style.backgroundColor = "#663322";

        switch(s)
        {
            // Left
            case 0:
              bar.style.top = this.posAdjust + "px";
              bar.style.left = (this.posStart + this.posAdjust) + "px";
              bar.style.width = (this.radius - this.posAdjust - this.borderWidth) + "px";
              bar.style.height = (this.boxHeight - (2 * this.posAdjust)) + "px";
              bar.style.borderLeft = this.borderString;
              break;
            // Right
            case 1:
              bar.style.top = this.posAdjust + "px";
              bar.style.right = (this.posStart + this.posAdjust) + "px";
              bar.style.width = (this.radius - this.posAdjust - this.borderWidth) + "px";
              bar.style.height = (this.boxHeight - (2 * this.posAdjust)) + "px";
              bar.style.borderRight = this.borderString;
              break;
            // Top
            case 2:
              bar.style.top = (this.posStart + this.posAdjust) + "px";
              bar.style.left = this.posAdjust + "px";
              bar.style.height = (this.radius - this.posAdjust - this.borderWidth) + "px";
              bar.style.width = (this.boxWidth - (2 * this.posAdjust)) + "px";
              bar.style.borderTop = this.borderString;
              break;
            // Bottom
            case 3:
              bar.style.bottom = (this.posStart + this.posAdjust) + "px";
              bar.style.left = this.posAdjust + "px";
              bar.style.height = (this.radius - this.posAdjust - this.borderWidth) + "px";
              bar.style.width = (this.boxWidth - (2 * this.posAdjust)) + "px";
              bar.style.borderBottom = this.borderString;
              break;
        }
        // Append bar
          this.box.appendChild(bar);
      }
    }
  }

  this.drawPixel = function(intx, inty, colour, transAmount, height, newCorner) {
    // Create pixel
    var pixel = document.createElement("DIV");
    pixel.style.height   = height + "px";
    pixel.style.width    = "1px";
    pixel.style.position = "absolute";
    pixel.style.fontSize = "1px";
    pixel.style.overflow = "hidden";
    pixel.style.backgroundColor = colour;

    // Set opacity if the transparency is anything other than 100
    if (transAmount != 100)
      setOpacity(pixel, transAmount);

    // Set the pixels position
    pixel.style.top = inty + "px";
    pixel.style.left = intx + "px";
    newCorner.appendChild(pixel);
  }
}

// ------------- UTILITY FUNCTIONS

/*
Blends the two colours by the fraction
returns the resulting colour as a string in the format "#FFFFFF"
*/
function BlendColour(Col1, Col2, Col1Fraction) {
  var red1 = parseInt(Col1.substr(1,2),16);
  var green1 = parseInt(Col1.substr(3,2),16);
  var blue1 = parseInt(Col1.substr(5,2),16);
  var red2 = parseInt(Col2.substr(1,2),16);
  var green2 = parseInt(Col2.substr(3,2),16);
  var blue2 = parseInt(Col2.substr(5,2),16);

  if(Col1Fraction > 1 || Col1Fraction < 0) Col1Fraction = 1;

  var endRed = Math.round((red1 * Col1Fraction) + (red2 * (1 - Col1Fraction)));
  if(endRed > 255) endRed = 255;
  if(endRed < 0) endRed = 0;

  var endGreen = Math.round((green1 * Col1Fraction) + (green2 * (1 - Col1Fraction)));
  if(endGreen > 255) endGreen = 255;
  if(endGreen < 0) endGreen = 0;

  var endBlue = Math.round((blue1 * Col1Fraction) + (blue2 * (1 - Col1Fraction)));
  if(endBlue > 255) endBlue = 255;
  if(endBlue < 0) endBlue = 0;

  return "#" + IntToHex(endRed)+ IntToHex(endGreen)+ IntToHex(endBlue);
}

/*
Converts a number to hexadecimal format
*/
function IntToHex(strNum) {
  base = strNum / 16;
  rem = strNum % 16;
  base = base - (rem / 16);
  baseS = MakeHex(base);
  remS = MakeHex(rem);
  return baseS + '' + remS;
}


/*
gets the hex bits of a number
*/
function MakeHex(x) {
  if((x >= 0) && (x <= 9))
  {
    return x;
  }
  else
  {
    switch(x)
    {
      case 10: return "A";
      case 11: return "B";
      case 12: return "C";
      case 13: return "D";
      case 14: return "E";
      case 15: return "F";
    }
  }
}


/*
For a pixel cut by the line determines the fraction of the pixel on the 'inside' of the
line.  Returns a number between 0 and 1
*/
function pixelFraction(x, y, r) {
  var pixelfraction = 0;

  /*
  determine the co-ordinates of the two points on the perimeter of the pixel that the
  circle crosses
  */
  var xvalues = new Array(1);
  var yvalues = new Array(1);
  var point = 0;
  var whatsides = "";

  // x + 0 = Left
  var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x,2)));

  if ((intersect >= y) && (intersect < (y+1))) {
    whatsides = "Left";
    xvalues[point] = 0;
    yvalues[point] = intersect - y;
    point =  point + 1;
  }
  // y + 1 = Top
  var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y+1,2)));

  if ((intersect >= x) && (intersect < (x+1))) {
    whatsides = whatsides + "Top";
    xvalues[point] = intersect - x;
    yvalues[point] = 1;
    point = point + 1;
  }
  // x + 1 = Right
  var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x+1,2)));

  if ((intersect >= y) && (intersect < (y+1))) {
    whatsides = whatsides + "Right";
    xvalues[point] = 1;
    yvalues[point] = intersect - y;
    point =  point + 1;
  }
  // y + 0 = Bottom
  var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y,2)));

  if ((intersect >= x) && (intersect < (x+1))) {
    whatsides = whatsides + "Bottom";
    xvalues[point] = intersect - x;
    yvalues[point] = 0;
  }

  /*
  depending on which sides of the perimeter of the pixel the circle crosses calculate the
  fraction of the pixel inside the circle
  */
  switch (whatsides) {
    case "LeftRight":
    pixelfraction = Math.min(yvalues[0],yvalues[1]) + ((Math.max(yvalues[0],yvalues[1]) - Math.min(yvalues[0],yvalues[1]))/2);
    break;
    case "TopRight":
    pixelfraction = 1-(((1-xvalues[0])*(1-yvalues[1]))/2);
    break;
    case "TopBottom":
    pixelfraction = Math.min(xvalues[0],xvalues[1]) + ((Math.max(xvalues[0],xvalues[1]) - Math.min(xvalues[0],xvalues[1]))/2);
    break;
    case "LeftBottom":
    pixelfraction = (yvalues[0]*xvalues[1])/2;
    break;
    default:
    pixelfraction = 1;
  }

  return pixelfraction;
}

// This function converts CSS rgb(x, x, x) to hexadecimal
function rgb2Hex(rgbColour) {
  try{
    // Remove rgb()
    var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")"));

    // Split RGB into array
    var rgbArray = rgbValues.split(", ");

    // Get RGB values
    var red   = parseInt(rgbArray[0]);
    var green = parseInt(rgbArray[1]);
    var blue  = parseInt(rgbArray[2]);

    // Build hex colour code
    var hexColour = "#" + IntToHex(red) + IntToHex(green) + IntToHex(blue);
  }
  catch(e){
    alert("There was an error converting the RGB value to Hexadecimal in function rgb2Hex");
  }

  return hexColour;
}

// Function by Simon Willison from sitepoint.com
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

/*
Returns index if the passed value is found in the
array otherwise returns false.
*/
function inArray(array, value) {
  for(var i = 0; i < array.length; i++){

    // Matches identical (===), not just similar (==).
    if (array[i] === value) return i;
  }
  return false;
}

/*
Returns true if the passed value is found as a key
in the array otherwise returns false.
*/
function inArrayKey(array, value) {
  for(key in array){
    // Matches identical (===), not just similar (==).
    if(key === value) return true;
  }
  return false;
}

// Attaches onload event cross browser
function xAttachEventOnLoad(event, func) {
 // Attach event
  if(window.addEventListener)
    window.addEventListener(event, func, false);
  else if(window.attachEvent)
    window.attachEvent("on" + event, func);
  else
    window.onload = func;
}
// JavaScript Document



var datePickerDivID = "datepicker";
var iFrameDivID = "datepickeriframe";

var dayArrayShort = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
var dayArrayMed = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var dayArrayLong = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var monthArrayShort = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var monthArrayMed = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
var monthArrayLong = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
 
var defaultDateSeparator = "-";       
var defaultDateFormat = "ymd"   
var dateSeparator = defaultDateSeparator;
var dateFormat = defaultDateFormat;


function displayDatePicker(dateFieldName, displayBelowThisObject, dtFormat, dtSep)
{
  var targetDateField = document.getElementsByName (dateFieldName).item(0);
  if (!displayBelowThisObject)
    displayBelowThisObject = targetDateField;
 
  // if a date separator character was given, update the dateSeparator variable
  if (dtSep)
    dateSeparator = dtSep;
  else
    dateSeparator = defaultDateSeparator;
 
  // if a date format was given, update the dateFormat variable
  if (dtFormat)
    dateFormat = dtFormat;
  else
    dateFormat = defaultDateFormat;
 
  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 ;
  }
 
  drawDatePicker(targetDateField, x, y);
}



function drawDatePicker(targetDateField, x, y)
{
  var dt = getFieldDate(targetDateField.value );
 
 
  if (!document.getElementById(datePickerDivID)) {
  
    var newNode = document.createElement("div");
    newNode.setAttribute("id", datePickerDivID);
    newNode.setAttribute("class", "dpDiv");
    newNode.setAttribute("style", "visibility: hidden;");
    document.body.appendChild(newNode);
  }
 
  // move the datepicker div to the proper x,y coordinate and toggle the visiblity
  var pickerDiv = document.getElementById(datePickerDivID);
  pickerDiv.style.position = "absolute";
  pickerDiv.style.left = x + "px";
  pickerDiv.style.top = y + "px";
  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv.style.display = (pickerDiv.style.display == "block" ? "none" : "block");
  pickerDiv.style.zIndex = 10000;
 
  // draw the datepicker table
  refreshDatePicker(targetDateField.name, dt.getFullYear(), dt.getMonth(), dt.getDate());
}


/**
This is the function that actually draws the datepicker calendar.
*/
function refreshDatePicker(dateFieldName, year, month, day)
{
  // if no arguments are passed, use today's date; otherwise, month and year
  // are required (if a day is passed, it will be highlighted later)
  var thisDay = new Date();
 
  if ((month >= 0) && (year > 0)) {
    thisDay = new Date(year, month, 1);
  } else {
    day = thisDay.getDate();
    thisDay.setDate(1);
  }
 
  // the calendar 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 crlf = "\r\n";
  var TABLE = "<table cols=7 class='dpTable'>" + crlf;
  var xTABLE = "</table>" + crlf;
  var TR = "<tr class='dpTR'>";
  var TR_title = "<tr class='dpTitleTR'>";
  var TR_days = "<tr class='dpDayTR'>";
  var TR_todaybutton = "<tr class='dpTodayButtonTR'>";
  var xTR = "</tr>" + crlf;
  var TD = "<td class='dpTD' onMouseOut='this.className=\"dpTD\";' onMouseOver=' this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
  var TD_title = "<td colspan=5 class='dpTitleTD'>";
  var TD_buttons = "<td class='dpButtonTD'>";
  var TD_todaybutton = "<td colspan=7 class='dpTodayButtonTD'>";
  var TD_days = "<td class='dpDayTD'>";
  var TD_selected = "<td class='dpDayHighlightTD' onMouseOut='this.className=\"dpDayHighlightTD\";' onMouseOver='this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
  var xTD = "</td>" + crlf;
  var DIV_title = "<div class='dpTitleText'>";
  var DIV_selected = "<div class='dpDayHighlight'>";
  var xDIV = "</div>";
 
  // start generating the code for the calendar table
  var html = TABLE;
 
  // this is the title bar, which displays the month and the buttons to
  // go back to a previous month or forward to the next month
  html += TR_title;
  html += TD_buttons + getButtonCode(dateFieldName, thisDay, -1, "<") + xTD;
  html += TD_title + DIV_title + monthArrayLong[ thisDay.getMonth()] + " " + thisDay.getFullYear() + xDIV + xTD;
  html += TD_buttons + getButtonCode(dateFieldName, thisDay, 1, ">") + xTD;
  html += xTR;
 
  // this is the row that indicates which day of the week we're on
  html += TR_days;
  for(i = 0; i < dayArrayShort.length; i++)
    html += TD_days + dayArrayShort[i] + xTD;
  html += xTR;
 
  // now we'll start populating the table with days of the month
  html += TR;
 
  // first, the leading blanks
  for (i = 0; i < thisDay.getDay(); i++)
    html += TD + " " + xTD;
 
  // now, the days of the month
  do {
    dayNum = thisDay.getDate();
    TD_onclick = " onclick=\"updateDateField('" + dateFieldName + "', '" + getDateString(thisDay) + "');\">";
    
    if (dayNum == day)
      html += TD_selected + TD_onclick + DIV_selected + dayNum + xDIV + xTD;
    else
      html += TD + TD_onclick + dayNum + xTD;
    
    // if this is a Saturday, start a new row
    if (thisDay.getDay() == 6)
      html += xTR + TR;
    
    // increment the day
    thisDay.setDate(thisDay.getDate() + 1);
  } while (thisDay.getDate() > 1)
 
  // fill in any trailing blanks
  if (thisDay.getDay() > 0) {
    for (i = 6; i > thisDay.getDay(); i--)
      html += TD + " " + xTD;
  }
  html += xTR;
 
  // add a button to allow the user to easily return to today, or close the calendar
  var today = new Date();
  var todayString = "Today is " + dayArrayMed[today.getDay()] + ", " + monthArrayMed[ today.getMonth()] + " " + today.getDate();
  html += TR_todaybutton + TD_todaybutton;
  html += "<button class='dpTodayButton' onClick='refreshDatePicker(\"" + dateFieldName + "\");'>this month</button> ";
  html += "<button class='dpTodayButton' onClick='updateDateField(\"" + dateFieldName + "\");'>close</button>";
  html += xTD + xTR;
 
  // and finally, close the table
  html += xTABLE;
 
  document.getElementById(datePickerDivID).innerHTML = html;
  // add an "iFrame shim" to allow the datepicker to display above selection lists
  adjustiFrame();
}


/**
Convenience function for writing the code for the buttons that bring us back or forward
a month.
*/
function getButtonCode(dateFieldName, dateVal, adjust, label)
{
  var newMonth = (dateVal.getMonth () + adjust) % 12;
  var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);
  if (newMonth < 0) {
    newMonth += 12;
    newYear += -1;
  }
 
  return "<button class='dpButton' onClick='refreshDatePicker(\"" + dateFieldName + "\", " + newYear + ", " + newMonth + ");'>" + label + "</button>";
}


/**
Convert a JavaScript Date object to a string, based on the dateFormat and dateSeparator
variables at the beginning of this script library.
*/
function getDateString(dateVal)
{
  var dayString = "00" + dateVal.getDate();
  var monthString = "00" + (dateVal.getMonth()+1);
  dayString = dayString.substring(dayString.length - 2);
  monthString = monthString.substring(monthString.length - 2);
 
  switch (dateFormat) {
    case "dmy" :
      return dayString + dateSeparator + monthString + dateSeparator + dateVal.getFullYear();
    case "ymd" :
      return dateVal.getFullYear() + dateSeparator + monthString + dateSeparator + dayString;
    case "mdy" :
    default :
      return monthString + dateSeparator + dayString + dateSeparator + dateVal.getFullYear();
  }
}


/**
Convert a string to a JavaScript Date object.
*/
function getFieldDate(dateString)
{
  var dateVal;
  var dArray;
  var d, m, y;
 
  try {
    dArray = splitDateString(dateString);
    if (dArray) {
      switch (dateFormat) {
        case "dmy" :
          d = parseInt(dArray[0], 10);
          m = parseInt(dArray[1], 10) - 1;
          y = parseInt(dArray[2], 10);
          break;
        case "ymd" :
          d = parseInt(dArray[2], 10);
          m = parseInt(dArray[1], 10) - 1;
          y = parseInt(dArray[0], 10);
          break;
        case "mdy" :
        default :
          d = parseInt(dArray[1], 10);
          m = parseInt(dArray[0], 10) - 1;
          y = parseInt(dArray[2], 10);
          break;
      }
      dateVal = new Date(y, m, d);
    } else if (dateString) {
      dateVal = new Date(dateString);
    } else {
      dateVal = new Date();
    }
  } catch(e) {
    dateVal = new Date();
  }
 
  return dateVal;
}


/**
Try to split a date string into an array of elements, using common date separators.
If the date is split, an array is returned; otherwise, we just return false.
*/
function splitDateString(dateString)
{
  var dArray;
  if (dateString.indexOf("/") >= 0)
    dArray = dateString.split("/");
  else if (dateString.indexOf(".") >= 0)
    dArray = dateString.split(".");
  else if (dateString.indexOf("-") >= 0)
    dArray = dateString.split("-");
  else if (dateString.indexOf("\\") >= 0)
    dArray = dateString.split("\\");
  else
    dArray = false;
 
  return dArray;
}


function updateDateField(dateFieldName, dateString)
{
  var targetDateField = document.getElementsByName (dateFieldName).item(0);
  if (dateString)
    targetDateField.value = dateString;
 
  var pickerDiv = document.getElementById(datePickerDivID);
  pickerDiv.style.visibility = "hidden";
  pickerDiv.style.display = "none";
 
  adjustiFrame();
  targetDateField.focus();
 
 
  if ((dateString) && (typeof(datePickerClosed) == "function"))
    datePickerClosed(targetDateField);
}



function adjustiFrame(pickerDiv, 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(iFrameDivID)) {
   
      var newNode = document.createElement("iFrame");
      newNode.setAttribute("id", iFrameDivID);
      newNode.setAttribute("src", "javascript:false;");
      newNode.setAttribute("scrolling", "no");
      newNode.setAttribute ("frameborder", "0");
      document.body.appendChild(newNode);
    }
    
    if (!pickerDiv)
      pickerDiv = document.getElementById(datePickerDivID);
    if (!iFrameDiv)
      iFrameDiv = document.getElementById(iFrameDivID);
    
    try {
      iFrameDiv.style.position = "absolute";
      iFrameDiv.style.width = pickerDiv.offsetWidth;
      iFrameDiv.style.height = pickerDiv.offsetHeight ;
      iFrameDiv.style.top = pickerDiv.style.top;
      iFrameDiv.style.left = pickerDiv.style.left;
      iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
      iFrameDiv.style.visibility = pickerDiv.style.visibility ;
      iFrameDiv.style.display = pickerDiv.style.display;
    } catch(e) {
    }
 
  } catch (ee) {
  }
 
}
/****POPUP MENU**/

  /***********************************************
* Pop-it menu- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var defaultMenuWidth="150px" //set default menu width.

var linkset=new Array()
//SPECIFY MENU SETS AND THEIR LINKS. FOLLOW SYNTAX LAID OUT

linkset[0]='<a href="product_add_to_cart.php">Order Entry</a>'
linkset[0]+='<a href="product_order.php">Order Summary</a>'
linkset[0]+='<a href="invoice_list.php">Invoice Status</a>'
linkset[0]+='<a href="product_po_hist.php">Purchase History</a>'

linkset[1]='<a href="member_profile.php">Member Profile</a>'
linkset[1]+='<a href="member_chk_pass.php">Change Password</a>'
//linkset[0]+='<hr>' //Optional Separator
linkset[1]+='<a href="include/logout.php">Logout</a>'

////No need to edit beyond here

var ie5=document.all && !window.opera
var ns6=document.getElementById

if (ie5||ns6)
document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')

function iecompattest(){
return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

function showmenu(e, which, optWidth){
if (!document.all&&!document.getElementById)
return
clearhidemenu()
menuobj=ie5? document.all.popitmenu : document.getElementById("popitmenu")
menuobj.innerHTML=which
menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth
menuobj.contentwidth=menuobj.offsetWidth
menuobj.contentheight=menuobj.offsetHeight
eventX=ie5? event.clientX : e.clientX
eventY=ie5? event.clientY : e.clientY
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px"
//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
else
menuobj.style.top=ie5? iecompattest().scrollTop+event.clientY+"px" : window.pageYOffset+eventY+"px"
menuobj.style.visibility="visible"
return false
}

function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function hidemenu(){
if (window.menuobj)
menuobj.style.visibility="hidden"
}

function dynamichide(e){
if (ie5&&!menuobj.contains(e.toElement))
hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
hidemenu()
}

function delayhidemenu(){
delayhide=setTimeout("hidemenu()",1000)
}

function clearhidemenu(){
if (window.delayhide)
clearTimeout(delayhide)
}

if (ie5||ns6)
document.onclick=hidemenu
//*****************Loading*************************


function IsNumeric(sText,obj)   
{   
    var ValidChars = "0123456789.";   
   var IsNumber=true;   
   var Char;   
   for (i = 0; i < sText.length && IsNumber == true; i++)    
      {    
      Char = sText.charAt(i);    
      if (ValidChars.indexOf(Char) == -1)    
         {   
         IsNumber = false;   
         }   
     }   
        if(IsNumber==false){   
          //  alert("Only numberic value");   
            obj.value=sText.substr(0,sText.length-1);   
        }   
   }   

