function checkForm(){
	var result = document.getElementById('formResult');
	result.innerHTML = "<img class='loading' src='images/load.gif' alt='loading...' /> checking application...";
	setTimeout( "appChecker();", 3000);
	return false;
}

function appChecker(){
	var missingFields = '';
	var errorMessage = '';
	var reqFields = new Array('member_type','last_name','first_name','street_address','city','country','email','work_phone','educational_institution','degree_field','graduated','password');
	var i = 0;
	var errors = 0;
	var result = document.getElementById('formResult');
	var email = document.getElementById('email').value;

	for(i=0;i<reqFields.length;i++) {
		if(!document.getElementById(reqFields[i]) || document.getElementById(reqFields[i]).value == ''){
			missingFields += '- ' + reqFields[i].replace("_"," ") + '<br />';
			errors++;
		}
	}

	if(email != '' && !checkEmail(email)){
		errorMessage += '- the email you provided appears to have a typo<br />';
	}


	var password1 = document.getElementById('password').value;
	var password2 = document.getElementById('password2').value;

	

		if(password1.length < 6 || password1.length > 20 ){
			if(password1 != ''){
				errorMessage += '- passwords must be between 6-20 characters in length<br />';
				errors++;
			}
		}


	
	if(password1 == password2){
		if(!alphanumeric(password1)){
			errorMessage += '- passwords can only contain alphanumeric characters (a-z 0-9)<br />';
			errors++;
		}
	}
	if(password1 != password2){
		errorMessage += '- the passwords you entered do not match<br />';
		errors++;
	}

	if(errors > 0){
		if(missingFields != ''){
			errorMessage += '<br />The following required fields were left blank:<br />' + missingFields;
		}
		result.innerHTML = "<p style=\"padding:10px;border:1px solid #F00;background:#FCC;\"><strong>Please address the following issues and then re-submit your application:</strong><br /><br />" + errorMessage + "</p>";
		return false;
	}
	else{
		result.innerHTML = "<p style=\"padding:10px;border:1px solid #0F0;background:#CFC;\">Submitting application. Please wait...</p>";
		setTimeout( "doSubmit();", 2000);
	}
	return false;
}

function doSubmit(){
	document.mainForm.submit();
}

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}
	else{
		return true;
	}
}

function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))		{
		}
		else{
			return false;
		}
	}
	return true;
}


function checkCountry(){

	var country = document.getElementById('country').value;

	if(country == 'United States'){
		var obj = document.getElementById('usState');
		obj.style.visibility = "visible";
		obj.style.display = "block";
		var obj = document.getElementById('nonUsState');
		obj.style.visibility = "hidden";
		obj.style.display = "none";
	}
	else{
		var obj = document.getElementById('usState');
		obj.style.visibility = "hidden";
		obj.style.display = "none";
		var obj = document.getElementById('nonUsState');
		obj.style.visibility = "visible";
		obj.style.display = "block";
	}
	return;
}