﻿//	THREE STEPS TO INSTALL Session Key format:
//
//	1.  Include this script into the HEAD of your HTML document
//	2.  Add the onLoad event handler into the BODY tag
//		<BODY onLoad="document.dateTest.testFormat1.focus()">
//	3.  Put the this code into the <INPUT HTML element
//			<input type="text" name="testFormat1" size='11' maxlength="11" onFocus="" value="12345"
//			     onKeyUp="checkFormat( this, this.value, event )" 
//			     onBlur ="checkFormat( this, this.value, event )" >

//	Session separator
var	strSeparator	= "-";

var	oldValue = "#@$*()%^@#_*%^&#@*@*@*@*^%";

/////////////////////////////////////////////////////////////////////////////

function checkFormat(vCtrlName, e)
{
	// vCtrlName - object name
	// sessionID - value in the field being checked
	// e         - event
	if (vCtrlName == null || vCtrlName.value == null)
		return;

	var sessionID = vCtrlName.value;
	if (sessionID == oldValue)
	{
		//alert( "Match: " + sessionID + " = " + oldValue );
		return false;
	}

	var keyCode = 0;
	if (e != null)
		keyCode = (window.Event) ? e.which : e.keyCode;
		
	if( keyCode == 8 ) //  Ignore the Netscape value for backspace. IE has no value
		return	false;

	// Eliminate all the ASCII codes that are not valid
	var alphaCheck = "0123456789";

	var s = vCtrlName.value;
	var size = s.length;
	var i = 0;
	var s2 = "";

	for (i = 0; i < size; i++) {
		var ch = s.charAt(i);
		if (alphaCheck.indexOf(ch) != -1)
			s2 = s2 + ch;
	}
//	alert( "text: " + s2 );

	sessionID = s2;


	//	Reformat session key to xxx-xxx-xxx
	if (sessionID.length > 11)
	{
		var p1 = vCtrlName.value.substr(0, 3);
		var p2 = vCtrlName.value.substr(4, 3);
		var p3 = vCtrlName.value.substr(7, 3)
		sessionID = p1 + strSeparator + p2 + strSeparator + p3;

		oldValue = vCtrlName.value = sessionID;
		return true;
	}
	
	if (sessionID.length >= 6)
	{
		var tmp1 = sessionID.substr(0, 3);
		var tmp2 = sessionID.substr(3, 3);
		var	tmp3 = sessionID.substr(6, sessionID.length - 1);
//		alert("1: tmp1: " + tmp1 + " tmp2: " + tmp2 + " tmp3: " + tmp3);
		sessionID = tmp1 + strSeparator + tmp2 + strSeparator + tmp3;
	}
	else if (sessionID.length >= 3)
	{
		var tmp1 = sessionID.substr(0, 3);
		var tmp2 = sessionID.substr(3, sessionID.length - 1);
//		alert("3: tmp1: " + tmp1 + " .... tmp2: " + tmp2);
		sessionID = tmp1 + strSeparator + tmp2;
	}


	if (sessionID.length > 11)
		sessionID = sessionID.substr(0, 11);

	oldValue		= sessionID;
	vCtrlName.value	= sessionID;

	return	true;
}
/////////////////////////////////////////////////////////////////////////////

