﻿
// Remove space characters
function Trim(sValue)
{		
	sValue = sValue.replace(/(^\s*)|(\s*$)/g, "");
	return sValue;
}

// Return a random number between 0 and iMax-1
function GetRandom(iMax) {
	return Math.floor(Math.random() * iMax);
}

// Resets field value
function ResetField(oField, defaultValue) {
	if (defaultValue == oField.value)
		oField.value = "";
	else 
		if (oField.value == "")
			oField.value = defaultValue;
}

// Check form submit
document.onkeypress = CheckSubmit;
function CheckSubmit(oEvent) {
	oEvent = (oEvent) ? oEvent : event;
	var keyCode;

	if (oEvent.keyCode)
		keyCode = oEvent.keyCode;
	else
		keyCode = oEvent.which;

	if (keyCode == 13)
		if (CurrentSubmit != null)
		{
			CurrentSubmit.click();
			CancelEvent(oEvent);
		}
}

var CurrentSubmit = null;
function SetSubmit(oButton) {
	CurrentSubmit = oButton;
}

function ClearSubmit() {
	CurrentSubmit = null;
}

// Cancel event propagation
function CancelEvent(oEvent)
{
	if (!oEvent)
		return;

	oEvent.cancelBubble = true;
	oEvent.returnValue = false;

	if (oEvent.stopPropagation)
		oEvent.stopPropagation();

	if (oEvent.preventDefault)
		oEvent.preventDefault();
}

// Allows only digits for input
function NumbersOnly(evt) {
	evt = (evt) ? evt : event;
	var c = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	if (c > 31 && (c < 48 || c > 57))
		return false;
	return true;
}

function ClearForm(oForm)
{
	var aInputs = document.getElementsByTagName('input');

	for (i = 0; i < aInputs.length; i++)
	{
		if ((aInputs[i].getAttribute('type') == 'text') && (aInputs[i].getAttribute('defaultValue') == aInputs[i].getAttribute('value')))
			aInputs[i].setAttribute('value', '');
	}

	return true;
}

function ResetForm()
{
	var aInputs = document.getElementsByTagName('input');

	for (i = 0; i < aInputs.length; i++)
	{
		if ((aInputs[i].getAttribute('type') == 'text') && (aInputs[i].getAttribute('defaultValue') == '') && (aInputs[i].getAttribute('value') == '') && 
			(aInputs[i].getAttribute('onfocus') != null))
		{
			var oFocusParam = new String(aInputs[i].getAttribute('onfocus'));
			if (oFocusParam.indexOf("ResetField") > 0)
			{
				oFocusParam = oFocusParam.substring(oFocusParam.indexOf("ResetField") + 18);
				oFocusParam = oFocusParam.substring(0, oFocusParam.indexOf("'"));
				ResetField(aInputs[i], oFocusParam);
			}
		}
	}
}

var state = 'none';
function ToggleDisplay(layer_ref)
{
	if (state == 'block')
		state = 'none';
	else
		state = 'block';

	if (document.all)
		eval( "document.all." + layer_ref + ".style.display = state");
	if (document.layers)
		document.layers[layer_ref].display = state; 

	if (document.getElementById &&!document.all)
	{
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}

function OpenUrl(url)
{
	window.open(url, 'ebook', 'status=0,toolbar=0,location=0,menubar=0,scrollbars=0,resizable=1,width=800,height=600');
	return false;
}

function GetBaseURL(withAppPath)
{
	var appPath = window.location.pathname.substring(0, window.location.pathname.indexOf('/', 2));
	var port = new String(window.location.port);

	if (port != '')
		port = ':' + port;

	var retVal = window.location.protocol + '//' + window.location.hostname + port;
	if (withAppPath)
		retVal = retVal + appPath;
	retVal = retVal + '/'

	return retVal;
}

function ValidateSearch(sTextBox) 
{
	var retVal = true;

	var oText = document.getElementById(sTextBox);
	if ((Trim(oText.value) == '') || (Trim(oText.value).length < 3))
	{
		alert('Por favor, introduza texto com pelo menos 3 caracteres.');
		retVal = false;
	}

	return retVal;
}

