function getRadioVal(radioName)
{
	radioObjects = document.getElementsByName(radioName);

	for(var i=0; i<radioObjects.length; i++)
	{
		if(radioObjects[i].checked == true)
			return radioObjects[i].value;
	}

	return null;
}


function validateOpeningRequest()
{
	var errors=new Array()

	// CHECK FOR ERRORS

	// Check name length
	if(document.getElementById('name').value.length==0)
		errors.push("You must enter your name.");
	if(document.getElementById('name').value.length > 100)
		errors.push("Name cannot be longer than 100 character.");

	// Check email length
	if(document.getElementById('email').value.length > 100)
		errors.push("Email address cannot be longer than 100 character.");

	// Check phone number format
	if(
		document.getElementById('phone_area_code').value.search(/^\d{3}$/) ||
		document.getElementById('phone_first_three').value.search(/^\d{3}$/) ||
		document.getElementById('phone_last_four').value.search(/^\d{4}$/)
 	)
		errors.push("Please enter a valid phone number including area code.");

	// Check number of openings being claimed
	if(document.getElementById('num_openings').value.search(/^\d{1,3}$/) )
		errors.push("Please enter a valid number of openings to claim.");

	// Check category
	if(document.getElementById('ind_type').value == 0)
		errors.push("Please select the category that best fits you.");

	// DISPLAY ERROR MESSAGE
	if(errors.length>0)
	{
		var errorMsg = "Before your application can be submitted, the following errors must be corrected:\n";
		for(var i=0; i<errors.length; i++)
			errorMsg += "\n- "+errors[i];
		alert(errorMsg);

		return false;
	}

	if(document.getElementById('affiliation').value=='Other/None')
		return confirm("You have not selected an affiliated program.  You can submit your information but be aware that the organization offering the opening may not accept your request.  Are you sure you want to continue?");

	return true;
}


function validateOpeningEdit()
{
	var errors=new Array()

	// CHECK FOR ERRORS

	// Check short description length
	if(document.getElementById('short_desc').value.length==0)
		errors.push("Enter a short description of the opening (50 character max.)");
	if(document.getElementById('short_desc').value.length > 50)
		errors.push("Short descriptions cannot be longer than 50 characters.");

	// Check full description length
	if(document.getElementById('full_desc').value.length==0)
		errors.push("Enter a full description of the opening (1000 character max.)");
	if(document.getElementById('full_desc').value.length > 1000)
		errors.push("Full descriptions cannot be longer than 1000 characters.");

	// Check email length
	if(document.getElementById('cont_email').value.length == 0)
		errors.push("Please enter an email address where notifications will be sent when an opening is filled.");
	if(document.getElementById('cont_email').value.length > 100)
		errors.push("Email address cannot be longer than 100 character.");


	// Check phone number format
	if(document.getElementById('slots_avail').value.search(/^\d{1,3}$/) )
		errors.push("Please enter the number of slots available for this opening.");

	// DISPLAY ERROR MESSAGE
	if(errors.length>0)
	{
		var errorMsg = "Before your application can be submitted, the following errors must be corrected:\n";
		for(var i=0; i<errors.length; i++)
			errorMsg += "\n- "+errors[i];
		alert(errorMsg);

		return false;
	}

	return true;
}


function openAvailabilityWindow()
{
	var loginName = document.getElementById('login_name').value;

	if(loginName.search(/^[a-zA-Z0-9]{1,32}$/) )
	{
		alert("Login names must contain between 1 and 32 letters and/or numbers.");
		return;
	}

	window.open("login_avail.php?login_name="+loginName, "null",  "toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resizable=1, directories=0, width=350, height=35");
}


function validateEditAcct()
{
	var errors=new Array()

	// CHECK FOR ERRORS

	// Check login name
	if(document.getElementById('login_name')!=null && document.getElementById('login_name').value.search(/^[a-zA-Z0-9]{1,32}$/) )
		errors.push("Login names must be between 1 and 32 characters and can only contain numbers and letters.");

	// Check password
	if(document.getElementById('password').value.length < 8)
	{
		errors.push("Passwords must be at least 8 characters long.");
		document.getElementById('password').value = "";
		document.getElementById('password_confirm').value = "";
	}
	else if(document.getElementById('password').value.length > 32)
	{
		errors.push("Passwords cannot be longer than 32 characters.");
		document.getElementById('password').value = "";
		document.getElementById('password_confirm').value = "";
	}
	else if(document.getElementById('password').value != document.getElementById('password_confirm').value)
	{
		errors.push("The password you entered does not match the password you entered in \"confirm password\".");
		document.getElementById('password').value = "";
		document.getElementById('password_confirm').value = "";
	}

	if(document.getElementById('org_name').value.length == 0)
		errors.push("Please enter an organization name.");
	if(document.getElementById('org_name').value.length > 150)
		errors.push("Organization names cannot be longer than 150 characters.");

	if(document.getElementById('org_url').value.length > 150 )
		errors.push("Web site address cannot be longer than 150 characters.");

	if(document.getElementById('org_address').value.length > 255 )
		errors.push("Street address cannot be longer than 255 characters.");

	if(document.getElementById('cont_name').value.length == 0 )
		errors.push("Please enter a contact name.");
	if(document.getElementById('cont_name').value.length > 100 )
		errors.push("Contact name cannot be longer than 100 characters.");

	if(document.getElementById('cont_email').value.length == 0 )
		errors.push("Please enter a contact email address.");
	if(document.getElementById('cont_email').value.length > 100 )
		errors.push("Contact email address cannot be longer than 100 characters.");

	// Check phone number format
	if(
		document.getElementById('primary_phone_area_code').value.search(/^\d{3}$/) ||
		document.getElementById('primary_phone_first_three').value.search(/^\d{3}$/) ||
		document.getElementById('primary_phone_last_four').value.search(/^\d{4}$/)
 	)
		errors.push("Please enter a valid primary phone number including area code.");

	// Check phone number format
	if(
		(
			document.getElementById('secondary_phone_area_code').value!="" ||
			document.getElementById('secondary_phone_first_three').value!="" ||
			document.getElementById('secondary_phone_last_four').value!=""
		)
		&&
		(
			document.getElementById('secondary_phone_area_code').value.search(/^\d{3}$/) ||
			document.getElementById('secondary_phone_first_three').value.search(/^\d{3}$/) ||
			document.getElementById('secondary_phone_last_four').value.search(/^\d{4}$/)
		)
 	)
		errors.push("Seconadary phone number is optional cannot be an invalid number.");


	if(document.getElementById('org_desc').value.length > 2500 )
		errors.push("Organization description cannot be longer than 2500 characters.");


	// DISPLAY ERROR MESSAGE
	if(errors.length>0)
	{
		var errorMsg = "Before your application can be submitted, the following errors must be corrected:\n";
		for(var i=0; i<errors.length; i++)
			errorMsg += "\n- "+errors[i];
		alert(errorMsg);

		return false;
	}

	return true;
}


function validateOpeningSearch()
{
	var errors=new Array()

	// CHECK FOR ERRORS

	// Check that number of slots is actually a number
	if(getRadioVal("hasSlotsPref")==1 && document.getElementById('numSlots').value.length!=0 && document.getElementById('numSlots').value.search(/^\d+$/))
		errors.push("Please enter a valid number for the number of available slots.");

	// Check that earliest start date isn't after the latest start date
	if(getRadioVal("hasDatePref")==1 && document.getElementById('earliest_date').value > document.getElementById('latest_date').value)
		errors.push("The ealiest starting date you selected is after the latest date.");

	// DISPLAY ERROR MESSAGE
	if(errors.length>0)
	{
		var errorMsg = "Before your application can be submitted, the following errors must be corrected:\n";
		for(var i=0; i<errors.length; i++)
			errorMsg += "\n- "+errors[i];
		alert(errorMsg);

		return false;
	}

	return true;
}


function validateLogin()
{
	var errors=new Array()

	// CHECK FOR ERRORS

	// Check name length
	if(document.getElementById('user_name').value.length==0)
		errors.push("Please enter the user name you chose when creating your account.");
	if(document.getElementById('user_name').value.length>32)
		errors.push("The user name you entered is longer than is allowed.");

	// Check password length
	if(document.getElementById('user_password').value.length==0)
		errors.push("Please enter your password.");
	if(document.getElementById('user_password').value.length>32)
	{
		document.getElementById('user_password').value = "";
		errors.push("The password you entered is longer than is allowed");
	}

	// DISPLAY ERROR MESSAGE
	if(errors.length>0)
	{
		var errorMsg = "Before your application can be submitted, the following errors must be corrected:\n";
		for(var i=0; i<errors.length; i++)
			errorMsg += "\n- "+errors[i];
		alert(errorMsg);

		return false;
	}

	return true;
}


var xhr;
function checkAvailability()
{
	// Before going through the hastle of an AJAX call, make sure the login name is valid
	if(document.getElementById('login_name').value.search(/^[a-zA-Z0-9]{1,32}$/) )
	{
		document.getElementById('login_name').style.backgroundColor = "#FFCCCC";
		return;
	}

	xhr = null;
	if(window.XMLHttpRequest)
	{
		try
		{
			xhr = new XMLHttpRequest();
		}
		catch(e)
		{
			xhr = null;
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xhr = null;
		}
	}

	// The XmlHttlRequest couldn't be created so re-display the link
	if(xhr == null)
	{
		document.getElementById('login_name').style.backgroundColor = ""; // Color may have been changed by the regex so change it back
		document.getElementById('check_avail_link').style.display = ""; // Set default display
		return;
	}

	xhr.onreadystatechange = handleResponse;
	xhr.open("POST", "login_available.php?login_name="+encodeURI(document.getElementById('login_name').value), true);
	xhr.send("");
}


function handleResponse()
{
	if(xhr.readyState==4) // Response has been received
	{
		if(xhr.status==200) // Request succeeded
		{
			if(xhr.responseText.charAt(0)=='S')
			{
				if(xhr.responseText.charAt(1)=='1') // Name is available - set to green
					document.getElementById('login_name').style.backgroundColor = "#CCFFCC";
				else // Name is unavailable - set to red
					document.getElementById('login_name').style.backgroundColor = "#FFCCCC";
			}
			else // If the PHP page had an error, revert to the default color and display the link
			{
				document.getElementById('login_name').style.backgroundColor = "";
				document.getElementById('check_avail_link').style.display = "";
			}
		}
		else // Request failed - display the plain link as a backup
		{
			document.getElementById('login_name').style.backgroundColor = "";
			document.getElementById('check_avail_link').style.display = "";
		}
	}
}









