var slocation_lastIndex = 0;
var sposition = null;
var xhr = false;
var xhr_url = null;

function slocation_onchange() {
	var slocation = returnObject('slocation');
	var slocation_value = slocation[slocation.selectedIndex].value;
	sposition = returnObject('sposition');
	
	if (slocation_value == '') {
			slocation.selectedIndex = slocation_lastIndex;
	 } else {
	 		slocation_lastIndex = slocation.selectedIndex;
	 		sposition.options.length = 1;
			sposition.options[0].text = 'Loading...';
			sposition.options[0].value = '';
			sposition.disabled = true;
			getPositions(slocation_value);
	}
}

function xhr_readystatechange () {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			eval(xhr.responseText);
		}
	}
}

function getPositions (LocationID) {
	if (window.XMLHttpRequest) {		// Mozilla, Safari, IE7,...
		xhr = new XMLHttpRequest();
		if (xhr.overrideMimeType) {
			xhr.overrideMimeType('text/js');
		}
	} else if (window.ActiveXObject) {	// IE6
		try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xhr) {
		alert('Error:\n\nCannot create an XMLHTTP instance to load positions at different location.');
		return false;
	}
	
	xhr_url = 'process/employment_getpositions.asp?location=' + LocationID;

	xhr.onreadystatechange = function() { xhr_readystatechange (); };
	xhr.open('GET', xhr_url, true);
	xhr.send(null);
}

function verifyApplyOnline () {
	var sname = returnObject('name');
	var sphone = returnObject('phone');
	var semail = returnObject('email');
	var sresume = returnObject('resume');
	
	if (sname.value == '') {
		alert('Please enter your name.');
		sname.focus();
		return false;
	}
	if (!phonefilter.test(sphone.value)) {
		alert('Please enter a valid phone number.');
		sphone.focus();
		return false;
	}
	if ((semail.value.length > 0)  && (!emailfilter.test(semail.value))) {
		alert('Please enter a valid e-mail address.');
		semail.focus();
		return false;
	}
	
	if (sresume.value.length > 0) {
		var sresume_ext = sresume.value.substr(sresume.value.lastIndexOf('.')).toLowerCase();
		if ((sresume_ext != '.doc') && (sresume_ext != '.pdf')) {
			alert('Please attach a Microsoft Word or Adobe PDF Document.');
			sresume.focus();
			return false;
		}
	}
	return true;
}

function selectPosition (positionID) {
	var sposition = returnObject('sposition');
	for (var i = 0; i < sposition.options.length; i++)
		if (sposition[i].value == positionID)
			sposition.selectedIndex = i;
}