/*******************************************************************
 * @author mpoisson
 * User Profile class
 ******************************************************************/
function Language() {
	this.id = 1;
	this.name = '';
}

Language.prototype.setID = function(id) {
	if (parseInt('0' + id, 10) == 0) {
		id=1;
	}
	this.id = id;
};

Language.prototype.getID = function() {
	return this.id;
};

Language.prototype.setName = function(name) {
	this.name = name;
};

Language.prototype.getName = function() {
	return this.name;
};

/******************************************************************
 * Language class end
 ******************************************************************/

/*******************************************************************
 * @author mpoisson	
 * Class used to handle language changes
 ******************************************************************/
function LanguageHandler() {
	this.hashMap = new Object();
};

LanguageHandler.prototype.TYPE_CONTROL = 1;
LanguageHandler.prototype.TYPE_IMG = 2;
LanguageHandler.prototype.TYPE_TAG_ID = 3;
LanguageHandler.prototype.TYPE_TAG_NAME = 4;

LanguageHandler.prototype.SERVICE_INDEX_ID = 0;
LanguageHandler.prototype.SERVICE_INDEX_VALUE = 1;
LanguageHandler.prototype.SERVICE_INDEX_TYPE = 2;
LanguageHandler.prototype.SERVICE_INDEX_TOOLTIP = 3;

// Calls language service 
LanguageHandler.prototype.load = function(lang, screen) {
	var service = null;
	var connector = new AjaxConnector();
	service = new Service("A0-001", oEnviroment.getServicePath() + oEnviroment.getServletName(), "");	
	service.addParameter("language", lang.getID());
	service.addParameter("screen", screen);
	service = connector.syncRun(service);
	return service;
};

/********************************************************************
 * Process service result and replaces translated items from html
 * Used only on async calls
 ********************************************************************/
LanguageHandler.prototype.process = function(service) {
	if (service !==null) {
		if (!service.getResult().hasErrors()) {
			var serviceRow = null;
			var translatedText = "";
			var elementId = "";
			var ctrlType = this.TYPE_CONTROL; 
			var tmp = null;
			
			while (service.getResult().hasNext()) {
				
				serviceRow = service.getResult().getCurrentRow().split("|!|");
			
				try {
					translatedText = '' + serviceRow[this.SERVICE_INDEX_VALUE];
					ctrlType = parseInt('0' + serviceRow[this.SERVICE_INDEX_TYPE], 10);
					elementId = '' + serviceRow[this.SERVICE_INDEX_ID];
					tooltip = '' + serviceRow[this.SERVICE_INDEX_TOOLTIP];
					
					// Replace text on different tags types
					if (ctrlType == this.TYPE_IMG) {
						tmp = $(elementId);
						this._changeAttribute(tmp, 'src', translatedText);
						if (tooltip.length) {
							this._changeAttribute(tmp, 'alt', tooltip);
							this._changeAttribute(tmp, 'title', tooltip);
						}
					} else if (ctrlType == this.TYPE_TAG_ID) {
						tmp = $(elementId);
						tmp.innerHTML = translatedText;
					} else if (ctrlType == this.TYPE_TAG_NAME) {
						tmp = document.getElementsByTagName(elementId);
						for (index=0; index < tmp.length; index++) {
							tmp[index].childNodes[0].nodeValue = translatedText;	
						}
					} else if (ctrlType == this.TYPE_CONTROL) {
						tmp = $(elementId);
						tmp.setAttribute('value', translatedText);
						if (tooltip.length) {
							this._changeAttribute(tmp, 'alt', tooltip);
							this._changeAttribute(tmp, 'title', tooltip);
						}
					} else {
						this.hashMap[elementId] = translatedText;
					}
				
				} catch (e) {
					//alert(e + " - " + elementId);
				}
						
				service.getResult().moveNext();
			}
		}
	}
};

// Retrieves translated text item
LanguageHandler.prototype.getText = function (key) {
	return this.hashMap[key];
};

// Retrieves translated text item
LanguageHandler.prototype.clear = function () {
	this.hashMap = new Object();
};

// Private: Changes attribute value to the specified element
LanguageHandler.prototype._changeAttribute = function (element, attrName, attrValue) {
	if (element.attributes.length) {
		for (index=0; index< element.attributes.length; index++) {
			if (element.attributes[index].nodeName.toLowerCase() == attrName) {
				element.attributes[index].nodeValue = attrValue;
			}
		}
	}
};
/******************************************************************
 * LanguageHandler class end
 ******************************************************************/