//funzione che dato il nome di un parametro lo va a cercare nell'URL e se presente ne restituisce il valore
function gup( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?]"+name+"=([^#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	  return "";
	else
	  return results[1];
}

function calcPercentageProgressBar(actualMiles, threshold) {
	return Math.round((actualMiles*100) / threshold);
}

function updateHeaderMiles(value) {
	document.getElementById("headerMiles").innerHTML = value;
}

function updateHeaderCoins(value) {
	document.getElementById("headerCoins").innerHTML = value;
}

function getWinnersWeek() {
	var curr = new Date; // get current date
	var curr2 = new Date; // get current date
	var first = curr.getDate() - curr.getDay()-6; // First day is the day of the month - the day of the week
	var last = curr2.getDate() - curr2.getDay(); // last day is the first day + 6
	
	var firstday = new Date(curr.setDate(first));
	var lastday = new Date(curr2.setDate(last));
	
	var dateToReturn = getMonthShortLabel(firstday.getMonth())+" "+firstday.getDate()+getNumberSuffix(firstday.getDate())+" - "+getMonthShortLabel(lastday.getMonth())+" "+lastday.getDate()+getNumberSuffix(lastday.getDate())+" Winners";
	return dateToReturn;				
}

function getNumberSuffix(number) {
	return (number > 3)?'th':(number==1)?'st':(number==2)?'nd':'rd';
}

function getWinnersMonth() {
	var curr = new Date; // get current date
	var last = curr.getMonth()-1;
	
	var lastMonth = new Date(curr.setMonth(last));
	
	var dateToReturn = getMonthLongLabel(lastMonth.getMonth())+" "+lastMonth.getFullYear()+" Winners";
	return dateToReturn;				
}

function getPrizeString(prize) {
	var value_of_prize = prize.split("_")[1];
	var name_of_prize = prize.split("_")[0];
	
	return "$"+value_of_prize+" "+name_of_prize;
}

function getHoursAMPM(hour, minutes) {
	var ap = "AM";
	if (hour   > 11) { ap = "PM";        }
	if (hour   > 12) { hour = hour - 12; }
	if (hour   == 0) { hour = 12;        }
	
	return hour+":"+minutes+ap;
}

function getDayInWeekShortLabel(num) {
	var dayNames = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ];

	return dayNames[num];
}

function getMonthShortLabel(num) {
	var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
								   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

	return monthNames[num];
}

function getMonthLongLabel(num) {
	var monthNames = [ "January", "February", "March", "April", "May", "June",
								   "July", "August", "September", "October", "November", "December" ];

	return monthNames[num];
}
				
// funzioni per la gestione del cookie
function getCookie(cookieName) {
	cookie_array = document.cookie.split ("; ");
	for (x=0; x < cookie_array.length; x++) {
		cookieParts_array = cookie_array[x].split("=");
		if (cookieParts_array[0] == cookieName) {
			return cookieParts_array[1];

			// alert('Cookie ' + cookieName + ' trovato');
		}
	}

	// alert('Nessun Cookie di nome ' + cookieName + ' trovato');

	return '';
}

function setCookie(cookieName, cookieValue) {

	document.cookie=  cookieName+"="+cookieValue;

	// alert('Cookie ' + cookieName + ' settato a ' + cookieValue);
}
