/*
	Javascript code written by PSLWeb.co.uk for Find a Tea Room (www.findatearoom.co.uk)
	Copyright ©2007 PSLWeb.co.uk - All Rights Reserved.
*/
/*
	Function:    validate
	Called From: add_basic.php form addBasicForm Submit button (onsubmit method)
	Description: Checks all required form fields to see if any are blank. If so then it highlights
					the Label text and sets the focus to that field (bottom up). Form submission is
					aborted if any fields are in error.
*/
function validate(loginForm) {
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('userID', 'pwd');
	// Count backwards so we highlight first field in error on the form
	for (i = fields.length - 1; i >= 0; i--) {
		x = fields[i]; // Get this field name
		if (loginForm[x].value == '') { // Is it blank?
			loginForm[x].focus(); // Set the focus here
			document.getElementById(x + 'Label').className = 'error'; // Label text shows error class
			errorFound = true;
		}
		else
			document.getElementById(x + 'Label').className = ''; // If no error clear error class
	}
	if (errorFound) { // Pop-up error window
		alert("Please complete required form fields");
		return false;
	}
	else
		return true;
}

/*
	Function:    setFocus
	Inputs:      fieldIdfr: integer denoting the field in error (top down)
	Called From: Body element (onload method) - only if server-side form validation error detected
	Description: Sets the focus to the first field that server-side code has detected as being in
					error.
*/
function setFocus(fieldIdfr) {
	var loginForm = document.getElementById('loginForm');
	// Firstly, enable form elements
	loginForm.submitBtn.disabled = false;
	if (fieldIdfr > 0) { // If server-side script sets error field
		var fields = new Array('null', 'userID', 'pwd');
		var x = fields[fieldIdfr]; // Get the field name in error
		loginForm[x].focus(); // Set the focus
	}
}


function charToNum(inChar) {
	var validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if (validChars.indexOf(inChar) == -1)
		return 62;
	else
		return validChars.indexOf(inChar);
}

function NumToChar(inNum) {
	var validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if ((inNum >= 0) && (inNum < 62))
		return validChars.charAt(inNum);
	else
		return " ";
}

function HexToDec(inHex) {
	var hexChars = "0123456789abcdef";
	return hexChars.indexOf(inHex.charAt(0)) * 16 + hexChars.indexOf(inHex.charAt(1));
}

function DecToHex(inDec) {
	var hexChars = "0123456789abcdef";
	return hexChars.charAt(Math.floor(inDec / 16)) + hexChars.charAt(inDec % 16);
}

function strip(inStr) {
   	while (inStr.substring(0, 1) == ' ')
		inStr = inStr.substring(1);
   	while (inStr.substring(inStr.length-1, inStr.length) == ' ')
		inStr = inStr.substring(0, inStr.length-1);
   	return inStr;
}

function encryptData(loginForm) {
	var blanks = "                        ";
	var eCodeIdx = 0;
	var eCodeUID = 0;
	var eCodePwd = 0;
	var shiftUID = 0;
	var shiftPwd = 0;
	var uIDChar = 0;
	var pwdChar = 0;
	// Get Form values
	var RStr = loginForm.RStr.value;
	var userID = strip(loginForm.userID.value);
	var password = strip(loginForm.pwd.value);
	// Remove Form elements - set values to empty
	loginForm.RStr.value = "";
	loginForm.userID.value = "";
	loginForm.pwd.value = "";
	// Remove Form elements - set name to empty (should prevent submission)
	loginForm.RStr.name = "";
	loginForm.userID.name = "";
	loginForm.pwd.name = "";
	var passwordMD5 = hex_md5(password);
	var mergeStr = "";
	var EncryptUID = "";
	var EncryptPwd = "";
	if (userID.length < 16)
		userID += blanks.substr(0, 16 - userID.length);
	if (password.length < 16)
		password += blanks.substr(0, 16 - password.length);
	for (i = 0; i < 32; i++)
		mergeStr += RStr.charAt(i) + passwordMD5.charAt(i);
	var eCode = hex_md5(mergeStr);
	for (i = 0; i < 16; i++) {
		eCodeUID = HexToDec(RStr.substr(eCodeIdx, 2));
		eCodePwd = HexToDec(eCode.substr(eCodeIdx, 2));
		uIDChar = userID.substr(i, 1);
		pwdChar = password.substr(i, 1);
		shiftUID = (eCodeUID + charToNum(uIDChar)) % 256;
		shiftPwd = (eCodePwd + charToNum(pwdChar)) % 256;
		EncryptUID += DecToHex(shiftUID);
		EncryptPwd += DecToHex(shiftPwd);
		eCodeIdx = (eCodeIdx + 2) % 32;
	}
	return EncryptUID + EncryptPwd;
}

function processForm() {
	var loginForm = document.getElementById('loginForm');
	if (validate(loginForm)) {
		loginForm.rStrRep.value = encryptData(loginForm);
		return true;
	}
	else
		return false;
}