// JavaScript Document

var username = "";
var password = "";
function makePOSTRequest(url, parameters,replyCallBack) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }

	http_request.onreadystatechange = replyCallBack;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function getRequestString()
{
	var t = document.getElementById("sendTo");
	var poststr = "username=" + encodeURI( username ) + "&password=" + encodeURI( password ) + "&sendTo=" + encodeURI( t.options[t.selectedIndex].value ) +"&create=" + encodeURI( document.getElementById("c1").value ) +"&exceed=" + encodeURI( document.getElementById("c4").value ) +"&listen=" + encodeURI( document.getElementById("c5").value ) +"&comment=" + escape( document.getElementById("comment").value ) +"&date=" + escape( document.getElementById("date").value );
	return poststr;
}

function emailPassword() {
	alert('Individual passwords limit access to authorized users. Passwords are emailed direct to participants.');
	return;
	
	var f = document.getElementById("username");
	username = f.options[f.selectedIndex].value;

	if (username == "" )
	{
			document.getElementById("usernameField").setAttribute( "bgColor","#ff0000" );
	}
	else
	{
		document.getElementById("usernameField").setAttribute( "bgColor","#ffffff" );

  		var poststr = "username=" + encodeURI( f.options[f.selectedIndex].value );
  		makePOSTRequest('emailPassword.php', poststr, emailSendReply); 
	}
}

function loginReply() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		//alert('"' + http_request.responseText + '"');
		//alert( 'received login reply, login ok');
		result = http_request.responseText;
		//previewWindow = window.open('', 'Preview', 'resizable=yes,scrollbars=yes,toolbar,width=1024,height=768');
		//previewWindow.document.write( result );	//document.getElementById( "sendTo" ).value );
		if (result == " wrong_password" )
		{
			//document.getElementById("passwordField").innerHTML = '<strong>Error: </strong>Wrong password entered.';	
			document.getElementById("passwordField").setAttribute( "bgColor", "#ff0000" );
			document.getElementById("passwordError").innerHTML = "<strong>Error: </strong>Wrong password entered.";
		}
		else if (result == " no_checks" )
		{
			document.getElementById("passwordField").setAttribute( "bgColor", "#ff0000" );
			document.getElementById("passwordError").innerHTML = "<strong>Error:</strong> Im sorry but you have no more online<strong><em> Shout!</em></strong> recognition cheques available to send this quarter";

		}
		else
		{
			document.getElementById("content").innerHTML = result;	//"<p>Hello result</p>";		
		}
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function login() {
	//alert ('logging in...' );
	var f = document.getElementById("username");
	username = f.options[f.selectedIndex].value;
	password = document.getElementById("password").value ;

	if (username == "" )
	{
			u = document.getElementById("usernameField");
			u.setAttribute( "bgColor","#ff0000" );
	}
	else
	{
		u = document.getElementById("usernameField");
		u.setAttribute( "bgColor","#ffffff" );
		
	
  		var poststr = "username=" + encodeURI( username ) + "&password=" + encodeURI( password );
  		 makePOSTRequest('demo/pages/recognitionForm.php', poststr, loginReply);

	}
 
}

/************************************************************************************************
 * Check box checking
 ************************************************************************************************/
var checkedList = new Array();

function checkBoxClicked() 
{
	
	var box = arguments[0];
	
	var found = -1;
	
	for (var i=0; i< checkedList.length; ++i) 
	{
		if (checkedList[i] == box.id)
		{
			found = i;
			break;
		}
	}

	if ( found == -1 )
	{
		checkedList.push( box.id );
		box.value = 1;
		if ( checkedList.length > 2 )
		{
			checkedList.shift();
		}
	}
	else
	{
		checkedList.splice(found,1);
		box.value = 0;
	}	

	var c;

	for (z=1; z< 7; z++) 
	{
		c = document.getElementById( "c"+z );
		if ( !c ) continue;
		c.checked = false;
		c.value = 0;
	}
	
	for (i=0; i< checkedList.length; i++) 
	{
		c = document.getElementById(checkedList[i]);
		if ( !c ) continue;
		c.checked = true;
		c.value = 1;
	}

}

function imposeMaxLength(Object, MaxLen, remainingDest)
{
	if(Object.value.length > MaxLen)
  	{     
    	Object.value = Object.value.substring(0, MaxLen);
  	}
	
	document.getElementById(remainingDest).innerHTML = MaxLen - Object.value.length;
}

function printShout() {
	//var f = document.getElementById("username");
	var t = document.getElementById("sendTo");

  	//var getstr = "username=" + encodeURI( username ) +"&sendTo=" + encodeURI( t.options[t.selectedIndex].value ) +"&Inclusive=" + encodeURI( document.getElementById("c1").value ) +"&Trustworthy=" + encodeURI( document.getElementById("c2").value ) +"&Fresh_Thinking=" + encodeURI( document.getElementById("c3").value ) +"&Expert=" + encodeURI( document.getElementById("c4").value ) +"&Forward_Looking=" + encodeURI( document.getElementById("c5").value ) +"&Engaging=" + encodeURI( document.getElementById("c6").value ) +"&comment=" + encodeURI( document.getElementById("comment").value ) + "&print=1" + encodeURI("1");
	var getstr = getRequestString(); //"username=" + encodeURI( username ) +"&sendTo=" + encodeURI( t.options[t.selectedIndex].value ) +"&create=" + encodeURI( document.getElementById("c1").value ) +"&exceed=" + encodeURI( document.getElementById("c4").value ) +"&listen=" + encodeURI( document.getElementById("c5").value ) +"&comment=" + encodeURI( document.getElementById("comment").value)  +"&print=" + encodeURI("0");
  			
  	var  url = "previewRecognitionForm.php?" + getstr;
 	previewWindow = window.open(url, 'Print', 'resizable=yes,scrollbars=yes,toolbar,width=1024,height=768');
}


/************************************************************************************************
 * Preview Report
 ************************************************************************************************/
function preview() {
	//var f = document.getElementById("username");
	var t = document.getElementById("sendTo");
	if ( t.options[t.selectedIndex].value == "" )
	{
		s = document.getElementById("sendToField");
		s.setAttribute( "bgColor", "#ff0000" );
		document.getElementById("previewTicket").setAttribute( "bgColor", "#ff0000" ); 
	}
	else
	{
		s = document.getElementById("sendToField");
		s.setAttribute( "bgColor", "#ffffff" ); 
		document.getElementById("previewTicket").setAttribute( "bgColor", "#ffffff" ); 

		//var getstr = "username=" + encodeURI( username ) +"&sendTo=" + encodeURI( t.options[t.selectedIndex].value ) +"&Inclusive=" + encodeURI( document.getElementById("c1").value ) +"&Trustworthy=" + encodeURI( document.getElementById("c2").value ) +"&Fresh_Thinking=" + encodeURI( document.getElementById("c3").value ) +"&Expert=" + encodeURI( document.getElementById("c4").value ) +"&Forward_Looking=" + encodeURI( document.getElementById("c5").value ) +"&Engaging=" + encodeURI( document.getElementById("c6").value ) +"&comment=" + encodeURI( document.getElementById("comment").value)  +"&print=" + encodeURI("0");
		var getstr = getRequestString(); //"username=" + encodeURI( username ) +"&sendTo=" + encodeURI( t.options[t.selectedIndex].value ) +"&create=" + encodeURI( document.getElementById("c1").value ) +"&exceed=" + encodeURI( document.getElementById("c4").value ) +"&listen=" + encodeURI( document.getElementById("c5").value ) +"&comment=" + encodeURI( document.getElementById("comment").value)  +"&print=" + encodeURI("0");
				
		var  url = "demo/pages/previewRecognitionForm.php?" + getstr;
		
	    previewWindow = window.open(url, 'Preview', 'resizable=no,scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,width=1024,height=768');
	}
}

/************************************************************************************************
 * Send Confirm
 ************************************************************************************************/
function sendConfirm()
{
	var t = document.getElementById("sendTo");
	if ( t.options[t.selectedIndex].value == "" )
	{
		document.getElementById("sendToField").setAttribute( "bgColor", "#ff0000" ); 
		document.getElementById("sendConfirmField").setAttribute( "bgColor", "#ff0000" ); 
		document.getElementById("sendConfirm").innerHTML = '<p align="center" class="white">Please select recipients name</p>';	

	}
	else
	{
		document.getElementById("sendButton").disabled = true;	//setAttribute( "disabled", "true" ); 
		document.getElementById("sendToField").setAttribute( "bgColor", "#ffffff" ); 
		document.getElementById("sendConfirmField").setAttribute( "bgColor", "#0099FF" ); 
		/* document.getElementById("sendConfirm").innerHTML = sendConfirmHTML;	*/
	}
}
/************************************************************************************************
 * View Recognitions
 ************************************************************************************************/
 function goBack()
 {
	 var poststr = "username=" + encodeURI( username ) + "&password=" + encodeURI( password );
  		 makePOSTRequest('demo/pages/recognitionForm.php' , poststr, loginReply);
 }
 function viewReceived()
  {
	  		var poststr = "username=" + encodeURI( username ) + "&password=" + encodeURI( password );
  		 makePOSTRequest('demo/pages/totalReceived.php' , poststr, loginReply);
 }
 function viewSent()
  {
	  var poststr = "username=" + encodeURI( username ) + "&password=" + encodeURI( password );
  		 makePOSTRequest('demo/pages/totalSent.php', poststr, loginReply);
 }