/**************************************************************************/
 /* PERIOD = 3 Ln 260 */
 /**************************************************************************/
function colorMe(obj, doWhat){

	if (doWhat == 'in') {
		obj.style.color = '#8d7362';
	} else {
		obj.style.color = '#cb7916';
	}
}

function colorMeAdmin(obj, doWhat){

	if (doWhat == 'in') {
		obj.style.color = '#8d7362';
	} else {
		obj.style.color = '#140F51';
	}
}

function ShowEditButtons(){

	if (document.getElementById('edit_buttons').style.visibility == "hidden"){
		document.getElementById('edit_buttons').style.visibility = "visible";
	}
	else {
		document.getElementById('edit_buttons').style.visibility = "hidden";
	}

}

function drillDown(action, staffid){

	var page_request = false;
	var date = new Date();
	var timestamp = date.getTime();


	if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){
			}
		}
	}else return false

	document.getElementById('toc_title').innerHTML = action;
	page_request.onreadystatechange=function(){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			var return_array=page_request.responseText.split(",");
			document.getElementById('main_content').innerHTML = return_array;
			//document.getElementById('toc_title').innerHTML = action;
			//document.title = tocName;
		}
	}
	page_request.open('GET', 'ajax/drilldown.php?action=' + action + '&activityid=' + num + '&staffid=' + staffid + '&time=' + timestamp, true);
	page_request.send(null);
}

/**
 *
 * @access public
 * @return void
 **/
function StaffDrill(staffid, staffname, periodID){

	var page_request = false;
	var date = new Date();
	var timestamp = date.getTime();
	//alert(periodID);

	document.getElementById('toc_title').innerHTML = "Please Wait";
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){
			}
		}
	}else return false


	page_request.onreadystatechange=function(){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			var return_array=page_request.responseText.split(",");
			document.getElementById('main_content').innerHTML = return_array;
			document.getElementById('toc_title').innerHTML = "Report for " + staffname;
		}
	}
	page_request.open('GET', 'ajax/staff_drilldown.php?staffid=' + staffid + '&periodID=' + periodID + '&time=' + timestamp, true);
	page_request.send(null);

}

//################################################
// menu buttons
function SwapMenuImage(obj, doWhat, img){

   obj.style.color = img;

}

function GoTo(pageNo){
	location.href = "index.php?page=" + pageNo;
}

// ################################################
// Admin functions

/**
 *
 * @access public
 * @return void
 **/
function GoToAdmin(pageNo){
	location.href = "admin_index.php?page=" + pageNo;
}

// ##################
// submitForm(sAction)

var num = 0; //var to host the user choice
var doc = 0;
var doc_name = "";
var msg = "Please make a selection";

function SetLP(toc_num){
	num = toc_num;
}

function SetDocToc(doc_num,toc_num, doc_title){
	doc = doc_num;
	num = toc_num;
	doc_name = doc_title;
}

function submitForm(sAction, staffid){

	switch(sAction){
		case "D":
			//check that a provider has been checked
			if (num == 0){
				alert(msg);
				return;
			}
			var answer = confirm('This will delete this activity!\nDo you want to continue?')
			if (answer==1) {
				saveActivity('D', staffid);
			} else {
				return;
			}

			break;

		case "A":
			//if (num == 0){
			//	alert(msg);
			//	return;
			//}
			drillDown('Add', staffid);
			//location.href = "admin_index.php?page=12&toc_num=" + num;
			break;




		case 'E':
			//check that a provider has been checked
			if (num == 0) {
				alert(msg);
				return;
			}
			drillDown('Edit', staffid);

			//document.getElementById('radiobar').style.display = inline;
			//location.href = "admin_index.php?page=11&toc_num=" + num;
			break;

		case "P": //change password
			//if (num == 0){
			//	alert(msg);
			//	return;
			//}
			drillDown('Password' , staffid);
			//location.href = "admin_index.php?page=12&toc_num=" + num;
			break;

		case "L": //logout
			//if (num == 0){
			//	alert(msg);
			//	return;
			//}
			//drillDown('Logoff');
			location.href = "destroy.php";
			break;
	}

}

// #####################################
 /**
  *
  * @access public
  * @return void
  **/
 function cancelEdit(){

 	location.href = "index.php?page=2";
 }

 /**
  *
  * @access public
  * @return void
  **/
 function BackToReport(){

 	location.href = "index.php?page=4";
 }

 /**
  *
  * @access public
  * @return void
  **/
 function SetPriority(val){
 	if (val == 3) {
 		document.getElementById('prioritynum').value= 1;
 	}
 }


 /**
  *
  * @access public
  * @return void
  **/
 function saveActivity(action, staffid){
 	/**************************************************************************/
 	/* PERIOD */
 	/**************************************************************************/
 	var periodID = 4; 
	//alert('##' + staffid);
	var page_request = false;
	var date = new Date();
	var timestamp = date.getTime();

	if (action != "D" && action != "DD") {
		var newText = document.getElementById('admin_activity').value;
	 	if (newText == "") {
	 		alert('Please enter some text - activity cannot be blank');
	 		return;
	 	}
	 	var priorityID = document.getElementById('priority').value;
	 	var activityNum = document.getElementById('prioritynum').value;

	 	if (priorityID == 3 && activityNum > 1) {
	 		alert('Only one opportunity may be recorded for this piority');
	 		document.getElementById('prioritynum').value= 1;
	 		return;
	 	}
	}
	//alert(activityNum + '  ' + priorityID);
	//return;
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}else return false

	document.getElementById('toc_title').innerHTML = "Please Wait...";
	page_request.onreadystatechange=function(){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){

			if (page_request.responseText != '') {
				var return_array=page_request.responseText.split(",");
				document.getElementById('toc_title').innerHTML = return_array;
			} else {
				drillDown(action, staffid);
				document.getElementById('toc_title').innerHTML = "Opportunites";

			}

		}
	}

	//here!

	strAction = 'ajax/admin.php?doWhat=' + action + '&new_text=' + newText + '&priority=' + priorityID + '&periodID=' + periodID + '&activityNum=' + activityNum + '&activityid=' + num + '&staffid=' + staffid + '&time=' + timestamp;
	//alert(strAction);
	page_request.open('GET', strAction , true);
	page_request.send(null);
}

/////////////////////////////////////////////////////////////////////
// Password

function ChangePass(staffid){

	var oldpass=document.getElementById('oldpass');
	var firstnewpass=document.getElementById('firstnewpass');
	var secondnewpass=document.getElementById('secondnewpass');

	if (oldpass.value!="") {
		if (firstnewpass.value==secondnewpass.value && firstnewpass.value!="") {
			//alert('done - ' + firstnewpass.value + ',' + lp_id);
			//alert(staffid);
			UpdatePass(oldpass.value, firstnewpass.value, staffid);
		}
		else {
			alert("Sorry, new passwords don't match");
			firstnewpass.value="";
			secondnewpass.value="";
			firstnewpass.focus();
			return;
		}
	} else {
		alert("Please enter your existing password");
		oldpass.focus();
	}
}

/////////////////////////////
function ShowPass(){
	var pass=document.getElementById('password');
	var changePass=document.getElementById('changePass');
	pass.style.display="inline";
	var oldpass=document.getElementById('oldpass');
	oldpass.focus();
	changePass.style.display="none";
}

function UpdatePass(oldpass, newpass, staffid){
	var page_request = false
	var date = new Date();
	var timestamp = date.getTime();
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}else return false

	page_request.onreadystatechange=function(){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			var return_array=page_request.responseText.split(",");
			var passTitle=document.getElementById('passwordTitle');
			var pass=document.getElementById('password');
			if (return_array[0]=='X') {
				passTitle.innerHTML="<p><strong>" + return_array[1] + "</strong></p>";
				var oldpass=document.getElementById('oldpass');
				oldpass.value="";
				oldpass.focus();
			} else {
				passTitle.innerHTML="<p><strong>" + return_array[1] + "</strong></p>";
				//pass.style.display="none";
			}
		}
	}
	strURL = 'ajax/password.php?newpass='+newpass+'&oldpass='+oldpass+'&staffid='+staffid + '&time=' + timestamp;
	//alert(strURL);
	page_request.open('GET', strURL , true);
	page_request.send(null);

}











