var elemtemp="";
var elem;
var cache;
//var flag=0;
var leftarray = '';
//var caseid=1;
//var parentTmp="";
var tmpObj;
var callfrommethod=0;
//var Domain_Path ="http://arabic.dev1.infoedge.com/";

function initFareaSuggestions(flistbox,designation){
	callfrommethod=1;
	var flistObj=document.getElementById(flistbox);
	var selValue =flistObj.options[flistObj.selectedIndex];	
	document.getElementById(designation).value='';
	document.getElementById('hiddenselected').value=selValue.text;
	//alert(selValue.text);
	myObj=tmpObj;
	myObj.getRPCData();
	myObj.parentTmp=selValue.text;
}

function createSuggestions_AR(elemid,param,dependant,foremail,multi,dependent_listID){
	elem=document.getElementById(elemid);
	if(document.getElementById('language') && (param == "arabicdesignation" || param == "arabiclocation")){objTextSuggested
			if(document.getElementById('language').value == "ar"){
		        //terminate = typeof(terminate)==undefined?false:terminate;
		        //var eleAr = document.getElementById(this.elem.id+"ar");
		        var objTextMain = elem.cloneNode(false);
		        var objTextSuggested = elem.cloneNode(false);
		        objTextSuggested.id = elem.id + "ar";
		        objTextSuggested.removeAttribute('name');
		        objTextSuggested.name = elem.name + "ar";
		        objTextMain.type = "hidden";
		        objParentNode = elem.parentNode;
		        objParentNode.replaceChild(objTextSuggested, elem);
		        objParentNode.appendChild(objTextMain);
		        new createSuggestions(objTextSuggested.id,param,dependant,foremail,multi);
		    }
        }
}

function createSuggestions(elemid,param,dependant,foremail,multi,dependent_listID,terminate){
	this.dependant=dependant;
	this.foremail=foremail;
	this.multi=multi;
	//parentTmp=param;
    this.parentTmp=param;
    this.caseid=1;
    var flag=0;
	this.parent_value='';
	this.elem=document.getElementById(elemid);
   // document.getElementById('hiddenselected').value='';
   // alert(dependent_listID);
	this.getRPCData=getRPCData2;
	this.getdata=getdata2;
	this.getHTTPObject=getHTTPObject2;
	this.handleHttpResponse=handleHttpResponse2;
	var http_suggest = getHTTPObject2();
	var myObj=this;
	if(param=='farea')
		tmpObj=myObj;
	else if(param =="arabicfareab2")
		tmpObj=myObj;
	if(param=='farea'){ flagx=0; }
	if(param=='designation') {flagy=0;  }
	if(param=='location'){ flagz=0;}
	/*if(document.getElementById('language') && (param == "arabicdesignation" || param == "arabiclocation")){
		if(document.getElementById('language').value == "ar"){
            terminate = typeof(terminate)==undefined?false:terminate;
            if(!terminate) {
                var eleAr = document.getElementById(this.elem.id+"ar");
                var objTextMain = this.elem.cloneNode(true);
                var objTextSuggested = this.elem.cloneNode(true);
                objTextSuggested.id = this.elem.id + "ar";
                objTextSuggested.removeAttribute('name');
                objTextSuggested.name = this.elem.name + "ar";
                objTextMain.type = "hidden";
                objParentNode = this.elem.parentNode;
                objParentNode.replaceChild(objTextSuggested, this.elem);
                objParentNode.appendChild(objTextMain);
                new createSuggestions(this.elem.id+"ar",param,dependant,foremail,multi,'',true);
            }
		}
	}*/
	if(param){
        if( dependant==1){
            var flist=document.getElementById(dependent_listID);
            var selectedValue =flist.options[flist.selectedIndex];
           // alert(flist.selectedIndex);
            if(flist.selectedIndex>0){
                this.parent_value=selectedValue.text;
                document.getElementById('hiddenselected').value=selectedValue.text;
            }
            else{
                this.parent_value=param;
               }
        }
         else
             this.parent_value=param;
		this.parent_value=this.parent_value.replace('&','@!');
		cache = new HashTable();
		myObj.getRPCData();
	}
	else{
		this.parent_value="";
		document.getElementById('hiddenselected').value="";
	}


                              

	function getHTTPObject2()
	{
	  var http_request;

		    if (window.XMLHttpRequest) { // For Mozilla and other browsers
		        http_request = new XMLHttpRequest();
		        if (http_request.overrideMimeType) {
		            http_request.overrideMimeType('text/xml');
		            }
		        }
		        else if (window.ActiveXObject) { // For Internet Explorer
		        try {
		            http_request = new ActiveXObject("Msxml2.XMLHTTP");
		        } catch (e) {
		            try {
		                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
		            }
		            catch (e) {}             }
		    }
		    if (!http_request) {
		        alert('Giving up :( Cannot create an XMLHTTP instance');
		        return false;
		    }

	  return http_request;
	}

	function getdata2()
	{
		try{
		
			myObj.flag=0;
            rnd = Math.random() * 10;
            if((myObj.parent_value == 'arabicdesignation' || myObj.parent_value == 'arabiclocation') && document.getElementById('language')){
            	if(document.getElementById('language').value == 'ar')
					http_suggest.open("GET",Domain_Path+"ni/suggestor/response.php?queryString="+myObj.parent_value.replace('&','@!')+"ar"+"&id="+myObj.caseid+"&rnd="+rnd,true);
				else
					http_suggest.open("GET",Domain_Path+"ni/suggestor/response.php?queryString="+myObj.parent_value.replace('&','@!')+"en"+"&id="+myObj.caseid+"&rnd="+rnd,true);
			}
			else{
				http_suggest.open("GET",encodeURI(Domain_Path+"ni/suggestor/response.php?queryString="+myObj.parent_value.replace('&','@!')+"&id="+myObj.caseid+"&rnd="+rnd,true));
			}
			//http_suggest.open("GET",Domain_Path+"ni/suggestor/response.php?queryString="+myObj.parent_value.replace('&','@!')+"&id="+myObj.caseid+"&rnd="+rnd,true);
			http_suggest.onreadystatechange = handleHttpResponse2;
			http_suggest.setRequestHeader("charset", "utf-8");
			http_suggest.send(null);
		}
		catch(exe){
		}
	}
/*
Function initiate rpc to createSuggestion
*/
	function getRPCData2()
	{
			if(document.getElementById('hiddenselected').value!=''&& callfrommethod==1){
				myObj.parent_value=document.getElementById('hiddenselected').value;
				callfrommethod=0;
				//alert(myObj.parent_value);
			}
			
			if(!cache.GetItem(myObj.parent_value)&&myObj.parent_value&&myObj.parent_value!='farea'){
				http_suggest = null;
				http_suggest = getHTTPObject2();
				//if(flag==1)	
					myObj.getdata();
			}
			else if(myObj.caseid<=38 && (myObj.parent_value=='farea'|| myObj.parent_value=='location'||  myObj.parent_value=='domain'||myObj.parent_value=='designation' || myObj.parent_value=='arabicdesignation'|| myObj.parent_value=='arabicdesignationb2'||myObj.parent_value=='arabicfareab2'||myObj.parent_value=='arabiclocation')&&myObj.parent_value==myObj.parentTmp){
				//alert(myObj.parent_value+"temp"+parentTmp);
				//if(flag==1)
					myObj.getdata();
			}
			else{
				var cacheresp = cache.GetItem(myObj.parent_value);
				new AutoSuggest(myObj.elem,cacheresp.split(';'),myObj.dependant,myObj.foremail,myObj.multi,myObj.parentTmp);
			}
			//document.getElementById('hiddenselected').value='';
	}

	function handleHttpResponse2()
	{
		try {
			if (http_suggest.readyState == 4){
			//alert(flag);
				myObj.flag=1;
				var suggestor_response = http_suggest.responseText;
				arrTmp=suggestor_response.split('|');
				if(!cache.GetItem(myObj.parent_value)){
					addToCache(arrTmp[2],arrTmp[0]);
				}
				else if(myObj.parent_value==arrTmp[2]){
					var tmp=arrTmp[0];
						arrTmp[0]=cache.GetItem(myObj.parent_value)+";"+tmp; //concat the new results of same fields
						addToCache(arrTmp[2],arrTmp[0]);
				}
				
				new AutoSuggest(myObj.elem,arrTmp[0].split(';'),myObj.dependant,myObj.foremail,myObj.multi,myObj.parentTmp);
				if(arrTmp[3]!='' && (myObj.parent_value=='farea'||myObj.parent_value=='designation'||myObj.parent_value=='location'|| myObj.parent_value=='domain'||myObj.parent_value=='arabicdesignation'||  myObj.parent_value=='arabicdesignationb2'||myObj.parent_value=='arabicfareab2'||myObj.parent_value=='arabiclocation')){
				myObj.parent_value=arrTmp[1];
				myObj.flag=1;
				if(arrTmp[3]<39 &&arrTmp[3]!=''){
				//if(arrTmp[3]!=''){ //uncomment it to show suggestion while working on field
						myObj.caseid=arrTmp[3];
						try {
							//myObj.parent_value=parentTmp
							if(myObj.flag==1)
								setTimeout(function() {myObj.getRPCData();},2000);
						}catch(e) { }
					}
				}
			
			}
		}
		catch(exe){
            alert(exe);
		}
		function addToCache(q, data) {
			cache.SetItem(q,data);
		}
	}
}




