/*
This script page contains cross-browser JavaScript that is used
to build a form element based calendar. Copyright 2007, 
JAWS Media. All rights reserved. News Pro 1.0
*/

//////////////////////////////////////////////////
// This function returns the selected value to a
// specified form element
//////////////////////////////////////////////////
function setDate(str) {

// Return the string if it is an empty date value
if (str == "   ") {
	return;
	}

mnth1 = document.form1.month.value;
mnth = mnth1;
mnth++;
year = document.form1.year.value;

// Formate date for MySQL YYYY-MM-DD
if(mnth<10) {mnth = "0" + mnth;}
dateStr = year+"-"+mnth+"-"+str;

// Remove any spaces from date value
dateStr = trim(dateStr);


// Populate PublishDate only
if(document.form1.JS_Mode.value == "PublishDate") {
	document.form1.PublishDate.value = dateStr;
	document.form1.DateLabel.value = getDayOfWeek(dateStr) +" - "+ getMonthName(mnth-1) +" "+ str +", "+ year;
	}
// Populate PublishDate and CloseDate values
if(document.form1.JS_Mode.value == "PublishDateCloseDate") {
	if(document.form1.SelectDate.value == "1") {
		document.form1.PublishDate.value = dateStr;
		document.form1.PublishDateLabel.value = getDayOfWeek(dateStr) +" - "+ getMonthName(mnth-1) +" "+ str +", "+ year;
		}
	if(document.form1.SelectDate.value == "2") {
		document.form1.CloseDate.value = dateStr;
		document.form1.CloseDateLabel.value = getDayOfWeek(dateStr) +" - "+ getMonthName(mnth-1) +" "+ str +", "+ year;
		}
	}
}/////////////////////////////////////////////////


//////////////////////////////////////////////////
// The function removes spaces from selected date
//////////////////////////////////////////////////
function trim(str) {
res="";
for(var i=0; i< str.length; i++) {
	if (str.charAt(i) != " ") {
		res +=str.charAt(i);
		}
	}

return res;
}/////////////////////////////////////////////////


//////////////////////////////////////////////////
// The method to get the Month name when given the 
// Month number of the year.
//////////////////////////////////////////////////
function getMonthName(mnth) {
if (mnth == 0) {name = "January";}
else if(mnth == 1) {name = "February";}
else if(mnth == 2) {name = "March";}
else if(mnth == 3) {name = "April";}
else if(mnth == 4) {name = "May";} 
else if(mnth == 5) {name = "June";} 
else if(mnth == 6) {name = "July";} 
else if(mnth == 7) {name = "August";} 
else if(mnth == 8) {name = "September";} 
else if(mnth == 9) {name = "October";} 
else if(mnth == 10) {name = "November";} 
else if(mnth == 11) {name = "December";}

return name;
}/////////////////////////////////////////////////


//////////////////////////////////////////////////
// The method to get the day of the week with a
// supplied argument
//////////////////////////////////////////////////
function getDayOfWeek(str) {
// Modify the str var to replace the
// YYYY-MM-DD with YYYY,MM,DD in form
str = str.split("-");

// Convert year to number
splitYear = parseInt(str[0]);
// Convert month to number
if(str[1].substr(0,1) == "0") {
	splitMonth = parseInt(str[1].substr(1,1));
	}
	else {splitMonth = parseInt(str[1]);}
// Convert date to number
if(str[2].substr(0,1) == "0") {
	splitDate = parseInt(str[2].substr(1,1));
	}
	else {splitDate = parseInt(str[2]);}


// Decrement splitMonth value by one, Jan = 0 in this array
splitMonth = splitMonth - 1;


// Enter date into method
tmpDate = new Date(splitYear,splitMonth,splitDate);
x = tmpDate.getDay();


// Build array of day names
weekDay = new Array();
weekDay[0] = "Sunday";
weekDay[1] = "Monday"; 
weekDay[2] = "Tuesday";
weekDay[3] = "Wednesday";
weekDay[4] = "Thursday";
weekDay[5] = "Friday";
weekDay[6] = "Saturday";

// Return the string
return weekDay[x];
}/////////////////////////////////////////////////


//////////////////////////////////////////////////
// Get number of days in month based on the year
//////////////////////////////////////////////////
function getNoOfDaysInMnth(mnth,yr) {
// Divide the year by 4 to see if it is a leap year
rem = yr % 4;
// If it divides evently, it is a leap year
if(rem == 0) {leap = 1;} 
	else {leap = 0;}
// Initialize this to be zero
noDays = 0;

// Determine the number of days for the given month
if( (mnth == 1) || (mnth == 3) || (mnth == 5) || (mnth == 7) || (mnth == 8) || (mnth == 10) || (mnth == 12)) {
	noDays=31;
	} 
else if (mnth == 2) {// If Feb, set to 28 unless it is leap year, 29
	noDays=28+leap;
	} 
else {// The other months that just have 30 days in the month
	noDays=30;
	}

return noDays;
}/////////////////////////////////////////////////


/**
 * The function to reset the date values in the buttons of the 
 * slots.
 */

function fillDates(dayOfWeek1,noOfDaysInmnth) {

 for(var i=1; i<43; i++) {
   str = "s"+i;
   document.form1.elements[str].value="   ";
 }


 startSlotIndx = dayOfWeek1;
 slotIndx = startSlotIndx;

 for(var i=1; i<(noOfDaysInmnth+1); i++) {
  slotName = "s"+slotIndx;

  val="";
  if (i<10) {
    val = " 0"+i+" ";
  } else {
    val = i;
  }

  document.form1.elements[slotName].value = val;
  slotIndx++;
 }
  
}//fillDates()
 

//////////////////////////////////////////////////
// The function that is called at the time of loading 
// the page. This function displays Today's date 
// and also displays the the calendar of current month.
//////////////////////////////////////////////////
function thisMonth() {
dt = new Date();
mnth  = dt.getMonth(); /* 0-11*/
dayOfMnth = dt.getDate(); /* 1-31*/
dayOfWeek = dt.getDay(); /*0-6*/
yr = dt.getFullYear(); /*4-digit year*/
var JM_thisMonth;

mnthName = getMonthName(mnth)+ " ";
document.form1.month.value = mnth;
document.form1.year.value = yr;
document.form1.currMonth.value = mnth;
document.form1.currYear.value = yr;
document.form1.monthYear.value = mnthName+yr;

// Format for mysql date
if((mnth+1)>9) {JM_thisMonth = yr+"-"+(mnth+1);}
else if((mnth+1)<10) {JM_thisMonth = yr+"-0"+(mnth+1);}
if(dayOfMnth>9) {JM_thisMonth += "-"+dayOfMnth;}
if(dayOfMnth<10) {JM_thisMonth += "-0"+dayOfMnth;}
 
// Automatically enter in PublishDate value if
// not the Subscriber Edit page; the Subscriber
// Edit page will already have this value populated
if(document.form1.DateLabel.value == "") {
	document.form1.PublishDate.value = JM_thisMonth;
	}

startStr = (mnth+1)+"/1/"+yr;
dt1 = new Date(startStr);
dayOfWeek1 = dt1.getDay(); /*0-6*/
noOfDaysInMnth = getNoOfDaysInMnth(mnth+1,yr);
fillDates(dayOfWeek1+1,noOfDaysInMnth);
}/////////////////////////////////////////////////


/**
 * The function that will be used to display the calendar of the next month.
 */

function nextMonth() {

 var currMnth = document.form1.month.value;
 currYr = document.form1.year.value;

 if (currMnth == "11") {
    nextMnth = 0;
    nextYr = currYr;
    nextYr++;
 } else {
   nextMnth=currMnth;
   nextMnth++;
   nextYr = currYr;
 }

 mnthName = getMonthName(nextMnth);
 document.form1.month.value=nextMnth;
 document.form1.year.value=nextYr;
 document.form1.monthYear.value= mnthName+" "+nextYr;

 str = (nextMnth+1)+"/1/"+nextYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay();

 noOfDays = getNoOfDaysInMnth(nextMnth+1,nextYr);

 fillDates(dayOfWeek+1,noOfDays);
 

}//nextMonth()

/**
 * The method to display the calendar of the previous month.
 */

function prevMonth() {

 var currMnth = document.form1.month.value;
 currYr = document.form1.year.value;

 if (currMnth == "0") {
    prevMnth = 11;
    prevYr = currYr;
    prevYr--;
 } else {
   prevMnth=currMnth;
   prevMnth--;
   prevYr = currYr;
 }

 str = (prevMnth+1)+"/1/"+prevYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay();

 /***********************************************
  * Remove the comment if do not want the user to 
  * go to any previous month than this current month.
  ***********************************************/

 /*

 runningMonth = document.form1.currMonth.value;
 rMonth=runningMonth;
 rMonth++;
 runningYear = document.form1.currYear.value;
 rYear=runningYear;

 str = (rMonth)+"/1/"+rYear;
 dt1 = new Date(str);
 
 if (dt.valueOf() < dt1.valueOf()) {
   alert('Cannot Go Before Current Month');
   return;
 }
 
 */

 /**************************************************
 * End of comment
 **************************************************/

 mnthName = getMonthName(prevMnth);
 document.form1.month.value=prevMnth;
 document.form1.year.value=prevYr;
 document.form1.monthYear.value= mnthName+" "+prevYr;

 noOfDays = getNoOfDaysInMnth(prevMnth+1,prevYr);
 fillDates(dayOfWeek+1,noOfDays);
 
}//prevMonth()