//preload images
totalNumImages = 5
preloadOnImages = new Array(totalNumImages)
for( i=0; i<totalNumImages; ++i ) {
	preloadOnImages[i] = new Image(1,1);
	var iStr = ( new Number(i) ).toString();
	preloadOnImages[i].src = "img/button_"+iStr+"_on.gif"
}
preloadOffImages = new Array()
for( i=0; i<totalNumImages; ++i ) {
	preloadOffImages[i] = new Image(1,1);
	var iStr = ( new Number(i) ).toString();
	preloadOffImages[i].src = "img/button_"+iStr+"_off.gif"
}

//image swap functions
function turnon( i ) {
	var iStr = ( new Number(i) ).toString();
	document.getElementById("i"+iStr).src = preloadOnImages[i].src;
	return true;
}
function turnoff( i ) {
	var iStr = ( new Number(i) ).toString();
	document.getElementById("i"+iStr).src = preloadOffImages[i].src;
	return true;
}

//table color changing functions
function turnonevent( tableId ) {
	document.getElementById( tableId ).style.backgroundColor = '#d7d7d7';
}
function turnoffevent( tableId ) {
	document.getElementById( tableId ).style.backgroundColor = '#f1f1ef';
}


//FROM OLD LAYOUT:

var timeout_id=0; //keeps track of our price changing timeout thing

//adds an email link, to avoid email harvesters
function add_email_with_class(who,className)
{
  if(who == "peter")
    document.write('<a href="mail'+'to:mail'+'@'+'geriatricservices.ca" class="' + className + '">mail'+'@'+'geriatricservices.ca</a>');
}
function add_email(who)
{
  add_email_with_class(who,"");
}

//checks to make sure they've entered all the info before submitting a registration
function register_form_check()
{
  day1 = document.forms[0].RegisterFor[0].checked
  day2 = document.forms[0].RegisterFor[1].checked
  bothdays = document.forms[0].RegisterFor[2].checked
  
  if (!day1 && !day2 && !bothdays) {
    alert("Please select the workshop day(s) you wish to register for to continue.");
    return false;
  }
  if (day1 && day2 || day1 && bothdays || day2 && bothdays) {
    alert("There was a problem with your registration. Please try again or contact us directly to register.");
    return false;
  }
  
  var retval = true;
  if (document.forms[0].Name.value == "")
    retval = false;
  if (document.forms[0].Address.value == "")
    retval = false;
  if (document.forms[0].PlaceOfEmployment.value == "")
    retval = false;
  if (retval == false)
    alert("Please fill in more fields to continue");
  return retval;
}

//updates the price and price per unit in the ordering page
function update_selflearn_price()
{
  //get and error check the quantity
  var quantity = document.forms[0].Quantity.value;
  
  var pricePerUnitVar=19;
  var shippingCostVar=0;
  var baseCostVar=0;
  var totalCostVar=0;
  
  var quantityInt = parseInt(quantity);
  //make sure it's in range
  if (!(quantityInt > 0 && quantityInt < 1000))
    quantityInt = 1;

  //see if the int has changed by comparing it as a string
  //if it did change, we know that it's an errored input
  if (quantity == ""+quantityInt) {
  
    //decide cost and shipping
    if (quantityInt <= 3) {
      pricePerUnitVar = 19;
      shippingCostVar = 2.5;
    }
    else if (quantityInt <= 10) {
      pricePerUnitVar = 19;
      shippingCostVar = 8;
    }
    else if (quantityInt <= 20) {
      pricePerUnitVar = 17;
      shippingCostVar = 12.5;
    }
    else {
      pricePerUnitVar = 15;
      shippingCostVar = 15;
      //calculate the "over 50" part
      if (quantityInt > 50 ) shippingCostVar = 0; //handled later
    }

    baseCostVar = quantityInt * pricePerUnitVar; //find base cost
    totalCostVar = baseCostVar + shippingCostVar; //get total cost
  }
  
  //set UI values
  if (document.forms[0].Shipping[0].selected && quantityInt <= 50) {
    document.getElementById('shippingCost').innerHTML = shippingCostVar.toFixed(2);
    document.getElementById('plusShippingNote').innerHTML = "";
  } else {
    document.getElementById('shippingCost').innerHTML = "[<a href=\"contact.php\">ask</a>]";
    document.getElementById('plusShippingNote').innerHTML = "+shipping";
    totalCostVar -= shippingCostVar; //reverse shipping cost
  }
  document.getElementById('pricePerUnit').innerHTML = pricePerUnitVar.toFixed(2);
  document.getElementById('baseCost').innerHTML = baseCostVar.toFixed(2);
  document.getElementById('totalCost').innerHTML = totalCostVar.toFixed(2);
}

//add a delay between entering a quantity into the box and updating the price
function update_selflearn_price_timed()
{
  clearTimeout(timeout_id);
  timeout_id = setTimeout('update_selflearn_price()',250);
}


