function $() {
  
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push(element);
  }
  
  return results.length < 2 ? results[0] : results;
}

document.getElementsByClassName = function(className, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  var elements = [];
  for(i=0;i<children.length;i++) {
  	child=children[i];
  	if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) elements.push(child);
  }  
  return elements;
  
}

function showControls(soid) {
	div=document.getElementById(soid+"controls"); 
	div.style.display="block"; 
	b1 = div.childNodes.item(1); if (b1) b1.style.display="inline";
	b2 = div.childNodes.item(2); if (b2) b2.style.display="inline";
	return div;
}
function showAllControls(soid) {
	div=showControls(soid); 
	div.childNodes.item(3).style.display="inline";
}

 function xbind_upload(input,title,aurl,maxfilesize,param,currentfile,currentlink) {
 	  window.xbinduploadinput=input;
 	  w=window.open("","upload","resizable=0, status=0, dependent=1, left="+(screen.width/2-170)+", top="+(screen.height/2-70)+", width=320, height=140",1);
 	  
 	  w.document.write("<HTML><HEAD><TITLE>Upload ( max "+Math.round(maxfilesize/1000,0)+" Kb )</TITLE><STYLE>table{FONT:12px MS Sans Serif}</STYLE></HEAD><BODY bgcolor=E9E8D3 ><FORM ACTION='"+aurl+"' ENCTYPE='multipart/form-data' method=POST><input type=hidden name='PARAM' value='"+param+"'/><input type=hidden id=inp_MAX_FILE_SIZE name='MAX_FILE_SIZE' value='"+maxfilesize+"'><TABLE>"+(currentfile?"<TR><td></td><TD><a href='"+currentlink+"'>"+currentfile+"</a></td></tr><tr><td></td><td>remove file</td></TR>":"")+"<TR><TD></td><TD>"+title+"</TD></TR><TR><TH align=left>File name:</TH><TD><INPUT TYPE=file NAME='file' id='file' onchange='this.form.submit();'></TD></TR></TABLE></FORM></BODY></HTML>");
 	  w.document.close();
 	  w.document.oncontextmenu=function() { return false; }
 }
 
 function xbind_uploadfinished(soid,original,resourcefile) {  	
 	window.xbinduploadinput.value=original;
 	rs=document.getElementById(soid);  	
if (rs) rs.src=resourcefile+"?"+reqdtm();	
window.xbinduploadinput.form.onchange();
 }


/** USAGE:
*
*	<body onload="javascript: XMask.Initialize()">
*
*		<input disabled="disabled" class="xmask xm_date"/>
*
*	</body>
*
*/

if ( typeof(XMask) === 'undefined' ) XMask = new Object();

XMask = {

   masks: {
   	  INT: {
   	    maxlen: 12,
   	    regex: /\d/
   	  },   	  
   	  FLOAT: {   	   
   	    regex: /[\d\.\,]/
   	  },   	  
      DATE: {
         format: '  /  /    ',
         regex:  /\d/
      },
      TIMESTAMP: {
         format: '    -  -  _  :  :  ',
         regex:  /\d/
      },
      STRING: {
      	regex: /[^<>;]/
      },
      TIME: {
         format: '  :  :  ',
         regex:  /\d/
      },
      username: {
        maxlen: 20,
      	regex: /\w/
      },
      password: {
        maxlen: 16,
      	regex: /\w/
      },
      email: {
        maxlen: 40,
      	regex: /[\w@\.\-]/
      },
      name: {
        maxlen: 30,
        regex: /[^\!\"\£\$\%\^\&\*\(\)\@\]\[\}\{\>\<\.\,\?\/\\\~\+\=\_\-\#\|]/
      },
      alphanum: {
      	regex: /\w/
      }
   },

   Initialize: function() {   	   		
      if ( document.getElementsByClassName ) { // Requires the Prototype library      	
      	 inputs=document.getElementsByClassName('xmask');   
      	 focused = false;   	 
      	 for(i=0;i<inputs.length;i++) {      	 	        	 	
      	 	input=inputs[i];         	 	
      	 	var match = /xm_(\w+)/.exec(input.className);      	 	
		    if ( match.length == 2 && XMask.masks[match[1]] ) {  	 	
	            input.mask = XMask.masks[match[1]];      	 	
	      	 	
	      	 	input.onkeypress=function(event) { return XMask.Apply(this,(event || window.event)); }      
	      	 	input.onkeydown=function(event) { return XMask.Apply2(this,(event || window.event)); } 	      	 	
	      	 	if (input.mask.format) {					
		      	 	if (!input.value ) input.value=input.mask.format.replace("_"," ");
		      	}
		      	if (input.mask.maxlen) {
		      		input.maxlen=input.mask.maxlen;
		      	}
	      	 	input.disabled=false;	      	 	
	      	 	if (input.type=="text" && !focused) {
	      	 		focused = true;
	      	 		input.focus();
	      	 	}
	      	 	//if (input.mask.format) input.style.fontFamily="Courier New";
	      	 }
         }
      	 i = document.getElementById('focusinput');
      	 if (i != undefined ) i.focus(); 
      }
   },
   

   Apply: function(object, event) {
      //return true;
      var key  = XMask.getKey(event);
      if ( XMask.isPrintable(key) ) {
         var ch      = String.fromCharCode(key);
         var str     = object.value + ch;
         var pos     = object.selectionStart;
         if (object.mask.format && pos >= object.mask.format.length) return false;
         if (!object.mask.format && object.maxlen && object.value.length >= object.maxlen) return false;
         if (object.mask.regex.test(ch) && (!object.inputmask || object.inputmask.test(ch))) {
         	if (!object.mask.format) return true;
         	if (object.mask.format) while( object.mask.format.charAt(pos ) != ' ' && pos < object.mask.format.length ) pos++;	               
       		object.value=object.value.substr(0,pos)+ch+object.value.substr(pos+1);
       		pos++;
         	if (object.mask.format) while( object.mask.format.charAt(pos ) != ' ' && pos < object.mask.format.length ) pos++;	               
       		object.selectionStart=pos;
       		object.selectionEnd=pos+1;	       	
         }           
         return false;
      } else if (object.mask.format) {
      	
      	if (key==8) {
   			pos=object.selectionStart;
      		if (pos>0) {
      		    pos--;
	       		ch=object.mask.format.charAt(pos); if (ch=="_") ch=" ";
	       		object.value=object.value.substr(0,pos)+ch+object.value.substr(pos+1);
	       		object.selectionStart=pos;
	       		object.selectionEnd=pos+1;	       	
	       	}
	       	
    		return false;
      	}
      }  
      return true;
   },
   
   Apply2: function(object, event) { 
      var result=true;      
      var key  = XMask.getKey(event);
	  var pos=object.selectionStart;
	  if (object.mask.format) {
	      if (key==46) {
	      		object.value=object.mask.format.replace("_"," ");
	      		pos=0;
	    		result=false;
	      }
	      if (key==39 || key==37) {
				result=false;
				if (key==39 && pos<object.value.length) {
					pos++;
	         		while( object.mask.format.charAt(pos ) != ' ' && pos < object.mask.format.length ) pos++;	               
				}
				if (key==37 && pos>0) {
					pos--;
	         		while( object.mask.format.charAt(pos ) != ' ' && pos >0 ) pos--;	               
				}
		  }      
      } 
      if (result) return result;
 		object.selectionStart=pos;
 		object.selectionEnd=pos+1;	       	      
      return result;
   },

	ApplyChange: function(object,event) {
		value="";
		for(i=0;i<object.value.length;i++) {
			ch=object.value.charAt(i);
			if (!(object.mask.regex.test(ch) && (!object.inputmask || object.inputmask.test(ch)))) ch=" ";
			value=value+ch;
			
		}
		object.value=value;
	},
   /* Returns true if the key is a printable character.
    */
   isPrintable: function(key) {
      return ( (key >= 32 /*&& key < 127) || (key > 255 */));
   },

   /* Returns the key code associated with the event.
    */
   getKey: function(e) {
      return window.event ? window.event.keyCode
           : e            ? e.which
           :                0;
   }
};
