// --------------------------------------------------------------------------
// Copyright © 1999-2010 KB Group (UK) Ltd.  All Rights Reserved.
// www.kbgroupuk.com
// --------------------------------------------------------------------------
// Amendments:
//
// Date		By	Description
// ----		--	---------------------------------------------------------
// 27/02/07	SK	Added trim functionality to input validation routines
// 21/09/06	SK	Added tab character to keypress detection routines
// 02/09/05	AK	Rewrote form validation routines
// 02/06/03	SK	Added version control
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
function NumericOnly(e) { 

	if (!e)
		e = window.event;

	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	// backspace = 8, delete = 46,<- = 37, -> = 39, 9 = tab
	if ((keyCode < 48 || keyCode > 57) && keyCode != 8 && keyCode != 46 && keyCode != 37 && keyCode != 39 && keyCode != 9) { 
		if (document.all)
			keyCode = 0;
		return false;
	}
	return true;
}

// --------------------------------------------------------------------------
function NumericSpOnly(e) { 

	if (!e)
		e = window.event;

	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	if ((keyCode < 48 || keyCode > 57) && keyCode != 32 && keyCode != 8 && keyCode != 46 && keyCode != 37 && keyCode != 39 && keyCode != 9) { 
		if (document.all)
			keyCode = 0;
		return false;
	}
	return true;
}

// --------------------------------------------------------------------------
function CardDate(e) { 

	if (!e)
		e = window.event;

	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	// backspace = 8, delete = 46,<- = 37, -> = 39, 47 = /
	if ((keyCode < 48 || keyCode > 57) && keyCode != 47 && keyCode != 8 && keyCode != 46 && keyCode != 37 && keyCode != 39 && keyCode != 9) { 
		if(document.all)
			keyCode = 0;
		return false;
	}
	return true;
}

// -------------------------------------------------------------------------
function IsValidText(strCheck, iMinLen, iMaxLen, strRegExp, strCheckRangeChars, strCheckChars) {

	strCheck = strCheck.trim();

	var len = strCheck.length;
	if (len < iMinLen || len > iMaxLen)
		return false;

	if (len) {
		if (window.RegExp) {
			var regExp = new RegExp(strRegExp, "i");
			if (!regExp.test(strCheck))
				return false;
		}
		else {
			var rangeLen = strCheckRangeChars.length;
			var checkLen = strCheckChars.length;

			var checkChar, numRangeCharsLeft, testCh1, testCh2, boolValidChar;
			for (var i = 0; i < len; i++) {
				checkChar = strCheck.charAt(i);
				numRangeCharsLeft = rangeLen;
				boolValidChar = false;

				while (numRangeCharsLeft) {
					testCh1 = strCheckRangeChars.charAt(numRangeCharsLeft - 2);
					testCh2 = strCheckRangeChars.charAt(numRangeCharsLeft - 1);
					if (testCh1 <= checkChar && testCh2 >= checkChar) {
						boolValidChar = true;
						break;
					}
					numRangeCharsLeft -= 2;
				}
				if (!boolValidChar && checkLen && (strCheckChars.indexOf(checkChar) != -1))
						boolValidChar = true;

				if (!boolValidChar)
					return false;
			}
		}
	}
	return true;
}

// -------------------------------------------------------------------------
String.prototype.trim = function ()
{
	return this.replace(/^\s+|\s+$/g, '');
}

// -------------------------------------------------------------------------
function isEmail(email) {

	if(window.RegExp) {
		var regExp = /^\w[\.\-_'\w]*@\w(\.?[-\w])*\.([a-z]{3,4}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2,4})?)$/i;
		if (!regExp.test(email))
			return false;
	}
	else {
		var invalidChars = " /;,:#!$^&*()+";
		var len = invalidChars.length;
		var badChar;
		for (var i = 0; i < len; i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0) > -1)
				return false;
		}

		var atPos = email.indexOf("@",1)

		if (atPos == -1 || email.indexOf("@", atPos+1) != -1)
			return false;

		var periodPos = email.indexOf(".",atPos)
		if (periodPos == -1 || (atPos + 2) > periodPos || (periodPos + 3) > email.length)
			return false;
	}
	return true;
}

// --------------------------------------------------------------------------
function clickReturn () {

	var bAgent = window.navigator.userAgent; 
	var bAppName = window.navigator.appName;

	if ((bAppName.indexOf("Explorer") >= 0) && (bAgent.indexOf("Mozilla/3") >= 0) && (bAgent.indexOf("Mac") >= 0))
		return true; 	// follow link
	else
		return false; 	// do not follow link
}

// --------------------------------------------------------------------------
function newWindow (sURL, sWidth, sHeight) {

	var oNewWin = open(sURL,'', 'width='+sWidth+', height='+sHeight+', toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, screenX=500, screenY=200');
}

// --------------------------------------------------------------------------
function launchCenter(url, name, height, width, vscroll) {

	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;

	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;

		str += ",scrollbars=" + vscroll;
	}

	return window.open(url, name, str);
}

// --------------------------------------------------------------------------
function clickBack() {

	// Detect the browser because the back command is different for the different browsers
	var bName = navigator.appName;
	var bVer = parseInt(navigator.appVersion);
	var NS4 = (bName == "Netscape" && bVer >= 4);
	var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);

	if (NS4) { 
		// load browser specific code to go back
		history.go(-1);
	} 
	else {
		// Internet Explorer 4.0+
		history.back();
	}
}

// --------------------------------------------------------------------------
function showHideDiv(div, visibility) {

	if (document.all)
		document.all[div].style.visibility = visibility;
	else if (document.getElementById)
		document.getElementById(div).style.visibility = visibility;
}
