//
// TCC javascript
//
// slideshow startup code, form validation, random images
//

//////////////////////////////////////////////////////////////////////
//
// SLIDE SHOW
//
//

//
// open slideshow in a new window
//
function openslideshow () {
  width = 440;
  height = 428;
  var newwindow = window.open('../gallery/index.html', 'gallery', 'width='+width+',height='+height+',resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,status=no');
  if (newwindow) {
    if (window.focus)
      newwindow.focus();
    return false;
  }
  return true; // unsuccessful
}


//////////////////////////////////////////////////////////////////////
//
// FORM VALIDATION
//
//

//
// return the value of the checked item in a radio group, or "" if none
// can also be used to return the value of the first checked item in a series of identically-named checkboxes
// rg is a radio group i.e. form.radiogroupname or checkbox group name
//
function getradiovalue (rg) {
	var retval = "";
	for (var i = 0; i < rg.length; i++) {
		if (rg[i].checked) {
			retval = rg[i].value;
			break;
		}
	}
	return retval;
}
//
// returns the number of checked boxes in a series of identically-named checkboxes
//
function getcheckedcount (cg) {
	var retval = 0;
	for (var i = 0; i < cg.length; i++) {
		if (cg[i].checked) {
			++retval;
		}
	}
	return retval;
}
//
// validate application form
//
function validate_applicationform(x) {
	//
	// check info for child 1
	//
  if (x.child1_name_first.value.search(/\S/) < 0) {
    alert("Please enter Child 1's first name");
    return false;
  }
  if (x.child1_name_last.value.search(/\S/) < 0) {
    alert("Please enter Child 1's last name");
    return false;
  }
  if (!getradiovalue(x.child1_gender)) {
    alert("Please enter Child 1's gender");
    return false;
  }
  if (!x.child1_dobyear.value) {
    alert("Please enter Child 1's date or expected date of birth");
    return false;
  }
  if (!x.child1_startyear.value) {
    alert("Please enter Child 1's desired enrollment date");
    return false;
  }
  if (!getradiovalue(x.child1_eastgate) && !getradiovalue(x.child1_lincoln) && !getradiovalue(x.child1_stata) && !getradiovalue(x.child1_westgate)) {
    alert("Please select at least one TCC center/schedule for Child 1");
    return false;
	}

  //
	// check info for child 2 - if a name is given, check gender and dates
	// 
  if (x.child2_name_last && x.child2_name_last.value.search(/\S/) >= 0) {
    if (!getradiovalue(x.child2_gender)) {
      alert("Please enter Child 2's gender");
      return false;
    }
    if (!x.child2_dobyear.value) {
      alert("Please enter Child 2's date or expected date of birth");
      return false;
    }
    if (!x.child2_startyear.value) {
      alert("Please enter Child 2's desired enrollment date");
      return false;
		}
    if (!getradiovalue(x.child2_eastgate) && !getradiovalue(x.child2_lincoln) && !getradiovalue(x.child2_stata) && !getradiovalue(x.child2_westgate)) {
      alert("Please select at least one TCC center/schedule for Child 2");
      return false;
	  }
  }
	//
	// check info for child 3 - if a name is given, check gender and dates
	// 
  if (x.child3_name_last && x.child3_name_last.value.search(/\S/) >= 0) {
    if (!getradiovalue(x.child3_gender)) {
      alert("Please enter Child 3's gender");
      return false;
    }
    if (!x.child3_dobyear.value) {
      alert("Please enter Child 3's date or expected date of birth");
      return false;
    }
    if (!x.child3_startyear.value) {
      alert("Please enter Child 3's desired enrollment date");
      return false;
		}
    if (!getradiovalue(x.child3_eastgate) && !getradiovalue(x.child3_lincoln) && !getradiovalue(x.child3_stata) && !getradiovalue(x.child3_westgate)) {
      alert("Please select at least one TCC center/schedule for Child 3");
      return false;
	  }
  }
	//
	// check info for parent/guardian 1
	//
  if (x.parent1_relationship.value.search(/\S/) < 0) {
    alert("Please enter Parent/Guardian 1's relationship to the child");
    return false;
  }
  if (x.parent1_name.value.search(/\S/) < 0) {
    alert("Please enter Parent/Guardian 1's name");
    return false;
  }
  if (x.parent1_address.value.search(/\S/) < 0) {
    alert("Please enter Parent/Guardian 1's address");
    return false;
  }
  if (x.parent1_citystatezip.value.search(/\S/) < 0) {
    alert("Please enter Parent/Guardian 1's city, state, and zip");
    return false;
  }
  if (x.parent1_home.value.search(/\S/) < 0) {
    alert("Please enter Parent/Guardian 1's home phone number");
    return false;
  }
	//
	// check info for parent/guardian 2
	// since there may be only one parent/guardian, we don't actually
	// check anything here
	//
  
	//
	// make sure that no more than two bright horizons centers are checked
	//
  if (getcheckedcount(x.brighthorizons) > 2) {
    alert("Please select no more than two Bright Horizons centers to which your application will be sent");
    return false;
	}

  // no errors
	return true;
}


//LIMIT BRIGHT HORIZONS TO 2 SELECTIONS
function horizons(horizons){
	//var boxes = new Array(document.form1.rogers,document.form1.upark,document.form1.onekendall,document.form1.westchurch);
	var count=0;
	for(x=0; x < horizons.length; x++){
		if(horizons[x].checked==true){
			count++
		}
	}
	if(count==2){
		for(x=0; x < horizons.length; x++){
			if(horizons[x].checked==false){horizons[x].disabled=true;}
		}
	}
	else{for(x=0; x< horizons.length; x++){horizons[x].disabled=false;}}
		
}

//
// validate contact form
//
function validate_contactform(x) {
  //x=MM_findObj(contactform);
  if (x.name.value.search(/\S/) < 0) {
    alert("Please enter your name");
    return false;
  }
  if (x.email.value.search(/\S/) < 0) {
    alert("Please enter your email address");
    return false;
  }
  if (x.email.value.search(/\S+@\S+\.\S{2,3}/) < 0) {
    alert("Please enter a valid email address");
    return false;
  }
  if (x.email2.value.search(/\S/) < 0) {
    alert("Please confirm your email address");
    return false;
  }
  if (x.email.value != x.email2.value) {
    alert("The two email addresses do not match");
    return false;
  }
  if (x.phone.value.search(/\S/) < 0) {
    alert("Please enter your phone number");
    return false;
  }
  if (x.comments.value.search(/\S/) < 0) {
    alert("Please enter your questions or comments");
    return false;
  }
  return true;
}

//////////////////////////////////////////////////////////////////////
//
// RANDOM IMAGES
//
//

//
// write code for home page photo (done in two parts)
//
var randhomephotos = new Array(
  new Array ("img/home/c1photo1.gif","img/home/c2photo1.gif"),
  new Array ("img/home/c1photo2.gif","img/home/c2photo2.gif"),
  new Array ("img/home/c1photo3.gif","img/home/c2photo3.gif"),
//  new Array ("img/home/c1photo4.gif","img/home/c2photo4.gif"),
  new Array ("img/home/c1photo5.gif","img/home/c2photo5.gif"),
  new Array ("img/home/c1photo6.gif","img/home/c2photo6.gif"),
  new Array ("img/home/c1photo7.gif","img/home/c2photo7.gif")
);
var nhomepagephoto = Math.floor(Math.random()*randhomephotos.length);    // pick one at random

function randhomephoto1() {
  document.writeln('<div><img src="'+randhomephotos[nhomepagephoto][0]+'" alt="" id="strut1b" /></div>');
}
function randhomephoto2() {
  document.writeln('<div><img src="'+randhomephotos[nhomepagephoto][1]+'" alt="" id="strut2b" /></div>');
}

//
// write code for center photos
// there are only two for each center, numbered 1 and 2
//
function randcenterphoto(ctr) {
	n = Math.floor(Math.random()*2)+1;
  document.writeln('<div id="divphotobar"><img src="../../img/lev3photos/'+ctr+n+'.jpg" alt="" width="438" height="131" /></div>');
}


//
// write code for level 2 photos
// note that in two sections, there are two photos overlapping the red navigation circle;
// in one other section, the smaller photo is replaced by a slideshow button;
// and in the last section, there is no red navigation circle
//
var randbuttonphotos = new Array(
	"../img/lev2photos/buttonphoto1.gif",
	"../img/lev2photos/buttonphoto2.gif",
	"../img/lev2photos/buttonphoto3.gif"
);
var randredcirclephotos = new Array(
	"../img/lev2photos/redcirclephotos1.gif",
	"../img/lev2photos/redcirclephotos2.gif",
	"../img/lev2photos/redcirclephotos3.gif",
	"../img/lev2photos/redcirclephotos4.gif",
	"../img/lev2photos/redcirclephotos5.gif",
	"../img/lev2photos/redcirclephotos6.gif",
	"../img/lev2photos/redcirclephotos7.gif",
	"../img/lev2photos/redcirclephotos8.gif",
	"../img/lev2photos/redcirclephotos9.gif"
);
var randnocirclephotos = new Array(
	"../img/lev2photos/nocirclephotos1.gif",
	"../img/lev2photos/nocirclephotos2.gif",
	"../img/lev2photos/nocirclephotos3.gif",
	"../img/lev2photos/nocirclephotos4.gif",
	"../img/lev2photos/nocirclephotos5.gif",
	"../img/lev2photos/nocirclephotos6.gif",
	"../img/lev2photos/nocirclephotos7.gif",
	"../img/lev2photos/nocirclephotos8.gif",
	"../img/lev2photos/nocirclephotos9.gif"
);
function randlevel2photo(type) {
	// select an image
	var imgfile;
	if (type == 'button') {
		imgfile = randbuttonphotos[Math.floor(Math.random()*randbuttonphotos.length)];
	}
	else if (type == 'nocircle') {
		imgfile = randnocirclephotos[Math.floor(Math.random()*randnocirclephotos.length)];
	}
	else {
		imgfile = randredcirclephotos[Math.floor(Math.random()*randredcirclephotos.length)];
	}
  // write css to select the image
  document.writeln("<style type='text/css'>");
  document.writeln("#nav2containertable td.r4c123 { background-image: url("+imgfile+") }");
  document.writeln("</style>");
}


