//--------------- LOCALIZEABLE GLOBALS ---------------
var d=new Date();
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
//Ensure correct for language. English is "January 1, 2004"
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
//---------------   END LOCALIZEABLE   ---------------
/* Please be aware if changing this array, that the script is depending on the first 4 being lodging and the last being meals */

var conversionRate = 1.06; // change to whatever 1 usd is in canadian
var boxes = new Array(
"sabbathday",
"thursdaynight",
"fridaynight",
"saturdaynight",
"thursdaysupper",
"fridaybreakfast",
"fridaylunch",
"fridaysupper",
"sabbathbreakfast",
"sabbathlunch",
"sabbathsupper",
"sundaybreakfast");

function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
}

function checkChoices(theForm) {
  var mealChecked=0
  for (i=0;i<boxes.length;i++) {
    if (boxes[i].indexOf('Lodge')==-1 && boxes[i]!='sabbathday') mealChecked += theForm.elements[boxes[i]].checked;
  }
  return (mealChecked > 0)
}

function unDisable(f,element,value){

    if(f[element].checked == false){

      f["sabbathday"].disabled=false;
      f["thursdaynight"].disabled=false;
      f["fridaynight"].disabled=false;
      f["saturdaynight"].disabled=false;
      f["thursdaysupper"].disabled=false;
      f["fridaybreakfast"].disabled=false;
      f["fridaylunch"].disabled=false;
      f["fridaysupper"].disabled=false;
      f["sabbathbreakfast"].disabled=false;
      f["sabbathlunch"].disabled=false;
      f["sabbathsupper"].disabled=false;
      f["sundaybreakfast"].disabled=false;
    }
    checksabbathday(f);
  }
function clearOutBoxes(f,element,value){
      f["sabbathday"].disabled=false;
      f["thursdaynight"].disabled=false;
      f["fridaynight"].disabled=false;
      f["saturdaynight"].disabled=false;
      f["thursdaysupper"].disabled=false;
      f["fridaybreakfast"].disabled=false;
      f["fridaylunch"].disabled=false;
      f["fridaysupper"].disabled=false;
      f["sabbathbreakfast"].disabled=false;
      f["sabbathlunch"].disabled=false;
      f["sabbathsupper"].disabled=false;
      f["sundaybreakfast"].disabled=false;
	  
  }
function checksabbathday(f) {
  chk = f.sabbathday.checked;
  f["thursdaynight"].disabled=chk;
  f["fridaynight"].disabled=chk;
  f["saturdaynight"].disabled=chk;
  f["thursdaysupper"].disabled=chk;
  f["fridaybreakfast"].disabled=chk;
  f["fridaylunch"].disabled=chk;
  f["fridaysupper"].disabled=chk;
  f["sundaybreakfast"].disabled=chk;

  if (chk) {
    f["thursdaynight"].checked=false;
    f["fridaynight"].checked=false;
    f["saturdaynight"].checked=false;
    f["thursdaysupper"].checked=false;
    f["fridaybreakfast"].checked=false;
    f["fridaylunch"].checked=false;
    f["fridaysupper"].checked=false;   
    f["sundaybreakfast"].checked=false; 
  }
  if (chk) {
    f["sabbathbreakfast"].disabled=false;
    f["sabbathlunch"].disabled=false;
    f["sabbathsupper"].disabled=false;
  }
  checkChoices(f);

}  


function getChecked(rad) {
  for (i=0;i<rad.length;i++) {
    if (rad[i].checked) return rad[i].value;
  }
  return "";
}

function CheckChoice(whichbox) { // we no longer need the whichbox
    var theForm = document.myform;
    var total = 0;
    var age = getChecked(theForm.age);
    if (age =="Child") { // no need to continue
      theForm.hiddentotal.value = 0;
      convertCurrency(theForm);
      return(formatCurrency(theForm.hiddentotal.value));
    }
    for (var i=0;i<boxes.length;i++) {
      var check = theForm.elements[boxes[i]];
      if (check.checked) {
        total += parseFloat(check.value);
        if (i<4) { // the registration boxes
          if (age != "Senior" && age != "Student") total += registrationFee; // registration fee
        }
      }
    }
    theForm.hiddentotal.value = total
    //Now, return with formatted total:
    convertCurrency(theForm);
    return(formatCurrency(theForm.hiddentotal.value));
}

//Define function to format a value as currency:
function formatCurrency(num) { // Courtesy of http://www7.brinkster.com/cyanide7/
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num)) num = "0";
  var sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  var cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10) cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

var submitted = false; // initially this is false

//Define function to init the form on reload:
function InitForm() {
  var theForm = document.myform;
  //Reset the displayed total on form:
  theForm.total.value='$0';
  theForm.hiddentotal.value=0;
  theForm.hiddenpriorradio.value=0;
  //Set all checkboxes and radio buttons on form-1 to unchecked:
  for (var i=0; i < theForm.elements.length; i++) {
    var elem = theForm.elements[i]
    if (elem.name=="subscribe") continue; // do not reset the subscribe
    if (elem.type == 'checkbox' || elem.type == 'radio') {
      elem.checked = false;
    }
  }
  submitted = false; // allow a new submission
}
/*
var image1=new Image()
image1.src="images/slide1.jpg"
var image2=new Image()
image2.src="images/slide2.jpg"
var image3=new Image()
image3.src="images/slide3.jpg"
var image4=new Image()
image4.src="images/slide4.jpg"
var image5=new Image()
image5.src="images/slide5.jpg"
var image6=new Image()
image6.src="images/slide6.jpg"
var image7=new Image()
image7.src="images/slide7.jpg"
var image8=new Image()
image8.src="images/slide8.jpg"
var image9=new Image()
image9.src="images/slide9.jpg"
var image10=new Image()
image10.src="images/slide10.jpg"

//variable that will increment through the images
var step=1
function slideit(){
  //if browser does not support the image object, exit.
  if (!document.images) return
  document.images["slide"].src=eval("image"+step+".src")
  if (step<10) step++
  else step=1
}
*/
var tId = "";
function initPage() {
  document.getElementById('dateformat').innerHTML=TODAY;	
  //tId=setInterval("slideit()",2500)
  InitForm();
}  

function validate(theForm) {
  if(submitted) {
    alert('This form has already been submitted, please wait for the registration to take place')
    return false;
  }
  if (theForm.fname.value=="") {
    alert("Please enter a first name")
    theForm.fname.focus();
    return false
  }
  if (theForm.lname.value=="") {
    alert("Please enter a last name")
    theForm.lname.focus();
    return false
  }
  if (getChecked(theForm.gender) =="") {
    alert("Please select a gender")
    theForm.gender[0].focus();
    return false
  }
  if (theForm.address.value=="") {
    alert("Please enter a street address")
    theForm.address.focus();
    return false
  }
  if (theForm.city.value=="") {
    alert("Please enter a city")
    theForm.city.focus();
    return false
  }
  if (theForm.state.options[theForm.state.selectedIndex].value=="") {
    alert("Please select a state")
    theForm.state.focus();
    return false
  }
  if (theForm.zipcode.value=="") {
    alert("Please enter a zip code")
    theForm.zipcode.focus();
    return false
  }
  if (theForm.country.options[theForm.country.selectedIndex].value=="") {
    alert("Please select a country")
    theForm.country.focus();
    return false
  }
  var phone = theForm.phone.value;

  if (phone=="") {
    alert("Please enter a phone number")
    theForm.phone.focus();
    return false
  }
  if (phone.length==10) {
    var pattern = /(\d{3})(\d{3})(\d{4})/
    theForm.phone.value = phone.replace(pattern,"($1) $2-$3")
  }
  if (theForm.email.value=="") {
    alert("Please enter an email address")
    theForm.email.focus();
    return false
  }
  if (getChecked(theForm.phonetype) =="") {
    alert("Please select a primary contact")
    theForm.phonetype[0].focus();
    return false
  }
  if (theForm.password.value=="") {
    alert("Please enter a password")
    theForm.password.focus();
    return false
  }  
  if (getChecked(theForm.age) =="") {
    alert("Please select an age")
    theForm.age[0].focus();
    return false
  }
  var lodging = "";
  for (var i=0;i<4;i++) { // the first 4 (0-3) are lodging
    var check = theForm.elements[boxes[i]];
    if (check.checked) {
      lodging = check.value
    }
  }
  if (lodging=="") {
    alert("Please select at least one day")
    theForm.elements[boxes[0]].focus()
    return false
  }
  // Meals
  var meal = false;
  for (var i=4;i<boxes.length;i++) { // the next are meals
    var check = theForm.elements[boxes[i]];
    if (check.checked) {
      meal=true;
      break; // one meal is enough
    }
  }
  if (meal && theForm.mealstyle[0].checked==false && theForm.mealstyle[1].checked==false) {
    alert("Please select a meal style")
    theForm.mealstyle[0].focus();
    return false
  }
/*  We check the subscribe in the html now - this section is commented out but left in in case
  // subscription
  // we do not want to bother the user to click since it is a yes no
  if (getChecked(theForm.subscribe) =="") {
    if (confirm("You did not choose to receive DEAR updates - Do you want to?\nPlease click OK to subscribe, cancel to not")) {
      theForm.subscribe[0].click();
    }
    else theForm.subscribe[1].click();
  }
*/
  submitted =true;
  return true; // true to submit
}
function checkCountry(theSel) {
  if (theSel.selectedIndex==0 && theSel.form.state.selectedIndex>=55) 
    theSel.form.state.selectedIndex=2; // move to United States
  else if (theSel.selectedIndex==1 && theSel.form.state.selectedIndex<55)
    theSel.form.state.selectedIndex=56; // move to Canada
  CheckChoice(); // get the latest value
  convertCurrency(theSel.form)    
}
function checkState(theSel) {
  theSel.form.country.selectedIndex=(theSel.selectedIndex<55)?0:1;
  CheckChoice(); // get the latest value
  convertCurrency(theSel.form)
}                
function convertCurrency(theForm) {
  var val = theForm.hiddentotal.value; 
  var canadian = formatCurrency(val*conversionRate)
  document.getElementById('currency').innerHTML =(theForm.country.selectedIndex==0)? "":"("+canadian+" Canadian dollars using a rate of "+conversionRate+")";
}
