function autoCompleteCity ()
{
	var url = new Array();
	var conf = new Array();

	conf['box-background'] = '#FFF';
	conf['box-border'] = 'solid 1px #000';
	conf['box-max-height'] = '125'; // ! number only

	conf['list-padding'] = '1px 5px';
	conf['list-margin'] = '0';
	conf['list-height'] = '20'; // ! number only
	conf['list-font-style'] = 'normal';
	conf['list-font-weight'] = 'normal';
	conf['list-font-size'] = '12px';
	conf['list-line-height'] = '20px';

	conf['list-color'] = '#000';
	conf['list-background'] = '#FFF';

	conf['list-active-color'] = '#FFF';
	conf['list-active-background'] = '#3399FF';
	conf['list-active-mouse-background'] = '#33CCFF';

	$('.auto-complete-city').each(function(){
        var foc = 0;
		var active = -1;
		var keybind = 0;
		var inputBefore = '';
		var position = '';
		var ua = $.browser;
		var textBox = $(this);
		var box = $(this).next();
        var width = 250;
		var height = textBox.height();
        textBox.attr('disabled', false);
  	    textBox.attr('autocomplete','on');
		box.width(width+2);
		var list = box.find('li');
		createList();

		textBox.focus(function()
        {
           cNdx = getNdx(textBox.attr('id'));
           var elem =  document.getElementById('lcity'+cNdx);
           if (elem.createTextRange )
           {
                var range = elem.createTextRange();
                range.move('character', textBox.val().length);
                range.select();
           }

           if (textBox.val().length > 1)
           {
              lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
              lsn = document.getElementById('lstate'+cNdx).value.toLowerCase();
              lctn = textBox.val().toLowerCase();
              theCNdx = getCityNdx(lcn, lsn, lctn);
              if (theCNdx == -1)
              {
  	     		   actionBox(list);
              }
           }

        });
		textBox.click(function()
        {
           cNdx = getNdx(textBox.attr('id'));
            if (!refreshCity)
            {
                population21();
                textBox.val("");
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }
            }
            textBox.focus();
            refreshCity = 0;

  	    });

		textBox.blur(function(e)
        {
          gCityNdx = getNdx(textBox.attr('id'));
          setTimeout("cityBlurCode()", 100);
		});

		textBox.keyup(function(event)
        {
            var lv = 0;
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
            cNdx = getNdx(textBox.attr('id'));
            lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
            lsn = document.getElementById('lstate'+cNdx).value.toLowerCase();
            cid = getCountryNdx(lcn);
            sid = getStateNdx(lcn, lsn);
            if (sid == -1 && theKey != 9)
            {
                if (document.createEvent)
                {
                   var e = document.createEvent("HTMLEvents");
                   e.initEvent("click", true, true);
                   document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                }
                else
                {
                   document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                }

           }
           else
           {
            if (theKey != 9 && document.getElementById("lstate"+ cNdx).value != "")
            {
            if (theList_lc.countries[cid].states[sid].cities)
            {
  		    	if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40 && theKey != 8 && theKey != 46 && theKey != 13)
                {
                    population21();
                           if (list.length && textBox.val().length > 2)
                           {
                               listIt();
                           }
                           else
                           {
                             do
                             {
                                ctid = getCitySubNdx(lcn, lsn, textBox.val().toLowerCase());
                                if (ctid == -1 && textBox.val().length)
                                {
                                    textBox.val(textBox.val().substring(0, textBox.val().length-1));
                                }
                             } while (textBox.val().length && ctid == -1);
                             if (ctid != -1)
                             {
                                 updateCity(cNdx);
                                 lv = 1;
                             }
                       }
                   }
             if (!lv)
                {
                  if (window.event)
                  {
     	       		  if(theKey == 40)
                      {
                       listDown(event);
                      }
       			      if(theKey == 38)
                      {
                        listUp(event);
                      }
		      	      if(theKey == 13)
                      {
                       listEnter(event);
                      }
                  }
               }
            }
            else
            {
                if (textBox.val().length > 1)
                {
                   textBox.val(textBox.val().substring(0, textBox.val().length-1));
                   population21();
                }
            }
            }
            }
		});
		textBox.error(function(e)
        {
//                    document.getElementById('msgTxt').innerHTML = "Blur.";
//                    setTimeout("clr()", 10);

          if (!foc)
          {
             lctn = textBox.val().toLowerCase();
             if (lctn != "")
             {
                theCNdx = getCityNdx(lcn, lsn, lctn);
                cNdx = getNdx(textBox.attr('id'));
                lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
                lsn = document.getElementById('lstate'+cNdx).value.toLowerCase();
                if (theCNdx == -1)
                {
//                 alert("Please select a valid city.");
                    if (static)
                    {
                       inlineMsg(textBox.attr('id'),'Please select a valid city.',2);

                    }
                    else
                    {
                       document.getElementById('msgTxt').innerHTML = "Please select a valid city.";
                    }
                    if (!window.event)
                    {
                        e.preventDefault();
      	      		    e.stopPropagation();
                    }
                    textBox.focus();
                    return false;
                 }
            }
            else
            {
                 selectBoxOff();
           }
          }
        });
		function createList () {
			list.width(width-8);
			list.height(conf['list-height']);

            cNdx = getNdx(textBox.attr('id'));
            if (document.getElementById("lcountry"+ cNdx).value != "" &&
                document.getElementById("lstate"+ cNdx).value != "")
            {
                lcn = document.getElementById("lcountry"+ cNdx).value.toLowerCase();
                lsn = document.getElementById("lstate"+ cNdx).value.toLowerCase();
                theCNdx = getCountryNdx(lcn);
                theSNdx = getStateNdx(lcn, lsn);
//                document.getElementById("currCountry"+ cNdx).value = document.getElementById("lcountry"+ cNdx).value;
//                document.getElementById("currState"+ cNdx).value = document.getElementById("lstate"+ cNdx).value;
                if (!theList.countries[theCNdx].states[theSNdx].cities)
                {
                    getCities(theList.countries[theCNdx].id, theList.countries[theCNdx].states[theSNdx].id, theCNdx, theSNdx, cNdx);
                }
           }

		}
        function listIt()
        {
				if(textBox.attr('id') && url[textBox.attr('id')] && inputBefore != input){
					var input = textBox.val();
				inputBefore = input;
					jQuery.ajax({
						url: url[textBox.attr('id')] + input,
						cache: true,
						success: function(data){
							box.empty();

							jQuery.each(data.split("\n"), function(){
								if(String(this).length > 0){
									var li = $('<li></li>');
									li.text(String(this));
									box.append(li);
								}
							});

							list = box.find('li');
							createList();

							var ajax = 1;

							actionBox(list,ajax);
						}
					});
				}else{
					actionBox(list);
				}
        }
		function actionBox (list,ajax) {
			if((!textBox.val() || ajax) && list.size() > 0){
                            cNdx = getNdx(textBox.attr('id'));
                            val2 = document.getElementById("lcountry"+ cNdx).value;
		 //		active = -1;
				box.scrollTop(0);
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});

				list.each(function(){
					$(this).css('display','block');
					$(this).addClass('select-list');
				});
				selectBoxOn();
			}else{
				var boxon = 0;
				var input = textBox.val();
				var inputLC = textBox.val().toLowerCase();
//					active = -1;
					box.scrollTop(0);
					list.css({
						'background':conf['list-background'],
						'color':conf['list-color']
					});

                    bsoff = 0;
                    var cnt = 0;
                    var theVal = null;
					list.each(function(){
                        matched = ($(this).text().toLowerCase().substr(0, inputLC.length) == inputLC);
                        if (matched)
                        {
                           bsoff = 1;
                        }
                        if ($(this).text() == input)
                        {
                            cnt ++;
                            theVal = $(this).text();
                        }
						if (!matched || $(this).text() == input){
                            $(this).css('display','none');
							$(this).removeClass('select-list');
						}else{
							$(this).css('display','block');
							$(this).addClass('select-list');
							boxon = 1;
                            cnt ++;
                            theVal = $(this).text();
						}
					});
					if(boxon){
                        if (cnt == 1)
                        {
     			            textBox.val(theVal);
                            if (!static)
                            {
                                document.getElementById('msgTxt').innerHTML = "";
                            }
                            else
                            {
                               hideMsg();
                            }
                            textBox.focus();
                       //     if (!window.event)
                            {
                                gCityNdx = getNdx(textBox.attr('id'));
                                setTimeout("cityBlurCode()", 100);
                            }
                        }
                        else
                        {
   						    selectBoxOn();
                        }
					}else{
						selectBoxOff();
                        if (!bsoff)
                        {
     			            textBox.val(textBox.val().substring(0, textBox.val().length-1));
                            if (!static)
                            {
                                document.getElementById('msgTxt').innerHTML = "";
                            }
                 else
                 {
                    hideMsg();
                 }

                            listIt();
                        }
					}
			}
		}
		function selectBoxOn () {
			if(box.css('display') != 'block'){
				active = -1;
			}

			if(box.height() > conf['box-max-height']){
				box.css({
					'max-height':conf['box-max-height'] + 'px',
					'overflow':'scroll'
					});
			}else{
				box.css('overflow','auto');
			}

			fixBox ()

			$(textBox).bind('blur',selectBoxOff);

			box.mouseover(function(){
				$(textBox).unbind('blur',selectBoxOff);
			});
			box.mouseout(function(){
				$(textBox).bind('blur',selectBoxOff);
			});

			list.bind('mouseover',listMouseOver);
			list.bind('click',listClick);
            box.bind('mouseover', listFocus);
            box.bind('mouseout', listBlur);
    		textBox.bind('keydown', 'down', listDown);
			textBox.bind('keydown', 'up', listUp);
            textBox.bind('keydown', 'return', listEnter);
//            box.bind('keydown', 'return', listEnter);
//            list.bind('keydown', 'return', listEnter);
//			// $(window).bind('keydown', 'return', listEnter);
			// $(window).bind('resize',fixBox);
			keybind = 1;
		}

        function listFocus() {
               foc = 1;
        }
        function listBlur() {
               foc = 0;
               if (textBox.val() != "")
               {
                 gCityNdx = getNdx(textBox.attr('id'));
                 setTimeout("cityBlurCode()", 100);
               }
        }
		function fixBox () {
			var offset = textBox.offset();
			var left = offset.left;
			var top = offset.top;

			box.css({
				'position':'absolute',
				'left':left + 'px'
			});

			var bHeight = getBrowserHeight();
			var scrollTop = getScrollTop();

			box.css('display','block');
			if(bHeight < top+height+box.height()-scrollTop && top+height-scrollTop > box.height()){
				box.css('top',(top-box.height()-1) + 'px');

				position = 'up';

				scrollTopNew = (box.find('li.select-list').size() -1) * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());
				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

			}else{
				if(ua.msie || ua.opera || ua.safari){
					box.css('top',(top+height+2) + 'px');
				}else{
					box.css('top',(top+height) + 'px');
				}

				position = 'down';
			}
		}
		function selectBoxOff () {
            active = -1;
			list.unbind('mouseover',listMouseOver);
			list.unbind('click',listClick);
            box.unbind('mouseover', listFocus);
            box.unbind('mouseout', listBlur);
			if(keybind){
				textBox.unbind('keydown', 'down', listDown);
 			    textBox.unbind('keydown', 'up', listUp);
                textBox.unbind('keydown', 'return', listEnter);
//				// $(window).unbind('keydown', 'return', listEnter);
				// $(window).unbind('resize',fixBox);
				keybind = 0;
			}

			box.scrollTop(0);
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			box.css('display','none');
		}
		function listMouseOver () {
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			$(this).css({
				'background':conf['list-active-mouse-background'],
				'color':conf['list-active-mouse-color']
			});
			active = box.find('li.select-list').index(this);
		}
		function listClick (event) {
        	var text = $(this).text().replace(/\s+$/,'');
            if (text != "")
            {
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

		   	    textBox.val(text);
 			    selectBoxOff();
                textBox.focus();
//            if (!window.event)
            {
                gCityNdx = getNdx(textBox.attr('id'));
                setTimeout("cityBlurCode()", 100);
                foc = 0;
            }
		   }
        }
		function listDown (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 40)
            {
			var next = 0;
			if(position == 'up' && active == -1){
				return true;
			}else{
				next = active + 1;
			}

			if(box.find('li.select-list').eq(active + 1).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active += 1;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());

				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listUp (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 38)
            {
			var next = 0;
			if(active - 1 < -1){
				if(position == 'up'){
					next = box.find('li.select-list').size() -1;
				}else{
					return true;
				}
			}else{
				next = active - 1;
			}

			if(box.find('li.select-list').eq(next).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active = next;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight();
				if(scrollTopNew - box.scrollTop() < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listEnter (event)
        {
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
			if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40)
            {
     	    var text = box.find('li.select-list').eq(active).text().replace(/\s+$/,'');
            if (text != "")
            {
            	textBox.val(text);
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

			selectBoxOff();
            textBox.focus();
//  			enableEnter(event);
//            if (!window.event)
            {
                gCityNdx = getNdx(textBox.attr('id'));
                setTimeout("cityBlurCode()", 100);
            }
          }
         }
		}
		function enableEnter(e){
			if(e.srcElement){
				o = e.srcElement;
			}else{
				o = e.target;
			}
			if (o.tagName != 'TEXTAREA' && e.keyCode == 13) {
				if(e.preventDefault){
					e.preventDefault();
					e.stopPropagation();
				}
				e.returnValue=false;
				e.cancelBubble=true;
			}
		}
		function getBrowserHeight() {
			if (window.innerHeight) {
				return window.innerHeight;
			}
			else if(document.documentElement && document.documentElement.clientHeight != 0){
				return document.documentElement.clientHeight;
			}
			else if ( document.body ) {
				return document.body.clientHeight;
			}
			return 0;
		}
		function getScrollTop() {
			var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
			return scrollTop;
		}
        function getNdx(val)
        {
           var cNdx = -1;
           cNdx = Number(val.substr(val.indexOf('lcity')+'lcity'.length));
           return cNdx;
        }

	});

}
        function clr()
        {
                       document.getElementById('msgTxt').innerHTML = "";

        }

var gCityNdx = -1;
function cityBlurCode()
{
     if (document.createEvent)
     {
         var e = document.createEvent("HTMLEvents");
         e.initEvent("error", true, true);
         document.getElementById("lcity"+ gCityNdx).dispatchEvent(e);
     }
     else
     {
        document.getElementById("lcity"+gCityNdx).fireEvent("onerror");
     }
}
function autoCompleteState ()
{
	var url = new Array();
	var conf = new Array();

	conf['box-background'] = '#FFF';
	conf['box-border'] = 'solid 1px #000';
	conf['box-max-height'] = '125'; // ! number only

	conf['list-padding'] = '1px 5px';
	conf['list-margin'] = '0';
	conf['list-height'] = '20'; // ! number only
	conf['list-font-style'] = 'normal';
	conf['list-font-weight'] = 'normal';
	conf['list-font-size'] = '12px';
	conf['list-line-height'] = '20px';

	conf['list-color'] = '#000';
	conf['list-background'] = '#FFF';

	conf['list-active-color'] = '#FFF';
	conf['list-active-background'] = '#3399FF';
	conf['list-active-mouse-background'] = '#33CCFF';

	$('.auto-complete-state').each(function(){
        var foc = 0;
        var blur = 0;
		var active = -1;
		var keybind = 0;
		var inputBefore = '';
		var position = '';
		var ua = $.browser;
		var textBox = $(this);
		var box = $(this).next();
        var width = 250;
		var height = textBox.height();
        textBox.attr('disabled', false);
  	    textBox.attr('autocomplete','on');
		box.width(width+2);
		var list = box.find('li');
		createList();

		textBox.focus(function()
        {
           cNdx = getNdx(textBox.attr('id'));
           var elem =  document.getElementById('lstate'+cNdx);
           if (elem.createTextRange)
           {
                var range = elem.createTextRange();
                range.move('character', textBox.val().length);
                range.select();
           }
           if (textBox.val().length > 1)
           {
              lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
              lsn = textBox.val().toLowerCase();
              theSNdx = getStateNdx(lcn, lsn);
              if (theSNdx == -1)
              {
     			   actionBox(list);
              }
           }
        });

		textBox.click(function()
        {
            cNdx = getNdx(textBox.attr('id'));
            if (!refreshState)
            {
                textBox.val("");
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

                document.getElementById("lcity"+ cNdx).value = "";
            }
            refreshState = 0;
            textBox.focus();
  	    });

		textBox.blur(function(e)
        {
          gStateNdx = getNdx(textBox.attr('id'));
          setTimeout("stateBlurCode()", 100);
		});

		textBox.keyup(function(event)
        {
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
            cNdx = getNdx(textBox.attr('id'));
            lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
            lsn = document.getElementById('lstate'+cNdx).value.toLowerCase();
            cid = getCountryNdx(lcn);
            sid = getStateNdx(lcn, lsn);
            if (cid == -1 && theKey != 9)
            {
                if (document.createEvent)
                {
                   var e = document.createEvent("HTMLEvents");
                   e.initEvent("click", true, true);
                   document.getElementById("lcountry"+ cNdx).dispatchEvent(e);
                }
                else
                {
                   document.getElementById("lcountry"+ cNdx).fireEvent("onclick");
                }

           }
           else
           {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

  		    	if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40 && theKey != 8 && theKey != 46 && theKey != 13)
            {
                           if (list.length && textBox.val().length > 2)
                           {
                               listIt();
                           }
                           else
                           {
                             do
                             {
                                sid = getStateSubNdx(lcn, textBox.val().toLowerCase());
                                if (sid == -1 && textBox.val().length)
                                {
                                    textBox.val(textBox.val().substring(0, textBox.val().length-1));
                                }
                             } while (textBox.val().length && sid == -1);
                             if (sid != -1)
                             {
                                 updateState(cNdx);
                                 lv = 1;
                             }
                       }
    		}
            if (window.event)
            {
			if(theKey == 40)
            {
             listDown(event);
            }
			if(theKey == 38)
            {
               listUp(event);
            }
			if(theKey == 13)
            {
               listEnter(event);
            }
            }
            }
		});
		textBox.error(function(e)
        {
          if (!foc)
          {
             cNdx = getNdx(textBox.attr('id'));
             lcn = document.getElementById('lcountry'+cNdx).value.toLowerCase();
             lsn = textBox.val().toLowerCase();
             theSNdx = getStateNdx(lcn, lsn);
             if (theSNdx == -1 && lsn != "")
             {
//                 alert("Please select a valid state.");
                 if (static)
                 {
                    inlineMsg(textBox.attr('id'),'Please select a valid state.',2);
                 }
                 else
                 {
                    document.getElementById('msgTxt').innerHTML = "Please select a valid state.";
                 }
                 if (!window.event)
                 {
                     e.preventDefault();
      	      		 e.stopPropagation();
                 }
                 textBox.focus();
                 return false;
            }
            else
            {
                 if (lsn != "")
                 {
                 selectBoxOff();
                 if (document.createEvent)
                 {
                     var e = document.createEvent("HTMLEvents");
                     e.initEvent("click", true, true);
                     document.getElementById("lcity"+ cNdx).dispatchEvent(e);
                 }
                 else
                 {
                    document.getElementById("lcity"+ cNdx).fireEvent("onclick");
                 }

                cNdx = getNdx(textBox.attr('id'));
                lcn = document.getElementById("lcountry"+ cNdx).value.toLowerCase();
                lsn = document.getElementById("lstate"+ cNdx).value.toLowerCase();
                theCNdx = getCountryNdx(lcn);
                theSNdx = getStateNdx(lcn, lsn);
                if (!theList.countries[theCNdx].states[theSNdx].cities)
                {
                    if (window.event)
                    {
                       document.getElementById("lcity"+ cNdx).disabled = true;
                    }
                    getCities(theList.countries[theCNdx].id, theList.countries[theCNdx].states[theSNdx].id, theCNdx, theSNdx, cNdx);
                }
                else
                {
                  document.getElementById("lcity"+ cNdx).disabled = false;
                  if (document.createEvent)
                  {
                     var e = document.createEvent("HTMLEvents");
                     e.initEvent("click", true, true);
                     document.getElementById("lcity"+ cNdx).dispatchEvent(e);
                  }
                  else
                  {
                    document.getElementById("lcity"+ cNdx).fireEvent("onclick");
                  }
                }
             }
           }
         }
        });
		function createList () {
			list.width(width-8);
			list.height(conf['list-height']);
		}
        function listIt()
        {
				if(textBox.attr('id') && url[textBox.attr('id')] && inputBefore != input){
					var input = textBox.val();
				inputBefore = input;
					jQuery.ajax({
						url: url[textBox.attr('id')] + input,
						cache: true,
						success: function(data){
							box.empty();

							jQuery.each(data.split("\n"), function(){
								if(String(this).length > 0){
									var li = $('<li></li>');
									li.text(String(this));
									box.append(li);
								}
							});

							list = box.find('li');
							createList();

							var ajax = 1;

							actionBox(list,ajax);
						}
					});
				}else{
					actionBox(list);
				}
        }
		function actionBox (list,ajax) {
			if((!textBox.val() || ajax) && list.size() > 0){
                            cNdx = getNdx(textBox.attr('id'));
                            val2 = document.getElementById("lcountry"+ cNdx).value;
		 //		active = -1;
				box.scrollTop(0);
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});

				list.each(function(){
					$(this).css('display','block');
					$(this).addClass('select-list');
				});
				selectBoxOn();
			}else{
				var boxon = 0;
				var input = textBox.val();
				var inputLC = textBox.val().toLowerCase();
//					active = -1;
					box.scrollTop(0);
					list.css({
						'background':conf['list-background'],
						'color':conf['list-color']
					});

                    bsoff = 0;
                    var cnt = 0;
                    var theVal = null;
					list.each(function(){
                        matched = ($(this).text().toLowerCase().substr(0, inputLC.length) == inputLC);
                        if (matched)
                        {
                           bsoff = 1;
                        }
                        if ($(this).text() == input)
                        {
                            cnt ++;
                            theVal = $(this).text();
                        }
						if (!matched || $(this).text() == input){
                            $(this).css('display','none');
		   		$(this).removeClass('select-list');
						}else{
							$(this).css('display','block');
							$(this).addClass('select-list');
							boxon = 1;
                            cnt ++;
                            theVal = $(this).text();
						}
					});
					if(boxon){
                        if (cnt == 1)
                        {
     			            textBox.val(theVal);
                     if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

                        cNdx = getNdx(textBox.attr('id'));
                            document.getElementById("lcity"+ cNdx).value = "";
                            textBox.focus();
                    if (window.event)
                    {
                       document.getElementById("lcity"+ cNdx).disabled = true;
                    }

  //                          if (!window.event)
                            {
                                gStateNdx = getNdx(textBox.attr('id'));
                                setTimeout("stateBlurCode()", 100);
                            }
      /*                      if (document.createEvent)
                            {
                                var e = document.createEvent("HTMLEvents");
                                e.initEvent("click", true, true);
                                document.getElementById("lcity"+ cNdx).dispatchEvent(e);
                            }
                            else
                            {
                                document.getElementById("lcity"+ cNdx).fireEvent("onclick");
                            }
      */
                        }
                        else
                        {
   						    selectBoxOn();
                        }
					}else{
						selectBoxOff();
                        if (!bsoff)
                        {
     			            textBox.val(textBox.val().substring(0, textBox.val().length-1));
                            if (!static)
                            {
                                 document.getElementById('msgTxt').innerHTML = "";
                            }
                            else
                            {
                               hideMsg();
                            }
                            listIt();
                        }
                        else
                        {
                           cNdx = getNdx(textBox.attr('id'));
                           document.getElementById("lcity"+ cNdx).value = "";
                           textBox.focus();
                           if (window.event)
                           {
                               document.getElementById("lcity"+ cNdx).disabled = true;
                           }
                           gStateNdx = getNdx(textBox.attr('id'));
                           setTimeout("stateBlurCode()", 100);
                        }
					}
			}
		}
		function selectBoxOn () {
			if(box.css('display') != 'block'){
//				active = -1;
			}

			if(box.height() > conf['box-max-height']){
				box.css({
					'max-height':conf['box-max-height'] + 'px',
					'overflow':'scroll'
					});
			}else{
				box.css('overflow','auto');
			}

			fixBox ()

			$(textBox).bind('blur',selectBoxOff);

			box.mouseover(function(){
				$(textBox).unbind('blur',selectBoxOff);
			});
			box.mouseout(function(){
				$(textBox).bind('blur',selectBoxOff);
			});

			list.bind('mouseover',listMouseOver);
			list.bind('click',listClick);
            box.bind('mouseover', listFocus);
            box.bind('mouseout', listBlur);
			textBox.bind('keydown', 'down', listDown);
			textBox.bind('keydown', 'up', listUp);
 		    textBox.bind('keydown', 'return', listEnter);
			// $(window).bind('resize',fixBox);
			keybind = 1;
		}
        function listFocus() {
               foc = 1;
        }
        function listBlur() {
               if (textBox.val() != "")
               {
               foc = 0;
               gStateNdx = getNdx(textBox.attr('id'));
               setTimeout("stateBlurCode()", 100);
               }
        }

		function fixBox () {
			var offset = textBox.offset();
			var left = offset.left;
			var top = offset.top;

			box.css({
				'position':'absolute',
				'left':left + 'px'
			});

			var bHeight = getBrowserHeight();
			var scrollTop = getScrollTop();

			box.css('display','block');
			if(bHeight < top+height+box.height()-scrollTop && top+height-scrollTop > box.height()){
				box.css('top',(top-box.height()-1) + 'px');

				position = 'up';

				scrollTopNew = (box.find('li.select-list').size() -1) * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());
				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

			}else{
				if(ua.msie || ua.opera || ua.safari){
					box.css('top',(top+height+2) + 'px');
				}else{
					box.css('top',(top+height) + 'px');
				}

				position = 'down';
			}
		}
		function selectBoxOff () {
            active = -1;
			list.unbind('mouseover',listMouseOver);
			list.unbind('click',listClick);
            box.unbind('mouseover', listFocus);
            box.unbind('mouseout', listBlur);
			if(keybind){
				textBox.unbind('keydown', 'down', listDown);
 			    textBox.unbind('keydown', 'up', listUp);
				textBox.unbind('keydown', 'return', listEnter);
				// $(window).unbind('resize',fixBox);
				keybind = 0;
			}

			box.scrollTop(0);
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			box.css('display','none');
		}
		function listMouseOver () {
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			$(this).css({
				'background':conf['list-active-mouse-background'],
				'color':conf['list-active-color']
			});
			active = box.find('li.select-list').index(this);
		}
		function listClick (event) {
        	var text = $(this).text().replace(/\s+$/,'');
            if (text != "")
            {
		   	    textBox.val(text);
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

 			    selectBoxOff();
                textBox.focus();

//                if (!window.event)

                cNdx = getNdx(textBox.attr('id'));
                    if (window.event)
                    {
                       document.getElementById("lcity"+ cNdx).disabled = true;
                    }
                {
                    gStateNdx = getNdx(textBox.attr('id'));
                    setTimeout("stateBlurCode()", 100);
                }
                foc = 0;
/*
                if (document.createEvent)
                {
                   var e = document.createEvent("HTMLEvents");
                   e.initEvent("click", true, true);
                   document.getElementById("lcity"+ cNdx).dispatchEvent(e);
                }
                else
                {
                   document.getElementById("lcity"+ cNdx).fireEvent("onclick");
                }
              */
		   }
        }
		function listDown (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 40)
            {
			var next = 0;
			if(position == 'up' && active == -1){
				return true;
			}else{
				next = active + 1;
			}

			if(box.find('li.select-list').eq(active + 1).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active += 1;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());

				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listUp (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 38)
            {
			var next = 0;
			if(active - 1 < -1){
				if(position == 'up'){
					next = box.find('li.select-list').size() -1;
				}else{
					return true;
				}
			}else{
				next = active - 1;
			}

			if(box.find('li.select-list').eq(next).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active = next;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight();
				if(scrollTopNew - box.scrollTop() < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listEnter (event)
        {
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
			if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40)
            {
     	    var text = box.find('li.select-list').eq(active).text().replace(/\s+$/,'');
            if (text != "")
            {
			textBox.val(text);
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

			selectBoxOff();
            textBox.focus();
//  			enableEnter(event);
//            if (!window.event)
            {
                gStateNdx = getNdx(textBox.attr('id'));
                setTimeout("stateBlurCode()", 100);
            }

            cNdx = getNdx(textBox.attr('id'));
                    if (window.event)
                    {
                       document.getElementById("lcity"+ cNdx).disabled = true;
                    }
/*
            if (document.createEvent)
            {
                var e = document.createEvent("HTMLEvents");
                e.initEvent("click", true, true);
                document.getElementById("lcity"+ cNdx).dispatchEvent(e);
            }
            else
            {
                document.getElementById("lcity"+ cNdx).fireEvent("onclick");
            }
*/
          }
         }
		}
		function enableEnter(e){
			if(e.srcElement){
				o = e.srcElement;
			}else{
				o = e.target;
			}
			if (o.tagName != 'TEXTAREA' && e.keyCode == 13) {
				if(e.preventDefault){
					e.preventDefault();
					e.stopPropagation();
				}
				e.returnValue=false;
				e.cancelBubble=true;
			}
		}
		function getBrowserHeight() {
			if (window.innerHeight) {
				return window.innerHeight;
			}
			else if(document.documentElement && document.documentElement.clientHeight != 0){
				return document.documentElement.clientHeight;
			}
			else if ( document.body ) {
				return document.body.clientHeight;
			}
			return 0;
		}
		function getScrollTop() {
			var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
			return scrollTop;
		}
        function getNdx(val)
        {
           var cNdx = -1;
           cNdx = Number(val.substr(val.indexOf('lstate')+'lstate'.length));
           return cNdx;
        }

	});

}
var gStateNdx = -1;
function stateBlurCode()
{
     if (document.createEvent)
     {
         var e = document.createEvent("HTMLEvents");
         e.initEvent("error", true, true);
         document.getElementById("lstate"+ gStateNdx).dispatchEvent(e);
     }
     else
     {
        document.getElementById("lstate"+gStateNdx).fireEvent("onerror");
     }
}
function autoCompleteCountry ()
{
	var url = new Array();
	var conf = new Array();

	conf['box-background'] = '#FFF';
	conf['box-border'] = 'solid 1px #000';
	conf['box-max-height'] = '125'; // ! number only

	conf['list-padding'] = '1px 5px';
	conf['list-margin'] = '0';
	conf['list-height'] = '20'; // ! number only
	conf['list-font-style'] = 'normal';
	conf['list-font-weight'] = 'normal';
	conf['list-font-size'] = '12px';
	conf['list-line-height'] = '20px';

	conf['list-color'] = '#000';
	conf['list-background'] = '#FFF';

	conf['list-active-color'] = '#FFF';
	conf['list-active-background'] = '#3399FF';
	conf['list-active-mouse-background'] = '#33CCFF';

	$('.auto-complete-country').each(function(){
        var foc = 0;
		var active = -1;
		var keybind = 0;
		var inputBefore = '';
		var position = '';
		var ua = $.browser;
		var textBox = $(this);
		var box = $(this).next();
        var width = 250;
		var height = textBox.height();
        textBox.attr('disabled', false);
  	    textBox.attr('autocomplete','on');
		box.width(width+2);
		var list = box.find('li');
		createList();

		textBox.focus(function()
        {
           cNdx = getNdx(textBox.attr('id'));
          lcn = textBox.val().toLowerCase();
           theCNdx = getCountryNdx(lcn);
           var elem =  document.getElementById('lcountry'+cNdx);
           if (elem.createTextRange)
           {
                var range = elem.createTextRange();
                range.move('character', textBox.val().length);
                range.select();
           }
           if (textBox.val().length > 1 && theCNdx == -1)
           {
  			   actionBox(list);
           }
        });

		textBox.click(function()
        {
            cNdx = getNdx(textBox.attr('id'));
            textBox.val("");
            if (!static)
            {
                document.getElementById('msgTxt').innerHTML = "";
            }
                 else
                 {
                    hideMsg();
                 }

            document.getElementById("lstate"+ cNdx).value = "";
            document.getElementById("lcity"+ cNdx).value = "";
            textBox.focus();
  	    });

		textBox.blur(function(e)
        {
          gCountryNdx = getNdx(textBox.attr('id'));
          setTimeout("countryBlurCode()", 100);
		});

		textBox.keyup(function(event)
        {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;
        	if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40 && theKey != 8 && theKey != 46 && theKey != 13)
            {
             cid = getCountryNdx(lcn);
			   if (cid != -1)
                 {
                            cNdx = getNdx(textBox.attr('id'));
                            document.getElementById("lstate"+ cNdx).value = "";
                            document.getElementById("lcity"+ cNdx).value = "";
                            textBox.focus();
                  //          if (!window.event)
                            {
                               gCountryNdx = getNdx(textBox.attr('id'));
                               setTimeout("countryBlurCode()", 100);
                             }
                            if (document.createEvent)
                            {
                                var e = document.createEvent("HTMLEvents");
                                e.initEvent("click", true, true);
                                document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                            }
                            else
                            {
                                document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                            }
                 }
                else
                {
                    if (textBox.val().length > 1)
                    {
                        listIt();
                    }
                    }
			}
            if (window.event)
            {
			if(theKey == 40)
            {
             listDown(event);
            }
			if(theKey == 38)
            {
               listUp(event);
            }
			if(theKey == 13)
            {
               listEnter(event);
            }
            }
		});
		textBox.error(function(e)
        {
          if (!foc)
          {
             cNdx = getNdx(textBox.attr('id'));
             lcn = textBox.val().toLowerCase();
             theCNdx = getCountryNdx(lcn);
             if (theCNdx == -1 && lcn != "")
             {
                 if (static)
                 {
                    inlineMsg(textBox.attr('id'),'Please select a valid country.',2);
                 }
                 else
                 {
                    document.getElementById('msgTxt').innerHTML = "Please select a valid country.";
                 }
//                 alert("Please select a valid country.");
                 if (!window.event)
                 {
                     e.preventDefault();
      	      		 e.stopPropagation();
                 }
                 textBox.focus();
                 return false;
            }
            else
            {
               if (lcn != "")
               {
                  if (!theList.countries[theCNdx].states.length)
                  {
//                  alert("The country is not supported.");
                 if (static)
                 {
                    inlineMsg(textBox.attr('id'),'The country is not yet supported.',2);
                 }
                 else
                 {
                    document.getElementById('msgTxt').innerHTML = "The country is not yet supported.";
                 }
                  textBox.val("");
                  if (!window.event)
                  {
                     e.preventDefault();
                     e.stopPropagation();
                  }
                  textBox.focus();
                  return false;
                 }
                 else
                 {
                    selectBoxOff();
                    /*
                    if (document.createEvent)
                    {
                       var e = document.createEvent("HTMLEvents");
                       e.initEvent("click", true, true);
                       document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                    }
                    else
                    {
                      document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                   }
                   updateState(cNdx);
                   */
                }
             }
          }
          }
        });
		function createList () {
			list.width(width-8);
			list.height(conf['list-height']);
		}
        function listIt()
        {
				if(textBox.attr('id') && url[textBox.attr('id')] && inputBefore != input){
					var input = textBox.val();
				inputBefore = input;
					jQuery.ajax({
						url: url[textBox.attr('id')] + input,
						cache: true,
						success: function(data){
							box.empty();

							jQuery.each(data.split("\n"), function(){
								if(String(this).length > 0){
									var li = $('<li></li>');
									li.text(String(this));
									box.append(li);
								}
							});

							list = box.find('li');
							createList();

							var ajax = 1;

							actionBox(list,ajax);
						}
					});
				}else{
					actionBox(list);
				}
        }
		function actionBox (list,ajax) {
			if((!textBox.val() || ajax) && list.size() > 0){
                            cNdx = getNdx(textBox.attr('id'));
                            val2 = document.getElementById("lcountry"+ cNdx).value;
		 //		active = -1;
				box.scrollTop(0);
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});

				list.each(function(){
					$(this).css('display','block');
					$(this).addClass('select-list');
				});
				selectBoxOn();
			}else{
				var boxon = 0;
				var input = textBox.val();
				var inputLC = textBox.val().toLowerCase();
//					active = -1;
					box.scrollTop(0);
					list.css({
						'background':conf['list-background'],
						'color':conf['list-color']
					});

                    bsoff = 0;
                    var cnt = 0;
                    var theVal = null;
					list.each(function(){
                        matched = ($(this).text().toLowerCase().substr(0, inputLC.length) == inputLC);
                        if (matched)
                        {
                           bsoff = 1;
                        }
                        if ($(this).text() == input)
                        {
                            cnt ++;
                            theVal = $(this).text();
                        }
						if (!matched || $(this).text() == input){
                            $(this).css('display','none');
							$(this).removeClass('select-list');
						}else{
							$(this).css('display','block');
							$(this).addClass('select-list');
							boxon = 1;
                            cnt ++;
                            theVal = $(this).text();
						}
					});
					if(boxon){
                        if (cnt == 1)
                        {
     			            textBox.val(theVal);
                            cNdx = getNdx(textBox.attr('id'));
                            document.getElementById("lstate"+ cNdx).value = "";
                            document.getElementById("lcity"+ cNdx).value = "";
                            textBox.focus();
                  //          if (!window.event)
                            {
                               gCountryNdx = getNdx(textBox.attr('id'));
                               setTimeout("countryBlurCode()", 100);
                             }
                            if (document.createEvent)
                            {
                                var e = document.createEvent("HTMLEvents");
                                e.initEvent("click", true, true);
                                document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                            }
                            else
                            {
                                document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                            }
                        }
                        else
                        {
   						    selectBoxOn();
                        }
					}else{
						selectBoxOff();
                        if (!bsoff)
                        {
     			            textBox.val(textBox.val().substring(0, textBox.val().length-1));
                            listIt();
                        }
                        else
                        {
                            cNdx = getNdx(textBox.attr('id'));
                            document.getElementById("lstate"+ cNdx).value = "";
                            document.getElementById("lcity"+ cNdx).value = "";
                            textBox.focus();
                  //          if (!window.event)
                            {
                               gCountryNdx = getNdx(textBox.attr('id'));
                               setTimeout("countryBlurCode()", 100);
                             }
                            if (document.createEvent)
                            {
                                var e = document.createEvent("HTMLEvents");
                                e.initEvent("click", true, true);
                                document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                            }
                            else
                            {
                                document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                            }

                        }
					}
			}
		}
		function selectBoxOn () {
			if(box.css('display') != 'block'){
//				active = -1;
			}

			if(box.height() > conf['box-max-height']){
				box.css({
					'max-height':conf['box-max-height'] + 'px',
					'overflow':'scroll'
					});
			}else{
				box.css('overflow','auto');
			}

			fixBox ()

			$(textBox).bind('blur',selectBoxOff);

			box.mouseover(function(){
				$(textBox).unbind('blur',selectBoxOff);
			});
			box.mouseout(function(){
				$(textBox).bind('blur',selectBoxOff);
			});

			list.bind('mouseover',listMouseOver);
			list.bind('click',listClick);
            box.bind('mouseover', listFocus);
            box.bind('mouseout', listBlur);
			textBox.bind('keydown', 'down', listDown);
			textBox.bind('keydown', 'up', listUp);
 		    textBox.bind('keydown', 'return', listEnter);
			// $(window).bind('resize',fixBox);
			keybind = 1;
		}
        function listFocus() {
               foc = 1;
        }
        function listBlur() {
               foc = 0;
               if (textBox.val() != "")
               {
               gCountryNdx = getNdx(textBox.attr('id'));
               setTimeout("countryBlurCode()", 100);
               }
        }
		function fixBox () {
			var offset = textBox.offset();
			var left = offset.left;
			var top = offset.top;

			box.css({
				'position':'absolute',
				'left':left + 'px'
			});

			var bHeight = getBrowserHeight();
			var scrollTop = getScrollTop();

			box.css('display','block');
			if(bHeight < top+height+box.height()-scrollTop && top+height-scrollTop > box.height()){
				box.css('top',(top-box.height()-1) + 'px');

				position = 'up';

				scrollTopNew = (box.find('li.select-list').size() -1) * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());
				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

			}else{
				if(ua.msie || ua.opera || ua.safari){
					box.css('top',(top+height+2) + 'px');
				}else{
					box.css('top',(top+height) + 'px');
				}

				position = 'down';
			}
		}
		function selectBoxOff () {
            active = -1;
			list.unbind('mouseover',listMouseOver);
			list.unbind('click',listClick);
            box.unbind('mouseover', listFocus);
            box.unbind('mouseout', listBlur);
			if(keybind){
				textBox.unbind('keydown', 'down', listDown);
 			    textBox.unbind('keydown', 'up', listUp);
				textBox.unbind('keydown', 'return', listEnter);
				// $(window).unbind('resize',fixBox);
				keybind = 0;
			}

			box.scrollTop(0);
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			box.css('display','none');
		}
		function listMouseOver () {
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			$(this).css({
				'background':conf['list-active-mouse-background'],
				'color':conf['list-active-color']
			});
			active = box.find('li.select-list').index(this);
		}
		function listClick (event) {
        	var text = $(this).text().replace(/\s+$/,'');
            if (text != "")
            {
		   	    textBox.val(text);
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

 			    selectBoxOff();
                textBox.focus();

                cNdx = getNdx(textBox.attr('id'));
                if (document.createEvent)
                {
                   var e = document.createEvent("HTMLEvents");
                   e.initEvent("click", true, true);
                   document.getElementById("lstate"+ cNdx).dispatchEvent(e);
                }
                else
                {
                   document.getElementById("lstate"+ cNdx).fireEvent("onclick");
                }
                if (!window.event)
                {
                    gCountryNdx = getNdx(textBox.attr('id'));
                    setTimeout("countryBlurCode()", 100);
                }
                foc = 0;
		   }
        }
		function listDown (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 40)
            {
			var next = 0;
			if(position == 'up' && active == -1){
				return true;
			}else{
				next = active + 1;
			}

			if(box.find('li.select-list').eq(active + 1).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active += 1;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());

				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listUp (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 38)
            {
			var next = 0;
			if(active - 1 < -1){
				if(position == 'up'){
					next = box.find('li.select-list').size() -1;
				}else{
					return true;
				}
			}else{
				next = active - 1;
			}

			if(box.find('li.select-list').eq(next).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active = next;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight();
				if(scrollTopNew - box.scrollTop() < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listEnter (event)
        {
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
			if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40)
            {
     	    var text = box.find('li.select-list').eq(active).text().replace(/\s+$/,'');
            if (text != "")
            {
			textBox.val(text);
			selectBoxOff();
            textBox.focus();
                 if (!static)
                 {
                    document.getElementById('msgTxt').innerHTML = "";
                 }
                 else
                 {
                    hideMsg();
                 }

//  			enableEnter(event);
            if (!window.event)
            {
                gCountryNdx = getNdx(textBox.attr('id'));
              setTimeout("countryBlurCode()", 100);
            }
            cNdx = getNdx(textBox.attr('id'));
            if (document.createEvent)
            {
                var e = document.createEvent("HTMLEvents");
                e.initEvent("click", true, true);
                document.getElementById("lstate"+ cNdx).dispatchEvent(e);
            }
            else
            {
                document.getElementById("lstate"+ cNdx).fireEvent("onclick");
            }
          }
         }
		}
		function enableEnter(e){
			if(e.srcElement){
				o = e.srcElement;
			}else{
				o = e.target;
			}
			if (o.tagName != 'TEXTAREA' && e.keyCode == 13) {
				if(e.preventDefault){
					e.preventDefault();
					e.stopPropagation();
				}
				e.returnValue=false;
				e.cancelBubble=true;
			}
		}
		function getBrowserHeight() {
			if (window.innerHeight) {
				return window.innerHeight;
			}
			else if(document.documentElement && document.documentElement.clientHeight != 0){
				return document.documentElement.clientHeight;
			}
			else if ( document.body ) {
				return document.body.clientHeight;
			}
    			return 0;
		}
		function getScrollTop() {
			var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
			return scrollTop;
		}
        function getNdx(val)
        {
           var cNdx = -1;
           cNdx = Number(val.substr(val.indexOf('lcountry')+'lcountry'.length));
           return cNdx;
        }

	});

}
var gCountryNdx = -1;
function countryBlurCode()
{
     if (document.createEvent)
     {
         var e = document.createEvent("HTMLEvents");
         e.initEvent("error", true, true);
         document.getElementById("lcountry"+ gCountryNdx).dispatchEvent(e);
     }
     else
     {
        document.getElementById("lcountry"+gCountryNdx).fireEvent("onerror");
     }
}

function autoCompleteStateCity ()
{
	var url = new Array();
	var conf = new Array();

	conf['box-background'] = '#FFF';
	conf['box-border'] = 'solid 1px #000';
	conf['box-max-height'] = '125'; // ! number only

	conf['list-padding'] = '1px 5px';
	conf['list-margin'] = '0';
	conf['list-height'] = '20'; // ! number only
	conf['list-font-style'] = 'normal';
	conf['list-font-weight'] = 'normal';
	conf['list-font-size'] = '12px';
	conf['list-line-height'] = '20px';

	conf['list-color'] = '#000';
	conf['list-background'] = '#FFF';

	conf['list-active-color'] = '#FFF';
	conf['list-active-background'] = '#3399FF';
	conf['list-active-mouse-background'] = '#33CCFF';

	$('.auto-complete-state-city').each(function(){
        var foc = 0;
		var active = -1;
		var keybind = 0;
		var inputBefore = '';
		var position = '';
		var ua = $.browser;
		var textBox = $(this);
		var box = $(this).next();
        var width = 250;
		var height = textBox.height();
        textBox.attr('disabled', false);
  	    textBox.attr('autocomplete','on');
		box.width(width+2);
		var list = box.find('li');
		createList();

		textBox.click(function()
        {
            document.getElementById('msgTxt').innerHTML = "";
            if (!refreshCityState)
            {
                textBox.val("");
            }
            refreshCityState = 0;
            textBox.focus();
  	    });
		textBox.focus(function(e)
        {
           var elem = document.getElementById("lstatecity0");
           if (elem.createTextRange)
           {
                var range = elem.createTextRange();
                range.move('character', textBox.val().length);
                range.select();
           }
		});

		textBox.blur(function(e)
        {
          gStateCityNdx = getNdx(textBox.attr('id'));
          setTimeout("stateCityBlurCode()", 100);
		});

		textBox.error(function(e)
        {
         if (!foc)
         {
           if (textBox.val() != "")
           {
             var matched = 0;
             var theMatch = "";
             valLC = textBox.val().toLowerCase();
  	    	   list.each(function(){
               if ($(this).text().toLowerCase().replace(/\s+$/,"") == valLC)
               {
                   matched = 1;
                   theMatch = $(this).attr('id');
  			   }
			});
            if (!matched)
            {
                 if (static)
                 {

                 }
                 else
                 {
                    document.getElementById('msgTxt').innerHTML = "Please select a valid city.";
                 }
//                 document.getElementById('msgTxt').innerHTML = "Please select a valid city.";
                 if (!window.event)
                 {
                     e.preventDefault();
      	      		 e.stopPropagation();
                 }
                 textBox.focus();
                 return false;
            }
            else
            {
                ndx = getCityStateNdx(textBox.val().toLowerCase());
                document.getElementById('filter').value = cityIdList[ndx];
                document.getElementById('filterCity').value = textBox.val();
            }
          }
         }
		});

		textBox.keyup(function(event)
        {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

        	if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40 && theKey != 8 && theKey != 46 && theKey != 13)
            {
                document.getElementById('filter').value = "";
                if (textBox.val().length == 1)
                {
                    updateStateCity();
                }
                if (textBox.val().length > 1)
                {

                           if (list.length)
                           {
                               listIt();
                           }
                           else
                           {
                              textBox.val(textBox.val().substring(0, textBox.val().length-1));
                           }
            }
			}
            if (window.event)
            {
			if(theKey == 40)
            {
             listDown(event);
            }
			if(theKey == 38)
            {
               listUp(event);
            }
			if(theKey == 13)
            {
//            alert(box.css('display'));
             if (box.css('display') == 'block')
             {
                listEnter(event);
             }
             else
             {
                            if (document.createEvent)
                            {
                                var e = document.createEvent("HTMLEvents");
                                e.initEvent("click", true, true);
                                document.getElementById("relistSubmit").dispatchEvent(e);
                            }
                            else
                            {
                                document.getElementById("relistSubmit").fireEvent("onclick");
                            }
             }
            }
            }
		});
		function createList () {
			list.width(width-8);
			list.height(conf['list-height']);
		}
        function listIt()
        {
				if(textBox.attr('id') && url[textBox.attr('id')] && inputBefore != input){
					var input = textBox.val();
				inputBefore = input;
					jQuery.ajax({
						url: url[textBox.attr('id')] + input,
						cache: true,
						success: function(data){
							box.empty();

							jQuery.each(data.split("\n"), function(){
								if(String(this).length > 0){
									var li = $('<li></li>');
									li.text(String(this));
									box.append(li);
								}
							});

							list = box.find('li');
							createList();

							var ajax = 1;

							actionBox(list,ajax);
						}
					});
				}else{
					actionBox(list);
				}
        }
		function actionBox (list,ajax) {
			if((!textBox.val() || ajax) && list.size() > 0){
    		 //		active = -1;
				box.scrollTop(0);
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});

				list.each(function(){
					$(this).css('display','block');
					$(this).addClass('select-list');
				});
				selectBoxOn();
			}else{
				var boxon = 0;
				var input = textBox.val();
				var inputLC = textBox.val().toLowerCase();
//					active = -1;
					box.scrollTop(0);
					list.css({
						'background':conf['list-background'],
						'color':conf['list-color']
					});

                    bsoff = 0;
                    var cnt = 0;
                    var theVal = null;
					list.each(function(){
                        matched = ($(this).text().toLowerCase().substr(0, inputLC.length) == inputLC);
                        if (matched)
                        {
                           bsoff = 1;
                        }
                        if ($(this).text() == input)
                        {
                            cnt ++;
                            theVal = $(this).text();
                        }
						if (!matched || $(this).text() == input){
                            $(this).css('display','none');
							$(this).removeClass('select-list');
						}else{
							$(this).css('display','block');
							$(this).addClass('select-list');
							boxon = 1;
                            cnt ++;
                            theVal = $(this).text();
						}
					});
					if(boxon){
                        if (cnt == 1)
                        {
     			            textBox.val(theVal.replace(/\s+$/,""));
                            document.getElementById('msgTxt').innerHTML = "";
   						    selectBoxOff();
                            textBox.focus();
                            setTimeout("stateCityBlurCode()", 100);
                        }
                        else
                        {
   						    selectBoxOn();
                        }
					}else{
						selectBoxOff();
                        if (!bsoff)
                        {
     			            textBox.val(textBox.val().substring(0, textBox.val().length-1));
                            document.getElementById('msgTxt').innerHTML = "";
                            listIt();
                        }
					}
			}
		}
		function selectBoxOn () {
			if(box.css('display') != 'block'){
//				active = -1;
			}

			if(box.height() > conf['box-max-height']){
				box.css({
					'max-height':conf['box-max-height'] + 'px',
					'overflow':'scroll'
					});
			}else{
				box.css('overflow','auto');
			}

			fixBox ()

			$(textBox).bind('blur',selectBoxOff);

			box.mouseover(function(){
				$(textBox).unbind('blur',selectBoxOff);
			});
			box.mouseout(function(){
				$(textBox).bind('blur',selectBoxOff);
			});

			list.bind('mouseover',listMouseOver);
			list.bind('click',listClick);
            box.bind('mouseover', listFocus);
            box.bind('mouseout', listBlur);
			textBox.bind('keydown', 'down', listDown);
			textBox.bind('keydown', 'up', listUp);
 		    textBox.bind('keydown', 'return', listEnter);
			// $(window).bind('resize',fixBox);
			keybind = 1;
		}
        function listFocus() {
               foc = 1;
        }
        function listBlur() {
               foc = 0;
               if (textBox.val() != "")
               {
               gStateCityNdx = getNdx(textBox.attr('id'));
               setTimeout("stateCityBlurCode()", 100);
               }
        }

		function fixBox () {
			var offset = textBox.offset();
			var left = offset.left;
			var top = offset.top;
            left = 0;
            top = 0;
			box.css({
				'position':'absolute',
				'left':left + 'px'
			});

			var bHeight = getBrowserHeight();
			var scrollTop = getScrollTop();

			box.css('display','block');
			if(bHeight < top+height+box.height()-scrollTop && top+height-scrollTop > box.height()){
				box.css('top',(top-box.height()-1) + 'px');

				position = 'up';

				scrollTopNew = (box.find('li.select-list').size() -1) * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());
				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

			}else{
				if(ua.msie || ua.opera || ua.safari){
					box.css('top',(top+height+2) + 'px');
				}else{
					box.css('top',(top+height) + 'px');
				}

				position = 'down';
			}
		}
		function selectBoxOff () {
            active = -1;
			list.unbind('mouseover',listMouseOver);
			list.unbind('click',listClick);
            box.unbind('mouseover', listFocus);
            box.unbind('mouseout', listBlur);
			if(keybind){
				textBox.unbind('keydown', 'down', listDown);
 			    textBox.unbind('keydown', 'up', listUp);
				textBox.unbind('keydown', 'return', listEnter);
				// $(window).unbind('resize',fixBox);
				keybind = 0;
			}
            function listFocus()
            {
               foc = 1;
            }
            function listBlur()
            {
               foc = 0;
               if (textBox.val() != "")
               {
               gCityNdx = getNdx(textBox.attr('id'));
               setTimeout("cityBlurCode()", 100);
               }
            }
			box.scrollTop(0);
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			box.css('display','none');
		}
		function listMouseOver () {
			list.css({
				'background':conf['list-background'],
				'color':conf['list-color']
			});
			$(this).css({
				'background':conf['list-active-mouse-background'],
				'color':conf['list-active-color']
			});
			active = box.find('li.select-list').index(this);
		}
		function listClick (event) {
        	var text = $(this).text().replace(/\s+$/,"");
            if (text != "")
            {
		      	textBox.val(text);
                document.getElementById('msgTxt').innerHTML = "";
    			selectBoxOff();
                textBox.focus();
                setTimeout("stateCityBlurCode()", 100);
                foc = 0;
		   }
        }
		function listDown (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 40)
            {
			var next = 0;
			if(position == 'up' && active == -1){
				return true;
			}else{
				next = active + 1;
			}

			if(box.find('li.select-list').eq(active + 1).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active += 1;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight() - (conf['box-max-height'] - list.outerHeight());

				if(box.scrollTop() - scrollTopNew < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listUp (event) {
           theKey = (event.keyCode) ? event.keyCode : event.charCode;

			if(theKey == 38)
            {
			var next = 0;
			if(active - 1 < -1){
				if(position == 'up'){
					next = box.find('li.select-list').size() -1;
				}else{
					return true;
				}
			}else{
				next = active - 1;
			}

			if(box.find('li.select-list').eq(next).size()){
				list.css({
					'background':conf['list-background'],
					'color':conf['list-color']
				});
				active = next;
				box.find('li.select-list').eq(active).css({
					'background':conf['list-active-background'],
					'color':conf['list-active-color']
				});

				scrollTopNew = active * list.outerHeight();
				if(scrollTopNew - box.scrollTop() < list.height()){
					box.scrollTop(scrollTopNew);
				}

				return true;
			}
            }
		}
		function listEnter (event)
        {
            theKey = (event.keyCode) ? event.keyCode : event.charCode;
			if(theKey != 37 && theKey != 38 && theKey != 39 && theKey != 40)
            {
               var text = box.find('li.select-list').eq(active).text().replace(/\s+$/,"");
               if (text != "")
               {
		      	textBox.val(text);
                document.getElementById('msgTxt').innerHTML = "";
    			selectBoxOff();
                textBox.focus();
                setTimeout("stateCityBlurCode()", 100);
              }
           }
		}
		function enableEnter(e){
			if(e.srcElement){
				o = e.srcElement;
			}else{
				o = e.target;
			}
			if (o.tagName != 'TEXTAREA' && e.keyCode == 13) {
				if(e.preventDefault){
					e.preventDefault();
					e.stopPropagation();
				}
				e.returnValue=false;
				e.cancelBubble=true;
			}
		}
		function getBrowserHeight() {
			if (window.innerHeight) {
				return window.innerHeight;
			}
			else if(document.documentElement && document.documentElement.clientHeight != 0){
				return document.documentElement.clientHeight;
			}
			else if ( document.body ) {
				return document.body.clientHeight;
			}
			return 0;
		}
		function getScrollTop() {
			var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
			return scrollTop;
		}
        function getNdx(val)
        {
           var cNdx = -1;
           cNdx = Number(val.substr(val.indexOf('li')+'li'.length));
           return cNdx;
        }
	});
}
function stateCityBlurCode()
{
     if (document.createEvent)
     {
         var e = document.createEvent("HTMLEvents");
         e.initEvent("error", true, true);
         document.getElementById("lstatecity0").dispatchEvent(e);
     }
     else
     {
        document.getElementById("lstatecity0").fireEvent("onerror");
     }
}

