
// global variables

var blnFormSubmit = true;

// global event handlers

// set kmmCheckLogin to document onkeypress, not to body onkeypress cause this will pass wrong event in FF
document.onkeypress = kmmCheckLogin;

// functions

function kmmCheckLogin(evt) {
	evt = (evt) ? evt : event;
	var keyValue = 13; // 'Enter' key value
  var keyCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (keyCode == keyValue) { // 'Enter' key is pressed
  	// returnValue settable in both IE and FF
  	evt.returnValue = false;
  	if (evt.preventDefault) {
  		evt.preventDefault();
  	}
		try {
			// keyCode not settable in FF, gives error 'setting a property that has only a getter'
	  	evt.keyCode = 0;
		}
		catch(e) {
		}
		kmmSubmitForm('formLogin');
		return false;
	}
}


function kmmSubmitForm(formId) {
	// submit form with id formId
  var formObj = document.getElementById(formId);
  if (formObj) {
  	if (formObj.submit) {
	  	if (blnFormSubmit) {
	  		formObj.submit();
	  		blnFormSubmit = false;
	  	}
	  }
	}
}


function kmsSetFocus(elmId) {
	var userid = document.getElementById(elmId);
	if (userid) {
		if (userid.focus) {
			userid.focus();
		}
	}
}


function kmmResizeInnerWindow() {
	 
	var clientHeight = parseInt(document.body.clientHeight);
	var clientWidth = parseInt(document.body.clientWidth);
	var unit = 'px';
	var innerWindow = document.getElementById('loginWindow');
	if (innerWindow) {
		 // todo width and height of elm don't show up? why!? find out later...
		var innerWindowWidth = innerWindow.style.width;
		var innerWindowHeight = innerWindow.style.height;
		var innerWindowWidth = 496;
		var innerWindowHeight = 360;
		innerWindow.style.top = parseInt(clientHeight/2) - parseInt(innerWindowHeight/2) + unit;
		innerWindow.style.left = parseInt(clientWidth/2) - parseInt(innerWindowWidth/2) + unit;
	}
}

function kmmResizeDemoWindow() {
	var clientHeight = parseInt(document.body.clientHeight);
	var clientWidth =  parseInt(document.body.clientWidth);
	var unit = 'px';
	var innerWindow = document.getElementById('demoWindow');
	if (innerWindow) {
		 // todo width and height of elm don't show up? why!? find out later...
		var innerWindowWidth = innerWindow.style.width;
		var innerWindowHeight = innerWindow.style.height;
		var innerWindowWidth = 601;
		var innerWindowHeight = 425;
		innerWindow.style.top = parseInt(clientHeight/2) - parseInt(innerWindowHeight/2) + unit;
		innerWindow.style.left = parseInt(clientWidth/2) - parseInt(innerWindowWidth/2) + unit;
	}
	
	var backgroundmiddle = document.getElementById("login_middle_left");
	if (backgroundmiddle) {
	  backgroundmiddle.style.height = (clientHeight - 70 - 70) + unit;	
	}
}

// get inner width based on http://www.quirksmode.org/viewport/compatibility.html
function getInnerMeasurement(dimension) {
	// returns x or y measurement depending on argument
	if (dimension) {
		var x = 800; //default value to return at least something
		var y = 600; //default value to return at least something
		if (self.innerHeight) {
			// all except Explorer
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			// Explorer 6 Strict Mode
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) {
			// other Explorers
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		if (dimension == 'x') {
			return x;
		}
		else if (dimension == 'y') {
			return y;
		}
	}
	else {
		//always return something
		var defaultdimension = (x < y) ? x : y;
		return defaultdimension;
	}
}


function kmmInitLicense(licenseneeded) {
  if (licenseneeded.toLowerCase() == 'true') {
    var licneeded = document.getElementById('licneeded');
    if (licneeded) {
      licneeded.style.display = '';
    }
  }
}


function kmmLicenseNeeded() {
  top.location.href = '?licenseaction=newlicense&sid=<#sid>';
}

