function createcookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function getcookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {c = c.substring(1,c.length);}
        if (c.indexOf(nameEQ) == 0) {return c.substring(nameEQ.length,c.length);}
    }
    return null;
}

function eraseCookie(name) {
document.cookie = name + "="+";" + "-1; path=/";
}

// export all the functions in the Public 'package'
Public = {};

(function(){ // setup a closure to hide our local stuff

Public.showhide = function (theid) {
    setid = document.getElementById(theid);
    if (setid.style.display == 'none') {
        setid.style.display = 'block';
    } else {
        setid.style.display = 'none';
    }
};

var progressIndicatorCounter = 0;

Public.progressIndicator = {
    show: function( ) {
        progressIndicatorCounter++;
        var pi=dojo.byId('loading-map');
	if ( pi ) {
          pi.style.display='block';       
        }
    },
    hide: function() {
        progressIndicatorCounter--;
        if (  progressIndicatorCounter <= 0 ) {
            progressIndicatorCounter = 0;
            dojo.byId('loading-map').style.display='none';
        }
    },
    hideAll: function() {
        progressIndicatorCounter = 0;
        dojo.byId('loading-map').style.display='none';
    }
};

// store the connect('hide')
var hideConnect = null;

function disconnectHide() {
    if ( hideConnect ) {
        try {
            dojo.disconnect( hideConnect );
        } catch ( e) {
            // catch the exception generated when the dialog already gone and we have a bad reference
        }
        hideConnect = null;
    }
    if ( dijit && dijit.byId ) {
        var dlg = dijit.byId('dlgTOS');
        if ( dlg.domNode.style.display != 'none' ) {
            dlg.hide();
        }
    }
}

Public.firstTimeTOS = function() {
    var txtTitle = 'First-Time User Notice';
    var firstdialog = new dijit.Dialog({ title: txtTitle, href: "/web/termsOfUseFirstTime.do", style:'width:400px', id:'dlgTOS' });
    dojo.body().appendChild(firstdialog.domNode);
    firstdialog.startup();
    firstdialog.show();
    firstdialog.closeButtonNode.style.display='none';
    // attach to 'hide' to capture the user hitting ESC
    hideConnect = firstdialog.connect( firstdialog, "_onKey", Public.userAcceptTOS );
}

Public.hideTOSLogout = function(){
    window.location = '/web/logout.do';
}

Public.userAcceptTOS = function(evt) {
    disconnectHide();
    if(!evt || evt.target.value !='yes') {
        var txtTitle = 'Terms Rejected';
        var thisdialog = new dijit.Dialog({ title: txtTitle, href: "/web/termsOfUseReject.do", style:'width:400px' });
        dojo.body().appendChild(thisdialog.domNode);
        thisdialog.startup();
        thisdialog.show();
        thisdialog.closeButtonNode.style.display='none';
        thisdialog.connect( thisdialog, "_onKey", Public.hideTOSLogout );
        dojo.xhrPost( {
        	url : '/web/logout.do'
    	});        
    } else {
        dojo.xhrPost( {
        	url : '/web/ajax/userAcceptTermsAction.do'
    	});        
    }
}

Public.userAcceptTOSFromLogin = function() {
    dojo.xhrPost( {
    	url : '/web/ajax/userAcceptTermsAction.do'    	
	});            
}




var x = -1; // next listing to show
var visibleMax = 2; // how many listings to have visible at once

function cycle() {
    var holder = document.getElementById('listexamples');
    if ( !holder ) {
        return;
    }
    window.setTimeout(cycle, 6000);
    if ( x == -1 ) {
        x = 0;
        return;
    }
    var pees = holder.getElementsByTagName('p');
    for (var y = 0; y < pees.length; y++) {
        pees[y].style.display = 'none';
    }
    for( var v=0;v<visibleMax;v++) {
        if (++x >= pees.length) {
            x = 0;
        }
        if ( pees[x] ) {
            pees[x].style.display = 'block';
        }
    }
}

if ( typeof dojo != "undefined" ) {
    dojo.addOnLoad( function() {
        cycle();
        if ( dijit && dijit.byId && dijit.byId('dialogTOS')) {
            var tos = dijit.byId('dialogTOS');
            tos.show();
        }
    } );

}
})();
