






XMLRPC._ajaxErrorHandler = function(text) { return true; }
page.m.addRatingBlock = function(providerId) {
var ratingBg = $('rating_bg_'+providerId);
var ratingHover = $('rating_hover_'+providerId);
var ratingActual = $('rating_actual_'+providerId);
[ratingBg, ratingHover, ratingActual].each(function(item, index) { 
addEvent(item, "mousemove", function(e) {
var cur = getPosition(e);
var offset = cur.x - getLeft(ratingBg);
var ratingInt = Math.ceil(offset / 23);
ratingHover.className = "rating_hover " + "rating_" + ratingInt.toString();
});
});
addEvent(ratingBg, "click", function(e) {
var cur = getPosition(e);
var offset = cur.x - getLeft(ratingBg);
var ratingInt = Math.ceil(offset / 23);
XMLRPC.call(Links.xmlrpc(),
page.c.onProviderRateSuccess,
page.c.onProviderRateFailure, 
page.c.onProviderRateFailure, 
'provider.rate',
[providerId, ratingInt*2]);
});
addEvent(ratingBg, "mouseout", function(e) {
ratingHover.className = "rating_hover";
});
}
page.c.onProviderRateSuccess = function(response) {
if (!response.param) { 
alert("You've already voted for a provider in last 24 hours.");
return;
};
var providerId = response.param.id;
var rating = response.param.rating;
var ratingActual = $('rating_actual_'+providerId);
ratingActual.className = "rating_actual rating_" + Math.round(rating/2).toString();
alert("Thank you for your feedback. Your vote has been accepted");
}
page.c.onProviderRateFailure = function(response) {
alert("There was an error submitting your vote.\nPlease try again later");
};












XMLRPC._ajaxErrorHandler = function(text) { return true; }
page.m.hilightedCells = [];
page.m.map = null;
page.m.visible = null;
page.m.addMapLocation = function(linkId, mapId, providerData) {
addEvent($(linkId), 'click', function(e) {
if (page.m.visible == linkId) {
$("map_container").appendChild($("location_map"));
page.m.visible = null;
return;
};
if (GBrowserIsCompatible()) {
page.m.visible = linkId;
$(mapId).appendChild($("location_map"));
var icon = new GIcon();
icon.image = Links.image("map_marker.png");
icon.shadow = Links.image("map_marker_shadow.png");
icon.iconSize = new GSize(32, 32);
icon.shadowSize = new GSize(32, 32);
icon.iconAnchor = new GPoint(16, 28);
icon.infoWindowAnchor = new GPoint(16, 28);
var marker = new GMarker(new GLatLng(providerData.latitude, providerData.longitude), icon);
page.m.map = new GMap2($("location_map"));
page.m.map.setCenter(new GLatLng(providerData.latitude, providerData.longitude), 13);
page.m.map.addControl(new GSmallMapControl());
page.m.map.addControl(new GMapTypeControl());
page.m.map.addOverlay(marker);
marker.openInfoWindowHtml(providerData.name);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(providerData.name);
});
};
});  
}

page.c.addOnLoadHandler(function(e) {
});







page.m._carCategories = [];
page.m.addCarCategory = function(id, name) {
page.m._carCategories.push({ id: id, name: name });
}


page.c.addOnLoadHandler(function(e) {
addEvent($("link_apply_filter"), "click", page.c.onApplyFilterClick);
});
page.c.onApplyFilterClick = function(e) {
var selected = [];
page.m._carCategories.each(function(category) {
var inputId = "item_" + category.id.toString();
var input = $(inputId);
if (input.checked) {
selected.push(category.id);
};
}); 
if (selected.length == 0) {
alert("Please select at least one category");
return;
};
var url = Links.action('directory', { mode: 'car', id: [selected] });
document.location = url;
}
Bindings.prototype = new Object();
Bindings.prototype.add = Bindings_add;
Bindings.prototype.handle = Bindings_handle;
Bindings.prototype.handleGroup = Bindings_handleGroup;
Bindings.prototype.remove = Bindings_remove;
function Bindings() {
this._bindings = {};
this.GROUPS = ['element', 'group', 'all'];
for (var i=0; i<this.GROUPS.length; i++) {
this._bindings[this.GROUPS[i]] = { sequence: [], hash: {} };
};
}
function Bindings_add(fun, group) {
this.remove(fun, group);
this._bindings[group].sequence.push(fun);
this._bindings[group].hash[fun] = this._bindings[group].sequence.length - 1;
}
function Bindings_handle(event) {
var handled = false;
for (var i=0; i<this.GROUPS.length; i++) {
var data = this.handleGroup(this.GROUPS[i], event);
handled = handled || data.handled;
if (data.terminated) {
break;
};
};
if (handled) {

};
}
function Bindings_handleGroup(group, event) {
var binding = this._bindings[group];
if (!binding || binding.sequence.length == 0) { 
return { handled: false, terminated: false };
};
for (var i = binding.sequence.length - 1; i >= 0; i--) {
if (!binding.sequence[i](event)) {
return { handled: true, terminated: true };
};
};
return { handled: true, terminated: false };
}
function Bindings_remove(fun, group) {
var index = this._bindings[group].hash[fun];
if (index == null) {
return;
};
this._bindings[group].sequence.splice(index, 1);
this._bindings[group].hash[fun] = null;
}
var Event = new Object();
Event.addElement = function(eventType, element, handler) {
Event.initBindings(element, eventType);
element._eventBindings[eventType].add(handler, 'element');
};
Event.addGroup = function(eventType, element, handler) {
Event.initBindings(element, eventType);
element._eventBindings[eventType].add(handler, 'group');
};
Event.initBindings = function(element, eventType) {
if (!element._eventBindings) {
element._eventBindings = {};
};
if (!element._eventBindings[eventType]) { 
element._eventBindings[eventType] = new Bindings();
addEvent(element, eventType, function(e) { element._eventBindings[eventType].handle(e); });
};
};






var Validators = {
POPUP_BOTTOM: 0,
POPUP_TOP: 1,
POPUP_LEFT: 2,
POPUP_RIGHT: 3,
errorPopup: null,
hideCallback: null,
offsetX: 3,
offsetY: 3,
timeout: 3000,
_errorPopupVisible: false
};
Validators.handleError = function(element, text) {
this.showErrorPopup(element, text);
}
Validators.showErrorPopup = function(element, text) {
if (Validators._errorPopupVisible) {
return;
};
try {
element.focus();
textBoxSelect(element, 0, element.value.length);
} catch (e) {


};
Validators.showErrorPopupNoSelect(element, text);
}
Validators.getErrorPopup = function() {
if (this.errorPopup == null) {
this.errorPopup = this.createErrorPopup();
};
return this.errorPopup;
}
Validators.getErrorPopupContent = function() {
if (this.errorPopup == null) {
this.errorPopup = this.createErrorPopup();
};
return this.errorPopup;
}
Validators.createErrorPopup = function() {
var errorPopup = document.createElement('div');
errorPopup.className = 'errorpopup';
document.body.appendChild(errorPopup);
addEvent(errorPopup, 'click', function(e) { Validators.hideErrorPopup(); });
return errorPopup;
}
Validators.hideErrorPopup = function() {
if (this.hideCallback != null) {
clearTimeout(this.hideCallback);
this.hideCallback = null;
};
this.doHideErrorPopup();
this._errorPopupVisible = false;
}
Validators.doHideErrorPopup = function() {
this.getErrorPopup().style.visibility = 'hidden';
}
Validators.showErrorPopupNoSelect = function(element, text) { 
if (this._errorPopupVisible) {
return;
};

this.hideErrorPopup();
try {
element.focus();
} catch (e) {

};
this.updateErrorPopup(text);
this.positionErrorPopup(element);
this.displayErrorPopup();
if (this.timeout) {
this.hideCallback = setTimeout(function() { Validators.hideCallback = null; Validators.hideErrorPopup(); }, this.timeout);
};
this._errorPopupVisible = true;
};
Validators.displayErrorPopup = function() {
var error_item = this.getErrorPopup();
error_item.style.visibility = 'visible';
error_item.style.position = 'absolute';
}
Validators.updateErrorPopup = function(text) {
var error_item = this.getErrorPopupContent();
error_item.innerHTML = text + "<div class='comment'>" + _("Click to Close the Window") + "</div>";
}
Validators.positionErrorPopup = function(element) {
var error_item = this.getErrorPopup();

var popupBase = element;
if (getElementComputedStyle(popupBase, "display") == "none") {
popupBase = $(element.id + "___Frame");
};

var popupPosition = this.POPUP_BOTTOM;
var top;
var left;
switch (popupPosition) {
case this.POPUP_TOP:
left = getLeft(popupBase) + this.offsetX;
top = (getTop(popupBase) - error_item.clientHeight - this.offsetY);
break;
case this.POPUP_BOTTOM:
left = getLeft(popupBase) + this.offsetX;
top = getBottom(popupBase) + this.offsetY;
break;
case this.POPUP_LEFT:
top  = getTop(popupBase);
left = getLeft(popupBase) - 150 - this.offsetX;
break;
case this.POPUP_RIGHT:
top  = getTop(popupBase);
left = getRight(popupBase) + this.offsetX;
break;
};
if (top < 0) { top = 0; };

if (left + 150 > document.body.clientWidth) {
left = document.body.clientWidth - 150;
};
if (left < 0) { left = 0; };
error_item.style.left = left.toString() + 'px';
error_item.style.top  = top.toString() + 'px';
}

Array.prototype.all = function() {
return this.reduce(function(a,b) { return a && b; }, true);
}







Validators.not_empty = function(elements, messages) {
var _t = this;
var result = elements.map(function(element) {
if (element.value == "") {
_t.handleError(element, messages.value_missing);
return false;
} else {
return true;
};
});
return result.all();
}




function $F(id) {
if ($(id)) {
if ($(id).getAttribute("multiple")) {
return $FM(id);
};
return $(id).value;
};
if ($(id + "_yy") && 
$(id + "_mm") && 
$(id + "_dd")) {
return $FD(id);
};
}


function $FD(id) {
return sprintf("%s-%02s-%02s", $F(id + "_yy"), $F(id + "_mm"), $F(id + "_dd"));
}


function $FM(id) {
var result = [];
$A($(id).options).each(function(option) {
if (option.selected) {
result.push(option.value);
};
});
return result;
}

Array.prototype.forall = function(callback) {
return this.reduce(function(a,b) { return a && callback(b); }, true);
}



Array.prototype.findFirst = function(predicate) {
for (var i = 0; i < this.length; i++) {
var item = this[i];
if (predicate(item)) {
return item;
};
};
return null;
}





Class.extend(FormControl, Object);
function FormControl(id, initialValue, validators, filters) {
FormControl.baseConstructor.call(this);
this.id = id;
this.initialValue = initialValue;
this.validators = validators;
this.filters = filters;
}
FormControl.prototype.validate = function() {
var validationResult;
if ($(this.id)) {
var oldValue = this.getValue();
this.setValue(this.filter(oldValue));
validationResult = !this.validators.findFirst(function(validator) { return !validator(); });
this.setValue(oldValue);
} else {
validationResult = !this.validators.findFirst(function(validator) { return !validator(); });
};
return validationResult;
}
FormControl.prototype.filter = function(value) {
return this.filters.reduce(function(value, f) { return f.apply(value); }, value);
}
FormControl.prototype.getName = function() {
return $(this.id).name;
}
FormControl.prototype.getValue = function() {
return $(this.id).value;
}
FormControl.prototype.setValue = function(value) {
$(this.id).value = value;
}
Class.extend(FormControlRadio, FormControl);
function FormControlRadio(id, initialValue, ids, validators, filters) {
FormControlRadio.baseConstructor.call(this, id, initialValue, validators, filters);
this.ids = ids;
}
FormControlRadio.prototype.getName = function() {
return $(this.ids[0]).name;
}
FormControlRadio.prototype.getValue = function() {
var checked = this.ids.findFirst(function(id) { return $(id) && $(id).checked; });
if (!checked) return null;
return $(checked).value;
}











Class.extend(InputForm, Object);
function InputForm() {
this._id = null;
this._validatedHandlers = [];
this._controls = [];
this._target = null;
this._targetLoaded = false;
this._submitting = false;
}
InputForm.prototype.getId = function() {
return this._id;
}
InputForm.prototype.addControl = function(control) {
this._controls.push(control);
}
InputForm.prototype.addValidatedHandler = function(handler) {
this._validatedHandlers.push(handler);
}
InputForm.prototype.beforeValidated = function(e) {
if (window.FCKeditorAPI) {
this._controls.each(function(control) {
var editor = FCKeditorAPI.GetInstance(control.id);
if (editor) {
$(control.id).value = editor.GetXHTML(true);
};
});
}
}
InputForm.prototype.getTarget = function() {
return this._target;
}
InputForm.prototype.getSubmitting = function() {
return this._submitting;
}
InputForm.prototype.isModified = function() {
var notModified = this._controls.forall(function(control) { 
return control.initialValue == $F(control.id); 
});
return !notModified;
}
InputForm.prototype.saveState = function() {
this._controls.each(function(control) { 
control.initialValue = $F(control.id);
});
}
InputForm.prototype.validate = function() {
return this._controls.findFirst(function(control) { return !control.validate(); });
}
InputForm.prototype.setId = function(id) {
this._id = id;
}
InputForm.prototype.setSubmitting = function(value) {
this._submitting = value;
}
InputForm.prototype.setTarget = function(target) {
this._target = target;
}
InputForm.prototype.setupEvents = function() {
var form = this;
addEvent($(this.getId()), "submit", function(e) {
page._inputFeatures.each(function(feature) { feature.cleanup(); });
form.setSubmitting(true);
form.beforeValidated(e);

var badField = null;
var forceSend = false;
var status;

badField = form.validate();




if (badField || forceSend) {
form.setSubmitting(false);
page._inputFeatures.each(function(feature) { 
if (!badField || $(badField.id) !== feature._element) {
feature.restore();
};
});
cancelEvent(e);
} else {
form.onValidated(e);
};
});
}
InputForm.prototype.onValidated = function(e) {
this._validatedHandlers.each(function(handler) {
handler(e);
});
}
InputForm.prototype.onTargetLoad = function(e) {
if (!this._targetLoaded) {
this._targetLoaded = true;
return;
};
this.onTargetLoadCustom(e);
}
InputForm.prototype.onTargetLoadCustom = function() {
}
InputForm.prototype.toHash = function() {
var result = this._controls.reduce(function(result, control) { 
result[control.getName()] = control.getValue(); 
return result; 
}, {});
return result;
}



