var _ms_XMLHttpRequest_ActiveX = ""; // Holds type of ActiveX to instantiate

var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	
var AJAX_obj;

var username_available = 0;
var band_name_available = 0;
var got_check_result = 0;

function issue_query(url, call_back, name) 
{
	if (name) // Only do the check if the user has entered a value into the edit box.
	{
		if (window.XMLHttpRequest) 	// Non I.E. browsers
		{
			AJAX_obj = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject)  // Internet Explorer
		{
			// Instantiate the latest MS ActiveX Objects
			if (_ms_XMLHttpRequest_ActiveX) {
				AJAX_obj = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
			} 
			else // loops through the various versions of XMLHTTP to ensure we're using the latest
			{
				for (var i = 0; i < versions.length ; i++) 
				{
					try 
					{
						AJAX_obj = new ActiveXObject(versions[i]);
						if (AJAX_obj) {
							_ms_XMLHttpRequest_ActiveX = versions[i]; // save a reference to the proper one to speed up future instantiations
							break;
						}
					}
					catch (objException) {
					// trap; try next one
					} ;
				} ;
			}
		}
		
		AJAX_obj.onreadystatechange = call_back;    
		AJAX_obj.open( "GET", url, true );
		AJAX_obj.send(null);
		//alert("send just called.");
	}
}

function check_user_callback() 
{
	if ( AJAX_obj.readyState == 4) 
	{
		if (AJAX_obj.status == 200) 
		{
			if (got_check_result == 0)
			{
				if (AJAX_obj.responseText == "AVAIL")
				{
					alert("The user name:\n\n " + document.register_form.username.value + "\n\n is available.");
					document.register_form.username.focus();
					username_available = 1;
				}
				else
				{
					alert("We're sorry, but the user name:\n\n " + document.register_form.username.value + "\n\nis already in use by another member.  Please try another username.");
					document.register_form.username.focus();
				}
			}
			got_check_result = 1;
		}
	}
}

function check_band_callback() 
{
	if ( AJAX_obj.readyState == 4) 
	{
		if (AJAX_obj.status == 200) 
		{
			if (got_check_result == 0)
			{
				if (AJAX_obj.responseText == "AVAIL")
				{
					alert("The band name:\n\n " + document.register_form.band_name.value + "\n\n is available.");
					document.register_form.band_name.focus();
					band_name_available = 1;
				}
				else
				{
					alert("We're sorry, but the band name:\n\n " + document.register_form.band_name.value + "\n\nis already in use by another member.  Please try another band name.");
					document.register_form.band_name.focus();
				}
			}
			got_check_result = 1;
		}
	}
}

function check_user_avail()
{
	var url = "/login/check_user_name.php?name=" + document.register_form.username.value;
	
	if (document.register_form.username.value == '')
	{
		alert("Please enter your desired username into the form, then click the 'Is Available?' button.");
		document.register_form.username.focus();
    	return false;
	}
	got_check_result = 0;
	username_available = 0;
	issue_query(url, check_user_callback, document.register_form.username.value);
}
 ;

function check_band_avail()
{
	var url = "/login/check_band_name.php?name=" + document.register_form.band_name.value;
	
	if (document.register_form.band_name.value == '')
	{
		alert("Please enter your desired band name into the form, then click the 'Is Available?' button.");
		document.register_form.band_name.focus();
    	return false;
	}
	got_check_result = 0;
	band_name_available = 0;
	issue_query(url, check_band_callback, document.register_form.band_name.value);
}


function check_reg_fields()
{
	if (document.register_form.member_type.value == '')
	{
		alert("Please select a membership plan.");
		document.register_form.member_type.focus();
    	return false;
	}
	if (document.register_form.name.value == '')
	{
		alert("Please enter your name.");
		document.register_form.name.focus();
    	return false;
	}
	if (document.register_form.band_name.value == '')
	{
		alert("Please enter the name of your band.");
		document.register_form.band_name.focus();
    	return false;
	}
	if (band_name_available == 0)
	{
		alert("Please check the availability of your band name by clicking the 'Is Available' button.");
		document.register_form.band_name.focus();
    	return false;
	}
	
	if (document.register_form.zip.value == '')
	{
		alert("Please enter your zip code.");
		document.register_form.zip.focus();
    	return false;
	}
	if (IsNumeric(document.register_form.zip.value) == false)
	{
		alert("Please enter only digits with an optional '-' seperator.");
		document.register_form.zip.focus();
		return false
	}
	if (checklen(document.register_form.zip.value, 5) == false)
	{
		alert("Please enter at least a 5-digit zip code.");
		document.register_form.zip.focus();
		return false
	}

	if (document.register_form.username.value == '')
	{
		alert("Please enter a username that you would like to use to login to your account.");
		document.register_form.username.focus();
    	return false;
	}
	if (username_available == 0)
	{
		alert("Please check the availability of your desired username by clicking the 'Is Available' button next to the edit box.");
		document.register_form.username.focus();
    	return false;
	}
	
	
	if (document.register_form.passwd.value == '')
	{
		alert("Please enter a password.");
		document.register_form.passwd.focus();
    	return false;
	}
	if (document.register_form.passwd2.value == '')
	{
		alert("Please re-enter the password.");
		document.register_form.passwd2.focus();
    	return false;
	}
	if (document.register_form.passwd.value != document.register_form.passwd2.value)
	{
		alert("The passwords that you entered did not match.");
		document.register_form.passwd2.focus();
    	return false;
	}
	if (document.register_form.email.value == '')
	{
		alert("Please enter your email address.");
		document.register_form.email.focus();
    	return false;
	}
	if (validate_email(document.register_form.email.value) == false)
	{
		alert("Your email address does not appear to be valid.");
		document.register_form.email.focus();
		return false
	}
	
	if (document.register_form.register_code.value == '')
	{
		alert("Please enter your registration code.  If you do not yet have a registration code, send us an email and will will send you a registration code.");
		document.register_form.register_code.focus();
    	return false;
	}
	if (!document.register_form.accept.checked)
	{
		alert("Please indicate that you have read and accept the terms and conditions by checking the box.");
		document.register_form.accept.focus();
    	return false;
	}
	if (!document.register_form.age.checked)
	{
		alert("Please indicate that you are either 18 years of age or older.");
		document.register_form.age.focus();
    	return false;
	}
	
    return true;
}
function validate_email(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
		return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;
	if (str.indexOf(at,(lat+1))!=-1)
		return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;
	if (str.indexOf(dot,(lat+2))==-1)
		return false;
	if (str.indexOf(" ")!=-1)
		return false;
	
	 return true;				
}
function IsNumeric(strString)
{
	var strValidChars = "0123456789-";
	var strChar;
	
	if (strString.length == 0) 
		return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length; i++)
	{
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 return false;
	}
	return true;
}

function checklen(strString, thelen)
{
	if (strString.length < thelen) 
		return false;
	else
		return true;
}
	

