﻿/*
Important: Any bug fixes in this file must be duplicated in the ChannelPartners file of the same name 
as it is a direct copy of this.
*/

/* Switches images on the fly. Used to change buttons from there normal image to their hover image and back again. */
function SwitchImage(controlIdToSwitch, imagePath) {
    var obj = document.getElementById(controlIdToSwitch);
    if (obj != null) {
        obj.src = imagePath;
    }
}

/* Add javascript to controlIdOfMessage onKeyUp and onKeyDown events */
function UpdateMessageLength(controlIdOfMessage, controlIdToUpdate) {
    var message = document.getElementById(controlIdOfMessage);
    if (message != null) {
        var label = document.getElementById(controlIdToUpdate);
        if (label != null) {
            if (message.value.length > 160) {
                message.value = Left(message.value, 160);
            }
            else {
                label.value = (160 - message.value.length) + " character(s) left.";
            }
        }
    }
}

/* Calculates and displays the number of characters in the message and number of messages chunks that will be required to send it. */
function UpdateMessageLengthAndNumSms(controlIdOfMessage, controlIdToUpdate) {

    var label = document.getElementById(controlIdToUpdate);
    if (label != null) {
        var numMessages = CalculateNumberOfMessages(controlIdOfMessage);
        var message = document.getElementById(controlIdOfMessage).value.replace(/^\s+|\s+$/g, '');
        if (message != null) {
            var numChars = message.length;

            if (numMessages == 0) {
                label.innerHTML = "0 characters and 0 text messages.";
            }
            else {
                if (numChars == 1) {
                    label.innerHTML = "1 character and 1 text message";
                }
                else if (numMessages == 1) {
                    label.innerHTML = message.length + " characters and 1 text message";
                }
                else {
                    label.innerHTML = message.length + " characters and " + numMessages + " text messages";
                }
            }
        }
    }
    return numMessages;
}

/* Calculates the number of message chunks in the message. */
function CalculateNumberOfMessages(controlIdOfMessage) {
    var message = document.getElementById(controlIdOfMessage);
    if (message != null) {
        var numChars = message.value.replace(/^\s+|\s+$/g, '').length;
        var numMessages = 0;

        if (message != null && numChars > 0) {
            if (message.value.length > 960) {
                message.value = Left(message.value, 960);
            }

            if (message.value.length == 0) {
                return 0;
            }
            else {
                numMessages = parseInt(((numChars) / 161) + 1)
                return numMessages;
            }
        }
    }

    return 0;
}

/* This function displays the number of characters and messages found and works out and displays how many credits will be required on the QuickBroadcast page.
If a bug is found in the below algorithm you will also need to update Informatel.ReadyToSms.Website.Controls.QuickBroadcaster.ParseNumbers  as this code was taken and converted from there.*/
function CalculateAndDisplayTotals(controlIdOfMobiles, controlIdOfMessage, control1IdToUpdate, control2IdToUpdate) {

    var numMessages = UpdateMessageLengthAndNumSms(controlIdOfMessage, control1IdToUpdate);
    if (numMessages >= 0) {
        var numMobiles = CalculateNumberOfMobiles(controlIdOfMobiles);
        var label = document.getElementById(control2IdToUpdate);
        /* Display the results. */
        var numToSend = numMessages * numMobiles;
        if (numToSend == 1) {
            label.innerHTML = "1 ReadyCredit required (1 mobile number * 1 text message)";
        }
        else if (numMessages == 1) {
            label.innerHTML = numToSend + " ReadyCredits required (" + numMobiles + " mobile numbers * 1 text message)";
        }
        else {
            label.innerHTML = numToSend + " ReadyCredits required (" + numMobiles + " mobile numbers * " + numMessages + " text messages)";
        }
    }
}

/* Counts the number of mobiles being sent to. */
function CalculateNumberOfMobiles(controlIdOfMobiles) {
    /* Initialize variables. */

    /* Calculate and display number of messages. */

    var currentStart = 0;
    var currentLen = 0;
    var numMobiles = 0;
    var mobileInput = document.getElementById(controlIdOfMobiles);

    /* Replace other delimiters with just a comma and removes any spaces, carriage returns, semi-colons and parentheses using regular expressions inside the replace functions.*/
    var rawNumbers = new String(mobileInput.value).replace(/ /g, "");
    rawNumbers = rawNumbers.replace(/\r\n/g, ",");
    rawNumbers = rawNumbers.replace(/\n/g, ",");/*Firefox needs \n where IE needs both \r\n*/
    rawNumbers = rawNumbers.replace(/;/g, ",");
    rawNumbers = rawNumbers.replace(/\(/g, "");
    rawNumbers = rawNumbers.replace(/\)/g, "");
    rawNumbers = rawNumbers.replace(/-/g, "");

    /* Loop through the string and look for any commas indicating the end of each number. 
    Count time this occurs and once at the end of the string if there is no delimiter there. */
    for (var i = 0; i < rawNumbers.length; i++) {
        if (rawNumbers.substring(i, i + 1) == ",")//delimter found.
        {
            if (currentLen > 3)//ignore anything less than 3 chars.
            {
                numMobiles++;               
            }
            currentStart = i + 1;
            currentLen = 0;
        }
        else if (i == rawNumbers.length - 1)//end of list with no delimiter.
        {
            if (currentLen > 3)//ignore anything less than 3 chars.
            {
                numMobiles++;
            }
            break;
        }
        else
        { currentLen++; }
        
    }

    return numMobiles;
}

/* Displays an alert with the number of mobiles, number of messages and number of ReadyCredits before scheduling the broadcast. Allows the customer to cancel the submission. */
function ShowSubmitQuickBroadcastConfirmationAlert(controlIdOfMessage, controlIdOfMobiles) {

    var numMessages = CalculateNumberOfMessages(controlIdOfMessage);
    var numMobiles = CalculateNumberOfMobiles(controlIdOfMobiles);
    var numCredits = numMessages * numMobiles;
    var reply;
    if (numMessages == 1 && numMobiles == 1) {
        reply = confirm('You are about to schedule 1 message for 1 phone number. 1 ReadyCredit will be deducted from your account. Continue?');
    }
    else if (numMessages == 1 && numMessages > 1) {
        reply = confirm('You are about to schedule 1 message for ' + numMobiles + ' phone numbers. ' + numCredits + ' ReadyCredits will be deducted from your account. Continue?');
    }
    else if (numMessages > 1 && numMobiles == 1) {
        reply = confirm('You are about to schedule ' + numMessages + ' messages for 1 phone number. ' + numCredits + ' ReadyCredits will be deducted from your account. Continue?');
    }
    else {
        reply = confirm('You are about to schedule ' + numMessages + ' messages for ' + numMobiles + ' phone numbers. ' + numCredits + ' ReadyCredits will be deducted from your account. Continue?');
    }
    return reply;
}

/* Asks the user for confirmation that they want to navigate away from the File Broadcast pages. Doing so will clear any broadcast they are part way through setting up. */
function CheckAndConfirmAppClosing(url) {
    if (document.title == "Web SMS | Online SMS management | Setup and send bulk sms from PC") {
        var r = confirm("You are about to leave the Broadcast Application. You will lose your changes if you proceed.");
        if (r == true) {
            window.location = url;
        }
    }
    else {
        window.location = url;
    }
}

/*  Confirms restarting the File Broadcast application.  Doing so will clear any broadcast they are part way through setting up. */
function CheckAndConfirmRestart() {
    var r = confirm("You are about to restart. You will lose your changes if you proceed. Restart?");
    if (r == true) {
        window.location = "Step1DataImport.aspx";
    }
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}


function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function ShowResetConfirmationAlert() {
    var reply = confirm('You will lose your current broadcast if you continue. Is this ok?');
    return reply;
}

function WarnUser() {
    var reply = confirm('You are about to navigate away from this page. Any changes you have made will be lost. Continue?');
    return reply;
}

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

/*function DetectIE6AndBelow
{
var browser = navigator.appName;
var b_version = navigator.appVersion;
var version = parseFloat(b_version);
if (browser == "Microsoft Internet Explorer" && version < 7)
{
return true;
}
else{return false;}
}


function ValidatePhone(phoneNumber)
{
if (!IsNumeric(phone))
{
return false;
}
    
/*check if 9 digits then starts with 4 (Excel can strip zeros and we will ad +61 later anyway)
if (phone.length == 9 && Left(phone(1) != "4")
{
return false; 
}
     
/*check for correct length
if (phone.length != 10 && phone.length != 9) { return false; }

if (Left(phone, 2) != "04" && phone.Length == 10) { return false; }/*check starts with 04 if it is 10 digits long

return true;
}
*/