var UNBG = "#ffffff"; //background colour for normal cells
var SELECTBG = "#fff0b4"; //background colour for selected cells
var TODAYFONT = "#000000"; //font colour for today

//some variables
var MONTHS = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var DAYS = new Array ("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
var DAYFIRST = false; //whether the day should come first or the month
var SELECTDAY=100, LASTDAY=30; //two global variables which are used later

//what to do when something is clicked, this should be overridden
function returndate (whichone, timems, daysshown, extras, wherefrom) {
  //it takes as arguments the calendar which was clicked, the time chosen,
  //the number of days being shown, any extra HTML appended to links, and
  //where it was called from (clicking on a day, month, or year)
}

//make the specified calendar visible
function showcalendar (whichone, initialdate, daysshown, thex, they, extras) {	
  setcalendar(whichone, 0, initialdate, daysshown, extras); 
  if (document.layers) {
	if (thex) {document.layers["calendar_" + whichone].left = thex;}
    if (they) {document.layers["calendar_" + whichone].top = they;}
	document.layers["calendar_" + whichone].hidden=false;
  }
  else if (document.getElementById) {document.getElementById("calendar_" + whichone).style.visibility = "visible";}
}

//hide the specified calendar
function hidecalendar (whichone) {
  if (document.layers) {document.layers["calendar_" + whichone].hidden=true;}
  else if (document.getElementById) {document.getElementById("calendar_" + whichone).style.visibility = "hidden";}
}

//fill the specified calendar with HTML
function setcalendar (whichone, monthoffset, initialdate, daysshown, extras) {
  var thetext = formcalendar (whichone, monthoffset, initialdate, daysshown, extras);
  if (document.layers) {var d=document.layers["calendar_" + whichone].document; d.open(); d.writeln (thetext); d.close();}
  else if (document.getElementById) {document.getElementById("calendar_" + whichone).innerHTML = thetext;}
  highlightdays (whichone, SELECTDAY, daysshown, LASTDAY);
}

//to account for Linux Netscape which says 2002 is 102
function makeyear (year) {return year < 500 ? (year + 1900) : year;} 
function props (o) {var r=""; for (var p in o) {r += p + ": " + o[p] + "\t";} alert (r);}
function initialdate (thedate) { //return either the current time right now or the input time
  var parts = thedate.split ("/");
  return parts.length == 3 ? new Date (parts[2], (DAYFIRST ? parts[1] : parts[0]) - 1, (DAYFIRST ? parts[0] : parts[1])).getTime() : 0;
}

//create the calendar for a certain month
function formcalendar (whichone, monthoffset, initialdate, daysshown, extras) {
  //start the function
  var todaydate = new Date(); //gets the day in the timezone of the user
  var startdate = initialdate ? new Date(initialdate) : new Date (); //initaldate can be any form of acceptable date
  initialdate = startdate.getTime(); //but it is only used by us in milliseconds
  var thedayselected = monthoffset == 0 ? startdate.getDate() : 100; //the date for the selected day, or an invalid one
  SELECTDAY = thedayselected;
  startdate.setMonth (startdate.getMonth() + monthoffset); //the startdate month incorporating the offset
  var thedaytoday = startdate.getMonth() == todaydate.getMonth() && startdate.getYear() == todaydate.getYear() ? todaydate.getDate() : 100; //the date for today if it's in this month
  startdate.setDate (1); //go back to the first day of the month
  startdate.setHours(1); //blank out the time components
  startdate.setMinutes(0);
  startdate.setSeconds(0);
  var returndate = new Date (startdate.toGMTString()); //the first day of the month in GMT
  var r = ""; //start the return string
  //the graphics on the table
//  r += "<table class=\"caltable\" border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
//  r += "<tr class=\"caltop\"><td><img src=\"images/calendar_popuptopleft.gif\" width=\"3\" height=\"3\" /></td>"; //the top left corner
//  r += "<td><img src=\"images/transparent.gif\" border=\"0\" width=\"3\" height=\"3\" /></td>"; //the strip across the  top
//  r += "<td><img src=\"images/calendar_popuptopright.gif\" width=\"3\" height=\"3\" /></td></tr>"; //the top right corner
//  r += "<tr><td class=\"caltop\"><img src=\"images/transparent.gif\" border=\"0\" width=\"3\" height=\"3\"></td><td>"; //the left side image
  //the data in the table
  r += "<table cellspacing=\"0\" border=\"0\" width=\"100%\">"; //start the return string
  r += "<tr align=\"center\" class=\"calhead\"><td><a class=\"calhead\" href=\"javascript:setcalendar('" + whichone + "'," + (monthoffset-1) + "," + initialdate + "," + daysshown + ",'" + extras + "')\">&lt;</a></td>";
  r += "<td colspan=\"5\">";
//  r += "<a class=\"calhead\" href=\"javascript:returndate('" + whichone + "'," + returndate.getTime() + "," + daysshown + ",'" + extras + "','month')\">";
  r += "<span class=\"calhead\">";
  r += MONTHS[startdate.getMonth()] + " " + makeyear(startdate.getYear());
//  r += "</a>";
r+= "</span>";
  r += "</td>";
  r += "<td><a class=\"calhead\" href=\"javascript:setcalendar('" + whichone + "'," + (monthoffset+1) + "," + initialdate + "," + daysshown + ",'" + extras + "')\">&gt;</a></td></tr><tr class=\"calhead\" align=\"center\">";
  for (var theday=0; theday<7; theday++) {r += "<td width=\"30\" class=\"calhead\">&nbsp;" + DAYS[theday] + "&nbsp;</td>";}
  r += "</tr><tr align=\"center\" class=\"caldays\">";
  var dayoffset = startdate.getDay(); //get the day of week of this first day of the month
  startdate.setMonth (startdate.getMonth()+1); //go forward a month
  startdate.setDate (0); //go back one day, now we are on the last day of the next month
  var lastday = startdate.getDate(); //get the last day of the month
  LASTDAY = lastday; //used later to highlight the correct calendar place
  if (dayoffset) {r += "<td colspan=\"" + dayoffset + "\" class=\"blank\"> </td>";}
  for (var theday=1; theday<=lastday; theday++) {
    returndate.setDate (theday);
    //display the day, with background highlighted if it is the selected day and we are in Netscape, in IE the dynamic highlighting works instead
    r += "<td id=\"day" + theday + "\" class=\"caldays\" bgcolor=\"" + (theday == thedayselected && document.layers ? SELECTBG : UNBG) + "\">";
//	((theday+dayoffset) % 7 > 1) ? r+= "<a class=\"caldays\" href=\"javascript:returndate('" + whichone + "'," + returndate.getTime() + "," + daysshown + ",'" + extras + "','day')\">" : r += "<span style=\"color: #bbb;\">";
//	r += (theday == thedaytoday) ? ("<font color=\"" + TODAYFONT + "\">" + theday + "</font>") : theday;
//	((theday+dayoffset) % 7 > 1) ? r += "</a>" : r += "</span>";
	((theday+dayoffset) % 7 > 1) ? r+= "<a class=\"caldays\" href=\"javascript:returndate('" + whichone + "'," + returndate.getTime() + "," + daysshown + ",'" + extras + "','day',0,0)\">" : r += "<a class=\"caldays\" href=\"javascript:returndate('" + whichone + "'," + returndate.getTime() + "," + daysshown + ",'" + extras + "','day',1," + (((theday+dayoffset) % 7 == 1) ? 0 : 1) + ")\">";
	r += (theday == thedaytoday) ? ("<font color=\"" + TODAYFONT + "\">" + theday + "</font>") : theday;
	((theday+dayoffset) % 7 > 1) ? r += "</a>" : r += "</a>";
	r += "</td>"; 
	if ((theday+dayoffset) % 7 == 0) {r += "</tr><tr align=\"center\">";}
  }
  if ((lastday + dayoffset) % 7 > 0) {r += "<td " + ((lastday + dayoffset) % 7 != 6 ? ("colspan=\"" + (7 - ((lastday + dayoffset) % 7)) + "\" ") : "") + "class=\"blank\"></td></tr><tr align=\"center\">";}
  //this table finishes with a space because IE5 on a Mac seems to need it to finish the table properly
  r += "<td colspan=\"7\" class=\"calhead\"><a class=\"calhead\" href=\"javascript:hidecalendar('" + whichone + "')\">close</a></td>\n</tr></table> "; //the close button and finishing the table
  //finishing the graphics on the table
//  r += "</td><td class=\"caltop\"><img src=\"images/transparent.gif\" border=\"0\" width=\"3\" height=\"3\"></td></tr>"; //the right side image
//  r += "<tr class=\"caltop\"><td><img src=\"images/calendar_popupbottomleft.gif\" width=\"3\" height=\"3\"></td>"; //the bottom left corner
//  r += "<td><img src=\"images/transparent.gif\" border=\"0\" width=\"3\" height=\"3\"></td>"; //the strip across the bottom
//  r += "<td><img src=\"images/calendar_popupbottomright.gif\" width=\"3\" height=\"3\"></td></tr></table> "; //the bottom right corner
  return r;
}

//highlight the days as a rollover
function highlightdays (whichone, startday, numdays, upto) {
  var colour;
  //500 or more are special values, and this only works on IE
  if (numdays < 500 && document.getElementById) { 
    for (var i=1; i<=upto; i++) { //loop through each of the days in this month
      colour = (i >= startday && i < (startday + numdays)) ?  SELECTBG : UNBG;
      if (document.layers) {var d=document.layers["calendar_" + whichone].document["day" + i].style.bgColor = colour;}
      else if (document.getElementById) {document.getElementById("day" + i).bgColor = colour;}
    } //highlight the appropriate days
  } //special values 500 for a whole month and 600 for a whole year
}

function terms() {
	var w = window.open("terms.cfm","terms","width=450, height=500, location=0, status=0, toolbars=0, scrollbars=1, scrolling=1");
	w.focus();
}

function card() {
	var w = window.open('card_verification.cfm','card','width=360,height=435');
	w.focus;
}

function shipday(f,extra,saturday) {
//	if(f.weekend.value!=extra) {
		f.checkdate.value=1;
		f.weekend.value=extra;
		f.whichday.value=saturday;
		f.submit();
//	} else {
//		return true;	
//	} 
}