var popup_win = 0;
var win_fixed = 0;

var w;
var h;

var template1;
var template2;
var text;
var page;

var time_out;
var write_timer;
var open_timer;

var scr_x;
var scr_y; 

var hide_menu = 0;
var id_target = "abc";

var id_middle;

var need_adjust = 0; // 5px adjustment in animation
var capital_flag = 0;

var id_first_flag;
var id_second_flag;
var move2_hide_flag;

var direction;

/* ------------------- INVISIBLE TEXT ------------------- */

function togglePlaceHolder(){

	var n = togglePlaceHolder.arguments.length;
	for(i=0; i<n; i++){
		id1 = togglePlaceHolder.arguments[i++];
		id2 = togglePlaceHolder.arguments[i];

		document.getElementById(id1).style.display = 'none';
		document.getElementById(id2).style.display = 'inline';
	}
}

/* --------------------- POPUP WINDOW -------------------- */
//function win_open(span_id, evt, size, media, thisWnd, point) {

function win_open() {

	arg1 = win_open.arguments[0];
	arg2 = win_open.arguments[3];
	arg3 = win_open.arguments[4];
	direction = win_open.arguments[6];


	if(win_open.arguments[2]== "large") {
		h = 0.40 * screen.height;
		w = 0.60 * screen.width;
	}

	if(win_open.arguments[2] == "middle") {
		h = 0.30 * screen.height;
		w = 0.40 * screen.width;
	}

	if(win_open.arguments[2] == "small") {
		h = 0.15 * screen.height;
		w = 0.30 * screen.width;
	}

	if(win_open.arguments[5]== "point") {
		time_out = 400;
	}else{
		time_out = 10;
	}
	open_timer = setTimeout("open_after_delay(arg1, arg2, arg3)", time_out);

}

function open_after_delay(span_id, media, main_wnd) {

	if(document.getElementById) {
		text = document.getElementById(span_id).innerHTML;
	}

	if(document.all) {
		text = document.all[span_id].innerHTML;
	}

	if(popup_win){
		if(!popup_win.closed ) popup_win.close(); 

		//if(!popup_win.closed) return; 
		//if(!popup_win.closed && win_fixed == 1) popup_win.close(); 
		//if(!popup_win.closed && win_fixed == 0) return; 
	}

	if( (scr_x - (w/2)) < 10 ) {
		win_x = 10;
	}else 
	if((scr_x + (w/2)) > screen.width -10) {
		win_x = screen.width - w -10;
	}else {
		win_x = scr_x - (w/2);
	}

	if( (scr_y + h + 80) > screen.height ) {
		win_y = scr_y-h-100;
	}else{
		win_y = scr_y+40;
	}
	if(media == "1") {
	template1 = "<html><head><meta content='text/html'> <link href='viewpage.css' type='text/css' rel='stylesheet' /> <script src='interact.js'></script>  <script src='media.js'></script> <title>LanguageTeach</title></head> ";
	}else {
	template1 = "<html ><head><meta content='text/html'><link href='viewpage.css' type='text/css' rel='stylesheet' /> <script src='interact.js'></script>  <title>LanguageTeach</title></head> ";
	}

	if(direction == "rtl"){
		template1 = template1 + "<body class='theBody_popup' dir='rtl'> ";
	}else{
		template1 = template1 + "<body class='theBody_popup'> ";
	}

	template2 = " </body></html><br /><br />";
	page = template1 + text + template2 ;
	
	win_fixed = 0;
/*
	popup_win = window.open("","MyWin",'width='+w+',height='+h+',top='+(win_y) +',left='+(win_x)+',location=0,directories=0,status=no,menuBar=no,scrollBars=yes,resizable=yes',false); 
*/

//---------------------------------
	var features = 'width='+w+',height='+h+',top='+(win_y) +',left='+(win_x)+',location=0,directories=0,status=no,menuBar=no,scrollBars=yes,resizable=yes';

	//popup_win = window.open("","MyWin",features,false); 
	popup_win = window.open("","",features,false); 
//---------------------------------
	popup_win.document.open();
	popup_win.document.write(page);
	popup_win.document.close();
//	main_wnd.focus();

}

function win_fix() {

//	if(popup_win.closed || win_fixed == 1) return;
//	win_fixed = 1;

	if(popup_win != 0 ){
		popup_win.focus();
	}
}

function win_close() {
	
	//pointer_in = 0;
	clearTimeout(open_timer);
	//clearTimeout(write_timer); 

	if(popup_win) {
		//if(win_fixed == 0) {
			popup_win.close();
			popup_win=0;
		//}
	}
}

function cancel_open() {

	clearTimeout(open_timer);
	//clearTimeout(write_timer); 

}

function update_pointer(evt)
{
	scr_x = evt.screenX;
	scr_y = evt.screenY;
}

/* ------------------- MENU -------------------------------------- */

var cursor_x;
var cursor_y;
var menu_evt;

function showMenu(){

	if(id_target != showMenu.arguments[1] && id_target != "abc" && showMenu.arguments[1].length > 10) {
		doHideMenu(id_target);
	}
	
	if(showMenu.arguments.length == 0){
		return;
	}

	id_anchor = showMenu.arguments[0];
	id_target = showMenu.arguments[1];
	top_menu = showMenu.arguments[2];
	left_menu = showMenu.arguments[3];
	
	menu_evt = 0;
	if(showMenu.arguments.length > 4){
		menu_evt = showMenu.arguments[4];

		if(navigator.appName == "Microsoft Internet Explorer"){

			cursor_x = menu_evt.x + document.body.scrollLeft;
			cursor_y = menu_evt.y + document.body.scrollTop;
		}else{
			cursor_x = menu_evt.pageX;// + window.pageXOffset;
			cursor_y = menu_evt.pageY;// + window.pageYOffset;
		}
	}

	var timeout = 500;

 show_menu_timeout = setTimeout("doShowMenu(id_anchor,id_target,top_menu,left_menu)",timeout);

}


function doShowMenu(){
/*
	if(id_target != showMenu.arguments[1] && id_target != "abc") {
		doHideMenu(id_target);
	}
*/
	var id_anchor = doShowMenu.arguments[0];
	id_target = doShowMenu.arguments[1];
	var top = doShowMenu.arguments[2];
	var left = doShowMenu.arguments[3];

	ancor_x = document.getElementById(id_anchor ).offsetLeft;
	ancor_y = document.getElementById(id_anchor ).offsetTop;
	ancor_h = document.getElementById(id_anchor ).offsetHeight;

	if(hide_menu != 0){
		cancelHideMenu();
	}

	if(menu_evt == 0){
   		menu_x = ancor_x + left + "px";
   		menu_y = ancor_y - ancor_h - top + "px";
	}else{
  		menu_x = cursor_x + left + "px";
   		menu_y = cursor_y - (ancor_h * 2) - top + "px";
	}

      document.getElementById(id_target ).style.top = menu_y;
      document.getElementById(id_target ).style.left = menu_x;
      document.getElementById(id_target ).style.display = 'inline';

}

function hideMenu(){

	clearTimeout( show_menu_timeout );

	var n = hideMenu.arguments.length;
	id = hideMenu.arguments[0];
	var timeout;
	if(hideMenu.arguments[1] > 0){
		timeout = hideMenu.arguments[1];
	}else{
		timeout = 750;
	}

	hide_menu = setTimeout("doHideMenu(id)", timeout );
}

function doHideMenu(){

	var n = doHideMenu.arguments.length;
	var id = doHideMenu.arguments[0];
	if(document.getElementById)
	{
		document.getElementById(id).style.display = 'none';
	}else	{
		document.all[id].style.display = 'none';
	}
}

function cancelHideMenu(){
	clearTimeout(hide_menu);
}

/* -------------- ONLINE TEST----------------------- */

function testForm (arg) {
	var text_to_send = "";
	var text_for_subject = "";
	var text_name = "";
	var text_email_addr = "";
	var len = document.forms[0].length;
	var i;
	var total_question = 0;
	var total_answers = 0;
	var correct_answers = 0;

	
		/* -- Overcheck/Undercheck Test and Calc Score ---- */

	for(i=0; i< len; i++) {

		if(document.forms[0].elements[i].type == "checkbox"){
		
			var buf = document.forms[0].elements[i].name;
			var ret = buf.search(/:/);
 		}
		if(document.forms[0].elements[i].type == "checkbox" && ret == -1){
			total_question++;	
		}

		if(document.forms[0].elements[i].checked &&  ret == -1){
			correct_answers ++;	
		}
		if(document.forms[0].elements[i].checked){
			total_answers ++;	
		}
	}

	if(total_answers > total_question ){
		alert("Overcheked");
		return;
	}
	if(total_answers < total_question ){
		alert("Undercheked");
		return;
	}

	var final_score = (correct_answers / total_question) * 100;
	if(final_score < 0) final_score = 0;
	final_score = Math.round(final_score);

		/* ----- Check name and create report header ------- */

	for(i=0; i< len; i++) {

		if(document.forms[0].elements[i].name == "student_name"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type your first name");
				document.forms[0].elements[i].focus();
				return;
			}
			text_to_send += "Student:  "
			text_to_send += document.forms[0].elements[i].value;
			text_name  += document.forms[0].elements[i].value;
			
		}
		if(document.forms[0].elements[i].name == "reply_email"){
			if( validateEmail(i) ) return;
			text_to_send += "\nE-mail:  ";
			text_to_send += document.forms[0].elements[i].value;
		}

		if(document.forms[0].elements[i].name == "course_info"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type course info");
				document.forms[0].elements[i].focus();
				return;
			}

			text_to_send += "\nCourse: ";
			text_to_send += document.forms[0].elements[i].value;
			//text_name  += "@";
			text_name += ("@" + document.forms[0].elements[i].value);
		
		}

		if(document.forms[0].elements[i].name == "comments"){
			if(document.forms[0].elements[i].value.length > 0){
				text_to_send += "\n\nComments:  ";	
				text_to_send += document.forms[0].elements[i].value;
			}
		}

		if(document.forms[0].elements[i].name == "test_name"){
			text_to_send += "\n\nTest name:  ";
			text_to_send += document.forms[0].elements[i].value;
			text_to_send += "\n-------------------------------------------------\n";
			text_for_subject += document.forms[0].elements[i].value;
		}

/*// ------------- for demo only -------------

		if(document.forms[0].elements[i].name == "email_input"){//for demo only
			if( validateEmail(i) ) return;
		}
*///-----------------------------------------------//
	}

		/*---------- create report body -------------*/

	for(i=0; i< len; i++) { 
		
		if(document.forms[0].elements[i].name == "question"){
			text_to_send += "\n\n";
			text_to_send += document.forms[0].elements[i].value;
			text_to_send += "\n\n";
		}

		if(document.forms[0].elements[i].type == "checkbox"){

			var buf = document.forms[0].elements[i].name;
			var ret = buf.search(/:/);
			//if(buf.search(/:/) == -1 ){
			if(ret == -1 ){
				text_to_send += "YES    ";
			}else{
				text_to_send += "NO   ";
			}

			if(document.forms[0].elements[i].checked){
				text_to_send += "X   ";
			}else{
				text_to_send += "    ";
			}
			
			if( (ret != -1) && (document.forms[0].elements[i].checked)){
				text_to_send += "ERROR    "
			}else{
				text_to_send += "         "
			}
			text_to_send += document.forms[0].elements[i].value;
			text_to_send += "\n";
		}
	}
	
		/*------- fill out hidden fields ------*/

	for(i=0; i< len; i++) { 

		if(document.forms[0].elements[i].name == "report" ){

			text_to_send += ("\n\nScore = " + final_score + "%"); 
			text_to_send += ("\n--------------------------------------------------\nLanguageTeach Technologies. All Rights Reserved.\n"); 
			document.forms[0].elements[i].value = text_to_send;
			//alert(document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "subject" ){

			text_for_subject += (";  Score = " + final_score + "%"); 
			document.forms[0].elements[i].value = text_for_subject;

			//alert("Subject: " + document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "student_info" ){
			document.forms[0].elements[i].value = text_name;
			//alert("Student info: " + document.forms[0].elements[i].value );

		}
/*//---------------- for demo only ------------

		if(document.forms[0].elements[i].name == "email" ){
			document.forms[0].elements[i].value = text_email_addr;	
			//alert("email: " + document.forms[0].elements[i].value );
		}
*///--------------------------------------------
		if(document.forms[0].elements[i].name == "page_location" ){
			document.forms[0].elements[i].value = window.location;	
			//alert("location: " + document.forms[0].elements[i].value );
		}

	}
		/* -------- Submit --------*/

	document.forms[0].submit();
	//document.forms[0].reset.click();

}

function clearForm() {

	var page_location = window.location;
	//page_location = document.URL;
	window.name = "theWindow"
	window.open(page_location, "theWindow");
}

function validateEmail()
{
	var i = validateEmail.arguments[0];
	var eml = document.forms[0].elements[i].value;

	if ( (eml.indexOf('@')== -1)||(eml.indexOf('.')== -1)||(eml.length < 7) ) {
			alert("E-mail is incorrect.");
    			document.forms[0].elements[i].focus();
			return 1;
	}

	if( (eml.length - eml.indexOf('.') )< 3 ){
		alert("E-mail is incorrect.");
		document.forms[0].elements[i].focus();
		return 1;
	}

	var regexp = /[~`! #$%&*()+\|=;:\"\'?<>,\\\/]/;

	position = eml.search(regexp);
	if ( position != -1) {
		alert("Sign \" " + eml.charAt(position) + " \" is prohibited.");
    		document.forms[0].elements[i].focus();
		return 1;
	}

	return 0;
}

//-------------- INSTANT TEXT QUIZ -------------------------------//


function monitorInstantField() {

	var id = monitorInstantField.arguments[0];
	var evt = monitorInstantField.arguments[1];

//alert(evt.keyCode);

	if(evt.keyCode == 13) {
		document.getElementById(id).blur();
	}

	if(evt.keyCode == 27 ) {
		document.getElementById(id).value = "";
	}

}

function clearInstantField( ) {

	var id = clearInstantField.arguments[0];
	document.getElementById(id).style.color = "#000000"; 
	document.getElementById(id).style.weight = "normal"; 
	//document.getElementById(id).value = "";
	//document.getElementById(id).size = document.getElementById(id).name.length+2 ;
}

function checkInstantField ( ) {

	var id = checkInstantField .arguments[0];

	//--------- Trim leading and trailing whitespace chars -----//

	var strReceived = document.getElementById(id).value;
	var len = document.getElementById(id).value.length;

	var begin = 0;
	var end = len-1;

	while(strReceived.charAt(begin) == "\x20" ){
		begin++;
	}
	while(strReceived.charAt(end) == "\x20" || strReceived.charAt(end) == "\x0a" || strReceived.charAt(end) == "\x0d" ){
		end--;
	}
	
	var strTrimed = strReceived.slice(begin,++end);
	strTrimed = strTrimed.replace(/\s{1,}/g, " ");//remove extra spaces
	if(strTrimed.length < 1) return;

	var strRef = document.getElementById(id).name;
	strRef = strRef.replace(/`/g, "'");//backward replace accent with apostrophe

	//--------------------------------------------------------//

	if(strTrimed == "help" || strTrimed == "?"  ) {
		document.getElementById(id).value = strRef;
		document.getElementById(id).style.color = "#0000FF"; 
		return;
	}

	if(strTrimed == strRef){

	  document.getElementById(id).style.color = "#008000";
	  document.getElementById(id).value = strTrimed;// + " OK" 

	}else{
	   if( document.getElementById(id).value.length > 0 ){
		document.getElementById(id).style.color = "#FF0000"; 
		document.getElementById(id).value = strTrimed;// + " ERR";
	   }
	}
}

//------------------ INSTANT MENU QUIZ --------------------//

function reset_quiz_menu() {

	var arg = reset_quiz_menu.arguments[0];
	var select_id = arg.id; 
	/*document.getElementById(select_id).style.backgroundColor = "#FFFFFF";*/
	document.getElementById(select_id).style.color = "#000000";
}

function check_option() {

	var element = check_option.arguments[0];
	var element_id = element.id; 
	var num = document.getElementById(element_id).selectedIndex;
	var answer = document.getElementById(element_id).options[num].value;
	if(answer == "yes"){
	/*document.getElementById(element_id).style.backgroundColor = "#80ff80";*/
		document.getElementById(element_id).style.color = "#008000";
	}else{
	/*document.getElementById(element_id).style.backgroundColor = "#ff8080";*/
		document.getElementById(element_id).style.color = "#CC0000";
	}
 }


//------------------ ANIMATION ---------------------------//

function movement() {

	id_first = movement.arguments[0];
	id_second = movement.arguments[1];
	id_third = movement.arguments[2];
	id_fourth = movement.arguments[3];
	id_middle = movement.arguments[4];
//---------------------
	move2_hide_flag = 0;

	if(document.getElementById(id_first).className == "move2_hide" ||
	   document.getElementById(id_second).className == "move2_hide" ){
		move2_hide_flag = 1;
	}

	if(move2_hide_flag ){
		id_first_flag = 1;
		id_second_flag = 0;

		if(id_first > id_second ){
			//document.getElementById(id_first).style.visibility = 'hidden';
			id_first_flag = 0;
		}else{
			//document.getElementById(id_second).style.visibility = 'visible';
			id_second_flag = 1;
		}
	}
//---------------------

	capital_flag = 0;
	var i_flag = 0;

	var first_text = document.getElementById(id_first ).innerHTML;

	if(first_text.charCodeAt(0)== 38){
		i_flag = 1;
		capital_flag = 1;
	} 
 
	if(first_text.charCodeAt(0)>64 && first_text.charCodeAt(0)< 91){
		capital_flag = 1;

		var first_text = new String();
		var first_text = document.getElementById(id_first ).innerHTML;
		if(i_flag == 0){
			document.getElementById(id_first ).innerHTML = first_text.toLowerCase();
		}
	}

	first_x = document.getElementById(id_first ).offsetLeft;
	first_y = document.getElementById(id_first ).offsetTop;
	second_x = document.getElementById(id_second ).offsetLeft;
	second_y = document.getElementById(id_second ).offsetTop;

	if(document.getElementById(id_middle) ) {
		middle_x = document.getElementById(id_middle ).offsetLeft;
	}

	first_w = document.getElementById(id_first).offsetWidth;
	second_w = document.getElementById(id_second).offsetWidth;

	need_adjust = 0;
	setTimeout("first_step(id_first,id_second)", 200);
setTimeout("do_movement(id_first,id_second,first_x,second_x,first_w,second_w,id_middle)",400);
}

function first_step(){
	ancor_h = document.getElementById(id_first).offsetHeight;
	first_y_start = first_y - ancor_h + "px";
	second_y_start = second_y + ancor_h + "px";

	if(move2_hide_flag == 0){

		document.getElementById(id_first ).style.top = first_y_start;
		document.getElementById(id_second ).style.top = second_y_start;
	}
}

function do_movement(){

	id_first = do_movement.arguments[0];
	id_second = do_movement.arguments[1];
	var first_w = do_movement.arguments[4];
	var second_w = do_movement.arguments[5];
	id_middle = do_movement.arguments[6];
	var delta = second_x - first_x;
	var shift_first = second_w + delta - first_w;
	var shift_second = delta;
	var middle_hidden = 0;

	var shift_max = ((shift_first > shift_second) ? shift_first : shift_second);
	var shift_middle = Math.abs(first_w - second_w);
	
	step = 1;
	m_timeout = 25;
	m_timeout_delta = 5;//10;

	if(document.getElementById(id_first).className == "move2_hide" ){
		shift_middle = first_w;
		middle_hidden = 1;
	}

	if(document.getElementById(id_second).className == "move2_hide" ){
		shift_middle = second_w;
		middle_hidden = 1;
	}

	for(i=0; i<shift_max; i++){
		if(shift_first > i){
			setTimeout("do_step_first(id_first)", m_timeout );
			need_adjust = 1;
		}
		m_timeout = m_timeout + m_timeout_delta;

		if(shift_second > i){
			setTimeout("do_step_second(id_second)", m_timeout );

		}
		m_timeout = m_timeout + m_timeout_delta;
		
		if(document.getElementById(id_middle) && middle_hidden == 0) {
	if ((shift_middle > shift_max-i) &&  ( middle_x < first_x || middle_x < second_x)) {

				setTimeout("do_step_middle(id_middle)", m_timeout );
			}
			m_timeout = m_timeout + m_timeout_delta;
		}
	}

	if(middle_hidden == 1){
		for(i=0; i<shift_middle; i++){
			setTimeout("do_step_middle(id_middle)", m_timeout );
			m_timeout = m_timeout + m_timeout_delta*3;
		}
	}

	m_timeout = m_timeout + 25;
	setTimeout("last_step(id_first,id_second)", m_timeout );
}

function do_step_first() {

	first_x = document.getElementById(id_first ).offsetLeft;
	first_x = first_x + 1 + "px";
	document.getElementById(id_first ).style.left = first_x;
}
function do_step_second() {

	second_x = document.getElementById(id_second ).offsetLeft;
	second_x = second_x - 1 + "px";
	document.getElementById(id_second ).style.left = second_x;
}

function do_step_middle() {

	middle_x = document.getElementById(id_middle).offsetLeft;

	if(document.getElementById(id_second).className == "move2_hide") {
 		middle_x = middle_x + 1 + "px";
		document.getElementById(id_middle).style.left = middle_x;
		return;
	}
	if(document.getElementById(id_first).className == "move2_hide") {
		middle_x = middle_x - 1 + "px";
		document.getElementById(id_middle).style.left = middle_x;
		return;
	}
 
	if(first_w > second_w){
		middle_x = middle_x - 1 + "px";
		document.getElementById(id_middle).style.left = middle_x;
		return;
	}
	if(first_w < second_w){
		middle_x = middle_x + 1 + "px";
		document.getElementById(id_middle).style.left = middle_x;
		return;
	}


}

function last_step(){

	first_y_end = first_y + "px";
	second_y_end = second_y + "px";

	document.getElementById(id_first ).style.top = first_y_end;
	document.getElementById(id_second ).style.top = second_y_end;

	if(document.getElementById(id_third) && document.getElementById(id_fourth)){
		setTimeout("togglePlaceHolder(id_third,id_fourth)", 300);
	}

	//---- show / hide the second swap element ------------//

	if(move2_hide_flag == 1){
		if( id_first_flag == 0){
			setTimeout("document.getElementById(id_first).style.visibility = 'hidden';",500);
		}
		if(id_second_flag == 1){
			document.getElementById(id_second).style.visibility = 'visible';
		}
	}

	//---------- small / capital letters substitution ----------// 

	if(need_adjust == 0) return;
	if( capital_flag == 0 ) return;

	var second_text = new String();
      var second_text = document.getElementById(id_second ).innerHTML;
	var first_letter = second_text.slice(0,1);
	var first_letter1 = first_letter.toUpperCase();

	document.getElementById(id_second ).innerHTML = second_text.replace(first_letter,first_letter1 );

}


/* -------------- ONLINE TEXT FIELD ----------------------- */

function onlineTextField(arg) {
	var text_to_send = ""; 
	var text_for_subject = "";
	var text_name = "";
	var text_email_addr = "";
	var len = document.forms[0].length;
	var i;
	var total_questions = 0;
	var right_answers = 0;
	var encoded_answer = "";
	var correct_answer = new String();
	var actual_answer = new String();
	var buffer_array = ""; 
	var buf;

	/* ----- Check name and create report header ------- */

	for(i=0; i< len; i++) {

		if(document.forms[0].elements[i].name == "student_name"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type your first name");
				document.forms[0].elements[i].focus();
				return;
			}
			text_to_send += "Student:  "
			text_to_send += document.forms[0].elements[i].value;
			text_name  += document.forms[0].elements[i].value;
			
		}

		if(document.forms[0].elements[i].name == "course_info"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type course info");
				document.forms[0].elements[i].focus();
				return;
			}

			text_to_send += "\nCourse: ";
			text_to_send += document.forms[0].elements[i].value;
			//text_name  += "@";
			text_name += ("@" + document.forms[0].elements[i].value);
		
		}

		if(document.forms[0].elements[i].name == "reply_email"){
			if( validateEmail(i) ) return;
			text_to_send += "\nE-mail:  "
			text_to_send += document.forms[0].elements[i].value;
		}

		if(document.forms[0].elements[i].name == "comments"){
			if(document.forms[0].elements[i].value.length > 0){
				text_to_send += "\n\nComments:  ";	
				text_to_send += document.forms[0].elements[i].value;
			}
		}

		if(document.forms[0].elements[i].name == "test_name"){
			text_to_send += "\n\nTest name:  ";
			text_to_send += document.forms[0].elements[i].value;
			text_to_send += "\n-------------------------------------------------\n";
			text_for_subject += document.forms[0].elements[i].value;
		}
	}
		/*---------- create report body -------------*/

	for(i=0; i< len; i++) { 

		if(document.forms[0].elements[i].name == "question"){
			text_to_send += "\n--- *** ---\n\n";
			text_to_send += document.forms[0].elements[i].value;
			total_questions++;
		}
		if(document.forms[0].elements[i].className == "textfield"){
			
			actual_answer = document.forms[0].elements[i].value;
			encoded_answer = document.forms[0].elements[i].name;
			//buffer_array = encoded_answer.split(",");
			buffer_array = encoded_answer.split("O");

			correct_answer = "";

	//--------- Trim leading and trailing whitespace chars -----//

			var len_actual_answer = actual_answer.length;

			var begin = 0;
			var end = len_actual_answer-1;

			while(actual_answer.charAt(begin) == "\x20" ){
				begin++;
			}
			while(actual_answer.charAt(end) == "\x20" || actual_answer.charAt(end) == "\x0a" || actual_answer.charAt(end) == "\x0d" ){
				end--;
			}
	
			actual_answer = actual_answer.slice(begin,++end);
			actual_answer = actual_answer.replace(/\s{1,}/g, " ");//remove extra spaces
			if(actual_answer.length < 1) {
				if(confirm("The text field is empty.\nPress 'OK' to live it as it is or press 'Cancel' to fill it out.")){
					actual_answer = "n/a"
				}else{
					document.forms[0].elements[i].focus();
					return;
				}
			}
	//--------------------------------------------------------//

			for(j=0;j<buffer_array.length;j++){
				buf = String.fromCharCode(buffer_array[j]);
				correct_answer = correct_answer.concat(buf);
			}

			if(correct_answer != actual_answer ){

				text_to_send += "\n--- ERROR --- \n ";

				text_to_send += "Actual answer:   ";
				text_to_send += actual_answer;
				text_to_send += "\nCorrect answer: ";
				text_to_send += correct_answer;

			}else{

				text_to_send += "\nCorrect answer: ";
				text_to_send += correct_answer;
				right_answers++;

			}
			text_to_send += "\n";
		}
	}

	var final_score = (right_answers / total_questions) * 100;
	if(final_score < 0) final_score = 0;
	final_score = Math.round(final_score);


		/*------- fill out hidden fields ------*/

	for(i=0; i< len; i++) { 

		if(document.forms[0].elements[i].name == "report" ){

			text_to_send += ("\n\nScore = " + final_score + "%"); 
			text_to_send += ("\n--------------------------------------------------\nLanguageTeach Technologies. All Rights Reserved.\n"); 
			document.forms[0].elements[i].value = text_to_send;
			//alert(document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "subject" ){

			text_for_subject += (";  Score = " + final_score + "%"); 
			document.forms[0].elements[i].value = text_for_subject;

			//alert("Subject: " + document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "student_info" ){
			document.forms[0].elements[i].value = text_name;
			//alert("Student info: " + document.forms[0].elements[i].value );

		}

		if(document.forms[0].elements[i].name == "page_location" ){
			document.forms[0].elements[i].value = window.location;	
			//alert("location: " + document.forms[0].elements[i].value );
		}

	}
		/* -------- Submit --------*/

	document.forms[0].submit();

	//document.forms[0].reset.click();

}


/* -------------- ONLINE MENU ----------------------- */

function onlineMenu(arg) {

	var text_to_send = ""; 
	var text_for_subject = "";
	var text_name = "";
	var text_email_addr = "";
	var len = document.forms[0].length;
	var i;
	var total_questions = 0;
	var right_answers = 0;
	var correct_answer = new String();
	var actual_answer = new String();

	var ret;
	var str;
	/* ----- Check name and create report header ------- */

	for(i=0; i< len; i++) {

		if(document.forms[0].elements[i].name == "student_name"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type your first name");
				document.forms[0].elements[i].focus();
				return;
			}
			text_to_send += "Student:  "
			text_to_send += document.forms[0].elements[i].value;
			text_name  += document.forms[0].elements[i].value;
			
		}

		if(document.forms[0].elements[i].name == "course_info"){
			if(document.forms[0].elements[i].value.length == 0){
				alert("Please type course info");
				document.forms[0].elements[i].focus();
				return;
			}

			text_to_send += "\nCourse: ";
			text_to_send += document.forms[0].elements[i].value;
			//text_name  += "@";
			text_name += ("@" + document.forms[0].elements[i].value);
		
		}

		if(document.forms[0].elements[i].name == "reply_email"){
			if( validateEmail(i) ) return;
			text_to_send += "\nE-mail:  "
			text_to_send += document.forms[0].elements[i].value;
		}

		if(document.forms[0].elements[i].name == "comments"){
			if(document.forms[0].elements[i].value.length > 0){
				text_to_send += "\n\nComments:  ";	
				text_to_send += document.forms[0].elements[i].value;
			}
		}

		if(document.forms[0].elements[i].name == "test_name"){
			text_to_send += "\n\nTest name:  ";
			text_to_send += document.forms[0].elements[i].value;
			text_to_send += "\n-------------------------------------------------\n";
			text_for_subject += document.forms[0].elements[i].value;
		}
	}
		/*---------- create report body -------------*/

	for(i=0; i< len; i++) { 

		if(document.forms[0].elements[i].name == "question"){
			text_to_send += "\n--- *** ---\n\n";
			text_to_send += document.forms[0].elements[i].value;
			total_questions++;
		}

	    if(document.forms[0].elements[i].className == "quiz_menu"){

		   	var num = document.forms[0].elements[i].selectedIndex;
		   	actual_answer = document.forms[0].elements[i].options[num].text;

		   	for(var k=0; k<document.forms[0].elements[i].options.length;k++){
				str = document.forms[0].elements[i].options[k].value;
				ret = str.search(/;/);
				if(ret != -1){
				   correct_answer = document.forms[0].elements[i].options[k].text;
				}
		   	}			

			if(correct_answer != actual_answer ){

				text_to_send += "\n--- ERROR --- \n ";

				text_to_send += "Actual answer:   ";
				text_to_send += actual_answer;
				text_to_send += "\nCorrect answer: ";
				text_to_send += correct_answer;

			}else{

				text_to_send += "\nCorrect answer: ";
				text_to_send += correct_answer;
				right_answers++;

			}

			text_to_send += "\n";
		}
	}

	var final_score = (right_answers / total_questions) * 100;
	if(final_score < 0) final_score = 0;
	final_score = Math.round(final_score);


		/*------- fill out hidden fields ------*/

	for(i=0; i< len; i++) { 

		if(document.forms[0].elements[i].name == "report" ){

			text_to_send += ("\n\nScore = " + final_score + "%"); 
			text_to_send += ("\n--------------------------------------------------\nLanguageTeach Technologies. All Rights Reserved.\n"); 
			document.forms[0].elements[i].value = text_to_send;
			//alert(document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "subject" ){

			text_for_subject += (";  Score = " + final_score + "%"); 
			document.forms[0].elements[i].value = text_for_subject;

			//alert("Subject: " + document.forms[0].elements[i].value );
		}
		if(document.forms[0].elements[i].name == "student_info" ){
			document.forms[0].elements[i].value = text_name;
			//alert("Student info: " + document.forms[0].elements[i].value );

		}

		if(document.forms[0].elements[i].name == "page_location" ){
			document.forms[0].elements[i].value = window.location;	
			//alert("location: " + document.forms[0].elements[i].value );
		}

	}
		/* -------- Submit --------*/

	document.forms[0].submit();
	//document.forms[0].reset.click();

}

/*--------------- PRINT ESSAY -----------------------------------*/

function preview_essay() {

	var report = "";

	var page = '<blockquote><div id="prn_header" align="right"><span id="print" onclick="prn(this)" style="cursor:pointer;cursor:hand;color:#0000FF;weight:bold;font-size:14px"><u>print</u></span>&nbsp;&nbsp;&nbsp;<span onclick="closeprn()" style="cursor:pointer;cursor:hand;color:#CC0000;weight:normal;font-size:14px"><u>close</u></span><br /><br /></div>';

	var ret = document.getElementsByTagName("textarea");

	for(i=0; i<ret.length; i++){
		if(ret[i].className == "essay"){
			report += ret[i].value;
			report += "<br /><br />______________________<br /><br />";
		}
	}
	
	report = report.replace(/\x0d\x0a/gi, "<br />");

	page += report;
	page += '</blockquote>';

	var features = 'location=0,directories=0,status=no,menuBar=no,scrollBars=yes,resizable=yes';
	win_report = window.open("","prn",features,false);

	with(win_report.document) {
		write('<html><body onload="window.focus()"><script src="interact.js"></script><link href="viewpage.css" type="text/css" rel="stylesheet" />'+page+'<\/body>');
		close();
	}
}

function prn() {
	document.getElementById('prn_header').style.display = "none";
	window.print();
	setTimeout("document.getElementById('prn_header').style.display = 'block';",3500 );
}

function closeprn() {
	window.close();
}


/*------------------------------------------------------------------------------*/



