function enable_email_save_button() {
  var button = document.getElementById("user_email_save_button");
  button.disabled=false;
}

function submit_zoom(file) {
  document.forms[0].zoom_file.value = file;
  submit_action_type('zoom');
}

function add_event(dest) {
  document.forms[0].addevent.value = "true";
  submit_action_type(dest);
}

function advance_next_page(next_page) {
  document.forms[0].next_page.value = next_page;
  submit_action_type("next_page");
}

function swap_image(dest_id, source_id) {
  var dest = document.getElementById(dest_id);
  var source = document.getElementById(source_id); 
  var hidden = document.getElementById("switch");

  hidden.value = source.src;  
  dest.src = source.src;
}


function incomplete_cal_warning() {
  alert("Your calendar is not complete.  Please upload an image for each month.");
  //submit_action_type('preview');
}

function advance_next_month(in_month, action_type) {
  document.forms[0].next_month.value = in_month;
  submit_action_type(action_type);
}


function start_new_cal() {
  var reply = confirm("You are about to lose all your data, continue?", "")
  if (reply) {
    submit_action_type('reset');
  } 
}

function cal_size_about() {
  var style = document.getElementById("cal_size_about").style;
  if (style.display == "block") {
      style.display = "";
      return;
  }
  style.display = "block";
  activate_mister_hidey();
}

function save_my_work() {
  var style = document.getElementById("email_div_id").style;
  
  if (style.display == "block") {
      style.display = "";
      return;
  }
/*
  this doesn't have to be done in "DHTML", so don't waste time with it
  tbody = document.getElementById('email_table');
   //first, remove all rows
  while (tbody.rows.length > 0) {
      tbody.removeChild(tbody.firstChild);
  }

  tbody.setAttribute("width", "320");
  tbody.cellPadding = 0;
  tbody.cellSpacing = 0;
  tbody.setAttribute("border", 0);
//tbody.setAttribute("border", 1);

  //the top image thingie
  tr = tbody.insertRow(tbody.rows.length);
  td = tr.insertCell(tr.cells.length);
  img = document.createElement("img");
  img.style.width = 320;
  img.setAttribute("src", "images/email_table_top.jpg");
  td.appendChild(img);
  td.colSpan = 3;

 
//  tr = tbody.insertRow(tbody.rows.length);
//  td = tr.insertCell(tr.cells.length);
//  td.setAttribute("background", "images/left_fade.jpg");
//  td.setAttribute("width", 1);
//td.style.width = 1;
  tr = tbody.insertRow(tbody.rows.length);
  td = tr.insertCell(tr.cells.length);  
  td.setAttribute("align", "right");
  td.colSpan = 3;
  var closeAnchor = document.createElement("a");
  closeAnchor.setAttribute("href", "javascript:hide_all_div_stuff();");  
  var closeText = document.createTextNode("close");
  closeAnchor.appendChild(closeText);
  td.appendChild(closeAnchor);

  //the copy with instructions
  tr = tbody.insertRow(tbody.rows.length);
  td = tr.insertCell(tr.cells.length);
  td.setAttribute("class", "mainbold");
//td.style.class = "mainbold";
  td.colSpan = 3;
  var email_message = "If you want to continue work from another computer, enter your email address here: ";
  if (document.all) {
      td.innerHTML = email_message;
  } else {
      txt =document.createTextNode(email_message);
      td.appendChild(txt);
  }

  //text entry and button
  tr = tbody.insertRow(tbody.rows.length);
  td = tr.insertCell(tr.cells.length);

  td.colSpan = 2;


  //the bottom image thingie
  tr = tbody.insertRow(tbody.rows.length);
  td = tr.insertCell(tr.cells.length);
  img = document.createElement("img");
  img.style.width= 320;
  img.setAttribute("src", "images/email_table_bottom.jpg");
  td.appendChild(img);
  td.colSpan = 3;
*/
  style.display = "block";
  activate_mister_hidey();
}

function add_HTML_border(td, imageName, backgroundName, width, height) {
  img = document.createElement("img");
  img.setAttribute("src", imageName);
  img.setAttribute("width", width);
  img.setAttribute("height", height);
//img.setAttribute("border", 1);
  td.setAttribute("background", backgroundName); 
  td.appendChild(img);
}

//that is, populate a div with the month selection stuff
function populate_div(divName, monthSelectorName, tableId, inputName, fixedMonthName) {
    var monthName;
    if (fixedMonthName != null) {
      monthName = fixedMonthName;
    } else {
      monthName  = getSelectedMonth(monthSelectorName);
    }
//    alert('and so we go'+monthName);

    var tr, td;

    tbody = document.getElementById(tableId);
    //first, remove all rows
    while (tbody.rows.length > 0) {
      tbody.removeChild(tbody.firstChild);
    }

    tbody.setAttribute("width", "220");
    tbody.setAttribute("border", "1");
    tbody.setAttribute("rules", "all");
    tbody.setAttribute("frame", "1");
    tr = tbody.insertRow(tbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.colSpan = 7;
    td.setAttribute("align", "center");

    titletable = document.createElement("table");
    titletable.setAttribute("width", "210");
    titletablebody = document.createElement("tbody");

    ttr = titletable.insertRow(titletable.rows.length);
    ttd = ttr.insertCell(ttr.cells.length);

    ttd = ttr.insertCell(ttr.cells.length);
    ttd.setAttribute("align", "right");
    if (document.all) {
      ttd.innerHTML = monthName;
    } else {
      txt =document.createTextNode(monthName);
      ttd.appendChild(txt);
    }

    ttd = ttr.insertCell(ttr.cells.length);
    ttd.setAttribute("align", "right");

    var closeAnchor = document.createElement("a");
    closeAnchor.setAttribute("href", "javascript:toggleLayer('"+divName+"');");  //well, I don't know, it works
    var closeText = document.createTextNode("close");
    closeAnchor.appendChild(closeText);

//JPF for some reason this causes the client to throw out all images...taking away the magic of DHTML.  
//var closeAnchor = document.createElement("input");  
//closeAnchor.setAttribute("type", "image");
//closeAnchor.setAttribute("onclick","javascript:toggleLayer('"+divName+"');");
//closeAnchor.setAttribute("src", "images/small_close_x.JPG");

    ttd.appendChild(closeAnchor);

    titletable.appendChild(titletablebody);
    td.appendChild(titletable);

    //populate the days of the week
    tr = tbody.insertRow(tbody.rows.length);
    var dayNames = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
    for (i=0; i < dayNames.length; i++) {
      dayName = dayNames[i];
      td = tr.insertCell(tr.cells.length);
      td.setAttribute("align", "center");
      if (document.all) {
        td.innerHTML = dayName;
      } else {
        txt =document.createTextNode(dayName);
        td.appendChild(txt);
      }    
    }

    //now populate the table with the actual numerical day links
    var offset = 1;
    var startDay = getStartDayOfMonth(monthName);
    var numDays = getNumberOfDaysInMonth(monthName);
    tr = tbody.insertRow(tbody.rows.length);
    for (e=0;e<startDay;e++) {
      td = tr.insertCell(tr.cells.length);
      offset++;
    }

    for (d=1;d<numDays+1;d++) {
      if (offset > 7) {
        tr = tbody.insertRow(tbody.rows.length);
        offset = 1;
      }
      td = tr.insertCell(tr.cells.length);
      td.setAttribute("align", "center");
      //var link = "<a href=\"javascript:pick_day(\'"+divName+"\', \'"+inputName+"\', \'"+d+"\');\">"+d+"</a>";

      var newAnchor = document.createElement("a");
      newAnchor.setAttribute("href", "javascript:pick_day(\'"+divName+"\', \'"+inputName+"\', \'"+d+"\')");
      var text = document.createTextNode(d);
      // add to the anchor
      newAnchor.appendChild(text);
      td.appendChild(newAnchor);

      offset++;
    }

    //make dummy entries do the Calendar isn't too ugly
    for(q=offset;q<8;q++) {
      td = tr.insertCell(tr.cells.length);      
    }

    toggleLayer(divName);
    activate_mister_hidey();
}

function activate_mister_hidey() {
    mh = document.getElementById('whole_page_div_transparent');  

    var w = document.body.clientWidth;
    var h = document.body.clientHeight;
    mh.style.width= w + "px";
    mh.style.height= h + "px";    

    //JPF interstingly, IE doesn't care about these, it seems...
    mh.style.x = 0;
    mh.style.y = 0;
    mh.style.top = 0;
    mh.style.left = 0;

    mh.style.display = "block";
    mh.style.background = "transparent";
    //mh.style.background = "black";
    mh.style.zIndex = 2;  

}

function hide_mister_hidey() {
    mh = document.getElementById('whole_page_div_transparent');   
    mh.style.display = "none";    
}


function hide_all_div_stuff() {
    hide_mister_hidey();
    if (open_window != null) {
      var style42 = document.getElementById(open_window).style;
      style42.display = "";
      open_window = null;
    }

    //hide the email name stuff
    var ed = document.getElementById("email_div_id").style;
    ed.display = "";

    var cal_size_element = document.getElementById("cal_size_about");
    if (cal_size_element) {
      cal_size_element.style.display = "";
    }
}

function getNumberOfDaysInMonth(month) {
    switch (month) {
        case "January": 
        case "March":
	case "May":
	case "July":
	case "August":
	case "December":
	case "October":
	  return 31;
        case "February":
          if (isLeapYear) {
	    return 29;
	  } else {
            return 28;
	  }
        default:
	  return 30;
    }

}

//this seems dodgy, but it should work and will be done
function getStartDayOfMonth(month) {
    switch (month) {
	case "January": 
            return start_day_January;   
        case "February":
            return start_day_February;
        case "March":
            return start_day_March;
	case "April":
            return start_day_April;
	case "May":
            return start_day_May;
        case "June":
            return start_day_June;
	case "July":
            return start_day_July;
	case "August":
            return start_day_August;
        case "September":
            return start_day_September;
	case "October":
            return start_day_October;
        case "November":
            return start_day_November;
	case "December":
            return start_day_December;
    }
}

function getSelectedMonth(monthSelectorName) {
    var month = document.getElementById(monthSelectorName);

    var n = month.selectedIndex;    // Which menu item is selected
    var val = month[n].text;
    return val;
}

function pickShowOrHide(divName) {
	var objDivStyle = eval('document.all.' + divName + '.style');
	if (objDivStyle.visibility == 'visible') {
		hideDiv(divName);
	} else {
		showDiv(divName);
	}
}

function showDiv(divName) {
	var objDivStyle = eval('document.all.' + divName + '.style');
	objDivStyle.visibility = 'visible';
}

function hideDiv(divName) {
	var objDivStyle = eval('document.all.' + divName + '.style');
	objDivStyle.visibility = 'hidden';
}
// ------------- the above I'll probably junk once I figure out this magic div stuff------------

function toggleLayer(whichLayer) {

    //JPF to be less clear, open_window is just the day selector table/pane

    if (open_window != null && whichLayer != open_window) {
      var style42 = document.getElementById(open_window).style;
      style42.display = "";
    }
    open_window = whichLayer;

    if (document.getElementById) {
	// this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;
	style2.display = style2.display? "":"block";
    } else if (document.all) {
	// this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
	style2.display = style2.display? "":"block";
    } else if (document.layers) {
	// this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = style2.display? "":"block";
    }
}

function set_form_var(formvar, newvalue) {
  if (document.layers) {
    document.layers[formvar].value = newvalue;
  } else if (document.getElementById) {
    document.getElementById(formvar).value = newvalue;
  } else if (document.all) {
    document.all[formvar].value = newvalue;
  } 
}

function pick_day(divname, inputname, day) {
  toggleLayer(divname);
  set_form_var(inputname, day);

  if (divname.indexOf("new") < 0) {
    var index = divname.indexOf("_");
    var id = divname.substring(0, index);    
    flag_event_change(id);
  }
  hide_mister_hidey();
}

function day_chooser(row, month_element) {
  //month = document.forms[0].month_element.value;
  //trying div stuff NewWindow=window.open('http://localhost/calendar/day_selector.php?row='+row, '','width=180,height=150,left=540,top=400');   
//alert("row looks to be:"+row);
  toggleLayer(row);
}

function which_to_del(row) {
  document.forms[0].to_del.value = row;
  document.forms[0].submit();
}

function submit_action_type(type) {
  document.forms[0].action_type.value = type;
  document.forms[0].submit();
}

//they changed months, so do both these things
function changed_month(event_id) {
  flag_event_change(event_id);

  //JPF Aug 1 06  erm...how long has this not been needed/working?!
  //hide_all_event_stuff();
}

function flag_event_change(event_id, field) {
//alert("hi, checking:"+field);
  if(check_date(field)) {
    document.forms[0].event_changes.value +=","+event_id;
  }
}

function flag_month_change(month_id) {
  document.forms[0].month_changes.value += ","+month_id;
}

function check_date(name) {
  var field = document.getElementById(name);
  //KISS, yo  (that is, let teh server-side stuff do a more accurate check
  if (field == null) {
    //we got here from somewhere else...just don't sweat it
    return true;
  }
  //alert('value:'+field.value);
  if (!(field.value > 0 && field.value < 32)) {
    field.value = "";
    return false;
  }
  return true;
}
