dealerInfo = {

	DEBUG: false,
	INFO: this.DEBUG ? this.DEBUG : false,

	_cname_pd: "dealerpf",
	_cname_pddtails: "dealerpf_details",
	_cexpire: 730,
	_cexpire_0: 0,
	_cpath: "/",

	preferredDealerXml: null,

	dealerSet: false,
	dealerID: null,
	hrefLocation : location.href.toLowerCase(),

	/* dealer detail */
	/*
		id:"59655",
		name:"Sterling Heights Dodge",
		address:"40111 VAN DYKE AVENUE",
		city:"STERLING HEIGHTS",
		state:"MI",
		zipcode:"48313",
		phone:"586-939-3900",
		email:"admin@sterlingheightsdodge.com",
		principal:"ANTHONY J VIVIANO",
		fivestar:"Y",
		latitude:"42.5882",
		longitude:"-83.0308",
		distance:"-1",
		URL:"http://www.test-fivestardealers.com/sterlingheights/Dodge/en_us/website?adid=&pid=&daaXmlPath=&bid=&rid=",
		gaqURL:"https://test-5.dodge.com/vehsuite/GetQuote.jsp?dealerid=59655&lang=en&adid=&pid=&daaXmlPath=&bid=&rid=",
		ttdURL:"https://test-5.dodge.com/vehsuite/TestDrive.jsp?dealerid=59655&lang=en&adid=&pid=&daaXmlPath=&bid=&rid=",
		invURL:"http://test-5.dodge.com/vehsuite/SearchInventory.jsp?dealerid=59655&lang=en&adid=&pid=&daaXmlPath=&bid=&rid=",
		sapURL:"http://www.test-fivestardealers.com/sterlingheights/Dodge/en_us?link=information-appt_sched.cgi&adid=&pid=&daaXmlPath=&bid=&rid=",
		partsURL:"https://test-5.dodge.com/vehsuite/GetAPart.jsp?dealerid=59655&lang=en&adid=&pid=&daaXmlPath=&bid=&rid=",
		servicesURL:"http://www.test-fivestardealers.com/sterlingheights/Dodge/en_us?link=service_specials.cgi&adid=&pid=&daaXmlPath=&bid=&rid=",
		servicesOffered:"Get A Dealer Quote, Five Star, Business Link, SRT, Sprinter, Viper",
		dma:"0505"
	*/

	id:					null,
	name:				null,
	address:			null,
	city:				null,
	state:				null,
	zipcode:			null,
	phone:				null,
	email:				null,
	principal:			null,
	fivestar:			null,
	webtools:			null,
	latitude:			null,
	longitude:			null,
	distance:			null,
	URL:				null,
	gaqURL:				null,
	ttdURL:				null,
	invURL:				null,
	sapURL:				null,
	partsURL:			null,
	servicesURL:		null,
	mapURL:				null,
	hoursURL:			null,
	contactusURL:		null,
	servicesOffered:	null,
	dma:				null,
	domainName:			null,
	hotdealsOBJ:		null,
	inventoryOBJ:		null,
	zip5:				null,
	dealerSitePointer:  null,


	init: function() {
		this.getPreferredDealerCookie();
		this.loadDealerDetails();
	},

	loadDealerDetails: function() {
		if (this.dealerID) {
			var pdc = this.getPreferredDealerDetailsCookie();
			// clearing a bad cookie state
			// should be removed in Jan 09
			if((pdc.toString()).contains(',}]')){
				this.clearPreferredDealerCookie();
				pdc = false;
			}
			//
			if (pdc) {
				this.DEBUG ? (console.log("load detail from cookie:\n\n " + pdc)) : null;
				var pdcA = eval(pdc);
				this.fromArray(pdcA[0]);
				this.DEBUG ? (console.log("got it?:\n\n " + this.toString())) : null;

				// Has preferred dealer been changed?
				if (this.dealerID != this.id) {
					this.getDealerXML(this.dealerID);
				}

			} else {
				this.DEBUG ? (console.log("go load xml for:\n\n " + this.dealerID)) : null;
				this.getDealerXML(this.dealerID);
			}
			this.dealerSet = true;
		}
	},

	setPreferredDealer: function (dealerID) {
  
        // Set PD Cookie
        this.dealerID = dealerID;
        this.setPreferredDealerCookie();
        
        // Set Dealer Info Cookie and Load Obj
        this.getDealerXML(this.dealerID);
        this.INFO?(alert("Set Preferred Dealer:\n\n " + this.toString())):null;
        
        if((this.hrefLocation.search("hostc") != -1) || (this.hrefLocation.search("hostd") != -1)){
	         /**
             * Persist a dealer as the user's preferred dealership for 2 years
             * @param {String} dealerCode The ID of the dealer to be flagged as the preferred dealer
             */
            ASC.Chrysler.setPreferredDealer(dealerID);
        }
    
    },

	getPreferredDealerCookie: function() {
		var dealerID = Cookie.get(this._cname_pd);
		if (dealerID) {
			this.dealerID = dealerID;
		}
		this.DEBUG ? (console.log("cookie value:\n\n " + this.dealerID)) : null;
	},

	setPreferredDealerCookie: function() {
		if (this.dealerID) {
			this.DEBUG ? console.log("setPreferredDealerCookie\nduration: " + this._cexpire + "\ndomain: " + this.getDomain() + "\npath: " + this._cpath) : null;
			Cookie.set(this._cname_pd, this.dealerID, {duration: this._cexpire, domain: this.getDomain(), path: this._cpath});
			
			//Set Autodata Cookie
			 if((this.hrefLocation.search("hostc") == -1) && (this.hrefLocation.search("hostd") == -1)){
                Cookie.set(this._cname_pd, this.dealerID, {duration: this._cexpire, path: this._cpath});
            }
		}
	},

	clearPreferredDealerCookie: function() {
		Cookie.set(this._cname_pd, '', {duration: -1, domain: this.getDomain(), path: this._cpath});
		Cookie.set(this._cname_pddtails, '', {duration: this._cexpire_0, path: this._cpath});
		
		//Set Autodata Cookie
		if((this.hrefLocation.search("hostc") == -1) && (this.hrefLocation.search("hostd") == -1)){
            Cookie.set(this._cname_pd, '', {duration: -1, path: this._cpath});
        }
        
        if((this.hrefLocation.search("hostc") != -1) || (this.hrefLocation.search("hostd") != -1)){
	         /** 
             * Delete the user's preferred dealer cookie 
             */ 
            ASC.Chrysler.removePreferredDealer(); 
         }
        
	},
	
	// This function was originally in with the misspelled name.  Provide the misspelling as legacy support
	//clearPreferredDealerCookie: function() { this.clearPreferredDealerCookie() },

	getPreferredDealerDetailsCookie: function() {
		return Cookie.get(this._cname_pddtails);
	},

	setPreferredDealerDetailsCookie: function() {
		Cookie.set(this._cname_pddtails, this.toArray(), {duration: this._cexpire_0, path: this._cpath});
	},

	getDomain: function() {
		domainRX = /\w*.com/;
		domain = domainRX.exec(window.location.hostname);
		domain = domain ? window.location.hostname.substring(domain.index) : window.location.hostname;
		this.DEBUG ? console.log("getDomain\nhostname: " + window.location.hostname + "\ndomain: " + domain ) : null;
		
		return domain;
	},

	getDealerXML: function(dealerID, argInboundCall) {
		var url= "/hostd/getlocatedealersbasic.xml";
		var args = "&dealerCode=" + dealerID;
		var func = "dealerInfo.parseDealer(http_request[i])";
		
		var xmlDoc = new Ajax(url,{method: 'post', data:args, 
                        onComplete:function(text, xml){
                            dealerInfo.parseDealer(xml);
                        }.bind(this)});
        		
		xmlDoc.request();
		
		if((this.hrefLocation.search("hostc") != -1) || (this.hrefLocation.search("hostd") != -1) && !argInboundCall){
            /**
             * @type String
             * returns an Object with the Preferred Dealerships information
             */
            ASC.Chrysler.getPreferredDealer();
        	}
	},

	getMapURL: function() {

		return this.mapURL;
	},

	setMapURL: function() {

	},

	getHoursURL: function() {
		return this.hoursURL;
	},

	setHoursURL: function() {

	},

	getContactUsURL: function() {
		return this.contactusURL;
	},

	setContactUsURL: function() {

	},

	isFiveStar: function() {
		return this.fivestar == 'Y';
	},

	parseDealer: function(http_request) {
		//console.log("response");
		//console.log(http_request);
		this.preferredDealerXml = http_request;

		pDealer = this.preferredDealerXml.getElementsByTagName("dealer");

		if (pDealer.length > 0) {
			//console.log("has data to parse")
			this.loadDealerObj(pDealer[0]);
			this.setPreferredDealerDetailsCookie();

			// Update Dealer Menu
			populate_dealerInfo();
		}
	},

	loadDealerObj: function(pDealerNode) {
		this.id					= pDealerNode.getAttribute('id');
		this.name				= pDealerNode.getAttribute('name');
		this.name               = this.name.replace(/'/, "&apos;");
		this.address			= pDealerNode.getAttribute('address');
		this.city				= pDealerNode.getAttribute('city');
		this.state				= pDealerNode.getAttribute('state');
		this.zipcode			= formatXMLValue(pDealerNode.getAttribute('zipcode'));
		this.phone				= formatXMLValue(pDealerNode.getAttribute('phone'));
		this.email				= pDealerNode.getAttribute('email');
		this.principal			= pDealerNode.getAttribute('principal');
		this.fivestar			= pDealerNode.getAttribute('fivestar');
		this.webtools			= pDealerNode.getAttribute('webtools');
		this.latitude			= pDealerNode.getAttribute('latitude');
		this.longitude			= pDealerNode.getAttribute('longitude');
		this.distance			= pDealerNode.getAttribute('distance');
		this.URL				= pDealerNode.getAttribute('URL');
		this.gaqURL				= pDealerNode.getAttribute('gaqURL');
		this.ttdURL				= pDealerNode.getAttribute('ttdURL');
		this.invURL				= pDealerNode.getAttribute('invURL');
		this.sapURL				= pDealerNode.getAttribute('sapURL');
		this.partsURL			= pDealerNode.getAttribute('partsURL');
		this.servicesURL		= pDealerNode.getAttribute('servicesURL');
		this.servicesOffered	= pDealerNode.getAttribute('servicesOffered');
		this.dma				= pDealerNode.getAttribute('dma');
		this.domainName			= pDealerNode.getAttribute('domainName');
		this.setMapURL();
		this.setHoursURL();
		this.setContactUsURL();
		this.zip5				= (this.zipcode != '' ? this.zipcode.substring(0,5) : '');
		this.dealerSitePointer	= pDealerNode.getAttribute('dealerSitePointer');
		this.DEBUG ? (console.log("dealer loaded:\n\n " + this.toArray())) : null;	
	},

	toArray: function() {
		return "[{" +
				"'id':'" + this.id + "'," +
				"'name':'" + this.name + "'," +
				"'address':'" + this.address + "'," +
				"'city':'" + this.city + "'," +
				"'state':'" + this.state + "'," +
				"'zipcode':'" + this.zipcode + "'," +
				"'phone':'" + this.phone + "'," +
				"'email':'" + this.email + "'," +
				"'principal':'" + this.principal + "'," +
				"'fivestar':'" + this.fivestar + "'," +
				"'webtools':'" + this.webtools + "'," +
				"'latitude':'" + this.latitude + "'," +
				"'longitude':'" + this.longitude + "'," +
				"'distance':'" + this.distance + "'," +
				"'URL':'" + this.URL + "'," +
				"'gaqURL':'" + this.gaqURL + "'," +
				"'ttdURL':'" + this.ttdURL + "'," +
				"'invURL':'" + this.invURL + "'," +
				"'sapURL':'" + this.sapURL + "'," +
				"'partsURL':'" + this.partsURL + "'," +
				"'servicesURL':'" + this.servicesURL + "'," +
				"'servicesOffered':'" + this.servicesOffered + "'," +
				"'dma':'" + this.dma + "'," +
				"'domainName':'" + this.domainName + "'," +
				"'mapURL':'" + this.mapURL + "'," +
				"'dealerSitePointer':'" + this.dealerSitePointer + "'" +
				"}]";
	},

	fromArray: function(pdc) {
		this.id					= pdc.id;
		this.name				= pdc.name;
		this.address			= pdc.address;
		this.city				= pdc.city;
		this.state				= pdc.state;
		this.zipcode			= pdc.zipcode;
		this.phone				= pdc.phone;
		this.email				= pdc.email;
		this.principal			= pdc.principal;
		this.fivestar			= pdc.fivestar;
		this.webtools			= pdc.webtools;
		this.latitude			= pdc.latitude;
		this.longitude			= pdc.longitude;
		this.distance			= pdc.distance;
		this.URL				= pdc.URL;
		this.gaqURL				= pdc.gaqURL;
		this.ttdURL				= pdc.ttdURL;
		this.invURL				= pdc.invURL;
		this.sapURL				= pdc.sapURL;
		this.partsURL			= pdc.partsURL;
		this.servicesURL		= pdc.servicesURL;
		this.servicesOffered	= pdc.servicesOffered;
		this.dma                = pdc.dma;
		this.domainName         = pdc.domainName;
		this.dealerSitePointer  = pdc.dealerSitePointer;
		
		if (pdc.mapURL == 'undefined') {
			this.setMapURL();
		}
		else {
			this.mapURL = pdc.mapURL;
		}
		this.setHoursURL();
		this.setContactUsURL();
		this.zip5 = (this.zipcode != "" ? this.zipcode.substring(0,5) : "");
	},

	toString: function() {
		return "Preferred Dealer\n" +
				"----------------\n" +
				"id: " + this.id + "\n" +
				"name: " + this.name + "\n" +
				"address: " + this.address + "\n" +
				"city: " + this.city + "\n" +
				"state: " + this.state + "\n" +
				"zipcode: " + this.zipcode + "\n" +
				"phone: " + this.phone + "\n" +
				"email: " + this.email + "\n" +
				"principal: " + this.principal + "\n" +
				"fivestar: " + this.fivestar + "\n" +
				"webtools: " + this.webtools + "\n" +
				"latitude: " + this.latitude + "\n" +
				"longitude: " + this.longitude + "\n" +
				"distance: " + this.distance + "\n" +
				"URL: " + this.URL + "\n" +
				"gaqURL: " + this.gaqURL + "\n" +
				"ttdURL: " + this.ttdURL + "\n" +
				"invURL: " + this.invURL + "\n" +
				"sapURL: " + this.sapURL + "\n" +
				"partsURL: " + this.partsURL + "\n" +
				"servicesURL: " + this.servicesURL + "\n" +
				"servicesOffered: " + this.servicesOffered + "\n" +
				"dma: " + this.dma + "\n" +
				"domainName: " + this.domainName + "\n" +
				"mapURL: " + this.mapURL + "\n" +
				"hoursURL: " + this.hoursURL + "\n" +
				"dealerSitePointer: " + this.dealerSitePointer + "\n" +
				"contactusURL: " + this.contactusURL;
	}
}