/***********************************************
Copyright (C) 2008  Ugur HAMZADAYI
************************************************/
var ug_ukeyboardpicker_popup = null;
function ug_ukeyboardpicker_popup_load() {
   if( ug_ukeyboardpicker_popup != null )
	  return;
	   
    ug_ukeyboardpicker_popup = new UKeyboardPickerCtrl();
	
	//Attach messages
	if( ug_ukeyboardpicker_popup != null ) {
       AttachEventUKeyboardPicker( window , "resize" , function(){ ug_ukeyboardpicker_popup.RefreshPosition();} );
	   AttachEventUKeyboardPicker( window , "scroll" , function(){ ug_ukeyboardpicker_popup.RefreshPosition();} );
	   AttachEventUKeyboardPicker( document.body , "click" , function(){ ug_ukeyboardpicker_popup.OnClickWindow();} );	   
       AttachEventUKeyboardPicker( ug_ukeyboardpicker_popup.keyboard , "mouseover" , function () {ug_ukeyboardpicker_popup.OnOverkeyboard();} );	   
       AttachEventUKeyboardPicker( ug_ukeyboardpicker_popup.keyboard , "mouseout" , function () {ug_ukeyboardpicker_popup.OnOutkeyboard();} );	      
	}
    
}

//Bu Önemli
function AttachEventUKeyboardPicker(obj, eventName, eventHandler) {
   
  if( obj ) {
	 
     if(eventName.substring(0, 2) == 'on') {
 	    eventName = eventName.substring(2,eventName.length);
	 }

     if(obj.attachEvent){
        obj.attachEvent('on'+ eventName, eventHandler);
     } 
     else 
     if( obj.addEventListener){
        obj.addEventListener(eventName, eventHandler, false);  
     }
	 
	 
  }
 
};

AttachEventUKeyboardPicker( window , 'onload' , ug_ukeyboardpicker_popup_load );




function UKeyboardPickerCtrl() {
	
   this.CreatePopup();
   
};


UKeyboardPickerCtrl.prototype.CreatePopup = function () {
   this.lang = 0;  //Türkçe
   this.timeid = null;
   this.ismouseover=false;
   this.stringlength=0;
   this.keycapslock=false;
   this.keyshift=false;
   this.keyaltgr=false;
   this.bodyclickcount=0;
   this.keyboard = ug_ukeyboardpicker_popup = document.createElement('div');
   this.keyboard.setAttribute('id','global_ukeyboardpopupid_1');
   this.keyboard.style.position = 'absolute';
   this.keyboard.style.display = 'none';
   this.keyboard.style.backgroundColor = '#FFFFFF';
   
   innerText = '<iframe id="global_ukeyboardpopupid_1frame" style="filter:alpha(opacity=0);-moz-opacity:0;opacity:0; position:absolute;display:none;" frameBorder="0" scrolling="no"></iframe>';
   innerText += '<div style="margin:2px;BACKGROUND:#909090;BORDER: #AAAAAA 1px solid" onmousedown="return false;"  onconextmenu="return false;" onselectstart="return false;" onscroll="return false;">';
   innerText += '<div onmousedown="return false;"  onconextmenu="return false;" onselectstart="return false;" onscroll="return false;" id="' + this.keyboard.id + 'panel"> </div>';
   innerText += '</div>';
   
   this.keyboard.innerHTML = innerText;
   document.body.appendChild(this.keyboard);
};



UKeyboardPickerCtrl.prototype.GetLeftPosition = function ( obj ) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
};

UKeyboardPickerCtrl.prototype.GetTopPosition = function ( obj ) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;	
   return curtop;
};

UKeyboardPickerCtrl.prototype.OnMouseOverOutControl = function( element , type ) {
  if( type == 0 )	
     element.style.backgroundColor = '#6D87CC';
  else
  if( type == 1 )	
    element.style.backgroundColor = '#CCCCCC'; 
     //element.style.backgroundColor = '#4D67AC'; 
}

UKeyboardPickerCtrl.prototype.OnClickKey = function ( strkey ) {
	
   if( strkey == "CAPS" ) {
  	 this.keycapslock= !this.keycapslock;   
	 this.Draw();
	 return;
   }
   if( strkey == "SHIFT" ) {
  	 this.keyshift= !this.keyshift;   
	 this.Draw();
	 return;
   }
   if( strkey == "ALTGR" ) {
  	 this.keyaltgr= !this.keyaltgr;   
	 this.Draw();
	 return;
   }


   if( this.targetControl != null ) {

	  

      if( strkey == "CLEARCONTROL" ) {
		  this.targetControl.value = "";
		  return;
	  }
	  else
      if( strkey == "backsp" ) {
		  this.targetControl.value = this.targetControl.value.substring( 0 , this.targetControl.value.length - 1 );
		  return;
	  }
	  else
      if( strkey == "ENTER" ) {
		  this.HidePopup();	  
		  return;
	  }
	  
	  
	  if( this.targetControl.value.length >= this.stringlength )
	     return;
	  
	  this.targetControl.focus();
      if( strkey == "backslash" ) {
		  this.targetControl.value = this.targetControl.value + "\\";
		  return;
	  }
	  else
      if( strkey == "tektirnak" ) {
		  this.targetControl.value = this.targetControl.value + "\'";
		  return;
	  }
	  else
      if( strkey == "cifttirnak" ) {
		  this.targetControl.value = this.targetControl.value + "\"";
		  return;
	  }
	  else
      if( strkey == "TAB" ) {
		  this.targetControl.value = this.targetControl.value + "\t";
		  return;
	  }
	  else
      if( strkey == "SPACE" ) {
		  this.targetControl.value = this.targetControl.value + " ";
		  return;
	  }

	  
      this.targetControl.value =  this.targetControl.value + strkey;
	  
   }
}


UKeyboardPickerCtrl.prototype.Draw = function () {
	
   var lines = null; 
   if( this.keyaltgr ) {

      if( this.keycapslock ) {
	 
	     if( this.keyshift ) {
            lines = new Array( "300","<",">","£","#","$","½","","{","[","]","}","backslash","|","backsp","lnend",
				      "300","TAB","@","","€","","","","","","","","¨","~","ENTER","lnend", 
				      "300","down","CAPS","æ","","","","","","","","","´","","`","lnend",
				      "300","down","SHIFT","|","","","","","","","","","","","down","SHIFT","lnend", 
				      "100","SPACE","down","ALTGR" 				   
				    );
   	     }
 	     else {
            lines = new Array( "300","<",">","£","#","$","½","","{","[","]","}","backslash","|","backsp","lnend",
				      "300","TAB","@","","€","","","","","","","","¨","~","ENTER","lnend", 
				      "300","down","CAPS","Æ","","","","","","","","","´","","`","lnend",
				      "300","SHIFT","|","","","","","","","","","","","SHIFT","lnend", 
				      "100","SPACE","down","ALTGR" 				   
				    );
 	      }
      }
      else {
	   
	      if( this.keyshift ) {
             lines = new Array( "300","<",">","£","#","$","½","","{","[","]","}","backslash","|","backsp","lnend",
				      "300","TAB","@","","€","","","","","","","","¨","~","ENTER","lnend", 
				      "300","CAPS","Æ","","","","","","","","","´","","`","lnend",
				      "300","down","SHIFT","|","","","","","","","","","","","down","SHIFT","lnend", 
				      "100","SPACE","down","ALTGR" 				   
				    );
   	     }
  	     else {
             lines = new Array( "300","<",">","£","#","$","½","","{","[","]","}","backslash","|","backsp","lnend",
				      "300","TAB","@","","€","","","","","","","","¨","~","ENTER","lnend", 
				      "300","CAPS","æ","","","","","","","","","´","","`","lnend",
				      "300","SHIFT","|","","","","","","","","","","","SHIFT","lnend", 
				      "100","SPACE","down","ALTGR" 				   
				    );
 	     }
      }

   }
   else
   if( this.keycapslock ) {
	 
	  if( this.keyshift ) {
         lines = new Array( "300","é","!","tektirnak","^","+","%","&","/","(",")","=","?","_","backsp","lnend",
				      "300","TAB","q","w","e","r","t","y","u","ı","o","p","ğ","ü","ENTER","lnend", 
				      "300","down","CAPS","a","s","d","f","g","h","j","k","l","ş","i",";","lnend",
				      "300","down","SHIFT",">","z","x","c","v","b","n","m","ö","ç",":","down","SHIFT","lnend", 
				      "100","SPACE","ALTGR" 				   
				    );
	  }
	  else {
         lines = new Array( "300","cifttirnak","1","2","3","4","5","6","7","8","9","0","*","-","backsp","lnend",
				    "300","TAB","Q","W","E","R","T","Y","U","I","O","P","Ğ","Ü","ENTER","lnend", 
				    "300","down","CAPS","A","S","D","F","G","H","J","K","L","Ş","İ",",","lnend",
				    "300","SHIFT","<","Z","X","C","V","B","N","M","Ö","Ç",".","SHIFT","lnend", 
				    "100","SPACE","ALTGR"
				    );
 	    }
   }
   else {
	   
	  if( this.keyshift ) {
         lines = new Array( "300","é","!","tektirnak","^","+","%","&","/","(",")","=","?","_","backsp","lnend",
				      "300","TAB","Q","W","E","R","T","Y","U","I","O","P","Ğ","Ü","ENTER","lnend", 
				      "300","CAPS","A","S","D","F","G","H","J","K","L","Ş","İ",";","lnend",
				      "300","down","SHIFT",">","Z","X","C","V","B","N","M","Ö","Ç",":","down","SHIFT","lnend", 
				      "100","SPACE","ALTGR" 				   
				    );
	  }
	  else {
         lines = new Array( "300","cifttirnak","1","2","3","4","5","6","7","8","9","0","*","-","backsp","lnend",
				    "300","TAB","q","w","e","r","t","y","u","ı","o","p","ğ","ü","ENTER","lnend", 
				    "300","CAPS","a","s","d","f","g","h","j","k","l","ş","i",",","lnend",
				    "300","SHIFT","<","z","x","c","v","b","n","m","ö","ç",".","SHIFT","lnend", 
				    "100","SPACE","ALTGR"
				    );
 	    }
   }



  var flag = false;
  innerText = ""; 
  widhtext="300";
  var bdown = false;
  for( i = 0;i < lines.length ; i++ ) {
	  
	  if( !flag ) {
		 widhtext=lines[i];
		 innerText += '<center><table width="'+widhtext+'" cellpadding="1" cellspacing="0" style="BACKGROUND: #FF00FF;BORDER: #BBBBBB 1px solid"><tr>';
		 flag = true;
		 i++;
		 
	  }
	  
	  if( lines[i] == "lnend") {
 		 innerText += '</tr></table></center>';   
		 flag = false;
		 continue;  
	  }
	  
  	  if( lines[i] == "down") {
		 bdown = true; 
		 continue;
	  }

      text = lines[i];
	  if( text == "" )
	     text = "&nbsp;";
	  else	 
	  if( text == "backslash" )
	     text = "\\";
      else		 
	  if( text == "cifttirnak" )
	     text = "\"";
	  else	 
	  if( text == "tektirnak" )
	     text = "'";
	  else	 
	  if( text == "SPACE" )
	     text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

      if( bdown )  
         innerText += '<td ondblclick="ug_ukeyboardpicker_popup.OnClickKey(\''+lines[i]+'\');" onclick="ug_ukeyboardpicker_popup.OnClickKey(\''+lines[i]+'\');" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#FFFFFF;FONT-FAMILY:verdena,arial;BACKGROUND:#B06060;BORDER: #EFEFEF 1px solid" >'+text+'</td>';
	  else 
    	 innerText += '<td ondblclick="ug_ukeyboardpicker_popup.OnClickKey(\''+lines[i]+'\');"  onclick="ug_ukeyboardpicker_popup.OnClickKey(\''+lines[i]+'\');" onmouseover="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 0 );" onmouseout="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 1 );" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#000000;FONT-FAMILY:verdena,arial;BACKGROUND:#CCCCCC;BORDER: #EFEFEF 1px solid" >'+text+'</td>';
	  
	  bdown = false;
	  
  }
  
  
  //Close Button
  innerText += '</table>';
  innerText += '<table width="300" height="20" cellpadding="2" cellspacing="4" style="BACKGROUND:#606060;BORDER:#606060 1px solid">';
  innerText += '<tr>';
  innerText += '<td><span style="FONT-WEIGHT:bold;FONT-SIZE:18px;font-style:italic;COLOR:#000000;FONT-FAMILY:arial;"></span></td>';
	  
  if( this.lang == 0 ) {//Türkçe
     innerText += '<td onmouseover="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 0 );" onmouseout="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 1 );" onclick="ug_ukeyboardpicker_popup.OnClickKey(\'CLEARCONTROL\');" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#000000;FONT-FAMILY:verdena,arial;BACKGROUND:#CCCCCC;BORDER: #EEEEEE 1px solid" >&nbsp;Temizle&nbsp;</td>';
     innerText += '<td onmouseover="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 0 );" onmouseout="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 1 );" onclick="ug_ukeyboardpicker_popup.HidePopup();" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#000000;FONT-FAMILY:verdena,arial;BACKGROUND:#CCCCCC;BORDER: #EEEEEE 1px solid" >&nbsp;Kapat&nbsp;</td>';
  }
  else {		 
     innerText += '<td onmouseover="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 0 );" onmouseout="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 1 );" onclick="ug_ukeyboardpicker_popup.OnClickKey(\'CLEARCONTROL\');" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#EEEEEE;FONT-FAMILY:verdena,arial;BACKGROUND:#CCCCCC;BORDER: #EEEEEE 1px solid" >&nbsp;Clear&nbsp;</td>';
     innerText += '<td onmouseover="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 0 );" onmouseout="ug_ukeyboardpicker_popup.OnMouseOverOutControl( this , 1 );" onclick="ug_ukeyboardpicker_popup.HidePopup();" style="cursor:pointer;width:28px;text-align:center;height:10px;FONT-WEIGHT: bold;FONT-SIZE:11px;COLOR:#EEEEEE;FONT-FAMILY:verdena,arial;BACKGROUND:#CCCCCC;BORDER: #EEEEEE 1px solid" >&nbsp;Close&nbsp;</td>';	  
  }
  innerText += '</tr>';
  innerText += '</table>';
  
  panel = document.getElementById(this.keyboard.id + 'panel');
  if( panel != null ) 
	 panel.innerHTML =  innerText;  
	 
}



UKeyboardPickerCtrl.prototype.Fillkeyboard = function () {
	this.Draw();
};


UKeyboardPickerCtrl.prototype.OnOverkeyboard = function () {
   this.ismouseover=true;	
   if( this.timeid ) {
	  clearTimeout(this.timeid);
	  this.timeid = null;
   }
}

UKeyboardPickerCtrl.prototype.OnOutkeyboard = function () {
   this.ismouseover=false;		
   this.timeid = setTimeout( "ug_ukeyboardpicker_popup.HidePopup()" , 10000 );
}


UKeyboardPickerCtrl.prototype.OnClickWindow = function () {
	
  if( window.event ) {
	 if( window.event.srcElement ) {
   	    controlyear	= window.event.srcElement;
		while( controlyear != null ) {
			if( controlyear.id == 'udatecalndarpickerselectyear' || controlyear.id == 'udatecalndarpickerselectmonth' ) {
				return false;
			}
			controlyear = controlyear.parentNode;
		}
	 }
	  
  }
	
  if( !this.ismouseover && this.bodyclickcount >= 1 ) {
	 if( this.keyboard.style.display == 'block' ) {
		 this.HidePopup();
	 }
  }

  this.bodyclickcount++;
  if( this.bodyclickcount > 2 )
	 this.bodyclickcount == 2; 
	 
  return false;
}


UKeyboardPickerCtrl.prototype.RefreshPosition = function () {

    if( this.alignControl == null )
	   return;
	 
	cxWin = window.innerWidth;
	scrollX = window.pageXOffset;
	if (navigator.appName == "Microsoft Internet Explorer") {
		if( document.documentElement.clientWidth ) {
    	   cxWin = document.documentElement.clientWidth;
		   scrollX = document.documentElement.scrollLeft;
		}
		else {
    	   cxWin = document.body.clientWidth;
		   scrollX = document.body.scrollLeft;
		}
		
	}
	

	cyWin = window.innerHeight;
	scrollY = window.pageYOffset;
	if (navigator.appName == "Microsoft Internet Explorer") {
		if( document.documentElement.clientWidth ) {		
    	   cyWin = document.documentElement.clientHeight;
		   scrollY = document.documentElement.scrollTop;
		}
		else {
    	   cyWin = document.body.clientHeight;
		   scrollY = document.body.scrollTop;
		}
		
	}


    //Range Belirleniyor...
	rx1 = scrollX;
	ry1 = scrollY;
	rx2 = rx1 + cxWin;
	ry2 = ry1 + cyWin;

    if( this.iframeWin == null ) { //iframe içinde degil ise
       hCtrl = this.alignControl.offsetHeight;	
	   wCtrl = this.alignControl.offsetWidth;	

   	   x1Pos = this.GetLeftPosition(this.alignControl);
 	   y1Pos = this.GetTopPosition(this.alignControl) + hCtrl;
 	   x2Pos = x1Pos + this.keyboard.offsetWidth;
	   y2Pos = y1Pos + this.keyboard.offsetHeight;

       if( y2Pos > ry2 ) {
		  y1Pos = this.GetTopPosition(this.alignControl) - this.keyboard.offsetHeight;
	   }

	   this.keyboard.style.left = x1Pos +'px'; 
       this.keyboard.style.top = y1Pos +'px'; 
	
	
       inlineframe = document.getElementById('global_ukeyboardpopupid_1frame');
	   if( inlineframe ) {
	      inlineframe.style.left = 0 + 'px';
	      inlineframe.style.top = 0 + 'px';
	      inlineframe.style.width =  this.keyboard.offsetWidth + 'px';
	      inlineframe.style.height = this.keyboard.offsetHeight +'px';
	      this.keyboard.style.zIndex = 1000;
	      inlineframe.style.zIndex = -1; //this.keyboard.style.zIndex - 1;
	   }
	}
	else { //iframe içinde ise
		
       hCtrl = this.keyboard.offsetHeight;	
  	   wCtrl = this.keyboard.offsetWidth;	
	   x1Pos = (cxWin - wCtrl ) / 2;
	   y1Pos = (cyWin - hCtrl ) / 2;
	   x1Pos += scrollX;
 	   y1Pos += scrollY;

	   this.keyboard.style.left = x1Pos +'px'; 
       this.keyboard.style.top = y1Pos +'px'; 
       inlineframe = document.getElementById('global_ukeyboardpopupid_1frame');
	   if( inlineframe ) {
	      inlineframe.style.left = 0 + 'px';
	      inlineframe.style.top = 0 + 'px';
	      inlineframe.style.width =  this.keyboard.offsetWidth + 'px';
	      inlineframe.style.height = this.keyboard.offsetHeight +'px';
	      this.keyboard.style.zIndex = 1000;
	      inlineframe.style.zIndex = -1; //this.keyboard.style.zIndex - 1;
	   }
	}
};

UKeyboardPickerCtrl.prototype.ShowPopup = function ( ctrl , alignctrl , language , iframeWin , length ) {
    this.iframeWin = iframeWin;
	this.stringlength = length;
	if( ctrl == null || alignctrl == null )
	   return false;

    if( this.timeid ) {
	   clearTimeout(this.timeid);
	   this.timeid = null;
    }
	
	
	this.bodyclickcount=0;
	this.alignControl = alignctrl;
	this.targetControl = ctrl;
	this.Fillkeyboard(); //Klavye çiziliyor....
	this.keyboard.style.display = 'block';


    inlineframe = document.getElementById('global_ukeyboardpopupid_1frame');
	if( inlineframe )
	   inlineframe.style.display = 'block';
	
	
	this.RefreshPosition();
	this.timeid = setTimeout( "ug_ukeyboardpicker_popup.HidePopup()" , 10000 );
        
};

UKeyboardPickerCtrl.prototype.HidePopup = function () {
	this.keyboard.style.display = 'none';
    inlineframe = document.getElementById('global_ukeyboardpopupid_1frame');
	if( inlineframe )
	   inlineframe.style.display = 'none';
	
    this.bodyclickcount = 0; 
	this.ismouseover = false;
    if( this.timeid ) {
	   clearTimeout(this.timeid);
	   this.timeid = null;
    }
};

function ShowUKeyboardPicker( idControl , idAlign , language , iframeWin , length ) {
	if( iframeWin != null ) {
	   control = iframeWin.document.getElementById(idControl);
   	   if( idAlign == null || idAlign == "" )
	      aligncontrol = control;
       else
          aligncontrol = iframeWin.document.getElementById(idAlign);
	}
	else {
       control = document.getElementById(idControl);
   	   if( idAlign == null || idAlign == "" )
	      aligncontrol = control;
       else
          aligncontrol = document.getElementById(idAlign);
	}
	
    if( ug_ukeyboardpicker_popup != null && control.disabled != true && control.readOnly != true ) {
		if( iframeWin != null )
		   ug_ukeyboardpicker_popup.ShowPopup( control , aligncontrol , language , iframeWin , lenght );
		else
		   ug_ukeyboardpicker_popup.ShowPopup( control , aligncontrol , language , iframeWin , length );
	}
}

