/*	comptonLogin.js		2007-01-16

	2007-09-25 - took care of the bad login problem - hourglass spun forever
	2007-12-22 - made name lookup case-insensitive
	2008-03-11 - changed the client drop-down to automatically populate the client
		     name field, and added the onFormLoad() function that is used 
		     when the teacher name and password were 'stored' by the browser
	2008-04-14 - name values are passed directly in, so no need to determine anything
*/

function onLoadForm()	{		// added function 2008-03-11
	if	(	document.forms['myForm'].Param_teacher_name.value	> ''
		&&	document.forms['myForm'].Param_teacher_password.value	> ''
		)
	{
		// username and password were already filled in!
		getClients();
	}

}

function VerifyLogin()	{

	if  (	document.forms['myForm'].Param_teacher_name.value == '' )
	{
		alert ('You must enter your teacher name to continue!');
		document.forms['myForm'].Param_teacher_name.focus();
		return false;
	};

	if  (	document.forms['myForm'].Param_teacher_password.value == '' )
	{
		alert ('You must enter a password to continue!');
		document.forms['myForm'].Param_teacher_password.focus();
		return false;
	//} else {
		//alert ( 'the password name is ' + document.forms['myForm'].Param_teacher_password.value  );
	};

	if  ( 		document.forms['myForm'].Param_client_name.value == '' 
		&&	document.forms['myForm'].Param_client_select.value == '' 
	    )
	{
		alert ('You must enter or select a client to continue!');
		document.forms['myForm'].Param_client_name.focus();
		return false;
	};


	if  (	document.forms['myForm'].Param_session_select.value == '' )
	{
		alert ('You must select a session type to continue!');
		document.forms['myForm'].Param_session_select.focus();
		return false;
	// } else {
		// alert ( 'the session name is ' + document.forms['myForm'].Param_session_select.value  );
	};

	return true;
};


function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try	{
  req = new ActiveXObject("Msxm12.XMLHTTP");
  } catch(err2) {
    try {
	req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(err3) {
		req = false;
	}
  }
}
return req;
}

var http = getXMLHTTPRequest();

function getClients() {
    var username = document.forms['myForm'].Param_teacher_name.value;
    var password = document.forms['myForm'].Param_teacher_password.value;

//alert ( 'username-->' + username + '<--' ) ;
//alert ( 'password-->' + password + '<--' ) ;

    var myurl = '/php/getAllClients.php'
		+	'?'
		+	'Param_teacher_name'
		+	'='
		+	username
		+	'&'
		+	'Param_teacher_password'
		+	'='
		+	password
		;
    // var myRand = parseInt(Math.random()*999999999999);
    var myRand = new Date().getTime();	// use time instead of a random number
    //var modurl = myurl + "?rand=" + myRand;
    var modurl = myurl + "&rand=" + myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHttpResponse;
    http.send(null);
} 

function useHttpResponse() {
    if  ( http.readyState == 4 ) {
	if ( http.status == 200 ) {

	   if  ( typeof(document.forms['myForm'].Param_email_select ) != "undefined" )
	   {

	      var  teachersClient = http.responseXML.getElementsByTagName('teachersClient');		// 2007-09-25
			// this will only fill in IF the login was successful				// 2007-09-25
	      if  ( teachersClient[0].childNodes.length > 0 )						// 2007-09-25
	      {												// 2007-09-25

		   var unusedEmailNames = http.responseXML.getElementsByTagName("unusedEmailNames");
		   if  ( unusedEmailNames[0].childNodes.length > 0 )
		   {			// are there any elements in this tag?
			   document.forms['myForm'].Param_email_select.options[0] = new Option(' ', ' ');
			   var unusedEmailNode = http.responseXML.getElementsByTagName('unusedEmail');
			   for ( var x=0; x<= ( unusedEmailNode.length - 1 ) ; x++ ) {
				var email = unusedEmailNode[x].childNodes[0].nodeValue ;
				document.forms['myForm'].Param_email_select.options[x + 1] = new Option(email, email);
			   };
		   };
	       } else {											// 2007-09-25
				//alert ('testing2');
			document.forms['myForm'].Param_teacher_password.value = '';
			document['myForm'].Param_teacher_name.focus();					// 2007-09-25
	       };											// 2007-09-25
	   };



	   var teachersClient = http.responseXML.getElementsByTagName("teachersClient");

//alert ( teachersClient[0].childNodes.length );

	   if  ( teachersClient[0].childNodes.length > 0 )
	   {			// are there any elements in this tag?
	   	   var clientNode = http.responseXML.getElementsByTagName('client');

		   for ( x=0; x<= ( clientNode.length - 1 ) ; x++ ) {

			var nameNode = clientNode[x].getElementsByTagName('name')[0]  ;
							//alert ( nameNode.childNodes[0].nodeValue ) ;

			var client = nameNode.childNodes[0].nodeValue.toLowerCase() ;		// 2007-12-22 - added toLowerCase

			if  ( clientNode[x].getElementsByTagName('active')[0].childNodes[0].nodeValue > '0' )	// 2008-03-11
			{
				document.forms['myForm'].Param_client_select.options[x] = new Option(client, client);
			}

			var language = '' ;
			if  (  clientNode[x].getElementsByTagName('language')[0].hasChildNodes()   ) 	{
				language = clientNode[x].getElementsByTagName('language')[0].childNodes[0].nodeValue  ;
				clientsLanguageArray[client] = language;
			};
			if  (  clientNode[x].getElementsByTagName('email')[0].hasChildNodes()   ) 	{
				email = clientNode[x].getElementsByTagName('email')[0].childNodes[0].nodeValue  ;
				clientsEmailAddyArray[client] = email;
			};

		   };
	   } else {
		while  ( document.forms['myForm'].Param_client_select.length  > 0 ) {	
			document.forms['myForm'].Param_client_select.options[0] = null;	// 2007-01-20
		};
		alert ( 'You have entered an incorrect username/password combination!  Please try again.' );
	   };
	   document.getElementById('waitImg').innerHTML = '';
	};
    } else {
	document.getElementById('waitImg').innerHTML
		= '<img src="/images/hourglass.gif">';
    };
};

var clientsLanguageArray = new Array();	// array to hold languages indexed by user name
var clientsEmailAddyArray = new Array();	// array to hold email addys indexed by user name	2007-04-06

function setSpecLanguage(thisClient)	{	// 2008-04-14 - now we pass the value in, so no need to try and determine it.

					//var thisClient = '';
					//	if  ( document.forms['myForm'].Param_client_name.value )	{
					//		/* if the name is filled in, use that, otherwise use the select/option (scrolling region) */
					//		thisClient =  document.forms['myForm'].Param_client_name.value.toLowerCase()  ;
					//	} else {
					//		thisClient =  document.forms['myForm'].Param_client_select.value.toLowerCase()  ;
					//	};

	document.forms['myForm'].Param_client_name.value	= thisClient;	// 2008-04-15 set the select value in case they typed it in
	document.forms['myForm'].Param_client_select.value	= thisClient;	// 2008-04-15 set the typed in value in case they clicked a name
	// alert ( clientsLanguageArray[thisClient] );
	var thisLanguage =  clientsLanguageArray[thisClient] ;
	document.forms['myForm'].Param_language_select[0].selected = true;	// set the first one (the blank one) to selected
	for ( var i =0; i < document.forms['myForm'].Param_language_select.length; i++ ) {
		if  ( document.forms['myForm'].Param_language_select[i].value ==  thisLanguage ) {
			document.forms['myForm'].Param_language_select[i].selected = true;
			break;
			// alert ( 'index ' + i + ' value : ' + document.forms['myForm'].Param_language_select[i].value );
		};
	};

	if  ( clientsEmailAddyArray[thisClient] > ''	)	{			// 2007-04-06
		var thisEmailAddy = clientsEmailAddyArray[thisClient]		;	// 2007-04-06
		document.forms['myForm'].Param_email_name.value = thisEmailAddy	;	// 2007-04-06
	} else {									// 2007-04-06
		document.forms['myForm'].Param_email_name.value = '';		;	// 2007-04-06
	};										// 2007-04-06

}


function blankClientName()	{							// 2008-03-11
		// when the client select is clicked (or changed somehow) that value fills in the name box
	document.forms['myForm'].Param_client_name.value 				// 2008-03-11
		= document.forms['myForm'].Param_client_select.value ;			// 2008-03-11
};
/**

	if  (	document.forms['myForm'].Param_client_name.value != '' )		// 2007-10-01
	{										// 2007-10-01
		if  ( confirm("You clicked a client name, do you want to BLANK out the client text field?") )
		{									// 2007-10-03
			document.forms['myForm'].Param_client_name.value = ''	;	// 2007-10-01
		} else {								// 2007-10-03
			alert	(	'Your text client name '			// 2007-10-03
				+	'"'						// 2007-10-03
				+	document.forms['myForm'].Param_client_name.value // 2007-10-03
				+	'"'						// 2007-10-03
				+	' will be used and your selected client name will be ignored!'// 2007-10-03
				);							// 2007-10-03
		};									// 2007-10-03
	};										// 2007-10-01
***/
		
function blankLanguageName()	{							// 2008-03-11
	document.forms['myForm'].Param_language_name.value
		=	document.forms['myForm'].Param_language_select.value;
	};										// 2007-10-01
/*****

	if  (	document.forms['myForm'].Param_language_name.value != '' )		// 2007-10-03
	{										// 2007-10-03
		if  ( confirm("You clicked a language name, do you want to BLANK out the language text field?") )
		{									// 2007-10-03
			document.forms['myForm'].Param_language_name.value = ''	;	// 2007-10-03
		} else {								// 2007-10-03
			alert	(	'Your text language name '			// 2007-10-03
				+	'"'						// 2007-10-03
				+	document.forms['myForm'].Param_language_name.value // 2007-10-03
				+	'"'						// 2007-10-03
				+	' will be used and your selected language name will be ignored!'
				);
		};									// 2007-10-03
	};										// 2007-10-03
***/



