-
Notifications
You must be signed in to change notification settings - Fork 0
/
jObject.js
1797 lines (1787 loc) · 66.2 KB
/
jObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright (C) 2012 by Pi Ke *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE*
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS*
* IN THE SOFTWARE. *
******************************************************************************
* File : jObject.js *
* Email : admin@pixelstech.net *
* Website : http://pixelstech.net *
* Description : jObject is a JavaScript API which helps users to define *
* JavaScrip behaviors systematically and consistently. It *
* creates many frequently used objects and it is also cross *
* platform compatible.It provides some frequently used *
* functions when doing web programming such as document *
* element css settings,event handling, form validation and *
* AJAX. All these processes are abstracted and encapsulated so*
* that users no need to know the internal working rationale of*
* these processes.It can largely release the developer from *
* the tedious JavaScript processing so that you can focus on *
* the functionalities of the web applications. *
* ============================================================================
* Version Date Author Comment
* ============================================================================
* v1.0.0 2011-12-05 Pi Ke Initial creation
* v1.1.0 2012-12-29 Pi Ke Add getMousePageX() and fix some bugs
* ============================================================================
* NOTICE : This file conforms to MIT free distribution license
******************************************************************************/
function $(selector){
return (new jObject(selector)).init();
}
/******************************************************************************
* Object : jObject
* Description : Main object in this module, it connects all parts of the
* module and define the behaviors of this module
* Parameters : selector -- id : #{id}
* name : ${name}
* class : .{class}
* tag : tagName
******************************************************************************/
function jObject(selector)
{
/* Define the HTML object which is currently selected */
this.element=document;
/* Define the HTML objects which are currently selected */
this.elements=null;
/* The selector specified by user */
this.selector=selector;
/* Current selected element type */
this.elementType=jObject.ELEMENT;
/* Applied to collection of elements. Specify the current selected
* element in the collection of elements */
this.elementIndex=-1;
}
jObject.prototype={
init:function(){
/* If the user put the element itself in */
if(typeof this.selector=="object"){
if(this.selector.length){
this.elements=this.selector;
}else{
this.element=this.selector;
}
}else if(this.selector!="undefined"&&this.selector!=null){
this.retrieveElement(this.selector);
}
return this;
},
/* Get element by ID */
_id:function(id){
if(this.element==document){
this.element=this.element.getElementById(id);
this.elementType=jObject.ELEMENT;
}else{
var errorObj=new ModalWindow("ID can be applied to only document object",ModalWindow.ERROR,"ID Error");
errorObj.init();
errorObj.show();
}
return this;
},
/* Get elements by name */
_name:function(name){
if(this.element==document){
this.elements=this.element.getElementsByName(name);
this.elementType=jObject.COLLECTION;
}
return this;
},
/* Get element which has unique name */
_unique_name:function(name){
this._name(name);
this.first();
this.elementType=jObject.ELEMENT;
return this;
},
/* Get elements by tag name */
_tagName:function(tagName){
this.elements=this.element.getElementsByTagName(tagName);
this.elementType=jObject.COLLECTION;
return this;
},
/********************************************************************
* Function : _class()
* Description : Get elements by class name.
* Parameters : className--class name to be matched
* tag--Optional, to narrow down macthed element,match
* elements with class name className and tag name
* tag
*
* NOTICE:
* This function is developed by Robert Nyman, http://www.robertnyman.com
* Code/licensing: http://code.google.com/p/getelementsbyclassname/
********************************************************************/
_class:function(className,tag){
this.elements=new Array();
if (document.getElementsByClassName) {
var elements = this.element.getElementsByClassName(className),
nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
current;
if(nodeName) {
for(var i=0, il=elements.length; i<il; i+=1){
current = elements[i];
if(nodeName.test(current.nodeName)) {
this.elements.push(current);
}
}
}else{
this.elements=elements;
}
}else if (document.evaluate) {
tag = tag || "*";
var classes = className.split(" "),
classesToCheck = "",
xhtmlNamespace = "http://www.w3.org/1999/xhtml",
namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
elements,
node;
for(var j=0, jl=classes.length; j<jl; j+=1){
classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
}
try {
elements = document.evaluate(".//" + tag + classesToCheck, this.element, namespaceResolver, 0, null);
}
catch (e) {
elements = document.evaluate(".//" + tag + classesToCheck, thie.element, null, 0, null);
}
while ((node = elements.iterateNext())) {
this.elements.push(node);
}
}else {
tag = tag || "*";
var classes = className.split(" "),
classesToCheck = [],
elements = (tag === "*" && this.element.all)? this.element.all : this.element.getElementsByTagName(tag),
current,
match;
for(var k=0, kl=classes.length; k<kl; k+=1){
classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
}
for(var l=0, ll=elements.length; l<ll; l+=1){
current = elements[l];
match = false;
for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
match = classesToCheck[m].test(current.className);
if (!match) {
break;
}
}
if (match) {
this.elements.push(current);
}
}
}
return this.elements;
},
/* Recursively get elements */
$:function(selector){
this.retrieveElement(selector);
return this;
},
/* Get first element of a collection of elements */
first:function(){
if(this.elements!=null){
this.element=this.elements[0];
this.elementType=jObject.ELEMENT;
this.elementIndex=0;
}
return this;
},
/* Get last element of a collection of elements */
last:function(){
if(this.elements!=null){
this.element=this.elements[this.elements.length-1];
this.elementType=jObject.ELEMENT;
this.elementIndex=this.elements.length-1;
}
return this;
},
/* Get previous element in the collection of elements */
previous:function(){
if(this.elementIndex>0){
this.element=this.elements[--this.elementIndex];
this.elementType=jObject.ELEMENT;
}else{
this.element=null;
}
return this;
},
/* Get next element in the collection of elements */
next:function(){
if(this.elementIndex<(this.elements.length-1)){
this.element=this.elements[++this.elementIndex];
this.elementType=jObject.ELEMENT;
}else{
this.element=null;
}
return this;
},
/****************************************************************
* Next section is to do the element operation, such as CSS style
* settings
****************************************************************/
/* Set style of element */
setStyle:function(style){
if(this.elementType==jObject.ELEMENT){
this.element.style.cssText=style;
}else{
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.cssText=style;
}
}
return this;
},
/* Get style of element */
getStyle:function(){
if(this.elementType!=jObject.COLLECTION){
var cssText="";
if(window.getComputedStyle){ //For Firefox and Chrome
var style=window.getComputedStyle(this.element,null);
var cssPair=new Array();
for(var i=0;i<style.length;i++){
var value=style.getPropertyValue(style.item(i).toUpperCase());
if(value!="undefined"&&value!=null){
cssPair.push(style.item(i)+":"+value);
}
}
cssText=cssPair.join(";");
}else if(this.element.currentStyle){ //For IE
var style=this.element.currentStyle;
var cssPair=new Array();
for(var prop in style){
cssPair.push(prop+":"+style[prop]);
}
cssText=cssPair.join(";");
}else{
//Only get the inline style
if(this.element.style&&this.element.style.cssText){
cssText=this.element.style.cssText;
}else{
cssText=this.element.getAttribute("style");
}
}
for(var key in DOMCSSMap){
cssText=cssText.replace(key,DOMCSSMap[key]);
}
return cssText.concat(";");
}else{
var errorObj=new ModalWindow("Cannot get style for a collection of elements at the same time",ModalWindow.ERROR,"Style getting error");
errorObj.init();
errorObj.show();
}
},
/* Get style for one single property */
getSingleStyle:function(name){
var value="undefined";
if(this.element.currentStyle){ //For IE
value=this.element.currentStyle[name];
}else if(window.getComputedStyle){ //For Firefox and Chrome
value=window.getComputedStyle(this.element,null).getPropertyValue(name);
}else{ //For others
var style=this.element.style.cssText.toUpperCase()+";";
var matchEx=new RegExp(";[\s| ]*"+name+".*?;","i");
if(matchEx.test(style)){
var str=matchEx.exec(style);
value=str.substring(str.indexOf(":")+1,str.length-1);
}
}
return value;
},
/* Add style to element */
addStyle:function(style){
var newStyle=this.getStyle();
this.setStyle(newStyle+style);
//alert(this.getStyle());
return this;
},
/* Remove style from element */
removeStyle:function(name){
var style=this.getStyle().toUpperCase();
var matchEx=new RegExp(";[\s| ]*"+name+".*?;","i");
if(matchEx.test(style)){
var str=matchEx.exec(style);
style=style.replace(str,"")+";";
}else if(style.indexOf(name.toUpperCase())==0){
var regEx=new RegExp("^"+name+".*?;","i");
style=style.replace(regEx,"");
}
this.setStyle(style);
return this;
},
/* Modify one style */
modifyStyle:function(name,newStyle){
var style=this.getStyle().toUpperCase();
var matchEx=new RegExp(";[\s| ]*"+name+".*?;","i");
if(matchEx.test(style)){
var str=matchEx.exec(style);
style=style.replace(str,";"+name.toUpperCase()+":"+newStyle+";");
}else if(style.indexOf(name.toUpperCase())==0){
var regEx=new RegExp("^"+name+".*?;","i");
style=style.replace(regEx,name.toUpperCase()+":"+newStyle+";");
}else{
style=style+name+":"+newStyle+";";
}
this.setStyle(style);
return this;
},
/* Set inner html of the specified element */
setHtml:function(html){
this.element.innerHTML=html;
return this;
},
/* Get inner html of the specified element */
getHtml:function(){
return this.element.innerHTML;
},
/* Append HTML to the specified element */
appendHtml:function(html){
this.element.innerHTML+=html;
return this;
},
/* Prepend html to the specified element */
prependHtml:function(html){
this.element.innerHTML=html+this.element.innerHTML;
return this;
},
/* Set inner text of the specified element */
setText:function(text){
this.element.innerText=text;
return this;
},
/* Get inner text of the specified element */
getText:function(){
return this.element.innerText;
},
/* Append text to the specified element */
appendText:function(text){
this.element.innerText+=text;
return this;
},
/* Prepend text to the specified element */
prependText:function(text){
this.element.innerText=text+this.element.innerText;
return this;
},
/* Set value of HTML element attribute */
setProperty:function(name,value){
this.element.setAttribute(name,value);
return this;
},
/* Get value of HTML element attribute */
getProperty:function(name){
return this.element.getAttribute(name);
},
/* Check whether a property is present or not */
hasProperty:function(name){
if(this.getProperty(name)!="undefined"&&this.getProperty(name)!=null){
return true;
}
return false;
},
/* Remove property from the element */
removeProperty:function(name){
if(this.hasProperty(name)){
this.element.removeAttribute(name);
}
return this;
},
/* Set value of HTML input element attribute */
setValue:function(value){
if(this.element!=null){
this.element.value=value;
}
return this;
},
/* Get value of HTML input element attribute */
getValue:function(){
if(this.element!=null){
return this.element.value;
}
return null;
},
/* Add event listener to the specified element */
addEventListener:function(type,handler){
switch(this.elementType){
case jObject.ELEMENT : EventHandler.addEventHandler(this.element,type,handler);break;
case jObject.COLLECTION : {
var size=this.elements.length;
for(var i=0;i<size;i++){
EventHandler.addEventHandler(this.elements[i],type,handler);
}
};break;
}
},
/* Remove event listener from the specified element */
removeEventListener:function(type,handler){
switch(this.elementType){
case jObject.ELEMENT : EventHandler.removeEventHandler(this.element,type,handler);break;
case jObject.COLLECTION : {
var size=this.elements.length;
for(var i=0;i<size;i++){
EventHandler.removeEventHandler(this.elements[i],type,handler);
}
};break;
}
},
/* Get class of an element */
getClass:function(){
return this.element.className;
},
/* Get the current selected element */
getElement:function(){
return this.element;
},
/* Get the current selected elements */
getElements:function(){
return this.elements;
},
/* Get element by id, class or tag name */
retrieveElement:function(selector){
if(selector!="undefined"&&selector!=null){
switch(selector.charAt(0)){
case "#":this.element=document.getElementById(selector.substr(1));this.elementType=jObject.ELEMENT;break;
case "$":this.elements=this.element.getElementsByName(selector.substr(1));this.elementType=jObject.COLLECTION;break;
case ".":this.elements=this._class(selector.substr(1));this.elementType=jObject.COLLECTION;break;
default:this.elements=this.element.getElementsByTagName(selector);this.elementType=jObject.COLLECTION;break;
}
}else{
var errorObj=new ModalWindow("Cannot find the element specified. Make sure you put the correct name",ModalWindow.ERROR,"No element found");
errorObj.init();
errorObj.show();
}
}
};
jObject.ELEMENT = 1;
jObject.COLLECTION = 2;
/* *
* DOM and CSS style attributes mapping
* key -- attributes in DOM object
* value -- css property
* */
var DOMCSSMap=new Array();
DOMCSSMap["backgroundAttachment"] = "background-attachment";
DOMCSSMap["backgroundColor"] = "background-color";
DOMCSSMap["backgroundImage"] = "background-image";
DOMCSSMap["backgroundPosition"] = "background-position";
DOMCSSMap["backgroundRepeat"] = "background-repeat";
DOMCSSMap["borderColor"] = "border-color";
DOMCSSMap["borderStyle"] = "border-style";
DOMCSSMap["borderTop"] = "border-top";
DOMCSSMap["borderRight"] = "border-right";
DOMCSSMap["borderLeft"] = "border-left";
DOMCSSMap["borderBottom"] = "border-bottom";
DOMCSSMap["borderTopColor"] = "border-top-color";
DOMCSSMap["borderRightColor"] = "border-right-color";
DOMCSSMap["borderBottomColor"] = "border-bottom-color";
DOMCSSMap["borderLeftColor"] = "border-left-color";
DOMCSSMap["borderTopStyle"] = "border-top-style";
DOMCSSMap["borderRightStyle"] = "border-right-style";
DOMCSSMap["borderBottomStyle"] = "border-bottom-style";
DOMCSSMap["borderLeftStyle"] = "border-left-style";
DOMCSSMap["borderTopWidth"] = "border-top-width";
DOMCSSMap["borderRightWidth"] = "border-right-width";
DOMCSSMap["borderBottomWidth"] = "border-bottom-width";
DOMCSSMap["borderLeftWidth"] = "border-left-width";
DOMCSSMap["borderWidth"] = "border-width";
DOMCSSMap["cssFloat"] = "float";
DOMCSSMap["fontFamily"] = "font-family";
DOMCSSMap["fontSize"] = "font-size";
DOMCSSMap["fontStyle"] = "font-style";
DOMCSSMap["fontVariant"] = "font-variant";
DOMCSSMap["fontWeight"] = "font-weight";
DOMCSSMap["letterSpacing"] = "letter-spacing";
DOMCSSMap["lineHeight"] = "line-height";
DOMCSSMap["listStyle"] = "list-style";
DOMCSSMap["listStyleImage"] = "list-style-image";
DOMCSSMap["listStylePosition"] = "list-style-position";
DOMCSSMap["listStyleType"] = "list-style-type";
DOMCSSMap["marginTop"] = "margin-top";
DOMCSSMap["marginRight"] = "margin-right";
DOMCSSMap["marginLeft"] = "margin-left";
DOMCSSMap["marginBottom"] = "margin-bottom";
DOMCSSMap["paddingTop"] = "padding-top";
DOMCSSMap["paddingRight"] = "padding-right";
DOMCSSMap["paddingLeft"] = "padding-left";
DOMCSSMap["paddingBottom"] = "padding-bottom";
DOMCSSMap["textAlign"] = "text-align";
DOMCSSMap["textDecoration"] = "text-decoration";
DOMCSSMap["textIndent"] = "text-indent";
DOMCSSMap["textTransform"] = "text-transform";
DOMCSSMap["verticalAlign"] = "vertical-align";
DOMCSSMap["whiteSpace"] = "white-space";
DOMCSSMap["wordSpacing"] = "word-spacing";
DOMCSSMap["zIndex"] = "z-index";
/************************************************************************
* Object : EventHandler
* Description : It is to define the cross browser compatible event handle
* object (BOM),it defines some methods related to client event
* such as addEventHandler and getEvent. All user related events
* such as mouse click, move, keydown, keypress can be handled
* by this object. It can also tess the mouse position and event
* target when a particular event trggered.
************************************************************************/
var EventHandler={
addEventHandler:function(element,type,handler){
if(element!=null){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent("on"+type,handler);
}else{
element["on"+type]=handler;
}
}
},
removeEventHandler:function(element,type,handler){
if(element!=null){
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}else if(element.detachEvent){
element.detachEvent("on"+type,handler);
}else{
element["on"+type]=null;
}
}
},
getEvent:function(event){
return event?event:window.event;
},
getTarget:function(event){
return event.target||event.srcElement;
},
getMouseX:function(event){
var e=EventHandler.getEvent(event);
return e.clientX;
},
getMouseY:function(event){
var e=EventHandler.getEvent(event);
return e.clientY;
},
getMouseScreenX:function(event){
var e=EventHandler.getEvent(event);
return e.screenX;
},
getMouseScreenY:function(event){
var e=EventHandler.getEvent(event);
return e.screenY;
},
getMousePageX:function(event){
var e=EventHandler.getEvent(event);
var x=e.pageX;
if(x===undefined){
x=e.clientX+(document.body.scrollLeft || document.documentElement.scrollLeft);
}
return x;
},
getMousePageY:function(event){
var e=EventHandler.getEvent(event);
var y=e.pageY;
if(y===undefined){
y=e.clientY+(document.body.scrollTop || document.documentElement.scrollTop);
}
return y;
},
getMouseButton:function(event){
if(document.implementation.hasFeature("MouseEvnts","2.0")){
return event.button;
}else{
switch(event.button){
case 0:
case 1:
case 3:
case 5:
case 7:
return 0; //Left button is clicked
case 2:
case 6:
return 2; //Right button is clicked
case 4:
return 1; //Middle button is clicked
}
}
},
getMouseWheelDirection:function(event){
if(event.wheelDelta)
return (event.wheelDelta>0)?1:-1; //1 indicates toword front of mouse,-1 toworm back of mouse
else
return (event.detail>0)?-1:1;
},
getKeyCode:function(event){
if(event.which){ //Firefox
return event.which;
}else if(typeof event.charCode=="number"){
return event.charCode;
}else{ //IE
return event.keyCode;
}
},
preventDefault:function(event){
if(event.preventDefault){
event.preventDefault();
}else{
event.returneValue=false;
}
},
stopPropagation:function(event){
if(event.stopPropagation){
event.stopPropagation();
}else{
event.cancelBubble=true;
}
},
getClipboardData:function(event){
var clip=(event.clipboardData||window.clipboardData);
return clip.getData("text");
},
setClipboardData:function(event,data){
if(event.clipboardData){
return event.clipboardData.setData("text/plain",data);
}else{
return window.clipboardData.setData("text",data);
}
}
};
/************************************************************************
* Object : Broswer
* Description : It is to define the cross browser compatible browser
* object model(BOM),it defines some properties related to
* browser such as browser width and height
************************************************************************/
var Browser={
/* Get browser window width */
getWidth:function(){
var windowWidth=window.innerWidth;
if(typeof windowWidth!="number"){
if(document.documentElement){
windowWidth=document.documentElement.clientWidth;
}else{
windowWidth=document.body.clientWidth;
}
}
return windowWidth;
},
/* Get browser window height */
getHeight:function(){
var windowHeight=window.innerHeight;
if(typeof windowHeight!="number"){
if(document.documentElement){
windowHeight=document.documentElement.clientHeight;
}else{
windowHeight=document.body.clientHeight;
}
}
return windowHeight;
},
/* Get scrolled region height */
getScrollTop:function(){
var scrollTop;
if(window.scrollY){
scrollTop=window.scrollY;
}else if(document.documentElement){
scrollTop=document.documentElement.scrollTop;
}else{
scrollTop=document.body.scrollTop;
}
return scrollTop;
},
/* Get scrolled region width */
getScrollLeft:function(){
var scrollLeft;
if(window.scrollX){
scrollLeft=window.scrollX;
}else if(document.documentElement){
scrollLeft=document.documentElement.scrollLeft;
}else{
scrollLeft=document.body.scrollLeft;
}
return scrollLeft;
},
/* Get mouse X position */
getMouseX:function(event){
var mouseX;
if(window.event){
mouseX=window.event.clientX;
}else{
mouseX=event.clientX;
}
return mouseX;
},
/* Get mouse Y position */
getMouseY:function(event){
var mouseY;
if(window.event){
mouseY=window.event.clientY;
}else{
mouseY=event.clientY;
}
return mouseY;
},
/* Get browser type , not recommend to use it */
getBrowserType:function(){
var agent=navigator.userAgent.toUpperCase();
if(agent.indexOf("MSIE 8.0")>0){
return "IE8";
}else{
if(agent.indexOf("MSIE 7.0")>0){
return "IE7";
}else{
if(agent.indexOf("FIREFOX")>0){
return "FIREFOX";
}else{
if(agent.indexOf("CHROME")>0){
return "CHROME";
}else{
if(agent.indexOf("SAFARI")>0){
return "SAFARI";
}else{
return "UNKNOWN";
}
}
}
}
}
}
};
/************************************************************************
* Object : System
* Description : System functions like show element and hide element
************************************************************************/
var System={
/* Print out some message to the browser window */
out:{
println:function(str){
document.write(str+"<br />");
},
print:function(str){
document.write(str);
}
},
/* Print out some ERROR message to the browser window */
err:{
println:function(str){
document.write("<font color='RED'>"+str+"</font><br />");
},
print:function(str){
document.write("<font color='RED'>"+str+"</font>");
}
},
/*******************************************************************
* Function : trim()
* Description : Remove spaces before and after a string or new lines
* after the string
* Parameters : str--String to be trimed
* Return : The trimed string
*******************************************************************/
trim:function(str){
return str.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
},
/*******************************************************************
* Function : showItem()
* Description : Display the specified element on the browser window
* Parameters : id--Element id or the element itself
*******************************************************************/
showItem:function(id){
var obj=null;
if(typeof id=="string"){
obj=$("#"+id).getElement();
}else{
obj=id;
}
obj.style.visibility="visible";
obj.style.display="block";
},
/*******************************************************************
* Function : hideItem()
* Description : Hide the specified element on the browser window
* Parameters : id--Element id or the element itself
*******************************************************************/
hideItem:function(id){
var obj=null;
if(typeof id=="string"){
obj=$("#"+id).getElement();
}else{
obj=id;
}
obj.style.display="none";
obj.style.visibility="hidden";
},
/*******************************************************************
* Function : toggleItem()
* Description : Toggle to show or hide the specified element on the
* browser window
* Parameters : id--Element id or the element itself
*******************************************************************/
toggleItem:function(id){
var obj=null;
if(typeof id=="string"){
obj=$("#"+id).getElement;
}else{
obj=id;
}
if(obj.style.visibility=="hidden"||obj.style.visibility=="undefined"){
this.showItem(obj);
}else{
this.hideItem(obj);
}
},
/*******************************************************************
* Function : scrollObject()
* Description : Scroll an element with different directions
* Parameters : obj--Object to be scrolled
* dir--Direction of scrolling, the direction can be
* top, left, right or bottom
* start--start position of the object
* mode--Auto mode or non-auto mode???
* step--Step size of the scrolling
*******************************************************************/
scrollObject:function(obj,dir,start,mode,step){
if(mode!="undefined"&&mode=="auto"){
if(obj.style.top==""||obj.style.top==null){
obj.style.top=0+"px";
}
switch(dir){
case "top":obj.style.top=parseInt(start)+parseInt(step)+"px";break;
case "left":obj.style.left=parseInt(start)+parseInt(step)+"px";break;
case "right":obj.style.right=parseInt(start)+parseInt(step)+"px";break;
case "bottom":obj.style.bottom=parseInt(start)+parseInt(step)+"px";break;
default:alert("Undefined scroll direction");break;
}
}else{
var amount=0;
if(document.documentElement&&Browser.getBrowserType()!="Chrome"){
switch(dir){
case "top":amount=document.documentElement.scrollTop;break;
case "left":amount=document.documentElement.scrollLeft;break;
case "right":amount=document.documentElement.scrollRight;break;
case "bottom":amount=document.documentElement.scrollBottom;break;
default:alert("Undefined scroll direction");break;
}
}else if(document.body){
switch(dir){
case "top":amount=document.body.scrollTop;break;
case "left":amount=document.body.scrollLeft;break;
case "right":amount=document.body.scrollRight;break;
case "bottom":amount=document.body.scrollBottom;break;
default:alert("Undefined scroll direction");break;
}
}else{
alert("Scroll is not supported");
}
this.scrollObject(obj,dir,start,"auto",amount);
}
}
};
/************************************************************************
* Object : Checker
* Description : Checker class is used for the form validation
************************************************************************/
var Checker={
/*******************************************************************
* Function : isEmpty()
* Description : Check whether a form field is empty or not
* Parameters : field--Field value or the field element, can be textbox
* textarea
*******************************************************************/
isEmpty:function(field){
if(typeof field=="object"){
if(field.value==""){
return true;
}
}else{
if(field==""){
return true;
}
}
return false;
},
/*******************************************************************
* Function : isEmail()
* Description : Check whether a string is an Email or not, this is
* to verify the form field which requires a user to enter
* a valid email.
* Parameters : email--The email to be verified
*******************************************************************/
isEmail:function(email){
var email_reg=/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
return email_reg.test(email);
},
/*******************************************************************
* Function : isPhone()
* Description : Check whether a string is a phone number ir not
* Parameters : phone-Phone number to be verified
*******************************************************************/
isPhone:function(phone){
var phone_reg=/^(\+[0-9]{2}\s*)*[0-9]+$/;
return phone_reg.test(phone);
},
/*******************************************************************
* Function : isZipCode()
* Description : Check whether a string is a zipcode or not
* Parameters : zipcode--ZipCode to be verified
*******************************************************************/
isZipCode:function(zipcode){
var zip_reg=/^[0-9]+$/;
return zip_reg.test(zipcode);
},
/*******************************************************************
* Function : isEqual()
* Description : Check whether two values are equal or no. It is useful
* when checking two passwords or two emails are the same
* or not when registering account
* Parameters : value1--First value to be checked
* value2--Second value to be checked
*******************************************************************/
isEqual:function(value1,value2){
return (value1==value2);
},
/*******************************************************************
* Function : isInRange()
* Description : Check whether a value is in the minimum and maximum
* length range. It is useful when one wants to restrict
* the length of the username or password or something
* else
* Parameters : value--Value to be verified
* min_len--Minimum length of the value required
* max_len--Maximum length of the value required
*******************************************************************/
isInRange:function(value,min_len,max_len){
return (value.length>=min_len&&value.length<=max_len);
},
/*******************************************************************
* Function : isSelected()
* Description : Check whether a radio button or checkbox is checked
* Parameters : field--Field name or field element
*******************************************************************/
isSelected:function(field){
var obj=null;
if(typeof field=="string"){
obj=$("$"+field).first().getElement()
}else{
obj=field;
}
return obj.checked;
},
/*******************************************************************
* Function : isNumber()
* Description : Check whether a value is a number or not
* Parameters : value-- Value to be verified
*******************************************************************/
isNumber:function(value){
var number_reg=/^[0-9]+\.?[0-9]*$/;
if(typeof value=="number"||number_reg.test(value)){
return true;
}
return false;
}
};
/************************************************************************
* Object : Calendar
* Description : An object related Date and Calendar. It can help users
* retrieve some frequently needed values and also can be
* used to do Date comparisons. Also, it can create a GUI
* for any date.