// Client stub for the Kbb PHP Class function Kbb(callback) { mode = 'sync'; if (callback) { mode = 'async'; } this.className = 'Kbb'; this.dispatcher = new HTML_AJAX_Dispatcher(this.className,mode,callback,'/server.php?','JSON'); } Kbb.prototype = { Sync: function() { this.dispatcher.Sync(); }, Async: function(callback) { this.dispatcher.Async(callback); }, getMakes: function() { return this.dispatcher.doCall('getMakes',arguments); }, getModels: function() { return this.dispatcher.doCall('getModels',arguments); }, getTrims: function() { return this.dispatcher.doCall('getTrims',arguments); } } if (!String.fromCharCode && !String.prototype.fromCharCode) { String.prototype.fromCharCode = function(code) { var h = code.toString(16); if (h.length == 1) { h = '0' + h; } return unescape('%' + h); } } if (!String.charCodeAt && !String.prototype.charCodeAt) { String.prototype.charCodeAt = function(index) { var c = this.charAt(index); for (i = 1; i < 256; i++) { if (String.fromCharCode(i) == c) { return i; } } } } if (!Array.splice && !Array.prototype.splice) { Array.prototype.splice = function(s, d) { var max = Math.max, min = Math.min, a = [], e, i = max(arguments.length - 2, 0), k = 0, l = this.length, n, v, x; s = s || 0; if (s < 0) { s += l; } s = max(min(s, l), 0); d = max(min(typeof d == 'number' ? d : l, l - s), 0); v = i - d; n = l + v; while (k < d) { e = this[s + k]; if (!e) { a[k] = e; } k += 1; } x = l - s - d; if (v < 0) { k = s + i; while (x) { this[k] = this[k - v]; k += 1; x -= 1; } this.length = n; } else if (v > 0) { k = 1; while (x) { this[n - k] = this[l - k]; k += 1; x -= 1; } } for (k = 0; k < i; ++k) { this[s + k] = arguments[k + 2]; } return a; } } if (!Array.push && !Array.prototype.push) { Array.prototype.push = function() { for (var i = 0, startLength = this.length; i < arguments.length; i++) { this[startLength + i] = arguments[i]; } return this.length; } } if (!Array.pop && !Array.prototype.pop) { Array.prototype.pop = function() { return this.splice(this.length - 1, 1)[0]; } } if (!DOMParser.parseFromString && window.ActiveXObject) { function DOMParser() {}; DOMParser.prototype = { parseFromString: function(str, contentType) { var xmlDocument = new ActiveXObject('Microsoft.XMLDOM'); xmlDocument.loadXML(str); return xmlDocument; } }; function XMLSerializer() {}; XMLSerializer.prototype = { serializeToString: function(root) { return root.xml || root.outerHTML; } }; }var HTML_AJAX = { defaultServerUrl: false, defaultEncoding: 'JSON', queues: false, clientPools: {}, httpClient: function(name) { if (name) { if (this.clientPools[name]) { return this.clientPools[name].getClient(); } } return this.clientPools['default'].getClient(); }, makeRequest: function(request) { if (!HTML_AJAX.queues[request.queue]) { var e = new Error('Unknown Queue: '+request.queue); if (HTML_AJAX.onError) { HTML_AJAX.onError(e); return false; } else { throw(e); } } else { var qn = request.queue; var q = HTML_AJAX.queues[qn]; HTML_AJAX.queues[request.queue].addRequest(request); return HTML_AJAX.queues[request.queue].processRequest(); } }, serializerForEncoding: function(encoding) { for(var i in HTML_AJAX.contentTypeMap) { if (encoding == HTML_AJAX.contentTypeMap[i] || encoding == i) { return eval("new HTML_AJAX_Serialize_"+i+";"); } } return new HTML_AJAX_Serialize_Null(); }, fullcall: function(url,encoding,className,method,callback,args, options) { var serializer = HTML_AJAX.serializerForEncoding(encoding); var request = new HTML_AJAX_Request(serializer); if (callback) { request.isAsync = true; } request.requestUrl = url; request.className = className; request.methodName = method; request.callback = callback; request.args = args; if (options) { for(var i in options) { request[i] = options[i]; } if (options.grab) { if (!request.args || !request.args.length) { request.requestType = 'GET'; } } } return HTML_AJAX.makeRequest(request); }, callPhpCallback: function(phpCallback, jsCallback, url) { var args = new Array(); for (var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } if (HTML_AJAX_Util.getType(phpCallback[0]) == 'object') { jsCallback(phpCallback[0][phpCallback[1]](args)); return; } if (!url) { url = HTML_AJAX.defaultServerUrl; } HTML_AJAX.fullcall(url, HTML_AJAX.defaultEncoding, false, false, jsCallback, args, {phpCallback: phpCallback}); }, call: function(className,method,callback) { var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } return HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,className,method,callback,args); }, grab: function(url,callback,options) { if (!options) { options = {grab:true}; } else { options['grab'] = true; } return HTML_AJAX.fullcall(url,'Null',false,null,callback, '', options); }, post: function(url,payload,callback,options) { var serializer = 'Null'; if (HTML_AJAX_Util.getType(payload) == 'object') { serializer = 'Urlencoded'; } return HTML_AJAX.fullcall(url,serializer,false,null,callback, payload, options); }, replace: function(id) { var callback = function(result) { HTML_AJAX_Util.setInnerHTML(document.getElementById(id),result); } if (arguments.length == 2) { HTML_AJAX.grab(arguments[1],callback); } else { var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,arguments[1],arguments[2],callback,args, {grab:true}); } }, append: function(id) { var callback = function(result) { HTML_AJAX_Util.setInnerHTML(document.getElementById(id),result,'append'); } if (arguments.length == 2) { HTML_AJAX.grab(arguments[1],callback); } else { var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,arguments[1],arguments[2],callback,args, {grab:true}); } }, Open: function(request) { }, Load: function(request) { }, contentTypeMap: { 'JSON': 'application/json', 'Null': 'text/plain', 'Error': 'application/error', 'PHP': 'application/php-serialized', 'HA' : 'application/html_ajax_action', 'Urlencoded': 'application/x-www-form-urlencoded' }, requestComplete: function(request,error) { for(var i in HTML_AJAX.queues) { if (HTML_AJAX.queues[i].requestComplete) { HTML_AJAX.queues[i].requestComplete(request,error); } } }, formEncode: function(form, array_format) { form = HTML_AJAX_Util.getElement(form); var el, inpType, value, name; var out = (array_format) ? {} : ''; var inputTags = form.getElementsByTagName('INPUT'); var selectTags = form.getElementsByTagName('SELECT'); var buttonTags = form.getElementsByTagName('BUTTON'); var textareaTags = form.getElementsByTagName('TEXTAREA'); var arrayRegex = /(.+)%5B%5D/; var validElement = function (element) { if (!element || !element.getAttribute) { return false; } el = element; name = HTML_AJAX_Util.encodeUrl(el.getAttribute('name')); if (!name) { return false; } if (element.disabled) { return false; } if (!array_format) { value = HTML_AJAX_Util.encodeUrl(el.value); } else { value = el.value; } inpType = el.getAttribute('type'); return true; } inputLoop: for (var i=0; i < inputTags.length; i++) { if (!validElement(inputTags[i])) { continue; } if (inpType == 'checkbox' || inpType == 'radio') { if (!el.checked) { continue inputLoop; } var arr_var = arrayRegex.exec(name); if (array_format && arr_var) { if (!out[arr_var[1]]) { out[arr_var[1]] = new Array(); } out[arr_var[1]].push(value); continue inputLoop; } } if (array_format) { out[name] = value; } else { out += name + '=' + value + '&'; } } selectLoop: for (var i=0; i 0) { this.addClient(); } } HTML_AJAX_Client_Pool.prototype = { isEmpty: function() { return this._len == 0; }, addClient: function() { if (this.maxClients != 0 && this._len > this.maxClients) { return false; } var key = this._len++; this._clients[key] = new HTML_AJAX_HttpClient(); return this._clients[key]; }, getClient: function () { for (var i = 0; i < this._len; i++) { if (!this._clients[i].callInProgress() && this._clients[i].callbackComplete) { return this._clients[i]; } } var client = this.addClient(); if (client) { return client; } return false; }, removeClient: function (client) { for (var i = 0; i < this._len; i++) { if (!this._clients[i] == client) { this._clients.splice(i, 1); return true; } } return false; }, clear: function () { this._clients = []; this._len = 0; } }; HTML_AJAX.clientPools['default'] = new HTML_AJAX_Client_Pool(0);function HTML_AJAX_Request(serializer) { this.serializer = serializer; } HTML_AJAX_Request.prototype = { serializer: null, isAsync: false, requestType: 'POST', requestUrl: '', className: null, methodName: null, timeout: 20000, args: null, callback: null, queue: 'default', priority: 0, customHeaders: {}, iframe: false, grab: false, multipart: false, phpCallback: false, addArg: function(name, value) { if ( !this.args ) { this.args = []; } if (!/[^a-zA-Z_0-9]/.test(name) ) { this.args[name] = value; } else { throw new Error('Invalid parameter name ('+name+')'); } }, getSerializedPayload: function() { return this.serializer.serialize(this.args); }, getContentType: function() { return this.serializer.contentType; }, completeUrl: function() { if (this.className || this.methodName) { this.addGet('c', this.className); this.addGet('m', this.methodName); } if (this.phpCallback) { if (HTML_AJAX_Util.getType(this.phpCallback) == 'array') { this.phpCallback = this.phpCallback.join('.'); } this.addGet('cb', this.phpCallback); } if (this.multipart) { this.addGet('multipart', '1'); } return this.requestUrl; }, compareTo: function(other) { if (this.priority == other.priority) { return 0; } return (this.priority > other.priority ? 1 : -1); }, addGet: function(name, value) { var url = new String(this.requestUrl); url += (url.indexOf('?') < 0 ? '?' : '&') + escape(name) + '=' + escape(value); this.requestUrl = url; } } function HTML_AJAX_HttpClient() { } HTML_AJAX_HttpClient.prototype = { request: null, _timeoutId: null, callbackComplete: true, aborted: false, init:function() { try { this.xmlhttp = new XMLHttpRequest(); } catch (e) { var XMLHTTP_IDS = new Array( 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ); var success = false; for (var i=0;i < XMLHTTP_IDS.length && !success; i++) { try { this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]); success = true; } catch (e) {} } if (!success) { try{ this.xmlhttp = new HTML_AJAX_IframeXHR(); this.request.iframe = true; } catch(e) { throw new Error('Unable to create XMLHttpRequest.'); } } } }, callInProgress: function() { switch ( this.xmlhttp.readyState ) { case 1: case 2: case 3: return true; break; default: return false; break; } }, makeRequest: function() { if (!this.xmlhttp) { this.init(); } try { if (this.request.Open) { this.request.Open(); } else if (HTML_AJAX.Open) { HTML_AJAX.Open(this.request); } if (this.request.multipart) { if (document.all) { this.iframe = true; } else { this.xmlhttp.multipart = true; } } var self = this; this.xmlhttp.open(this.request.requestType,this.request.completeUrl(),this.request.isAsync); if (this.request.customHeaders) { for (i in this.request.customHeaders) { this.xmlhttp.setRequestHeader(i, this.request.customHeaders[i]); } } if (this.request.customHeaders && !this.request.customHeaders['Content-Type']) { var content = this.request.getContentType(); if(window.opera && content != 'application/xml') { this.xmlhttp.setRequestHeader('Content-Type','text/plain; charset=utf-8'); this.xmlhttp.setRequestHeader('x-Content-Type', content + '; charset=utf-8'); } else { this.xmlhttp.setRequestHeader('Content-Type', content + '; charset=utf-8'); } } if (this.request.isAsync) { if (this.request.callback) { this.callbackComplete = false; } this.xmlhttp.onreadystatechange = function() { self._readyStateChangeCallback(); } } else { this.xmlhttp.onreadystatechange = function() {} } var payload = this.request.getSerializedPayload(); if (payload) { this.xmlhttp.setRequestHeader('Content-Length', payload.length); } this.xmlhttp.send(payload); if (!this.request.isAsync) { if ( this.xmlhttp.status == 200 ) { HTML_AJAX.requestComplete(this.request); if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load) { HTML_AJAX.Load(this.request); } return this._decodeResponse(); } else { var e = new Error('['+this.xmlhttp.status +'] '+this.xmlhttp.statusText); e.headers = this.xmlhttp.getAllResponseHeaders(); this._handleError(e); } } else { var self = this; this._timeoutId = window.setTimeout(function() { self.abort(true); },this.request.timeout); } } catch (e) { this._handleError(e); } }, abort: function (automatic) { if (this.callInProgress()) { this.aborted = true; this.xmlhttp.abort(); if (automatic) { HTML_AJAX.requestComplete(this.request); this._handleError(new Error('Request Timed Out: time out was '+this.request.timeout+'ms')); } } }, _readyStateChangeCallback:function() { try { switch(this.xmlhttp.readyState) { case 1: break; case 2: if (this.request.Send) { this.request.Send(); } else if (HTML_AJAX.Send) { HTML_AJAX.Send(this.request); } break; case 3: if (this.request.Progress) { this.request.Progress(); } else if (HTML_AJAX.Progress ) { HTML_AJAX.Progress(this.request); } break; case 4: window.clearTimeout(this._timeoutId); if (this.aborted) { if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load) { HTML_AJAX.Load(this.request); } } else if (this.xmlhttp.status == 200) { if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load ) { HTML_AJAX.Load(this.request); } var response = this._decodeResponse(); if (this.request.callback) { this.request.callback(response); this.callbackComplete = true; } } else { var e = new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText); this._handleError(e); } HTML_AJAX.requestComplete(this.request); break; } } catch (e) { this._handleError(e); } }, _decodeResponse: function() { var content = null; try { content = this.xmlhttp.getResponseHeader('X-Content-Type'); } catch(e) {} if(!content || content == null) { content = this.xmlhttp.getResponseHeader('Content-Type'); } if(content.indexOf(';') != -1) { content = content.substring(0, content.indexOf(';')); } if(content == 'application/xml') { return this.xmlhttp.responseXML; } var unserializer = HTML_AJAX.serializerForEncoding(content); return unserializer.unserialize(this.xmlhttp.responseText); }, _handleError: function(e) { HTML_AJAX.requestComplete(this.request,e); if (this.request.onError) { this.request.onError(e); } else if (HTML_AJAX.onError) { HTML_AJAX.onError(e,this.request); } else { throw e; } } } function HTML_AJAX_Dispatcher(className,mode,callback,serverUrl,serializerType) { this.className = className; this.mode = mode; this.callback = callback; this.serializerType = serializerType; if (serverUrl) { this.serverUrl = serverUrl } else { this.serverUrl = window.location; } } HTML_AJAX_Dispatcher.prototype = { queue: 'default', timeout: 20000, priority: 0, options: {}, doCall: function(callName,args) { var request = new HTML_AJAX_Request(); request.requestUrl = this.serverUrl; request.className = this.className; request.methodName = callName; request.timeout = this.timeout; request.contentType = this.contentType; request.serializer = eval('new HTML_AJAX_Serialize_'+this.serializerType); request.queue = this.queue; request.priority = this.priority; for(var i in this.options) { request[i] = this.options[i]; } for(var i=0; i < args.length; i++) { request.addArg(i,args[i]); }; if ( this.mode == "async" ) { request.isAsync = true; if (this.callback[callName]) { var self = this; request.callback = function(result) { self.callback[callName](result); } } } else { request.isAsync = false; } return HTML_AJAX.makeRequest(request); }, Sync: function() { this.mode = 'sync'; }, Async: function(callback) { this.mode = 'async'; if (callback) { this.callback = callback; } } };HTML_AJAX.Open = function(request) { var loading = document.getElementById('HTML_AJAX_LOADING'); if (!loading) { loading = document.createElement('div'); loading.id = 'HTML_AJAX_LOADING'; loading.innerHTML = 'Loading...'; loading.style.color = '#888888'; loading.style.position = 'absolute'; loading.style.top = 0; loading.style.right = 0; loading.style.fontSize = '11px'; loading.style.width = '60px'; loading.style.padding = '4px'; loading.style.fontFamily = 'Arial, Helvetica, sans'; loading.count = 0; document.body.insertBefore(loading,document.body.firstChild); } else { if (loading.count == undefined) { loading.count = 0; } } loading.count++; if (request.isAsync) { request.loadingId = window.setTimeout(function() { loading.style.display = 'block'; },500); } else { loading.style.display = 'block'; } } HTML_AJAX.Load = function(request) { if (request.loadingId) { window.clearTimeout(request.loadingId); } var loading = document.getElementById('HTML_AJAX_LOADING'); loading.count--; if (loading.count == 0) { loading.style.display = 'none'; } } Array.prototype.______array = '______array'; var HTML_AJAX_JSON = { org: '../www.json.org/default.htm', copyright: '(c)2005 JSON.org', license: '../www.crockford.com/JSON/license.html', stringify: function (arg) { var c, i, l, s = '', v; switch (typeof arg) { case 'object': if (arg) { if (arg.______array == '______array') { for (i = 0; i < arg.length; ++i) { v = this.stringify(arg[i]); if (s) { s += ','; } s += v; } return '[' + s + ']'; } else if (typeof arg.toString != 'undefined') { for (i in arg) { v = arg[i]; if (typeof v != 'undefined' && typeof v != 'function') { v = this.stringify(v); if (s) { s += ','; } s += this.stringify(i) + ':' + v; } } return '{' + s + '}'; } } return 'null'; case 'number': return isFinite(arg) ? String(arg) : 'null'; case 'string': l = arg.length; s = '"'; for (i = 0; i < l; i += 1) { c = arg.charAt(i); if (c >= ' ') { if (c == '\\' || c == '"') { s += '\\'; } s += c; } else { switch (c) { case '\b': s += '\\b'; break; case '\f': s += '\\f'; break; case '\n': s += '\\n'; break; case '\r': s += '\\r'; break; case '\t': s += '\\t'; break; default: c = c.charCodeAt(); s += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); } } } return s + '"'; case 'boolean': return String(arg); default: return 'null'; } }, parse: function (text) { var at = 0; var ch = ' '; function error(m) { throw { name: 'JSONError', message: m, at: at - 1, text: text }; } function next() { ch = text.charAt(at); at += 1; return ch; } function white() { while (ch) { if (ch <= ' ') { next(); } else if (ch == 'default.htm') { switch (next()) { case 'default.htm': while (next() && ch != '\n' && ch != '\r') {} break; case '*': next(); for (;;) { if (ch) { if (ch == '*') { if (next() == 'default.htm') { next(); break; } } else { next(); } } else { error("Unterminated comment"); } } break; default: error("Syntax error"); } } else { break; } } } function string() { var i, s = '', t, u; if (ch == '"') { outer: while (next()) { if (ch == '"') { next(); return s; } else if (ch == '\\') { switch (next()) { case 'b': s += '\b'; break; case 'f': s += '\f'; break; case 'n': s += '\n'; break; case 'r': s += '\r'; break; case 't': s += '\t'; break; case 'u': u = 0; for (i = 0; i < 4; i += 1) { t = parseInt(next(), 16); if (!isFinite(t)) { break outer; } u = u * 16 + t; } s += String.fromCharCode(u); break; default: s += ch; } } else { s += ch; } } } error("Bad string"); } function array() { var a = []; if (ch == '[') { next(); white(); if (ch == ']') { next(); return a; } while (ch) { a.push(value()); white(); if (ch == ']') { next(); return a; } else if (ch != ',') { break; } next(); white(); } } error("Bad array"); } function object() { var k, o = {}; if (ch == '{') { next(); white(); if (ch == '}') { next(); return o; } while (ch) { k = string(); white(); if (ch != ':') { break; } next(); o[k] = value(); white(); if (ch == '}') { next(); return o; } else if (ch != ',') { break; } next(); white(); } } error("Bad object"); } function number() { var n = '', v; if (ch == '-') { n = '-'; next(); } while (ch >= '0' && ch <= '9') { n += ch; next(); } if (ch == '.') { n += '.'; while (next() && ch >= '0' && ch <= '9') { n += ch; } } if (ch == 'e' || ch == 'E') { n += 'e'; next(); if (ch == '-' || ch == '+') { n += ch; next(); } while (ch >= '0' && ch <= '9') { n += ch; next(); } } v = +n; if (!isFinite(v)) { ////error("Bad number"); } else { return v; } } function word() { switch (ch) { case 't': if (next() == 'r' && next() == 'u' && next() == 'e') { next(); return true; } break; case 'f': if (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') { next(); return false; } break; case 'n': if (next() == 'u' && next() == 'l' && next() == 'l') { next(); return null; } break; } error("Syntax error"); } function value() { white(); switch (ch) { case '{': return object(); case '[': return array(); case '"': return string(); case '-': return number(); default: return ch >= '0' && ch <= '9' ? number() : word(); } } return value(); } };