var currentlyActiveField;
var currentlyResultField;
var status;
var list_id;
var prev_id;
function activateInputField(inputFieldId,resultedDivId,statusValue){
if(document.getElementById(currentlyResultField))
document.getElementById(currentlyResultField).innerHTML="";
currentlyActiveField=inputFieldId;
currentlyResultField=resultedDivId;
status=statusValue;
}

function takeValue(liReference){
if(document.getElementById(currentlyActiveField)){
document.getElementById(currentlyActiveField).value=liReference.innerHTML;
}
if(document.getElementById(currentlyResultField)){
document.getElementById(currentlyResultField).innerHTML="";
}
}
function changeColour(liReference){
if(document.getElementById("list_"+list_id)){
document.getElementById("list_"+list_id).style.background="#FFFFFF";
}
liReference.style.background="#dcedad";
list_id=-1;


}
function reGainColour(liReference){
liReference.style.background="#FFFFFF";
}
function hideSearchValues(){
if(document.getElementById(currentlyResultField)){
document.getElementById(currentlyResultField).innerHTML="";
}
}
function completerFun(e) {
var KeyID = (window.event) ? event.keyCode : e.keyCode;

if(KeyID==38 || KeyID==40 || KeyID==13){
arrowKeysActions(KeyID);
   }else{
list_id=0;

	   var xmlHttp;
                           if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                                    xmlHttp = new XMLHttpRequest();
                              } else if (window.ActiveXObject) { // IE
                                   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
  if(document.getElementById(currentlyActiveField)!=null)                         
  var searchValue= document.getElementById(currentlyActiveField).value;
  
  var url="ajaxGetData.do?status="+status+"&value=";
  url=url+searchValue;
               xmlHttp.open('POST',url, true);
               xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
               xmlHttp.onreadystatechange = function() {
       													if (xmlHttp.readyState == 4) {
         																	 document.getElementById(currentlyResultField).innerHTML = xmlHttp.responseText ;
      																				 }
   														}
      			xmlHttp.send(null);
      			}
}
function arrowKeysActions(KeyID){
switch(KeyID)
   {
      case 13://enter
     if(document.getElementById(currentlyResultField)){
     hideSearchValues();
     }
      break;
      
      case 38://up arrow key
     list_id=list_id-1;
     if(document.getElementById("list_"+list_id)){
      document.getElementById("list_"+list_id).style.background="#dcedad";
      if(document.getElementById(currentlyActiveField)){
      document.getElementById(currentlyActiveField).value= document.getElementById("list_"+list_id).innerHTML;
      }
      prev_id=list_id+1;
      }else{
      prev_id=0;
      
      
     
      list_id=document.getElementById("totalNumber").value-1;
      if(document.getElementById("list_"+list_id)){
      document.getElementById("list_"+list_id).style.background="#dcedad";
      document.getElementById(currentlyActiveField).value= document.getElementById("list_"+list_id).innerHTML;
      }
      }
      if(document.getElementById("list_"+prev_id) && document.getElementById("list_"+list_id)){
       document.getElementById("list_"+prev_id).style.background="#FFFFFF";
      }
      break;//down arrow key
      case 40:
     list_id=list_id+1;
     if(document.getElementById("list_"+list_id)){
      document.getElementById("list_"+list_id).style.background="#dcedad";
      if(document.getElementById(currentlyActiveField)){
      document.getElementById(currentlyActiveField).value= document.getElementById("list_"+list_id).innerHTML;
      }
      prev_id=list_id-1;
      }else{
      prev_id=list_id-1;
      list_id=0;
      if(document.getElementById("list_"+list_id)){
      document.getElementById("list_"+list_id).style.background="#dcedad";
      document.getElementById(currentlyActiveField).value= document.getElementById("list_"+list_id).innerHTML;
      }
      }
      if(document.getElementById("list_"+prev_id) && document.getElementById("list_"+list_id)){
      document.getElementById("list_"+prev_id).style.background="#FFFFFF";
      }
      break;
   }
}
function getDropDownValues(slectedFieldId, resultedDivId,status,pageName){
var xmlHttp;
                           if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                                    xmlHttp = new XMLHttpRequest();
                              } else if (window.ActiveXObject) { // IE
                                   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
                            
  var searchValue= document.getElementById(slectedFieldId).value;
  
  var url="ajaxGetData.do?type=dropdown&status="+status+"&value=";
  url=url+searchValue;
  if(!pageName){
  }else if(pageName=="contactEdit"){
   url=url+"&pageName=contactEdit";
  }
  
               xmlHttp.open('POST',url, true);
               xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
               xmlHttp.onreadystatechange = function() {
       													if (xmlHttp.readyState == 4) {
         																	 document.getElementById(resultedDivId).innerHTML = xmlHttp.responseText ;
      																				 }
   														}
      			xmlHttp.send(null);
      			
      			
}
function removeZipValue(fieldId){
document.getElementById(fieldId).value="";
}
function getCitiesByState(slectedFieldId, resultedDivId,status,countrySelectedId,pageName){
var xmlHttp;
                           if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                                    xmlHttp = new XMLHttpRequest();
                              } else if (window.ActiveXObject) { // IE
                                   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
                            
  var searchValue= document.getElementById(slectedFieldId).value;
  var country= document.getElementById(countrySelectedId).value;

  var url="ajaxGetData.do?type=states&status="+status+"&country="+country+"&value=";
  url=url+searchValue;
    if(!pageName){
  }else if(pageName=="contactEdit"){
   url=url+"&pageName=contactEdit";
  }
  //alert(url);
               xmlHttp.open('POST',url, true);
               xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
               xmlHttp.onreadystatechange = function() {
       													if (xmlHttp.readyState == 4) {
         																	 document.getElementById(resultedDivId).innerHTML = xmlHttp.responseText ;
      																				 }
   														}
      			xmlHttp.send(null);
      			
      			
}
