/* Scroll to top right (to take care of 800x600 resolutions) */
function Scroll()
{
	scrollTo(1024, 0);
}

/* Change to an IE-specific stylesheet if IE is detected */
function SelectCSS()
{
	// Pre-determine to change the FIRST instance '[0]' of the tag 'link' in the page head
	var style = document.getElementsByTagName("link")[0];
	
	if (navigator.userAgent.indexOf("MSIE") != -1)  
	{
		style.setAttribute("href", "styles/alliance_globalstyles_ie.css");
	}
	else 
	{
		// Do nothing because the default CSS file is for Firefox
	}
}

/* Wait for 5 seconds before calling the fade script */
function Delay()
{
	setTimeout("FadeCaller()", 5000);
}

/* Grab answers/data to the question which called this function */
function GrabData(which)
{
	var data = document.getElementById(which).innerHTML;
	DisplayDivContent(data);
}

/* Display the answer in the infoframe div */
function DisplayDivContent(data)
{
	parent.document.getElementById('infoframe').innerHTML = data;
}

/* Call the image cross-fade script */
function FadeCaller()
{
	crossfade(document.getElementById('Leaves'), 'images/BodyIdentGraphic_Leaves.jpg', '3', '');
}

/* Master field validation function */
function ValidateRequiredFields()
{
	if (IsNotEmpty(document.ALLIANCEReferralForm.ReferrerName, "Your Name")) {
		if (IsNotEmpty(document.ALLIANCEReferralForm.ReferrerOrganization, "Your Organization")) {
			if (IsNotEmpty(document.ALLIANCEReferralForm.ReferrerContactNumber, "Your Contact Number")) {
				if (IsNotEmpty(document.ALLIANCEReferralForm.FirstName, "First Name")) {
					if (IsNotEmpty(document.ALLIANCEReferralForm.LastName, "Last Name")) {
						if (IsNotEmpty(document.ALLIANCEReferralForm.DayPhone, "Day Phone")) {
							if (IsNotEmpty(document.ALLIANCEReferralForm.DOBMonth, "DOB Month")) {
								if (IsNotEmpty(document.ALLIANCEReferralForm.DOBDay, "DOB Day")) {
									if (IsNotEmpty(document.ALLIANCEReferralForm.DOBYear, "DOB Year")) {
										
										// All mandatory fields filled in, so check radio buttons
										if (!RadioButtonsSelected()) {
											
											// Everything passes, so implicit submit to emailprocessor.php
											return true;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}

/* Check that no mandatory fields are left blank */
function IsNotEmpty(element, fieldname)
{
	var str = element.value;
    if (str.length == 0)
	{
		alert("Please make sure to fill in all mandatory fields marked with an asterisk (*):\n\n" + fieldname);
		return false;
    } 
	else 
	{
		return true;
	}
}

/* Make sure that no radio buttons are left unchecked */
function RadioButtonsSelected() 
{
	PayorSource_Selected = false;
	HomeCarePrevious_Selected = false;
	RecentlyInFacility_Selected = false;
	ProblemUnassisted_Selected = false;
	UsesAssistiveDevice_Selected = false;

	for (i=0; i<document.ALLIANCEReferralForm.PayorSource.length; i++) {
		if (document.ALLIANCEReferralForm.PayorSource[i].checked == true) {
			PayorSource_Selected = true; break;
		}
	}

	for (i=0; i<document.ALLIANCEReferralForm.HomeCarePrevious.length; i++) {
		if (document.ALLIANCEReferralForm.HomeCarePrevious[i].checked == true) {
			HomeCarePrevious_Selected = true; break;
		}
	}

	for (i=0; i<document.ALLIANCEReferralForm.RecentlyInFacility.length; i++) {
		if (document.ALLIANCEReferralForm.RecentlyInFacility[i].checked == true) {
			RecentlyInFacility_Selected = true; break;
		}
	}

	for (i=0; i<document.ALLIANCEReferralForm.ProblemUnassisted.length; i++) {
		if (document.ALLIANCEReferralForm.ProblemUnassisted[i].checked == true) {
			ProblemUnassisted_Selected = true; break;
		}
	}

	for (i=0; i<document.ALLIANCEReferralForm.UsesAssistiveDevice.length; i++) {
		if (document.ALLIANCEReferralForm.UsesAssistiveDevice[i].checked == true) {
			UsesAssistiveDevice_Selected = true; break;
		}
	}

if ((PayorSource_Selected == false) || 
	(HomeCarePrevious_Selected == false) || 
	(RecentlyInFacility_Selected == false) ||
	(ProblemUnassisted_Selected == false) || 
	(UsesAssistiveDevice_Selected == false) ) {
	
		alert("Please make sure you have provided an answer for all of the following questions:\n\nPayor Source\nHas client ever received home health care services?\nWas client recently hospitalized or in a nursing/rehabilitation facility?\nDoes client have a difficult time leaving the home when not assisted?\nDoes client use an assistive device such as a cane, walker or wheelchair?");
		return true;
	}
	else 
	{
		return false;
	}
}

/* Macromedia image-swap scripts */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
