/**
 * create VOX.USER namespace if it isn't exist
 */
if (typeof VOX == "undefined") {
	var VOX = {};
}
if (typeof VOX.BOT == "undefined") {
	VOX.BOT = {};
}

/**
 * The utility class for analysing data from backend
 */
VOX.BOT.DataAnalyse = function(){
	
}


/**
 * The defination of the class DataEngine.
 * this class is responsible for data get
 */
VOX.BOT.DataEngine = function(){
	// initial data analyse object
	this._dataAnalyseObj = new VOX.BOT.DataAnalyse();
}

/**
 * the property's defination of class DataEngine
 */
VOX.BOT.DataEngine.prototype = {
	/** CONSTRAINT DEFINATION **/
	//HOST_ABSOLUTE : "http://192.168.1.188:8080/",                // for develop test
	//HOST_RELATIVE : "http://192.168.1.188:8080/vox",       // for develop test
	HOST_ABSOLUTE : "http://"+window.location.host+"/", // for deploy
	HOST_RELATIVE : "/"+window.location.href.replace(new RegExp("/+", 'g'), "/").split("/")[2], // for deploy

	/** PATH CONSTRANT **/
	GET_BOT_RESPONSE_PATH : "/GetResponse.action",
	/**GET_BOT_RESPONSE_PATH : "/GetResponseFromContextBot.action",**/
	
	getHostAbsoluteUrl : function(){
		return this.HOST_ABSOLUTE;
	},
	getResponse:function(input,wave_file_name,voice_id,bot_id,intercepted,dataEngineListener, sentence_mode){
		if(input==null){
			alert("[ERROR]user input message is null!");
			return;
		}

		var xhr;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
		      xhr = new XMLHttpRequest(); 
		} else if (window.ActiveXObject) { // IE 
		      try {
				 xhr = new ActiveXObject("MSXML2.XMLHTTP");
				} catch(e) {
				 xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
		}
		var myself=this;
		var request_path=this.HOST_RELATIVE+this.GET_BOT_RESPONSE_PATH+"?BOT_inputText="+input + "&sentence_mode=" + sentence_mode;
		//prompt("",request_path);
		if(wave_file_name!=null)
			request_path+="&BOT_wavFileName="+wave_file_name;
		if(voice_id!=null)
			request_path+="&BOT_voiceId="+voice_id;
		if(bot_id!=null)
			request_path+="&BOT_botId="+bot_id;
		if(intercepted!=null)
			request_path+="&BOT_intercepted="+intercepted;
		request_path += "&boutFin=" + document.getElementById("bout_fin").value;
        xhr.open("POST", request_path, true);
      	//prompt("",request_path);
        xhr.setRequestHeader("Content-Type", "Application/x-www-form-urlencoded");
        xhr.onreadystatechange=function(){
        	if (xhr.readyState == 4) {
        		if (xhr.status == 200) {
        			var a=[];
        			try {
        				a[0]=xhr.getResponseHeader("BOT_outputText");
        				a[1]=xhr.getResponseHeader("USER_AMOUNT_OF_SENTENCES");
        				a[2]=xhr.getResponseHeader("USER_AVERAGE_SENTENCES_LENGTH");
        				a[3]=xhr.getResponseHeader("USER_AMOUNT_OF_VOCABULARY");
        				a[4]=xhr.getResponseHeader("USER_WORD_FREQUENCY");
        				a[5]=xhr.getResponseHeader("USER_INPUT_CHECKED");
        				a[6]=xhr.getResponseHeader("USER_SCORE_SESSION");
        				a[7]=xhr.getResponseHeader("PROCESS_RESULT");
        				a[8]=xhr.getResponseHeader("USER_SCORE");       	
        				a[9]=xhr.getResponseHeader("USER_LOGGEDIN");       	
        				a[10]=xhr.getResponseHeader("BOT_rightHint");
        				a[11]=xhr.getResponseHeader("BOUT_FIN");
        				a[12]=xhr.getResponseHeader("TTS_inputText");
        				a[13]=xhr.getResponseHeader("changeColor");
        				a[14]=xhr.getResponseHeader("newWords");
        				a[15]=xhr.getResponseHeader("bgcolor");
        				//by CSI.1111
        				
        				a[16]=xhr.getResponseHeader("UpdateReminder");
						dataEngineListener.success(a);
        			} catch(e) {
        				dataEngineListener.failure(-101, 'response parse failed');
        			} finally {
        				delete xhr.onreadystatechange;
        				xhr = null;
        			} 
        		} else {
        			dataEngineListener.failure(xhr.status, xhr.statusText);
        			delete xhr.onreadystatechange;
        			xhr = null
        		}
        	}
        }
        xhr.send("");
	}
}
