
function checkLogin()
{
	if (!document.login_form.id.value)
	{
		window.alert("Please enter your ID.\nYou can choose not to enter your password, but your ID is required.");
		return false;
	}
	return true;
}

function confirmDelete()
{
	return window.confirm("Are you sure you wish to remove this contact?");
}

function checkNewImage()
{
   if (!document.getElementById('imgfile').value)
   {
	   window.alert("Please specify an image to upload.");
	   return false;
   }
   showloader();
   return true;
}

function checkNewContact()
{
   if (!document.getElementById('newcontactname').value)
   {
	   window.alert("Please enter a name for this contact.");
	   return false;
   }
   showloader();
   return true;
}

function checkNewDoctor()
{
   if (!document.getElementById('newdoctorname').value)
   {
	   window.alert("Please enter a name for this doctor.");
	   return false;
   }
   showloader();
   return true;
}

function expand(id)
{
   //document.getElementById('newcontact').style.display = "none";
   if (document.getElementById(id).style.display == "none")
   {
      document.getElementById(id).style.display = "block";
   }
   else
   {
      document.getElementById(id).style.display = "none";
   }
}

function expandSetting(ref, id)
{
   ref.style.backgroundImage = "url('s-grad" + (document.getElementById(id).style.display == "none" ? "2" : "") + ".jpg')";
   expand(id);
}

function replace_sec(id)
{
   document.getElementById('sec-content').innerHTML = document.getElementById(id).innerHTML;
}

function editMedication(medID, name, notes)
{
   if (document.getElementById('editmedication').style.display == "none")
   {
      document.getElementById('editmedication').style.display = "block";
	  document.editmedicationform.themedid.value = medID;
	  document.editmedicationform.medname.value = name;
	  document.editmedicationform.mednotes.value = notes;
   }
   else
   {
      document.getElementById('editmedication').style.display = "none";
   }
}

function newMedication(id)
{
   if (document.getElementById('newmedication').style.display == "none")
   {
      document.getElementById('newmedication').style.display = "block";
	  document.newmedicationform.theid.value = id;
   }
   else
   {
      document.getElementById('newmedication').style.display = "none";
   }
}

function replaceCond(mid, lid)
{
   document.getElementById("cond_main").innerHTML = document.getElementById(mid).innerHTML;
   for (var i = 0; i < document.getElementById("totalconds").innerHTML; i++)
   {
	   document.getElementById('condlink' + i).style.color = "#000";
   }
   document.getElementById(lid).style.color = "#c30;";
}

function replaceCont(mid, lid, contactID)
{
   document.getElementById("cont_main").innerHTML = document.getElementById(mid).innerHTML;
   for (var i = 0; i < document.getElementById("totalconts").innerHTML; i++)
   {
	   document.getElementById('contlink' + i).style.color = "#000";
   }
   document.getElementById(lid).style.color = "#c30;";
}

function replaceDoc(mid, lid)
{
   document.getElementById("docs_main").innerHTML = document.getElementById(mid).innerHTML;
   for (var i = 0; i < document.getElementById("totaldocs").innerHTML; i++)
   {
	   document.getElementById('doclink' + i).style.color = "#000";
   }
   document.getElementById(lid).style.color = "#c30;";
}

function showloader()
{
   document.getElementById("loader").style.display = "block";
}

function masterReplace(id)
{
   document.getElementById('products').style.display = "none";
   document.getElementById('users').style.display = "none";
   document.getElementById('agents').style.display = "none";
   document.getElementById('masters').style.display = "none";
   document.getElementById('settings').style.display = "none";
   document.getElementById(id).style.display = "block";
}

function setDob(day, month, year)
{
	document.getElementById("day").selectedIndex = day - 1;
	document.getElementById("month").selectedIndex = month - 1;
	document.getElementById("year").selectedIndex = year - 1900;
}

function showDrop(id)
{
   document.getElementById('prodsdrop').style.display = 'none';
   document.getElementById('userdrop').style.display = 'none';
   document.getElementById('masterdrop').style.display = 'none';
   document.getElementById(id).style.display = 'block';
}

function selectAll(theElement)
{
	var theForm = theElement.form;
	for (var i = 0; i < theForm.length; i++) 
	{
		if (theForm[i].type == 'checkbox' && theForm[i].name != 'allpends')
		{
			theForm[i].checked = theElement.checked;
		}
	}
	document.getElementById("checkallpends").setAttribute("title", theElement.checked ? "Deselect All" : "Select All");
}

function selectAllValids(theElement)
{
	var theForm = theElement.form;
	for (var i = 0; i < theForm.length; i++) 
	{
		if (theForm[i].type == 'checkbox' && theForm[i].name != 'allvalids')
		{
			theForm[i].checked = theElement.checked;
		}
	}
	document.getElementById("checkallvalids").setAttribute("title", theElement.checked ? "Deselect All" : "Select All");
}

function selectAllValidsAG(theElement)
{
    var theForm = theElement.form;
	for (var i = 0; i < theForm.length; i++) 
	{
		if (theForm[i].type == 'checkbox' && theForm[i].name != 'allvalidsAG')
		{
			theForm[i].checked = theElement.checked;
		}
	}
	document.getElementById("allvalidsAG").setAttribute("title", theElement.checked ? "Deselect All" : "Select All");
}

function selectAllDelsAG(theElement)
{
    var theForm = theElement.form;
	for (var i = 0; i < theForm.length; i++) 
	{
		if (theForm[i].type == 'checkbox' && theForm[i].name != 'alldelsAG')
		{
			theForm[i].checked = theElement.checked;
		}
	}
	document.getElementById("alldelsAG").setAttribute("title", theElement.checked ? "Deselect All" : "Select All");
}

function mediMail(id)
{
   document.getElementById('medi_mail').style.display = document.getElementById('medi_mail').style.display == "block" ? "none" : "block";
   if (id != -1)
   {
	  document.medi_mail_form.rcpt.value = id;
   }
}

function checkMediMailForm()
{
   var msg = "";
   if (document.medi_mail_form.medi_from.value == "") msg += "No from address supplied.\n";
   if (!checkEmail(document.medi_mail_form.medi_from.value)) msg += "Invalid from address supplied.\n";
   if (document.medi_mail_form.medi_subject.value == "") msg += "No subject supplied.\n";
   if (document.medi_mail_form.medi_message.value == "") msg += "No message supplied.\n";
   
   if (msg != "")
   {
	  window.alert("The following errors occurred:\n" + msg);
	  return false;
   }
   return true;
}

function showDownloadMessage()
{
   window.alert("This function will allow you to download your details to your hard drive.\nIf you have purchased a USB drive, then select the appropriate drive to save the file to.");
}

function checkEmail(email)
{
   var foundAt = false;
   var foundDot = false;
   var atPos = 0;
   var dotPos = 0;

   for (var i = 0; i < email.length; i++)
   {
      if (email.charAt(i) == "@")
      {
         foundAt = true;
         atPos = i;
      }
      else
      {
         if (email.charAt(i) == ".")
         {
            foundDot = true;
            dotPos = i;
         }
      }
   }

   if (foundAt && foundDot && (atPos < dotPos) && (email.length > 6))
   {
      return true;
   }
   return false;
}

function runSearch(elem)
{
   var prep = elem.name == "pend_search" ? "p" : "v";
   var matchStr = document.getElementById(prep + '_search_text').value;
   if (matchStr == "")
   {
	  alert("Please enter some search terms.");
	  return;
   }
   
   var theTable = document.getElementById(prep + "CTable");
   for (var i = 0; i < theTable.rows.length; i++) 
   {
	  for (var j = 0; j < theTable.rows[i].cells.length; j++)
	  {
	     if (theTable.rows[i].cells[j].style.background)
		 {
			if (theTable.rows[i].cells[j].innerHTML.indexOf(matchStr) > -1)
			{
			   theTable.rows[i].cells[j].style.background = "#ff9";
			}
			else
		    {
			   theTable.rows[i].cells[j].style.background = "#fff";
		    }
		 }
	  }
   }
}

function editEmail(id)
{
   document.getElementById("email_form").innerHTML = "<form action=\"masterloggedin.php\" method=\"post\"" + document.getElementById(id).innerHTML + "</form>";
}

function checkSQ()
{
   if (document.getElementById("sq_check").innerHTML == "false")
   {
	  alert("As this is the first time you have used the system, you need to" + 
			"\nset your secret question for security reasons. This is the question" + 
			"\nthat we will ask you if you ever forget your login password.");
	  expand('security');
	  replace_sec('question');
	  return false;
   }
   showLoader();
   return true;
}

function editUser(id)
{
	window.open ("edituser.php?id=" + id, "", "width=500,height=300,resizable=0");
}

function checkQueryString()
{
   var loc = window.location.href;
   if (loc.indexOf('?') != -1)
   {
	  masterReplace(loc.substring(loc.indexOf('?') + 1, loc.length));
   }
}