function removePlacesOptionData(fieldName) {
	if ( fieldName) {
		if ( fieldName.options)
			if ( fieldName.options.length > 0 )
				fieldName.options.length = 0 ;
	}
}
function disableOtherField(field) {
	if ( field) {
		field.value = "";
		field.disabled = true;
	}
}
function enableOtherField(field) {
	if ( field ) {
		field.value = "";
		field.disabled = false;
	}
}
function disableFields() {
	if ( arguments.length > 0 ) {
		for ( var i=0; i<arguments.length; i++ ) {
			var field = arguments[i];
			if ( field) {	
				field.disabled = true;
			}
		}
		if ( theForm.save)
			theForm.save.disabled = true;
	}
}
function enableFields() {
	if ( arguments.length > 0 ) {
		for ( var i=0; i<arguments.length; i++ ) {
			var field = arguments[i];
			if ( field) {	
				field.disabled = false;
			}
		}
		if ( theForm.save)
			theForm.save.disabled = false;
	}
}
var stateIdField = null;
var stateOtherField = null;
var cityIdField = null;
var cityOtherField = null;
var locationIdField = null;
var locationOtherField = null;
var theForm = null;
function onCountryChange(val, stateId, stateOther, cityId, cityOther, locationId, locationOther, form) {
	var countryId = val;
	setFields(stateId, stateOther, cityId, cityOther, locationId, locationOther, form);
	if ( countryId != "" && countryId != 0 ) {
		sendAjaxReq(updateStates, 'getStates', countryId, 'places.do');
		disableFields(stateId, stateOther, cityId, cityOther, locationId, locationOther);
	}
	else {
		setStateDefault();
		setCityDefault();
		setLocationDefault();
	}
}
function updateStates(stateXML) {
	enableFields(stateIdField, stateOtherField, cityIdField, cityOtherField, locationIdField, locationOtherField);
	removePlacesOptionData(stateIdField);
	var content = stateXML.getElementsByTagName("content")[0];
	var total = content.getAttribute("total");
	if ( total > 0 ) {
		if ( stateIdField )
			stateIdField.appendChild(createPlaceOption(getMessageById("ST-N"),""));
		var state = content.getElementsByTagName("state");
		for (var i = 0 ; i < state.length ; i++) {
			var item = state[i];
			var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		    var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
			if ( stateIdField )
				stateIdField.appendChild(createPlaceOption(name,id));
		}
		disableOtherField(stateOtherField);	
		setCityDefault();
		setLocationDefault();
	}
	else {
		setStateOther();
		setCityOther();
		setLocationOther();
	}
}
function setStateDefault() {
	removePlacesOptionData(stateIdField);
	disableOtherField(stateOtherField);
}
function setStateOther() {
	removePlacesOptionData(stateIdField);
	if ( stateIdField )
		stateIdField.appendChild(createPlaceOption(getMessageById("ST-O"),"1"));
	enableOtherField(stateOtherField);
}
function onStateChange(val, stateId, stateOther, cityId, cityOther, locationId, locationOther, form) {
	var stateId = val;
	setFields(stateId, stateOther, cityId, cityOther, locationId, locationOther, form);
	if ( stateId != "" && stateId != 0 && stateId != 1 ) {
		sendAjaxReq(updateCities, 'getCities', stateId, 'places.do');
		disableOtherField(stateOtherField);
		disableFields(cityId, cityOther, locationId, locationOther);
	}
	else if ( stateId == 1 ) {
		setStateOther();
		setCityOther();
		setLocationOther();
	}
	else {
		setCityDefault();
		setLocationDefault();
	}
}
function updateCities(cityXML) {
	enableFields(cityIdField, cityOtherField, locationIdField, locationOtherField);	
	removePlacesOptionData(cityIdField);
	var content = cityXML.getElementsByTagName("content")[0];
	var total = content.getAttribute("total");
	if ( total > 0 ) {
		if ( cityIdField )
			cityIdField.appendChild(createPlaceOption(getMessageById("CT-N"),""));
		var city = content.getElementsByTagName("city");
		for (var i = 0 ; i < city.length ; i++) {
			var item = city[i];
			var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		    var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
			if ( cityIdField )
				cityIdField.appendChild(createPlaceOption(name,id));
		}
		if ( cityIdField )
			cityIdField.appendChild(createPlaceOption(getMessageById("CT-O"),"1"));
		disableOtherField(cityOtherField);
		setLocationDefault();		
	}
	else {
		setCityOther();
		setLocationOther();
	}
}
function setCityDefault() {
	removePlacesOptionData(cityIdField);
	disableOtherField(cityOtherField);
}
function setCityOther() {
	removePlacesOptionData(cityIdField);
	if ( cityIdField )
		cityIdField.appendChild(createPlaceOption(getMessageById("CT-O"),"1"));
	enableOtherField(cityOtherField);
}
function onCityChange(val, stateId, stateOther, cityId, cityOther, locationId, locationOther, form) {
	var cityId = val;
	setFields(stateId, stateOther, cityId, cityOther, locationId, locationOther, form);
	if ( cityId != "" && cityId != 0 && cityId != 1 ) {
		sendAjaxReq(updateLocations, 'getLocations', cityId, 'places.do');
		disableOtherField(cityOtherField);
		disableFields(locationId, locationOther);
	}
	else if ( cityId == 1 ) {
		//setCityOther();
		enableOtherField(cityOtherField)
		setLocationOther();
	}
	else {
		setLocationDefault();
	}
}
function updateLocations(locationXML) {
	enableFields(locationIdField, locationOtherField);	
	removePlacesOptionData(locationIdField);
	var content = locationXML.getElementsByTagName("content")[0];
	var total = content.getAttribute("total");
	if ( total > 0 ) {
		if ( locationIdField )
			locationIdField.appendChild(createPlaceOption(getMessageById("LC-N"),""));
		var location = content.getElementsByTagName("location");
		for (var i = 0 ; i < location.length ; i++) {
			var item = location[i];
			var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		    var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
			if ( locationIdField )
				locationIdField.appendChild(createPlaceOption(name,id));
		}
		if ( locationIdField )
			locationIdField.appendChild(createPlaceOption(getMessageById("LC-O"),"1"));
		disableOtherField(locationOtherField);
	}
	else {
		setLocationOther();
	}
}
function setLocationDefault() {
	removePlacesOptionData(locationIdField);
	disableOtherField(locationOtherField);
}
function setLocationOther() {
	removePlacesOptionData(locationIdField);
	if ( locationIdField )
		locationIdField.appendChild(createPlaceOption(getMessageById("LC-O"),"1"));
	enableOtherField(locationOtherField);
}
function onLocationChange(val, stateId, stateOther, cityId, cityOther, locationId, locationOther, form) {
	var locationIdVal = val;
	setFields(stateId, stateOther, cityId, cityOther, locationId, locationOther, form);
	if ( locationId.type == "select-multiple") {
		var otherFlag = false;
		for (var i=0; i<locationId.options.length; i++) {
			if ( locationId[i].selected == true && locationId[i].value == "1") {
				otherFlag = true;
				break;
			}
		}
		if ( otherFlag == true)
			enableOtherField(locationOtherField);
		else
			disableOtherField(locationOtherField);	
	}
	else {
		if ( locationIdVal != "" ) {
			if ( locationIdVal == 1 )
				enableOtherField(locationOtherField);
			else
				disableOtherField(locationOtherField);
		}
		else
			disableOtherField(locationOtherField);
	}
}
function createPlaceOption(optionText, optionValue) {
	var option = document.createElement("option");
	option.setAttribute("value",optionValue);
	var text=document.createTextNode(optionText);
	option.appendChild(text);
	return option;
}

function setFields(stateId, stateOther, cityId, cityOther, locationId, locationOther, form) {
	stateIdField = stateId;
	stateOtherField = stateOther;
	cityIdField = cityId;
	cityOtherField = cityOther;
	locationIdField = locationId;
	locationOtherField = locationOther;
	theForm = form;
}
function showCountryCode(countryField, phoneField) {
	if ( countryField ) {
		var countryId = countryField.value;
		var countryCode = "";
		if (countryId == 197 )
			countryCode = "91";
		else if (countryId == "320")
			countryCode = "001";
		if ( phoneField && phoneField.value == "")
			phoneField.value = countryCode;
	}
}
