// JavaScript Document

function rpcValidator(_config) {
	this.config = _config;
	this.service = 'validator';
}


rpcValidator.prototype.getDistance = function(point1, point2, callback) {

	function point(latitude, longitude) {
		this.latitude = new xmlrpcval(latitude);
		this.longitude = new xmlrpcval(longitude);
	}
	
	var var_point1 = new point(point1.latitude, point1.longitude);
	var par_point1 = new xmlrpcval();
	par_point1.addStruct(var_point1);

	var var_point2 = new point(point2.latitude, point2.longitude);
	var par_point2 = new xmlrpcval();
	par_point2.addStruct(var_point2);

	var msg = new xmlrpcmsg('validator.getDistance');
    msg.addParam(new xmlrpcval(this.config.user, 'string'));
    msg.addParam(new xmlrpcval(this.config.password, 'string'));
	msg.addParam(par_point1);
	msg.addParam(par_point2);

	path = this.config.path + '/xmlrpc/proxy.php?server=' + this.config.server + '&service=' + this.service;
	client = new xmlrpc_client(path, this.config.proxy, this.config.port);
	if(callback) {
	    client.send(msg, this.config.method, callback);
		return true;
	} else {
	    var res = client.send(msg, this.config.method);
		if (res.faultCode()) {
			return "An error occurred: " + res.faultString();
		}
	    return xmlrpc_decode(res.value());
	}
}

rpcValidator.prototype.getPostalAddress = function(country, zipcode, number, callback) {

    var msg = new xmlrpcmsg('validator.getPostalAddress');
    msg.addParam(new xmlrpcval(this.config.user, 'string'));
    msg.addParam(new xmlrpcval(this.config.password, 'string'));
    msg.addParam(new xmlrpcval(country, 'string'));
    msg.addParam(new xmlrpcval(zipcode, 'string'));
    msg.addParam(new xmlrpcval(number, 'string'));

	path = this.config.path + '/xmlrpc/proxy.php?server=' + this.config.server + '&service=' + this.service;
	client = new xmlrpc_client(path, this.config.proxy, this.config.port);
	if(callback) {
	    client.send(msg, this.config.method, callback);
		return true;
	} else {
	    var res = client.send(msg, this.config.method);
		if (res.faultCode()) {
			return "An error occurred: " + res.faultString();
		}
	    return xmlrpc_decode(res.value());
	}
}

rpcValidator.prototype.isValidBankAccount = function(country, account, callback) {

    var msg = new xmlrpcmsg('validator.isValidBankAccount');
    msg.addParam(new xmlrpcval(this.config.user, 'string'));
    msg.addParam(new xmlrpcval(this.config.password, 'string'));
    msg.addParam(new xmlrpcval(country, 'string'));
    msg.addParam(new xmlrpcval(account, 'string'));

	path = this.config.path + '/xmlrpc/proxy.php?server=' + this.config.server + '&service=' + this.service;
	client = new xmlrpc_client(path, this.config.proxy, this.config.port);
	if(callback) {
	    client.send(msg, this.config.method, callback);
		return true;
	} else {
	    var res = client.send(msg, this.config.method);
		if (res.faultCode()) {
		  return "An error occurred: " + res.faultString();
		}
	    return xmlrpc_decode(res.value());
	}
}

rpcValidator.prototype.isValidEmailAddress = function(email, method, callback) {

    var msg = new xmlrpcmsg('validator.isValidEmailAddress');
    msg.addParam(new xmlrpcval(this.config.user, 'string'));
    msg.addParam(new xmlrpcval(this.config.password, 'string'));
    msg.addParam(new xmlrpcval(email, 'string'));
    msg.addParam(new xmlrpcval(method, 'string'));

	path = this.config.path + '/xmlrpc/proxy.php?server=' + this.config.server + '&service=' + this.service;
	client = new xmlrpc_client(path, this.config.proxy, this.config.port);
	if(callback) {
	    client.send(msg, this.config.method, callback);
		return true;
	} else {
	    var res = client.send(msg, this.config.method);
		if (res.faultCode()) {
		  return "An error occurred: " + res.faultString();
		}
//	    return res.value();
	    return xmlrpc_decode(res.value());
	}
}
