/Trunk/Website/js/PopupCalendar.js

# · JavaScript · 405 lines · 287 code · 43 blank · 75 comment · 89 complexity · 61d1fc2dec094d5f3a6542d8755825ed MD5 · raw file

  1. // ********************
  2. // Begin Popup Calendar
  3. // ********************
  4. var popCalDstFld;
  5. var temp;
  6. var popCalWin;
  7. // ******************************
  8. // Expected params:
  9. // [0] Window Name
  10. // [1] Destination Field
  11. // [2] Short Date Format
  12. // [3] Month Names
  13. // [4] Day Names
  14. // [5] Localized Today string
  15. // [6] Localized Close string
  16. // [7] Window Title
  17. // [8] First Day of Week
  18. // ******************************
  19. function popupCal()
  20. {
  21. //debugger; //remove slashes to activate debugging in Visual Studio
  22. var tmpDate = new Date();
  23. var tmpString = "";
  24. var tmpNum = 0;
  25. var popCalDateVal;
  26. var dstWindowName = "";
  27. //Initialize the window to an empty object.
  28. popCalWin = new Object();
  29. //Check for the right number of arguments
  30. if (arguments.length < 2)
  31. {
  32. alert("popupCal(): Wrong number of arguments.");
  33. return void(0);
  34. }
  35. //Get the command line arguments -- Localization is optional
  36. dstWindowName = popupCal.arguments[0];
  37. popCalDstFld = popupCal.arguments[1];
  38. temp = popupCal.arguments[1];
  39. popCalDstFmt = popupCal.arguments[2]; //Localized Short Date Format String
  40. popCalMonths = popupCal.arguments[3]; //Localized Month Names String
  41. popCalDays = popupCal.arguments[4]; //Localized Day Names String
  42. popCalToday = popupCal.arguments[5]; //Localized Today string
  43. popCalClose = popupCal.arguments[6]; //Localized Close string
  44. popCalTitle = popupCal.arguments[7]; //Window Title
  45. popCalFirstDayWeek = popupCal.arguments[8]; //First Day of Week
  46. //check destination field name
  47. if (popCalDstFld != "")
  48. popCalDstFld = document.getElementById(popCalDstFld);
  49. //default localized short date format if not provided
  50. if (popCalDstFmt == "")
  51. popCalDstFmt = "m/d/yyyy";
  52. //default localized months string if not provided
  53. if (popCalMonths == "")
  54. popCalMonths = "January,February,March,April,May,June,July,August,September,October,November,December";
  55. //default localized months string if not provided
  56. if (popCalDays == "")
  57. popCalDays = "Sun,Mon,Tue,Wed,Thu,Fri,Sat";
  58. //default localized today string if not provided
  59. if (popCalToday == "" || typeof popCalToday == "undefined")
  60. popCalToday = "Today";
  61. //default localized close string if not provided
  62. if (popCalClose == "" || typeof popCalClose == "undefined")
  63. popCalClose = "Close";
  64. //default window title if not provided
  65. if (popCalTitle == "" || typeof popCalTitle == "undefined")
  66. popCalTitle = "Calendar";
  67. tmpString = new String(popCalDstFld.value);
  68. //If tmpString is empty (meaning that the field is empty)
  69. //use todays date as the starting point
  70. if(tmpString == "")
  71. popCalDateVal = new Date()
  72. else
  73. {
  74. //Make sure the century is included, if it isn't, add this
  75. //century to the value that was in the field
  76. tmpNum = tmpString.lastIndexOf( "/" );
  77. if ( (tmpString.length - tmpNum) == 3 )
  78. {
  79. tmpString = tmpString.substring(0,tmpNum + 1)+"20"+tmpString.substr(tmpNum+1);
  80. popCalDateVal = new Date(tmpString);
  81. }
  82. else
  83. {
  84. //localized date support:
  85. //If we got to this point, it means the field that was passed
  86. //in has no slashes in it. Use an extra function to build the date
  87. //according to supplied date formatstring.
  88. popCalDateVal = getDateFromFormat(tmpString,popCalDstFmt);
  89. }
  90. }
  91. //Make sure the date is a valid date. Set it to today if it is invalid
  92. //"NaN" is the return value for an invalid date
  93. if( popCalDateVal.toString() == "NaN" || popCalDateVal.toString() == "0")
  94. {
  95. popCalDateVal = new Date();
  96. popCalDstFld.value = "";
  97. }
  98. //Set the base date to midnight of the first day of the specified month,
  99. //this makes things easier?
  100. var dateString = String(popCalDateVal.getMonth()+1) + "/" + String(popCalDateVal.getDate()) + "/" + String(popCalDateVal.getFullYear());
  101. //Call the routine to draw the initial calendar
  102. reloadCalPopup(dateString, dstWindowName);
  103. return void(0);
  104. }
  105. function closeCalPopup()
  106. {
  107. //Can't tell the child window to close itself, the parent window has to
  108. //tell it to close.
  109. popCalWin.close();
  110. return void(0);
  111. }
  112. function reloadCalPopup() //[0]dateString, [1]dstWindowName
  113. {
  114. //Set the window's features here
  115. var windowFeatures = "toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, height=270, width=270, top=" + ((screen.height - 270)/2).toString()+",left="+((screen.width - 270)/2).toString();
  116. var tmpDate = new Date( reloadCalPopup.arguments[0] );
  117. if (tmpDate.toString() == "Invalid Date")
  118. tmpDate = new Date();
  119. tmpDate.setDate(1);
  120. //Get the calendar data
  121. var popCalData = calPopupSetData(tmpDate,reloadCalPopup.arguments[1]);
  122. //Check to see if the window has been initialized, create it if it hasn't been
  123. if( popCalWin.toString() == "[object Object]" )
  124. {
  125. popCalWin = window.open("",reloadCalPopup.arguments[1],windowFeatures);
  126. popCalWin.opener = self;
  127. // Window im Vordergrund
  128. popCalWin.focus();
  129. }
  130. else
  131. {
  132. popCalWin.document.close();
  133. popCalWin.document.write('');
  134. }
  135. //this is the line with the big problem
  136. popCalWin.document.write(popCalData);
  137. return void(1);
  138. }
  139. function calPopupSetData(firstDay,dstWindowName)
  140. {
  141. var popCalData = "";
  142. var lastDate = 0;
  143. var fnt = new Array( "<FONT SIZE=\"1\">", "<B><FONT SIZE=\"2\">", "<FONT SIZE=\"2\" COLOR=\"#EF741D\"><B>");
  144. var dtToday = new Date();
  145. var thisMonth = firstDay.getMonth();
  146. var thisYear = firstDay.getFullYear();
  147. var nPrevMonth = (thisMonth == 0 ) ? 11 : (thisMonth - 1);
  148. var nNextMonth = (thisMonth == 11 ) ? 0 : (thisMonth + 1);
  149. var nPrevMonthYear = (nPrevMonth == 11) ? (thisYear - 1): thisYear;
  150. var nNextMonthYear = (nNextMonth == 0) ? (thisYear + 1): thisYear;
  151. var sToday = String((dtToday.getMonth()+1) + "/01/" + dtToday.getFullYear());
  152. var sPrevMonth = String((nPrevMonth+1) + "/01/" + nPrevMonthYear);
  153. var sNextMonth = String((nNextMonth+1) + "/01/" + nNextMonthYear);
  154. var sPrevYear1 = String((thisMonth+1) + "/01/" + (thisYear - 1));
  155. var sNextYear1 = String((thisMonth+1) + "/01/" + (thisYear + 1));
  156. var tmpDate = new Date( sNextMonth );
  157. tmpDate = new Date( tmpDate.valueOf() - 1001 );
  158. lastDate = tmpDate.getDate();
  159. if (this.popCalMonths.split) // javascript 1.1 defensive code
  160. {
  161. var monthNames = this.popCalMonths.split(",");
  162. var dayNames = this.popCalDays.split(",");
  163. }
  164. else // Need to build a js 1.0 split algorithm, default English for now
  165. {
  166. var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  167. var dayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
  168. }
  169. var styles = "<style><!-- body{font-family:Arial,Helvetica,sans-serif;font-size:9pt}; td { font-family: Arial, Helvetica, sans-serif; font-size: 9pt; color: #666666}; A { text-decoration: none; };TD.day { border-bottom: solid black; border-width: 0px; }--></style>"
  170. var cellAttribs = "align=\"center\" class=\"day\" BGCOLOR=\"#F1F1F1\"onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
  171. var cellAttribs2 = "align=\"center\" BGCOLOR=\"#F1F1F1\" onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
  172. var htmlHead = "<HTML><HEAD><TITLE>"+popCalTitle+"</TITLE>" + styles + "</HEAD><BODY BGCOLOR=\"#F1F1F1\" TEXT=\"#000000\" LINK=\"#364180\" ALINK=\"#FF8100\" VLINK=\"#424282\">";
  173. var htmlTail = "</BODY></HTML>";
  174. var closeAnchor = "<CENTER><input type=button value=\""+popCalClose+"\" onClick=\"javascript:window.opener.closeCalPopup()\"></CENTER>";
  175. var todayAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sToday+"','"+dstWindowName+"');\">"+popCalToday+"</A>";
  176. var prevMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sPrevMonth+"','"+dstWindowName+"');\">" + monthNames[nPrevMonth] + "</A>";
  177. var nextMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sNextMonth+"','"+dstWindowName+"');\">" + monthNames[nNextMonth] + "</A>";
  178. var prevYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sPrevYear1+"','"+dstWindowName+"');\">"+(thisYear-1)+"</A>";
  179. var nextYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sNextYear1+"','"+dstWindowName+"');\">"+(thisYear+1)+"</A>";
  180. popCalData += (htmlHead + fnt[1]);
  181. popCalData += ("<DIV align=\"center\">");
  182. popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\"><TR><TD width=\"45\">&nbsp</TD>");
  183. popCalData += ("<TD width=\"45\" align=\"center\" " + cellAttribs2);
  184. popCalData += (" >");
  185. popCalData += (fnt[0]+prevYear1Anchor+"</FONT></TD>");
  186. popCalData += ("<TD width=\"70\" align=\"center\" "+cellAttribs2);
  187. popCalData += (" >");
  188. popCalData += (fnt[0]+todayAnchor+"</FONT></TD>");
  189. popCalData += ("<TD width=\"45\" align=\"center\" "+cellAttribs2);
  190. popCalData += (" >");
  191. popCalData += (fnt[0]+nextYear1Anchor+"</FONT></TD><TD width=\"45\">&nbsp</TD>");
  192. popCalData += ("</TR></TABLE>");
  193. popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\">");
  194. popCalData += ("<TR><TD width=\"55\" align=\"center\" "+cellAttribs2);
  195. popCalData += (" >");
  196. popCalData += (fnt[0] + prevMonthAnchor + "</FONT></TD>");
  197. popCalData += ("<TD width=\"140\" align=\"center\">");
  198. popCalData += ("&nbsp;&nbsp;"+fnt[1]+"<FONT COLOR=\"#000000\">" + monthNames[thisMonth] + ", " + thisYear + "&nbsp;&nbsp;</FONT></TD>");
  199. popCalData += ("<TD width=\"55\" align=\"center\" "+cellAttribs2);
  200. popCalData += (" >");
  201. popCalData += (fnt[0]+nextMonthAnchor+"</FONT></TD></TR></TABLE><BR>");
  202. popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"2\" cellpadding=\"1\" width=\"245\">" );
  203. popCalData += ("");
  204. popCalData += ("<TR>");
  205. /*
  206. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[0]+"</FONT></TD>");
  207. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[1]+"</FONT></TD>");
  208. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[2]+"</FONT></TD>");
  209. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[3]+"</FONT></TD>");
  210. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[4]+"</FONT></TD>");
  211. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[5]+"</FONT></TD>");
  212. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[6]+"</FONT></TD>");
  213. */
  214. var xday = 0;
  215. for (xday = 0; xday < 7; xday++)
  216. {
  217. popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[(xday+popCalFirstDayWeek)%7]+"</FONT></TD>");
  218. };
  219. popCalData += ("</TR>");
  220. var calDay = 0;
  221. var monthDate = 1;
  222. var weekDay = firstDay.getDay();
  223. do
  224. {
  225. popCalData += ("<TR>");
  226. for (calDay = 0; calDay < 7; calDay++ )
  227. {
  228. if(((weekDay+7-popCalFirstDayWeek)%7 != calDay) || (monthDate > lastDate))
  229. {
  230. popCalData += ("<TD width=\"35\">"+fnt[1]+"&nbsp;</FONT></TD>");
  231. continue;
  232. }
  233. else
  234. {
  235. anchorVal = "<A HREF=\"javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');window.opener.closeCalPopup()\">";
  236. jsVal = "javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');window.opener.closeCalPopup()";
  237. popCalData += ("<TD width=\"35\" "+cellAttribs+" onClick=\""+jsVal+"\">");
  238. if ((firstDay.getMonth() == dtToday.getMonth()) && (monthDate == dtToday.getDate()) && (thisYear == dtToday.getFullYear()) )
  239. popCalData += (anchorVal+fnt[2]+monthDate+"</A></FONT></TD>");
  240. else
  241. popCalData += (anchorVal+fnt[1]+monthDate+"</A></FONT></TD>");
  242. weekDay++;
  243. monthDate++;
  244. }
  245. }
  246. weekDay = popCalFirstDayWeek;
  247. popCalData += ("</TR>");
  248. } while( monthDate <= lastDate );
  249. popCalData += ("</TABLE></DIV><BR>");
  250. popCalData += (closeAnchor+"</FONT>"+htmlTail);
  251. return( popCalData );
  252. }
  253. function calPopupSetDate()
  254. {
  255. calPopupSetDate.arguments[0].value = calPopupSetDate.arguments[1];
  256. }
  257. // utility function
  258. function padZero(num)
  259. {
  260. return ((num <= 9) ? ("0" + num) : num);
  261. }
  262. // Format short date
  263. function constructDate(d,m,y)
  264. {
  265. var fmtDate = this.popCalDstFmt
  266. fmtDate = fmtDate.replace ('dd', padZero(d))
  267. fmtDate = fmtDate.replace ('d', d)
  268. fmtDate = fmtDate.replace ('MM', padZero(m))
  269. fmtDate = fmtDate.replace ('M', m)
  270. fmtDate = fmtDate.replace ('yyyy', y)
  271. fmtDate = fmtDate.replace ('yy', padZero(y%100))
  272. return fmtDate;
  273. }
  274. // ------------------------------------------------------------------
  275. // Utility functions for parsing in getDateFromFormat()
  276. // ------------------------------------------------------------------
  277. function _isInteger(val) {
  278. var digits="1234567890";
  279. for (var i=0; i < val.length; i++) {
  280. if (digits.indexOf(val.charAt(i))==-1) { return false; }
  281. }
  282. return true;
  283. }
  284. function _getInt(str,i,minlength,maxlength) {
  285. for (var x=maxlength; x>=minlength; x--) {
  286. var token=str.substring(i,i+x);
  287. if (token.length < minlength) { return null; }
  288. if (_isInteger(token)) { return token; }
  289. }
  290. return null;
  291. }
  292. // ------------------------------------------------------------------
  293. // getDateFromFormat( date_string , format_string )
  294. //
  295. // This function takes a date string and a format string. It matches
  296. // If the date string matches the format string, it returns the
  297. // getTime() of the date. If it does not match, it returns 0.
  298. // ------------------------------------------------------------------
  299. function getDateFromFormat(val,format) {
  300. val=val+"";
  301. format=format+"";
  302. var i_val=0;
  303. var i_format=0;
  304. var c="";
  305. var token="";
  306. var x,y;
  307. var now=new Date();
  308. var year=now.getYear();
  309. var month=now.getMonth()+1;
  310. var date=1;
  311. while (i_format < format.length) {
  312. // Get next token from format string
  313. c=format.charAt(i_format);
  314. token="";
  315. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  316. token += format.charAt(i_format++);
  317. }
  318. // Extract contents of value based on format token
  319. if (token=="yyyy" || token=="yy" || token=="y") {
  320. if (token=="yyyy") { x=4;y=4; }
  321. if (token=="yy") { x=2;y=2; }
  322. if (token=="y") { x=2;y=4; }
  323. year=_getInt(val,i_val,x,y);
  324. if (year==null) { return 0; }
  325. i_val += year.length;
  326. if (year.length==2) {
  327. if (year > 70) { year=1900+(year-0); }
  328. else { year=2000+(year-0); }
  329. }
  330. }
  331. else if (token=="MM"||token=="M") {
  332. month=_getInt(val,i_val,token.length,2);
  333. if(month==null||(month<1)||(month>12)){return 0;}
  334. i_val+=month.length;}
  335. else if (token=="dd"||token=="d") {
  336. date=_getInt(val,i_val,token.length,2);
  337. if(date==null||(date<1)||(date>31)){return 0;}
  338. i_val+=date.length;}
  339. else {
  340. if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
  341. else {i_val+=token.length;}
  342. }
  343. }
  344. // If there are any trailing characters left in the value, it doesn't match
  345. if (i_val != val.length) { return 0; }
  346. // Is date valid for month?
  347. if (month==2) {
  348. // Check for leap year
  349. if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
  350. if (date > 29){ return 0; }
  351. }
  352. else { if (date > 28) { return 0; } }
  353. }
  354. if ((month==4)||(month==6)||(month==9)||(month==11)) {
  355. if (date > 30) { return 0; }
  356. }
  357. var newdate=new Date(year,month-1,date);
  358. return newdate;
  359. }
  360. // ******************
  361. // End Popup Calendar
  362. // ******************