    var map = null;
    var pinID = 0;
    var postcode = "SK6 3AH";
    var zoomLevel = 17;
    var postcodeField = "";
    var latField = "";
    var lonField = "";
    var xmlDoc;


    function initLookup(elementString) {
        cp_set_config("access_token", "34f35-fb321-aa25e-0005b");
        cp_set_config("elements", elementString);
        cp_set_config("err_msg1", "Postcode could not be found. Please call us on 0800 0699 243 for further help");
        cp_set_config("err_msg2", "Postcode could not be found. Please call us on 0800 0699 243 for further help");
    }

    function pendingCount(oForm, ss) {
        var aElems = oForm.elements;
        var prepayCount = 0;
        var voucherCount = 0;

        for (var iI = 0; iI < aElems.length; iI++) {
            if (aElems[iI].type == 'checkbox') {
                if (aElems[iI].name.toString().match("chkIncludePrepay") == "chkIncludePrepay") {
                    if (aElems[iI].checked)
                        prepayCount++;
                }
                if (aElems[iI].name.toString().match("chkIncludeVoucher") == "chkIncludeVoucher") {
                    if (aElems[iI].checked)
                        voucherCount++;
                }
            }
        }
        if (prepayCount == 0 && voucherCount == 0) {
            for (var iI = 0; iI < aElems.length; iI++) {
                if (aElems[iI].type == 'hidden') {
                    if (aElems[iI].name.toString().match("hidPrepayCount") == "hidPrepayCount") {
                        prepayCount = aElems[iI].value;
                    }
                    if (aElems[iI].name.toString().match("hidVoucherCount") == "hidVoucherCount") {
                        voucherCount = aElems[iI].value;
                    }
                }
            }
        }

        var appendString = "";
        if (ss.toString() == "1")
            appendString = "\n\nThere are requests on this invoice from employees who do not have a signed salary amendment letter marked on their account";
            
        if (prepayCount == 0 && voucherCount == 0)
            return "You are about to approve all highlighted requests. Are you sure?" + appendString;
            
        return "You are about to request an invoice for " + voucherCount.toString() + " vouchers and " + prepayCount.toString() + " prepayments. Are you sure?" + appendString;
        
    }

    function previewTemplates() {
        window.open(unescape("/management/company/companyEmailList.aspx"), 'previewWin', 'width=550,height=700,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }

    function previewPending(companyId) {
        window.open(unescape("/management/company/pendingList.aspx?companyId=" + companyId), 'previewWin', 'width=550,height=700,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }

    function previewCreditNote(creditNoteId, companyId) {
        window.open(unescape("/management/tools/creditNotePreview.aspx?creditNoteId=" + creditNoteId + "&companyId=" + companyId), 'previewWin', 'width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }    
    
    function previewInvoice(invoiceId, companyId)
    {
        window.open(unescape("/management/tools/invoicePreview.aspx?invoiceNumber=" + invoiceId + "&companyId=" + companyId),'previewWin','width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');   
    }

    function previewInvoiceRegen(invoiceId, companyId)
    {
        window.open(unescape("/management/tools/invoicePreview.aspx?regen=true&invoiceNumber=" + invoiceId + "&companyId=" + companyId),'previewWin','width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');   
    }

    function previewPayroll(invoiceId, companyId)
    {
        window.open(unescape("/management/tools/payrollPreview.aspx?invoiceNumber=" + invoiceId + "&companyId=" + companyId), 'previewWin','width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }

    function previewPayrollRegen(invoiceId, companyId)
    {
        window.open(unescape("/management/tools/payrollPreview.aspx?regen=true&invoiceNumber=" + invoiceId + "&companyId=" + companyId),'previewWin','width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }

    function previewPreInvoicePayrollRegen(companyId)
    {
        window.open(unescape("/management/tools/payrollPreview.aspx?regen=true&companyId=" + companyId), 'previewWin', 'width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');
    }

    function resetFormElements(oForm, sType)
    {
	    var aElems = oForm.elements;

	    for (var iI=0;iI<aElems.length;iI++)
	    {
		    if (aElems[iI].type == sType)
			    aElems[iI].checked = false;
	    }
    }
	
	function clearData(thisBox, value)
	{
	    if(thisBox.value ==(value))
	    {
            thisBox.value = ("");	    
	    }

	}
	
	function resetData(thisBox, value)
	{
		if(thisBox.value == (""))
	    {
            thisBox.value = value;
	    }
	}
    
    function previewVoucher(voucherId, companyId)
    {
        window.open(unescape("/management/tools/voucherPreview.aspx?voucherId=" + voucherId + "&companyId=" + companyId),'previewWin','width=950,height=600,left=0,top=0,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=No,fullscreen=No');   
    }

    function FTGetRoute(routeStart, routeEnd)
    {  
        map = new VEMap('myMap');
        map.LoadMap();      
        map.GetRoute(routeStart,routeEnd,null,null,onGotRoute);
    }
    
    function FTLoadXML()
    {
        xmlDocLoc = document.getElementById('mapXML').innerHTML;
        if (window.ActiveXObject)
        {
            var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(xmlDocLoc);
        
        } else {
        
        var parser=new DOMParser();
        var xmlDoc=parser.parseFromString(xmlDocLoc,"text/xml");
        
        }
        return(xmlDoc);
    }
     
    function FTGetMap()
    {
        var xmlDoc = FTLoadXML();
        var locs = new Array; 
        
        map = new VEMap('myMap'); map.LoadMap();
               
        var tracking = xmlDoc.documentElement;
                       
        for (var iNode = 0; iNode < tracking.childNodes.length; iNode++)
        {
               var thisPID = tracking.childNodes[iNode].childNodes[0].childNodes[0].nodeValue;
               var thisNme = tracking.childNodes[iNode].childNodes[1].childNodes[0].nodeValue;
               var thisLat = tracking.childNodes[iNode].childNodes[2].childNodes[0].nodeValue;
               var thisLon = tracking.childNodes[iNode].childNodes[3].childNodes[0].nodeValue;
               var thisAdd = tracking.childNodes[iNode].childNodes[5].childNodes[0].nodeValue;
               var thisLoc = new VELatLong(thisLat,thisLon);
               
               locs.push(thisLoc);
               
               var pin = new VEPushpin(thisPID, thisLoc, null, thisNme, thisAdd);
               map.AddPushpin(pin);              
        }            
            map.SetMapView(locs);
    }
    
    function zoomToPin(lat,lon)
    {
        var loc = new VELatLong(lat, lon);
        map.SetCenterAndZoom(loc, 16);
    }
    

//------------------------------------------------------------------------------------------------------------------------
// Form Map Controls -----------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------

    function GetMap(postcodeSource,latDest,lonDest)
    
    // This function takes 3 input IDs, so we know which input boxes on the screen to address.
    
    {  
        try
        {   
            map = new VEMap('myMap');
            
            // Initialise global input IDs
            
            postcodeField = document.getElementById(postcodeSource);
            latField = document.getElementById(latDest);
            lonField = document.getElementById(lonDest);
            
            if(postcodeField.value.length > 0)
            {
                postcode = postcodeField.value;
            } else {
                zoomLevel = 5;
            }
            
            map.Find('', postcode, 1, zoomMap);
            map.LoadMap();
            map.HideDashboard();
            map.AttachEvent("onclick", mapClick);
        }
        catch(err)
        {

        }
    }

    function updateMapFromPostcode()
    {
        if(postcodeField.value.length > 0)
        {
            zoomLevel = 17;
            postcode = postcodeField.value;
            map.Find('', postcode, 1, markNewLocation);
        }
    }
    
    function markNewLocation()
    {
        latField.value = map.GetCenter().Latitude;
        lonField.value = map.GetCenter().Longitude;
        zoomMap();
    }
    
    function zoomMap()
    {
        map.SetZoomLevel(zoomLevel);
        map.DeleteAllPushpins(); 
        AddPin(lonField.value, latField.value, '', '')       
    }
    
     function mapClick(e)
     {
        lonField.value = e.view.LatLong.Latitude;
        latField.value = e.view.LatLong.Longitude;
        map.SetCenter(e.view.LatLong);
        map.DeleteAllPushpins();      
        AddPin(e.view.LatLong.Longitude, e.view.LatLong.Latitude, '', '')
     }
     
//------------------------------------------------------------------------------------------------------------------------
// End Form Map Controls -------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------
   
    function loadPoiArray()
    {

    }

    function AddPin(thisLat, thisLong, thisTitle, thisDesc)
    {   
        var pin = new VEPushpin(pinID, new VELatLong(thisLong, thisLat), null, thisTitle, thisDesc);
        map.AddPushpin(pin);
        pinID++;
    }  

    function onGotRoute(route)
    {
        map.ZoomOut(3);
        var routeinfo="";
        var bg = 0;
        //routeinfo += "<p>Total distance: ";
        //routeinfo += route.Itinerary.Distance+" ";
        //routeinfo += route.Itinerary.DistanceUnit+"\n";
        
        routeinfo += "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" style=\"color:Black;background-color:White;width:355px;border-collapse:collapse;\">";

        var len = route.Itinerary.Segments.length;
        for(var i = 0; i<len ;i++)
        {
            routeinfo += "<tr><td style=\"background-color:";
            if ( bg == 0 ) { routeinfo += "#FFFFFF"; } else {routeinfo += "#F0F0F0"; }
            routeinfo += "\"><p>";
            if ( i == 0 || i == len - 1 ) { routeinfo += "<strong>"; } 
            routeinfo += route.Itinerary.Segments[i].Instruction+" ";
            if ( i == 0 || i == len - 1 ) { routeinfo += "</strong>"; } 
            routeinfo += "</p></td><td style=\"background-color:";
            if ( bg == 0 ) { routeinfo += "#FFFFFF"; } else {routeinfo += "#EEEEEE"; }
            routeinfo += "\"><p>";
            routeinfo += route.Itinerary.Segments[i].Distance;           
            routeinfo += " " + route.Itinerary.DistanceUnit;
            routeinfo += "</p></td></tr>";
            bg = bg + 1;
            if ( bg == 2 ){ bg = 0; }
        }
        
        routeinfo += "<table>";
        
        if(document.getElementById("routeDirections")){
        document.getElementById("routeDirections").innerHTML = routeinfo;
        }
    }   
