// JavaScript Document
	var CitySync = {}
	CitySync.Super = Class.create();
	CitySync.Sub = {};
	
	CitySync.Sub.Base = Class.create();
	CitySync.Sub.DropOff = Class.create();
	CitySync.Sub.PickUp = Class.create();
	
	Object.extend(CitySync.Sub.Base.prototype,
					{
						
						initialize : function(ele, options){
							
							this.options = { onChange : this.needSuperFunction.bind(this) };
							
							Object.extend(this.options, options || {});
							
							this.ele = $(ele);
							this.attachListener();
						
						},
						attachListener : function(){
							
							return false;
							
						},
						needSuperFunction : function(){
							
							console.log("I need super function!! %o", this);
							
						},
						update : function(xml){
							
							throw { message : "This function needs to be overwritten by sub classes" };
							
						},
						notify : function(){
							
							this.ele.innerHTML = "";
							var tmp = $C("option", { innerHTML : "Loading..." });
							
							this.ele.appendChild(tmp);		
							
							
						},
						filterUniversal : function(ele){
						
							return ele.getAttribute("LocType") == "PD";
						
						},
						filterPickUp : function(ele){
							
							return ele.getAttribute("LocType") == "P" || ele.getAttribute("LocType") == "PD";
						
						},
						filterDropOff : function(ele){
							
							return ele.getAttribute("LocType") == "D"  || ele.getAttribute("LocType") == "PD";
						
						},
						getPickUp : function(xml){
							var arr = $A(xml.getElementsByTagName("CITY"));
							
							return arr.findAll(this.filterPickUp);
						
						},
						getDropOff : function(xml){
							var arr = $A(xml.getElementsByTagName("CITY"));
							
							return arr.findAll(this.filterDropOff);
						},
						formatLabel : function(str){
							
							var ret = str.toLowerCase().gsub(/ [a-z]{1}/i, function(myMat){
																				 	return myMat[0].toUpperCase();
																				 });
							
							ret = ret.charAt(0).toUpperCase() + ret.substring(1);
							return ret;
							
						},
						setByValue : function(val){
							
							$A(this.ele.options).find(this._setByValue.bind(this, val));
							
						},
						_setByValue : function(val, option, itr){
							
							if(option.value == val){
								this.ele.selectedIndex = itr;
								return true;
							}
							return false;
						},
						buildOption : function(obj){
							var val = this.formatLabel(obj.getAttribute("v"));
							
							var option = $C("option", { value : val, innerHTML : val });
							
							this.ele.appendChild(option);
							
						}
					}
				);
	Object.extend(Object.extend(CitySync.Sub.DropOff.prototype, CitySync.Sub.Base.prototype),
					{
						
						update : function(xml){
							
							var newList = this.getDropOff(xml);
							
							this.ele.innerHTML = "";
							
							
							newList.each(this.buildOption.bind(this));
							
							/* add an 'other' option at end of list */
							var option = $C("option", { value : "other", innerHTML : "Other" });
							
							this.ele.appendChild(option);
						}
						
					}
				);
	Object.extend(Object.extend(CitySync.Sub.PickUp.prototype, CitySync.Sub.Base.prototype),
					{
						
						update : function(xml){
							var newList = this.getPickUp(xml);
							
							this.ele.innerHTML = "";
							
							newList.each(this.buildOption.bind(this));
							
							/* add an 'other' option at end of list */
							var option = $C("option", { value : "other", innerHTML : "Other" });
							
							this.ele.appendChild(option);
						
						},
						attachListener : function(){
							
							Event.observe(this.ele, "change", this.options.onChange);
							
						}
					}
				);
	
	
	
	Object.extend(Object.extend(CitySync.Super.prototype, Ajax.Application.Cache.prototype),
					{
					
						initialize : function(subject, observers, options){
							
							this.method = "get";
							this.options = {
												subOptions : { 
													onChange : this.synchronizeCity.bindAsEventListener(this)															 
												}
											
										   }
							Object.extend(this.options, options || {});			
											
							this.url    =  options.url;
							this.observers = [];
							
							this.subject = $(subject);
							
							this.attachListener();
							
							observers.each(this.createObserver.bind(this));
							
						},
						synchronizeCity : function(e){
							
							var val = Event.element(e).value;

							this.observers.each(this.setByValue.bind(this, val));
							
						},
						setByValue : function(val, observer){
							
							observer.setByValue(val);
							
						},
						createObserver : function(str){
							
							
							var ele = $(str);
							
							if(ele.getAttribute("id").match("pu"))
								this.observers.push(new CitySync.Sub.PickUp(ele, this.options.subOptions));
							else
								this.observers.push(new CitySync.Sub.DropOff(ele, this.options.subOptions));
							
						
						
						},
						attachListener : function(){
							
							Event.observe(this.subject, "change", this.updateXML.bindAsEventListener(this));
						
						},
						updateXML : function(e){

							var ele = Event.element(e);

							this.observers.each(this.notifyLoad);
							
							this.sendRequest({ pucode : ele.value }, this.receiveXML.bind(this));
													
						},
						receiveXML : function(eAja){
							
							this.updateObserverArr(eAja.responseXML);
							
						},
						notifyLoad : function(obj){
							
							obj.notify();
							
						},
						updateObserverArr : function(xml){
							
							this.observers.each(this.updateObserver.bind(this, xml));
							
						
						},
						updateObserver : function(xml, ele){
							
							ele.update(xml);
							
							document.getVars = [];
						    var urlHalves = String(document.location).split('?');
						    if(urlHalves[1]){
						      var urlVars = urlHalves[1].split('&');
						      for(var i=0; i<=(urlVars.length); i++){
						         if(urlVars[i]){
						            var urlVarPair = urlVars[i].split('=');
						            document.getVars[urlVarPair[0]] = urlVarPair[1];
						         }
						      }
						   } 
						   
						   /* if pucity var was set before calling this then use it */
						   if(typeof(pucity) == "string") {
						   		document.getVars.pucity = pucity;
							}
						   
						   /* set pucity */
							var sel = document.carform.pucity;
							var str = '';
							
							if (document.getVars.pucity != undefined) {
								str = document.getVars.pucity;
							}
							
							for (i=0; i<sel.options.length; i++) {
								if (sel.options[i].text.toLowerCase() == str.toLowerCase()) {
									sel.selectedIndex = i;
								}
							}
							
							/* set docity */
							if (document.carform.docity != undefined) {
								var sel = document.carform.docity;
								var str = '';
								
								if (document.getVars.pucity != undefined) {
									str = document.getVars.pucity;
								}
								
								for (i=0; i<sel.options.length; i++) {
									if (sel.options[i].text.toLowerCase() == str.toLowerCase()) {
										sel.selectedIndex = i;
									}
								}
							}
							
						}					
					}
				);
	

