﻿// 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 timesin =0;
    var arrayResults;
    var localSearch = new GlocalSearch();
    var map ;
// Google maps loading
    function load(addressid, postcodeid, latid, lngid) 
    {
        if (GBrowserIsCompatible()) 
        {
            map = new GMap2(document.getElementById("myMap"));
            map.addControl(new GSmallMapControl());
            var lat = "";
			var lng = "";
            var geocoder = new GClientGeocoder();
            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(",");
            
            GEvent.addListener(map,"click", function(overlay,latlng) 
            {
                if (latlng) 
                {
                    
                }
                if (overlay) 
                {
                    if (overlay instanceof GMarker) 
                    { 
                        var type = typeof overlay;
                        var place;
                        for(y = 0; y < arrayResults.length; y++)
                        {
                            var temp = arrayResults[y];
                            var splittest = temp.split("|");
                            if(overlay.getPoint() == splittest[0])
                                place = splittest[1];
                        }
                        var myHtml = place;
                       
                        map.openInfoWindow(overlay.getPoint(), myHtml);
                    }
                }
            });
            if (lat != "" && lng != "") {
                var point = new GLatLng(lat, lng);

                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
            }
            else {
                arrayResults = new Array(multiaddress.length);
                for (i = multiaddress.length-1; i >= 0; i--) {
                    geocoder.getLatLng(
                    multiaddress[i], TryPoint1)
                }
            }         
        }
    }

    function TryPoint1(point) {
        if (!point) 
        {
            usePointFromPostcode(multipostcode[timesin], placePoint)
            arrayResults[timesin] = multipostcode[timesin];
            timesin++;           
        }
        else {
            map.setCenter(point, 13);
            var marker = new GMarker(point);
            map.addOverlay(marker);

            arrayResults[timesin] = point + "|" + multiaddress[timesin];
            timesin++;

        }
    }

    function placePoint(point) 
    {
       
        
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
    }

    function usePointFromPostcode(postcode, callbackFunction) 
    {

        localSearch.setSearchCompleteCallback(null,
        function() 
        {
            if (localSearch.results[0]) 
            {
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = new GLatLng(resultLat, resultLng);
                callbackFunction(point);
            } 
            else 
            {
                alert("Postcode not found!");
            }
        });
       
        localSearch.execute(postcode + ", UK");
    }
    
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;
}
