﻿
function ConfirmLogout() {

    if (confirm('Are you sure you want to log-out?')) {
        return true;
    } else {
        return false;
    }
    
}


function privacy(thetarget,targettitle)
{
    var url = 'http://' + document.domain + thetarget;
   
    if (window.open(url,targettitle,'resizable=1,scrollbars=1,width=850,height=600') == false){
        alert('Zubed failed to open a popup window. Please check your browser settings and try again');
    } 
    
    return false;

}

function drawCircle(map, lat, lng, color, width, fillColor, radius) {
            /*  Dev:    Google Groups
                Date:   2007-08-16
                Desc:   Draws a circles on the map at a set distance from the passed long lat.
                        Found on: http://www.reimers.dk/blogs/jacob_reimers_weblog/archive/2006/11/22/going-around-in-circles.aspx */
            
            var size = 3958.76 //Use 6371 for km
            var Clng = (radius/size)*(180/Math.PI)/Math.cos(lat*(Math.PI/180));
            var Cpoints = [];
            
            lng = parseFloat(lng);
            lat = parseFloat(lat);
            
            var swLat = 99999;
            var swLng = 99999;
            var neLat = -99999;
            var neLng = -99999;
            for (var i=0; i < 33; i++) {
                var theta = Math.PI * (i/16);
                Cx = lng + (Clng * Math.cos(theta));
                Cy = lat + ((radius/size)*(180/Math.PI) * Math.sin(theta));

                if(Cx < swLng){ swLng = Cx;}
                if(Cy < swLat){ swLat = Cy;}
                if(Cx > neLng){ neLng = Cx;}
                if(Cy > neLat){ neLat = Cy;}

                Cpoints.push(new GPoint(Cx,Cy));                
            };
  
  
            try{
                map.addOverlay(new GPolygon(Cpoints, color, width, 0.6, fillColor, 0.5));
            } catch(e){
                console.log(e);
            }

            var swLoc = new GLatLng(swLat,swLng);
            var neLoc = new GLatLng(neLat,neLng);
            var gBounds = new GLatLngBounds(swLoc,neLoc);

            return gBounds;
        }

 /*
       name - name of the cookie
       value - value of the cookie
       [expires] - expiration date of the cookie
         (defaults to end of current session)
       [path] - path for which the cookie is valid
         (defaults to path of calling document)
       [domain] - domain for which the cookie is valid
         (defaults to domain of calling document)
       [secure] - Boolean value indicating if the cookie transmission requires
         a secure transmission
       * an argument defaults when it is assigned null as a placeholder
       * a null placeholder is not required for trailing omitted arguments
    */
    function setCookie(name, value, expires, path, domain, secure) {
      var curCookie = name + "=" + escape(value) +
          ((expires) ? "; expires=" + expires.toGMTString() : "") +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          ((secure) ? "; secure" : "");
      //spitOut("setCookie: " + curCookie);
      document.cookie = curCookie;
    }


    /*
      name - name of the desired cookie
      return string containing value of specified cookie or null
      if cookie does not exist
    */
    function getCookie(name) {
      var dc = document.cookie;
      var prefix = name + "=";
      var begin = dc.indexOf("; " + prefix);
      if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
      } else
        begin += 2;
      var end = document.cookie.indexOf(";", begin);
      if (end == -1)
        end = dc.length;
      return unescape(dc.substring(begin + prefix.length, end));
    }


    /*
       name - name of the cookie
       [path] - path of the cookie (must be same as path used to create cookie)
       [domain] - domain of the cookie (must be same as domain used to
         create cookie)
       path and domain default if assigned null or omitted if no explicit
         argument proceeds
    */
    function deleteCookie(name, path, domain) {
      if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    }
    
    function doBoxOpen(sSectionName, pairedSection)
{
    // Dev:     James Pearson
    // Date:    2008-04-01
    // Desc:    Replaces doToggleSection. Opens a side box from its closed state.

    if ($(sSectionName)) {
    
        $(sSectionName).style.display = 'block';
        
         var oCollapse = $(sSectionName + '-collapse');
        oCollapse.onclick = null;
        
        var oContent = $(sSectionName + '-content');        
        oContent.style.display = 'block';
        
        //new Effect.BlindDown(oContent.id,{scaleTo:100, duration: 0.2, scaleMode: 'contents'});
        
       
        oCollapse.onclick = function(){ 
                doBoxClose(sSectionName); 
        }
        
        if (pairedSection != ''){
            doBoxClose(pairedSection);
        }
    }
    
    return false;
}

function doBoxClose(sSectionName, pairedSection)
{
    // Dev:     James Pearson
    // Date:    2008-04-01
    // Desc:    Replaces doToggleSection. Closes a side box from its opened state.

    if ($(sSectionName)) {
        var oCollapse = $(sSectionName + '-collapse');
        oCollapse.onclick = null;
        
        var oContent = $(sSectionName + '-content');
         oContent.style.display = 'none';

        //new Effect.BlindUp(oContent.id,{scaleTo:0, duration: 0.2, scaleMode: 'contents'});
        
        oCollapse.onclick = function(){ 
            doBoxOpen(sSectionName); 
        }
        
        if (pairedSection != ''){
            doBoxOpen(pairedSection);
        }
    }
    
    return false;
}
function doBoxHide(sSectionName) {
    if ($(sSectionName)) {
        $(sSectionName).style.display = 'none';
    }
}

function roundNumber(rnum, rlength) { 
    // Arguments: number to round, number of decimal places
    var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
    return newnumber; 
}

// Processes an ISO date in to a javascript date.
// Found on: http://delete.me.uk/2005/03/iso8601.html
Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));
    
    if (d[1]) {

        var offset = 0;
        var date = new Date(d[1], 0, 1);

        if (d[3]) { date.setMonth(d[3] - 1); }
        if (d[5]) { date.setDate(d[5]); }
        if (d[7]) { date.setHours(d[7]); }
        if (d[8]) { date.setMinutes(d[8]); }
        if (d[10]) { date.setSeconds(d[10]); }
        if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
        if (d[14]) {
            offset = (Number(d[16]) * 60) + Number(d[17]);
            offset *= ((d[15] == '-') ? 1 : -1);
        }

        offset -= date.getTimezoneOffset();
        time = (Number(date) + (offset * 60 * 1000));
        this.setTime(Number(time));
    }
}


Date.prototype.toDDMMYYYY = function() {

    var curr_date = (this.getDate() < 10 ? '0' : '') + this.getDate();
    var curr_month = this.getMonth();
    curr_month++;   // cater for javascript zero based month
    curr_month = (curr_month < 10 ? '0' : '') + curr_month;
    var curr_year = this.getFullYear();
    return curr_date + "/" + curr_month + "/" + curr_year;
}
