// dropdown.js

var doc = {
	"metadata" : {
		type	:"Type"
	},
	
	"brand" : {
		tagname	: "SubBrand",
		title	: "subBrandTitle"
	},
	
	"product" : {
	 	title			: "productTitle",
	 	overview		: "supportOverview",
	 	download		: "supportDownload",
	 	documentation	: "supportDocumentation",
	 	community		: "supportCommunity"
	}
}; // end of doc


//Note: id compare is for fixing translation string bug. All text compare should be changed to use id compare.
function getTypesFromServer(request, argus) {
	var returnedString = request.responseText.split('\\'); // delete the garbage word
	
	var browser = new chkAjaBrowser();
	var opera	  = browser.bw.opera;
	var safari	  = browser.bw.safari;
	var konqueror = browser.bw.konqueror;
	var mozes	  = browser.bw.mozes;
	var msie	  = browser.bw.msie;
	
	if (msie) {
		execScript(returnedString[0]);
		var countOptions = options.option.length;
		for (i=0;i<countOptions;i++) {
			if (options.option[i]) {
				var option = document.createElement("option");
				option.value = options.option[i].value; // set option value
		
				//if (i == 0) // support type font setting
				if (options.option[i].text == "software" ||
					options.option[i].text == "Software" ||
					options.option[i].text == "Logiciels" ||
					options.option[i].text == "软件" ||
					options.option[i].text == "Systems" ||
					options.option[i].id == "Systems" ||
					options.option[i].text == "服务器" ||
					options.option[i].text == "Systèmes" ||
					options.option[i].text == "Services électroniques IBM" ||
					options.option[i].text == "IBMエレクトロニック・サービス" ||
					options.option[i].text == "IBM Electronic Services" ) {
					
				} else { // support category indent
					if (options.option[i].text == "My systems" ||
						options.option[i].text == "Mes systèmes" ||
						options.option[i].id == "My systems" ||
						options.option[i].id == "My システム" ||
						options.option[i].text == "My search with inventory data" ||
						options.option[i].text == "Mes recherches avec inventaire" ||
						options.option[i].text == "インベントリーを活用するMy 検索" ||
						options.option[i].id == "My search with inventory data" ||
						options.option[i].text == "Assist on-site" ||
						options.option[i].text == "Assistance en direct" ||
						options.option[i].text == "Assist on-site(US)" ||
						options.option[i].text == "Open a service request" ||
						options.option[i].id == "Open a service request") {
						options.option[i].text = '         ' + options.option[i].text;
					} else if (options.option[i].text == "Ouverture dune demande de service" ) {
						options.option[i].text = '         Ouverture d\'une demande de service';
					} else {
						options.option[i].text = '     ' + options.option[i].text;
					}
				}
				var text = document.createTextNode(options.option[i].text); // set option text
				argus.obj.appendChild(option);
				option.appendChild(text);
			}
		}
	} else {
		eval(returnedString[0]); // get options object
		var countOptions = options.option.length;
		//alert(options.option.length);
		for (i=0;i<countOptions;i++) { // add the types to options directly
			var option = document.createElement("option");
			option.value = options.option[i].value; // set option value
	
			//if (i == 0) // support type font setting
			if (options.option[i].text == "software" ||
				options.option[i].text == "Software" ||
				options.option[i].text == "Logiciels" ||
				options.option[i].text == "软件" ||
				options.option[i].text == "Systems" ||
				options.option[i].text == "服务器" ||
				options.option[i].id == "Systems" ||
				options.option[i].text == "Systèmes" ||
				options.option[i].text == "Services électroniques IBM" ||
				options.option[i].text == "IBMエレクトロニック・サービス" ||
				options.option[i].text == "IBM Electronic Services") {
				//option.style.color = "darkblue";
				option.style.fontWeight = "bold";
				//option.style.fontStyle = "italic";
			} else { // support category indent
				if (options.option[i].text == "My systems" ||
					options.option[i].text == "Mes systèmes" ||
					options.option[i].text == "My システム" ||
					options.option[i].id == "My systems" ||
					options.option[i].text == "My search with inventory data" ||
					options.option[i].text == "Mes recherches avec inventaire" ||
					options.option[i].text == "インベントリーを活用するMy 検索" ||
					options.option[i].id == "My search with inventory data" ||
					options.option[i].text == "Assist on-site" ||
					options.option[i].text == "Assist on-site" ||
					options.option[i].text == "Assist on-site(US)" ||
					options.option[i].text == "Assistance en direct" ||
					options.option[i].text == "Open a service request" ||
					options.option[i].id == "Open a service request" ) {
					option.style.textIndent = "24pt";
				} else if (options.option[i].text == "Ouverture dune demande de service" ) {
					options.option[i].text = 'Ouverture d\'une demande de service';
					option.style.textIndent = "24pt";
				} else {
					option.style.textIndent = "12pt";
				}
			}
			var text = document.createTextNode(options.option[i].text); // set option text
			argus.obj.appendChild(option);
			option.appendChild(text);
		}
	}
}

function getBrandsFromServer(request, argus) {
	var returnedString = request.responseText.split('\\');
	eval(returnedString[0]);
	
	var countOptions = options.option.length; // options got from returned result
	for (i=0;i<countOptions;i++) { // add the types to options directly
		var option = document.createElement("option");
		option.value = options.option[i].value; // set option value
		var text = document.createTextNode(options.option[i].text); // set option text
		//argus.obj.appendChild(option);
		option.appendChild(text);
		//if (i == 0) { // support type font setting
		if (text == "software" ||
			text == "Software" ||
			text == "Logiciels" ||
			text == "软件" ||
			text == "Systems" ||
			text == "服务器" ||
			text == "Systèmes" ||
			text == "Services électroniques IBM" ||
			text == "IBMエレクトロニック・サービス" ||
			text == "IBM Electronic Services") {
			//option.style.color = "darkblue";
			option.style.fontWeight = "bold";
			option.style.fontStyle = "italic";
		} else { // support category indent
			option.style.textIndent = "12px";
		}
	}
}

function getProductsFromServer(request, argus) {
	var returnedString = request.responseText.split('\\');
	eval(returnedString[0]);

	var countOptions = options.option.length;
	for (i=0;i<countOptions;i++) { // add the types to options directly
		var option = document.createElement("option");
		option.value = options.option[i].value; // set option value
		var text = document.createTextNode(options.option[i].text); // set option text
		argus.obj.appendChild(option);
		option.appendChild(text);
	}
}


function clearOptions(options) {
	// do nothing if option == 1
	if (options.length == 1 || options.id == "select_type") {
		return;
	}
	options.length = 1; // clear options
}

function select_change(obj) {
	if (obj.id == "select_brand") {
		if (obj.value == "noselect") {
			clearOptions(document.getElementById("select_product"));
			document.getElementById("second_dropdown").style.display = "none";
			document.getElementById("go2").style.display = "inline";
			document.getElementById("go3").style.display = "none";
		} else {
			var types = obj.options[obj.selectedIndex].text.LTrim();
			clearOptions(document.getElementById("select_product"));
			send_argus.callback 	= getProductsFromServer;
			send_argus.element.obj 	= document.getElementById("select_product");
			send_argus.element.value= types;
			//send_argus.data			= '&p1=3&p2='+document.getElementById("select_brand").tagName+'&p3='+send_argus.element.value;
			send_argus.data			= '&p1=3&p2='+''+'&p3='+send_argus.element.value;
			send_argus.method		= 'GET';
			sendRequest(send_argus.callback, 
						send_argus.element, 
						send_argus.data, 
						send_argus.method, 
						send_argus.url, 
						true, 
						true);
			if (types == "Information Management(US)" ||
				types == "Lotus(US)" ||
				types == "Rational(US)" ||
				types == "Tivoli(US)" ||
				types == "WebSphere(US)" ||
				types == "Other software(US)" ||
				types == "Information Management" ||
				types == "Lotus" ||
				types == "WebSphere" ||
				types == "Autres logiciels" ||
				types == "Rational" ||
				types == "Tivoli" ||
				types == "Other software") {
				document.getElementById("second_dropdown").style.display = "inline";
			} else {
				document.getElementById("second_dropdown").style.display = "none";
			}
			document.getElementById("go2").style.display = "inline";
			document.getElementById("go3").style.display = "none";
		}
	} else if (obj.id == "select_product") {
		if (obj.value == "noselect") {
			document.getElementById("go2").style.display = "inline";
			document.getElementById("go3").style.display = "none";
		} else {
			document.getElementById("go2").style.display = "none";
			document.getElementById("go3").style.display = "inline";
		}
	}
}

function link_to(go_obj) {
	if (go_obj.id == "go1") {
		//var posi_type = document.forms['dropdownlist_form'].elements['select_type'].value;
		var type_element = document.getElementById("select_type");
		var posi_type = type_element.options[type_element.selectedIndex].value;
		if (posi_type == "noselect") {
			//document.forms["dropdownlist_form"].elements["select_type"].focus;
			return;
		} else {
			location.href = posi_type;
		}
	}
	if (go_obj.id == "go2") {
		//var posi_brand = document.forms['dropdownlist_form'].elements['select_brand'].value;
		var brand_element = document.getElementById("select_brand");
		var posi_brand = brand_element.options[brand_element.selectedIndex].value;
		if (posi_brand == "noselect") {
			//document.forms["dropdownlist_form"].elements["select_brand"].focus;
			return;
		} else {
			location.href = posi_brand;
		}
	}
	if (go_obj.id == "go3") {
		//var posi_product = document.forms['dropdownlist_form'].elements['select_product'].value;
		var product_element = document.getElementById("select_product");
		var posi_product = product_element.options[product_element.selectedIndex].value;
		if (posi_product == "noselect") {
			//document.forms["dropdownlist_form"].elements["select_type"].focus;
			return;
		} else {
			location.href = posi_product;
		}
	}
}

function on_load(load_select) {
	send_argus.callback		= getTypesFromServer;
	//send_argus.callback		= getBrandsFromServer;
	send_argus.element.obj 	= load_select;
	send_argus.data			= '&p1=1&p2=ga';
	send_argus.method		= 'GET';
	sendRequest(send_argus.callback, 
				send_argus.element,
				send_argus.data, 
				send_argus.method, 
				send_argus.url, 
				true, 
				true);
}

function getPageLocale() {
	var html = parent.document.documentElement;
	var locale = html.getAttribute("lang");
	return locale.replace("-","_");
}

String.prototype.LTrim = function() {
	return this.replace(/(^\s*)/g, "");
}
