/* Web Desktop API - a web desktop toolkit
Copyright 2007 Hakan ALBAG (hakan@albag.net)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

last edit : 09.Sep.2007 13:00 CET

*/

// variables needed for Drag&Drop
var dragWinDiv 		= null;
var dragMouseOffset = null;
var dragInitPos		= null;

var resizeWinDiv	= null;
var resizeMousePos = null;

var resizeInitWidth  = 0;
var resizeInitHeight = 0;

var resizeDirection = 0; // 1,2,3 => EAST,SOUTH-EAST,SOUTH


function Window(id,title,options){		
	this.id 		= id;	
	this.title 		= title;	
	// options handled in init()
	
	this.mainDiv  	= null;		
	this.titleDiv  	= null;		
	this.contentDiv = null;	
	this.rightBarDiv = null;	
	this.innerWinDiv = null;	
	this.editable	= false;	
	this.editMode   = false;
	this.titleEditMode = false;
	this.closable   = true;
	this.movable	= true;
	this.foldable	= true;
	this.isFolded 	= false;	//dblclck title
	this.dimensions = {"width":500,"height":300}
	this.focusSeq	= 1;
	
	this.onChange	= null;
	this.onClose 	= null;
	this.onMove		= null;
	this.onEnterEditor = null;	
	
	//methods
	this.init = init;
	this.setContent = setContent;
	this.setTitle = setTitle;
	this.setPosition = setPosition;
	this.setDimensions = setDimensions;
	this.setVisible	= setVisible;
	this.focus		= focus;
	this.saveEditorContent = saveEditorContent;
	this.getEditor	= getEditor; 	// returns reference to editor (textarea) element, only if exists
	this.createEditor = createEditor;
	this.createTitleEditor = createTitleEditor;	
	
	this.createInnerWindowDiv = createInnerWindowDiv; // private
	
	//call init method
	this.init();
	
	function init(){					
		
		this.editable = (options && options.editable)?true:false;
		this.closable = (options && options.closable==false)?false:true;
		this.movable  = (options && options.movable==false)?false:true;
		this.foldable = (options && options.foldable==false)?false:true;			
		
		this.dimensions.width = (options && options.width)?options.width:500;
		this.dimensions.height = (options && options.height)?options.height:300;				
		this.focusSeq	= (options && options.focusSeq)?parseInt(options.focusSeq):1;							
		
		this.onChange = (options && options.onChange)?options.onChange:function(){};				
		this.onMove = (options && options.onMove)?options.onMove:function(){};				
		this.onClose  = (options && options.onClose)?options.onClose:function(){};				
		this.onEnterEditor = (options && options.onEnterEditor)?options.onEnterEditor:function(){};				
		
		if(this.id.length==0)
			this.id = "win"+Math.round(Math.random()*10000);	// could be better
				
					
		this.mainDiv = document.createElement("div");		
		this.mainDiv.id = this.id; 			
		this.mainDiv.className="window";								
		
		this.mainDiv.style.width= this.dimensions.width;
		this.mainDiv.style.height= this.dimensions.height
		
		this.mainDiv.style.top= (options && options.y)?options.y:((WindowMng.windows.length%15+1)*22+30);				
		this.mainDiv.style.left= (options && options.x)?options.x:((WindowMng.windows.length%15+1)*22+50);
		
		this.mainDiv.oncontextmenu=showWindowContextMenu;
		this.mainDiv.onmouseover = function(event){contextEnable=true;}
		this.mainDiv.onmouseout = function(event){contextEnable=false;}
		
		
		this.titleDiv = document.createElement("div");
		this.titleDiv.className="title_bar";
		
		title_left_corner_div = document.createElement("div");
		title_left_corner_div.className="title_bar_left_corner";		
		this.titleDiv.appendChild(title_left_corner_div);
		
		this.titleTextDiv = document.createElement("div");
		this.titleTextDiv.className="title_bar_content";
		this.titleTextDiv.style.width = this.dimensions.width-20;
		this.titleTextDiv.innerHTML = this.title;

		/* edit title*/
		this.titleTextDiv.ondblclick = function(event){
			var win = WindowMng.getWinById(this.parentNode.parentNode.id);
			win.createTitleEditor();						
		}
								
		this.titleDiv.appendChild(this.titleTextDiv);
		
		var title_right_corner_div = document.createElement("div");
		title_right_corner_div.className="title_bar_right_corner";
		title_right_corner_div.style.left=this.dimensions.width-10;
		this.titleDiv.appendChild(title_right_corner_div );			
		
		if(this.movable){
			this.titleDiv.onmousedown = function(event){
				var w = WindowMng.getWinById(this.parentNode.id);
				if(w.titleEditMode)
					return;
				dragWinDiv 		= this.parentNode;	
				dragInitPos		= {"x":dragWinDiv.style.left,"y":dragWinDiv.style.top};				
				dragMouseOffset = getMouseOffset(dragWinDiv,event);			
			}
			this.titleDiv.onmouseup = function(event){
				if(typeof closeGonnaBeClicked != "undefined" && closeGonnaBeClicked)
					return;
				if(!dragWinDiv)
					return;
				
				dragWinDiv.style.opacity=1;
				dragWinDiv.style.filter="alpha(opacity=100)";
				w = WindowMng.getWinById(dragWinDiv.id);
				
				/* if(dragInitPos.x!=dragWinDiv.style.left && dragInitPos.y!=dragWinDiv.style.top) */
					/* trigger onMove even if its not moved really, to store focusSequence */
					w.onMove(w);
				dragWinDiv = null;			
				dragInitPos = null;
				
			}
		}else{	// non-movable
			this.titleDiv.style.cursor="default";
		
		}			
		
		if(this.closable){
			var closeDiv = document.createElement("div");		
			closeDiv.className="btn_close";
			closeDiv.style.width=14;
			closeDiv.style.height=14;
			closeDiv.style.position = "absolute";						
			closeDiv.style.left= parseInt(this.mainDiv.style.width)-20;
			closeDiv.style.top= 2;		
			closeDiv.style.cursor = "pointer";
			closeDiv.innerHTML = " &nbsp;";
			closeDiv.onmousedown=function(){
				closeGonnaBeClicked = true; // has global scope
			}
			
			closeDiv.onclick = function(){
							
				if(confirm("Close?")){
					this.parentNode.parentNode.style.display="none";												
					win = WindowMng.getWinById(this.parentNode.parentNode.id);													
					win.onClose(win);				
					WindowMng.removeWinById(this.parentNode.parentNode.id);								
				}				
				closeGonnaBeClicked = false;
			}
			this.titleDiv.appendChild(closeDiv);
		}
		
		this.mainDiv.appendChild(this.titleDiv);		
		
		this.createInnerWindowDiv();		
					
		this.mainDiv.onmousedown = function(event){
			WindowMng.getWinById(this.id).focus();	// ID of the mainDIV, comes from event
		};
		
		document.onmousemove = mouseMove;		
		document.body.appendChild(this.mainDiv);					
		WindowMng.addWin(this);
	}
	

	function createInnerWindowDiv(){
		var divContent  = document.createElement("div");		
		divContent.className = "content";		
		this.contentDiv = divContent;				
		
		this.contentDiv.ondblclick=function(event){
			var win = WindowMng.getWinById(this.parentNode.parentNode.parentNode.id);
			win.createEditor();
		}
		
		var divBottom 	= document.createElement("div");
		divBottom.className = "bottomBar";
		setResizeListener(divBottom,3);
		
		var divInnerLeft = document.createElement("div");
		divInnerLeft.className = "innerLeftGroup";
		divInnerLeft.appendChild(divContent);
		divInnerLeft.appendChild(divBottom);
				
						
		var divRight	= document.createElement("div");
		divRight.className = "rightBar";
		this.rightBarDiv = divRight;		
		setResizeListener(this.rightBarDiv,1);
		
		
		var divCorner 	= document.createElement("div");
		divCorner.className = "resizeCorner";
		setResizeListener(divCorner,2);

		var divInnerRight = document.createElement("div");
		divInnerRight.className = "innerRightGroup";
		divInnerRight.appendChild(divRight);
		divInnerRight.appendChild(divCorner);
		
		var divInnerWin = document.createElement("div");
		divInnerWin.className="innerWindow";
		this.innerWinDiv = divInnerWin;
		this.innerWinDiv.appendChild(divInnerLeft);
		this.innerWinDiv.appendChild(divInnerRight);				
		
		this.setDimensions(this.dimensions.width,this.dimensions.height);
		this.mainDiv.appendChild(divInnerWin);						
	}
	
	
	
	function setResizeListener(div,allowedDirection){
		
		div.onmousedown = function(event){						
			if(!event)
				event = window.event;			
			resizeDirection	= allowedDirection;
			resizeInitWidth  = parseInt(this.parentNode.parentNode.parentNode.clientWidth);
			resizeInitHeight = parseInt(this.parentNode.parentNode.parentNode.clientHeight);
			resizeWinDiv 	= this.parentNode.parentNode.parentNode;				
			resizeWinDiv.style.opacity=0.7;
			resizeWinDiv.style.filter="alpha(opacity=70)";			
			resizeMousePos	= mouseCoords(event);												
		}
		
		div.onmouseup = function(event){								
			
			var w = WindowMng.getWinById(this.parentNode.parentNode.parentNode.id);
			if(resizeWinDiv!=null){			
				resizeWinDiv.style.opacity=1.0;
				resizeWinDiv.style.filter="alpha(opacity=100)";
				resizeWinDiv = null;		
			}
			resizeMousePos = null;			
			resizeDirection	= 0;
			w.onMove(w);
		}		
	}
	
	/* replace content with editor*/
	function createEditor(){
		var w = this; 
		
		if(w.dimensions.width<280){
			w.setDimensions(280,w.dimensions.height);
		}
		if(w.dimensions.height<100){		
			w.setDimensions(w.dimensions.width,100);
		}
		
		if(w.editable && !w.editMode){				
			w.editMode = true;							
			
			var txt = document.createElement("textarea");
			txt.id="editor"+w.id;
			txt.className = "editor";										
			txt.style.width = w.contentDiv.clientWidth-18;
			
			
			if(document.all){			
				txt.style.height = w.contentDiv.clientHeight-32;				
			}else{				
				txt.style.height = w.contentDiv.clientHeight-34;				
			}					
			tempContent = w.contentDiv.innerHTML;
																		
			txt.value=html2txt(tempContent);				
									
			btn = document.createElement("button");				
			btn.className="editor";				
			btn.onclick= function(event){					
				var win = WindowMng.getWinById(this.parentNode.parentNode.parentNode.parentNode.id);
				win.saveEditorContent();
				win.editMode = false;
				win.onChange(win);
				
				// depedency on tooltip API					
				showTooltip("You can also use <b>double-enter</b> to submit your note!",parseInt(win.mainDiv.style.left)+20,parseInt(win.mainDiv.style.top)+parseInt(win.mainDiv.style.height)-45,0.6);
			}
			
			txt.style.marginLeft = (document.all)?6:4;
			btn.style.marginLeft = (document.all)?6:4;
			
			firstEnterFlag = false;
			txt.onkeypress = function(event){
							
				keynum=getPressedKeyCode(event);
				
				if(keynum==13){ //enter											
					if(firstEnterFlag){		//double-enter -> saveContent					
						firstEnterFlag = false;							
						var win = WindowMng.getWinById(this.parentNode.parentNode.parentNode.parentNode.id);
						editor = win.getEditor();
						if(!document.selection){// remove new line - MOZ only
							pos = editor.selectionStart-1;
							if(editor.value.length>pos){
								if(editor.value.charAt(pos)=='\n'){										
									editor.value=editor.value.substr(0,pos)+editor.value.substr(pos+1);
								}; 
							}
							
						}
						win.saveEditorContent();
						win.editMode = false;
						win.onChange(win);
						
					}else{
						firstEnterFlag = true;
						window.setTimeout("firstEnterFlag = false",400);
					}
				}				
			}
			
			
			w.contentDiv.innerHTML ="";					
			w.contentDiv.appendChild(txt);
			w.contentDiv.appendChild(btn); 						
			w.onEnterEditor(w);
			txt.focus();
		}
	
	
	}
	
	// context menu for editor text area
	function showEditorContextMenu(event){		
		return true;		
	}
	
	// context menu for window
	function showWindowContextMenu(event){		
		contextShow(event);
		return false;
	}
	
	/* title editor */
	function createTitleEditor(){
				
		var w = this;
		
		if(w.dimensions.width<280 || w.dimensions.height<100){		
			w.setDimensions(280,100);
		}
		
		if(w.titleEditMode)
			return;
		titleInput = document.createElement("input");			
		titleInput.type="text";
		titleInput.className="title_bar_content";
		titleInput.style.width=(parseInt(w.titleTextDiv.style.width)-15)+"px";
		titleInput.style.border="none";
		titleInput.style.height="15px";
		titleInput.value = w.titleTextDiv.innerHTML;
		
		titleInput.onblur=function(event){				
			var pw = WindowMng.getWinById(this.parentNode.parentNode.parentNode.id);
			pw.setTitle(htmlentities(this.value));
			pw.titleEditMode = false;
			pw.onChange(pw);
		}
		
		titleInput.onkeypress=function(e){				
			var keyCode = getPressedKeyCode(e);																			
			if(keyCode==13){ //enter
				this.blur();					
			}else if(keyCode==9){	//TAB						
				var win = WindowMng.getWinById(this.parentNode.parentNode.parentNode.id);
				win.createEditor();
				this.blur();
				document.getElementById("editor"+win.id).focus();
			}
		}
		
		w.titleTextDiv.innerHTML = "";
		w.titleTextDiv.appendChild(titleInput);
		titleInput.focus();		
		titleInput.select();
		w.titleEditMode = true;		
	
	}
	
	function setContent(html){	
		this.contentDiv.innerHTML = html;	
	}	
	
	function getEditor(){
		
		if(this.editable && this.editMode){		
			return document.getElementById("editor"+this.id);
		}
		return null;
	}
	
	function setTitle(titleTxt){	
		this.title = titleTxt;		
		this.titleTextDiv.innerHTML = this.title;	
	}	
	
	function saveEditorContent(){
		var editor = document.getElementById("editor"+this.id);
		editor.value = editor.value.substring(0,2000);
		var content = editor.value;
		this.setContent(txt2html(content));	
	}
	
	function setDimensions(width,height){		
								
		this.dimensions.width = width;
		this.dimensions.height= height;
		this.mainDiv.style.width  = width;
		this.mainDiv.style.height = height;
				
		this.titleTextDiv.style.width = this.dimensions.width-20;				
		this.titleDiv.childNodes[2].style.left=this.dimensions.width-10; // title right corner	
		if(this.titleDiv.childNodes.length==4)
			this.titleDiv.childNodes[3].style.left=this.dimensions.width-20; // close div		
		
		if(document.all){
			this.contentDiv.style.height = this.dimensions.height-(20+4); //20 title, 4 bottom
			this.contentDiv.style.width  = this.dimensions.width-6;
			
		}else{
			this.contentDiv.style.height = this.dimensions.height-(20+14); //20 title, 4 bottom
			this.contentDiv.style.width  = this.dimensions.width-16;
		}
		
		this.rightBarDiv.style.height = this.dimensions.height-(20+4);//20 title, 4 bottom	
		this.innerWinDiv.style.width  = (document.all)?this.dimensions.width:this.dimensions.width-2;
		this.innerWinDiv.style.height = this.dimensions.height-20;		
		this.innerWinDiv.childNodes[0].style.width= this.dimensions.width-6;		//left group
		
	}

	function setPosition(x,y){
		this.mainDiv.style.left=x;
		this.mainDiv.style.top=y;		
	}

	function setVisible(b){
		this.mainDiv.style.display=(b)?"block":"none";		
	}
	
	function focus(){				
		
		WindowMng.focusWin(this);				
	}
	
	
	// detect mouse motion 
	function mouseMove(event){
		event	= event || window.event;
		var mousePos = mouseCoords(event);									
			
		if(dragWinDiv){		
			dragWinDiv.style.opacity=0.7;
			dragWinDiv.style.filter="alpha(opacity=70)";
			
			dragWinDiv.style.top      = (mousePos.y - dragMouseOffset.y);
			dragWinDiv.style.left	  = (mousePos.x - dragMouseOffset.x);		
			return false;
		}
		else if(resizeWinDiv){		
			
			var w = resizeInitWidth+mousePos.x - resizeMousePos.x;
			var h = resizeInitHeight+mousePos.y - resizeMousePos.y;						
			
			var limitsReached = (w<=100||w>=500||h<=45||h>=500);			

			// down limits
			if(w<=100)
				w = 100;
			if(h<=45)
				h = 45;
			
			//upper limits
			if(w>=500)
				w = 500;
			if(h>=500)
				h = 500;
			
				
			var win = WindowMng.getWinById(resizeWinDiv.id);
			switch(resizeDirection){
				case 1:	// east
					win.setDimensions(w,win.dimensions.height); break;
				case 2: // south-east
					win.setDimensions(w,h); break;
				case 3: // south
					win.setDimensions(win.dimensions.width,h); break;
			}
			
			
			if(limitsReached){
				// finish resize event - save session
				/* copied from onmouseup */
				if(resizeWinDiv!=null){			
					resizeWinDiv.style.opacity=1.0;
					resizeWinDiv.style.filter="alpha(opacity=100)";
					resizeWinDiv = null;		
				}
				resizeMousePos = null;			
				resizeDirection	= 0;
				win.onMove(win);
	
			}
			
			return false;		
		}

	}	
	
	function getMouseOffset(target,event){
		event = event || window.event;		
		var mousePos  = mouseCoords(event);		
		return {x:mousePos.x - parseInt(target.style.left), y:mousePos.y - parseInt(target.style.top)};
		
	}
	
	function html2txt(html){
		var regex_html = new RegExp("<([A-Z][A-Z0-9]*)\\b[^>]*>(.*?)</\\1>|<([A-Z][A-Z0-9]*)\\b[^>]*/>","gim");		
		var res = regex_html.exec(html);
		
		var htmlClone = html;
		
		htmlClone = htmlClone.replace(/<br>/gim,"\n");		
		
		while(res){			
			if(res[1]=='a'){ // remove A 
				htmlClone = htmlClone.replace(res[0],res[2]);				
			}else
				htmlClone = htmlClone.replace(res[0],"");	// delete anything else
			res = regex_html.exec(html);			
		}
		
		htmlClone = htmlReverseentities(htmlClone);
		return htmlClone;			
	}
		
	
	function txt2html(txt){
		// replace all html entities
		txt = htmlentities(txt);
		
		// catch url, taken from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21245168.html (google cache), but modified quite a lot
		// ( ht|f)tp(s?))\:\/\/)?  -  optional protocol prefix 
		//  [a-z0-9@\-\.]+  -  one or more subnet name (this catches "www" as well) (catches also email or ftp://username@ftpserver.com type ftp urls)
		// \.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)  -  one and only one top level domain, should be updated
		// (\.[a-z]{2})?  -  optional country suffix (.tr, ...)
		// (\:[0-9]+)?  -  optional port number
		// (\/([a-z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]*))* one or more subfolders or url parameters
		// gim  -  global, ignore case, multiline
		var urlCatcherRegExp = /(((ht|f)tp(s?))\:\/\/)?[a-z0-9@\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|de)(\.[a-z]{2})?(\:[0-9]+)?(\/([a-z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]*))*/gim;
		
		var regex_url= new RegExp(urlCatcherRegExp);
		var res = regex_url.exec(txt);
		
		var txtClone = txt;
		while(res){							
			var url = res[0];
			if(url.indexOf("://")<0) // no protocol prefix, we must add one
			{ 
				if(url.indexOf("@")>0) // assuming this is a mail address
				{ 
					if(url.indexOf("mailto:")<0)  // adding a mailto identifier if lacks
						url = "mailto:"+url;
				}
				else url = "http://"+url; // assuming this is an http address
			}
			
			var temp = "<a href=\""+url+"\" target=\"_blank\">"+res[0]+"</a>";
			txtClone = txtClone.replace(res[0],temp);						
			res = regex_url.exec(txt);						
		}
		txt = txtClone;		

		//nl2br
		txt = txt.replace(/\n/gmi,"<br>");		
	
		return txt;
	}
	
	function htmlentities(text){
	
		text = text.replace(/&/g,"&amp;");
		text = text.replace(/"/g,"&quot;");
		//text = text.replace(/'/g,"&apos;"); 		//this  fails in IE
		text = text.replace(/</g,"&lt;");		
		text = text.replace(/>/g,"&gt;");			    
		return text;
	}
	
	function htmlReverseentities(text){
		text = text.replace(/&amp;/g,"&");	
		text = text.replace(/&quot;/g,"\"");		
		text = text.replace(/&lt;/g,"<");		
		text = text.replace(/&gt;/g,">");			    
		return text;
	}
		
}

var WindowMng = new Object();
WindowMng.windows = new Array();
WindowMng.getWinById = function(id){
		for(i=0;i<this.windows.length;i++){
			if(id==this.windows[i].id)
				return this.windows[i];
		}
		return null;	
}
WindowMng.cascade = function(xOffset,yOffset){
		if(typeof xOffset=="undefined")
			xOffset = 50;
		if(typeof yOffset=="undefined")
			yOffset = 30;
			
		for(i=0;i<this.windows.length;i++){
			this.windows[i].mainDiv.style.top = ((i+1)*22+yOffset);
			this.windows[i].mainDiv.style.left = ((i+1)*22+xOffset);			
		}		

}

WindowMng.tile = function(xOffset,yOffset){
		if(typeof xOffset=="undefined")
			xOffset = 50;
		if(typeof yOffset=="undefined")
			yOffset = 30;
		scr_w  = parseInt(document.body.scrollLeft+document.body.clientWidth);
		scr_h  = parseInt(document.body.scrollTop+document.body.clientHeight);
		
		max_h = 0;
		
		tmp_x_offset = xOffset;
		tmp_y_offset = yOffset;
		for(i=this.windows.length-1;i>=0;i--){			
			
			//new row needed?
			if(parseInt(this.windows[i].mainDiv.clientWidth)+tmp_x_offset >= scr_w){ 
				tmp_x_offset = xOffset;				
				yOffset += max_h+10;
				max_h=0;
			}						
			
			this.windows[i].mainDiv.style.left = tmp_x_offset;
			this.windows[i].mainDiv.style.top = yOffset ;			
			
			h = parseInt(this.windows[i].mainDiv.clientHeight);
			if(h>max_h) 
				max_h=h;
				
			tmp_x_offset+=parseInt(this.windows[i].mainDiv.clientWidth)+10;
		}		

}


WindowMng.removeWinById = function(id){

		for(i=0;i<this.windows.length;i++){
					if(id==this.windows[i].id)
						break;
				}
		var arr1 = this.windows.slice(0,i);
		var arr2 = this.windows.slice(i+1);
		arr1.concat(arr2);
		this.windows = arr1;
}

WindowMng.focusWin=function(win){
		win.focusSeq = parseInt(this.windows[this.windows.length-1].focusSeq)+1;
		this.windows.sort(cmp_focus_seq);
				
		for(i=0;i<this.windows.length;i++){
			this.windows[i].mainDiv.style.zIndex=50+i;					
		}		
		
}

WindowMng.addWin= function(win){
	if(this.windows.length>0){
		this.windows.sort(cmp_focus_seq);			
		win.focusSeq = parseInt(this.windows[this.windows.length-1].focusSeq)+1;
	}
	this.windows.push(win);
	this.focusWin(win);		
}

function mouseCoords(ev){	
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function cmp_focus_seq(w1,w2){
	return w1.focusSeq-w2.focusSeq;
}

function getPressedKeyCode(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;	
	return code;
}
