var touched_username = 0;
var touched_password = 0;

var numbers = "0123456789";
var bad_chars = "'`\"\\/:; ";
var bad_chars_no_space = "'`\"\\/:;";
var bad_name_chars = "`\"\\/:;~!@#$%^&*()+={}[]|<>/?,";
var bad_login_name_chars = bad_name_chars + ".";
var bad_name_chars_numbers = bad_name_chars + numbers;
var bad_email_chars = "`\"\\/ (){}[]|<>/,&+=*'%?!~#^:;";
var bad_username_chars = bad_chars + "~!@#$%^&*()-+={}[]|<>,./?\ ";
var bad_password_chars = bad_username_chars;

function check_string(s, bad_chars)
{
	var i;
	var found = -1;
   	
	for (i = 0; i < bad_chars.length; i++)
   	{
  		found = s.indexOf(bad_chars.charAt(i));
   		if (found > -1)
   			break;
   	}

	return 	found;w
}

function check_trim(inputstring)
{
	if ( (inputstring.indexOf(" ") >= 0) && (inputstring.indexOf(" ") != inputstring.length) ) 
		return true; 
	return false ;
}

function middlenamecheck(thisform)
{
	if(thisform.t_middlename.value != "")
	{
		var i;
   		var currChar;
   		var len = thisform.t_middlename.value.length;
   		for(i = 0; i < len; i++)
   		{
   			currChar = thisform.t_middlename.value.charAt(i);
			if((currChar < "a" || currChar > "z") && (currChar < "A" || currChar > "Z") && isNaN(currChar) && currChar != "." && currChar != " " && currChar != "'")
			{
				alert("Please use only letters, numbers and dots in your middle name");
				thisform.t_middlename.focus();
				return false;
			} 
   		}
	}
	return true;
}

function firstnamecheck(thisform){
	if (thisform.t_firstname.value.length == 0){
		alert("Please enter First Name.");
		thisform.t_firstname.focus(); 
		return false;
	}
	
	/*if ( check_string( thisform.t_firstname.value, bad_name_chars ) > -1 ){
		alert( "Please remove any special characters from first name!" )
		thisform.t_firstname.focus(); 
		return false;
		}*/
	var i;
   	var currChar;
   	var len = thisform.t_firstname.value.length;
   	for(i = 0; i < len; i++)
   	{
   		currChar = thisform.t_firstname.value.charAt(i);
		if((currChar < "a" || currChar > "z") && (currChar < "A" || currChar > "Z") && isNaN(currChar) && currChar != "." && currChar != " " && currChar != "'" && currChar != "-")
		{
			alert("Please use only letters, numbers and dots in your first name");
			thisform.t_firstname.focus();
			return false;
		} 
   	}
	return middlenamecheck(thisform); 
}
	
function lastnamecheck(thisform){
if ( thisform.t_lastname.value.length == 0) {
	alert("Please enter Last Name.");
	thisform.t_lastname.focus();
	return false;
	}  
	/*if ( check_string( thisform.t_lastname.value, bad_name_chars ) > -1 ){
		alert( "Please remove any special characters from last name!" )
		thisform.t_lastname.focus(); 
		return false;
		}*/

	var i;
   	var currChar;
   	var len = thisform.t_lastname.value.length;
   	for(i = 0; i < len; i++)
   	{
   		currChar = thisform.t_lastname.value.charAt(i);
		if((currChar < "a" || currChar > "z") && (currChar < "A" || currChar > "Z") && isNaN(currChar) && currChar != "." && currChar != " " && currChar != "'" && currChar != "-")
		{
			alert("Please use only letters, numbers and dots in your last name");
			thisform.t_lastname.focus();
			return false;
		} 
   	}
	return true;
}
	
/* This is, at best, a perfunctory test of email address 
    entry validity.  Is there an "@" sign and sufficient
    letters prefixing it. Alert user if questioned. */ 
	
	
function emailcheck(thisform) {
   
   // RS0131 
   // check if user has entered an email address at all
   if (thisform.t_email_external.value == 0) {
	alert("Please enter Email address");
	thisform.t_email_external.focus();
	return false;
   }
   // check for beginining/trailing spaces
   // FOR FUTURE: this function could extract the actual email address from the value entered
   // by the user and eliminate the beginning and trailing spaces.

/*   if (check_trim(thisform.t_email_external.value)==true ) {
        alert("This email address has some spaces in between, please remove them");
        thisform.t_email_external.focus();
	return false;
   } */

   // check for spl characters that are invalid
   if (check_string(thisform.t_email_external.value, bad_email_chars) > -1)
   {
	alert("Please remove all spl. chrs from email address")
	thisform.t_email_external.focus();
	return false;
   }



   // check for valid email address
   
   var x = thisform.t_email_external.value.toLowerCase();
   var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

   var invalidEmails = new Array ("unknown@unknown.com","none@none.com","noemail@aol.com", "noemail@yahoo.com", "noemail@hotmail.com", "noemail@netzero.com", "noemail@juno.com", "noemail@netscape.com", "noemail@prodegy.net", "noemail@msn.net");
   var isEmpty = false ;
   if(x == "" || x == null) { isEmpty = true; }
   
   if(isEmpty)
   {
	alert('Please enter a value for the email address field.');
	thisform.t_email_external.focus();
	return false;
   }

   if(!isEmpty && !filter.test(x))
   {
	alert('Please enter a valid email address');
	thisform.t_email_external.focus();
	return false;
   }
   
   for(i=0; i<invalidEmails.length; i++)
   {
	if(invalidEmails[i] == x)
   	{
		alert('Please enter a valid email address');
		thisform.t_email_external.focus();
		return false;
   	}
   }	


   // end of valid email address		




 
   // Check for an @ sign
   var at_sign = thisform.t_email_external.value.indexOf("@");
   if (at_sign < 0) 
   {
	var msg = "Oops!  You forgot an '@' sign in your e-mail address.  Please enter an address such as ";
	msg = msg + thisform.t_email_external.value + "@Military.com";

	alert(msg);
	thisform.t_email_external.focus();
	return false;
   }

   // Check for a domain
   var dot = thisform.t_email_external.value.substring(at_sign, thisform.t_email_external.value.length + 1).indexOf(".");
   if (dot < 0) 
   {
	var msg = "Oops, you forgot a '.' following the @ sign in your e-mail address.  Please enter an address such as ";
	msg = msg + thisform.t_email_external.value + ".com";
				
	alert(msg);
	thisform.t_email_external.focus();
	return false;
   }
   // most probably wont reach here as check_string will scream
   // Check for multiple addresses
   if (thisform.t_email_external.value.indexOf(",") != -1) 
   {
	var msg = "Sorry.  You can't enter multiple e-mail addresses.  Please remove the comma (,) from your e-mail address. ";
	alert(msg);
	thisform.t_email_external.focus();
	return false;
   }

   // Check domain to have .edu, .com, .gov, .net, .org, or any 2 country letters at the end
  /* var slength = thisform.t_email_external.value.length;
   var strlower = thisform.t_email_external.value.toLowerCase();
	
   var edu = strlower.indexOf(".edu",slength-4);
   var com = strlower.indexOf(".com",slength-4);
   var org = strlower.indexOf(".org",slength-4);
   var net = strlower.indexOf(".net",slength-4);
   var gov = strlower.indexOf(".gov",slength-4);
   var mil = strlower.indexOf(".mil",slength-4);

   var other = thisform.t_email_external.value.charAt(slength-3).indexOf(".");
   var email_chars = bad_username_chars + "1234567890";

   var tmp = check_string(thisform.t_email_external.value.substring(slength-2, slength), email_chars);

   if ((edu == -1) && (com == -1) && (org == -1) && (net == -1) && (mil == -1) && (gov == -1) 
          && ((other == -1) || (tmp > -1)) ) 
   {
	var msg = "It appears that you have mistyped your email address. There is no .gov, .mil, .org, .com, .net, .edu, or any other country code at the end of your address.";

	alert(msg);
	thisform.t_email_external.focus();
	return false;
   }*/
  
   return true;

}
 
function emailretypecheck(thisform) 
{
   // RS0131 
   // check if user has retyped the email address at all
   if (thisform.t_email_external_retype.value == 0) {
	alert("Please retype Email address for confirmation");
	thisform.t_email_external_retype.focus();
	return false;
   }

   return true;
}
 
function compareemail(thisform) {
   if ( thisform.t_email_external.value != thisform.t_email_external_retype.value ) 
   {
	alert("Your current E-mail Address does not match the retyped E-mail address.");
	thisform.t_email_external_retype.focus();
	return false;
   } 
    return true; 
}
	
function loginnamecheck(thisform)
{
   if ( thisform.t_login_name.value.length == 0 )  
   {
	alert("Please enter Desired Member Name.");
	thisform.t_login_name.focus(); 
	return false;
   }
   
   if (check_trim(thisform.t_login_name.value)==true )
   {
	alert("Please remove the spaces in the Desired Member Name");
        thisform.t_login_name.focus();
        return false;
   }

   /* if ( check_string( thisform.t_login_name.value, bad_login_name_chars ) > -1 )
   {
	alert( "Please remove any special characters from desired login name!" )
	thisform.t_login_name.focus(); 
	return false;
   } */
   var i;
   var currChar;
   var len = thisform.t_login_name.value.length;
   for(i = 0; i < len; i++)
   {
   	currChar = thisform.t_login_name.value.charAt(i);
	if((currChar < "a" || currChar > "z") && (currChar < "A" || currChar > "Z") && isNaN(currChar) && currChar != "_")
	{
		alert("Please use only letters, numbers and underscores in the login name");
		thisform.t_login_name.focus();
		return false;
	} 
   }
    
   return true; 
}
	
function passwordcheck(thisobject) {

	var len=thisobject.value.length;
	var currChar;

	if ( thisobject.value.length == 0 )
	{
		alert("Please type in a password");
		thisobject.focus(); 
		return false;
	}  
	if ( thisobject.value.length < 4 )
	{
		alert("Password should be more than 3 characters");
		thisobject.focus(); 
		return false;
	}  
        if (check_trim(thisobject.value)==true)
	{
		alert("Please choose a password without spaces");
                thisobject.focus(); 
		return false;
	}  
	
	for (var i=0; i < len; i++)
	{
		currChar = thisobject.value.charAt(i);
		if((currChar < "a" || currChar > "z") && (currChar < "A" || currChar > "Z") && isNaN(currChar) && currChar != "_")
		{
			alert("Please use only letters, numbers and underscores in your password");
			thisobject.focus();
			return false;
		} 
	}
	return true; 
}
	
function repasswordcheck(thatobject){
if ( thatobject.value.length == 0 ) {
	alert("Please retype password for confirmation");
	thatobject.focus(); 
	return false;
	}  
	return true; }

function comparepwdcheck(thisobject, thatobject){	
if ( thisobject.value.toUpperCase() != thatobject.value.toUpperCase() ) {
	alert("Password does not match retyped password .");
	thisobject.focus();
	return false;
	}  
	return true; }
	
function gendercheck(thisform) {		
		var genderM = "";		
		var genderF = "";		
		genderM = thisform.rb_gender[0].checked;
		genderF = thisform.rb_gender[1].checked;
		if(genderM==false && genderF==false) {
			alert("Please choose a Gender.");
			thisform.rb_gender[0].focus();
			return false;	
		}
		return true;
	 }

function birthmonthcheck(thisform){
if ( thisform.lb_birthmonth.selectedIndex == 0 ) {
	alert("Please select birth month.");
	thisform.lb_birthmonth.focus();
	return false;
	}
	return true; }
	
function birthdaycheck(thisform){
if ( thisform.lb_birthday.selectedIndex == 0 ) {
	alert("Please select birth date.");
	thisform.lb_birthday.focus();
	return false;
	}
	return true; }
	
function birthyearcheck(thisform){
if ( thisform.lb_birthyear.selectedIndex == 0 ) {
	alert("Please select birth year.");
	thisform.lb_birthyear.focus();
	return false;
	}
	return true; }

function datecheck(thisform) { 
	var myDayStr = "";
	var myMonthStr = "";
	var myYearStr = "";
	var myDateStr = "";
	var myDate_string = "";
	var myDate_array = "";
	
	var birthMonthIndex = thisform.lb_birthmonth.selectedIndex;
   	var birthDayIndex   = thisform.lb_birthday.selectedIndex;
   	var birthYearIndex  = thisform.lb_birthyear.selectedIndex;
   	myDayStr   = thisform.lb_birthday.options[birthDayIndex].value;
	myMonthStr   = thisform.lb_birthmonth.options[birthMonthIndex].value;	
	myYearStr   = thisform.lb_birthyear.options[birthYearIndex].value;	 
	//myDayStr = thisform.lb_birthday.value;
	//myMonthStr = thisform.lb_birthmonth.value;
	//myYearStr = thisform.lb_birthyear.value;
	
	myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;	
		
	myDate = new Date( myDateStr );
	myDate_string = myDate.toGMTString();	
	myDate_array = myDate_string.split( ' ' );	
	
	if ( myDate_array[2] != myMonthStr ) {
	  alert( myDateStr + " is not a valid date." );
          thisform.lb_birthmonth.focus();
	  return false;
	} 
	
	return true;
	 
}

function birthdatecheck(thisform)
{
	if(thisform.lb_birthmonth.selectedIndex != 0 || thisform.lb_birthday.selectedIndex != 0 || thisform.lb_birthyear.selectedIndex != 0)
	{
		if(thisform.lb_birthmonth.selectedIndex == 0 || thisform.lb_birthday.selectedIndex == 0 || thisform.lb_birthyear.selectedIndex == 0)
		{
			alert("Please select a birthmonth, birthdate and birthyear");
			thisform.lb_birthmonth.focus();
			return false;
		}
		else
		{
			return datecheck(thisform);
		}		
	}
	return true;
}



function zipcheck(thisform){
if ( thisform.t_zip.value.length == 0 ) {
	alert("Please enter Zip.");
	thisform.t_zip.focus();
	return false;
	}
   return true; }
   
function servicecodecheck(thisform){
	if ( thisform.lb_service_code.selectedIndex == 0 ) {
	alert("Please select service code");
	thisform.lb_service_code.focus();
	return false;
	}
	return true; }

function militarystatuscheck(thisform){
	if ( thisform.lb_current_military_status.selectedIndex == 0 ) {
	alert("Please select military status");
	thisform.lb_current_military_status.focus();
	return false;
	}
	return true; }
	
function hearaboutus(thisform){
	if ( thisform.lb_hear_about_us.selectedIndex == 0 ) {
	alert("Please specify how you heard about us");
	thisform.lb_hear_about_us.focus();
	return false;
	}
	return true; 
}



function selfdescriptioncheck(thisform) {
	if (thisform.t_self_description.value.length > 50)
	{
		alert("Self Description must be less than 50 characters in length.");
		return false;
	}
}

function validate_t_photo(thiselement)
{
	if (thiselement.value.length <= 0)
	{
		alert("Please select an image to upload.");
		return false;
	}
	return true;
}


