/******************************************************************************* common.js *******************************************************************************/ /** * Get the element object in the document by id. */ /** * Add a event handler to a DOM object. * @param * obj - The DOM object. * type - The event name of the DOM object. * fn - The function name of the added event handler. */ function addEvent( obj, type, fn ) { if ( obj.attachEvent ) { obj['e'+type+fn] = fn; obj[type+fn] = function() { obj['e'+type+fn]( window.event ); } obj.attachEvent( 'on'+type, obj[type+fn] ); } else if ( obj.addEventListener ) { obj.addEventListener( type, fn, false ); } else { alert("Handler could not be added."); } } /** * Remove a event handler to a DOM object. * @param * obj - The DOM object. * type - The event name of the DOM object. * fn - The function name of the added event handler. */ function removeEvent( obj, type, fn ) { if ( obj.detachEvent ) { obj.detachEvent( 'on'+type, obj[type+fn] ); obj[type+fn] = null; } else if ( obj.addEventListener ) { obj.removeEventListener( type, fn, false ); } else { alert("Handler could not be removed."); } } /** * Create an XML HTTP request object. */ function createXMLHttpRequest() { var xmlHttpRequest = null; if(window.XMLHttpRequest) { //Mozilla try { xmlHttpRequest = new XMLHttpRequest(); } catch (e) { return null; } } else if (window.ActiveXObject) { // IE try { xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return null; } } } return xmlHttpRequest; } function addToFavarite(obj, url, title) { // BlueDestiny: http://www.never-online.net/blog/article.asp?id=108 var e = window.event || arguments.callee.caller.arguments[0]; var B = { IE : /MSIE/.test(window.navigator.userAgent) && !window.opera , FF : /Firefox/.test(window.navigator.userAgent) , OP : !!window.opera }; obj.onmousedown = null; if (B.IE) { obj.attachEvent("onmouseup", function () { try { window.external.AddFavorite(url, title); window.event.returnValue = false; } catch (exp) {} }); } else { if (B.FF || obj.nodeName.toLowerCase() == "a") { obj.setAttribute("rel", "sidebar"), obj.title = title, obj.href = url; } else if (B.OP) { var a = document.createElement("a"); a.rel = "sidebar", a.title = title, a.href = url; obj.parentNode.insertBefore(a, obj); a.appendChild(obj); a = null; } } } /******************************************************************************* input.js *******************************************************************************/ /** * Enable all inputs in selected mode when they got foucs. */ function enableSelectOnFocusToInput(e) { if (!document.getElementsByTagName) return; var inputs = document.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { var input = inputs[i]; if (input.type == "text" || input.type == "password") { addEvent(input, "focus", selectOnFocus); } } } /** * Set an input in selected mode when got foucs. */ function selectOnFocus() { if ( this.select ) { this.select(); } } // Do enableSelectOnFocusToInput when window loaded. addEvent(window, "load", enableSelectOnFocusToInput); /** * Enable to clear the zero value in any input. */ function enableClearZeroForInput(e) { if (!document.getElementsByTagName) return; var inputs = document.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { var input = inputs[i]; if (input.type == "text") { if (input.value == "0" || input.value == "0.0") { input.value = ""; } else if (input.value != "") { var s = input.value; try { if (s == Math.round(s)) { input.value = Math.round(s); } } catch (e) { } } } } } // Do enableClearZeroForInput when window loaded. addEvent(window, "load", enableClearZeroForInput); /******************************************************************************* cityCode.js Add to a form three controls and cityCode.js automaticaly works: 2 selects: city, district. 1 hidden input: districtCode. *******************************************************************************/ function isCity(districtCode) { if (110000 <= districtCode && districtCode <= 119999) return true; else if (120000 <= districtCode && districtCode <= 129999) return true; else if (310000 <= districtCode && districtCode <= 319999) return true; else if (500000 <= districtCode && districtCode <= 509999) return true; else return false; } function updateSelect(select, code, xml, onchange) { while(select.childNodes.length > 0) { select.removeChild(select.childNodes[0]); } var results = xml.getElementsByTagName("i"); var option = null; for(var i = 0; i < results.length; i++) { option = document.createElement("option"); option.appendChild(document.createTextNode(results[i].getAttribute("n"))); option.value=results[i].getAttribute("c"); if (option.value == code) { option.selected = true; } select.appendChild(option); } select.onchange(); } function onReadyState4(xmlHttpRequest, select, code, onchange) { if (xmlHttpRequest.status == 200) { updateSelect(select, code, xmlHttpRequest.responseXML, onchange); } else { select.value = xmlHttpRequest.status; } } function createRequest(select, code, form) { var request = new Object; request.xmlHttpRequest = createXMLHttpRequest(); if (request.xmlHttpRequest == null) { return null; } request.select = select; request.code = code; request.form = form; return request; } function sendRequest(url, request, callback) { var xmlHttpRequest = request.xmlHttpRequest; xmlHttpRequest.open("GET", url, true); xmlHttpRequest.onreadystatechange = callback; xmlHttpRequest.send(""); } /************************************************ * request objects and functions section * */ function updateCity(request) { var xmlHttpRequest = request.xmlHttpRequest; if (xmlHttpRequest.readyState == 4) { var select = request.select; var code = null; if (isCity(request.code)) { code = Math.floor(request.code / 10000) * 10000; } else { code = Math.floor(request.code / 100) * 100; } onReadyState4(xmlHttpRequest, select, code, onCity); } } function updateDistrict(request) { var xmlHttpRequest = request.xmlHttpRequest; if (xmlHttpRequest.readyState == 4) { var select = request.select; var code = request.code; onReadyState4(xmlHttpRequest, select, code, onDistrict); } } /************************************************ * send functions * */ function sendCity(form, code) { if (theCity(form) == null) { onDistrictCodeChange(form); return; } var request = createRequest(theCity(form), code, form); if (request != null) { sendRequest("/frontend/ajax/selectedCities.do", request, function() { updateCity(request); }); } } function sendDistrict(form, code) { if (theDistrict(form) == null) { onDistrictCodeChange(form); return; } var request = createRequest(theDistrict(form), code, form); if (request != null) { sendRequest("/frontend/ajax/selectedDistricts.do?districtCode=" + code, request, function() { updateDistrict(request); }) } } /************************************************ * onload function * */ function theDistrictCode(form) { return form["districtCode"]; } function theCity(form) { return form["city"]; } function theDistrict(form) { return form["district"]; } function onDistrictCodeChange(form) { if (form["onDistrictCodeChange"] != null) { form.onDistrictCodeChange(); } } function searchAndInitCityCodeForms(e) { for (var i = 0; i < document.forms.length; i++) { var form = document.forms[i]; if (theDistrictCode(form) != null && theCity(form) != null && theDistrict(form) != null) { initCityCodeForm(form); } } } function initCityCodeForm(form) { if (theCity(form).onchange == null) { theCity(form).onchange = onInitCity; theDistrict(form).onchange = onDistrict; var code = theDistrictCode(form).value; sendCity(form, code); } } function onInitCity() { var form = this.form; theCity(form).onchange = onCity; var code = theDistrictCode(form).value; sendDistrict(form, code); } function onCity() { saveUrl('indexSearchChangeCity'); var form = this.form; var code = theCity(form).value; theDistrictCode(form).value = code; sendDistrict(form, code); } function onDistrict() { saveUrl('indexSearchChangeDistrict'); var form = this.form; theDistrictCode(form).value = theDistrict(form).value; onDistrictCodeChange(form); } addEvent(window, "load", searchAndInitCityCodeForms); /******************************************************************************* businessZone.js Add to a form a select (businessZone) and businessZone.js automaticaly works. Add a div (form.name + "_businessZones") and businessZone.js automaticaly works. *******************************************************************************/ function sendBusinessZonesRequest(url, form) { var businessZonesRequest = createXMLHttpRequest(); businessZonesRequest.open("GET", url, true); businessZonesRequest.onreadystatechange = function() { onBusinessZones(form, businessZonesRequest); } businessZonesRequest.send(""); } function onBusinessZones(form, businessZonesRequest) { if (businessZonesRequest.readyState == 4) { if (businessZonesRequest.status == 200) { var items; // 从返回中解析出所有的商圈 items = businessZonesRequest.responseXML.getElementsByTagName("h2"); var businessZones = new Array; var zoneCodes = new Array; for( var i = 0; i < items.length; i++) { businessZones[i] = items[i].getAttribute("n"); zoneCodes[i] =items[i].getAttribute("c"); } form["_businessZones"] = businessZones; form["_businessCodeZones"] = zoneCodes; // 更新商圈控件 var ctrlBusinessZone = formBusinessZone(form); var ctrlBusinessZones = formBusinessZones(form); if(ctrlBusinessZone) updateCtrlBusinessZone(form, ctrlBusinessZone.value); //if (ctrlBusinessZones == null && ctrlBusinessZone) { //updateCtrlBusinessZone(form, ctrlBusinessZone.value); //} //else { // updateCtrlBusinessZone(form, ctrlBusinessZone.value);//updateCtrlBusinessZones(form, null); //} } } } function updateCtrlBusinessZone(form, businessZone) { var businessZones = form["_businessZones"]; var businessCodeZones = form["_businessCodeZones"]; var ctrlBusinessZone = formBusinessZone(form); if(ctrlBusinessZone.tagName.toUpperCase() != "SELECT") { return; } for (var c = ctrlBusinessZone.childNodes.length - 1; c >= 0; c--) { var childNode = ctrlBusinessZone.childNodes[c]; if (typeof childNode.tagName == "undefined") { ctrlBusinessZone.removeChild(childNode); } } while(ctrlBusinessZone.childNodes.length > 1) { ctrlBusinessZone.removeChild(ctrlBusinessZone.childNodes[1]); } for(var i = 0; i < businessZones.length; i++) { var option = document.createElement("option"); option.appendChild(document.createTextNode(businessZones[i])); option.value = businessCodeZones[i]; if (businessZone == businessCodeZones[i]) { option.selected = true; businessZone = null; } ctrlBusinessZone.appendChild(option); } } function updateCtrlBusinessZones(form, businessZone) { var businessZones = form["_businessZones"]; var ctrlBusinessZones = formBusinessZones(form); while(ctrlBusinessZones.childNodes && ctrlBusinessZones.childNodes.length > 0) { ctrlBusinessZones.removeChild(ctrlBusinessZones.childNodes[0]); } for(var i = 0; i < businessZones.length; i++) { var checkbox = document.createElement("input"); checkbox.id = "bz_" + Math.round(Math.random() * 1.0e16); checkbox.name = "businessZone"; checkbox.value = businessZones[i]; checkbox.type = "checkbox"; checkbox["class"] = "checkbox"; if (businessZone == businessZones[i]) { checkbox.selected = true; businessZone = null; } ctrlBusinessZones.appendChild(checkbox); var label = document.createElement("label"); label.appendChild(document.createTextNode(businessZones[i])); label["for"] = checkbox["id"]; label["class"] = "checkbox"; ctrlBusinessZones.appendChild(label); ctrlBusinessZones.appendChild(document.createTextNode(" ")); } } function sendBusinessZone(form, districtCode) { var url = "/frontend/ajax/listBusinessZones.do?districtCode=" + districtCode; sendBusinessZonesRequest(url, form); } function formBusinessZone(form) { return form["businessZone"]; } function formBusinessZones(form) { return $(form.name + "_businessZones"); } function searchAndInitBusinessZoneForms(e) { for (var i = 0; i < document.forms.length; i++) { var form = document.forms[i]; if (theDistrictCode(form) != null && (formBusinessZone(form) != null || formBusinessZones(form) != null)) { initBusinessZoneForm(form); } } } function initBusinessZoneForm(form) { if (form["onDistrictCodeChange" + "_BusinessZone"] == null) { if (form["onDistrictCodeChange"] != null) { form["onDistrictCodeChange" + "_BusinessZone"] = form["onDistrictCodeChange"]; form["onDistrictCodeChange"] = function() { form["onDistrictCodeChange" + "_BusinessZone"]; sendBusinessZone(form, theDistrictCode(form).value); } } else { form["onDistrictCodeChange"] = function() { sendBusinessZone(form, theDistrictCode(form).value); } } form["onDistrictCodeChange"](); } } addEvent(window, "load", searchAndInitBusinessZoneForms);