// JavaScript Document
// Script - tms1.js

<!-- ***************************************************************************************************************** -->
// Function to open popup to start session in IE browsers
<!-- ***************************************************************************************************************** -->
function session_start_popup(active) {
	// display the popup if active == true
	if(active == 'true') {
		var w = 400;
		var h = 400;
		var URL = "sess_start_popup.php";
		var attribs;
		attribs = "width=" + w + ",height=" + h + ",";
		attribs += "channelmode=0,dependent=0,directories=0,fullscreen=0,";
		attribs += "location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0";
		popupWin = window.open( URL, "popupWin", attribs);
		popupWin.blur();
	} else {
		return;
	}
	
}

<!-- ***************************************************************************************************************** -->
// Function to select the Webmail Program to use
<!-- ***************************************************************************************************************** -->
function webmailSelection() {
	var msg;
	var app = "";	

	len = document.webmailChoice.webmailApp.length
	for (i = 0; i <len; i++) {
		if (document.webmailChoice.webmailApp[i].checked) {
		app = document.webmailChoice.webmailApp[i].value
		}
	}	
	document.f.goto_uri.value = app;
	
	//msg = "Radio Button clicked\n\n" + app + " selected";
	//alert(msg);
}

<!-- ***************************************************************************************************************** -->
// Function to  show or hide a DIV
<!-- ***************************************************************************************************************** -->
function reverseContentDisplay(divID, linkID) {
	if(divID.length < 1) {
		return;
	}
	if(document.getElementById(divID).style.display == "none") {
		document.getElementById(divID).style.display = "block";
		//document.getElementById(linkID).src = "images/hide.gif";
		document.getElementById(linkID).title = "Click to hide this content.";
	}else{
		document.getElementById(divID).style.display = "none";
		//document.getElementById(linkID).src = "images/show.gif";
		document.getElementById(linkID).title = "Click to show this content.";
	}
}

<!-- ***************************************************************************************************************** -->
// Generate a random password - userEditForm
<!-- ***************************************************************************************************************** -->
function generateRanPass() {
	var ranPass;
	var msg;
	// generate
	ranPass = randomString(7);
	
	// update fields
	document.getElementById('password').value = ranPass;
	document.getElementById('password2').value = ranPass;
	
	msg = "Your randomly generated password is:  " + ranPass + "\n\nPlease make a note of it.";
	msg += "\n\nThe 'Password' and 'Re-type Password' fields have been automatically filled with your random password";
	msg += "\n\nPlease note that your password is case sensitive.";
	alert(msg);
}

<!-- ***************************************************************************************************************** -->
// Random string generator - with length strLen
<!-- ***************************************************************************************************************** -->
function randomString(strLen) {
	var chars = "2346789BCDEFGHKMPRTWXTZbcdefghkmprtwxyz";
	var randomstring = '';
	for (var i=0; i<strLen; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

<!-- ***************************************************************************************************************** -->
// Set a form's action before submitting
<!-- ***************************************************************************************************************** -->
function setFormAction(formName, newAction, msg) {
	document.getElementById(formName).action = newAction;
	if(msg) {
		return confirm(msg);
	} else {
		return true;
	}
}

<!-- ***************************************************************************************************************** -->
// Validate 'Edit User' form
<!-- ***************************************************************************************************************** -->
function validateEditUserForm(formName, id, passID, passConfID) {
	//alert(formName);
	var msg;
	var checkEmpty;
	var list = "id=" + id + "\n";

	// Get fields
	var elmA = document.getElementById(formName).elements;
	for(i=0; i<elmA.length; i++) {

		// Text fields
		if(elmA[i].type == "text") {
			checkEmpty = false;
			// check empty for these fields
			if(elmA[i].name == "firstName") {checkEmpty = true; msg = "First Name";}
			if(elmA[i].name == "lastName") {checkEmpty = true; msg = "Last Name";}
			// handle email
			if(elmA[i].name == "email") {
				if(!validEmail(elmA[i].value, 'Email')) return false;
			// handle the rest
			} else {
				if(!validText(elmA[i].value, msg, checkEmpty)) return false;
			}
		}
		
		// debug
		if(elmA[i].type=="text") {
			list = list + elmA[i].name + " [text]\n";
		}
		if(elmA[i].type=="password") {
			list = list + elmA[i].name + " [password]\n";
		}
	}
	
	// handle password
	if(document.getElementById(passID).value.length < 7) {alert("Your Password must be between 7 and 16 characters long"); return false;}
	if(document.getElementById(passConfID).value == "") {alert("You haven't re-typed your Password"); return false;}
	if(document.getElementById(passID).value != document.getElementById(passConfID).value) {
		alert("Your Password and re-typed Password do not match");
		return false;
	}
	
	// if conf email checked advise contents
	if(document.getElementById('sendConfEmail').checked == true) {
		var newPass = document.getElementById(passID).value;
		if(document.getElementById(passID).value == "UNCHANGED") newPass = "Your password has not been changed"
		msg = "A confirmation email will be sent advising the following:\n\n";
		msg += "Email: " + document.getElementById('email').value + "\n";
		msg += "Password: " + newPass + "\n";
		alert(msg);
	}
	
	// confrim save or not
	msg = "Save changes to '" + document.getElementById('email').value + "'";
	if(confirm(msg)) {
		// parse text fields for quotes
		parseFormTextFields(formName);
		return true;
	} else {
		return false;
	}

}

<!-- ***************************************************************************************************************** -->
// Toggle element visibility
<!-- ***************************************************************************************************************** -->
function toggleVis(id) {
	var el = document.getElementById(id);
	//alert(el.style.display);
	if (el.style.display == 'none') {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
}

<!-- ***************************************************************************************************************** -->
// Generic functions
<!-- ***************************************************************************************************************** -->
// submit a form
function submitForm(formName) {
	document.forms(formName).submit();
}

// Function to find all text fields in a form and parse them
function parseFormTextFields(formName) {
	// Find text fields
	var elmA = document.getElementById(formName).elements;
	for(i=0; i<elmA.length; i++) {
		// Parse text field
		if(elmA[i].type=="text") {
		// parse it
		parseTextFieldById(elmA[i].id);
		}
	}
	return true;
}

<!-- ***************************************************************************************************************** -->
// Function change quotes to HTML code for a text input field
function parseTextFieldById(id) {
	var oldStr;
	var newStr;
	oldStr = document.getElementById(id).value;
	str = "Old Text: " + oldStr + "\n";
	newStr = parseQuotes(oldStr);
	str += "New Text: " + newStr;
	//alert(str);
	document.getElementById(id).value = newStr;

}

<!-- ***************************************************************************************************************** -->
// Convert quote chars to HTML format - primarily for query strings
function parseQuotes(strIn) {
	var strOut = "";
	var cChar;

	for( var i=0; i<strIn.length; i++ ) {
		cChar = strIn.charAt(i);

		if( cChar == '"' )		 {	// Double Quotes
			strOut += "&quot;";
		}else if( cChar == "'" ) { 	// Single quote
			strOut += "&#39;";
		}else{
			strOut += cChar;					// char is okay
		}
		
	}
return strOut;
}

<!-- ***************************************************************************************************************** -->
// Validate Email Address Input
function validEmail(email, msg) {
	
	// Check required field?
	if(email == "required") {
		msg = "Your " + msg + " is required";
		alert(msg);
		return false;
	}

	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	if (AtPos == -1 || StopPos == -1) {
		alert("Your " + msg + " appears to be invalid:\n'" + email + "'\n\nShould be in a format like:\n'yourname@yourdomain.com'");
		return false;
	}

	// Check the email for invalid characters
	for( var i=0; i < email.length; i++ ) {
		cChar = email.charAt(i);
		cc = cChar.charCodeAt(0)

		// Valid
		if((cc > 47 && cc < 58) || (cc > 96 && cc < 123) || (cc > 64 && cc < 91)) {
			//      0-9                    a-z                    A-Z
		// Valid
		} else if( (cc > 44 && cc < 47) || (cc == 64) || (cc == 95) ) {
			//                 -  .               @             _
		// Invalid
		} else {
			// Return the offending char
			msg = "Your " + msg + " contains an invalid character: " + cChar;
			alert(msg);
			return false;
		}

	}

return true;	
}

<!-- ***************************************************************************************************************** -->
// Validate Text Input
function validText(strIn, msg, checkEmpty) {
	//alert('Validating: ' + strIn);
	// acceptable chars: '0-9'; ' ';
	
	var cChar;
	var valid;
	valid = true;
	
	// Check for empty string?
	if(checkEmpty && !strIn) {
		msg = "You haven't entered your " + msg;
		alert(msg);
		return false;
	}

	// Check required field?
	if(strIn == "required") {
		msg = msg + " is required";
		alert(msg);
		return false;
	}

	// Check the string for invalid characters
	for( var i=0; i < strIn.length; i++ ) {
		cChar = strIn.charAt(i);
		cc = cChar.charCodeAt(0)

		// Valid
		if((cc > 47 && cc < 58) || (cc > 96 && cc < 123) || (cc > 64 && cc < 91)) {
			//      0-9                    a-z                    A-Z
		// Valid
		} else if( (cc == 32) || (cc == 39) || (cc > 43 && cc < 48) || (cc == 64) || (cc == 92) || (cc == 95) ) {
			//         space           '              ,  -  .  /             @             \              _
		// Invalid
		} else {
			// Return the offending char
			msg = "Your " + msg + " contains an invalid character: " + cChar;
			alert(msg);
			return false;
		}

	}

	return true;
}


