

/////////////////////////////////////////////////////////
// getSelectedIndex(selectbox) function:
//document.form.selectboxname.options[getSelectedIndex(document.form.selectboxname)].value
/////////////////////////////////////////////////////////
function getSelectedIndex(selectbox) {
   return selectbox.selectedIndex;
}

/////////////////////////////////////////////////////////
// valueofSelectedRadio(radio_array) function:
// It accepts, as a parameter, a reference to a Radio object
// array, and returns answer = 0 if none of them is checked,
// else it returns the value of the radio button that is checked. 
/////////////////////////////////////////////////////////
function valueofSelectedRadio(radio_array) {

	answer = 0;
	var i;
	for (i = 0; i < radio_array.length; ++ i)

		if (radio_array[i].checked)
			answer = radio_array[i].value;

	return answer;
}

function check_login() {
//make sure the login and pwd fields are filled out
// if not, send message, if yes, submit form 
	var fixThis = "";
	if ((document.welcome.login.value=='') || (document.welcome.pwd.value=='')) {
		fixThis = "Please enter login and password";
		alert(fixThis);
		return false;
	}
	document.welcome.what_to_do.value='login';
	document.welcome.submit();
}

function logout(){
	document.welcome.what_to_do.value='logout'; 
	document.welcome.submit();
}

function submit_subscription(){
	// verify that all personal info fields are completed
	if (document.subscription.email.value=="") {
		alert("Please make sure you enter your email address.");
		return false;
	}
	
	if (!isAnEmail(document.subscription.email.value)) {
		alert("Please make sure you enter a valid email address.");
		document.subscription.email.value="";
		return false;
	}

	if (document.subscription.phone_no.value=="") {
		alert("Please make sure you enter your phone number.");
		return false;
	}
	if (document.subscription.first_name.value=="") {
		alert("Please make sure you enter your first name.");
		return false;
	}
	
	if (document.subscription.last_name.value=="") {
		alert("Please make sure you enter your last name.");
		return false;
	}
	
	if (document.subscription.pwd.value=="") {
		alert("Please make sure you enter a password.");
		return false
	}
	
	if (!isAlphaorNumber(document.subscription.pwd.value)) {
		alert("Please make sure your password only consists of letters and numbers, no special characters.");
		document.subscription.pwd.value="";
		return false;
	}

	document.subscription.what_to_do.value='subscribe'; 
	document.subscription.submit();
}

function submit_profile(){
	// verify that all personal info fields are completed
	if (document.profile.email.value=="") {
		alert("Please make sure you enter your email address.");
		return false;
	}
	
	if (!isAnEmail(document.profile.email.value)) {
		alert("Please make sure you enter a valid email address.");
		document.profile.email.value="";
		return false;
	}

	if (document.profile.phone_no.value=="") {
		alert("Please make sure you enter your phone number.");
		return false;
	}
	if (document.profile.first_name.value=="") {
		alert("Please make sure you enter your first name.");
		return false;
	}
	
	if (document.profile.last_name.value=="") {
		alert("Please make sure you enter your last name.");
		return false;
	}

	if (document.profile.pwd.value=="") {
		alert("Please make sure you enter a password.");
		return false
	}
	
	if (!isAlphaorNumber(document.profile.pwd.value)) {
		alert("Please make sure your password only consists of letters and numbers, no special characters.");
		document.profile.pwd.value="";
		return false;
	}

	document.profile.what_to_do.value='modify_profile'; 
	document.profile.submit();
}




/////////////////////////////////////////////////////////
// isAlpha(entry) function:
// Determines whether or not the entry is btw a/a or A/Z
/////////////////////////////////////////////////////////
function isAlphaorNumber(entry){

	answer = 1; //Assume entry is valid

  for (var i=0; i<entry.length; i++) {
	if (entry.charAt(i)!='a' && entry.charAt(i)!='A' && entry.charAt(i)!='b' && entry.charAt(i)!='B'
		&& entry.charAt(i)!='c' && entry.charAt(i)!='C' && entry.charAt(i)!='d' && entry.charAt(i)!='D'
		&& entry.charAt(i)!='e' && entry.charAt(i)!='E' && entry.charAt(i)!='f' && entry.charAt(i)!='F'
		&& entry.charAt(i)!='g' && entry.charAt(i)!='G' && entry.charAt(i)!='h' && entry.charAt(i)!='H'
		&& entry.charAt(i)!='i' && entry.charAt(i)!='I' && entry.charAt(i)!='j' && entry.charAt(i)!='J'
		&& entry.charAt(i)!='k' && entry.charAt(i)!='K' && entry.charAt(i)!='l' && entry.charAt(i)!='L'
		&& entry.charAt(i)!='m' && entry.charAt(i)!='M' && entry.charAt(i)!='n' && entry.charAt(i)!='N'
		&& entry.charAt(i)!='o' && entry.charAt(i)!='O' && entry.charAt(i)!='p' && entry.charAt(i)!='P'
		&& entry.charAt(i)!='q' && entry.charAt(i)!='Q' && entry.charAt(i)!='r' && entry.charAt(i)!='R'
		&& entry.charAt(i)!='s' && entry.charAt(i)!='S' && entry.charAt(i)!='t' && entry.charAt(i)!='T'
		&& entry.charAt(i)!='u' && entry.charAt(i)!='U' && entry.charAt(i)!='v' && entry.charAt(i)!='V'
		&& entry.charAt(i)!='w' && entry.charAt(i)!='W' && entry.charAt(i)!='x' && entry.charAt(i)!='X'
		&& entry.charAt(i)!='y' && entry.charAt(i)!='Y' && entry.charAt(i)!='z' && entry.charAt(i)!='Z'
		&& entry.charAt(i)!='0' && entry.charAt(i)!='1' && entry.charAt(i)!='2' && entry.charAt(i)!='3'
		&& entry.charAt(i)!='4' && entry.charAt(i)!='5' && entry.charAt(i)!='6' && entry.charAt(i)!='7'
		&& entry.charAt(i)!='8' && entry.charAt(i)!='9') {

			answer = 0;
			break;
	}
  }
	return answer;

}


////////////////////////////////////////////////////////////////////////
// isanemail() function:
// determines whether or not an entry is a valid email address
////////////////////////////////////////////////////////////////////////

function isAnEmail(entry) {

  var answer = 0;
  // assume address is not a valid email address
  var char1 = "@";
  var char1_position;
  var char2 = ".";
  var char2_position;

  if ((character_exists(entry, "@"))
                && (character_exists(entry, "."))){
  //first check that @ and . are in the string

                for (var i=0; i<entry.length; i++) {
                //find the position of @ and of . in the string
                if (entry.charAt(i) == char1) {
                                char1_position = i;
                        }
                if (entry.charAt(i) == char2) {
                                char2_position = i;
                        }
                }

                if ((char1_position < char2_position)
                        //check that @ comes before . in string
                        && (!(character_exists(entry, ",")))
                        && (!(character_exists(entry, ":")))
                        && (!(character_exists(entry, ";")))
                        && (!(character_exists(entry, "'")))
                        && (!(character_exists(entry, '"')))
                        && (!(character_exists(entry, " ")))){

                                answer = 1;
                }

  }//end if

return answer;

}

////////////////////////////////////////////////////////////////////////
// character_exists() function:
// determines whether or not a specific character exists in a character
// string
////////////////////////////////////////////////////////////////////////

function character_exists(entry, which_char) {

  var char_exists = 0;
  // assume character does not exist

    for (var i=0; i<entry.length; i++) {

      if (entry.charAt(i) == which_char) {
        char_exists = 1;
                break;
      }
    }
        
  return char_exists;
}
	