// JavaScript Document

function validate(validationcode, name, value) {
	var url = 'system/functions/validate.php?validationcode='+validationcode+'&name='+name+'&value='+value;
	var http_request = setupAJAX();
	
	http_request.onreadystatechange = function() { checkValid(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function openedmenu(menu) {
	var url = 'system/openedmenu.php?menu='+menu+'&curpage='+currentPage();
	var http_request = setupAJAX();

	http_request.open('GET', url, true);
	http_request.send(null);
}

function calculate() {
	var input_level = document.getElementById("input_level").value;
	var input_domicile = document.getElementById("input_domicile").value;
	var input_campus = document.getElementById("input_campus").value;
	
	var input_credits_fall = document.getElementById("input_credits_fall").value;
	var input_credits_spring = document.getElementById("input_credits_spring").value;
	var input_credits_summer = document.getElementById("input_credits_summer").value;
	
	var input_housing = document.getElementById("input_housing").value;
	var input_config = document.getElementById("input_config").value;
	var input_mealplan = document.getElementById("input_mealplan").value;
	
	var input_paymentplan = document.getElementById("input_paymentplan").checked ? 1 : 0;
//	var input_parking = document.getElementById("input_parking").checked ? 1 : 0;
	var input_preview = document.getElementById("input_preview").checked ? 1 : 0;
	var input_previewguests = document.getElementById("input_previewguests").value;
	
	var url = 'system/calculator.php?func=calculate&input_level='+input_level+'&input_domicile='+input_domicile+'&input_campus='+input_campus+'&input_credits_fall='+input_credits_fall+'&input_credits_spring='+input_credits_spring+'&input_credits_summer='+input_credits_summer+'&input_housing='+input_housing+'&input_config='+input_config+'&input_mealplan='+input_mealplan+'&input_paymentplan='+input_paymentplan+'&input_preview='+input_preview+'&input_previewguests='+input_previewguests;
	
/*	var url = 'system/calculator.php?func=calculate&input_level='+input_level+'&input_domicile='+input_domicile+'&input_campus='+input_campus+'&input_credits_fall='+input_credits_fall+'&input_credits_spring='+input_credits_spring+'&input_credits_summer='+input_credits_summer+'&input_housing='+input_housing+'&input_config='+input_config+'&input_mealplan='+input_mealplan+'&input_paymentplan='+input_paymentplan+'&input_parking='+input_parking+'&input_preview='+input_preview+'&input_previewguests='+input_previewguests; */
		
	var http_request = setupAJAX();
	
	http_request.onreadystatechange = function() { reportTuition(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function domicile_campus(func) {
	var input_domicile = document.getElementById("input_domicile").value;
	var input_campus = document.getElementById("input_campus").value;
	
	var url = 'system/calculator.php?func='+func+'&input_domicile='+input_domicile+'&input_campus='+input_campus;
	var http_request = setupAJAX();
	
	http_request.onreadystatechange = function() { domicileCampusBoxes(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null)
}

function housing_config() {
	var input_housing = document.getElementById("input_housing").value;
	var input_config = document.getElementById("input_config").value;
	var input_mealplan = document.getElementById("input_mealplan").value;
	
	var url = 'system/calculator.php?func=housing&input_housing='+input_housing+'&input_config='+input_config+'&input_mealplan='+input_mealplan;
	var http_request = setupAJAX();
	
	http_request.onreadystatechange = function() { configBox(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null)
}

function setupAJAX() {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		var msxmlhttp = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
		for (var i = 0; i < msxmlhttp.length; i++) {
			try {
				http_request = new ActiveXObject(msxmlhttp[i]);
			} catch (e) {
				http_request = null;
			}
			if (http_request != null)
				break;
		}
	}
	
	return http_request;
}

function checkValid(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var valid = xmldoc.getElementsByTagName('valid')[0].firstChild.data;
			var id = xmldoc.getElementsByTagName('id')[0].firstChild.data;
			
			if (valid == '0')
				document.getElementById(id).style.display = 'inline';
			else
				document.getElementById(id).style.display = 'none';
		}
	}
}

function reportTuition(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			var xmldoc = http_request.responseXML;
			document.getElementById('calc_output').innerHTML = xmldoc.getElementsByTagName('tables')[0].firstChild.data;
			document.getElementById('calc_total').innerHTML = "Based on the responses below, one year at ODU will cost about <strong>$"+xmldoc.getElementsByTagName('bottomline')[0].firstChild.data+"</strong>.  Scroll down for a detailed listing of the rates and fees.";
		}
	}
}

function domicileCampusBoxes(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
		//	alert(http_request.responseText);
			
			var input_domicile = document.getElementById('input_domicile');
			var input_campus = document.getElementById('input_campus');
			
			var domicile = xmldoc.getElementsByTagName('select')[0].getElementsByTagName('option');
			var campus = xmldoc.getElementsByTagName('select')[1].getElementsByTagName('option');
			
			for(i=input_domicile.options.length-1;i>=0;i--) {
				input_domicile.remove(i);
			}
			
			for(i=input_campus.options.length-1;i>=0;i--) {
				input_campus.remove(i);
			}
			
			for (i=0; i<domicile.length; i++) {
				value = domicile[i].getElementsByTagName('value')[0].firstChild.data;
				description = domicile[i].getElementsByTagName('description')[0].firstChild.data;	
				selected = domicile[i].getElementsByTagName('selected')[0].firstChild.data == 1 ? true : false;
					
				addOption(input_domicile, description, value, selected);
			}

			for (i=0; i<campus.length; i++) {
				value = campus[i].getElementsByTagName('value')[0].firstChild.data;
				description = campus[i].getElementsByTagName('description')[0].firstChild.data;	
				selected = campus[i].getElementsByTagName('selected')[0].firstChild.data == 1 ? true : false;
				
				addOption(input_campus, description, value, selected);
			}
		}
	}
}

function configBox(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			//alert(http_request.responseText);
			
			var input_config = document.getElementById('input_config');
			var input_config_div = document.getElementById('input_config_div');
			var input_mealplan = document.getElementById('input_mealplan');
			
			var config = xmldoc.getElementsByTagName('select')[0].getElementsByTagName('option');
			var mealplan = xmldoc.getElementsByTagName('select')[1].getElementsByTagName('option');
			
			for(i=input_config.options.length-1;i>=0;i--) {
				input_config.remove(i);
			}
			
			for(i=input_mealplan.options.length-1;i>=0;i--) {
				input_mealplan.remove(i);
			}
			
			for (i=0; i<config.length; i++) {
				value = config[i].getElementsByTagName('value')[0].firstChild.data;
				description = config[i].getElementsByTagName('description')[0].firstChild.data;	
				selected = config[i].getElementsByTagName('selected')[0].firstChild.data == 1 ? true : false;

					
				addOption(input_config, description, value, selected);
			}
			
			if (config[0].getElementsByTagName('value')[0].firstChild.data == '0')
				input_config_div.style.display = "none";
			else
				input_config_div.style.display = "inline";
			
			for (i=0; i<mealplan.length; i++) {
				value = mealplan[i].getElementsByTagName('value')[0].firstChild.data;
				description = mealplan[i].getElementsByTagName('description')[0].firstChild.data;	
				selected = mealplan[i].getElementsByTagName('selected')[0].firstChild.data == 1 ? true : false;
				
				addOption(input_mealplan, description, value, selected);
			}
		}
	}
}