// 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 valDescription(s, form) {
	var pDesc = valobj.getObject(form, "photo_description");
	locResult = valobj.runRequiredText(pDesc, "Photo Description");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = pDesc.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("photo_description", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valName(s, form) {
	var fName = valobj.getObject(form, "name");
	locResult = valobj.runRequiredText(fName, "Name");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = fName.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("name", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function valEmail(s,form) {
	var emailbox = valobj.getObject(form, "email");
	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("email", 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 valPictureFile(s, form) {
	var fName = valobj.getObject(form, "userfile");
	locResult = valobj.runRequiredText(fName, "Picture Submission");
	if (locResult!=null) { 
		msg.push(locResult);
		locResult = false; 
		if(!s){ if(foc==null){foc = fName.id;} }
	} else { locResult = true; }
	valobj.setScreenNotice("userfile", locResult);
	if (!s) {
		result = result && locResult;
	}
}

function runValidatePictureContest(form) {
	
	result = true;
	foc = null;
	msg = new Array();
	//var button = (document.getElementById) ? document.getElementById("btnSubmit") : document.forms[form].elements["btnSubmit"];
	valobj.resetNotices(form);
	
	// 	validationElements
	valName(false, form)
	valEmail(false, form)
	valCounty(false, form)
	valZipCode(false, form)
	valSchool(form)
	valDescription(false, form)
	valPictureFile(false, form)
	
	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;
				
			}
		}
	}
}

function CheckFieldLength(fn,wn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}