﻿function ADSLCheckParser(rawAdslResult) {
    this.rawAdslResult = rawAdslResult;

    //returns true if the phone number is valid
    this.IsValid = function() {
        return this.rawAdslResult.code == "ok";
    };

    //returns true if the phone number is on iiNet's network
    this.IsOnNetwork = function() {
        //return this.rawAdslResult.code == "ok";

        return !(this.rawAdslResult.exchange.availability.SSS == "unknown");
    };


    this.ExchangeName = function() {
        return this.rawAdslResult.exchange.name;
    }
}


function PhoneNumber(areaCode, prefix, number) {
    this.areaCode = areaCode;
    this.prefix = prefix;
    this.number = number;
}


function PostCodeCheckParser(rawPostCodeResult) {
    this.rawPostCodeResult = rawPostCodeResult;

    //returns true if the postcode is valid
    this.IsValid = function() {
        return (this.rawPostCodeResult.network == "on" || this.rawPostCodeResult.network == "off");
    };

    //returns true if the postcode is on iiNet's network
    this.IsOnNetwork = function() {
        return this.rawPostCodeResult.network == "on";
    };

    this.ExchangeName = function() {
        return "";
    }
}

function AdslCheckBuilder(config) {
    this.config = config;

    this.BuildUrl = function(areaCode, prefix, phoneNumber) {
        return this.config.adslCheckerUrl + "?check=EXCH&type=JSON&area=" + areaCode + "&prefix=" + prefix + "&number=" + phoneNumber + "&jsoncallback=?";
    }
}

function PostCodeCheckBuilder(config) {
    this.config = config;

    this.BuildUrl = function(postCode) {
        return this.config.postCodeCheckerUrl + "?postcode=" + postCode + "&jsoncallback=?";
    }
}

function AdslChecker(config) {
    this.config = config;

    this.CheckPhoneNumber = function(phoneNumber, callback) {
        var checkBuilder = new AdslCheckBuilder(this.config);

        $.getJSON(checkBuilder.BuildUrl(phoneNumber.areaCode, phoneNumber.prefix, phoneNumber.number),
                    function(data) {
                        var checker = new ADSLCheckParser(data);
                        callback(checker);
                    });
    }

    this.CheckPostCode = function(postcode, callback) {
        var checkBuilder = new PostCodeCheckBuilder(this.config);

        $.getJSON(checkBuilder.BuildUrl(postcode),
                  function(data) {
                      var checker = new PostCodeCheckParser(data);
                      callback(checker);
                  });
    }
}

function TestPQResults() {
    return {

        "qualifies": 1,
        "retval": 1,
        "network": "on"

    }
}