﻿
function daysInMonth(month,year)
{
	var arr = [0,31,28,31,30,31,30,31,31,30,31,30,31]
	if (month == 2)
        // if Feb. 29th, year is not a valid date the new Date() will return
        // a date object equivelent to March 1st, year
        return ((new Date(year,1,29)).getDate() == 1)? 28: 29
	else if (month < 1 || month > 12)
		return 0
	else
		return arr[month]
}

// convert a date string in DD/MM/YYYY format to a date object
function strToDate(str)
{
	// check format
	var reg = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (!reg.test(str))
		return null
	var arr = str.split('/')
	var day = arr[0]
	var mon = arr[1]
	var year = arr[2]
	
	if (year < 2003)
		return null
	if (mon < 1 || mon > 12)
		return null
	if (day < 1 || day > daysInMonth(mon,year))
		return null
	return new Date(year,mon-1,day)
}

function addItemToMyCart(
	itemId				// id of item to be added
	,isAvailable		// boolean - indicates wether sellable item is available - true by default
	,saleStartDateStr	// sale's start day in DD/MM/YYYY - if null no limit
	,saleEndDateStr		// sale's ending date in DD/MM/YYYY - if null no limit
	)
{
	var today = new Date()
	var fromDate = strToDate(saleStartDateStr)
	var toDate = strToDate(saleEndDateStr)
	today.setHours(0,0,0,0) // reset hours, we care only about the date
	
	if (isAvailable == false) { // item not available
		alert('   .מוצר זה אינו זמין\n.אנא בחר מוצר אחר')
		return
	}
	if (fromDate && today - fromDate < 0) { // sale didn't start yet
		alert('.מוצר זה אינו זמין עדיין\n   .אנא בחר מוצר אחר')
		return
	}
	if (toDate && toDate - today < 0) { // sale already ended
		alert('   .מוצר זה אינו זמין\n.אנא בחר מוצר אחר')
		return
	}
	location = 'ShoppingCart.asp?itemId=' + itemId
}
