/**** Template B functions ****/

/* Initialize the page with the correct content */
function loadPage (defaultSection) {
	var url = document.URL;
	if (url.lastIndexOf('#') == -1)
		showSection(defaultSection);
	else {
		var section = url.substring(url.lastIndexOf('#') + 1);
		showSection (section);
	}
}



/* Called whenever a left-nav selection is made */
function showSectionForAnchor (anchor) { 
	var nameOfDiv = anchor.id.substring(3);
	showSection (nameOfDiv);
}

function showSection (nameOfDiv) {
	// Show the text section itself, and hide the one currently displayed
	var allSections = document.getElementsByClassName ('hidden-section');
	for (var i=0; i < allSections.length; ++i) {
  		var section = allSections[i];
		section.style.display = 'none';
	}
	var theDiv = document.getElementById (nameOfDiv);
	if (theDiv) {
		theDiv.style.display = 'block';
	}
	resetSelectedAnchor ('left-nav-link', nameOfDiv);
	resetSelectedAnchor ('sequence-nav-link', nameOfDiv);
}

function resetSelectedAnchor (anchorClass, nameOfDiv) {
	// Helper function for showSection
	// Hide the anchor(s) currently selected in this set; show the correct one
	var allAnchors = document.getElementsByClassName (anchorClass);
	for (var i=0; i < allAnchors.length; ++i) {
		var thisAnchor = allAnchors[i];
		if (thisAnchor.id.substring(3) == nameOfDiv) {
			if (thisAnchor.className.indexOf('selected') == -1)
				thisAnchor.className += ' selected';
		} else {
			if (thisAnchor.className.indexOf('selected') != -1)
				thisAnchor.className = thisAnchor.className.replace /* (/\sselected/, ''); */
					(new RegExp("\\sselected"), '');
		}
	}
}

/* Right arrow */
function nextPage() {
	var allSections = document.getElementsByClassName ('hidden-section');
	for (var i=0; i < allSections.length; ++i) {
		if ((allSections[i].style.display != 'none') && (i < allSections.length)) {
			showSection (allSections[i+1].id);
			break;
		}
	}
}

/* Left arrow */
function prevPage() {
	var allSections = document.getElementsByClassName ('hidden-section');
	for (var i=0; i < allSections.length; ++i) {
  		var section = allSections[i];
		if ((allSections[i].style.display != 'none') && (i > 0)) {
			showSection (allSections[i-1].id);
			break;
		}
	}
}



/***** Sign-up form functions *****/

function student_parent_info(){
	var txt=document.getElementById("parentinfo");
	txt.innerHTML=document.form1.parent_name.value+"<br>";
	txt.innerHTML+=document.form1.parent_email.value+"<br>";
	txt.innerHTML+=document.form1.parent_phone.value+"<br>";
	txt.innerHTML+=document.form1.parent_address.value+"<br>";
	var tempvar=document.form1.parent_address2.value;
	if(tempvar!=''){
	txt.innerHTML+=document.form1.parent_address2.value+"<br>";
	}
	txt.innerHTML+=document.form1.parent_city.value+", "+document.form1.parent_state.value+" "+document.form1.parent_zip.value;

	var txt2=document.getElementById("studentinfo");
	txt2.innerHTML=document.form1.student_name.value+"<br>";
	txt2.innerHTML+=document.form1.student_email.value+"<br>";
	txt2.innerHTML+=document.form1.phone.value+"<br>";
	txt2.innerHTML+=document.form1.high_school.value+"<br>";
	txt2.innerHTML+=document.form1.GPA.value+"<br>";
	
	var txt3=document.getElementById("mailto-info");
	txt3.innerHTML=document.form1.student_name.value+"<br>";
	txt3.innerHTML+=document.form1.student_email.value+"<br>";
	txt3.innerHTML+=document.form1.parent_name.value+"<br>";
	txt3.innerHTML+=document.form1.parent_email.value+"<br>";
}

function mailtestinfo(){
	oFormObject = document.forms['sendatest'];
	txt=oFormObject.elements["sendatestinfo"];
	txt.value="Student:\n";
	txt.value+=document.form1.student_name.value+"\n";
	txt.value+=document.form1.student_email.value+"\n\n";
	txt.value+="Parent:\n";
	txt.value+=document.form1.parent_name.value+"\n";
	txt.value+=document.form1.parent_email.value+"\n";
	document.sendatest.submit();
}

function scorefill(){
	var txt=document.getElementById("test-info");
	txt.innerHTML=testTaken;
	if(testTaken=='SAT'){
		txt.innerHTML+=" "+document.form1.Recent_SAT_Date.value+"<br>";
		txt.innerHTML+="Reading: "+document.form1.Recent_SAT_RDG.value+", ";
		txt.innerHTML+="Writing: "+document.form1.Recent_SAT_WRIT.value+", <br>";
		txt.innerHTML+="Math: "+document.form1.Recent_SAT_Math.value+", ";
		txt.innerHTML+="Essay: "+document.form1.Recent_SAT_Essay.value;
	}else{
		txt.innerHTML+=" "+document.form1.PSAT_Date.value+"<br>";
		txt.innerHTML+="Reading: "+document.form1.PSAT_Reading.value+", ";
		txt.innerHTML+="Writing: "+document.form1.PSAT_Writing.value+", ";
		txt.innerHTML+="Math: "+document.form1.PSAT_Math.value;
	}
	
	var txt=document.getElementById("goals-info");
	txt.innerHTML="Score goal(s): "+document.form1.SAT_Goal.value+"<br>";
	txt.innerHTML+=document.form1.targets.value;
	
	document.getElementById("prev-prep-info").innerHTML=document.getElementById("previous_prep").value;
	
	document.getElementById("reco-info").innerHTML=document.getElementById("heardabout").value+", "+document.getElementById("ref_name").value;

	if(document.getElementById("anything_else").value!=''){
		document.getElementById("other-info").innerHTML=document.getElementById("anything_else").value;
	}else{
		document.getElementById("other-info").innerHTML="none";
	}
}



function cardsave(){
	var longcard=document.getElementById("cardnumber").value;
	var shortcard="XXXX XXXX XXXX "+longcard.substr(-4)+"<br>";
	var txt=document.getElementById("card-number-info");
	txt.innerHTML=shortcard;
	magiccode=document.getElementById("exp_month").selectedIndex;
	txt.innerHTML+="Expiration: "+document.getElementById("exp_month").options[magiccode].value+"-";
	magiccode=document.getElementById("exp_year").selectedIndex;
	txt.innerHTML+=document.getElementById("exp_year").options[magiccode].value+"<br>";
	txt.innerHTML+="Security code: "+document.getElementById("cardcode").value;
	
	var txt2=document.getElementById("billinginfo");
	txt2.innerHTML=document.getElementById("cardholder_firstname").value+" "+document.getElementById("cardholder_lastname").value+"<br>";
	txt2.innerHTML+=document.getElementById("billingaddress").value+"<br>";
	if(document.getElementById("billingaddress2").value!=''){
		txt2.innerHTML+=document.getElementById("billingaddress2").value+"<br>";
	}
	txt2.innerHTML+=document.getElementById("billingcity").value+", "+document.getElementById("billingstate").value+" "+document.getElementById("billingzip").value+"<br>";
	txt2.innerHTML+=document.getElementById("billingphone").value;
}


/* Called from "Register" button */
function submitSeminarSignup() {
	// This will obviously do more when it's completed!
	validate ('section-3');
	document.form1.submit();
}

function submitPrivateSignup() {
	// This will obviously do more when it's completed!
	validate ('section-2');
	document.form1.submit();
}


/* Called from "Continue" buttons and "Edit this section" links */
function modifyForm (button) {

	switch (button.id) {
		case 'section-1-continue':  

			if (validate ('section-1')) {
				changeFormSectionToDone ('section-1');
				changeFormSectionToEditable ('section-2');
				student_parent_info();
				scrollFormTo ('section-2');
			}
			break;
			
		case 'section-2-continue':
			if (validate ('section-2')) {
				if (areScoresHighEnough()) {
					changeFormSectionToDone ('section-2');
					scorefill();
					changeFormSectionToEditable ('section-3');
					showDiv ('section-3');
					hideDiv ('we-can-help');
					scrollFormTo ('section-3');
				} else {
					hideDiv ('section-3');
					showDiv ('we-can-help');
					scrollFormTo ('we-can-help');
				}
			}
			break;
			
		case 'section-1-edit':
			changeFormSectionToEditable ('section-1');
			if (isEditable('section-2'))
				changeFormSectionToDone ('section-2');
				scorefill();
			if (isEditable('section-3'))
				changeFormSectionToDone ('section-3');
			scrollFormTo ('section-1');
			break;
			
		case 'section-2-edit':
			changeFormSectionToEditable ('section-2');
			if (isEditable('section-1'))
				changeFormSectionToDone ('section-1');
			if (isEditable('section-3'))
				changeFormSectionToDone ('section-3');
			scrollFormTo ('section-2');
			break;
	}
}
function validate_ps_no5(){
	var alertext='';

	if(document.form1.student_name.value==''){
		alertext=alertext+'Please enter the student\'s name\n';
	}

	if(document.form1.student_email.value==''){
		alertext=alertext+'Please enter the student\'s email address\n';
	}

	if(document.form1.phone.value==''){
		alertext=alertext+'Please enter the student\'s cell phone number\n';
	}

	if(document.form1.high_school.value==''){
		alertext=alertext+'Please enter the student\'s high school\n';
	}

	if(document.form1.grad_year.value==''){
		alertext=alertext+'Please select your graduation year\n';
	}

	if(document.form1.parent_name.value==''){
		alertext=alertext+'Please enter the parent\'s name\n';
	}

	if(document.form1.parent_email.value==''){
		alertext=alertext+'Please enter the parent\'s email\n';
	}

	if(document.form1.parent_phone.value==''){
		alertext=alertext+'Please enter the parent\'s phone number\n';
	}

	if(document.form1.parent_address.value==''){
		alertext=alertext+'Please enter your address\n';
	}

	if(document.form1.parent_city.value==''){
		alertext=alertext+'Please enter your city\n';
	}

	if(document.form1.parent_zip.value==''){
		alertext=alertext+'Please enter your zip code\n';
	}

	if((document.form1.ref_name.value=='')&&(document.form1.heardabout.value=='')){
		alertext=alertext+'Please let us know how you heard about BodSAT\n';
	}


	if(alertext!=''){
		alert(alertext);
//		changeFormSectionToEditable('section-1');
		return false;
	}else{
		changeFormSectionToDone ('section-1');
		changeFormSectionToEditable ('section-2');
		student_parent_info();
		scrollFormTo ('section-2');
		return true;
		//document.form1.submit();
	}
}

function validate_cc_no5(){//checks credit card information
	var alertext='';

	if(document.form1.cardholder_firstname.value==''){
		alertext=alertext+'Please enter the Card Holder\'s First Name\n';
	}

	if(document.form1.cardholder_lastname.value==''){
		alertext=alertext+'Please enter the Card Holder\'s Last Name\n';
	}

	if(document.form1.cardnumber.value==''){
		alertext=alertext+'Please enter the Card Number\n';
	}

	if(document.form1.cardcode.value==''){
		alertext=alertext+'Please enter the Card Verification Number\n';
	}

	if(document.form1.billingaddress.value==''){
		alertext=alertext+'Please enter the Billing Address\n';
	}

	if(document.form1.billingcity.value==''){
		alertext=alertext+'Please enter the Billing City\n';
	}

	if(document.form1.billingzip.value==''){
		alertext=alertext+'Please enter the Billing Zip Code\n';
	}

	if(document.form1.billingphone.value==''){
		alertext=alertext+'Please enter the Billing Phone Number\n';
	}

	if(alertext!=''){
		alert(alertext);
		return false;
	}else{
		document.form1.submit();
//	document.ccinfo.submit(); //if no errors, submit billing information
	}
}


/* Called by modifyForm(), i.e. whenever the user clicks Continue or Edit */
function validate (sectionID) {
	var field = document.getElementById ('student_name');
	if (! field.validity) {
			switch(sectionID){
				case 'section-1':
					validate_ps_no5();
					break;
				case 'section-3':
					validate_cc_no5();
					break;
				default:
					return true;
					break;
			}
		//alert("no html5");
		//return true; 
	
		// This browser doesn't use HTML5 form validation!
		// Do something else, e.g. same mechanism used by old site
	}
		
	var fields;
	var errorDivName;
	switch (sectionID) {
		case 'section-1':
	
			fields = ['student_name', 'student_email', 'phone', 'high_school', 'GPA',
					  'parent_name', 'parent_email', 'parent_phone', 
					  'parent_address', 'parent_city', 'parent_state', 'parent_zip'];
			// fields = ['student_name'];  // for testing convenience, that's all
			errorDivName = 'section-1-error-msg';
			
			break;
		case 'section-2':
			// Only need to form-validate one of: PSAT, SAT, Neither.
			// Check state of radio button to see which one to use.
			var whichSubform = getRadioVal('which_test');
			if (whichSubform == 'PSAT') 
				fields = ['PSAT_Date', 'PSAT_Reading', 'PSAT_Writing', 'PSAT_Math',
						  'SAT_Goal', 'targets', 'ref_name']; 
			else if (whichSubform == 'SAT')
				fields = ['Recent_SAT_Date', 'Recent_SAT_RDG', 'Recent_SAT_WRIT', 
					   	  'Recent_SAT_Math', 'Recent_SAT_Essay',
						  'SAT_Goal', 'targets', 'ref_name']; 
			else
				return true;
			errorDivName = 'section-2-error-msg';
			break;
		case 'section-3':
			fields = ['cardnumber', 'cardcode', 'cardholder_firstname', 'cardholder_lastname',
					  'billingaddress', 'billingcity', 'billingzip', 'billingphone'];
			errorDivName = 'section-3-error-msg';
			//nohtmlvalidate_form();
			break;
	}
	var allOK = true;
/* TESTING	*/
	for (fieldID in fields) {
		field = document.getElementById (fields[fieldID]);
		if (! field.checkValidity()) {  // built-in HTML5 sweetness
			addClass (field, 'render-invalid');  // turns its border red
			allOK = false;
		} else {
			removeClass (field, 'render-invalid');
		}
	}
	if (allOK){
		hideDiv(errorDivName);

}else{
		showDiv(errorDivName);  // "Please correct the highlighted fields"

}
	return allOK;
}

/* Called after validate() when user clicks "Continue" for Scores section */
function areScoresHighEnough() {
 // alert("scorechecker");
	var whichSubform = getRadioVal('which_test');
	if (whichSubform == 'PSAT')  {
		var reading = parseFloat(document.getElementById('PSAT_Reading').value);
		var writing = parseFloat(document.getElementById('PSAT_Writing').value);
		var math = parseFloat(document.getElementById('PSAT_Math').value);
		total=reading+writing+math;
		if ((total >= 170) && (math >= 55)) {
			//alert("PSAT good: "+total);
			return true;
		}
	} else if ((whichSubform == 'SAT')||(whichSubform==null)) {
		var reading = parseFloat(document.getElementById('Recent_SAT_RDG').value);
		var writing = parseFloat(document.getElementById('Recent_SAT_WRIT').value);
		var math = parseFloat(document.getElementById('Recent_SAT_Math').value);
		total=reading+writing+math;
		if ((total >= 1800) && (math >= 550)) {
			//alert("SAT good: "+total);
			return true;
		}
	}
	return false;
}

var testTaken='SAT';
/* Called when the user clicks radio buttons "PSAT", "SAT", "Neither" */
function changeTest (radioBtn) {
	if (radioBtn.checked) {
		switch (radioBtn.value) {
			case 'PSAT':
				testTaken='PSAT';
				hideDiv ('SAT-scores');
				showDiv ('PSAT-scores');
				showDiv ('has-taken-a-test')
				hideDiv ('needs-diagnostic');
				showDiv ('section-3');
				break;
			case 'SAT':
				testTaken='SAT';
				hideDiv ('PSAT-scores');
				showDiv ('SAT-scores');
				showDiv ('has-taken-a-test')
				hideDiv ('needs-diagnostic');
				showDiv ('section-3');
				break;
			case 'neither':
				hideDiv ('has-taken-a-test')
				showDiv ('needs-diagnostic');
				hideDiv ('section-3');
				break;
		}
	}
}

function changeHeardAbout (dropdown) {
	var refName = document.getElementById('ref_name');
	switch (dropdown.value) {
		case 'School counselor':
		case 'College counselor':
		case 'Friend or parent':
			if (! refName.hasAttribute ('required'))
				refName.setAttribute ('required', 'required');
			break;
		case 'Internet':
		case 'Other':
			if (refName.hasAttribute ('required')) {
				refName.removeAttribute ('required');
				removeClass (refName, 'render-invalid');
			}
			break;
	}
}

/* Called by fields' onBlur handlers; only checks an already-invalidated field. */
function checkFixedField (field) {
	if (field.className.indexOf('render-invalid') != -1) {
		if (field.checkValidity()) {
			removeClass (field, 'render-invalid');
		}
	}
}


/* Assorted helper functions */

function changeFormSectionToDone (sectionName) {
	hideDiv (sectionName + '-form');
	showDiv (sectionName + '-summary');
	section = document.getElementById(sectionName);
	removeClass (section, 'editable-section');
	addClass    (section, 'done-section');
}
function changeFormSectionToEditable (sectionName) {
	hideDiv (sectionName + '-summary');
	showDiv (sectionName + '-form');
	section = document.getElementById(sectionName);
	removeClass (section, 'upcoming-section');
	removeClass (section, 'done-section');
	addClass    (section, 'editable-section');
}

function isEditable (sectionName) {
	return (document.getElementById(sectionName).className.indexOf('editable-section') != -1);
}
function showDiv(id) {
	// $('#' + id).slideUp();  ...forces page to scroll back to top! JQuery bug.
	document.getElementById(id).style.display = 'block';
}
function hideDiv(id) {
	// $('#' + id).slideDown();  ...forces page to scroll back to top! JQuery bug.
	document.getElementById(id).style.display = 'none';
}
function scrollFormTo(id) {
	$('html,body').delay(500).animate ({scrollTop: $('#' + id).offset().top}, 'slow');
}


function hideVid(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function showVid(d) {
if(d.length < 1) { return; }
var i=1;
for(i=1;i<=3;i++){
	document.getElementById("jvid"+i).style.display = "none";
	document.getElementById("wvid"+i).style.display = "none";
}
document.getElementById(d).style.display = "block";
scrollFormTo(d);
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function addClass (element, newClass) { 
	if (element.className.indexOf(newClass) == -1)
		element.className += (' ' + newClass);
}
function removeClass (element, newClass) { 
	if (element.className.indexOf(newClass) != -1)
		element.className = element.className.replace 
			(new RegExp("\\s" + newClass), ''); 
}

function hideall(){
var i=1;
for(i=1;i<=3;i++){
	document.getElementById("jvid"+i).style.display = "none";
	document.getElementById("wvid"+i).style.display = "none";
}
}

function getRadioVal(radioName) { 
	var rads = document.getElementsByName(radioName);
	for(var rad in rads) {
		if(rads[rad].checked)
			return rads[rad].value;
	}
	return null;
}

