// JavaScript Document
var bookmarkurl = "http://www.webpower.pt"
var bookmarktitle = document.title
function addfav(){
	if (document.all)
	window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

var popUp = null;

function NewWindow(mypage, myname, w, h, scroll, resize) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resize+''
	popUp = window.open(mypage, myname, settings)
	if (popUp.window.focus) { popUp.window.focus(); }
}

/*
the third argument to changeKey should be a function function exampleKeyChecker (keyCode, key)
which returns an object { cancelKey: boolean, replaceKey: boolean, newKeyCode: number, newKey:string }
Not all properties need to be present, if cancelKey is set to true the other properties are not needed.
If replaceKey is set to true then at least newKeyCode needs to be set.
the fourth argument is an object used to allow others Keys like (backspace)
all properties need to be present...
{ backaspaceKey: boolean }
*/
function CheckKey (textControl, evt, keyChecker,opts) {
	var keyCode = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode : evt.which ? evt.which : void 0;
	var key;
	if (keyCode) 
		key = String.fromCharCode(keyCode);
	var keyCheck = keyChecker(keyCode, key);
	if(!SpecialOptsKeys(keyCode,opts))
	{
		if (keyCode && window.event && !window.opera) {
			if (keyCheck.cancelKey)
				return false;
			else if (keyCheck.replaceKey) {
				window.event.keyCode = keyCheck.newKeyCode;
				if (window.event.preventDefault)
					window.event.preventDefault();
				return true;
			}
			else
				return true;
		}
		else if (typeof textControl.setSelectionRange != 'undefined') {
			if (keyCheck.cancelKey) {
				if (evt.preventDefault)
					evt.preventDefault();
				return false;
			}
			else if (keyCheck.replaceKey) {
				// cancel the key event and insert the newKey for the current selection
				if (evt.preventDefault)
					evt.preventDefault();
				var oldSelectionStart = textControl.selectionStart;
				var oldSelectionEnd = textControl.selectionEnd;
				var selectedText = textControl.value.substring(oldSelectionStart,oldSelectionEnd);
				var newText = typeof keyCheck.newKey != 'undefined' ? keyCheck.newKey : String.fromCharCode(keyCheck.newKeyCode);
				textControl.value = textControl.value.substring(0, oldSelectionStart) + newText + textControl.value.substring(oldSelectionEnd);textControl.setSelectionRange(oldSelectionStart + newText.length,oldSelectionStart + newText.length);
				return false;
			}
			else {
			return true;
			} 
		}
		else if (keyCheck.cancelKey) {
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			return false;
		}
		else {
		return true;
		}
	}else
		return true;
}
// options	{ backaspaceKey: boolean}
function SpecialOptsKeys(keyCode,opts){
	var result = false;
	if(opts.backaspaceKey)
		if(keyCode == 8)
			result = true;
	if(opts.ponto)
		if(keyCode == 46)
			return true;
	if(opts.negativo)
		if(keyCode==45)
			return true;
	return result;
}

// apenas permite a introdução de digitos returnamdo "true" quando a key de entrada não o for...
function Cancel_No_Digits (keyCode, key) {
  return { cancelKey: !isDigit(keyCode, key) };
}

// apenas permite a introdução de digitos e "/" returnamdo "true" quando a key de entrada não o for...
var DTSplit_Code = 47; //    "/"
function Cancel_No_Date (keyCode, key) {
  return { cancelKey: isDigit(keyCode, key) ? false : keyCode == DTSplit_Code ? false : true };
}

function isDigit (keyCode, key) {
  return "0123456789".indexOf(key) != -1;
}

/**
*	Como invocar:
* <input onkeypress='return CheckKey(this, event, Cancel_No_Digits,{ backaspaceKey: true,ponto:true/false});'>
*/
