function popUpLicense() { // http://javascript.internet.com/generators/popup-window.html
	URL = "license.php" ;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400,left = 212,top = 184');");
}
function getTLCert(testid) {
	URL = "tl-cert-view.php?testid=" + testid;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=1000,height=800,left = 75,top = 50');");
}
function getPSCert(testid) {
	URL = "cert-view.php?testid=" + testid;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=1000,height=800,left = 75,top = 50');");
}
function moveToList(src, tgt) {

	if(src.options.selectedIndex==-1) {
		alert("Please select an item from the list."); 
		return;
	}
	
	if(src.options.length==0) return;
	
	var Acurrlist = optionsToArray(tgt, "%"); // make array of current items in the target list
	var i;
	
	for(i = 0; i < src.options.length; i++) { // add to the array the selected items from source list 
		if(src.options[i].selected) {
			if(!alreadyInList(src.options[i].text, tgt)) {
				Acurrlist[Acurrlist.length] = src.options[i].text + "%" + src.options[i].value;
			}
		}
	}
	
	tgt.length = 0; // clear target list 
	
	for(i = 0; i < Acurrlist.length; i++) { // add selected items to target list
		tv = Acurrlist[i].split('%');
		tgt.options[tgt.options.length] = new Option(tv[0], tv[1]);
	}
	
	for(i = 0; i < src.options.length; i++) { // clear selected items from source list
		if(src.options[i].selected) {
			src.options[i] = null;
		}
	}

}

function optionsToArray(sel, delim) {

	Aretval = new Array();
	var i;
	
	for(i=0; i<sel.length; i++) {
		Aretval[i] = sel.options[i].text + delim + sel.options[i].value;
	}
	
	return Aretval;
}

function alreadyInList(item, sel) {
	var i;
	for(i = 0; i < sel.options.length; i++) {
		if(item == sel.options[i].text) {
			return true;
		}
	}
	return false;
}
	
function selToArray(selfield) {
	var retval=new Array();
	for(i=0; i<selfield.options.length;i++) {
		retval[i] = selfield.options[i].value;
	}
	return retval;
}

function updateMembershipString(selfield, inpfield) {
	inpfield.value=selToArray(selfield).join('~');
//	return false;
	return true;
}

function showMissedQuestions(qnum) {
	var mq='missedquestion'+qnum;
	var mqelement=document.getElementById(mq);
	if(mqelement.style.display=="none") {
		mqelement.style.display="";
	}
	else {
		mqelement.style.display="none";
	}
}


// Prompts the user to confirm his/her deletion before proceding.
// Requires a string "description" of the item being deleted
// Requires a string "deleteURL" which is the URL of the page 
//                               that actually deletes the item
function confirmDelete(description, deleteURL) {
	if(confirm('Are you sure you wish to delete ' + description + '?')) location.replace(deleteURL); 
}

// Used with list pages to display an extra "new" link
// in the event that the link beneath the list is bebeath the fold
function displayCheck(linkID) {
	if (typeof(window.innerHeight)=='number') // FF
	{
		if (document.body.offsetHeight + 5 >= window.innerHeight)
		{
			document.getElementById("newLink").style.display = "block";
			// add 5 to the offset height -- a straight >
		}
	}
	else if (document.body && document.body.clientHeight)  // IE
	{
		if (document.documentElement.scrollHeight >= (document.documentElement.clientHeight + 15))
		{
			document.getElementById("newLink").style.display = "block";
		
		// add 15 to the client height -- a straight >
		}
	}
}
function hideShowCorrect(ckbox) {
	if (ckbox.checked == true) {
		document.getElementById("showCorrectRow").style.display = "table-row";
	}
	else {
		document.getElementById("correct_ck").checked = false;
		document.getElementById("showCorrectRow").style.display = "none";
	}
}
function displayShowCorrect() {
	if (document.getElementById("show_result").checked == true) {
		document.getElementById("showCorrectRow").style.display = "table-row";
	}
}
// check to see if any of the fields on the tl_payment page are blank
function validatePaymentFields() {

	if ( !document.makepayment.license.checked ) {
		alert("You must accept the license agreement before your payment can be processed.");
		document.makepayment.subbutton.disabled = false;
		document.makepayment.subbutton.value = "Submit Payment";
		return;
	}
	if (document.makepayment.test_id.type == "radio") {
		var x = false;
		for (var i=0; i < document.makepayment.test_id.length; i++)
		{
			if (document.makepayment.test_id[i].checked == true)
					x = true;
		}
		if ( x != true ) {
			alert("Please select one of the tests.");
			document.makepayment.subbutton.disabled = false;
			document.makepayment.subbutton.value = "Submit Payment";
			return;
		}
	}
	first = document.makepayment.first.value;
	last  = document.makepayment.last.value;
	email = document.makepayment.email.value;
	cc_number = document.makepayment.cc_number.value;
	cc_name = document.makepayment.cc_name.value;
	cc_month = document.makepayment.cc_month.value;
	cc_year	 = document.makepayment.cc_year.value;
	cc_address = document.makepayment.cc_address.value;
	cc_zip = document.makepayment.cc_zip.value;

//	alert(first + "-" + last  + "-" + email  + "-" + cc_number  + "-" + cc_name  + "-" + cc_month  + "-" + cc_year  + "-" + cc_address  + "-" + cc_zip);
	if ( checkString(first) || checkString(last) || checkString(email) || checkString(cc_number) || checkString(cc_name) || checkString(cc_address) || checkString(cc_zip)) {
		alert(" All fields are required for payment processing. Please complete the entire form.");
		document.makepayment.subbutton.disabled = false;
		document.makepayment.subbutton.value = "Submit Payment";
		return;
	}
	if ( !isValidEmail(email) ) {
		alert("Please enter a valid email address.")
		document.makepayment.subbutton.disabled = false;
		document.makepayment.subbutton.value = "Submit Payment";
		document.makepayment.email.focus();
		return;
	}
//	if ( !isValidZipCode(cc_zip) ) {
//		alert("Please enter a valid zip code.")
//		document.makepayment.subbutton.disabled = false;
//		document.makepayment.subbutton.value = "Submit Payment";
//		document.makepayment.cc_zip.focus();
//		return;
//	}
	document.makepayment.cc_number.value = stripCharsInBag(cc_number, "-");

	document.makepayment.subbutton.disabled = false;
	document.makepayment.subbutton.value = "Submit Payment";
	
	document.makepayment.submit()	
}
function validateAcctSignupFields() {

	name = document.acctsignup.PSClient_Name.value;
	desc  = document.acctsignup.PSClient_Description.value;
	code = document.acctsignup.PSClient_Code.value;
	first = document.acctsignup.First_Name.value;
	last  = document.acctsignup.Last_Name.value;
	email = document.acctsignup.Login.value;
	pass = document.acctsignup.Password.value;
	cc_number = document.acctsignup.CC_Number.value;
	cc_name = document.acctsignup.CC_Holder.value;
	cc_month = document.acctsignup.cc_month.value;
	cc_year	 = document.acctsignup.cc_year.value;
	cc_address = document.acctsignup.CC_Address.value;
	cc_zip = document.acctsignup.CC_Zip.value;

//	alert(first + "-" + last  + "-" + email  + "-" + cc_number  + "-" + cc_name  + "-" + cc_month  + "-" + cc_year  + "-" + cc_address  + "-" + cc_zip);
	if ( checkString(name) || checkString(desc) || checkString(code) || checkString(pass) || checkString(first) || checkString(last) || checkString(email) || checkString(cc_number) || checkString(cc_name) || checkString(cc_address) || checkString(cc_zip)) {
		alert(" All fields are required for payment processing. Please complete the entire form.");
		document.acctsignup.subbutton.disabled = false;
		document.acctsignup.subbutton.value = "Signup";
		return;
	}

	if ( !document.acctsignup.license.checked ) {
		alert("You must accept the license agreement before your payment can be processed.");
		document.acctsignup.subbutton.disabled = false;
		document.acctsignup.subbutton.value = "Signup";
		return;
	}
	if ( !isValidEmail(email) ) {
		alert("Please enter a valid email address.")
		document.acctsignup.subbutton.disabled = false;
		document.acctsignup.subbutton.value = "Signup";
		document.acctsignup.Login.focus();
		return;
	}
//	if ( !isValidZipCode(cc_zip) ) {
//		alert("Please enter a valid zip code.")
//		document.acctsignup.subbutton.disabled = false;
//		document.acctsignup.subbutton.value = "Signup";
//		document.acctsignup.CC_Zip.focus();
//		return;
//	}
	document.acctsignup.CC_Number.value = stripCharsInBag(cc_number, "-");

	document.acctsignup.subbutton.disabled = false;
	document.acctsignup.subbutton.value = "Signup";
	
	document.acctsignup.submit()	

}
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}
// Check that string theField.value is not empty 
// and not all whitespace.
function checkString (theField) {
	if (isEmpty(theField) || isWhitespace(theField)) {
		return true; 
	}
    return false;
}
function isWhitespace (s) {
   var i;
 	var whitespace = " \s\t\n\r";
	
   // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
function isValidEmail(theField) {

	var str = theField + "";

	// Grab everything before the '@'
	namestr = str.substring(0, str.indexOf("@"));  
	// Grab everything after the '@'
	domainstr = str.substring(str.indexOf("@")+1, str.length);

	//RULES: 
	// (1) namestr cannot be empty, or that would indicate no 
	//     characters before the '@. 
	// (2) domainstr must contain a period that is not the first 
	//		 character (i.e. right after the '@').  
	// (3) The last character must be an alpha.
  	if ( isEmpty(namestr) || isWhitespace(namestr) || domainstr.indexOf(".") <= 0 || domainstr.indexOf("@") != -1 || !isAlpha(str.charAt(theField.length-1)) ) 
	{
				return false;
	}
   
   return true;
}
// Returns true if string s contains only alpha characters.
function isAlpha(s) {
	// Return immediately if an invalid value was passed in
	if (s+"" == "undefined" || s+"" == "null" || s+"" == "")	
		return false;

	var isValid = true;

	// Convert to a string for performing string comparisons.
	s += "";	

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  	for (i = 0; i < s.length; i++) {
		// Alpha must be between "A"-"Z", or "a"-"z"
		if ( !( ((s.charAt(i) >= "a") && (s.charAt(i) <= "z")) ||
      			((s.charAt(i) >= "A") && (s.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
		}
   }
   
	return isValid;
}
function isValidZipCode(theField, s) {
 	var s2 = theField;
	var s1 = "";
	
	s2 += "";
	s1 = s2.toUpperCase();
	
	for ( i = 0; i < s1.length; i++ ) {
		if (!( (s1.charAt(i) == '-') || ( (s1.charAt(i) >= '0') && (s1.charAt(i) <=  '9') ) )) {
		   return false;
		}
	}

	return true;
}
// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}
function displayPayConfirmation(msg) {
	alert(msg);
}
function hideMenuOptions() {
	document.getElementById("home").style.display = "none";
	document.getElementById("tests").style.display = "none";
	document.getElementById("results").style.display = "none";
}
function displayTLCertificateAlert() {
	alert("Use your browser's Print option to print this certificate.");
}
function displayPSCertificateAlert() {
	alert("Use your browser's Print option to print this certificate.\n\nFor best results, print in landscape with no headers or footers.");
}
function popUpSecurity() { // http://javascript.internet.com/generators/popup-window.html
URL = "security.php"
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=380,left = 212,top = 184');");
}
function validateUserFields() {

	first = document.userform.First_Name.value;
	last  = document.userform.Last_Name.value;
	email = document.userform.Login.value;

	if ( checkString(last) ) {
		alert(" Please enter a Last Name for this user. ");
		document.userform.Last_Name.focus();
		return;
	}
	if ( checkString(first) ) {
		alert(" Please enter a First Name for this user. ");
		document.userform.First_Name.focus();
		return;
	}
	if ( checkString(email) ) {
		alert(" Please enter an Email address for this user. ");
		document.userform.Login.focus();
		return;
	}
	if ( !isValidEmail(email) ) {
		alert("Please enter a valid email address.")
		document.userform.Login.focus();
		return;
	}
	document.userform.submit();
}
function validateTestQuestions() {

	var found_it = false ;
	
	for (var i=0; i<document.test.Correct_Answer.length; i++)  {

		if (document.test.Correct_Answer[i].checked)  {
			found_it = true ;
		}
	}
	if (found_it) {
		document.test.submit()	;
	}
	else {
		alert("Please select a correct answer for this question.");
	}

}
function setBillingTestType(tType) {

	if ( tType == "history" )
			document.monthlybilling.testType.value = "history";
	else if ( tType == "query" ) 
			document.monthlybilling.testType.value = "query";
	else
			document.monthlybilling.testType.value = "users";

	document.monthlybilling.submit();
}
function stripAvailable(available,selected) {
	if ( selected.length > 0 ) {
		for (i = 0; i < selected.length; i++)
		{   
			x = selected.options[i].value;

			for (i2 = 0; i2 < available.length; i2++)
			{   
				if ( available.options[i2].value == x ) {
					available.options[i2] = null;
					break;
				}
			}
		}
	}
}
