/* required loc.js */

var CSV_PATH = "data/base_en.csv";

var Rows = new Array(); /* Data CSV Objects */
var Countries = new Array(); /* "Name" = Country, "States" = Array { String } */

var _unsupportedLanguage = false;

/* Localization Support */

function sendDataRequest(lang) {
	if (!lang) {
		lang = "de";
	}
	
	if (locLanguage) {
		locLanguage = lang;
	}
	var request = getRequest();
	
	if (null == request) {
		return;
	}
	
	if (locLanguage) {
		CSV_PATH = "data/base_"+locLanguage+".csv"; /* base_de, base_en and so on... */
	}
	
	request.open("GET", CSV_PATH, true);
	request.onreadystatechange = function() {
		switch (request.readyState) {
			case 4:
				if (request.status != 200) {
					onError();
				} else {
					onSuccess(request.responseText);
				}
				break;
			
			default:
				return false;
				break;
		}
	};
	request.send(null);
}

function getRows() {
	return Rows;
}

function getCountries() {
	return Countries;
}

function getRequest() {
	var request = null;
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Current Browser has no XMLHttpRequest object!");
	}
	
	return request;
}

function onError() {
	if (!_unsupportedLanguage) {
		_unsupportedLanguage = true;
		sendDataRequest(locDefault);
	} else {
		alert("Could not load base data!");
	}
}


/**
 * Called when the CSV-Data is fully loaded.
 *
 * @param text csv raw string
 */
function onSuccess(text) {
	text = text.replace("\r\n", "\n");
	text = text.replace("\r", "\n");
	
	var csvRows = text.split("\n");
	
	text = null;

	var header = csvRows[0].split(";");
	var i = 1;
	var columns = null;
	for (i = 1; i < csvRows.length; i += 1) {
		columns = csvRows[i].split(";")
		if (columns.length > 1) {
			generateData(header, columns);
		}
		csvRows[i] = null;
	}
	
	csvRows = null;	
	
	showCountries();
	
	document.getElementById("HDSearchLoader").style.display = "none";
}

function generateData(header, columns) {
	var index = Rows.length;
	var obj = new Object();
	// set all data from header to new data object
	for (i = 0; i < header.length; i += 1) {
		obj[header[i]] = columns[i];
	}
	
	Rows[index] = obj;
	
	checkCountryAndState(obj["Staat"], obj["Bundesland"]);
}

function checkCountryAndState(country, state) {
	var count = Countries.length;
	var tmpCountry = provideCountry(country);
	provideState(tmpCountry, state);
}

function provideState(country, state) {
	if (state && state.length > 1) {
		var tmpState = getState(country, state);
		if (null == tmpState) {
			tmpState = createState(country, state);
		}
		return tmpState;
	}
	return null;
}

function getState(country, state) {
	var count = country["States"].length;
	for (var i = 0; i < count; i += 1) {		
		if (country["States"][i].toLowerCase() == state.toLowerCase()) {
			return country["States"][i];
		}
	}
	return null;
}

function createState(country, state) {
	var index = country["States"].length;
	country["States"].push(state);
	return state;
}

function provideCountry(name) {
	var country = getCountry(name);
	if (null == country) {
		country = createCountry(name);
	}
	return country;
}

function createCountry(name) {
	var obj = new Object();
	var index = Countries.length;
	obj["Name"] = name;
	obj["States"] = new Array();
	Countries[index] = obj;
	
	return Countries[index];
}

function getCountry(name) {
	var count = Countries.length;
	for (var i = 0; i < count; i += 1) {
		if (Countries[i]["Name"].toLowerCase() == name.toLowerCase()) {
			return Countries[i];
		}
	}
	return null;
}

function showCountries() {
	var sel = document.getElementById("State");
	sel.innerHTML = '';
	
	var n = document.createElement("option");
	n.value = "";
	n.appendChild(document.createTextNode(locCountry[locLanguage]));
	
	sel.appendChild(n);
	
	for (var i = 0; i < Countries.length; i += 1) {
		var opt = document.createElement("option");
		opt.value = i;
		opt.appendChild(document.createTextNode(Countries[i]["Name"]));
		sel.appendChild(opt);
	}
	
	sel.selectedIndex = 0;
	
	showStates(-1);
}

function selectCountry(name) {
	showStates(name);
}

function showStates(index) {
	var sel = document.getElementById("Bundesland");
	
	if (index >= 0) {
		var country = Countries[index];
		
		sel.innerHTML = '';
		var n = document.createElement("option");
		n.value = "";
		n.appendChild(document.createTextNode(locStateCounty[locLanguage]));
		
		sel.appendChild(n);
		
		if (country) {	
			var states = country["States"];
			for (var i = 0; i < states.length; i += 1) {
				//content += '<option value="' + i + '">' + states[i]+ '</option>';
				var opt = document.createElement("option");
				opt.value = i;
				opt.appendChild(document.createTextNode(states[i]));
				sel.appendChild(opt);
			}
		}
		
		sel.selectedIndex = 0;
	} else {
		sel.innerHTML = '';
		var n = document.createElement("option");
		n.value = "";
		n.appendChild(document.createTextNode(locStateCounty[locLanguage]));
		sel.appendChild(n);
	}
}

function getCountryByName(name) {
	for (var i = 0; i < Countries.length; i += 1) {
		if (Countries[i]["Name"] == name) {
			return Countries[i];
		}
	}
	return null;
}
