/*
 * Copyright (c) 2005 Absolutely Training Limited
 *  
 * Created on 08-Feb-2005
 *
 * @author roberto
 */
 
/**
	0, no messages shown
	1, meaningfull messases for user showm
	2, all message shown, including debuging ones. 
*/
var debug = 2;

//init
new RTEDataObject(); 

 // constructor
function RTEDataObject() {}

RTEDataObject.prototype.get = function (tempVariable){	

	var variable = tempVariable.replace(/^cmi\./, "").replace(/^adl\.nav\./, "");
	var localProperty = variable;
	if (variable.indexOf(".") > 0) {
		localProperty = variable.substring(0, variable.indexOf("."));
	}
	var remainder = variable.substring(variable.indexOf(".") + 1, variable.length);
	var getterName = "get" + localProperty.substring(0,1).toUpperCase() + localProperty.substring(1, localProperty.length);
	var getterFunction = this[getterName];
	
	if (getterFunction != null) {
		var variableInfo = this[getterName].apply(this, new Array());
		return variableInfo;
	} 
	else {
		if (this[localProperty] != null && variable.indexOf(".") > 0) {
			return this[localProperty].get(remainder);							
		} else {
			if (this[remainder] != null){
				var info = new VariableInfo("0", "", this[remainder]);		
				return info;
			} else if (this[remainder] == null){
				return VariableInfo.ERROR_403;
			} else {			
				//TODO if is not null, it must be undefined, so return a 301
				return VariableInfo.GET_401;
			}
		}
	}	
};

RTEDataObject.prototype.set = function (tempVariable, value){

	var variable = tempVariable.replace(/^cmi\./, "").replace(/^adl\.nav\./, "");
	var localProperty = variable;
	if (variable.indexOf(".") > 0){
		localProperty = variable.substring(0, variable.indexOf("."));
	}
	var remainder = variable.substring(variable.indexOf(".")+1, variable.length);
	var setterName = "set" + localProperty.substring(0,1).toUpperCase() + localProperty.substring(1, localProperty.length);
    var setterFunction = this[setterName];
	if (setterFunction != null){
		// the remainder will only be used if the attribute to set is an object. It is needed for dependency errors
		var args = new Array(value, remainder);		
		var variableInfo = this[setterName].apply(this, args);
		return variableInfo;
	} 
	else {
		if (this[localProperty] != null) {			
			return this[localProperty].set(remainder, value);
		} else {
			if(debug>1){
				alert("undefined :" + tempVariable + "/" + value);
			}
				return VariableInfo.SET_401;
			
		}
	}
};

RTEDataObject.prototype.setDirect = function (tempVariable, value){
	var variable = tempVariable.replace(/^cmi\./, "").replace(/^adl\.nav\./, "");
	if (variable.indexOf(".") == -1) {
		if (variable != "")
		{
			if (this[variable] + "" != "undefined"){
				this[variable] = value;	
				return VariableInfo.NO_SET_ERROR;
			}
			else {
				if (debug>1) alert("Can't setDirect value: " + variable + " element does not exist");
				return VariableInfo.ERROR_404;
			}
		}
		//TODO this is suggesting that no error has occured, when in reality the variable was not found.
		else {
			return VariableInfo.NO_SET_ERROR;
		}
	} 
	else {
		var localProperty =  variable.substring(0, variable.indexOf("."));
		var remainder = variable.substring(variable.indexOf(".") + 1, variable.length);
		if (this[localProperty] != null || this[localProperty] + "" != "undefined") {	
			return this[localProperty].setDirect(remainder, value);
		} else {
			if (debug>1) alert("Cant setDirect value: " + variable + ".Element does not exist");
			return VariableInfo.ERROR_404;
		}
	}
};

RTEDataObject.prototype.set_children = function(_children){
	    return VariableInfo.ERROR_404;
};

/**
 * print attributes for this object to string, for those attributes that are not null.
 */
RTEDataObject.prototype.printMe = function(type){

	var localProperty = null;	
	var valuePairs = "";
	var objectInArray;

	for (var i = 0; i < this.propertiesArray.length; i++){            
		if (this[this.propertiesArray[i]] != null) {			
			var newPair = type + this.propertiesArray[i] + "=" + this[this.propertiesArray[i]] + VALUE_PAIR_STRING_DELIMITER;
			valuePairs = valuePairs + newPair;
		}
	}
	if (this.objectsArray != null){
		for (var i = 0; i < this.objectsArray.length; i++){            
		   objectInArray = this[this.objectsArray[i]];
		   var strObjectArray = objectInArray.printMe(type + this.objectsArray[i] + ".");
		   valuePairs += strObjectArray;
		   if (strObjectArray.length > 0) {
	   		   valuePairs += VALUE_PAIR_STRING_DELIMITER;
		   }
		}
	}
	//strips out trailing VALUE_PAIR_STRING_DELIMITER
	valuePairs = valuePairs.substring(0, valuePairs.length - VALUE_PAIR_STRING_DELIMITER.length);
	return valuePairs
};
