function getHTTPObject() {
	var xmlhttp;
	if (window.XMLHttpRequest){
  		xmlhttp=new XMLHttpRequest()
  	}
	else if (window.ActiveXObject){
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  	}else{	
 		return false;
  	}
  	return xmlhttp;
}
	var http = getHTTPObject(); 
	
function x(thing){
	return document.getElementById(thing);
}

function doSub(span,list){
	clear('subcat');
	x(span).style.display = "block";
}

function clear(topic){
	switch(topic){
		case "subcat":
		if(document.all){
		var ls = getElementsByName_iefix('div','subcat');
		}else{
		var ls = document.getElementsByName('subcat');
		}
		break;
	}
	
	for(i=0;i<ls.length;i++){
		ls[i].style.display = "none";
	}
}

function getElementsByName_iefix(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

var commTable;
var occTable;
var commcount = 0;
var occcount = 0;
function prepIE(){
	if(document.all){
	var tbod = document.createElement('tbody');
	x('comm_table').appendChild(tbod);
	var tbod = document.createElement('tbody');
	x('occ_table').appendChild(tbod);
	commTable = x('comm_table').firstChild;
	occTable = x('occ_table').firstChild;
	}else{
	commTable = x('comm_table');
	occTable = x('occ_table');
	}
	
	//Commute Table
	var row = document.createElement('tr');
	var cell = document.createElement('td');
	cell.setAttribute('width','300');
	var b = document.createElement('b');
	var label = document.createTextNode('Method of Transportation');
	b.appendChild(label);
	cell.appendChild(b);
	row.appendChild(cell);
	var cell = document.createElement('td');
	var b = document.createElement('b');
	var label = document.createTextNode('Miles Traveled Per Day (Round Trip)');
	b.appendChild(label);
	cell.appendChild(b);
	row.appendChild(cell);
	commTable.appendChild(row);
	
	//Other Table
	var row = document.createElement('tr');
	var cell = document.createElement('td');
	cell.setAttribute('width','300');
	var b = document.createElement('b');
	var label = document.createTextNode('Method of Transportation');
	b.appendChild(label);
	cell.appendChild(b);
	row.appendChild(cell);
	var cell = document.createElement('td');
	var b = document.createElement('b');
	var label = document.createTextNode('Miles Traveled Per Day/Week/Month/Year (Round Trip)');
	b.appendChild(label);
	cell.appendChild(b);
	row.appendChild(cell);
	occTable.appendChild(row);
}

function addComm(table){
	switch(table){
		case "occ":
			var theTable = occTable;
			//get the selected value
			var sel = x('occ_menu').options[x('occ_menu').selectedIndex];
			break;
		case "comm":
			var theTable = commTable;
			//get the selected value
			var sel = x('com_menu').options[x('com_menu').selectedIndex];
			break;
	}
	
	if(sel.value != ""){
	//add a row to the table
	var row = document.createElement('tr');
	var cell = document.createElement('td');
	var label = document.createTextNode(sel.text);
	cell.appendChild(label);
	row.appendChild(cell);
	var cell = document.createElement('td');
	var box = document.createElement('input');
	box.setAttribute('type','text');
	switch(table){
		case "comm":
			box.setAttribute('name','com_'+commcount+'_'+sel.value);
			break;
		case "occ":
			box.setAttribute('name','occ_'+occcount+'_'+sel.value);
			break;
	}			
	box.setAttribute('size','10');
	cell.appendChild(box);
	if(table == "occ"){
		var sl = document.createElement('select');
		sl.setAttribute('name','occ_'+occcount+'_'+sel.value+'_amt');
		var opt = document.createElement('option');
		opt.setAttribute('value','day');
		opt.appendChild(document.createTextNode('Day'));
		sl.appendChild(opt);
		var opt = document.createElement('option');
		opt.setAttribute('value','week');
		opt.appendChild(document.createTextNode('Week'));
		sl.appendChild(opt);
		var opt = document.createElement('option');
		opt.setAttribute('value','month');
		opt.appendChild(document.createTextNode('Month'));
		sl.appendChild(opt);
		var opt = document.createElement('option');
		opt.setAttribute('value','year');
		opt.appendChild(document.createTextNode('Year'));
		sl.appendChild(opt);
		cell.appendChild(sl);
	}
	row.appendChild(cell);
	theTable.appendChild(row);
	
	//Remove option
	switch(table){
		case "occ":
			x('occ_menu').removeChild(sel);
			//Increase commcount
			occcount++;
			break;
		case "comm":
			x('com_menu').removeChild(sel);
			//Increase commcount
			commcount++;
			break;
	}
	}
	
}

function clearBRS(){
	var tempar = new Array
	for(t=0;t<x('usr_br').options.length;t++){
		tempar[t] = x('usr_br').options[t];
	}
	for(v=0;v<tempar.length;v++){
		x('usr_br').removeChild(tempar[v]);
	}
}

function brSwitch(){
	var type = x('usr_htype').options[x('usr_htype').selectedIndex];
	clearBRS();
	switch(type.value){
		case "urban":
			var op = document.createElement('option');
			op.setAttribute('value','stu_1');
			op.appendChild(document.createTextNode('Studio Apartment - 1 Bath'));
			x('usr_br').appendChild(op);
			
			var op = document.createElement('option');
			op.setAttribute('value','1_1');
			op.appendChild(document.createTextNode('1 Bedroom - 1 Bath'));
			x('usr_br').appendChild(op);
			
			var op = document.createElement('option');
			op.setAttribute('value','2_1');
			op.appendChild(document.createTextNode('2 Bedroom - 1 Bath'));
			x('usr_br').appendChild(op);
			
			var op = document.createElement('option');
			op.setAttribute('value','3_2');
			op.appendChild(document.createTextNode('3 Bedroom - 2 Bath'));
			x('usr_br').appendChild(op);
			break;
		case "suburb":
			var op = document.createElement('option');
			op.setAttribute('value','2_1');
			op.appendChild(document.createTextNode('2 Bedroom - 1 Bath'));
			x('usr_br').appendChild(op);
			
			var op = document.createElement('option');
			op.setAttribute('value','3_2');
			op.appendChild(document.createTextNode('3 Bedroom - 2 Bath'));
			x('usr_br').appendChild(op);
			
			var op = document.createElement('option');
			op.setAttribute('value','5_4');
			op.appendChild(document.createTextNode('5 Bedroom - 4 Bath'));
			x('usr_br').appendChild(op);
			break;
	}
}


function clearRes(){
	if(document.all){
	var ls = getElementsByName_iefix('div','resultDiv');
	}else{
	var ls = document.getElementsByName('resultDiv');
	}
	for(i=0;i<ls.length;i++){
		ls[i].style.display = "none";
	}
}

function showResult(res){
	clearRes();
	x(res).style.display = 'block';
}

function showDetails(){
	if(x('LS').style.display == "none"){
		x('LS').style.display = "block";
		x('Trans').style.display = "none";
	}else{
		x('LS').style.display = "none";
		x('Trans').style.display = "block";	
	}
}
