function visLength(str) {
  for (j=str.length; j>0; j--)
    if (str.charAt(j-1) != " ") return j;
  return 0;
}

/*
function validateForm
  Description: checks document.subscribe_form.username.value for valid email
               and document.subscribe_form.password.value for embedded spaces
  Uses: visLength
*/
function validateForm() {
	var errorString = "", invalid = 0;
	var usr = document.subscribe_form.username.value;
	var pwd = document.subscribe_form.password.value;
	if ((usr == "") || (usr == "undefined") || (visLength(usr) == 0)) {
		errorString = "Please type an email\n";
	    invalid = 1;
	} 
	else {
		if (usr.indexOf("@") == -1) {
			errorString = "E-Mail must contain a \'@\' sign\n";
			invalid = 1;
		}
		if (usr.indexOf(".") == -1) {
			errorString += "E-Mail must contain a period\n";
			invalid = 1;
		}
		if (usr.indexOf(" ") != -1) {
			errorString += "E-Mail must not contain embedded spaces\n";
			invalid = 1;
		}
	}
	if (pwd.indexOf(" ") != -1){
		errorString += "Password must not contain embedded spaces\n"
		invalid = 1;
	}
	if (invalid) 
		alert(errorString);
	else
		document.subscribe_form.submit(); 
}


/*
function removeSpace
  Removes any spaces from a string, but not any other 
  blank spaces like tabs or '\r'.
*/
function removeSpace(str) {
  newStr = str;
  for (j=0; j<str.length; j++) {
    if (str.charAt(j) == " ") newStr = newStr.substring(1);
    else  break;
  }
  for (j=newStr.length-1; j>0; j--) {
    if (newStr.charAt(j) == " ") newStr = newStr.substring(0,j);
    else break;
  }
  return newStr;
}



function goToNewRecord() {
    var errorString = "", invalid = 0;
    var usr = document.subscribe_form.username.value;
    var pwd = document.subscribe_form.password.value;
    if ((usr == "") || (usr == "undefined") || (visLength(usr) == 0)) {
        errorString = "Please type an email\n";
        invalid = 1;
    } 
    else {
        if (usr.indexOf("@") == -1) {
            errorString = "E-Mail must contain a \'@\' sign\n";
            invalid = 1;
        }
        if (usr.indexOf(".") == -1) {
            errorString += "E-Mail must contain a period\n";
            invalid = 1;
        }
        if (usr.indexOf(" ") != -1) {
            errorString += "E-Mail must not contain embedded spaces\n";
            invalid = 1;
        }
    }
    if (pwd.indexOf(" ") != -1){
        errorString += "Password must not contain embedded spaces\n"
        invalid = 1;
    }
    if (invalid) 
        alert(errorString);
    else {
        document.create_new_record.username.value = document.subscribe_form.username.value;
        document.create_new_record.password.value = document.subscribe_form.password.value;
        document.create_new_record.submit(); 
    }
}
