[ PHPXref.com ] [ Generated: Sun Jul 20 20:12:55 2008 ] [ Scout Tracker 0.13 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/includes/ -> functions.js (source)

   1  
   2  function checkscript() {
   3   for (i=0;i<4;i++) {
   4    box = document.example.elements[i];
   5    if (!box.value) {
   6     alert('You haven\'t filled in ' + box.name + '!');
   7     box.focus()
   8    }
   9    if (box.value='x') {
  10     box.value=now()
  11    }
  12   }
  13   return true;
  14  }
  15  // ===================================================================

  16  // Author: Matt Kruse <matt@mattkruse.com>

  17  // WWW: http://www.mattkruse.com/

  18  //

  19  // NOTICE: You may use this code for any purpose, commercial or

  20  // private, without any further permission from the author. You may

  21  // remove this notice from your final code if you wish, however it is

  22  // appreciated by the author if at least my web site address is kept.

  23  //

  24  // You may *NOT* re-distribute this code in any way except through its

  25  // use. That means, you can include it in your product, or your web

  26  // site, or any other form where the code is actually being used. You

  27  // may not put the plain javascript up on your site for download or

  28  // include it in your javascript libraries for download. 

  29  // If you wish to share this code with others, please just point them

  30  // to the URL instead.

  31  // Please DO NOT link directly to my .js files from your site. Copy

  32  // the files to your server and use them there. Thank you.

  33  // ===================================================================

  34  
  35  // HISTORY

  36  // ------------------------------------------------------------------

  37  // May 17, 2003: Fixed bug in parseDate() for dates <1970

  38  // March 11, 2003: Added parseDate() function

  39  // March 11, 2003: Added "NNN" formatting option. Doesn't match up

  40  //                 perfectly with SimpleDateFormat formats, but 

  41  //                 backwards-compatability was required.

  42  
  43  // ------------------------------------------------------------------

  44  // These functions use the same 'format' strings as the 

  45  // java.text.SimpleDateFormat class, with minor exceptions.

  46  // The format string consists of the following abbreviations:

  47  // 

  48  // Field        | Full Form          | Short Form

  49  // -------------+--------------------+-----------------------

  50  // Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)

  51  // Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)

  52  //              | NNN (abbr.)        |

  53  // Day of Month | dd (2 digits)      | d (1 or 2 digits)

  54  // Day of Week  | EE (name)          | E (abbr)

  55  // Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)

  56  // Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)

  57  // Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)

  58  // Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)

  59  // Minute       | mm (2 digits)      | m (1 or 2 digits)

  60  // Second       | ss (2 digits)      | s (1 or 2 digits)

  61  // AM/PM        | a                  |

  62  //

  63  // NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!

  64  // Examples:

  65  //  "MMM d, y" matches: January 01, 2000

  66  //                      Dec 1, 1900

  67  //                      Nov 20, 00

  68  //  "M/d/yy"   matches: 01/20/00

  69  //                      9/2/00

  70  //  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"

  71  // ------------------------------------------------------------------

  72  
  73  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');
  74  var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  75  function LZ(x) {return(x<0||x>9?"":"0")+x}
  76  
  77  // ------------------------------------------------------------------

  78  // isDate ( date_string, format_string )

  79  // Returns true if date string matches format of format string and

  80  // is a valid date. Else returns false.

  81  // It is recommended that you trim whitespace around the value before

  82  // passing it to this function, as whitespace is NOT ignored!

  83  // ------------------------------------------------------------------

  84  function isDate(val,format) {
  85   var date=getDateFromFormat(val,format);
  86   if (date==0) { return false; }
  87   return true;
  88   }
  89  
  90  // -------------------------------------------------------------------

  91  // compareDates(date1,date1format,date2,date2format)

  92  //   Compare two date strings to see which is greater.

  93  //   Returns:

  94  //   1 if date1 is greater than date2

  95  //   0 if date2 is greater than date1 of if they are the same

  96  //  -1 if either of the dates is in an invalid format

  97  // -------------------------------------------------------------------

  98  function compareDates(date1,dateformat1,date2,dateformat2) {
  99   var d1=getDateFromFormat(date1,dateformat1);
 100   var d2=getDateFromFormat(date2,dateformat2);
 101   if (d1==0 || d2==0) {
 102    return -1;
 103    }
 104   else if (d1 > d2) {
 105    return 1;
 106    }
 107   return 0;
 108   }
 109  
 110  // ------------------------------------------------------------------

 111  // formatDate (date_object, format)

 112  // Returns a date in the output format specified.

 113  // The format string uses the same abbreviations as in getDateFromFormat()

 114  // ------------------------------------------------------------------

 115  function formatDate(date,format) {
 116   if (date=="0000-00-00" || date=="x" or date=="X" || date=null) { date='12-12-12' }
 117   format=format+"";
 118   var result="";
 119   var i_format=0;
 120   var c="";
 121   var token="";
 122   var y=date.getYear()+"";
 123   var M=date.getMonth()+1;
 124   var d=date.getDate();
 125   var E=date.getDay();
 126   var H=date.getHours();
 127   var m=date.getMinutes();
 128   var s=date.getSeconds();
 129   var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
 130   // Convert real date parts into formatted versions

 131   var value=new Object();
 132   if (y.length < 4) {y=""+(y-0+1900);}
 133   value["y"]=""+y;
 134   value["yyyy"]=y;
 135   value["yy"]=y.substring(2,4);
 136   value["M"]=M;
 137   value["MM"]=LZ(M);
 138   value["MMM"]=MONTH_NAMES[M-1];
 139   value["NNN"]=MONTH_NAMES[M+11];
 140   value["d"]=d;
 141   value["dd"]=LZ(d);
 142   value["E"]=DAY_NAMES[E+7];
 143   value["EE"]=DAY_NAMES[E];
 144   value["H"]=H;
 145   value["HH"]=LZ(H);
 146   if (H==0){value["h"]=12;}
 147   else if (H>12){value["h"]=H-12;}
 148   else {value["h"]=H;}
 149   value["hh"]=LZ(value["h"]);
 150   if (H>11){value["K"]=H-12;} else {value["K"]=H;}
 151   value["k"]=H+1;
 152   value["KK"]=LZ(value["K"]);
 153   value["kk"]=LZ(value["k"]);
 154   if (H > 11) { value["a"]="PM"; }
 155   else { value["a"]="AM"; }
 156   value["m"]=m;
 157   value["mm"]=LZ(m);
 158   value["s"]=s;
 159   value["ss"]=LZ(s);
 160   while (i_format < format.length) {
 161    c=format.charAt(i_format);
 162    token="";
 163    while ((format.charAt(i_format)==c) && (i_format < format.length)) {
 164     token += format.charAt(i_format++);
 165     }
 166    if (value[token] != null) { result=result + value[token]; }
 167    else { result=result + token; }
 168    }
 169   return result;
 170   }
 171   
 172  // ------------------------------------------------------------------

 173  // Utility functions for parsing in getDateFromFormat()

 174  // ------------------------------------------------------------------

 175  function _isInteger(val) {
 176   var digits="1234567890";
 177   for (var i=0; i < val.length; i++) {
 178    if (digits.indexOf(val.charAt(i))==-1) { return false; }
 179    }
 180   return true;
 181   }
 182  function _getInt(str,i,minlength,maxlength) {
 183   for (var x=maxlength; x>=minlength; x--) {
 184    var token=str.substring(i,i+x);
 185    if (token.length < minlength) { return null; }
 186    if (_isInteger(token)) { return token; }
 187    }
 188   return null;
 189   }
 190   
 191  // ------------------------------------------------------------------

 192  // getDateFromFormat( date_string , format_string )

 193  //

 194  // This function takes a date string and a format string. It matches

 195  // If the date string matches the format string, it returns the 

 196  // getTime() of the date. If it does not match, it returns 0.

 197  // ------------------------------------------------------------------

 198  function getDateFromFormat(val,format) {
 199   val=val+"";
 200   format=format+"";
 201   var i_val=0;
 202   var i_format=0;
 203   var c="";
 204   var token="";
 205   var token2="";
 206   var x,y;
 207   var now=new Date();
 208   var year=now.getYear();
 209   var month=now.getMonth()+1;
 210   var date=1;
 211   var hh=now.getHours();
 212   var mm=now.getMinutes();
 213   var ss=now.getSeconds();
 214   var ampm="";
 215   
 216   while (i_format < format.length) {
 217    // Get next token from format string

 218    c=format.charAt(i_format);
 219    token="";
 220    while ((format.charAt(i_format)==c) && (i_format < format.length)) {
 221     token += format.charAt(i_format++);
 222     }
 223    // Extract contents of value based on format token

 224    if (token=="yyyy" || token=="yy" || token=="y") {
 225     if (token=="yyyy") { x=4;y=4; }
 226     if (token=="yy")   { x=2;y=2; }
 227     if (token=="y")    { x=2;y=4; }
 228     year=_getInt(val,i_val,x,y);
 229     if (year==null) { return 0; }
 230     i_val += year.length;
 231     if (year.length==2) {
 232      if (year > 70) { year=1900+(year-0); }
 233      else { year=2000+(year-0); }
 234      }
 235     }
 236    else if (token=="MMM"||token=="NNN"){
 237     month=0;
 238     for (var i=0; i<MONTH_NAMES.length; i++) {
 239      var month_name=MONTH_NAMES[i];
 240      if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
 241       if (token=="MMM"||(token=="NNN"&&i>11)) {
 242        month=i+1;
 243        if (month>12) { month -= 12; }
 244        i_val += month_name.length;
 245        break;
 246        }
 247       }
 248      }
 249     if ((month < 1)||(month>12)){return 0;}
 250     }
 251    else if (token=="EE"||token=="E"){
 252     for (var i=0; i<DAY_NAMES.length; i++) {
 253      var day_name=DAY_NAMES[i];
 254      if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
 255       i_val += day_name.length;
 256       break;
 257       }
 258      }
 259     }
 260    else if (token=="MM"||token=="M") {
 261     month=_getInt(val,i_val,token.length,2);
 262     if(month==null||(month<1)||(month>12)){return 0;}
 263     i_val+=month.length;}
 264    else if (token=="dd"||token=="d") {
 265     date=_getInt(val,i_val,token.length,2);
 266     if(date==null||(date<1)||(date>31)){return 0;}
 267     i_val+=date.length;}
 268    else if (token=="hh"||token=="h") {
 269     hh=_getInt(val,i_val,token.length,2);
 270     if(hh==null||(hh<1)||(hh>12)){return 0;}
 271     i_val+=hh.length;}
 272    else if (token=="HH"||token=="H") {
 273     hh=_getInt(val,i_val,token.length,2);
 274     if(hh==null||(hh<0)||(hh>23)){return 0;}
 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)){return 0;}
 279     i_val+=hh.length;}
 280    else if (token=="kk"||token=="k") {
 281     hh=_getInt(val,i_val,token.length,2);
 282     if(hh==null||(hh<1)||(hh>24)){return 0;}
 283     i_val+=hh.length;hh--;}
 284    else if (token=="mm"||token=="m") {
 285     mm=_getInt(val,i_val,token.length,2);
 286     if(mm==null||(mm<0)||(mm>59)){return 0;}
 287     i_val+=mm.length;}
 288    else if (token=="ss"||token=="s") {
 289     ss=_getInt(val,i_val,token.length,2);
 290     if(ss==null||(ss<0)||(ss>59)){return 0;}
 291     i_val+=ss.length;}
 292    else if (token=="a") {
 293     if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
 294     else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
 295     else {return 0;}
 296     i_val+=2;}
 297    else {
 298     if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
 299     else {i_val+=token.length;}
 300     }
 301    }
 302   // If there are any trailing characters left in the value, it doesn't match

 303   if (i_val != val.length) { return 0; }
 304   // Is date valid for month?

 305   if (month==2) {
 306    // Check for leap year

 307    if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
 308     if (date > 29){ return 0; }
 309     }
 310    else { if (date > 28) { return 0; } }
 311    }
 312   if ((month==4)||(month==6)||(month==9)||(month==11)) {
 313    if (date > 30) { return 0; }
 314    }
 315   // Correct hours value

 316   if (hh<12 && ampm=="PM") { hh=hh-0+12; }
 317   else if (hh>11 && ampm=="AM") { hh-=12; }
 318   var newdate=new Date(year,month-1,date,hh,mm,ss);
 319   return newdate.getTime();
 320   }
 321  
 322  // ------------------------------------------------------------------

 323  // parseDate( date_string [, prefer_euro_format] )

 324  //

 325  // This function takes a date string and tries to match it to a

 326  // number of possible date formats to get the value. It will try to

 327  // match against the following international formats, in this order:

 328  // y-M-d   MMM d, y   MMM d,y   y-MMM-d   d-MMM-y  MMM d

 329  // M/d/y   M-d-y      M.d.y     MMM-d     M/d      M-d

 330  // d/M/y   d-M-y      d.M.y     d-MMM     d/M      d-M

 331  // A second argument may be passed to instruct the method to search

 332  // for formats like d/M/y (european format) before M/d/y (American).

 333  // Returns a Date object or null if no patterns match.

 334  // -------------------- ---------------------------------------------

 335  function parseDate(val) {
 336   if (val=="0000-00-00" || val==null || val=="x" || val=="X")  {
 337     d='12-12-12';
 338     return new d;
 339   } else {
 340    var preferEuro=(arguments.length==2)?arguments[1]:false;
 341    generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
 342    monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
 343    dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
 344    var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
 345    var d=null;
 346    for (var i=0; i<checkList.length; i++) {
 347     var l=window[checkList[i]];
 348     for (var j=0; j<l.length; j++) {
 349      d=getDateFromFormat(val,l[j]);
 350      if (d!=0) { return new Date(d); }
 351     }
 352    }
 353    return null;
 354   }
 355  }


[ Powered by PHPXref - Served by Debian GNU/Linux ]