var keyboardUpperArr = new Array("0","1","2","3","4","5","6","7","8","9",
								 "A","B","C","Ç","D","E","F","G","Ğ","H","I","İ","J","K","L","M","N",
								 "O","Ö","P","Q","R","S","Ş","T","U","Ü","V","W","X","Y","Z");
var keyboardLowerArr = new Array("0","1","2","3","4","5","6","7","8","9",
								 "a","b","c","ç","d","e","f","g","ğ","h","ı","i","j","k","l","m","n",
								 "o","ö","p","q","r","s","ş","t","u","ü","v","w","x","y","z");
var keyboardArr = keyboardLowerArr;

var overlapArr;				   
TOTAL_ELEMENT = keyboardArr.length;
NUMBER_COUNT = 10;
letterCount = TOTAL_ELEMENT - NUMBER_COUNT;
browserWidth = 793;
keyboardWidth = 100;
browserHeight = 543;
var currentInput = null;
var keypad = null;
var keypadSize = new Object();

var Winx=0; // current Window(document) size
var Winy=0;

var WinWidth=0; // client Window size
var WinHeight=0;

var keypadWidth = 0;
var keypadHeight = 0;

var ScrollLeft; // current scroll positions
var ScrollTop;

// moving keypad...
N = (document.all) ? 0 : 1;
var ob;
var over = false;
var keypadVisible = false;

function populateKeys(){
	for (var i=0;i<TOTAL_ELEMENT;i++){
		var b = document.getElementsByName("b" + i)[0];
		b.value = keyboardArr[b.getAttribute("idx")];
	}
}

function changeLetters(){
	var b = document.getElementsByName("bUpperOrLower")[0];
	var message = document.getElementById("message");
	if(b.value == "Küçük Harf" || b.value == 'Lower Case') {
		if(b.value == "Küçük Harf") {
			b.value = "Büyük Harf";
			message.innerHTML = "Sanal Klavye'de büyük harf yazmak için \"Büyük Harf\" düğmesine basın.";
		} else {
			b.value = "Upper Case";
			message.innerHTML = "To write in upper case in the Virtual Keyboard, please click \"Upper Case\" button.";		
		}
		keyboardArr = keyboardLowerArr;
	}
	else{
		if(b.value == "Büyük Harf") {
			b.value = "Küçük Harf";
			message.innerHTML = "Sanal Klavye'de küçük harf yazmak için \"Küçük Harf\" düğmesine basın.";
		} else {
			b.value = "Lower Case";
			message.innerHTML = "To write in lower case in the Virtual Keyboard, please click \"Lower Case\" button.";							
		}
		keyboardArr = keyboardUpperArr;
	}
	populateKeys();
}

function scrambleOrReset(){
	var e = document.getElementsByName("bScrambleOrReset")[0];
	if(e.value == "Harfleri Karıştır" || e.value == 'Shuffle Letters'){
		if(e.value == "Harfleri Karıştır") e.value = "Harfleri Düzenle";
		else e.value = "Arrange Letters";
		for (var i=10;i<TOTAL_ELEMENT;i++){
			var randomnumber = Math.floor(Math.random()*(TOTAL_ELEMENT - 10)) + 10;
			var b = document.getElementsByName("b" + i)[0];
			var btemp = document.getElementsByName("b" + randomnumber)[0];
			var tmpIdx = b.getAttribute("idx");
			b.setAttribute("idx",btemp.getAttribute("idx"));
			btemp.setAttribute("idx",tmpIdx);
		}
	}
	else{
		if(e.value == "Harfleri Düzenle") e.value = "Harfleri Karıştır";
		else e.value = "Shuffle Letters";	
		for (var i=10;i<TOTAL_ELEMENT;i++){
			var b = document.getElementsByName("b" + i)[0];
			b.setAttribute("idx",i);
		}
	}
	populateKeys();
}

function scrambleNumbers(){
	for (var i=0;i<NUMBER_COUNT;i++){
		var randomnumber = Math.floor(Math.random()*10);
		var b = document.getElementsByName("b" + i)[0];
		var btemp = document.getElementsByName("b" + randomnumber)[0];
		var tmpIdx = b.getAttribute("idx");
		b.setAttribute("idx",btemp.getAttribute("idx"));
		btemp.setAttribute("idx",tmpIdx);
	}
	populateKeys();
}


function trim(inputString) {

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { 
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { 
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, 		 retValue.length); 
   }
   return retValue; 
  
} 

function  writeByKeypad(keypadChar){

		NS4 = (document.layers);						
		f=currentInput;
		if(f.maxLength == null || (f.maxLength != null && f.maxLength == -1) || (f.maxLength != null && f.value.length < f.maxLength)){
			f.value = f.value + trim(keypadChar);
		}
		f.focus();
	
}

function  sil(){		
	currentInput.value = currentInput.value.substring(0,currentInput.value.length-1);
	currentInput.focus();
} 

function showKeypad() {		
	if (document.all) {
		if(keypad.style.visibility != "visible"){
			initiateKeyboardPos(0);	
			keypad.style.visibility="visible";
		}
	} else if (document.layers) {
		if(keypad.visibility != "show"){
			initiateKeyboardPos(0);
			keypad.visibility="show";	
		}
	} else {
		if(keypad.style.visibility != "visible"){
			initiateKeyboardPos(0);
			keypad.style.visibility="visible";
		}
	}	
}


function hideKeypad() {	
	if (document.all) {	
		keypad.style.visibility="hidden";
			
	} else if (document.layers) {
		keypad.visibility="hidden";		
	} else {	
		keypad.style.visibility="hidden";
	}
}

function WinSizeNotSupported() {
	alert("Browser'ınız bazı fonksiyonları desteklemiyor.");
}

//--------------------------------------------
//gets size of available document or Windowsize,
//whichever is bigger.
function getWinSize() {
	getWinSize = WinSizeNotSupported;
	
	if(document.all) {
		// Internet Explorer	
		getWinSize = wsq1;
		
		if (! document.body.scrollWidth) {
			//Operaspecific: Opera in the Internet mode
			// doesn't understand scrollWidth/scrollHeight,
			// but clientWidth gives the scrollWidth... 
			getWinSize = wsq2;
		};

	} 
	else if(document.layers) {
		// Netscape??
		getWinSize = wsq3;
	}
	else if (document.getElementById) {
		
		if (document.height) {
			// Netscape 6 & Mozilla 1
			getWinSize = wsq3;
		}
	
		// For Opera & Opera in Mozilla mode:
		else if (document.body.offsetWidth) {
			getWinSize = wsq5;
		}
		
	}
	//alert(getWinSize);		
	getWinSize();
}

// For IE 6:
function wsq1() {
	with(document.body){
		WinWidth = clientWidth;
		WinHeight = clientHeight;
				
		Winx = scrollWidth;
		Winy = scrollHeight;
		
		if (clientWidth > Winx) Winx = clientWidth;
		if (clientHeight > Winy) Winy = clientHeight;
		ScrollLeft = scrollLeft;
		ScrollTop = scrollTop;
	}
}

// For Opera in the IE mode
function wsq2() {	
	WinWidth = document.body.clientWidth;
	WinHeight = document.body.clientHeight;
	Winx = document.body.clientWidth;
	Winy = document.body.clientHeight;
	ScrollLeft = document.body.scrollLeft;
	ScrollTop = document.body.scrollTop;
}

// For Netscape 6 & Mozilla 1
// Netscape 4.08 (Win)
function wsq3() {
	
	WinWidth = window.innerWidth;
	WinHeight = window.innerHeight;
	Winx = document.width;
	Winy = document.height;
	if (window.innerWidth > Winx) Winx = window.innerWidth;
	if (window.innerHeight > Winy) Winy = window.innerHeight;
	ScrollLeft = window.pageXOffset;
	ScrollTop = window.pageYOffset;
}


//--------------------------------------------
// returns size of "div" element in divsize.
// divsize must be "= new Object();"
function getObjSize(obj, divsize) {
	if (document.getElementById || document.all) {
		// IE
		// Opera as Opera, IE, Mozilla
		// Netscape 6, Mozilla
		getObjSize = gosq1;
	}
	else
	{
		if (document.layers){
			// Netscape 4.08
			getObjSize = gosq2;
		}
		else
		{
			alert("getObjSize not defined.");
		}
	}
	//alert(getObjSize);
	getObjSize(obj,divsize);
}

function gosq1(obj,ds) {
	//ds.top = parseInt(obj.style.top);
	//ds.left = parseInt(obj.style.left);
	
	ds.top = obj.offsetTop;
	ds.left = obj.offsetLeft;	
	
	var tempObj = obj.offsetParent;
	while(tempObj.offsetParent) {
		
		ds.top = ds.top + tempObj.offsetTop;
		ds.left = ds.left + tempObj.offsetLeft;
		//alert("parent" + tempObj.offsetLeft);
		
		tempObj = tempObj.offsetParent;
	}
		
	ds.right = ds.left + parseInt(obj.offsetWidth);
	ds.bottom = ds.top + parseInt(obj.offsetHeight);
	ds.width = parseInt(obj.offsetWidth);
	ds.height = parseInt(obj.offsetHeight);
	
	
	
}

function gosq2(obj,ds) {
	ds.top = parseInt(obj.style.top);
	ds.left = parseInt(obj.style.left);
	ds.right = ds.left + parseInt(obj.clip.width);
	ds.bottom = ds.top + parseInt(obj.clip.height);
	ds.width = parseInt(obj.clip.width);
	ds.heigt = parseInt(obj.clip.height);
}


/*
 * callcount parameter is for number of sequential call 
 */
function initiateKeyboardPos(callCount) {


		/*
		 * if callCount is equal to maxCallCount function will not call itself 
		 * again even two objects overlap
		 */
		
		MAX_CALL_COUNT = 10; 
		divElement = document.getElementById("div_keypad");
		getWinSize();
		getObjSize(keypad, keypadSize);
		x=0;//ScrollLeft; 
		y=0;//ScrollTop+60;
		
					
		/*x = Math.floor(Math.random() * (WinWidth - keypadSize.width)) + ScrollLeft; 
		y = Math.floor(Math.random() * (WinHeight - keypadSize.height)) + ScrollTop; 
		alert("x : " + x + " - y : "+ y);		
		alert("winWidth : " + WinWidth + " - divWidth : " + keypadSize.width);
		alert("winHeight : " + WinHeight + " - divHeight : " + keypadSize.height);
		*/		
		divElement.style.left=x+ ScrollLeft;
		divElement.style.top=y+ ScrollTop;
		
		for (i=0; i < overlapArr.length; i++){									
			if(isOverlap(overlapArr[i], divElement) && (callCount < MAX_CALL_COUNT)) {
				initiateKeyboardPos(callCount+1);										
			}	

		}
}

/*
 * Used to check two elements are overlapped or not. 
 */
 
function isOverlap(elm1, elm2) {
	
	var obj1 = new Object();
	var obj2 = new Object();
	
				
	getObjSize(elm1, obj1);
	getObjSize(elm2, obj2);
	
	/* 
	 * Dimensions of element1 must be smaller than element2 to compare their dimensions correctly.
	 * so size of elements are checked and variable references are changed, if necessary. 	 
	 */	
	area1 = (obj1.right - obj1.left) * (obj1.bottom - obj1.top);
	area2 = (obj2.right - obj2.left) * (obj2.bottom - obj2.top);
		
	if(area1 > area2) {
		tempElm = elm1;
		elm1 = elm2;
		elm2 = tempElm;
		getObjSize(elm1, obj1);
		getObjSize(elm2, obj2);

	}
		
/*	
	alert(" - left : " + obj1.left + 		  
		  " - right : " + obj1.right+
		  " - top : " + obj1.top + 		  
		  " - bottom : " + obj1.bottom);

	alert(" - left : " + obj2.left + 		  
		  " - right : " + obj2.right+
		  " - top : " + obj2.top + 		  
		  " - bottom : " + obj2.bottom);
*/	
		
	if (((obj1.left >= obj2.left) && (obj1.left <= obj2.right) && (obj1.top >= obj2.top) && (obj1.top <= obj2.bottom)) || //is top left point of obj1 inside obj2 
		((obj1.right >= obj2.left) && (obj1.right <= obj2.right) && (obj1.top >= obj2.top) && (obj1.top <= obj2.bottom)) || //is top right point of obj1 inside obj2
		((obj1.left >= obj2.left) && (obj1.left <= obj2.right) && (obj1.bottom >= obj2.top) && (obj1.bottom <= obj2.bottom)) || //is bottom left point of obj1 inside obj2
		((obj1.right >= obj2.left) && (obj1.right <= obj2.right) && (obj1.bottom >= obj2.top) && (obj1.bottom <= obj2.bottom))) { //is bottom right point of obj1 inside obj2
	//		alert("xx");
		return true;
		
	}	
	return false
	
	
	
	/*
	elm1X1 = elm1.offsetLeft;
	elm1Y1 = elm1.offsetTop;
	
	
	elm1X1 = objectSize.offsetLeft;
	elm1Y1 = objectSize.offsetTop;
		
	elm1X2 = elm1X1 + elm1.offsetWidth;		
	elm1Y2 = elm1Y1;
				
	elm1X3 = elm1X1;		
	elm1Y3 = elm1Y1 + elm1.offsetHeight;
	
	elm1X4 = elm1X1 + elm1.offsetWidth;		
	elm1Y4 = elm1Y1 + elm1.offsetHeight;
	*/
	/* second elment bigger element dimensions*/
	/*
	elm2X1 = elm2.offsetLeft;
	elm2Y1 = elm2.offsetTop;
		
	elm2X2 = elm2X1 + elm2.offsetWidth;		
	elm2Y2 = elm2Y1;
				
	elm2X3 = elm2X1;		
	elm2Y3 = elm2Y1 + elm2.offsetHeight;
		
	elm2X4 = elm2X1 + elm2.offsetWidth;		
	elm2Y4 = elm2Y1 + elm2.offsetHeight;		
	*/
		
	//alert("pX1 : " + pX1 + " - pX2 : " + pX2 + " - pX3 : " + pX3 + " - pX4 : " + pX4);
	
	/*	
	if (((elm1X1 >= elm2X1) && (elm1X1 <= elm2X2) && (elm1Y1 >= elm2Y1) && (elm1Y1 <= elm2Y4)) || //is top left point of obj1 inside obj2 
		((elm1X2 >= elm2X1) && (elm1X2 <= elm2X2) && (elm1Y2 >= elm2Y1) && (elm1Y2 <= elm2Y4)) || //is top right point of obj1 inside obj2
		((elm1X3 >= elm2X1) && (elm1X3 <= elm2X2) && (elm1Y3 >= elm2Y1) && (elm1Y3 <= elm2Y4)) || //is bottom left point of obj1 inside obj2
		((elm1X4 >= elm2X1) && (elm1X4 <= elm2X2) && (elm1Y4 >= elm2Y1) && (elm1Y4 <= elm2Y4))) { //is bottom right point of obj1 inside obj2
			
		return true;
	}	
	return false
	*/
}


function MD(e) {
	if (over){
		if (N) {
			ob = document.getElementById("div_keypad");
			X=e.layerX;
			Y=e.layerY;
			return false;
		} else {
			ob = document.getElementById("div_keypad");
			ob = ob.style;
			X=event.offsetX;
			Y=event.offsetY;
		}
	}
	//alert(keypadVisible);
	if(keypadVisible) {	
		showKeypad();
	} else {
		hideKeypad();
	}
}


function MM(e) {
	if (ob) {
		if (N) {
			ob.style.top = e.pageY-Y;
			ob.style.left = e.pageX-X;
		} else {
			ob.pixelLeft = event.clientX-X + document.body.scrollLeft;
			ob.pixelTop = event.clientY-Y + document.body.scrollTop;
			return false;
		}
	}
}

function keypadSubmit() {
	hideKeypad();
	if(document.forms[0]) {
		if(document.forms[0].onsubmit){
			if(document.forms[0].onsubmit()){
				document.forms[0].submit();
			}
		} else {		
			document.forms[0].submit();
		}
	}
}


function MU() {
	ob = null;
}

if (N) {
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}


function initializeKeyboard(overlapObj, activationObj){
	overlapArr = overlapObj;
	keypad = document.getElementById("div_keypad");
	// Keypad will be visible when activation objects are focussed.
	for (i=0; i < activationObj.length; i++) {
		activationObj[i].onmouseover = function() {
			keypadVisible=true;
		}
		activationObj[i].onmouseout = function() {
			keypadVisible=false;
		}
		activationObj[i].onfocus = function() {
			currentInput = this;
			if (document.all) {			
				if(keypad.style.visibility != "visible"){	
					initiateKeyboardPos(0);
					keypad.style.visibility="visible";
				}
			} else if (document.layers) {
				
				if(keypad.style.visibility != "show"){	
					initiateKeyboardPos(0);
					keypad.visibility="show";	
				}

			} else {
				if(keypad.style.visibility != "visible"){
					initiateKeyboardPos(0);
					keypad.style.visibility="visible";
				}
			}	
			//currentInput 		
		}
	}
	scrambleNumbers();
	document.onmousedown = MD;
	document.onmousemove = MM;
	document.onmouseup = MU;	
}