// JavaScript for ASPMaker 6+
//(C) 2001-2007 e.World Technology Ltd.




// Update a combobox with filter value
// object_value_array format
// object_value_array[n] = option value
// object_value_array[n+1] = option text 1
// object_value_array[n+2] = option text 2
// object_value_array[n+3] = option filter value
function ew_UpdateOpt(obj, object_value_array, parent_obj) { 
	var i, j, lo;
	var arValue = [];
	if (obj.options) {
		for (i=0; i<obj.options.length; i++) {
			if (obj.options[i].selected)
				arValue[arValue.length] = obj.options[i].value;
		}
	} else {
		arValue[arValue.length] = obj.value;
	}
	lo = (obj.type == "select-multiple") ? 0 : 1;
	for (i=obj.length-1; i>=lo; i--) {
		obj.options[i] = null;
	}
	if (parent_obj.options) {
		for (i=0; i<parent_obj.options.length; i++) {
			if (parent_obj.options[i].selected) { 
				for (j=0; j<object_value_array.length; j=j+4) {
					if (object_value_array[j+3].toUpperCase() == parent_obj.options[i].value.toUpperCase()) {
						ew_NewOpt(obj, object_value_array[j], object_value_array[j+1], object_value_array[j+2]); 
					}
				}
			}
		}
	} else {
		for (j=0; j<object_value_array.length; j=j+4) {
			if (object_value_array[j+3].toUpperCase() == parent_obj.value.toUpperCase()) {
				ew_NewOpt(obj, object_value_array[j], object_value_array[j+1], object_value_array[j+2]);
			}
		}
	}
	ew_SelectOpt(obj, arValue);
}

// Create combobox option 
function ew_NewOpt(obj, value, text1, text2) {
	var text = text1;
	if (text2 != "")
		text += EW_FIELD_SEP + text2;
	var optionName = new Option(text, value, false, false)
	obj.options[obj.length] = optionName;
}

// Select combobox option
function ew_SelectOpt(obj, value_array) {
	var i, j;
	for (i=0; i<value_array.length; i++) {
		for (j=0; j<obj.length; j++) {
			if (obj.options[j].value.toUpperCase() == value_array[i].toUpperCase()) {
				obj.options[j].selected = true;
				break;
			}
		}
	}
	if (obj.autoselect && obj.autoselect.toLowerCase() == "true") {
		if (obj.type == "select-one" && obj.options.length == 2 &&
			!obj.options[1].selected) {
			obj.options[1].selected = true;
		} else if (obj.type == "select-multiple" && obj.options.length == 1 &&
			!obj.options[0].selected) {
			obj.options[0].selected = true;
		}
	}
}





// Create XMLHTTP
// Note: AJAX feature requires IE5.5+, FF1+, and NS6.2+
function ew_CreateXMLHttp() {
	if (!(document.getElementsByTagName || document.all))
		return;		
	var ret = null;
	try {
		ret = new ActiveXObject('Msxml2.XMLHTTP');
	}	catch (e) {
	    try {
	        ret = new ActiveXObject('Microsoft.XMLHTTP');
	    } catch (ee) {
	        ret = null;
	    }
	}
	if (!ret && typeof XMLHttpRequest != 'undefined')
	    ret = new XMLHttpRequest();	
	return ret;
}

// Update a combobox with filter value by AJAX
function ew_AjaxUpdateOpt(obj, parent_obj, async) {
	if (!(document.getElementsByTagName || document.all))
		return;
	try {
		var i, j, lo;
		var arValue = [];
		if (obj.options) {
			for (i=0; i<obj.options.length; i++) {
				if (obj.options[i].selected)
					arValue[arValue.length] = obj.options[i].value;
			}
		} else {
			arValue[arValue.length] = obj.value;
		}
		lo = (obj.type == "select-multiple") ? 0 : 1;
		for (i=obj.length-1; i>=lo; i--) {
			obj.options[i] = null;
		}
		var s = eval('obj.form.s_' + obj.name + '.value');
		//var s = eval('s_' + obj.name);
		//if (!s || s == '' || filter_value == '') return;
		if (!s || s == '') return;
		var lc = eval('obj.form.lc_' + obj.name + '.value');
		if (!lc || lc == '') return;
		var ld1 = eval('obj.form.ld1_' + obj.name + '.value');
		if (!ld1 || ld1 == '') return;
		var ld2 = eval('obj.form.ld2_' + obj.name + '.value');
		if (!ld2 || ld2 == '') return;
		var lft = eval('obj.form.lft_' + obj.name + '.value');
		var xmlHttp = ew_CreateXMLHttp();
		if (!xmlHttp) return;
		var arSelValue = [];
		if (parent_obj.options) {
			for (i=0; i<parent_obj.options.length; i++) {
				if (parent_obj.options[i].selected)
					arSelValue[arSelValue.length] = encodeURIComponent(parent_obj.options[i].value); 
			}
		} else {
			arSelValue[arSelValue.length] = encodeURIComponent(parent_obj.value); 
		}
		xmlHttp.open('get', EW_LOOKUP_FILE_NAME + '?s=' + s + '&q=' + arSelValue.join(",") +
			'&lc=' + encodeURIComponent(lc) +
			'&ld1=' + encodeURIComponent(ld1) +
			'&ld2=' + encodeURIComponent(ld2) +
			'&lft=' + encodeURIComponent(lft), async);
		var f = function() {
		//alert(xmlHttp.responseText);
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200 &&
			xmlHttp.responseText) {
			//alert(xmlHttp.responseText);
			var object_value_array = xmlHttp.responseText.split('\r');
			for (var j=0; j<object_value_array.length-2; j=j+3) {
				ew_NewOpt(obj, object_value_array[j], object_value_array[j+1],
					object_value_array[j+2]);
			}
			ew_SelectOpt(obj, arValue);
		}
	}
	if (async || document.all)
		xmlHttp.onreadystatechange = f;
	xmlHttp.send(null);
	if (!async && !document.all)
		f();
	} catch (e) {}
}






// Functions for adding new option dynamically

// Show add option
function ew_ShowAddOption(id) {
	if (!document.getElementById) return;
	var elem;
	elem = document.getElementById("ao_" + id);
	if (elem) elem.style.display = "block"; 
	elem = document.getElementById("cb_" + id);
	if (elem)	elem.style.display = "none";	
}

// Hide add option
function ew_HideAddOption(id) {
	var elem;
	elem = document.getElementById("cb_" + id);
	if (elem)	elem.style.display = "inline"; 
	elem = document.getElementById("ao_" + id);
	if (elem) elem.style.display = "none"; 
}

// Post new option
function ew_PostNewOption(id) {
	var elem;
	var url = EW_ADD_OPTION_FILE_NAME + "?";
	elem = document.getElementById("ltn_" + id);
	url += "ltn=" + encodeURIComponent(elem.value);
	elem = document.getElementById("pfn_" + id);
	if (elem) url += "&pfn=" + encodeURIComponent(elem.value);
	elem = document.getElementById("pvn_" + id);
	if (elem) elem = document.getElementById(elem.value);
	if ((elem) && (elem.options) && (elem.selectedIndex != -1)) url += "&pf=" + encodeURIComponent(elem.options[elem.selectedIndex].value);
	elem = document.getElementById("pfq_" + id);
	if (elem) url += "&pfq=" + encodeURIComponent(elem.value);
	elem = document.getElementById("dfn_" + id);
	if (elem) url += "&dfn=" + encodeURIComponent(elem.value);
	elem = document.getElementById("dfq_" + id);
	if (elem) url += "&dfq=" + encodeURIComponent(elem.value);
	elem = document.getElementById("lfn_" + id);
	if (elem) url += "&lfn=" + encodeURIComponent(elem.value);
	elem = document.getElementById("lfq_" + id);
	if (elem) url += "&lfq=" + encodeURIComponent(elem.value);
	elem = document.getElementById("df2n_" + id);
	if (elem) url += "&df2n=" + encodeURIComponent(elem.value);
	elem = document.getElementById("df2q_" + id);
	if (elem) url += "&df2q=" + encodeURIComponent(elem.value);	

	var lf = document.getElementById("lf_" + id);
	var lfm = document.getElementById("lfm_" + id);
	if (lf) {
		if (ew_HasValue(lf)) {
			url += "&lf=" + encodeURIComponent(lf.value); 
		} else {
			if (!ew_OnError(lf, (lfm?lfm.value:"Missing link field value")))
				return false;		
		}
	}
	
	var df = document.getElementById("df_" + id);
	var dfm = document.getElementById("dfm_" + id);
	if (df) {
		if (ew_HasValue(df)) {
			url += "&df=" + encodeURIComponent(df.value); 
		} else {
			if (!ew_OnError(df, (dfm?dfm.value:"Missing display field value")))
				return false;
		}
	}
	
	var df2 = document.getElementById("df2_" + id);
	var df2m = document.getElementById("df2m_" + id);
	if (df2) {
		if (ew_HasValue(df2)) {
			url += "&df2=" + encodeURIComponent(df2.value); 
		} else {
			if (!ew_OnError(df2, (df2m?df2m.value:"Missing display field #2 value")))
				return false;
		}
	}
	
	try {			
		var xmlHttp = ew_CreateXMLHttp();
		if (!xmlHttp) return;		
		xmlHttp.open('get', url, true); // not Async
		xmlHttp.onreadystatechange = function() {
			//alert(xmlHttp.responseText);					
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200 &&
				xmlHttp.responseText) {				
				var opt = xmlHttp.responseText.split('\r');
				if (opt.length > 3 && opt[0]== 'OK') {
					var elem = document.getElementById(id);
					if (elem) {
						ew_NewOpt(elem, opt[1], opt[2], opt[3]);
						ew_HideAddOption(id);
						elem.options[elem.options.length-1].selected = true;
						if (elem.onchange) elem.onchange();
						elem.focus();
					}
				} else {
					alert(xmlHttp.responseText);
				}				
			}
		}		
		xmlHttp.send(null);
	}	catch (e) {}

}



