// ************* start of profile functions *************
function goURL(){
	navigate("forward");
}
function navigate(direction)
{
	document.myaccount.action.value=direction;
	if(document.myaccount.profession && document.myaccount.otherProfession) {
		//The page being submitted is a profile change or new registration
		if(encourageParamedicInfo())
			document.myaccount.submit();
	} else {
		document.myaccount.submit();
	}
}

//given a name, search server for set of matching users
function validateEmail()
{
      var email = DWRUtil.getValue("email");
      // perform search and callback to 'doesExist'
      JEmail.doesEmailExist(doesEmailExist, trim(email));
}

function doesEmailExist(userID){
	if(userID != 0){
		alert("You probably already have an existing account! \nHelp yourself and us by reusing your existing account. \n\t-Login at the top of the page instead of creating a new account\n\t-If you are having trouble logging in use the forgot password link just under the login in blue\n\nor you can continue and create a new account");
	}

}

// given a name, search server for set of matching users
function validateUserName()
{
      var userName = DWRUtil.getValue("uName");
      // perform search and callback to 'doesExist'
      JUserName.doesExist(doesExist, trim(userName));
}

function doesExist(userID){
	if(document.myaccount.save!=null)
		document.myaccount.save.disabled=false;
	if(document.myaccount.next!=null)
		document.myaccount.next.disabled=false;
//	document.myaccount.save2.disabled=false;
	document.myaccount.uNameIcon.src="../images/check.gif";
	if(userID != 0){
		alert("Sorry, that user name is already taken. Please choose another one.");
		if(document.myaccount.save!=null)
			document.myaccount.save.disabled=true;
		if(document.myaccount.next!=null)
			document.myaccount.next.disabled=true;
		document.myaccount.uNameIcon.src="../images/error.png";
	}
	if(document.myaccount.userName.value.length < 6){
		alert("Sorry, that user name is too short. It must be at least 6 characters.");
		if(document.myaccount.save!=null)
    		document.myaccount.save.disabled=true;
		if(document.myaccount.next!=null)
			document.myaccount.next.disabled=true;
		document.myaccount.uNameIcon.src="../images/error.png";
	}
}

// encourages the input of the Paramedic-only fields for paramedics
function encourageParamedicInfo() {
	if(isParamedic() && paramedicFieldsEmpty())
		return !confirm("You have identified yourself as a paramedic. We suggest you fill out the fields under " +
					   "the For Paramedics Only heading. Click OK to go back and fill in this information, " +
					   "or click Cancel to submit your information anyway.");
		//Cancel submits the form, OK goes back
	else
		return true;
}

//returns true if one or more of the profession drop-down menus have a value starting with "EMS"
function isParamedic() {
	if(document.myaccount) {
		var profession = document.myaccount.profession;
		var professionValue = profession.options[profession.selectedIndex].text;
		if(professionValue.substring(0,3) == "EMS")
			return true;

		var otherProfession = document.myaccount.otherProfession;
		var otherProfessionValue = otherProfession.options[otherProfession.selectedIndex].text;
		if(otherProfessionValue.substring(0,3) == "EMS")
			return true;

		return false;
	}
}

//returns true if any of the fields under the For Paramedics Only section are empty or unchosen
function paramedicFieldsEmpty() {
	var pmState = document.myaccount.pmState;
	var pmStateValue = pmState.options[pmState.selectedIndex].text;
	if(pmStateValue == "Choose a State")
		return true;
	if(trim(document.myaccount.pmLicense.value) == "")
		return true;
	if(trim(document.myaccount.pmExpire.value) == "")
		return true;
	if(trim(document.myaccount.pmNremt.value) == "")
		return true;

	return false;
}

// removes leading and trailing blanks from the string
function trim(sString)
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);

	while (sString.substring(sString.length-1, sString.length) == ' ')
		sString = sString.substring(0,sString.length-1);

	return sString;
}


