
var dictionaryList = new Array();
var dictionaryRequestList = new Array();
var countDict = 0;

/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */

function Dictionary(dictionaryName, dictId, questionName, showDictionary, url, reletedName, urlParams, defaultValue) {
	
	this.dictionaryName = dictionaryName;
	this.questionName = questionName;	
	this.dictId = dictId;
	this.showDictionary = showDictionary;
	this.url = url;
	this.defaultValue = defaultValue;
	this.reletedName = reletedName;
	this.urlParams = urlParams;
}


Dictionary.prototype.getDictionary = function(controlNumber, menuContentJson){
	if(controlNumber==dictionaryRequestList[this.dictId]) {
		if(menuContentJson != null){
	        var dictionary = menuContentJson.dictionary;
	        var values = menuContentJson.values;
	        if(this.showDictionary!=null) {
		        this.showDictionary(dictionary, values, this.questionName, this.defaultValue, this.reletedName);
		        updateDicts(this.questionName, dictionaryList);
	        }
	     }
     }
}



Dictionary.prototype.executeUrl = function(){	
	if(document.getElementById(this.questionName + 'Process') && document.getElementById(this.questionName + 'Process')!=null) {
		document.getElementById(this.questionName + 'Process').style.visibility = 'visible';
	}

	var invocationURL = this.url + '&dictId=' + this.dictId;
	if(this.dictionaryName && this.dictionaryName != ''){
		invocationURL = invocationURL + '&dictionaryName=' + this.dictionaryName;
	}	
	if( this.urlParams ){
		for(var i = 0; i< this.urlParams.length; i++ ){
			var paramName = this.urlParams[i].paramName;
			var paramValue = this.urlParams[i].paramValue;
			if( this.reletedName ){
				if( containsInArray(this.reletedName, paramValue) ){
					paramValue = getValueQuestion(paramValue);
				}
			}
			invocationURL += '&' + paramName + '=';
			invocationURL += paramValue;

		}
		invocationURL += '&values='
		var value = getValueQuestion(this.questionName);
		if((value == null || value == '') && this.defaultValue){
			value = this.defaultValue;
		}
		invocationURL += value;
	}
	var requestNum = ++dictionaryRequestList[this.dictId];
	invocationURL += "&rNum=" + requestNum;
	
	//scriptElement=document.getElementById('dictScript' + this.questionName);
	headElement = document.getElementsByTagName('head').item(0);

    //if (scriptElement != null) {
    //    headElement.removeChild(scriptElement);
    //}
    
	var scriptTag = document.createElement("script");
	scriptTag.setAttribute("id", 'dictScript' + this.questionName);
    scriptTag.setAttribute("type", "text/javascript");
	scriptTag.setAttribute("src", invocationURL);
    headElement.appendChild(scriptTag);
}




function updateDicts(inputName, dictionaryList){
	if( dictionaryList ){
		for(var i = 0; i<dictionaryList.length; i++ ){
			if( containsInArray(dictionaryList[i].reletedName, inputName) && inputName!=dictionaryList[i].questionName ){
				dictionaryList[i].executeUrl();
			}
		}
	}
}

function updateRelateDicts(dictionaryList){
	if( dictionaryList ){
		for(var i in dictionaryList ){
			if(dictionaryList[i].reletedName.length > 0){
				dictionaryList[i].executeUrl();
			}
		}

	}
}




