//cteate AJAX object
function createAJAXObj(){
	var xmlHttp;
	try
    {   // Firefox, Opera 8.0+, Safari 
		xmlHttp=new XMLHttpRequest();
    }
	catch (e){    // Internet Explorer
		try{      
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      
		}catch (e){ 
        try{   
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                
        }catch (e){        
                  return null;
         }
		}
	}
  	return xmlHttp
}

  function citySuggest(city, formNo, idName)
  {		
	if(city != null){
		city = trimAll(city);
	}
 	e = window.event;
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	  if(city != null && city.length > 0){
		if(KeyID != 13){  
			var ajaxObj = createAJAXObj();
			if(formNo == null) formNo = "0";
			else if(formNo.length == 0) formNo = "0";
	
			if(ajaxObj != null && city.length > 0){
				ajaxObj.onreadystatechange=function()
				{
					if(ajaxObj.readyState==4)
					{	//alert(ajaxObj.responseText);
						if(ajaxObj.responseText != null){
							document.getElementById(idName).innerHTML=ajaxObj.responseText;
							overLayer =document.getElementById("searchcityselect");
							var myReference = document.forms[formNo].searchCity;
							overLayer.style.position = "absolute";
							overLayer.style.left = myReference.style.left;
							overLayer.style.top = myReference.style.top;
							overLayer.style.width = myReference.style.width;
							overLayer.style.height = myReference.style.height;
							document.getElementById(idName).style.display="block";
						}
						if(ajaxObj.responseText != null){
							aa = ajaxObj.responseText.substring(0, 4);
							if(aa == "<div"){
								document.getElementById(idName).style.display="none";
								upst = document.forms[formNo].updateState.value;
								if(upst != "")
									document.forms[formNo].searchState.value=upst;
							}
						}
						
					}
				}
                ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityState", true);
				ajaxObj.send(null);

            }
		  }else{	updateCityState(formNo);
					document.getElementById(idName).innerHTML="";
					document.getElementById(idName).style.display="none";
		  	   }
	  }else{
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }

  }

function citySuggest(event, city, formNo, idName)
{
    if (!event) {
        if(window.event) {
            e = window.event;
        } else {
            return;
        }
    } else {
        e = event;
    }

    if(city != null){
        city = trimAll(city);
    }

   //     e = window.event;
  if(e.keyCode)
      KeyID = e.keyCode;
  else if(e.which)
          KeyID = e.which;
  else if(e.charCode)
          KeyID = e.charCode;
    if(city != null && city.length > 0){
      if(KeyID != 13){
          var ajaxObj = createAJAXObj();
          if(formNo == null) formNo = "0";
          else if(formNo.length == 0) formNo = "0";

          if(ajaxObj != null && city.length > 0){
              ajaxObj.onreadystatechange=function()
              {
                  if(ajaxObj.readyState==4)
                  {	//alert(ajaxObj.responseText);
                      if(ajaxObj.responseText != null){
                          document.getElementById(idName).innerHTML=ajaxObj.responseText;
                          overLayer =document.getElementById("searchcityselect");
                          var myReference = document.forms[formNo].searchCity;
                          overLayer.style.position = "absolute";
                          overLayer.style.left = myReference.style.left;
                          overLayer.style.top = myReference.style.top;
                          overLayer.style.width = myReference.style.width;
                          overLayer.style.height = myReference.style.height;
                          document.getElementById(idName).style.display="block";
                      }
                      if(ajaxObj.responseText != null){
                          aa = ajaxObj.responseText.substring(0, 4);
                          if(aa == "<div"){
                              document.getElementById(idName).style.display="none";
//								upst = document.forms[formNo].updateState.value;
                              //alert(document.getElementById(idName).innerHTML);
                              var hiddenState = document.getElementsByName("updateState");
                              if (hiddenState.length > 0)  {
                                  upst = hiddenState[0].value;
                                  if(upst != "")
                                      document.forms[formNo].searchState.value=upst;
				      document.forms[formNo].searchCountry.value=getCountry(upst);
                              }
                          }
                      }

                  }
              }
              ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityState", true);
              ajaxObj.send(null);
          }
        }else{	updateCityState(formNo);
                  document.getElementById(idName).innerHTML="";
                  document.getElementById(idName).style.display="none";
               }
    }else{
              document.getElementById(idName).innerHTML="";
              document.getElementById(idName).style.display="none";
    }

}

  function citySuggestOtravel(city, formNo, idName)
  {	
  	if(city != null){
		city = trimAll(city);
	}
 	e = window.event;
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	  if(city != null && city.length > 0){
		  if(KeyID != 13){  
			var ajaxObj = createAJAXObj();
			if(formNo == null) formNo = "0";
			else if(formNo.length == 0) formNo = "0";
	
			if(ajaxObj != null && city.length > 0){
				ajaxObj.onreadystatechange=function()
				{	
					if(ajaxObj.readyState==4)
					{	//alert(ajaxObj.responseText);
						if(ajaxObj.responseText != null){
							document.getElementById(idName).innerHTML=ajaxObj.responseText;
							overLayer =document.getElementById("searchcityselect");
							var myReference = document.forms[formNo].searchOCity;
							overLayer.style.position = "absolute";
							overLayer.style.left = myReference.style.left;
							overLayer.style.top = myReference.style.top;
							overLayer.style.width = myReference.style.width;
							overLayer.style.height = myReference.style.height;
							document.getElementById(idName).style.display="block";
						}
						if(ajaxObj.responseText != null){
							aa = ajaxObj.responseText.substring(0, 4);
							if(aa == "<div"){
								//document.getElementById(idName).style.display="none";
								upst = document.forms[formNo].updateState.value;
								if(upst != "")
									document.forms[formNo].searchOState.value=upst;
							}
						}
						
					}
				}
				ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityOState", true);
				ajaxObj.send(null);
			}
		  }else{	
		  			updateCityOState(formNo);
					document.getElementById(idName).innerHTML="";
					document.getElementById(idName).style.display="none";
		  }
	  }else{
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }

  }

function citySuggestOtravel(event, city, formNo, idName)
{
    if (!event) {
        if (window.event) {
            e = window.event;
        } else {
            return;
        }
    } else {
        e = event;
    }

    if (city != null) {
        city = trimAll(city);
    }
    //e = window.event;
    if (e.keyCode)
        KeyID = e.keyCode;
    else if (e.which)
        KeyID = e.which;
	else if(e.charCode)
			KeyID = e.charCode;
	  if(city != null && city.length > 0){
		  if(KeyID != 13){
			var ajaxObj = createAJAXObj();
			if(formNo == null) formNo = "0";
			else if(formNo.length == 0) formNo = "0";

			if(ajaxObj != null && city.length > 0){
				ajaxObj.onreadystatechange=function()
				{
					if(ajaxObj.readyState==4)
					{	//alert(ajaxObj.responseText);
						if(ajaxObj.responseText != null){
							document.getElementById(idName).innerHTML=ajaxObj.responseText;
							overLayer =document.getElementById("searchcityselect");
							var myReference = document.forms[formNo].searchOCity;
							overLayer.style.position = "absolute";
							overLayer.style.left = myReference.style.left;
							overLayer.style.top = myReference.style.top;
							overLayer.style.width = myReference.style.width;
							overLayer.style.height = myReference.style.height;
							document.getElementById(idName).style.display="block";
						}
						if(ajaxObj.responseText != null){
							aa = ajaxObj.responseText.substring(0, 4);
							if(aa == "<div"){
								//document.getElementById(idName).style.display="none";
                                var hiddenState = document.getElementsByName("updateState");
                                if (hiddenState.length > 0)  {
                                    upst = hiddenState[0].value;
                                    if(upst != "")
                                        document.forms[formNo].searchOState.value=upst;
                                }

/*
								upst = document.forms[formNo].updateState.value;
								if(upst != "")
									document.forms[formNo].searchOState.value=upst;
*/
							}
						}

					}
				}
				ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityOState", true);
				ajaxObj.send(null);
			}
		  }else{
		  			updateCityOState(formNo);
					document.getElementById(idName).innerHTML="";
					document.getElementById(idName).style.display="none";
		  }
	  }else{
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }

  }

  function citySuggestDtravel(city, formNo, idName)
  {	
    if(city != null){
		city = trimAll(city);
	}
 	e = window.event;
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	  if(city != null && city.length > 0){
		 	if(KeyID != 13){  
  	
		//alert(cityPropertyName);
		var ajaxObj = createAJAXObj();
		if(formNo == null) formNo = "0";
		else if(formNo.length == 0) formNo = "0";
		//alert("1111"+idName);

		if(ajaxObj != null && city.length > 0){
			ajaxObj.onreadystatechange=function()
			{	
				if(ajaxObj.readyState==4)
				{	//alert(ajaxObj.responseText);
					if(ajaxObj.responseText != null){
						document.getElementById(idName).innerHTML=ajaxObj.responseText;
						overLayer =document.getElementById("searchcityselect");
						var myReference = document.forms[formNo].searchDCity;
						overLayer.style.position = "absolute";
						overLayer.style.left = myReference.style.left;
						overLayer.style.top = myReference.style.top;
						overLayer.style.width = myReference.style.width;
						overLayer.style.height = myReference.style.height;
						document.getElementById(idName).style.display="block";
					}
						if(ajaxObj.responseText != null){
							aa = ajaxObj.responseText.substring(0, 4);
							if(aa == "<div"){
                                //document.getElementById(idName).style.display="none";
                                alert("formNo = "+formNo);
                                upst = document.forms[formNo].updateState.value;
								if(upst != "")
                                    document.getElementsByName("searchDState")[0].value=upst;
							}
						}
					
				}
			}
			ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityDState", true);
			ajaxObj.send(null);
		}
	  }else{	updateCityDState(formNo);
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }
	  }else{
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }

  }

function citySuggestDtravel(event, city, formNo, idName)
{
    if (!event) {
        if (window.event) {
            e = window.event;
        } else {
            return;
        }
    } else {
        e = event;
    }

    if (city != null) {
        city = trimAll(city);
    }
    //e = window.event;
    if (e.keyCode)
        KeyID = e.keyCode;
    else if (e.which)
        KeyID = e.which;
	else if(e.charCode)
			KeyID = e.charCode;
	  if(city != null && city.length > 0){
		 	if(KeyID != 13){

		//alert(cityPropertyName);
		var ajaxObj = createAJAXObj();
		if(formNo == null) formNo = "0";
		else if(formNo.length == 0) formNo = "0";
		//alert("1111"+idName);

		if(ajaxObj != null && city.length > 0){
			ajaxObj.onreadystatechange=function()
			{
				if(ajaxObj.readyState==4)
				{	//alert(ajaxObj.responseText);
					if(ajaxObj.responseText != null){
						document.getElementById(idName).innerHTML=ajaxObj.responseText;
						overLayer =document.getElementById("searchcityselect");
						var myReference = document.forms[formNo].searchDCity;
						overLayer.style.position = "absolute";
						overLayer.style.left = myReference.style.left;
						overLayer.style.top = myReference.style.top;
						overLayer.style.width = myReference.style.width;
						overLayer.style.height = myReference.style.height;
						document.getElementById(idName).style.display="block";
					}
						if(ajaxObj.responseText != null){
							aa = ajaxObj.responseText.substring(0, 4);
							if(aa == "<div"){
                                //document.getElementById(idName).style.display="none";
                                var hiddenState = document.getElementsByName("updateState");
                                if (hiddenState.length > 0)  {
                                    upst = hiddenState[0].value;
                                    if(upst != "")
                                        document.forms[formNo].searchDState.value=upst;
                                }
/*
                                alert("formNo = "+formNo);
                                upst = document.forms[formNo].updateState.value;
								if(upst != "")
									document.forms[formNo].searchDState.value=upst;
*/
							}
						}

				}
			}
			ajaxObj.open("GET", "/lq/includes/suggetion/citySuggestion.jsp?city=" + city + "&formNo=" + formNo + "&idName=" + idName + "&cityPropertyName=updateCityDState", true);
			ajaxObj.send(null);
		}
	  }else{	updateCityDState(formNo);
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }
	  }else{
				document.getElementById(idName).innerHTML="";
				document.getElementById(idName).style.display="none";
	  }

  }


function updateCityState(formNo){
	if(document.getElementById("searchcityselect") != null){	
		var cityValue = document.getElementById("searchcityselect").value;
		if(cityValue != null && "" != cityValue){
			var index = cityValue.indexOf(",");
			var city = cityValue.substring(0, index);
			var state = cityValue.substring(index + 1);
			document.forms[formNo].searchCity.value=city;
			document.forms[formNo].searchState.value=state;
			// Modified 2009/09/01 by Aleh Yurkevich for CR 5362 
			// Begin 
			document.forms[formNo].searchCountry.value=getCountry(state);
			// End 
		}
	}	
}

// Modified 2009/09/01 by Aleh Yurkevich for CR 5362 
// Begin
function getCountry(state) {
	var CANADA = "CA";
	var USA = "US";
	var MEXICO = "MX";
	var EMPTY = "";
	
	if (state == null) {
		return EMPTY;
	}	
	else if (state == "AB" || state == "BC"	|| state == "MB" || state == "NB" || state == "NF" || state == "NT"
			|| state == "NS" || state == "NU" || state == "ON" || state == "PE"	|| state == "QC"
			|| state == "SK" || state == "YT") {
		return CANADA;
	} 
	else if (state == "CH" || state == "CHH" || state.toLowerCase() == "juarez" || state.toLowerCase() == "chihuahua") {
		return MEXICO;
	} 
	else {
		return USA;
	}	
}
// End

function updateCityOState(formNo){
	if(document.getElementById("searchcityselect") != null){
		var cityValue= document.getElementById("searchcityselect").value;
		if(cityValue != null && "" != cityValue){
			var index = cityValue.indexOf(",");
			var city = cityValue.substring(0, index);
			var state = cityValue.substring(index + 1);
			document.forms[formNo].searchOCity.value=city;
			document.forms[formNo].searchOState.value=state;
		}
	}
}

function updateCityDState(formNo){
	if(document.getElementById("searchcityselect") != null){
		var cityValue= document.getElementById("searchcityselect").value;
		if(cityValue != null && "" != cityValue){
			var index = cityValue.indexOf(",");
			var city = cityValue.substring(0, index);
			var state = cityValue.substring(index + 1);
			document.forms[formNo].searchDCity.value=city;
			document.forms[formNo].searchDState.value=state;
		}
	}
}


function searchDown(e, formNo, idName){
		if(formNo == null)
			formNo = "0";
		else if(formNo.length == 0)
			formNo = "0";
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	if(KeyID == 13)
		{return false;}
	var city = document.forms[formNo].searchCity.value;
	if(KeyID == 40  && city != null && city.length > 0){
		if(document.getElementById(idName).childNodes[0] != null && document.getElementById(idName).childNodes[0].value != null){
			document.getElementById(idName).childNodes[0].focus();
		}
	}
}

function searchDownOtravel(e, formNo, idName){
		if(formNo == null)
			formNo = "0";
		else if(formNo.length == 0)
			formNo = "0";
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	var city = document.forms[formNo].searchOCity.value;
	if(KeyID == 40  && city != null && city.length > 0){
		if(document.getElementById(idName).childNodes[0] != null && document.getElementById(idName).childNodes[0].value != null)
			document.getElementById(idName).childNodes[0].focus();
	}
}

function searchDownDtravel(e, formNo, idName){
		if(formNo == null)
			formNo = "0";
		else if(formNo.length == 0)
			formNo = "0";
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	var city = document.forms[formNo].searchDCity.value;
	if(KeyID == 40  && city != null && city.length > 0){
		if(document.getElementById(idName).childNodes[0] != null && document.getElementById(idName).childNodes[0].value != null)
			document.getElementById(idName).childNodes[0].focus();
	}
}

function removeSuggetion(event) {
    if (!event) {
        if (window.event) {
            e = window.event;
        } else {
            return;
        }
    } else {
        e = event;
    }

    //e = window.event;
    var cfe = e.srcElement || e.target;
    var name = cfe.name;
    if (name != "searchCity") {
        document.getElementById('citysearchresult').innerHTML = "";
        document.getElementById('citysearchresult').style.display = "none";
    }
}

function removeResSuggetion(event){
    if (!event) {
        if(window.event) {
            e = window.event;
        } else {
            return;
        }
    } else {
        e = event;
    }

    // e = window.event;
 	var cfe = e.srcElement || e.target;
 	var name = cfe.name;
 	if(name != "searchCity"){
			document.getElementById('citysearchresult').innerHTML="";
			document.getElementById('citysearchresult').style.display="none";
			document.getElementById('citysearchLandmark').innerHTML="";
			document.getElementById('citysearchLandmark').style.display="none";
 	}
 	if(name == "searchOCity"){
			document.getElementById('citysearchDtravel').innerHTML="";
			document.getElementById('citysearchDtravel').style.display="none";
 	}else if(name == "searchDCity"){
			document.getElementById('citysearchOtravel').innerHTML="";
			document.getElementById('citysearchOtravel').style.display="none";
 	}else if(name != "searchOCity" && name != "searchDCity"){
			document.getElementById('citysearchOtravel').innerHTML="";
			document.getElementById('citysearchOtravel').style.display="none";
			document.getElementById('citysearchDtravel').innerHTML="";
			document.getElementById('citysearchDtravel').style.display="none";
 	}
}


function removeSuggetionOnKeyup(e, idName){
	KeyID = "";
	if(e.keyCode) 
		KeyID = e.keyCode;
	else if(e.which) 
			KeyID = e.which;
	else if(e.charCode) 
			KeyID = e.charCode;
	if(KeyID == 13 || KeyID == 27){
		document.getElementById(idName).innerHTML="";
		document.getElementById(idName).style.display="none";
	}
}

function removeSuggetionOnClick(idName){
		document.getElementById(idName).innerHTML="";
		document.getElementById(idName).style.display="none";
}

function removeUpdateSuggetionOnClick(idName, cityPropertyName, formNo){
		if("updateCityOState" == cityPropertyName){
			updateCityOState(formNo);
		}else if("updateCityDState" == cityPropertyName){
			updateCityDState(formNo);
		}else if(cityPropertyName == null || "" == cityPropertyName || "updateCityState" == cityPropertyName){
			updateCityState(formNo);
		}
		document.getElementById(idName).innerHTML="";
		document.getElementById(idName).style.display="none";
}

function trimAll(sString) {

	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function getEvent(e){
  if(window.event != null) {
    return event;
  }
  return e;
}