function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function travelAgent() {
	var form = document.forms['travelAgentForm'];
	if (form == null) return;
	if (form['province'] == null) return;
	if (form['city'] == null) return;
	var opt_provinces = form['province'].options;
	if (typeof agents == 'undefined') return;
	if (agents.province == null) return;
	if (!isArray(agents.province)) return;
	var limit = agents.province.length;
	for(i = 0; i < limit; i++) {
		var p = agents.province[i];
		opt_provinces[i+1] = new Option(p.text, p.value);
	} // end for
} // end function

function populateCities(index) {
	if (index <0) return;	//error
	var form = document.forms['travelAgentForm'];
	if (form == null) return;
	if (form['province'] == null) return;
	if (form['city'] == null) return;
	var opt_cities = form['city'].options;
	if(opt_cities != null && index > -1) {
		if (agents.province == null) return;
		if (!isArray(agents.province)) return;
		if (agents.province[index] == null) return;
		if (agents.province[index].city == null) return;
		if (!isArray(agents.province[index].city)) return;
		var limit = agents.province[index].city.length;
		opt_cities.length = 1;	//reset city list
		for(i = 0; i < limit; i++) {
			var c = agents.province[index].city[i];
			opt_cities[i+1] = new Option(c.text, c.value);	
		}
	} // end if
} // end function

function provinceOnChange() {
	var form = document.forms['travelAgentForm'];
	if (form == null) return;
	if (form['province'] == null) return;
	if (form['city'] == null) return;
	var opt_provinces = form['province'].options;
	if (opt_provinces == null) return;
	if (opt_provinces.selectedIndex == 0) {
		form['city'].options.length = 1;	//reset city list
		return;
	}
	if(opt_provinces != null) {
		populateCities(opt_provinces.selectedIndex-1);
	} // end if
} // end function

function cityOnChange() {
	var form = document.forms['travelAgentForm'];
	if (form == null) return;
	if (form['province'] == null) return;
	if (form['city'] == null) return;
	var opt_provinces = form['province'].options;
	var opt_cities = form['city'].options;
	if (opt_provinces == null) return;
	if (opt_cities == null) return;
	if (opt_provinces.selectedIndex == 0) return;	//don't response on selecting default option
	if (opt_cities.selectedIndex == 0) return;	//don't response on selecting default option
	if(opt_provinces != null && opt_cities != null) {
		writeAgents(opt_provinces.selectedIndex-1, opt_cities.selectedIndex-1);
		repositionDivisions();
	} // end if
} // end function

function writeAgents(p, c) {
	if (agents.province == null) return;
	if (agents.province[p] == null) return;
	if (agents.province[p].city == null) return;
	if (agents.province[p].city[c] == null) return;
	var as = agents.province[p].city[c].agent;
	if (as == null) return;
	if (!isArray(as)) return;
	
	//dl
	var agentListHolder = document.getElementById('agentList');
	if (agentListHolder == null) return;

	//remove all child nodes
	while(agentListHolder.childNodes.length != 0) {
		agentListHolder.removeChild(agentListHolder.firstChild);
	}

	var rowCount = 0;
	for (var i=0; i<as.length; i++) {
		var name = as[i].name;
		var tel  = as[i].tel;
		if (name == null || tel == null) { continue; }
		if (name == '' || tel == '') { continue; }

		var className = (rowCount%2 == 0) ? 'odd' : 'even';
		rowCount++;

		var numLinesName = numLines(name);
		var numLinesTel = numLines(tel);

		var elm = document.createElement('div');
		elm.className = className + " left";
		elm.innerHTML = name;
		elm.style.height = max(numLinesName,numLinesTel)*20+"px"		
		agentListHolder.appendChild(elm);
		
		elm = document.createElement('div');
		elm.className = className + " right";
		elm.innerHTML = tel;
		elm.style.height = max(numLinesName,numLinesTel)*20+"px"
		agentListHolder.appendChild(elm);
	}
} // end function

// returns the number of lines in the text
function numLines(str) {
	var count=1;
	str = str.replace(/<br>/g,"<br/>");
	str = str.replace(/<br \/>/g,"<br/>");
	
	while(str.indexOf("<br/>")!=-1) {
		count++;
		str = str.substring(str.indexOf("<br/>")+5);
	}
	return count;
}

function max(x,y) {
	if (x>y) return x;
	else return y;
}