/**
 * collect and handle ajax errors thrown by the error: handler for jQuery::$.ajax(). Return a description of what went wrong
 *
 * @return void
 * @author Dan Bryant <db@leadingtone.org>
 * @copyright LearningChange LLC and Dan Bryant (Shared Source Initiave/Dual Ownership),  8 August, 2007
 * @package core.js
 * @version: 1.0
 * @perams:
 *	request: the request object
 *	estring: a decriptive string explaining the error
 **/
function ajax_error(request,estring,exception)
{
	var message = '';
	if(request) message += 'Request object: ' + request.status + ' :: ' + request.statusText + '; ';
	if(estring) message += 'Error string: ' + estring + '; ';
	if(exception) 
	{	message += 'Error object:';
		if(exception.name) message += 'Name: ' + exception.name + ', ';
		if(exception.description) message += 'Description: ' + exception.description + ', ';
		if(exception.message) message += 'Message: ' + exception.message + ', ';		
		message = erase_last(', ',message) + '; ';
	}
	return 'AjaxError: ' + erase_last('; ',message) + '.';
}

// analog of erase_last in format.php
function erase_last(kill,string)
{
	return string.substr(0,(string.length - kill.length));
}



// http://www.paulspages.co.uk/pcp/cookielib/
// http://javascript.about.com/library/blcookie1.htm
// http://www.sameshirteveryday.com/2007/06/02/javascript-cookies-library/
// EXAMPLES ... DO NOT DELETE THIS DOCUMANTATION!
// Cookies.set('rid','1',1);
// alert(Cookies.get('rid'));
// Cookies.erase('rid');
// // Create an alias for a cookie named 'a'  
// Cookies.alias('greeting', 'a', 0);  
// // Set the value  
// Cookies.greeting(parseInt(Cookies.greeting()) + 1);  
// // Display the value  
// alert(  
//     Cookies.greeting() + 'n' +  
//     Cookies.get('a') + 'n' +  
//     Cookies.get('greeting')  
// );
var Cookies = {  
    aliases: {},  
  
    alias: function(alias, name, defaultValue)  
    {  
        Cookies.aliases[alias] = name;  
  
        Cookies[alias] = function(value, days)  
        {  
            if(value == null)  
                return Cookies.get(name, defaultValue);  
            else  
                Cookies.set(name, value, days);  
        }  
    },  
  
    set: function(name, value, days)  
    {  
        name = Cookies.aliases[name] || name;  
  
        var expires = '';  
  
        if(!isNaN(days))  
        {  
            var date = new Date();  
            date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);  
            expires = "; expires=" + date.toGMTString();  
        }  
  
        document.cookie = name + "=" + escape(value) + expires + "; path=/";  
    },  
  
    get: function(name, defaultValue)  
    {  
        name = Cookies.aliases[name] || name;  
  
        var regex = new RegExp(name + "s*=s*(.*?)(;|$)");  
        var cookies = document.cookie.toString();  
        var match = cookies.match(regex);  
  
        if(match)  
            return unescape(match[1]);  
  
        return defaultValue;  
    },  
  
    erase: function(name)  
    {  
        Cookies.set(name, '', -1);  
    }  
}

// http://www.irt.org/script/368.htm
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function timeTillDate(whenDay,whenMonth,whenYear) {
    var now = new Date();
    var then = new Date(y2k(whenYear),whenMonth-1,whenDay);

    var difference = Date.UTC(y2k(then.getYear()),then.getMonth(),then.getDate(),0,0,0) -
                     Date.UTC(y2k(now.getYear()),now.getMonth(),now.getDate(),0,0,0);
    
    return difference/(1000*60*60*24);
}

// by Ultimater :: http://www.webdeveloper.com/forum/archive/index.php/t-83142.html
Date.prototype.getDaysInThisMonth=function(){
	var m=this.getMonth();
	var y=this.getFullYear();
	var isLeapYear = y%4 == 0;
	var ma = [31,28,31,30,31,30,31,31,30,31,30,31];
	if(isLeapYear) ma[1]++;
	return ma[m];
}

// by Ultimater :: http://www.webdeveloper.com/forum/archive/index.php/t-83142.html
Date.prototype.getUTCDaysInThisMonth=function(){
	var m=this.getUTCMonth();
	var y=this.getUTCFullYear();
	var isLeapYear = y%4 == 0;
	var ma = [31,28,31,30,31,30,31,31,30,31,30,31];
	if(isLeapYear) ma[1]++;
	return ma[m];
}

