function nametag_preview_draw()
{
	document.getElementById("nametag_input_name").value = document.getElementById("nametag_input_name").value.toUpperCase();
	document.getElementById("nametag_input_town").value = document.getElementById("nametag_input_town").value.toUpperCase();
	document.getElementById("nametag_preview_name").innerHTML = document.getElementById("nametag_input_name").value;
	document.getElementById("nametag_preview_town").innerHTML = document.getElementById("nametag_input_town").value;
	if(document.getElementById("nametag_input_town").value == 'HEY LISTEN')
	{
		document.getElementById("nametag_preview").style.backgroundImage = 'url(tinktag.jpg)';
	}
}
function hide_school_details()
{
	document.getElementById("school_details_div").style.display = 'none';
}
function unhide_school_details()
{
	document.getElementById("school_details_div").style.display = 'block';
}
function hide_shipping_address()
{
	document.getElementById("addresstitle").style.display = 'none';
	document.getElementById("shipping_address_div").style.display = 'none';
}
function unhide_shipping_address()
{
	document.getElementById("addresstitle").style.display = 'inline';
	document.getElementById("shipping_address_div").style.display = 'block';
}
function toggle_shipping_details()
{
	if(document.getElementById("shippingtoggle").checked)
	{
		unhide_shipping_address();
	}
	else
	{
		hide_shipping_address();
	}
}

function init_school_details()
{
	if(
		(
		   document.getElementById('address_school_name').value != ''
		|| document.getElementById('address_school_street1').value != ''
		|| document.getElementById('address_school_street2').value != ''
		|| document.getElementById('address_school_city').value != ''
		|| document.getElementById('address_school_state').value != ''
		|| document.getElementById('address_school_zip').value != ''
		)
		&&
		document.getElementById("schooltoggle").checked == false
	)
	{
		document.getElementById('school_select_div').style.display = 'none';
		unhide_school_details();
	}
	else
	{
		toggle_school_details();
	}
}

function toggle_school_details()
{
	if(document.getElementById("schooltoggle").checked)
	{
		unhide_school_details();
	}
	else
	{
		hide_school_details();
	}
}
function check_internship()
{
	isMananagement = document.getElementById('personal_internship').value;
	isMananagement = isMananagement.substring(isMananagement.indexOf('|')+1);
	if(isMananagement == 1)
	{
		disable_housing();
	}
	else
	{
		enable_housing();
	}
	if(document.getElementById('personal_internship').value == '525|0')
	{
		unhide_other_internship();
	}
	else
	{
		hide_other_internship();
	}
}
function disable_housing()
{
//	document.getElementById('housing0').disabled = true;
	document.getElementById('housing1').disabled = true;
	document.getElementById('housing2').disabled = true;
	document.getElementById('housing3').disabled = true;
	document.getElementById('housing2').checked  = true;
	document.getElementById('housing_warn_label').style.display = 'block';
}
function enable_housing()
{
//	document.getElementById('housing0').disabled = false;
	document.getElementById('housing1').disabled = false;
	document.getElementById('housing2').disabled = false;
	document.getElementById('housing3').disabled = false;
	document.getElementById('housing_warn_label').style.display = 'none';
}
function clear_with_focus(id,defaulttext)
{
	object = document.getElementById(id);
	if(object.value == defaulttext)
	{
		object.style.color = '#000'
		object.value = '';
	}
}
function restore_with_focus(id,defaulttext)
{
	object = document.getElementById(id);
	if(object.value == defaulttext || object.value == '')
	{
		object.style.color = '#ccc'
		object.value = defaulttext;
	}
}
function check_source()
{
	if(document.getElementById('source_select_input').value == 1)
	{
		unhide_other_source();
	}
	else
	{
		hide_other_source();
	}
}
function init()
{
	document.getElementById("school_select_div").style.display = 'block';
	toggle_shipping_details();
	init_school_details();
	check_internship();
	restore_with_focus('personal_bddate','DD');
	restore_with_focus('personal_bdyear','YYYY');
	restore_with_focus('nametag_input_name','MICKEY');
	restore_with_focus('nametag_input_town','HOMETOWN, USA');
	replace_school_state();
	nametag_preview_draw();
	check_source();
}
function hide_other_source()
{
	document.getElementById("source_other_input").style.display = 'none';
	document.getElementById("source_other_label").style.display = 'none';
}
function unhide_other_source()
{
	document.getElementById("source_other_input").style.display = 'inline';
	document.getElementById("source_other_label").style.display = 'inline';
}
function hide_other_internship()
{
	document.getElementById("internship_other_input").style.display = 'none';
	document.getElementById("internship_other_label").style.display = 'none';
}
function unhide_other_internship()
{
	document.getElementById("internship_other_input").style.display = 'inline';
	document.getElementById("internship_other_label").style.display = 'inline';
}

function xmlhttpGet(strURL) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('GET', strURL);
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			eval(self.xmlHttpReq.responseText);
		}
	}
	self.xmlHttpReq.send(null);
}

function fallback()
{

	unhide_school_details();
}

function replace_school_state()
{
	document.getElementById('school_ajax_state').disabled = true;
	document.getElementById('school_ajax_city').disabled = true;
	document.getElementById('school_ajax_name').disabled = true;
	removeOptions(document.getElementById('school_ajax_state'));
	addOption(document.getElementById('school_ajax_state'),'','Loading...');
	removeOptions(document.getElementById('school_ajax_city'));
	addOption(document.getElementById('school_ajax_city'),'','Select a State First');
	removeOptions(document.getElementById('school_ajax_name'));
	addOption(document.getElementById('school_ajax_name'),'','Select a City First');
	xmlhttpGet('schoolajax.php?action=s');
}
function replace_school_city()
{
	if(document.getElementById('school_ajax_state').value != '')
	{

		clear_school_details();
		document.getElementById('school_ajax_city').disabled = true;
		document.getElementById('school_ajax_name').disabled = true;
		removeOptions(document.getElementById('school_ajax_city'));
		addOption(document.getElementById('school_ajax_city'),'','Loading...');
		removeOptions(document.getElementById('school_ajax_name'));
		addOption(document.getElementById('school_ajax_name'),'','Select a City First');
		xmlhttpGet('schoolajax.php?action=c&state='+document.getElementById('school_ajax_state').value);
	}
}
function replace_school_name()
{
	if(document.getElementById('school_ajax_city').value != '')
	{
		clear_school_details();
		document.getElementById('school_ajax_name').disabled = true;
		removeOptions(document.getElementById('school_ajax_name'));
		addOption(document.getElementById('school_ajax_name'),'','Loading...');
		document.getElementById('school_ajax_name').innerHTML = '<option selected="selected">Loading...</option>';
		xmlhttpGet('schoolajax.php?action=i&state='+document.getElementById('school_ajax_state').value+'&city='+document.getElementById('school_ajax_city').value);
	}
}
function replace_school_details()
{
	if(document.getElementById('school_ajax_name').value != '')
	{
		clear_school_details();
		document.getElementById('schooltogglewrapper').style.display = 'none';
		document.getElementById("schooltoggle").checked = false;
		xmlhttpGet('schoolajax.php?action=d&id='+document.getElementById('school_ajax_name').value);
	}
}

function clear_school_details()
{
	document.getElementById('address_school_name').value = '';
	document.getElementById('address_school_street1').value = '';
	document.getElementById('address_school_street2').value = '';
	document.getElementById('address_school_city').value = '';
	document.getElementById('address_school_state').value = '';
	document.getElementById('address_school_zip').value = '';
}

function removeOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}

function addOption(selectbox,value,text)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

