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

title

Body

[close]

/javascript/ -> 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) {
  10     
  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   format=format+"";
 117   var result="";
 118   var i_format=0;
 119   var c="";
 120   var token="";
 121   var y=date.getYear()+"";
 122   var M=date.getMonth()+1;
 123   var d=date.getDate();
 124   var E=date.getDay();
 125   var H=date.getHours();
 126   var m=date.getMinutes();
 127   var s=date.getSeconds();
 128   var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
 129   // Convert real date parts into formatted versions

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

 172  // Utility functions for parsing in getDateFromFormat()

 173  // ------------------------------------------------------------------

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

 191  // getDateFromFormat( date_string , format_string )

 192  //

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

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

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

 196  // ------------------------------------------------------------------

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

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

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

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

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

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

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

 322  // parseDate( date_string [, prefer_euro_format] )

 323  //

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

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

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

 327  // y-M-d   MMM d, y   MMM d,y   y-MMM-d   d-MMM-y  MMM d

 328  // M/d/y   M-d-y      M.d.y     MMM-d     M/d      M-d

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

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

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

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

 333  // -------------------- ---------------------------------------------

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


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