﻿/*

    PhoneNumbers.js
    
    Used to enforce particular phone number formats for consistency


*/

function forcePhoneFormat(regionID, theField) {

    if (theField && theField.value && theField.valueOf != '') {

        if (theField.value.indexOf('+64') > -1) {
            //New Zealand - we'll just leave as is
        } else {

            //Remove +61
            theField.value = theField.value.replace("+61", "0");

            //Remove anything but digits
            theField.value = theField.value.replace(/[^\d\.\+]/g, "");

            // 13XXXX - change to 13 XX XX
            if (theField.value.length == 6 && theField.value.substr(0, 2) == "13") {
                theField.value = "13 " + theField.value.substr(2, 2) + " " + theField.value.substr(4, 2);

                // 1300XXXXXX or 1800XXXXXX - Change to 1300 XXX XXXX  or 1800 XXX XXX
            } else if (theField.value.length == 10 &&
                         (theField.value.substr(0, 4) == "1300" || theField.value.substr(0, 4) == "1800")) {
                theField.value = theField.value.substr(0, 4) + " " + theField.value.substr(4, 3) + " " + theField.value.substr(7, 3);

                // XXXXXXXX - change to (XX) XXXX XXXX
            } else if (theField.value.length == 8) {
                theField.value = "(" + areaCodeForRegion(regionID) + ") " + theField.value.substr(0, 4) + " " + theField.value.substr(4, 4);

                // 04XXXXXXXX - change to XXXX XXX XXX (MOBILE)
            } else if (theField.value.length == 10 && theField.value.substr(0, 2) == "04") {
                theField.value = theField.value.substr(0, 4) + " " + theField.value.substr(4, 3) + " " + theField.value.substr(7, 3);

                // XXXXXXXXXX - change to (XX) XXXX XXXX
            } else if (theField.value.length == 10 && theField.value.substr(0, 1) == "0") {
                theField.value = "(" + theField.value.substr(0, 2) + ") " + theField.value.substr(2, 4) + " " + theField.value.substr(6, 4);

                // ANYTHING ELSE - GIVE AN ERROR MESSAGE
            } else {
                alert('Invalid Phone Number');
            }
        }
    }

}


function forceMobileFormat(regionID, theField) {
    
    if (theField && theField.value && theField.valueOf != '') {

        if (theField.value.indexOf('+64') > -1) {
            //New Zealand - we'll just leave as is
        } else {


            //Remove anything but digits
            theField.value = theField.value.replace(/[^\d\.\+]/g, "");

            if (regionID < 9) {
                //AUSTRALIA

                //Remove +61
                theField.value = theField.value.replace("+61", "0");

                if (theField.value.length == 10 && theField.value.substr(0, 2) == "04") {
                    theField.value = theField.value.substr(0, 4) + " " + theField.value.substr(4, 3) + " " + theField.value.substr(7, 3);
                } else {
                    alert('Invalid Mobile Phone Number');
                }
            }
        }
    }
}

function areaCodeForRegion(regionID) {

    switch (regionID) {
        case 1:
            return "02";
        case 2:
            return "02";
        case 3:
            return "08";
        case 4:
            return "07";
        case 5:
            return "08";
        case 6:
            return "03";
        case 7:
            return "03";
        case 8:
            return "08";
    }
    return "";
}
