function getDate(sql) {
    var date = (/(\d+).*?(\d+).*?(\d+)/).exec(sql);
    return new Date(date[1], date[2]-1, date[3]);
}

function getSQLDate(date) {
    return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
}

function gethelp (elem, text) {
	$(elem).set('src', '/css/images/icons/helpon.png');

	var pos  = $(elem).getPosition();
	var xpos = pos.x-250;
	var ypos = pos.y+20;
	
	var helpdiv = new Element('div', {
		'id': 'helpdiv',
		'style': 'top: ' + ypos + 'px; left: ' + xpos + 'px;',
		'class': 'helpdiv'
	});
	
	var helpdiv_top = new Element('img', {
		'src': '/css/images/home/help-top.png'
	});
	helpdiv_top.inject(helpdiv);
	
	var help_bg = new Element('div', {
		'class': 'help-bg'
	});
	help_bg.inject(helpdiv);
	
	var helptext = new Element('p', {
		'text': text,
		'class': 'helptext'
	});
	helptext.inject(help_bg);
	
	var helpdiv_bottom = new Element('img', {
		'src': '/css/images/home/help-bottom.png',
		'style': 'width: 250px; height: 10px;'
	});
	helpdiv_bottom.inject(helpdiv);
	
	helpdiv.inject($(document.body));
	
	$(elem).addEvent('mouseout', function(e) {
		this.set('src', '/css/images/icons/helpout.png');
		if ($('helpdiv')) { $('helpdiv').dispose(); }
	});

}


function pad(n, totalDigits) {
        n = n.toString(); 
        var pd = ''; 
        if (totalDigits > n.length) 
        { 
            for (i=0; i < (totalDigits-n.length); i++) 
            { 
                pd += '0'; 
            } 
        } 
        return pd + n.toString();
}

function SQLToMinutes(duration) {
    duration = (/^(\d+).*?(\d+)/).exec(duration);
    return parseInt(duration[1], 10) * 60 + parseInt(duration[2], 10);
}

function getKcal(weight, minutes, factor) {
    return parseInt(((factor * 3.5 * weight) / 200) * minutes);
}

function changeWeek(sql, next) {
    var date = getDate(sql);
    if (next) date = date.nextWeek();
    else date = date.prevWeek();
    return getSQLDate(date);
}

function formatDate(sql) {
    var date = getDate(sql);
    return pad(date.getDate())+'/'+pad((date.getMonth()+1))+'/'+new String(date.getFullYear()).substring(2);
}

function updateBMI (prefix) {
    var weight = $(prefix + '_weight');
    weight = parseInt(weight.value);
    var height = parseInt($(prefix + '_height').value);
    if (isNaN(weight) || isNaN(height)) return;
    height /= 100;
    $(prefix + '_bmi').set('html', (weight / (height * height)).toFixed(1));
}

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
}

Date.prototype.prevDay = function() {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate()-1);
}

Date.prototype.nextDay = function() {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate()+1);
}

Date.prototype.prevWeek = function() {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate()-7);
}

Date.prototype.nextWeek = function() {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate()+7);
}


Array.prototype.inArray = function (element) {
          for (var i = 0; i < this.length; i++) {
              if (this[i] == element) {
                      return true;
              }
          }
          return false;
};


function ucfirst( str ) {
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1, str.length-1);
}


function isEditing () {
	var yes = 0;

	// Check if user is editing some row already
	$each($(document.body).getElements('img.editbutton'), function(elem) {
		if (!elem.hasClass('hover')) {

			$('stickywin-text').set('html', 'Please finish editing first. Press the Enter key on your keyboard in the field you are editing.');
			new StickyWinModal({
	  			content: $('stickywin').get('html'),
	  			fadeDuration: 500
			});

		 	yes = 1;
			return;
		}
	});
	
	if (yes == 1) {
		return true;
  	} else {
		return false;
	}
}
