var date = '';
var noOfSmokies = 0;
var noOfSalmon = 0;
var price = 10.00;

function buildCal(m, y, cM, cH, cDW, cD, brdr){
	
	var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

	var todaydate=new Date() //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	
	var t='<div class="'+cM+'"><table class="'+cM+'" cols="7" cellpadding="0" border="'+brdr+'" cellspacing="0"><tr align="center">';
	t+='<td colspan="7" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td></tr><tr align="center">';
	for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
	t+='</tr><tr align="center">';
	
	for(i=1;i<=42;i++)
	{
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		if (x==scanfortoday)
		{
			temp = '<span id="today">';
			if (DayOfWeek(x, m, y) == 3)
			{
				temp += '<span id="wed" style="cursor:pointer;text-descoration:underline;" onclick="setDate(\'' + x + '/' + m + '/' + y + '\'); document.getElementById(\'displayDate\').value = \'Delivery Date' + x + '/' + m + '/' + y + '\'">' + x + '</span>';
			}
			else
			{
				temp += x;
			}
			temp += '</span>';
			x = temp;
		}
		else if(x>scanfortoday)
		{
			if (DayOfWeek(x, m, y) == 3)
			{
				x = '<span id="wed" style="cursor:pointer;text-decoration:underline;" onclick="setDate(\'' + x + '/' + m + '/' + y + '\'); document.getElementById(\'displayDate\').value = \'Delivery Date ' + x + '/' + m + '/' + y + '\'">' + x + '</span>';
			}
		}
		t+='<td class="'+cD+'">'+x+'</td>';
		if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
	}
	return t+='</tr></table></div>';
}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d;
}

var themonths=['January','February','March','April','May','June','July','August','September','October','November','December']
var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year

function updatecalendar(theselection)
{
	var m, y, temp;
	temp = theselection[theselection.selectedIndex].value.split('.');
	m = temp[0];
	y = temp[1];
	var themonth=parseInt(m)+1;
	var calendarstr=buildCal(themonth, y, "main", "month", "daysofweek", "days", 0);
	if (document.getElementById);
	document.getElementById("calendarspace").innerHTML=calendarstr;
}

function cal()
{
	var html = '';
	html += '<select class="months" onchange="updatecalendar(this.options)">';
	html += ('<option value="'+(curmonth-1)+'" selected="selected">Select Date</option>')
	cm = curmonth-1;
	cy = curyear;
	for (i=0; i<4; i++)
	{
		if(cm == 12)
		{
			cm = 0;
			cy += 1;
		}
		//display option for 12 months of the year
		html += ('<option value="'+cm+'.'+cy+'">'+themonths[cm]+' '+cy+'</option>')
		++cm;
	}
	html += '</select>';
	return html;
}

function processOrder()
{
	document.getElementById('item_name').value = noOfSmokies + ' Pair of Smokies and ' + noOfSalmon + ' Pack of Salmon to be delivered ' + date;
	document.getElementById('amount').value = (parseInt(noOfSmokies) + parseInt(noOfSalmon)) * price;
}

function setDate(d)
{
	date = d;
}

function setSmokieQty(selection)
{
	noOfSmokies = selection[selection.selectedIndex].value;
}

function setSalmonQty(selection)
{
	noOfSalmon = selection[selection.selectedIndex].value;
}

function enableBuyNow()
{
	document.getElementById('buyNow').disabled = false;
}

function resetCal()
{
	document.getElementById('smokieqty').selectedIndex = 0;
	document.getElementById('salmonqty').selectedIndex = 0;
}

function updatePrice()
{
	noOfSmokies = parseFloat(noOfSmokies);
	noOfSalmon = parseFloat(noOfSalmon);
	document.getElementById('totalprice').innerHTML = '&pound;' + currencyFormatted((noOfSmokies + noOfSalmon) * price);
}

function currencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
