//////////////////////////////////////////////////////////////////////////////////// //GET IMPORTANT VARS //////////////////////////////////////////////////////////////////////////////////// var addon_CDdrilldown_base = 'http://www.propshopspain.com/property'; //////////////////////////////////////////////////////////////////////////////////// //THOUSAND SEPARATION // Function to separate thousands //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_thousands( val, separator) { var regExpRule = new RegExp('(-?[0-9]+)([0-9]{3})'); sVal = val + ''; if(separator === undefined) { separator = ","; } while(regExpRule.test(sVal)) { sVal = sVal.replace(regExpRule, '$1'+separator+'$2'); } return sVal; } //////////////////////////////////////////////////////////////////////////////////// //AJAX CODE, LOAD XMLHTTPREQUEST // Should work for various browsers //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_getHTTPObject() { if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); } try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } return false; } //////////////////////////////////////////////////////////////////////////////////// //VARIABLES //////////////////////////////////////////////////////////////////////////////////// var http = addon_CDdrilldown_getHTTPObject(); var elementList = new Array(); var selectList = new Array(); var container; var addon_CDdrilldown_errors = ''; //////////////////////////////////////////////////////////////////////////////////// //INITIALISE // Get a list of all elements // Format them //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_init() { /** CREATE AN INDEX OF SELECT OBJECTS **/ container = document.getElementById('addon_CDdrilldown'); selectList = container.getElementsByTagName('select'); nSelects = selectList.length; for( x in selectList ) { if(typeof(selectList[x])!== 'object' ){continue;} // Don't parse any non-objects; if(selectList[x].id == '') { selectList[x].id = selectList[x].name; } else { selectList[x].name = selectList[x].id; } selectList[x].id = selectList[x].name; elementList.push(selectList[x].id); selectList[x].onchange = addon_CDdrilldown_lock; /** CREATE A SPAN TO REPLACE SELECTS WHEN LOCKED **/ var newSpan = document.createElement('span'); newSpan.id = selectList[x].id + "-change"; newSpan.onclick = addon_CDdrilldown_unlock; newSpan.className = 'addon_CDdrilldown_changeSpan'; newSpan.style.display = 'none'; selectList[x].parentNode.insertBefore(newSpan, selectList[x]); /** MAKE A LOADING IMAGE TOO **/ newImage = new Image(); newImage.src = addon_CDdrilldown_base+"/addons/CDdrilldown/images/load.gif"; newImage.id = selectList[x].id + "-loading"; newImage.className = 'addon_CDdrilldown_loadImg'; newImage.style.visibility = 'hidden'; selectList[x].parentNode.insertBefore(newImage, selectList[x].nextSibling); } /** FORMAT THE SELECT OBJECTS **/ /** Probably best to do this in the loop above, so we're adding * to the list and also updating the selects as we go. **/ /** LOAD THE FIRST UPDATE **/ addon_CDdrilldown_update(); } //////////////////////////////////////////////////////////////////////////////////// //REQUEST AN UPDATE // Perform the update request and update the fields // This is done by parsing the index and finding which // elements are invisible (sending) and which are visible // (used). // The sending ones have their values added to the // 'arguments' string. // The result is a GET transaction to query.php which // is registered against a response handler function. //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_update() { var send = new Array(); var used = new Array(); var arguments = ''; /** DISABLE WHILE LOADING **/ for(x in selectList) { if(typeof(selectList[x]) !== 'object'){ continue; } selectList[x].disabled = 'disabled'; } /** PREPARE URL **/ for (i=0; i < selectList.length; i++) { switch(selectList[i].style.display) { case 'none': if(selectList[i].name.indexOf('[]') > -1) // Bug fix { // ~~~~~~~ var parsed_ptr = selectList[i].name.indexOf('[]'); // This resolves an issue with var parsed_name = selectList[i].name.substr(0, parsed_ptr); // PHP not reading url vars that } else { // contain [] in their names, such var parsed_name = selectList[i].name; // as 'pclass[]'. } /////////////////////// send.push(parsed_name); arguments += '&' + parsed_name + '=' + encodeURI(selectList[i].value); break; default: used.push(selectList[i].name); /** Also set its loading image to visible **/ var loadImg = document.getElementById(selectList[i].name + '-loading'); loadImg.style.visibility = 'visible'; } } random = new Date().getTime(); arguments = '?used=' + used.join(',') + '&send=' + send.join(',') + arguments + '&rand=' + random; /** SEND REQUEST **/ // First using PHP address, then if failed use JS address try { http.open("GET", addon_CDdrilldown_base + "/addons/CDdrilldown/query.php" + arguments, true); http.onreadystatechange = addon_CDdrilldown_dataloader; http.send(null); } catch ( e ) { http.open("GET", window.location.href + "/addons/CDdrilldown/query.php" + arguments, true); http.onreadystatechange = addon_CDdrilldown_dataloader; http.send(null); } } //////////////////////////////////////////////////////////////////////////////////// //HANDLE RESPONSE // Summary: Process the response and load it into the relevant fields. // Read each line one by one. // Where 'FIELD' is in the first few characters, select that field name. // Subsequently enter the following data lines as options //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_dataloader() { if( http.readyState < 4 ) { /** SHOW 'LOADING' SEQUENCE **/ } if( http.readyState == 4 ) { /** READ THE DATA **/ var fieldName; // The current field name var fieldData; // The field's data var response = http.responseText; var lines = new Array(); lines = response.split('
'); for(i=0; i < lines.length; i++) { /** READ EACH LINE **/ /** END IT, FOR WEB HOSTS WHO APPEND THINGS TO PAGE OUTPUT **/ if(lines[i] == "~QUERYEND~") { break; } if(lines[i].substr(0,7) == '~FIELD:') { /** IS A NEW FIELD **/ fieldName = lines[i].substr(8, lines[i].length-8); /** ( has to do some tomfoolery to get the current select object ) **/ var thisSelect; for(x in selectList) { selectList[x].name == fieldName ? thisSelect = selectList[x] : false ; } /** Empty it **/ while( thisSelect.hasChildNodes() ) { thisSelect.removeChild( thisSelect.childNodes[0] ); } var objOption = new Option('All', ''); thisSelect.options[0] = objOption; thisSelect.disabled = ''; //thisSelect.backgroundImage = ''; } else if(lines[i].substr(0,7) == '~ERROR~') { /** IS AN ERROR **/ addon_CDdrilldown_errors += "\n" + lines[i].substr(8,lines[i].length); /** should display error in a box if possible **/ alert("Addon CD Drilldown Error:\n"+addon_CDdrilldown_errors); return 'error'; } else if(lines[i] !== '') { /** IS DATA **/ var thisSelect; for(x in selectList) { selectList[x].name == fieldName ? thisSelect = selectList[x] : false ; } optionText = lines[i]; // Option text, e.g. 'Detatched (5)' optionValue = lines[i].substr(0, lines[i].lastIndexOf(' ')); // Above, less '_(5)'; /** EXCEPTIONS FOR MIN/MAX **/ switch( fieldName.substr(fieldName.length-4, fieldName.length) ) { case '-max': optionText = 'To ' + addon_CDdrilldown_thousands(lines[i]); optionValue = lines[i]; break; case '-min': optionText = 'From ' + addon_CDdrilldown_thousands(lines[i]); optionValue = lines[i]; break; } /** EXCEPTIONS FOR PCLASS **/ if( fieldName == 'pclass[]' ) { divider_colon = lines[i].indexOf(':'); strlength = lines[i].length; pclass_id = lines[i].substr(0, divider_colon); pclass_text = lines[i].substr(divider_colon+1, strlength); optionText = pclass_text; optionValue = pclass_id; } /** EXCEPTIONS FOR BLANK FIELDS **/ // Eg: Where 'city' returns ' (4)' because 4 have a blank reference. if(lines[i].substr(0, lines[i].lastIndexOf(' ')) == '' && lines[i].substr(lines[i].length-1, lines[i].length) == ')') // This second line avoids conflicts with price lines. { continue; // They won't be displayed at all. } var objOption = new Option(optionText, optionValue); var nOptions = thisSelect.options.length; thisSelect.options[nOptions] = objOption; } } /** MAKE LOADING IMAGES DISAPPEAR **/ var loadImgs = container.getElementsByTagName('img'); for(x in loadImgs) { if(typeof(loadImgs[x]) !== 'object'){ continue; } var id = loadImgs[x].id; if( id.substr( id.length - 8, id.length) == '-loading' ) { loadImgs[x].style.visibility = 'hidden'; } } /** ENABLE ONCE LOADED **/ for(x in selectList) { if(typeof(selectList[x]) !== 'object'){ continue; } selectList[x].disabled = ''; } } // <-- End readstate==4 (received) } //////////////////////////////////////////////////////////////////////////////////// //LOCK THE FIELD // Summary: When changed, a field will be 'locked' from being edited again. //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_lock( evt ) { /** FIND THE SELECT THAT HAS CHANGED **/ var thisSelect; if(window.event === undefined) { /** it's standards-compliant **/ thisSelect = evt.target; } else { /** it's IE **/ thisSelect = window.event.srcElement; } /** LOCK IT **/ var fieldName = thisSelect.name; var fieldValue = thisSelect.options[thisSelect.selectedIndex].text; var fieldSpan; var spans = container.getElementsByTagName('span'); for ( x in spans ) { if(typeof(spans[x]) !== 'object'){continue;} var spanName = spans[x].id; if(spanName.substr(spanName.length-7, spanName.length) == '-change' && spanName.substr(0, spanName.length-7) == fieldName) { fieldSpan = spans[x]; break; } } if( fieldName.substr(fieldName.length-4, fieldName.length) !== '-max' && fieldName.substr(fieldName.length-4, fieldName.length) !== '-min' && fieldValue !== 'All' ) { fieldValue = fieldValue.substr(0, fieldValue.lastIndexOf(' ')); } thisSelect.style.display = 'none'; fieldSpan.style.display = 'inline-block'; fieldSpan.innerHTML = fieldValue; addon_CDdrilldown_update(); } //////////////////////////////////////////////////////////////////////////////////// //UNLOCK THE FIELD // Summary: When clicked, the field will 'unlock' again. //////////////////////////////////////////////////////////////////////////////////// function addon_CDdrilldown_unlock( evt ) { /** FIND THE SELECT TO UNLOCK **/ var thisSpan; var thisSelect; if(window.event === undefined) { /** it's standards-compliant **/ thisSpan = evt.target; } else { /** it's IE **/ thisSpan = window.event.srcElement; } /** Should now loop through all selects and find the one that matches the span's id **/ for (x in selectList) { if(typeof(selectList[x]) !== 'object') { continue; } var field = thisSpan.id.substr(0, thisSpan.id.length-7); if(selectList[x].name == field) { thisSelect = selectList[x]; break; } } /** UNLOCK IT **/ thisSpan.style.display = 'none'; thisSelect.style.display = 'inline-block'; addon_CDdrilldown_update(); }