// JavaScript Document
/*******
	This javascript file loads a list of data into a calling form select list, and can 
	be utilized to load large lists into forms after the rest of the page loads, optimizing 
	for web crawlers, etc. while maintaining the same general user experience
	Forrest - 2008 Jul 21
*****/

var scratchPad = ""
var newData = true
var selectInAptCode = ""
var selectOutAptCode = ""
var duplicate = true

function getData(departList, arriveList) {
	var departList1 = ""
	var arriveList1 = ""
	duplicate = false
	getData1(departList, arriveList, departList1, arriveList1)
}

function getData1(departList, arriveList, departList1, arriveList1) {
	scratchPad = ""
	selectOutAptCode = departList.options[departList.options.selectedIndex].value
	selectInAptCode = arriveList.options[arriveList.options.selectedIndex].value
   if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {openURL("../getList.cfm", departList, arriveList, departList1, arriveList1);};
    req.open("GET", "../getList.cfm", true);
    req.send("");
	} else {
	  //alert("req is undefined!!")
  }
}

function openURL(url, departList, arriveList, departList1, arriveList1) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
		scratchPad = req.responseText
		if (scratchPad != "") {
			processData(departList, arriveList, departList1, arriveList1)
		}
	} else {
		scratchPad = ""
		//alert("Errpr retrieving data")
		//alert("Error is: " + req.status + "\n" +req.statusText)
    }
	}
}

function processData(departList, arriveList, departList1, arriveList1) {
	if (newData) {
		newData = false
		lines = []
		lines = scratchPad.split("\n")		
		splitArray(lines, departList, arriveList, departList1, arriveList1)
	}
}

function splitArray(lineArray, departList, arriveList, departList1, arriveList1) {
	var x
	var myString
	var temp = []
	var inDepart, inArrive = false
	var myOption
	for (x in lineArray) {
		if (inDepart) {
			myString = trim(lineArray[x])
			if (myString == "") {
				inDepart = false				
			} else {
				temp = myString.split("\t")
				myOption = new Option()
				myOption.text = temp[0]
				myOption.value = temp[1]
				if (selectOutAptCode == myOption.value) {
					myOption.selected = true
				}
				departList.options[departList.options.length] = myOption
				if (duplicate) {
					myOption = new Option()
					myOption.text = temp[0]
					myOption.value = temp[1]
					if (selectOutAptCode == myOption.value) {
						myOption.selected = true
					}
					departList1.options[departList1.options.length] = myOption
				}
			}
		}
		if (inArrive) {
			myString = trim(lineArray[x])
			if (myString == "") {
				inArrive = false
			} else {
				temp = myString.split("\t")
				myOption = new Option()
				myOption.text = temp[0]
				myOption.value = temp[1]
				if (selectInAptCode == myOption.value) {
					myOption.selected = true
				}
				arriveList.options[arriveList.options.length] = myOption
				if (duplicate) {
					myOption = new Option()
					myOption.text = temp[0]
					myOption.value = temp[1]
					if (selectInAptCode == myOption.value) {
						myOption.selected = true
					}
					arriveList1.options[arriveList1.options.length] = myOption
				}
			}
		}
		if (lineArray[x].indexOf('Departure Cities:') > -1) {
			inDepart = true
			departList.options.length = 0
			if (duplicate) {
				departList1.options.length = 0
			}
		}
		if (lineArray[x].indexOf('Arrival Cities:') > -1) {
			inArrive = true
			arriveList.options.length = 0
			if (duplicate) {
				arriveList1.options.length = 0
			}
		}
	}
}

function trim( strText ){
	return( strText.replace(new RegExp("^([\\s]+)|([\\s]+)$", "gm"), "") );
}

