var xmlDocLocations;
var arrLocations;
var slcCity, tableOutput;
var isIE, isFF;

var CNI_IE_CITY = 0;
var CNI_IE_LOCATION = 1;
var CNI_IE_ADDLINE1 = 2;
var CNI_IE_ADDLINE2 = 3;
var CNI_IE_ADDLINE0 = 4;
var CNI_FF_CITY = 1;
var CNI_FF_LOCATION = 3;
var CNI_FF_ADDLINE1 = 5;
var CNI_FF_ADDLINE2 = 7;
var CNI_FF_ADDLINE0 = 9;

Array.prototype.unique = function() {
	var r = new Array();
	o: for (var i = 0, n = this.length; i < n; i++) {
		for (var x = 0, y = r.length; x < y; x++) {
			if (r[x] == this[i]) {
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

function loadXML(select, table, xmlFile) {
	isFF = isIE = false;
	slcCity = document.getElementById(select);
	tableOutput = document.getElementById(table);
	if (window.ActiveXObject) {
		isIE = true;
		xmlDocLocations = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocLocations.async = false;
		xmlDocLocations.onreadystatechange = init;
		xmlDocLocations.load(xmlFile);
	}
	else if (document.implementation && document.implementation.createDocument) {
		isFF = true;
		xmlDocLocations = document.implementation.createDocument("", "", null);
		xmlDocLocations.onload = init;
		xmlDocLocations.load(xmlFile);
	}
	else {
		alert('Unsupported browser used. Contact support.');
	}
}

function init() {
	if (xmlDocLocations.readyState == 4 || isFF) {
		var menuhtml = '';
		
		//	Get names of cities
		var cities = xmlDocLocations.getElementsByTagName('City');
		var cityNames = new Array();
		for (i = 0; i < cities.length; i++) {
			cityNames.push(cities[i].childNodes[0].nodeValue);
		}
		
		//	Get unique names
		cityNames = cityNames.unique().sort();
		for (j = 0; j < cityNames.length; j++) {
			slcCity.options.add(new Option(cityNames[j], cityNames[j]));
		}
	}
}

function CityChanged() {
	if (!arrLocations) {
		arrLocations = xmlDocLocations.getElementsByTagName('Store');
	}
	
	var results = new Array();
	for (j = 0; j < arrLocations.length; j++) {
		if (isIE) {
			if (slcCity.value == arrLocations[j].childNodes[CNI_IE_CITY].childNodes[0].nodeValue) {
				results.push(arrLocations[j]);
			}
		}
		else if (isFF) {
			if (slcCity.value == arrLocations[j].childNodes[CNI_FF_CITY].childNodes[0].nodeValue) {
					results.push(arrLocations[j]);
			}
		}
	}

	//	Delete current rows
	while (tableOutput.rows.length > 2) {
		tableOutput.deleteRow(2);
	}
	
	//	Add new rows
	var lastRow = tableOutput.rows[0].cloneNode(true);
	var addressContents = '';
	for (k = 0; k < results.length; k++) {
		addressContents = '';
		if (isIE) {
			lastRow.children[0].innerHTML = results[k].childNodes[CNI_IE_LOCATION].childNodes[0].nodeValue;
			try {
				addressContents += results[k].childNodes[CNI_IE_ADDLINE0].childNodes[0].nodeValue + '<br />';
			}
			catch (e) { }
			addressContents += results[k].childNodes[CNI_IE_ADDLINE1].childNodes[0].nodeValue;
			try{
				addressContents += '<br />' + results[k].childNodes[CNI_IE_ADDLINE2].childNodes[0].nodeValue;
			}
			catch(e){}
		}
		else if (isFF) {
			lastRow.children[0].innerHTML = results[k].childNodes[CNI_FF_LOCATION].childNodes[0].nodeValue;
			try {
				addressContents += results[k].childNodes[CNI_FF_ADDLINE0].childNodes[0].nodeValue + '<br />';
			}
			catch (e) { }
			addressContents += results[k].childNodes[CNI_FF_ADDLINE1].childNodes[0].nodeValue;
			try {
				addressContents += '<br />' + results[k].childNodes[CNI_FF_ADDLINE2].childNodes[0].nodeValue;
			}
			catch (e) { }
		}
		lastRow.children[1].innerHTML = addressContents;
		lastRow.removeAttribute('style');
		lastRow.className = (k % 2 == 0 ? 'OddRows' : 'EvenRows');
		tableOutput.children[0].appendChild(lastRow);
		lastRow = tableOutput.rows[0].cloneNode(true);
	}
}

function getOuterHTML(object) {
	var element;
	if (!object) return null;
	element = document.createElement("div");
	element.appendChild(object.cloneNode(true));
	return element.innerHTML;
}


function openWindow() {
	window.open("https://www.kotak.com/j1001mp/netapp/MainPage.jsp", "", "width=" + screen.availWidth + ",height=" + screen.availHeight + ",resizable=1,status=1,location=1,scrollbars=1,maximize=1");
}

function openFooterWindow(url) {
	window.open(url, "", "width=" + screen.availWidth + ",height=" + screen.availHeight + ",resizable=1,status=1,location=1,scrollbars=1,maximize=1");
}

function ChangeToHttp() {
	var loc = document.location.toString();
	var index = loc.indexOf(":");
	var url = loc.substring(index, loc.length);
	if (index == "5") {
		httpUrl = "http" + url;
		location.replace(httpUrl); // get rid of current page in history
		location.href = httpUrl;
	}
}

