/*
 * Centralized class for accessing ajax web services.
 */
function Services(){
    this.url = null;
    this.data = null;
    this.dataType ='json';
    this.type ='POST';
    this.successFunction = null;
    this.errorFunction = null;
    this.async = true;
    this.service = null;
}

Services.prototype.login = function(user, onSuccess, onError){
    this.url = '/services/login.php';
    this.service ='login';
    this.data = user;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.forgotPassword = function(email, onSuccess, onError){
    this.url = '/services/forgotPassword.php';
    this.data = email;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.register = function(data, onSuccess, onError){
    this.url = '/services/register.php';
    this.data = data;
    this.service ='register';
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.isExistingNumberRegistered = function(number, onSuccess, onError){
    this.url = '/services/validateExistingNumber.php';
    this.data = number;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.getAvailableNumbersByAddress = function(address, onSuccess, onError){
    this.url = '/services/getAvailablePhoneNumbersByAddress.php';
    this.data = address;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.emailExists = function(email, onSuccess, onError){
    this.url = '/services/emailExists.php';
    this.data = email;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.validateIMHandle = function(im, onSuccess, onError){
    this.url = '/services/imHandleExists.php';
    this.data = im;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.validateAddress = function(address, onSuccess, onError){
    this.url = '/services/validateAddress.php';
    this.data = address;
    this.successFunction = onSuccess;
    this.errorFunction = onError;
    return this.send();
}
Services.prototype.onSuccess = function(data, status, request){
    //call responder function
    if(this.successFunction){
        this.successFunction(data, status, request)
    }
    this.reset();
}
Services.prototype.onError = function(request, status, error){
    //call responder function
    if(this.errorFunction){
        this.errorFunction(request, status, error)
    }
    this.reset();
}
Services.prototype.reset = function(){
    //clean up
    this.url = null;
    this.async = true;
    this.successFunction = null;
    this.errorFunction = null;
    this.data = null
    this.service = null;
}
Services.prototype.send = function(){
    if(!this.url){
        return -1;
    }
    var thisClass = this;
    return jQuery.ajax({
        url:this.url,
        async:this.async,
        service:this.service,
        type:this.type,
        dataType:this.dataType,
        data:this.data,
        success:function (data, status,request){
            thisClass.onSuccess(data, status,request)
        },
        error:function(request, status, error){
            thisClass.onError(request, status, error)
        }
    }
    );
}



