![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 /* $Id: calendar.js 38 2008-02-11 11:38:51Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/js/calendar.js $ */ 2 var hourMSecs = 3600*1000; 3 4 /** 5 * no comment needed 6 */ 7 function isInArray(myArray, intValue) { 8 9 for (var i = 0, i_cmp = myArray.length; i < i_cmp; i++) { 10 if (myArray[i] == intValue) { 11 return true; 12 } 13 } 14 return false; 15 } 16 17 // This function gets called when the end-user clicks on some date. 18 function selected(cal, date) { 19 // cal.sel.value = date; // just update the date in the input field. 20 // Pedro A. : Lets pass the date in a Ymd format and let our formatDate function to the rest. 21 cal.sel.value = (cal.date.print("%Y%m%d%H%M")); 22 setDate(cal.form, cal.sel.name); 23 if (cal.dateClicked && (cal.sel.id == "ini_date" || cal.sel.id == "end_date")) 24 // if we add this call we close the calendar on single-click. 25 cal.callCloseHandler(); 26 } 27 28 // And this gets called when the end-user clicks on the _selected_ date, 29 // or clicks on the "Close" button. It just hides the calendar without 30 // destroying it. 31 function closeHandler(cal) { 32 cal.hide(); // hide the calendar 33 // cal.destroy(); 34 _dynarch_popupCalendar = null; 35 } 36 37 // This function shows the calendar under the element having the given id. 38 // It takes care of catching "mousedown" signals on document and hiding the 39 // calendar if the click was outside. 40 function showCalendar(id, format, form_name, showsTime, showsOtherMonths) { 41 var el = document.getElementById(id); 42 if (_dynarch_popupCalendar != null) { 43 // we already have some calendar created 44 _dynarch_popupCalendar.hide(); // so we hide it first. 45 } else { 46 // first-time call, create the calendar. 47 var cal = new Calendar(1, null, selected, closeHandler); 48 // uncomment the following line to hide the week numbers 49 // cal.weekNumbers = false; 50 if (typeof showsTime == "string") { 51 cal.showsTime = true; 52 cal.time24 = (showsTime == "24"); 53 } 54 if (showsOtherMonths) { 55 cal.showsOtherMonths = true; 56 } 57 _dynarch_popupCalendar = cal; // remember it in the global var 58 cal.setRange(1900, 2070); // min/max year allowed. 59 cal.create(); 60 } 61 _dynarch_popupCalendar.setDateFormat(format); // set the specified date format 62 _dynarch_popupCalendar.parseDate(el.value); // try to parse the text in field 63 _dynarch_popupCalendar.sel = el; // inform it what input field we use 64 _dynarch_popupCalendar.form = form_name; // inform it what form we use 65 66 // the reference element that we pass to showAtElement is the button that 67 // triggers the calendar. In this example we align the calendar bottom-right 68 // to the button. 69 _dynarch_popupCalendar.showAtElement(el, "Bl"); // show the calendar 70 71 return false; 72 } 73 74 var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); 75 var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 76 function LZ(x) { 77 return(x<0||x>9?"":"0")+x 78 } 79 80 function formatDate(date,format) { 81 format=format+""; 82 var result=""; 83 var i_format=0; 84 var c=""; 85 var token=""; 86 var y=date.getYear()+""; 87 var M=date.getMonth()+1; 88 var d=date.getDate(); 89 var E=date.getDay(); 90 var H=date.getHours(); 91 var m=date.getMinutes(); 92 var s=date.getSeconds(); 93 var Y, yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k; 94 var value=new Object(); 95 if(y.length < 4){ 96 y=""+(y-0+1900); 97 } 98 value["y"]=""+y; 99 value["yyyy"]=y; 100 value["Y"]=y; 101 value["yy"]=y.substring(2,4); 102 value["M"]=M; 103 value["MM"]=LZ(M); 104 value["MMM"]=MONTH_NAMES[M-1]; 105 value["NNN"]=MONTH_NAMES[M+11]; 106 value["b"]=MONTH_NAMES[M+11]; 107 value["d"]=d; 108 value["dd"]=LZ(d); 109 value["E"]=DAY_NAMES[E+7]; 110 value["EE"]=DAY_NAMES[E]; 111 value["H"]=H; 112 value["HH"]=LZ(H); 113 if(H==0) { 114 value["h"]=12; 115 } else if(H>12) { 116 value["h"]=H-12; 117 } else { 118 value["h"]=H; 119 } 120 value["hh"]=LZ(value["h"]); 121 if(H>11) { 122 value["K"]=H-12; 123 } else { 124 value["K"]=H; 125 } 126 value["k"]=H+1; 127 value["KK"]=LZ(value["K"]); 128 value["kk"]=LZ(value["k"]); 129 if(H > 11) { 130 value["a"]="pm"; 131 } else { 132 value["a"]="am"; 133 } 134 value["m"]=m; 135 value["mm"]=LZ(m); 136 value["s"]=s; 137 value["ss"]=LZ(s); 138 while(i_format < format.length) { 139 c=format.charAt(i_format); 140 token=""; 141 while((format.charAt(i_format)==c) &&(i_format < format.length)) { 142 token += format.charAt(i_format++); 143 } 144 if(value[token] != null) { 145 result=result + value[token]; 146 } else { 147 result=result + token; 148 } 149 } 150 return result; 151 } 152 153 // ------------------------------------------------------------------ 154 // Utility functions for parsing in getDateFromFormat() 155 // ------------------------------------------------------------------ 156 function _isInteger(val) { 157 var digits="1234567890"; 158 for (var i=0, i_cmp=val.length; i < i_cmp; i++) { 159 if (digits.indexOf(val.charAt(i))==-1) { 160 return false; 161 } 162 } 163 return true; 164 } 165 function _getInt(str,i,minlength,maxlength) { 166 for (var x=maxlength; x>=minlength; x--) { 167 var token=str.substring(i,i+x); 168 if (token.length < minlength) { 169 return null; 170 } 171 if (_isInteger(token)) { 172 return token; 173 } 174 } 175 return null; 176 } 177 178 // ------------------------------------------------------------------ 179 // getDateFromFormat( date_string , format_string ) 180 // 181 // This function takes a date string and a format string. It matches 182 // If the date string matches the format string, it returns the 183 // getTime() of the date. If it does not match, it returns 0. 184 // ------------------------------------------------------------------ 185 function getDateFromFormat(val,format) { 186 val=val+""; 187 format=format+""; 188 var i_val=0; 189 var i_format=0; 190 var c=""; 191 var token=""; 192 var token2=""; 193 var x,y; 194 var now=new Date(); 195 var year=now.getYear(); 196 var month=now.getMonth()+1; 197 var date=1; 198 var hh=now.getHours(); 199 var mm=now.getMinutes(); 200 var ss=now.getSeconds(); 201 var ampm=""; 202 203 while (i_format < format.length) { 204 // Get next token from format string 205 c=format.charAt(i_format); 206 token=""; 207 while ((format.charAt(i_format)==c) && (i_format < format.length)) { 208 token += format.charAt(i_format++); 209 } 210 // Extract contents of value based on format token 211 if (token=="yyyy" || token=="yy" || token=="y" || token=="Y") { 212 if (token=="Y") { x=4;y=4; } 213 if (token=="yyyy") { x=4;y=4; } 214 if (token=="yy") { x=2;y=2; } 215 if (token=="y") { x=2;y=4; } 216 year=_getInt(val,i_val,x,y); 217 if (year==null) { return 0; } 218 i_val += year.length; 219 if (year.length==2) { 220 if (year > 70) { 221 year=1900+(year-0); 222 } else { 223 year=2000+(year-0); 224 } 225 } 226 } else if (token=="MMM"||token=="NNN") { 227 month=0; 228 for (var i=0, i_cmp=MONTH_NAMES.length; i<i_cmp; i++) { 229 var month_name=MONTH_NAMES[i]; 230 if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) { 231 if (token=="MMM"||(token=="NNN"&&i>11)) { 232 month=i+1; 233 if (month>12) { 234 month -= 12; 235 } 236 i_val += month_name.length; 237 break; 238 } 239 } 240 } 241 if ((month < 1)||(month>12)) { 242 return 0; 243 } 244 } else if (token=="EE"||token=="E") { 245 for (var i=0, i_cmp=DAY_NAMES.length; i<i_cmp; i++) { 246 var day_name=DAY_NAMES[i]; 247 if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) { 248 i_val += day_name.length; 249 break; 250 } 251 } 252 } else if (token=="MM"||token=="M") { 253 month=_getInt(val,i_val,token.length,2); 254 if(month==null||(month<1)||(month>12)){ 255 return 0; 256 } 257 i_val+=month.length; 258 } else if (token=="dd"||token=="d") { 259 date=_getInt(val,i_val,token.length,2); 260 if(date==null||(date<1)||(date>31)) { 261 return 0; 262 } 263 i_val+=date.length; 264 } else if (token=="hh"||token=="h") { 265 hh=_getInt(val,i_val,token.length,2); 266 if(hh==null||(hh<1)||(hh>12)) { 267 return 0; 268 } 269 i_val+=hh.length; 270 } else if (token=="HH"||token=="H") { 271 hh=_getInt(val,i_val,token.length,2); 272 if(hh==null||(hh<0)||(hh>23)) { 273 return 0; 274 } 275 i_val+=hh.length; 276 } else if (token=="KK"||token=="K") { 277 hh=_getInt(val,i_val,token.length,2); 278 if(hh==null||(hh<0)||(hh>11)) { 279 return 0; 280 } 281 i_val+=hh.length; 282 } else if (token=="kk"||token=="k") { 283 hh=_getInt(val,i_val,token.length,2); 284 if(hh==null||(hh<1)||(hh>24)) { 285 return 0; 286 } 287 i_val+=hh.length;hh--; 288 } else if (token=="mm"||token=="m") { 289 mm=_getInt(val,i_val,token.length,2); 290 if(mm==null||(mm<0)||(mm>59)){ 291 return 0; 292 } 293 i_val+=mm.length; 294 } else if (token=="ss"||token=="s") { 295 ss=_getInt(val,i_val,token.length,2); 296 if(ss==null||(ss<0)||(ss>59)){ 297 return 0; 298 } 299 i_val+=ss.length; 300 } else if (token=="a") { 301 if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="am";} 302 else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="pm";} 303 else {return 0;} 304 i_val+=2; 305 } else { 306 if (val.substring(i_val,i_val+token.length)!=token) {return 0;} 307 else {i_val+=token.length;} 308 } 309 } 310 // If there are any trailing characters left in the value, it doesn't match 311 if (i_val != val.length) { return 0; } 312 // Is date valid for month? 313 if (month==2) { 314 // Check for leap year 315 if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year 316 if (date > 29){ return 0; } 317 } else { 318 if (date > 28) { return 0; } 319 } 320 } if ((month==4)||(month==6)||(month==9)||(month==11)) { 321 if (date > 30) { return 0; } 322 } 323 // Correct hours value 324 if (hh<12 && ampm=="pm") { hh=hh-0+12; } 325 else if (hh>11 && ampm=="am") { hh-=12; } 326 var newdate=new Date(year,month-1,date,hh,mm,ss); 327 return newdate.getTime(); 328 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Nov 20 03:00:14 2008 | Cross-referenced by PHPXref 0.7 |