// 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 valTitle(s, form) {
	var lTitle = valobj.getObject(form, "txtTitle");
	locResult = valobj.runRequiredText(lTitle, "Title");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lTitle.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtTitle", 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 valPhoneElements(form,elem) {
	// Cellphone is not required.  Check to see if they put any content into any of the
	// component textboxes
	var phoneAC = valobj.getObject(form, "txtPhoneNumberAC");
	var phoneIdx = valobj.getObject(form, "txtPhoneNumberIdx");
	var phoneNum = valobj.getObject(form, "txtPhoneNumberNum");
	
	for(x=0;x<elem+1;x++) {
		if (x == 0) {		
			locResult = valobj.runNumericText(phoneAC, "Phone Area Code", true);
			if (locResult!=null) { 
				msg.push(locResult);
				locResult = false;
			} else { locResult = true; }
			result = result && locResult;
		}
		
		if (x == 1) {	
			locResult = valobj.runNumericText(phoneIdx, "Phone first 3 digits", true);
			if (locResult!=null) { 
				msg.push(locResult);
				locResult = false;
			} else { locResult = true; }
			result = result && locResult;
		}
		
		if (x == 2) {	
			locResult = valobj.runNumericText(phoneNum, "Phone last 4 digits", true);
			if (locResult!=null) { 
				msg.push(locResult);
				locResult = false;
			} else { locResult = true; }
			result = result && locResult;
		}
	}
}

function valAdvisorCommunityGroup(s, form) {
	var lLoc = valobj.getObject(form, "txtAdvisorCommunityGroup");
	locResult = valobj.runRequiredText(lLoc, "Community Organization");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lLoc.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtAdvisorCommunityGroup", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valPBCLocation(s, form) {
	var lLoc = valobj.getObject(form, "txtPBCLocation");
	locResult = valobj.runRequiredText(lLoc, "Powered By Choice Group Location");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lLoc.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtPBCLocation", 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 valAddress1(s, form) {
	var lStreet1 = valobj.getObject(form, "txtStreetAddress1");
	locResult = valobj.runRequiredText(lStreet1, "Street Address");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lStreet1.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtStreetAddress1", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valCity(s, form) {
	var lCity = valobj.getObject(form, "txtCity");
	locResult = valobj.runRequiredText(lCity, "City");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = lCity.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtCity", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valZip(s, form) {
	
	var zipbox = valobj.getObject(form,"txtZipcode");	
	locResult = valobj.runRequiredText(zipbox, "Zip Code");
	if (locResult!=null) {
		msg.push(locResult);
		locResult = false;
		if(!s){ if(foc==null){foc = zipbox.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("txtZipcode",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 verifyAdvisorMode() {
	
	schoolAdv = document.getElementById('cbxSchoolAdvisor');
	commAdv = document.getElementById('cbxCommunityAdvisor');
	
	if (!schoolAdv.checked && !commAdv.checked) {
		msg.push('You must select to advise in a school or community setting!');
		result = result && false;
	}
	
}

function runValidateAdvisor(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
	schoolAdv = valobj.getObject(form, "cbxSchoolAdvisor");
	commAdv = valobj.getObject(form, "cbxCommunityAdvisor");
	
	valFirstName(false, form);
	valLastName(false, form);
	valTitle(false, form);
	valEmail(false,form);
	verifyAdvisorMode();
	if (schoolAdv.checked)
		valSchool(form);
	if (commAdv.checked)
		valAdvisorCommunityGroup(false, form);
	valPBCLocation(false, form);
	valCounty(false, form);
	valAddress1(false, form);
	valCity(false, form);
	valZip(false, form);
	valPhoneElements(form,2);
	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 setAdvisor(form) {
	// Added: Hide clubs section if selecting Community
	schAdvCbx = valobj.getObject(form,"cbxSchoolAdvisor");
	comAdvCbx = valobj.getObject(form,"cbxCommunityAdvisor");
	schAdvDiv = valobj.getObject(form,"schoolAdvisorDiv");
	comAdvDiv = valobj.getObject(form,"communityAdvisorDiv");
	// CLubs section
	clubsDiv = valobj.getObject(form,"advisorSchoolClubs");
	
	if (schAdvCbx.checked) {
		schAdvDiv.style.display = "block";
		clubsDiv.style.display = "block";
	} else {
		schAdvDiv.style.display = "none";
		clubsDiv.style.display = "none";
	}
	
	if (comAdvCbx.checked) {
		comAdvDiv.style.display = "block";
		if (!schAdvCbx.checked) { clubsDiv.style.display = "none"; }
	} else {
		comAdvDiv.style.display = "none";
	}
}

function toggleOrgDetail(form,orgID) {
	orgCbx = valobj.getObject(form,"org_" + orgID);
	orgTxt = valobj.getObject(form,"org_name_" + orgID);
	
	orgTxt.disabled = ( !orgCbx.checked );
}


function setAdvisorPhone(form) {
	
	var selcountry = valobj.getObject(form, "cboCountry");
	var phone = valobj.getObject(form, "phoneNumberDiv");
	
	if ( selcountry.options[selcountry.selectedIndex].value == US_ID )
		phone.style.display = "block";
	else 
		phone.style.display = "none";
}

function getCountyYMCA(ajaxPath, isCountyChange) {
	
	if(document.getElementById('cbxCommunityAdvisor').checked) {	
		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";
}