var WEEK_STR = new Array("SUN","MON","TUE","WED","THU","FRI","SAT");
var WEEK_STR2 = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var WEEK_FULLSTR = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var MONTH_STR = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

function setShipping(type) {
	var s_week = document.getElementById("shipping_week");
	var s_month = document.getElementById("shipping_month");
	var s_day = document.getElementById("shipping_day");
	var div_shipping = document.getElementById("div_shipping");
	
	var delivery_date;
	if (type == 0) {
		delivery_date = getNextNBizDay(6);
		div_shipping.style.backgroundImage="url(images/shipping/shipping_home_std.jpg)";
		shipping_type = 0;
	}
	else {
		//return false;
		delivery_date = getNextNBizDay(3);
		div_shipping.style.backgroundImage="url(images/shipping/shipping_home_exp.jpg)";
		shipping_type = 1;
	}
/*************************************/
/****   Sprint Festival **************/
/*
	var sf_date = new Date();
	var sf_month = sf_date.getMonth() + 1;
	var sf_day = sf_date.getDate();
	
	if( sf_month == 1 && sf_day == 22){
		delivery_date.setDate(3);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 23){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 23){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 24){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 25){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 26){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 27){
		delivery_date.setDate(5);
		delivery_date.setMonth(1);
	}
*/
/**************************************/	
/******** National Day, 2009 ***************
	nd = new Date();
	nd_date = nd.getDate();
	nd_month = nd.getMonth()+1;
	if(type == 0){
		if( nd_month == 9 && nd_date == 28 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
	}
	else if(type == 1){
		if( nd_month == 9 && nd_date == 28 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 9 && nd_date == 29 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 9 && nd_date == 30 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 10 && nd_date == 1 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
	}	
******************************************/	
	
	s_week.innerHTML = WEEK_STR[delivery_date.getDay()];
	s_month.innerHTML = MONTH_STR[delivery_date.getMonth()];
	s_day.innerHTML = delivery_date.getDate();
}
function setCoShipping(type) {
	var div_shipping = document.getElementById("blk_shipping_method");
	var shipping_date = document.getElementById("shipping_date");
	var txt_shipping = document.getElementById("txt_shipping");
	
	var delivery_date;
	if (type == 0 || type == 2) {
		delivery_date = getNextNBizDay(6);
		if( type == 2 ){
			div_shipping.style.backgroundImage="url(images/shipping/shipping_checkout_std_only.jpg)";
		}
		else{
  			div_shipping.style.backgroundImage="url(images/shipping/shipping_checkout_std.jpg)";
		}
		txt_shipping.innerHTML = "Standard S & H:";
		var new_shipping = parseFloat(byId("order_shipping").innerHTML) - shipping_extra;
		byId("order_shipping").innerHTML = pm(new_shipping);
		byId("hid_shipping").value = pm(new_shipping);
		var new_total = parseFloat(byId("order_total").innerHTML) - shipping_extra;
		byId("order_total").innerHTML = pm(new_total);
		byId("hid_total").value = pm(new_total);
		
		shipping_type = 0;
		shipping_extra = 0;
	}
	else if(type == 1) {
		//return false;
		delivery_date = getNextNBizDay(3);
		div_shipping.style.backgroundImage="url(images/shipping/shipping_checkout_exp.jpg)";
		txt_shipping.innerHTML = "Express S & H:";
		var extra = expressShippingExtra(subtotal);
		var new_shipping = shippings + extra;
		byId("order_shipping").innerHTML = pm(new_shipping);
		byId("hid_shipping").value = pm(new_shipping);
		var new_total = parseFloat(byId("order_total").innerHTML) + extra;
		byId("order_total").innerHTML = pm(new_total);
		byId("hid_total").value = pm(new_total);
		
		shipping_type = 1;
		shipping_extra = extra;
	}


	byId("hid_shipping_type").value = shipping_type;
	
/*************************************/
/****   Sprint Festival **************/
/*
	var sf_date = new Date();
	var sf_month = sf_date.getMonth() + 1;
	var sf_day = sf_date.getDate();
	
	if( sf_month == 1 && sf_day == 22){
		delivery_date.setDate(3);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 23){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 23){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 24){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 25){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 26){
		delivery_date.setDate(4);
		delivery_date.setMonth(1);
	}
	else if( sf_month == 1 && sf_day == 27){
		delivery_date.setDate(5);
		delivery_date.setMonth(1);
	}
*/
/**************************************/	
/******** National Day, 2009 ***************
	nd = new Date();
	nd_date = nd.getDate();
	nd_month = nd.getMonth()+1;
	if(type == 0){
		if( nd_month == 9 && nd_date == 28 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
	}
	else if(type == 1){
		if( nd_month == 9 && nd_date == 28 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 9 && nd_date == 29 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 9 && nd_date == 30 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
		else if( nd_month == 10 && nd_date == 1 ){
			delivery_date.setDate(7);
			delivery_date.setMonth(10-1);
		}
	}	
******************************************/		
	
	
	var week = delivery_date.getDay();
	var month = delivery_date.getMonth()+1;
	if (month<10) 
		month="0"+month;
	var day = delivery_date.getDate();
	if (day<10)
		day = "0"+day;
	shipping_date.innerHTML = WEEK_STR2[week] + ".," + month + "/" + day;
}
function expressShippingExtra(subtotal) {
	//var extra = Math.max(Math.floor((subtotal-50)/50), 0) * 5 + 10;
	var extra = 0;
	
	if( subtotal < 100 ){
		extra = 10;
	}
	else if( subtotal >=100 && subtotal < 150 ){
		extra = 15;
	}
	else if (subtotal >=150 && subtotal < 250){
		extra = 20;
	}
	else if (subtotal >=250 && subtotal < 300){
		extra = 25;
	}
	else if (subtotal >=300 && subtotal < 400){
		extra = 30;
	}
	else if (subtotal >=400 && subtotal < 500){
		extra = 35;
	}
	else if (subtotal >=500 && subtotal < 600){
		extra = 40;
	}
	else if (subtotal >=600 && subtotal < 800){
		extra = 45;
	}
	else if (subtotal >=800 && subtotal < 1000){
		extra = 50;
	}
	else if (subtotal>=1000){
		extra = 5 * Math.ceil((subtotal * 0.05)/5);
	}
	
	return extra;
}

function getNextNBizDay(nDay) {
	var now = new Date();
	//var gmtHours = now.getTimezoneOffset()/60;
	//alert(gmtHours + ", " + now.getHours());
	//if (nDay <= 3)
		//now.setHours(now.getHours() + gmtHours + 1);
	//else
		//now.setHours(now.getHours() + gmtHours - 6);
	
	if (nDay <= 0) 
		return now;
	else {
		while (nDay > 0) {
			now.setDate(now.getDate()+1);
			var day = now.getDay();
			/*
			if (day == 6 || day == 0)
				alert("weekend: "+now);
			if (check_holiday_part(now)) 
				alert("holiday: "+now);
			*/
			if (day != 6 && day != 0 && !check_holiday_part(now))
				nDay--;
		}
		return now;
	}
}

// US Holiday, only part of holidays
function check_holiday_part (dt_date) {
	// check simple dates (month/date - no leading zeroes)
	var n_date = dt_date.getDate(),
		n_month = dt_date.getMonth() + 1;
	var s_date1 = n_month + '/' + n_date;
		
	if (   s_date1 == '1/1'   // New Year's Day
		|| s_date1 == '7/4'   // Independence Day
		|| s_date1 == '9/6'   // Labor Day
		//|| s_date1 == '11/26' // Thanks Giving Day
		|| s_date1 == '12/25' // Christmas Day
		//|| ( s_date1 == '12/26' && dt_date.getFullYear() == 2008 )
		|| ( s_date1 == '7/3' && dt_date.getFullYear() == 2009 )
	) return true;
	
	// weekday from beginning of the month (month/num/day)
	var n_wday = dt_date.getDay(),
		n_wnum = Math.floor((n_date - 1) / 7) + 1;
	var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;
	if( s_date2 == '11/4/4' ) //ThanksGiving Day, 4th Thursday in Nov.
		return true;
	
	var dt_temp = new Date (dt_date);
	dt_temp.setDate(1);
	dt_temp.setMonth(dt_temp.getMonth() + 1);
	dt_temp.setDate(dt_temp.getDate() - 1);
	n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
	var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;
	
	if (   s_date3 == '5/1/1'  // Memorial Day, last Monday in May
	) return true;
	
	return false;
}

// All US Holiday every year
function check_holiday_all (dt_date) {

	// check simple dates (month/date - no leading zeroes)
	var n_date = dt_date.getDate(),
		n_month = dt_date.getMonth() + 1;
	var s_date1 = n_month + '/' + n_date;
		
	if (   s_date1 == '1/1'   // New Year's Day
		|| s_date1 == '6/14'  // Flag Day
		|| s_date1 == '7/4'   // Independence Day
		|| s_date1 == '11/11' // Veterans Day
		|| s_date1 == '12/25' // Christmas Day
		//|| ( s_date1 == '12/26' && dt_date.getFullYear() == 2008 )
		|| ( s_date1 == '7/3' && dt_date.getFullYear() == 2009 )
	) return true;
	
	// weekday from beginning of the month (month/num/day)
	var n_wday = dt_date.getDay(),
		n_wnum = Math.floor((n_date - 1) / 7) + 1;
	var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;
	
	if (   s_date2 == '1/3/1'  // Birthday of Martin Luther King, third Monday in January
		|| s_date2 == '2/3/1'  // Washington's Birthday, third Monday in February
		|| s_date2 == '5/3/6'  // Armed Forces Day, third Saturday in May
		|| s_date2 == '9/1/1'  // Labor Day, first Monday in September
		|| s_date2 == '10/2/1' // Columbus Day, second Monday in October
		|| s_date2 == '11/4/4' // Thanksgiving Day, fourth Thursday in November
	) return true;

	// weekday number from end of the month (month/num/day)
	var dt_temp = new Date (dt_date);
	dt_temp.setDate(1);
	dt_temp.setMonth(dt_temp.getMonth() + 1);
	dt_temp.setDate(dt_temp.getDate() - 1);
	n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
	var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;
	
	if (   s_date3 == '5/1/1'  // Memorial Day, last Monday in May
	) return true;

	// misc complex dates
	if (s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0) 
	// Inauguration Day, January 20th every four years, starting in 1937. 
	) return true;
		
	if (n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2
	// Election Day, Tuesday on or after November 2. 
	) return true;
	
	return false;
}
function byId(id) {
	return document.getElementById(id);
}
function writeProofDate() {
	var delivery_date = getNextNBizDay(1);
	//document.write("8am Central, 02/15/2009");
	//document.write("8am Central, " + (delivery_date.getMonth()+1) + "/" + delivery_date.getDate() + "/" + delivery_date.getFullYear() );
	document.write("8am Central, Next Biz Day");
}
function writeReceivingDate() {
	var delivery_date = getNextNBizDay(6);
	//document.write("Fri. 02/19/2009");
	var month = (delivery_date.getMonth()+1);
	var day = delivery_date.getDate();
	if(month < 10)	month = "0" + month;
	if(day < 10) 	day = "0" + day;
	document.write( WEEK_FULLSTR[delivery_date.getDay()] + ", " + month + "/" + day );
}
