// Global Browser Sniffer Vars

var ns4 = (document.ids);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
// End Global Vars


NS4 = (document.layers) ? true : false;

function checkEnter(event) { 
    var code = 0;

    if (NS4){
	code = event.which;
    } else {
	code = event.keyCode;
	}
   
    if (code==13){
	form.change_password.submit();
	}
}


  

function toForm() {
    document.change_password.password_old.focus();
}


// Show container

function show(id){
    // Netscape 4
    if(ns4){
		document.ids[id].display = "block";
    }
    // Explorer 4
    else if(ie4){
		document.all[id].style.display = "block";
    }
    // W3C - Explorer 5+ and Netscape 6+
    else if(ie5 || ns6){
		document.getElementById(id).style.display = "block";
    }
}

// Hide Container
function hide(id){
    // Explorer 4
    if(ie4){
		document.all[id].style.display = "none";
    }
    // W3C - Explorer 5+ and Netscape 6+
    else if(ie5 || ns6){
		document.getElementById(id).style.display = "none";
    }
}


function clearContainer() {
    
    if (ie4 || ie5 || ns6) {
		document.getElementById("error_div").innerHTML = "<br>" ;
		return "";
    } else {
		return "";
    }
}
	
/////////////////////////////////////////////////////////////////////////////////////////////
//form validation functions 

function validate_reg (f) {
	//define/set global and local variables used by this function
	message = ""
	document.getElementById("error_div").innerHTML = "<br>";
	var CookieFlag = window.navigator.cookieEnabled;
		
	//first, loop thru these feilds to see if they are blank, contain nothing but a space, or contain only 1 character (except first name, which can be 1 character).  
	//if they are, call the function to add the appropriate line to the error message
	//if all fields are filled in, move on to check for specific problems w/ what the user entered
	for (var i = 0; i < f.length; i++)
	{
		var e = f.elements[i];
		if (e.type == "text") 
		{
			if ( (e.value == null) || 
				 (e.value == "") || 
				 (isBlank(e.value)) || 
				 (e.value.length < 2 && e.name != "first_names") )	//catches ppl who enter only one character for text fields (other than first name)
				 {
					addToErrorMsg (e.name);
					continue;
				 }
				
			//check to see if there's an @ symbol in any field other than email
			if ( (e.name != "email") && (e.value.indexOf("@") != -1 ) ) {
				addToErrorMsg (e.name);
			}
										
			//check the format of the email address
			if (e.name == "email") {
				emailCheck (e)
			}
				
			//if a user is from the US, make sure the zip is at least 5 numeric characters
			if ( (e.name == "ha_postal_code") && (f.ha_country_code.value == "us") ) {
				if ( (e.value.length < 5) || (isNaN(e.value)) ) {
					message	+= "&#149;  Please check the format of your postal code.<br>"
				}
			}
				
						
		}//end very first if
	}//end for
	
	//now start checking the other fields on the form (dropdowns and radio buttons)
		
	if ( (f.birthdate.value == '') || (f.birthdate.selectedIndex == 0)) {
		message += '&#149;  You did not select a date of birth.<br>'
	}	
	
	if ( (f.ha_country_code.value == '') || (f.ha_country_code.selectedIndex == 0)) {
		message += '&#149;  You did not select a Country.<br>'
	}	
	
	if ((f.sex[0].checked != true) && 
            (f.sex[1].checked != true)) {
        
    	message	+= '&#149;  You did not select a gender.<br>'
	
	}		
	
	//check to see if if the user allows cookies.
	if (CookieFlag == false) 
	{
		window.open('/network_reg/no_cookies.html','cookie_window','scrollbars=yes,resizable=yes','width=750,height=400');
		return;
	}	
	
	//check to see if the error message has changed; alert the user
	if (message != "") {
			errorText = "<b>There were errors with your registration form. Please correct the errors listed below and click the SIGN ME UP button at the bottom of the page.</b><br><br>";
			errorText = errorText.concat(message);
	   	 	document.getElementById("error_div").innerHTML = errorText ;
	    	show("error_div") ;
			
	} else {
		f.submit();
		// we are ready to submit the form
	}

}//end validate()	
	
	
function isBlank(string) {
  	for(var i = 0; i < string.length; i++) {
  		var c = string.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '')) return false;	
	}//end for	
	
	return true;
}//end isBlank
	
	
function addToErrorMsg (field) { 
	switch (field) {
		case "first_names": 
			message += "&#149; You did not enter your first name.<br>" 
			return;
		case "last_name": 
			message += "&#149;  You did not enter your last name.<br>"
			return;
		case "email": 
			message += "&#149;  You did not enter your e-mail address.<br>"
			return;
		//case "password": 
		//	message += "\n Please verify your password"
		//	return;
		//case "password_confirmation": 
		//	message += "\n Please verify your password confirmation"
		//	return;
		case "ha_postal_code": 
			message += "&#149;  You did not enter your zip/postal code.<br>"
			return;
		}//end switch
}//end addToErrorMsg()
  
  
function emailCheck (e) {
	//alert ('checking email');
	var hasInvalidChars_p = checkInvalidChars(e);
	var pos_at = e.value.indexOf("@");			//first occurance of @
	var pos_dot = e.value.indexOf (".");		//first occurance of .
	var last_at = e.value.lastIndexOf("@");		//last occurance of @
	var last_dot = e.value.lastIndexOf(".");	//last occurance of .
	
					
	if ((hasInvalidChars_p) ||					//make sure there are no invalid characters, including a space
		(pos_at == -1) || 						//make sure there's an @
		(pos_at != last_at) ||					//make sure there's only one @ in email
		(last_dot < pos_at) || 					//make sure there's a . after the @, also catches no .
		(last_dot == e.value.length - 1) || 	//make sure email doesnt end in .
		(pos_at == e.value.length - 1)  ||		//make sure email doesnt end in @
		(e.value.charAt(pos_at - 1) == ".") ||	//no . comes right before @
		(e.value.charAt(pos_at + 1) == ".")	||	//no . comes right after @
		(pos_at == 0) ||						//doesnt start w/ @
		(pos_dot == 0)							//doesnt start w/ .
	  	) {
			message += "&#149;  Please verify your e-mail address<br>(example e-mail: name@domain.com)<br>"
	} //end if
		  
	function checkInvalidChars (email) {
	  var invalidChars = " /:,;";
	  var count = 0;
	  for (i=0; i < invalidChars.length; i++){
	  	badChar = invalidChars.charAt(i);
		if ( email.value.indexOf(badChar,0) != -1 ) {
			count += 1;
		} //end if
	  }//end for
	  
	  if ( count > 0 ) {
	  	return true;
	  } else {
	  	return false;
	  }
	}//end checkInvalidChars()	
  
} //end checkEmail

 function validate_cu (f) { 
					
					message = ""
					document.getElementById("error_div").innerHTML = "";
					
					if (isBlank(f.password.value)) {
						message += "&#149;  Please enter a password.<br>";
					}
					emailCheck (f.email);
					
					if (message != "") {
						errorText = "<b>There were errors with your login form. Please correct the errors listed below and click the LOG IN button:</b><br>";
						errorText = errorText.concat(message);
				   	 	document.getElementById("error_div").innerHTML = errorText ;
				    	show("error_div") ;
					
					} else {
						f.submit();
						//alert("submit the form");
					}
			
				} 
//end form validation functions
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  

