// Script will run validation on teen registration

// Location Picker constants:
var US_ID = 1;
var PA_ID = 44;


var result = true;
var foc = null;
var msg = new Array();

valobj = new dynValid();
var locResult;

function valFirstName(s, form) {
	var fName = valobj.getObject(form, "txtFirstName");
	locResult = valobj.runRequiredText(fName, "First name");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = fName.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtFirstName", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valLastName(s, form) {
	var lName = valobj.getObject(form, "txtLastName");
	locResult = valobj.runRequiredText(lName, "Last name");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lName.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtLastName", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valEmail(s,form) {
	var emailbox = valobj.getObject(form, "txtEmail");
	locResult = valobj.runEmailText(emailbox, "Email address", true);
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = emailbox.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtEmail", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valCellphone(form) {
	// Cellphone is not required.  Check to see if they put any content into any of the
	// component textboxes
	var cellAC = valobj.getObject(form, "txtCellNumberAC");
	var cellIdx = valobj.getObject(form, "txtCellNumberIdx");
	var cellNum = valobj.getObject(form, "txtCellNumberNum");
	
	if (cellAC.value != "" || cellIdx.value != "" || cellNum.value != "") {
		// Run numeric check
		locResult = valobj.runNumericText(cellAC, "Cell Phone Area Code", false);
		if (locResult!=null) { 
			msg.push(locResult);
			locResult = false;
			if(!s){ if(foc==null){foc = cellAC.id;} }
		} else { locResult = true; }
		result = result && locResult;
		
		locResult = valobj.runNumericText(cellIdx, "Cell Phone first 3 digits", false);
		if (locResult!=null) { 
			msg.push(locResult);
			locResult = false;
			if(!s){ if(foc==null){foc = cellIdx.id;} }
		} else { locResult = true; }
		result = result && locResult;
		
		locResult = valobj.runNumericText(cellNum, "Cell Phone last 4 digits", false);
		if (locResult!=null) { 
			msg.push(locResult);
			locResult = false;
			if(!s){ if(foc==null){foc = cellNum.id;} }
		} else { locResult = true; }
		result = result && locResult;
	}
}

function valAge(s, form) {
	var selage = valobj.getObject(form, "cboAge");
	locResult = valobj.runSelectValid(selage, "Year you were born", 0);
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = selage.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("cboAge",locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valGrade(s, form) {
	var selgrade = valobj.getObject(form, "cboGrade");
	locResult = valobj.runSelectValid(selgrade, "Grade level", 0);
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = selgrade.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("cboGrade",locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valCounty(s, form) {
	var selcounty = valobj.getObject(form, "cboCounty");
	locResult = valobj.runSelectValid(selcounty, "County", 0);
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = selcounty.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("cboCounty",locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valZipCode(s, form) {
	
	// ONLY VALIDATE IF US
	var countryDrop = valobj.getObject(form,"cboCountry");
	if (countryDrop.options[countryDrop.selectedIndex].value == US_ID) {
		var zipbox = valobj.getObject(form,"txtZip");	
		locResult = valobj.runUSZipText(zipbox, "Zip Code", true, false);
		if (locResult!=null) {
			msg.push(locResult);
			locResult = false;
			if(!s){ if(foc==null){foc = zipbox.id;} }
		} else { locResult = true; }
		valobj.setScreenNotice("txtZip",locResult);
		if (!s) {
			result = result && locResult;
		}
	}
	
}

function valSchool(form) {
	locResult = true;
	
	// Test if this is a PA registration - if PA do check of school picker
	var stateDrop = valobj.getObject(form,"cboState");
	if (stateDrop.options[stateDrop.selectedIndex].value == PA_ID) {
		// Check the school dropdown for a selection; if not visible, 
		// Check for single-school value
		var singlePASchool = valobj.getObject(form,"schoolSingle");
		var PASchoolDrop = valobj.getObject(form,"cboSchool");
		noSchool = true;
		if (singlePASchool.display != "none") { noSchool = false; }
		else {
			inResult = valobj.runSelectValid(PASchoolDrop, "Your School", 0);
			if (inResult==null) { noSchool = false; }
		}
		if (noSchool) {
			msg.push('You have not selected a schoool');
			locResult = false;
		}
	} else {
		// Check to see if they filled in the school box
		var nonPASchool = valobj.getObject(form,"txtOtherSchool");
		locResult = valobj.runRequiredText(nonPASchool, "School Name");
		if (locResult!=null) { 
			msg.push(locResult);
			locResult = false; 
		} else { locResult = true; }
	}
	result = result && locResult;
}

function valUsername(s, form) {

	var uName = valobj.getObject(form, "txtUsername");
	// We alter the onscreen notice, so reset to defaults
	var uLabel = valobj.getObject(form, "val_txtUsername");
	uLabel.style.color = "#ff0000";
	uLabel.style.fontWeight = "normal";
	uLabel.innerText = "&laquo;";
	uLabel.style.display = "none";
	
	locResult = valobj.runRequiredText(uName, "Username");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = uName.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtUsername", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valPassword(s, form, doRetype,isNew) {
	
	var pwd1 = "";
	var pwd2 = "";
	
	// Check that they entered one
	var pWord = valobj.getObject(form, "txtPassword");
	pwd1 = pWord.value;
	if (isNew) {
		locResult = valobj.runRequiredText(pWord, "Password");
		if (locResult!=null) { 
			msg.push(locResult);
			locResult = false; 
			if(!s){ if(foc==null){foc = pWord.id;} }
		} else { locResult = true; }
		valobj.setScreenNotice("txtPassword", locResult);
		if (!s) {
			result = result && locResult;
		}
	}
	
	if (doRetype) {
		var pWord2 = valobj.getObject(form, "txtPassword2");
		pwd2 = pWord2.value;
		if (isNew) {
			// Check that they entered it again
			locResult = valobj.runRequiredText(pWord2, "Retyped Password");
			if (locResult!=null) { 
				msg.push(locResult);
				locResult = false; 
				if(!s){ if(foc==null){foc = pWord2.id;} }
			} else { locResult = true; }
			valobj.setScreenNotice("txtPassword2", locResult);
			if (!s) {
				result = result && locResult;
			}
		}
		
		if (locResult) {
			// Make sure they match
			if (pwd1 !== pwd2) {
				msg.push('Password and retyped password must match!');
				locResult = false; 
				if(!s){ if(foc==null){foc = pWord.id;} }
			} else { locResult = true; }
			valobj.setScreenNotice("txtPassword", 'Password and retyped password must match!');
			if (!s) {
				result = result && locResult;
			}
		}
	}
}

function runValidateTeen(form,isNew) {
	
	result = true;
	foc = null;
	msg = new Array();
	var button = (document.getElementById) ? document.getElementById("btnSubmit") : document.forms[form].elements["btnSubmit"];
	valobj.resetNotices(form);
	
	// 	validationElements
	valFirstName(false,form);
	valLastName(false,form);
	valEmail(false,form);
	valCellphone(form);
	valAge(false,form);
	valGrade(false,form);
	valZipCode(false,form);
	valCounty(false, form);
	valSchool(form);
	valUsername(false,form);
	valPassword(false,form,true,isNew);
	
	
	alertstxt = valobj.getMessages(msg);
	
	if (alertstxt!="") { alert(alertstxt); }
	if (foc!=null) { if(document.all){document.all(foc).focus()} else {document.getElementById(foc).focus();} }
	if (!result)
		button.disabled = false;
	else {
		document.forms[form].submit();	
	}
}


/*********************************************************/

function checkAge(sel,div) {
	
	thisSpan = document.getElementById(div);
	if ( thisSpan.hasChildNodes() ) 
		thisSpan.removeChild(thisSpan.childNodes[0]);
	curVal = sel.options[sel.selectedIndex].value;
	if ( curVal != "") {
		if ( curVal < 13 ) {
			txtNode = document.createTextNode("Thanks for connecting to nrg! We can't collect your info online until you're 13, but you can still take part in the nrg cause and improve nutrition and physical activity choices with your friends. Have fun!");
			thisSpan.appendChild(txtNode);
		}
	}
}

function getCountyYMCA(ajaxPath, isCountyChange) {
	
	cbox = document.getElementById('cbxYMCA');
	countyDrop = document.getElementById('cboCounty');
	stateDrop = document.getElementById('cboState');
	ymcaDrop = document.getElementById('cboYMCAName');
	ymcaSpan = document.getElementById('YMCANames');
	
	ymcaDrop.disabled = false;
	ymcaSpan.style.display = "inline";
	// Only PA counties will run this function
	if (stateDrop.options[stateDrop.selectedIndex].value == 44) {
		if (cbox.checked) {
			// We get YMCA list from county dropdown
			// Get the currently selected CountyID
			countyID = countyDrop.options[countyDrop.selectedIndex].value;
			if ( countyID != "") {
				// Do an ajax call to get the YMCAs for this county if not populated
				if (ymcaDrop.options.length == 0 || isCountyChange) {
					path = ajaxPath + 'ymca.php?county=' + countyID;
					runAjaxCall(path,'postCountyYMCA',null,null);
				}
			}
		} else {
			ymcaDrop.disabled = true;	
		}
	} else {
		// Out of state, hide box
		ymcaSpan.style.display = "none";
	}
}

function postCountyYMCA(str) {
	ymcaDrop = document.getElementById('cboYMCAName');

	if (ymcaDrop.options.length>1)
		ymcaDrop.options.length = 0;
	
	if (str.length > 0) {
		var oAry = str.split('|');
		if (oAry.length > 0) {
			for(o = 0;o < oAry.length; o++)
			{
				iAry = oAry[o].split('~');
				opt = document.createElement('option');
				opt.value = iAry[0]; opt.text = iAry[1];
				ymcaDrop.options[ymcaDrop.options.length] = opt;
				
			}
		}
	}
}

function getTestUsername(ajaxPath) {
	// Verify that this username is available
	fName = document.getElementById('txtFirstName').value;
	lName = document.getElementById('txtLastName').value;
	uName = document.getElementById('txtUsername').value;
	
	if ( uName != "" ) {
		path = ajaxPath + 'userCheck.php?uname=' + uName + '&fname=' + fName + '&lname=' + lName;
		runAjaxCall(path,'postTestUsername',null,null);
	}
}

function postTestUsername(str) {
	// Returned string is empty if OK, else fill in with suggested value
	uLabel = document.getElementById('val_txtUsername');
	uText = document.getElementById('txtUsername');
	if (str == "") {
		// Get the warning label for the field, and "dress" as OK
		uLabel.style.color = "#00cc00";
		uLabel.style.fontWeight = "bold";
		addText = document.createTextNode("Username OK");
		if (uLabel.hasChildNodes()) { uLabel.removeChild(uLabel.childNodes[0]); }
		uLabel.appendChild(addText);
	} else {
		uLabel.style.color = "#cc00000";
		uLabel.style.fontWeight = "bold";
		addText = document.createTextNode("Username has been selected.  A suggested username has been entered.");
		if (uLabel.hasChildNodes()) { uLabel.removeChild(uLabel.childNodes[0]); }
		uLabel.appendChild(addText);
		uText.value = str;
	}
	uLabel.style.display = "inline";
}

function getPBCLocationDropDown(ajaxPath) {
	// Set the locations based on county
	countyDropDown = document.getElementById('cboCounty');
	countyID = countyDropDown.options[countyDropDown.selectedIndex].value;

	if ( countyID > 0 ) {
		path = ajaxPath + 'pbcLocations.php?county=' + countyID;
		runAjaxCall(path,'postPBCLocationDropDown',null,null);
	}
}

function postPBCLocationDropDown(str) {
	locationsDrop = document.getElementById('cboPBCLocation');

	locationsDrop.options.length = 0;
	
	if (str.length > 0) {
		var oAry = str.split('|');
		if (oAry.length > 0) {
			for(o = 0;o < oAry.length; o++)
			{
				iAry = oAry[o].split('~');
				opt = document.createElement('option');
				opt.value = iAry[0]; opt.text = iAry[1];
				locationsDrop.options[locationsDrop.options.length] = opt;
				
			}
		}
	}
}