// NOTE: duplicate functions in /asp/codeUtilsServer.asp
//
// validate User checks to see if validation code is present in user code
function validateUser(code)
{
    if (code == "") return 0;
    
    else    
    {
        var tempCode= decrypt(code);
        if(tempCode == "" || tempCode.indexOf("682") == -1) 
			return 0;
        else 
            return code;
    }
}

//returns an encoded string using a straight n digit vigenere cipher key
function encrypt(str)
{
	var reference= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	var refLength= reference.length; // is 62;
	// add valid user code to login
	str += "682";
	var strLength= str.length;
	var position, key;
	var startKey= 4;
	var stopKey= 7;
	var curChar= "";
	var startIndexChar= 0;
	var newIndexChar= 0;
	var overflow= 0;
	var encodeStr= "";
	if (strLength < 3) return "";
	
	for (position= 0; position < strLength; position++)
	{
		for (key= startKey; key < stopKey; key++)
		{
			if (position >= strLength) return encodeStr;
			
			curChar= str.substring(position, position + 1);
			startIndexChar= reference.indexOf(curChar);
			if (startIndexChar == -1) return "";
			
			newIndexChar= startIndexChar + key;
			overflow= newIndexChar - refLength;
			if (overflow >= 0) newIndexChar= overflow;
			
			encodeStr += reference.substring(newIndexChar, newIndexChar + 1);
			if (key < stopKey - 1) position++;
		}	
	}
	return encodeStr; 
}

//returns a decoded string using a straight n digit vigenere cipher key
function decrypt(code)
{
	if (code == "") return "";
	
	var reference= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	var refLength= reference.length; // is 62;
	var codeLength= code.length;
	var position, key;
	var startKey= 4;
	var stopKey= 7;
	var curChar= "";
	var startIndexChar= 0;
	var newIndexChar= 0;
	var underflow= 0;
	var decodeStr= "";
	if (codeLength < 3) return "";
	
	for (position= 0; position < codeLength; position++)
	{
		for (key= startKey; key < stopKey; key++)
		{
			if (position >= codeLength) return decodeStr;
			
			curChar= code.substring(position, position + 1);
			startIndexChar= reference.indexOf(curChar);
			if (startIndexChar == -1) return "";
			
			newIndexChar= startIndexChar - key;
			underflow= newIndexChar;
			if (underflow < 0) newIndexChar= refLength + underflow;
			
			decodeStr += reference.substring(newIndexChar, newIndexChar + 1);
			if (key < stopKey - 1) position++;
		}	
	}
	return decodeStr; 
}

// Set user code and name in top level frameset
function setUserCode(code, name)
{
	top.UserCode= (code == "")? 0:code;
	top.UserName= name;
}

// If user code and name are in current href, set them
function setupUserCode()
{
	var curArgs= getArgs(location.href);
	var uc= getArgValue(curArgs, "uc");
	if(uc != "" && uc != 0)
	{
		var un= getArgValue(curArgs, "un");
		setUserCode(uc, un);
	}
}

