//*---------------------------------------------------
// IMAGES ROLL OVERS
// DESCRIPTION:
// 	general image swapping script
// MM_swapImgRestore():
//	restores all images on screen to original
// MM_preloadImages():
//	preloads all roll over images
//	call with location of image
//	EX: MM_preloadImages('images/imagename.gif')
// MM_swapImage()
//	switches out an image
//	USE: MM_swapImage('imagename','','imagelocation',1)
//--------------------------------------------------*/

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_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_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];}
}



function nbrsOnly(e, allowDot) {
	//alert(kc)
	//var e = window.event;
	//kc = event.keyCode
	e 		=  e || window.event ;    
	var kc 	=  e.which || e.keyCode;
	//alert(kc);
	switch (kc) {
		//required for non-ie browsers
		case 8 ://backspace
		case 9 ://tab
		case 35 ://end
		case 36 ://home
		case 37 ://<--
		case 39 ://-->
		//
		case 46 ://.||delete
		case 48 ://0
		case 49 ://1
		case 50 ://2
		case 51 ://3
		case 52 ://4
		case 53 ://5
		case 54 ://6
		case 55 ://7
		case 56 ://8
		case 57 ://9
			if (kc == 46 && allowDot == undefined) {
				return false;
			}
			return true;
			//e.returnValue = true;
			break;
		default :
			return false;
			//e.returnValue = false;
	}
}

function toggle(object, vis, dis) {
  if (document.getElementById) {
	  document.getElementById(object).style.visibility = vis;
	  document.getElementById(object).style.display = dis;
  }
  else if (document.layers && document.layers[object] != null) {
	  document.layers[object].visibility = vis;
	  document.layers[object].display = dis;
  }
  else if (document.all) {
	  document.all[object].style.visibility = vis;
	  document.all[object].style.display = dis;
  }
  return false;
}


function isEmpID(valToChk) {
	/*
		check for properly formatted employee ID
			- check for start of string (^)
			- followed by 8 numbers
			- followed by end of string ($)
	*/
	re = /^\d{8}$/;
	return re.test(valToChk);
}

function isLast4ssn(valToChk) {
	/*
		check for properly formatted last 4 of SSN
			- check for start of string (^)
			- followed by 4 numbers
			- followed by end of string ($)
	*/
	re = /^\d{4}$/;
	return re.test(valToChk);
}

function isEmailAddress(valToChk) {
	/*
		pretty basic check for a properly formatted email address, does the following:
			- check for start of string (^)
			- followed by 1 or more characters(.+), 
			- followed by 1 @ sign (@{1}), 
			- followed by 1 or more characters (.+), 
			- followed by 1 period (\.{1}), 
			- followed by 1 or more characters (.+)
			- followed by end of string ($)
	*/
	re = /^.+@{1}.+\.{1}.+$/;
	return re.test(valToChk);
}

function isZip(valToChk) {
	/*
		check for properly formatted zip code
			- check for start of string (^)
			- followed by 5 numbers
			- followed by end of string ($)
	*/
	re = /^\d{5}$/;
	return re.test(valToChk);
}

function isStAbbrv(valToChk) {
	/*
		check for properly formatted state abbreviation
			- check for start of string (^)
			- followed by 2 alpha characters
			- followed by end of string ($)
	*/
	re = /^[A-Za-z]{2}$/;
	return re.test(valToChk);
}

// Verifies radio or checkbox set has at least one item checked
function isChecked (list, msg) {
	var checked = false;
	for (var i=0, n=list.length; i<n; i++) {
		if (list[i].checked) {
			checked = true;
			break;
		}
	}
	// Alert and focus if a message is defined
	if (!checked && (msg != undefined)) {
		alert(msg);
		list[0].focus();
	}
	return checked;
}

function isPhoneNbr(valToChk) {
	/*
		check for properly formatted phone number
			- check for start of string (^)
			- followed by 3 numbers
			- followed by a "-"
			- followed by 3 numbers
			- followed by a "-"
			- followed by 4 numbers
			- followed by end of string ($)
	*/
	re = /^\d{3}-\d{3}-\d{4}$/
	return re.test(valToChk);
}

// Validates mm/dd/yy and mm/dd/yyyy dates
function isDate (valToChk) {
	return dateCheck(valToChk);
}
function dateCheck (dateval) {
	// Valid date check
	if (isNaN(new Date(dateval).getYear()))
		return (0);

	// Length check
	if ((dateval.length != 8) && (dateval.length != 10))
		return (0);

	// Formatting check
	a = dateval;
	
	slash1=a.indexOf("/");

	if ((slash1 == 0) || (slash1 == -1))
		return (0);

	d1 = slash1 + 1;
	
	slash2=a.indexOf("/", d1);
	
	if (slash2 == -1)
		return (0);
		
	y1 = slash2 + 1
	
	mm = a.substring(0, slash1)// month
	dd = a.substring(d1, slash2)// day
	if (dateval.length == 10) {
		yy = a.substr(y1+2, 2)// year
	} else {
		yy = a.substr(y1, 2)// year
	}
	
	if (yy.length != 2)
		return (0);

	//basic error checking
	if (mm<1 || mm>12) return (0);
	if (dd<1 || dd>31) return (0);
	if (yy<0 || yy>99) return (0);
	
	//advanced error checking

	// months with 30 days
	if (mm==4 || mm==6 || mm==9 || mm==11)
		{
		if (dd==31) return (0);
			}

	// february, leap year
	if (mm==2)
		{
		// feb
		var g=parseInt(yy/4,10)
		if (isNaN(g)) 
			{
			return (0);
			}

		if (dd>29) return (0);
		if (dd==29 && ((yy/4)!=parseInt(yy/4,10))) return (0);
		}

	return true;
}

function showLogin() {
	try {
		defaultpanel.togglepanelplus('down');
		document.loginForm.empID.focus();
	}
	catch (e) {
		alert('You are currently logged in to the Healthy Edge Web site.');
	}
}



function showHideRegForm(fldVal, divObj) {
	if (fldVal != "employee") {//== "spouse" || fldVal == "dependent"
		toggle(divObj, "visible", "block");
	}
	else {// if (fldVal == "employee") {
		toggle(divObj, "hidden", "none");
	}
}

function chkRegForm(frmObj) {
	with(frmObj) {
		if (!isEmpID(empID.value)) {
			alert("Please enter an eight digit employee ID, including leading zeros (i.e., 00054321)");
			empID.focus();
			return false;
		}
		
		if (employeeMonthOfBirth.selectedIndex == 0) {
			alert("Please select a Employee Month of Birth.");
			employeeMonthOfBirth.focus();
			return false;
		}
		if (employeeDayOfBirth.selectedIndex == 0) {
			alert("Please select a Employee Day of Birth.");
			employeeDayOfBirth.focus();
			return false;
		}
		
		var empMDdate = employeeMonthOfBirth.options[employeeMonthOfBirth.selectedIndex].value + "/" + employeeDayOfBirth.options[employeeDayOfBirth.selectedIndex].value + "/2008";
		if (!isDate(empMDdate)) {
			alert("The Employee Month/Day of Birth is not a valid combination, please verify your selections.")
			employeeMonthOfBirth.focus();
			return false;
		}
		
		if (employeeAddrStNbr.value == "") {
			alert("Please enter the Employee Address Street Number.");
			employeeAddrStNbr.focus();
			return false;
		}
		
		if (uType.selectedIndex == 0) {
			alert("Please select a User Type.");
			uType.focus();
			return false;
		}
		
		//if user type is anything other than employee, run "extra" validation
		if (uType.selectedIndex > 1) {
			if (firstName.value == "") {
				alert("Please enter your First Name.");
				firstName.focus();
				return false;
			}
			
			if (lastName.value == "") {
				alert("Please enter your Last Name.");
				lastName.focus();
				return false;
			}
			
			//force selection of address question option
			if (!gender[0].checked && !gender[1].checked) {
				alert("Please indicate your gender.");
				gender[0].focus();
				return false;
			}
			
			//force selection of address question option
			if (!addrSameAsEmp[0].checked && !addrSameAsEmp[1].checked) {
				alert("Please indicate if your address is the same as or different\nfrom that of the TECO employee in your family.");
				addrSameAsEmp[0].focus();
				return false;
			}
			//if different from employee, require entry of address
			else if (addrSameAsEmp[1].checked) {
				if (addrLine1.value == "") {
					alert("Please enter Address Line 1.")
					addrLine1.focus();
					return false;
				}
				if (addrCity.value == "") {
					alert("Please enter a City.")
					addrCity.focus();
					return false;
				}
				if (!isStAbbrv(addrState.value)) {
					alert("Please enter a two-character State abbrevation.")
					addrState.focus();
					return false;
				}
				if (!isZip(addrZip.value)) {
					alert("Please enter a 5 digit zip code.")
					addrZip.focus();
					return false;
				}
			}				

			if (emailAddress.value != "" && !isEmailAddress(emailAddress.value)) {
				alert("Please enter a properly formated email address (i.e., someone@domain.com)");
				emailAddress.focus();
				return false;
			}
			
			if (emailAddress.value != "" && emailAddressConf.value != emailAddress.value) {
				alert("Email addresses do not match. Please retype your email address.");
				emailAddressConf.value = "";
				emailAddressConf.focus();
				return false;
			}

			if (monthOfBirth.selectedIndex == 0) {
				alert("Please select a Month of Birth.");
				monthOfBirth.focus();
				return false;
			}
			if (dayOfBirth.selectedIndex == 0) {
				alert("Please select a Day of Birth.");
				dayOfBirth.focus();
				return false;
			}
			
			var mdDate = monthOfBirth.options[monthOfBirth.selectedIndex].value + "/" + dayOfBirth.options[dayOfBirth.selectedIndex].value + "/2008";
			if (!isDate(mdDate)) {
				alert("The Month/Day of Birth is not a valid combination, please verify your selections.")
				monthOfBirth.focus();
				return false;
			}
		}
		
		if (ageRange.selectedIndex == 0) {
			alert("Please select an Age Range.");
			ageRange.focus();
			return false;
		}
		if (tshirtSize.selectedIndex == 0) {
			alert("Please select an T-Shirt Size.");
			tshirtSize.focus();
			return false;
		}
	}

	return true;
}

function hideRegForm() {
	toggle('nonEmpInfo', "hidden", "none");
	toggle('addrDiff', "hidden", "none");
}



//For Calendar
function viewday (yr, mm, dd, fa) {
	window.open('index.cfm?fuseaction=' + fa + '&year=' +yr+ '&month=' +mm+ '&day=' +dd,'VIEWDAY','scrollbars,resizable,height=400,width=400');
}
function drawAlert () {
	alert ("Please set the layout of your printer to 'landscape' mode in order to print this calendar properly.  Also, please set all of the margins of this document to 0.5in.  Thank you.");//This page provides a printable copy of the 6.0 Cold Fusion Calendar Application.  
}


