var currentCityIndex=-1;var sig=+1;var ant=-1;var cantidadFila=14;var listObject=function(){this.jsonData;this.myContainer;this.defaultList;this.selected;this.textBox;this.dependant;this.cityexist;this.messageNoCity;this.IsIncorrecCity};var appendList=function(b,d,a,h,g){var c=new listObject();c.myContainer=createList(a);c.textBox=$("#"+b);c.dependant=g;c.messageNoCity=d;if(g!=null){loadData("",c,false);loadData(h,c.dependant,false);c.cityexist=true;if(h!=""){c.dependant.cityexist=true}}c.textBox.bind("click",function(){var i=null;if(c.cityexist){c.myContainer.remove();c.myContainer=createList(a);var j=c.textBox.offset();c.myContainer.css("left",j.left);c.myContainer.css("top",j.top+21);c.myContainer.html(buildList(c.jsonData,c));showList(c.myContainer);f()}});c.textBox.bind("blur",function(i){blurCitypopup(a,b,c.myContainer,c);UpdateCityToTextBox(c)});c.textBox.bind("keypress",function(i){if(!i){i=window.event}if(i.keyCode==13){return false}});c.textBox.bind("keydown",function(i){if(!i){i=window.event}e(i)});c.textBox.bind("keyup",function(i){if(!i){i=window.event}if(!(i.keyCode==37||i.keyCode==38||i.keyCode==39||i.keyCode==40)){c.textBox.trigger("click");c.myContainer.html(c.defaultList);c.myContainer.html(searchDestination(c.jsonData,c.textBox.val(),c));$(c.myContainer).bgiframe();if(currentCityIndex>-1){$(".itemCity.selected").removeClass("selected");$(".itemCity:first").addClass("selected")}}});c.textBox.bind("focus",function(){this.select();if(jQuery.trim(c.textBox.val()).length==0){c.textBox.trigger("click")}});var f=function(){var i=c.textBox.val();c.IsIncorrecCity=false;if(i.length>0){$(".itemCity:contains('"+i.substring(0,i.length-4)+"')").addClass("selected");itemSelected=$(".itemCity.selected");if(itemSelected.length>0){currentCityIndex=parseInt(itemSelected[0].id)}}else{currentCityIndex=-1}};var e=function(n){var m=document.getElementById(a).getElementsByTagName("a");var i=document.getElementById(b);var k;if(!n){n=window.event}switch(n.keyCode){case 13:if(m.length==0){return}n.target?n.preventDefault():n.returnValue=false;if(m.length==1){currentCityIndex=0}if(currentCityIndex==-1||m.length==0){hideList(c.myContainer)}else{var l=m[currentCityIndex].childNodes[0].nodeValue;var j=m[currentCityIndex].childNodes[1].innerHTML;cityDependant(j,l,c);i.blur()}break;case 27:n.target?n.preventDefault():n.returnValue=false;hideList(c.myContainer);i.blur();break;case 37:currentCityIndex=currentCityIndex-cantidadFila;if(currentCityIndex<0){currentCityIndex=currentCityIndex+cantidadFila}break;case 38:currentCityIndex=(currentCityIndex==0||currentCityIndex==ant)?m.length+ant:currentCityIndex+ant;break;case 39:if(currentCityIndex==-1){currentCityIndex=0}currentCityIndex=currentCityIndex+cantidadFila;if(currentCityIndex>m.length){currentCityIndex=currentCityIndex-cantidadFila}break;case 40:currentCityIndex=(currentCityIndex==m.length+ant)?0:currentCityIndex+sig;break}$(".itemCity.selected").removeClass("selected");$(".itemCity").eq(currentCityIndex).addClass("selected")};hideList(c.myContainer);this.myObject=c;return c};var createList=function(b){var a;if(a=$("#"+b)){a.remove()}var c=$('<div id="'+b+'"></div>');$("body").append(c);c.hide();return c};var showList=function(a){$(a).bgiframe();a.css("display","");a.show()};var hideList=function(a){a.hide()};var loadData=function(b,c,a){var d=window.location.href;var e=d.split("/")[5];if(b&&b!=""){$.ajax({type:"GET",url:"/_layouts/GetONDList.ashx?originCode="+b+"&lang="+e,success:function(f){c.jsonData=JSON.parse(f);c.defaultList=buildList(c.jsonData,c);if(a){if(!inList(c.textBox.val(),JSON.parse(f))){c.textBox.val("")}}}})}else{$.ajax({type:"GET",url:"/_layouts/GetONDList.ashx?lang="+e,success:function(f){c.jsonData=JSON.parse(f);c.defaultList=buildList(c.jsonData,c);if(c.dependant&&c.selected){if(!inList(c.selected,c.dependant.jsonData)){c.dependant.textbox.val("")}}}});return c.jsonData}};var buildList=function(d,b){var a=0;var c="";c=$("<span></span>");innerList=$("<ul></ul>");$.each(d.routes,function(e,f){if(a==14){c.append(innerList);innerList=$("<ul></ul>");a=0}innerList.append(buildLink(f.code,f.name,b,e));a+=1});c.append(innerList);return c};var searchDestination=function(c,d,b){var a=0;tmpList=$("<span></span>");innerList=$("<ul></ul>");if(d.length>=3){$.each(c.routes,function(e,f){if(a==14){tmpList.append(innerList);innerList=$("<ul></ul>");a=0}code=replaceSpecialChars(f.code.toLowerCase());name=replaceSpecialChars(f.name.toLowerCase());if(code.match(replaceSpecialChars(d.toLowerCase()))||(name.indexOf(replaceSpecialChars(d.toLowerCase()))==0)){innerList.append(buildLink(f.code,f.name,b,e));a+=1}})}tmpList.append(innerList);if(d.length<3&&d.length>0){b.IsIncorrecCity=true;return $("<ul><li>"+errorMessage+"</li></ul>")}if(d.length>0){if(a==0){b.IsIncorrecCity=true;return $("<ul><li>"+b.messageNoCity+"</li></ul>")}else{return tmpList}}else{return buildList(b.jsonData,b)}};var replaceSpecialChars=function(a){tmpText=a;tmpText=tmpText.replace(/\u00E1/g,"a");tmpText=tmpText.replace(/\u00E9/g,"e");tmpText=tmpText.replace(/\u00ED/g,"i");tmpText=tmpText.replace(/\u00F3/g,"o");tmpText=tmpText.replace(/\u00FA/g,"u");return tmpText};var blurCitypopup=function(c,g,d,a){var b=YAHOO.util.Event,f=YAHOO.util.Dom;var g=f.get(g);var e=f.get(e);b.on(document,"click",function(j){var h=b.getTarget(j);var i=f.get(c);if(h!=i&&!f.isAncestor(i,h)&&h!=g&&!f.isAncestor(g,h)){hideList(a.myContainer);if(a.IsIncorrecCity){a.textBox.val("")}}})};var UpdateCityToTextBox=function(b){var d="";var a="";var c=$(".itemCity.selected");if(c.length>0){b.IsIncorrecCity=false;d=c[0].firstChild.data;a=c.find("span").html();b.textBox.val(d+" "+a);b.selected=a;if(b.dependant){b.dependant.cityexist=true;loadData(a,b.dependant,true)}}};var buildLink=function(g,e,d,f){var b=$("<li></li>");var c=$('<a id="'+f+'" href="#" class="itemCity">'+e+'<span class="code">'+g+"</span></a>");c.unbind();c.bind("click",function(a){cityDependant(g,e,d);currentCityIndex=parseInt(this.id);return false});b.append(c);return b};var cityDependant=function(c,a,b){b.textBox.val(a+" "+c);b.selected=c;if(b.dependant){b.dependant.cityexist=true;loadData(c,b.dependant,true)}hideList(b.myContainer)};var inList=function(c,d){for(var b=0;b<d.routes.length;b++){var a=d.routes[b].name+" "+d.routes[b].code;if(c.match(a)!=null){return true}}return false};if(!this.JSON){JSON={}}(function(){function f(n){return n<10?"0"+n:n}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(key){return this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z"};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+string+'"'}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==="object"&&typeof value.toJSON==="function"){value=value.toJSON(key)}if(typeof rep==="function"){value=rep.call(holder,key,value)}switch(typeof value){case"string":return quote(value);case"number":return isFinite(value)?String(value):"null";case"boolean":case"null":return String(value);case"object":if(!value){return"null"}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==="[object Array]"){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||"null"}v=partial.length===0?"[]":gap?"[\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"]":"["+partial.join(",")+"]";gap=mind;return v}if(rep&&typeof rep==="object"){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==="string"){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}v=partial.length===0?"{}":gap?"{\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"}":"{"+partial.join(",")+"}";gap=mind;return v}}if(typeof JSON.stringify!=="function"){JSON.stringify=function(value,replacer,space){var i;gap="";indent="";if(typeof space==="number"){for(i=0;i<space;i+=1){indent+=" "}}else{if(typeof space==="string"){indent=space}}rep=replacer;if(replacer&&typeof replacer!=="function"&&(typeof replacer!=="object"||typeof replacer.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":value})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==="object"){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v}else{delete value[k]}}}}return reviver.call(holder,key,value)}cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}}());