//Trims left side spaces. Used by trimAll.
function leftTrim ( s ){
	return s.replace( /^\s*/, "" )
}

//Trims right side spaces. Used by trimAll.
function rightTrim ( s ){
	return s.replace( /\s*$/, "" );
}

//Trims left and right side spaces.
function trimAll ( s ){
	return rightTrim(leftTrim(s));
}

function isEffDateValid(effDate,nyMon, nyDay, nyYr){
	//Validate Effective Date for Event 29 = Manager H&W Benefits.
	var effDt;
	effDt = effDate.value;
	
	if (!IsDate(effDate,0)){
		alert("Please enter a valid Effective Date.");
		effDate.focus();
		return false;}	
		
	var vdate = new Date(effDt);
	var nydate = new Date(nyYr, nyMon-1, nyDay);
	if (vdate > nydate){
		alert("The Effective Date entered must be less than Next Year's Plan Start date. Please review and change.");
		return false;}
		
	return true;
}

function IsEmpty(s){  
	var ss=trimAll(s)
	return ((ss == null) || (ss.length == 0))
}

function IsSSN(el, defaultEmptyOK)	{
	var i, newSSN, t;
	t = trimAll(el.value);
	newSSN = "";
	if (IsEmpty(t)) return defaultEmptyOK;
		
	for (i=0; i < t.length; i++){
		if ((t.charCodeAt(i)<58) && (t.charCodeAt(i)>47)) {
			newSSN = newSSN+t.charAt(i);
		}
	}		
	if (newSSN.length != 9){			
		return false;
	}
	return true;
}

//Correctly formats the SSN.       Eg: "123456789*/" =>  "123-45-6789"
function FormatSSN(usrSSN){
	var knt=0, newSSN="";
	if (usrSSN=="") {
		return "";
	}else{
		for (var i=0; i < usrSSN.length; i++){
			if ((usrSSN.charCodeAt(i)<58) && (usrSSN.charCodeAt(i)>47)) {
				newSSN = newSSN + usrSSN.charAt(i);
				knt++;
				if ((knt==3)||(knt==5)) newSSN = newSSN + "-";
			}
		}	
		return newSSN;
	}
}

//Correctly formats the Zip Code.       Eg: "123456789*/" =>  "12345-6789"
function FormatZip(usrZip){
	var knt=0, newZip="", usrZipLength=0;
	if (usrZip=="") {
		return "";
	}else{
		//Determine if zip is 5-digit or 9-digit
		for (var i=0; i < usrZip.length; i++){
			if ((usrZip.charCodeAt(i)<58) && (usrZip.charCodeAt(i)>47)) {
				usrZipLength++;	//Number of digits in zip code
			}
		}	
		if (usrZipLength==9){
			for (var i=0; i < usrZip.length; i++){
				if ((usrZip.charCodeAt(i)<58) && (usrZip.charCodeAt(i)>47)) {
					newZip = newZip + usrZip.charAt(i);
					knt++;
					if (knt==5) newZip = newZip + "-";
				}
			}	
		}else{
			for (var i=0; i < usrZip.length; i++){
				if ((usrZip.charCodeAt(i)<58) && (usrZip.charCodeAt(i)>47)) {
					newZip = newZip + usrZip.charAt(i);
				}
			}	
		}
		return newZip;
	}
}

//Correctly formats the phone numbers.       Eg: "1234567890*/" =>  "123-456-7890"
function FormatPhone(usrPhone){
	var knt=0, newPhone="";
	if (usrPhone=="") {
		return "";
	}else{
		for (var i=0; i < usrPhone.length; i++){
			if ((usrPhone.charCodeAt(i)<58) && (usrPhone.charCodeAt(i)>47)) {
				newPhone = newPhone + usrPhone.charAt(i);
				knt++;
				if ((knt==3)||(knt==6)) newPhone = newPhone + "-";
			}
		}	
		return newPhone;
	}
}

//Correctly formats the phone numbers with extensions.      Eg: "1234567890*/3222" =>  "123-456-7890-3222"
function FormatPhoneExt(usrPhone){
	var knt=0, newPhone="", usrPhoneLength=0;
	if (usrPhone=="") {
		return "";
	}else{
		for (var i=0; i < usrPhone.length; i++){
			if ((usrPhone.charCodeAt(i)<58) && (usrPhone.charCodeAt(i)>47)) {
				usrPhoneLength++;	//Number of digits in phone number - determines if ext is included or not.
			}
		}	
		if (usrPhoneLength==10){	//Only phone, no extension.
			for (var i=0; i < usrPhone.length; i++){
				if ((usrPhone.charCodeAt(i)<58) && (usrPhone.charCodeAt(i)>47)) {
					newPhone = newPhone + usrPhone.charAt(i);
					knt++;
					if ((knt==3)||(knt==6)) newPhone = newPhone + "-";
				}
			}	
		}else{		//Phone with extension.
			for (var i=0; i < usrPhone.length; i++){
				if ((usrPhone.charCodeAt(i)<58) && (usrPhone.charCodeAt(i)>47)) {
					newPhone = newPhone + usrPhone.charAt(i);
					knt++;
					if ((knt==3)||(knt==6)||(knt==10)) newPhone = newPhone + "-";
				}
			}	
		}
		return newPhone;
	}
}

function IsEmail(el,defaultEmptyOK)	{
	var i,t, pos1, pos2,ch1, ch2;
	ch1 = "@";
	ch2 = "."
	t=trimAll(el.value)
	if (IsEmpty(t)) return defaultEmptyOK;		
		
	pos1=t.indexOf("@");
	pos2=t.indexOf(".");
	
	if ((pos1>0) && (pos2>0))
		return true;
	else
		return false;
}	


function IsDate(mdy,defaultEmptyOK){
	var i;
	var day = "";
	var month = "";
	var year = "";
	var flag = 1;
	var d = mdy.value;
	if (IsEmpty(d)) return defaultEmptyOK;	
	for (i=0; i < d.length; i++){
	
		if ((d.charCodeAt(i)<58) && (d.charCodeAt(i)>47)) {	
			
			if (flag==1){
				month=month+d.charAt(i)					
			}else if (flag==2){
				day=day+d.charAt(i)	
			}else if (flag==3){
				year=year+d.charAt(i)
				if (year.length>4){
					return false;
				}
			}	
		}else if(d.charAt(i)=="/"){
			flag=flag+1;
		}else{
			return false;
		}
	}
	if ((year.length!=4) || (isNaN(year)) || (year <= 1900) || (year >= 2079) || (year == "")){
		return false;
	}
	
	day=parseInt(day,10);
	month=parseInt(month,10);
	year=parseInt(year,10);		
	if ((isNaN(month)) || (isNaN(day)) || (isNaN(year)))
	return false;
	if ((month==0) || (day==0) || (year==0))
	return false;

	if (month==2){
		if (IsLeapYear(year)) {
			if (day>29)	
				return false;			
			else 
				return true;
		}else {
			if (day>28)	
				return false;			
			else 
				return true;
		}
	}else{		
		if ((isNaN(month)) || (month >12 || month==0) || (month == ""))
			return false;
		if ((isNaN(day)) || (day >31 || day==0) || (day == ""))
			return false;
			
		if (month<=7){
			if ((month%2 == 1) && (day>31))
				return false;
			else 
				if ((month%2 == 0) && (day>30))
					return false;
				else return true;
		}else {
			if ((month%2 == 0) && (day>31))
				return false;
			else 			
				if ((month%2 == 1) && (day>30))
					return false;
				else 
					return true;
		}
	}	
	return true;
}

function IsLeapYear(y){
	if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))
		return true;
	else 
		return false;
}
	function IsZip(el,defaultEmptyOK)
		{
			var i, newZip, t;
			t=trimAll(el.value);
			newZip="";
			if (IsEmpty(t)) return defaultEmptyOK;	
			for (i=0; i < t.length; i++)
			{
				if ((t.charCodeAt(i)<58) && (t.charCodeAt(i)>47)) 
				{
					newZip=newZip+t.charAt(i);
				}
			}		
			if (newZip.length != 3){			
				return false;
			}
			return true;
		}	
function IsZip1(el,defaultEmptyOK){
	var i, newZip, t;
	t=trimAll(el.value);
	newZip="";
	if (IsEmpty(t)) return defaultEmptyOK;	
	for (i=0; i < t.length; i++){
		if ((t.charCodeAt(i)<58) && (t.charCodeAt(i)>47)) {
			newZip=newZip+t.charAt(i);
		}
	}		
	if ((newZip.length != 5) && (newZip.length != 9)){			
		return false;
	}
	return true;
}	
	
function IsCurrency(el,defaultEmptyOK){
	var t;
	t = trimAll(el.value);
	if (IsEmpty(t)) return defaultEmptyOK;	
	if (isNaN(t))return false;
	if (defaultEmptyOK)
	{
	if (parseInt(t)<0) return false;
	}
	else
	{
	if (parseInt(t)<=0) return false;
	}
	return true;	
}
	


function IsYear(el,defaultEmptyOK){
	if (IsEmpty(el)) return defaultEmptyOK;	
	if (isNaN(el))return false;
	if ((parseInt(el)<1901)||(parseInt(el)>2078)) return false;
	return true;	
}

function IsDigit(el,defaultEmptyOK)	{
	var i,t;
	t = trimAll(el.value);
	if (IsEmpty(t)) return defaultEmptyOK;	
	for (i=0; i < t.length; i++){
		if ((t.charCodeAt(i)>=58) || (t.charCodeAt(i)<=47)) {
			return false;
		}			
	}
	if (parseInt(t)<0) return false;
	return true;
}

function IsPhone(el,defaultEmptyOK){
	var i, newPhone,t;
	t = trimAll(el.value);
	if (IsEmpty(t)) return defaultEmptyOK;	
	newPhone="";
	if (t.length>0)	{
		for (i=0; i< t.length; i++){
			if ((t.charCodeAt(i)<58) && (t.charCodeAt(i)>47)) {
				newPhone=newPhone+t.charAt(i)	
			}
		}			
		if (newPhone.length != 10) return false;
	}
	return true
}

function IsPhoneExt(el,defaultEmptyOK){
	var i, newPhone,t;
	t = trimAll(el.value);
	if (IsEmpty(t)) return defaultEmptyOK;	
	newPhone="";
	if (t.length>0)	{
		for (i=0; i< t.length; i++){
			if ((t.charCodeAt(i)<58) && (t.charCodeAt(i)>47)) {
				newPhone=newPhone+t.charAt(i);
			}
		}		
		if (newPhone.length < 10) return false;
	}
	return true;
}

function IsRadioSelected(radio_form, radio_button){
	var radio_choice = false;
	var counter, maxIndex;
	
	//All options ineligible - so only images (no radio buttons) are displayed.
	if (!(eval("document."+radio_form+"."+radio_button))){
		return (true);
	}
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) { //Single option available
		if (!(eval("document."+radio_form+"."+radio_button).checked)){
			return (false);
		}	
	}else{ //Multiple options available
		//Loop through all the radio elements.
		for (counter = 0; counter < maxIndex; counter++){
			//Is any radio element checked?
			if (eval("document."+radio_form+"."+radio_button)[counter].checked) {
				radio_choice = true; 
			}
		}
		//If no radio items are checked, return false
		if (!radio_choice){
			return (false);
		}
	}
	//If at least 1 radio item is checked, return true
	return (true);
}

function IsThisRadioSelected(radio_form, radio_button, index){
	var radio_choice = false;
	var  maxIndex;
	
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) {
		if (eval("document."+radio_form+"."+radio_button).checked) return (true);
	}else{
		if (eval("document."+radio_form+"."+radio_button)[index].checked) return (true);
	}	
	return (false);
}

function GetThisRadioValue(radio_form, radio_button, index){
	var maxIndex;
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) { //Single option available
		return (eval("document."+radio_form+"."+radio_button).value);
	}else{ //Multiple options available, Is any radio element checked?
		return eval("document."+radio_form+"."+radio_button)[index].value;
	}
	return (null);
}

function GetRadioSelected(radio_form, radio_button){
	var counter, maxIndex;
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) { //Single option available
		return (eval("document."+radio_form+"."+radio_button).value);
	}else{ //Multiple options available
		for (counter = 0; counter < maxIndex; counter++){
			//Is any radio element checked?
			if (eval("document."+radio_form+"."+radio_button)[counter].checked) {
				return eval("document."+radio_form+"."+radio_button)[counter].value;
			}
		}
	}
	return (null);
}

function DeselectRadio(radio_form, radio_button, radio_index){
	var maxIndex;
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) { //Single option available
		eval("document."+radio_form+"."+radio_button).checked = false;
	}else{ //Multiple options available
		eval("document."+radio_form+"."+radio_button)[radio_index].checked = false;
	}
	return (true);
}

function SelectRadio(radio_form, radio_button, radio_index){
	var maxIndex;
	maxIndex = eval("document."+radio_form+"."+radio_button).length;
	if (isNaN(maxIndex)) { //Single option available
		eval("document."+radio_form+"."+radio_button).checked = true;
	}else{ //Multiple options available
		eval("document."+radio_form+"."+radio_button)[radio_index].checked = true;
	}
	return (true);
}

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}
