﻿// Method to remove whitespace from a string.
function removeSpaces(string) {
    var tstring = "";

    string = '' + string;

    splitstring = string.split(" ");

    for (i = 0; i < splitstring.length; i++)
        tstring += splitstring[i];

    return tstring;
}


function tidyMobileNumber(number) {
    var tidyNumber = removeSpaces(number);

    // remove any plus symbols as we don't use them
    if (tidyNumber.substring(0, 1) == "+")
        tidyNumber = tidyNumber.substring(1);

    // Now check for starts with "0" and if so remove and replace with "44"
    if (tidyNumber.substring(0, 1) == "0")
        tidyNumber = "44" + tidyNumber.substring(1);

    return tidyNumber;
}

/*
This is used on the MyProfile.aspx page to get confirmation from the user that
they want to update their mobile number.
*/
function VerifyMobileChange(newMobId, currentNumber) {
    var el = document.getElementById(newMobId);

    if (el) {
        var newNumber = el.value;

        if (newNumber != currentNumber) {
            return confirm('Changing your mobile number will lock your account until the new\nnumber has been verified by SMS message. Do you want to continue?', 'Confirm Mobile Number change');
        }
        else
            return true;
    }
    else
        alert('Cannot find element ' + newMobId);
}

/*   
Determins the number of charcters remaining in the text box, given a limit
field = text box to measure. count = field containing the count, max = maximum number of charcters
*/
function CountLeft(field, count, max) {
    var textbox = document.getElementById(field);
    var counter = document.getElementById(count);

    // if the length of the string in the input field is greater than the max value, trim it 
    if (textbox.value.length > max) {
        textbox.value = textbox.value.substring(0, max);
        alert('You have exceded the maximum length of ' + max + ' characters');
    }
    else
        counter.innerHTML = max - textbox.value.length;
    // calculate the remaining characters  
}



var multiaddress;
var multipostcode;
var i;
var geocoder;
var timesin = 0;
var arrayResults;
var map;
var zoom = 13;


// Google maps loading
function load(addressid, postcodeid, latid, lngid) {
    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(-34.397, 150.644);

    var myOptions = {
        zoom: 13,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("myMap"),
            myOptions);

    var lat = "";
    var lng = "";

    var address = document.getElementById(addressid).value;
    var postcode = document.getElementById(postcodeid).value;

    if (latid != null) {
        lat = document.getElementById(latid).value;
        lng = document.getElementById(lngid).value;
    }

    multiaddress = address.split(",");
    multipostcode = postcode.split(",");

    zoom = multiaddress.length > 1 ? 6 : 13;

    if (lat != "" && lng != "") {
        var point = new google.maps.LatLng(lat, lng);

        map.setCenter(point, zoom);
        var marker = new new google.maps.Marker({
            position: point,
            map: map,
            title: address
        });

        var infowindow = new google.maps.InfoWindow({
            content: address
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

    }
    else {
        arrayResults = new Array(multiaddress.length);
        for (i = 0; i < multiaddress.length; i++) {

            geocoder.geocode({ 'address': multiaddress[i] }, tryresult);
        }

        map.setZoom(zoom);
    }
}


function tryresult(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

        map.setCenter(results[0].geometry.location);
        
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
            title: multiaddress[timesin]
        });

        var infowindow = new google.maps.InfoWindow({
            content: multiaddress[timesin]
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

        timesin++;
    } else {
        //alert("Geocode was not successful for the following reason: " + status);
    }
}

String.prototype.pad = function (l, s, t) {
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
}

function SetUpdateProgressPosition(controlId) {
    var div = document.getElementById(controlId);
    if (div == null || div == '' || typeof (div) != 'object') {
        return false;
    }

    var scrollTop = f_scrollTop();
    var scrollLeft = f_scrollLeft();
    var clientWidth = f_clientWidth();
    var clientHeight = f_clientHeight();

    // Calculate left position
    var left = (clientWidth - (div.offsetWidth - 35)) / 2;
    left = scrollLeft + left;

    // Set position
    div.style.position = 'absolute';
    div.style.left = left + 'px';
    div.style.top = scrollTop + (clientHeight / 2) + 'px';

    div.style.zindex = '1000';
}

function f_clientWidth() {
    return document.documentElement.clientWidth;
}

function f_clientHeight() {
    return document.documentElement.clientHeight;
}

function f_viewportWidth() {
    var test1 = document.documentElement.scrollWidth;
    var test2 = document.documentElement.offsetWidth;

    if (test1 > test2) // all but Explorer Mac
        return document.documentElement.scrollWidth;
    else
        return document.documentElement.offsetWidth;
}

function f_viewportHeight() {
    var test1 = document.documentElement.scrollHeight;
    var test2 = document.documentElement.offsetHeight;

    if (test1 > test2) // all but Explorer Mac
        return document.documentElement.scrollHeight;
    else
        return document.documentElement.offsetHeight;
}

function f_scrollLeft() {
    return f_filterResults(
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0);
}

function f_scrollTop() {
    return f_filterResults(
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0);
}

function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function MouseOver(obj) {
    if (obj) {
        var filename = obj.src;

        var extensionPos = filename.lastIndexOf('.');
        var extension = filename.substring(extensionPos);
        filename = filename.substring(0, extensionPos);

        filename += '-hover' + extension;
        obj.src = filename;
    }
}

function MouseOut(obj) {
    if (obj) {
        var filename = obj.src;

        var extensionPos = filename.lastIndexOf('.');
        var extension = filename.substring(extensionPos);
        filename = filename.substring(0, extensionPos - 6);
        filename += extension;

        obj.src = filename;
    }
}
