function CopyEmail(tbEmail, tbPassword, tbPasswordConfirm) {
    tbPassword.value = tbEmail.value; 
    tbPasswordConfirm.value = tbEmail.value;
}

function toggleVisibility(DIV_ID) {
     if (document.getElementById(DIV_ID).style.display == "block") { document.getElementById(DIV_ID).style.display = "none" }
     else { document.getElementById(DIV_ID).style.display = "block" } 
}

function toggleVisibilityOn(DIV_ID) {
     document.getElementById(DIV_ID).style.display = "block" 
}

function toggleVisibilityOff(DIV_ID) {
     document.getElementById(DIV_ID).style.display = "none"
}

function ShowCount(txtDisplay, inputCheck, max) {
	var counter = 0;
	var selected = 0;
	var total_items = inputCheck.length;
	for(counter = 0; counter < total_items; counter++){
		if(inputCheck[counter].selected) selected++;
	}
	if(selected > max) txtDisplay.style.color = 'red'; 
	else txtDisplay.style.color = 'black';
	txtDisplay.innerHTML = selected + '/' + max;
}

function ShowCountName(txtDisplay, inputCheck, max) {
	var counter = 0;
	var selected = 0;
	var output = '';
	var total_items = inputCheck.length;
	for(counter = 0; counter < total_items; counter++){
		if(inputCheck[counter].selected){
			selected++;
			output = output + '<br>' + inputCheck[counter].text + ' (' + selected + '/' + max + ')';
		}
	}
	if(selected > max) txtDisplay.style.color = 'red'; 
	else txtDisplay.style.color = 'black';
	txtDisplay.innerHTML = output;
}

function TextareaCount(txtDisplay, inputCheck, max) {
	var total_items = inputCheck.length;
	if(total_items > max) txtDisplay.style.color = 'red'; 
	else txtDisplay.style.color = 'black';
	txtDisplay.innerHTML = total_items + '/' + max;
}

function CountCopies(max, origin) {
	var counter = 0;
	var show_next = 1;
	
	for(counter = 1; counter <= max; counter++){
		myDiv = document.getElementById('format'+counter);
		mySelect = myDiv.getElementsByTagName('SELECT')[0];
		myText = myDiv.getElementsByTagName('INPUT')[0];
		
		myDiv.style.display = 'none';
		
		// if in readyOnly mode (Select = disabled), don't show empty DIVs
		if(mySelect.disabled == true){
			if(mySelect.selectedIndex == 0) myDiv.style.display = 'none';
			else myDiv.style.display = 'block';
		}
		
		// else in insert/edit mode
		else{
			// show first DIV and any other if previous DIV was filled in
			if(show_next == 1) myDiv.style.display = 'block';
			// otherwise clear the div and the textbox since they are hidden
			else{
				mySelect.selectedIndex = 0;
				myText.value = "";
			}
			if(mySelect.selectedIndex != 0 && myText.value.length > 0 && show_next == 1) show_next = 1;
			else show_next = 0;
		}
		
	}
	origin.focus(); // fixes IE which looses focus after each keystroke
}

function BookType() {
	var counter = 0;
	var types = 2; // fiction and non-fiction
	
	myDiv = document.getElementById('typeRB');	// this DIV contains radio button list
	
	for(counter = 0; counter < types; counter++){
		myRB = myDiv.getElementsByTagName('INPUT')[counter];
		myTempDiv = document.getElementById('type'+counter);
		if(myRB.checked == true) myTempDiv.style.display = 'block';
		else{
			mySelects = myTempDiv.getElementsByTagName('SELECT');
			var i = 0;
			for(i; i<mySelects.length; i++) mySelects[i].selectedIndex = 0;
			myTempDiv.style.display = 'none';
		}
	}
}
// client side form validation


// "contact us" e-mail form
function checkForm_ContactUs(srcForm) {
	var textarea_max = 1000;
	with (srcForm) {
		
		if (checkRadio(recipient,"Please note: One department (e-mail) must be selected.", 1, 1) == false) {
			for(var counter = 0; counter < recipient.length; counter++) {
				td = recipient[counter].parentNode.style.backgroundColor = '#ff9900';
			}
			return false;
		}
		/* subject drop down removed per director's request 2009/02/09	
		if (checkLength(subject,"Please note: A subject is required.", 5, 50) == false) {
			subject.focus();
			return false;
		}
		*/
		if (checkText(realname,"Please note: A name is required.") == false) {
  			realname.focus();
			return false;
		}
		
		if (checkEmail(email,"Please note: A valid e-mail is required.") == false) {
  			email.focus();
			return false;
		}
		
		if (checkLength(comments,"Please note: A comment is required, but should not exceed " + textarea_max + " characters.", 5, textarea_max) == false) {
			comments.focus();
			return false;
		}
		
		
		
	}
}

function checkText(srcText, errorText) {
	with (srcText) {
		if (!trim(value)) {
			window.alert(errorText);
			return false;
		}
		else {
			return true;
		}
	}	
}

function checkEmail(srcText, errorText) {
	with (srcText) {
		if (!trim(value) || !isEmail(trim(value))) {
			window.alert(errorText);
			return false;
		}
		else {
			return true;
		}
	}	
}

function checkLength(srcText, errorText, mini, maxi) {
	with (srcText) {
		if (!trim(value) || trim(value).length < mini || trim(value).length > maxi) {
			window.alert(errorText);
			return false;
		}
		else {
			return true;
		}
	}	
}

// checks how many buttons in 'srcRadio' radiobutton group are selected; must be between mini and maxi
function checkRadio(srcRadio, errorText, mini, maxi) {
	var qtySelected = 0;
	for(var counter = 0; counter < srcRadio.length; counter++) {
		srcRadio[counter].parentNode.style.background = 'none';
		if(srcRadio[counter].checked) { qtySelected++; }
	}
	if(qtySelected < mini || qtySelected > maxi) {
		window.alert(errorText);
		return false;
	}
	else {
		return true;
	}
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(srcText)
{
	return srcText.replace(/^\s+|\s+$/g,'');
}

/*
Check if a string is in valid email format. 
Returns true if valid, false otherwise.
*/
function isEmail(srcText)
{
	var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(srcText);
}
