var serverSideErrorMessages = new Array();

function addServerSideErrorMessage(message)
{
	serverSideErrorMessages[serverSideErrorMessages.length] = message;
}

function displayServerSideErrorMessages(heading)
{
	if(serverSideErrorMessages.length == 0)
		return;
		
	var messages = "";
	
	for(var i = 0; i < serverSideErrorMessages.length;i++)
	{
		messages = messages + "\n* " + serverSideErrorMessages[i];
	}
	
	messages = heading + "\n" + messages;
	window.alert(messages);
}

// Need to show plan description document in new window
function displayBenefitGrid(planDescriptionURL)
{
	// If we don't have a plan description, then alert the user
	if(planDescriptionURL == "")
	{
		alert("No Information for this product Id.");
	}
	// Else, open a new window pointed to the provided url for the plan description
	else
	{
		window.open(planDescriptionURL,'MS_PlanDescription', 'top=100,left=150,height=550,width=750,status=no,resizable=yes,scrollbars=yes,toolbar=no,titlebar=no,menubar=no,location=no');
	}
			
}

// Function to launch Glossary in a separate window. It either displays an individual item or displays the 
// entire list.
var GlossaryChildWindow;
function launchGlossary(strGlossaryURL, strKey)
{
	if (GlossaryChildWindow == null || GlossaryChildWindow.closed)
	{
		var GlossaryURL = strGlossaryURL;
		
		if (strKey != "")
		{
			GlossaryURL += "#" + strKey;
		}
		window.open(GlossaryURL, 'MS_Glossary', "height=500,width=560,location=0,status=0,resizeable=1,scrollbars=yes,toolbar=0");
	}
}

// Function to launch FAQ in a separate window.
var FAQChildWindow;
function launchFAQ(strFAQURL)
{
	if (FAQChildWindow == null || FAQChildWindow.closed)
	{
		FAQChildWindow = window.open(strFAQURL, 'MS_FAQ', "toolbar=no,menubar=no,location=no,status=yes,height=500,width=600,scrollbars=yes");
	}
	else
	{
		FAQChildWindow.focus();
	}
}

// Function to launch Contact Us in a separate window.
var ContactUsChildWindow;
function launchContactUs(strContactUsURL, memberID, userID, groupType)
{
	if (ContactUsChildWindow == null || ContactUsChildWindow.closed)
	{
		var windowcontent = "<HTML><HEAD><TITLE></TITLE>";
		windowcontent += "<script language=javascript> function popuponload(){document.forms[0].submit();}<\/script>";
		windowcontent += "</HEAD>";
		windowcontent += "<body onload='javascript:popuponload();'>";
		windowcontent += "<form name = 'MS-ContactUs' method='post' action=\"" + strContactUsURL  + "\">";

		windowcontent += "<input type=\"hidden\" name = \"MEMBER_ID\" value=\"" + memberID + "\">";
		windowcontent += "<input type=\"hidden\" name = \"USER_ID\" value=\"" + userID + "\">";
		windowcontent += "<input type=\"hidden\" name = \"GROUP_TYPE\" value=\"" + groupType + "\">";

		windowcontent += "</form></body></html>";
		ContactUsChildWindow = window.open("", "MS_ContactUs");
		ContactUsChildWindow.document.write(windowcontent);
		ContactUsChildWindow.document.close();
	}
	ContactUsChildWindow.focus();
	ContactUsChildWindow.location.reload();
}


function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

var waiveWindow;
var POSTBACK_TIME_OUT = 100;
var userOption="";

function OpenWaivePopup()
{
	var frm = document.forms[0];
	
	frm.elements["txtNavigateTo"].value = "Y";
	
	// Open window, set target to new window, then submit
	waiveWindow =  window.open("", "SelectBenefitWaive","top=150,left=250,height=500,width=500,status=no,resizable=no,scrollbars=yes,toolbar=no,titlebar=no,menubar=no,location=no");
	frm.target = "SelectBenefitWaive";
	
	frm.submit();
	setTimeout("checkForWindowClose()", POSTBACK_TIME_OUT);

	// Reset target, action, and delete dependent person key
	frm.target = "";
	frm.elements["txtNavigateTo"].value = "";
}

function checkForWindowClose()
{
	try
	{
		if(waiveWindow.document != null)
			setTimeout("checkForWindowClose()", POSTBACK_TIME_OUT);
	}
	catch(e)
	{ 
		if(this.userOption == "")
			SetAcceptDeclineWaive('N');
	}
}

var ViewPlanChildWindow;
function launchMedicalViewPlan(planDocURL, custID, classNumber, effectiveDate, groupType)
{
	var benPackageID = getSelectedBenefitPackageId();
	
	if(benPackageID != null)
	{
		if (ViewPlanChildWindow == null || ViewPlanChildWindow.closed)
		{
			var windowcontent = "<HTML><HEAD><TITLE></TITLE>";
			windowcontent += "<script language=javascript> function popuponload(){document.objform.submit();}<\/script>";
			windowcontent += "</HEAD>";
			windowcontent += "<body onload='javascript:popuponload();'>";
			windowcontent += "<form name = 'objform' method='post' action=\"" + planDocURL  + "\">";

			windowcontent += "<input type=\"hidden\" name = \"CUST_ID\" value=\"" + custID + "\">";
			windowcontent += "<input type=\"hidden\" name = \"CLASS_NBR\" value=\"" + classNumber + "\">";
			windowcontent += "<input type=\"hidden\" name = \"EFF_DATE\" value=\"" + effectiveDate + "\">";
			windowcontent += "<input type=\"hidden\" name = \"BEN_PKG_ID\" value=\"" + benPackageID + "\">";
			windowcontent += "<input type=\"hidden\" name = \"GROUP_TYPE\" value=\"" + groupType + "\">";
			windowcontent += "</form></body></html>";
			ViewPlanChildWindow = window.open("","MS_ViewPlanDocument");
			ViewPlanChildWindow.document.write(windowcontent);
			ViewPlanChildWindow.document.close();
		}
		ViewPlanChildWindow.focus();
		ViewPlanChildWindow.location.reload();
	}
	else
	{
		alert('Please select a plan.');
	}
}

//for Dental
function launchDentalViewPlan(planDocURL)
{
	var benPackageID = getSelectedBenefitPackageId();
	
	if(benPackageID != null)
	{
		if (ViewPlanChildWindow == null || ViewPlanChildWindow.closed)
		{
			var pdfFileName = getPDFFileName(benPackageID);
			if(pdfFileName != null)
				window.open(planDocURL + pdfFileName,"MS_ViewPlanDocument");
			else
				alert("Benefit Package Id is not proper.");
		}
	}
	else
	{
		alert('Please select a plan.');
	}
}

function getPDFFileName(benPkgId)
{
	switch(benPkgId) 
	{ 
		case "030464001": case "030463901": case "030463701": case "030463801": 
			return "HIBPD50030HD 5-04.pdf"; 
		case "030465701": case "030465401": case "030465501": case "030465601": 
			return "HIBPD50033HD 5-04.pdf"; 
		case "030465301": case "030465001": case "030465101": case "030465201": 
			return "HIBPD50031HD 5-04.pdf"; 
		case "030464801": case "030464501": case "030464601": case "030464701": 
			return "HIBPD50034HD 5-04.pdf"; 
		case "030464401": case "030464101": case "030464201": case "030464301": 
			return "HIBPD50032HD 5-04.pdf"; 
	}
	
	return null;

}


var EmployerLetterChildWindow;
function launchEmployerLetter(employerWelcomeText)
{
	EmployerLetterChildWindow = window.open("", "EmployerLetter","top=150,left=250,height=500,width=500,status=no,resizable=no,scrollbars=yes,toolbar=no,titlebar=no,menubar=no,location=no");
	EmployerLetterChildWindow.document.write(employerWelcomeText);
	EmployerLetterChildWindow.document.close();

	EmployerLetterChildWindow.focus();
	EmployerLetterChildWindow.location.reload();
}

function launchApp(appURL, appWindowName)
{	
	if(appURL != "") 
		window.open(appURL, appWindowName,"top=150,left=250,height=500,width=500,status=no,resizable=no,scrollbars=yes,toolbar=no,titlebar=no,menubar=no,location=no");
	else
		alert("Application URL not found");
}

function launchPDF(pdfURL)
{	
	if(pdfURL != "") 
		window.open(pdfURL, '','top=100,left=150,height=550,width=750,status=no,resizable=yes,scrollbars=yes,toolbar=no,titlebar=no,menubar=no,location=no');
	else
		alert("Information not found");
}

function ValidateDate(source)
{
	var value = document.getElementById(source.controltovalidate).value;
	
	if(value.length == 0)
	{
		source.isvalid = true;
		return true;
	}

	//convert the value to a date
	var date = GetDate(value);
	
	if(date == null)
	{
		source.errormessage = source.invalidformat;
		source.isvalid = false;
		return false;
	}
	
	if(source.minvalue)
	{
		var minDate = GetDate(source.minvalue);
		
		if(minDate && date < minDate)
		{
			source.errormessage = source.errorminvalue;
			source.isvalid = false;
			return false;
		}
	}
	
	if(source.maxvalue)
	{
		var maxDate = GetDate(source.maxvalue);
		
		if(maxDate && date > maxDate)
		{
			source.errormessage = source.errormaxvalue;
			source.isvalid = false;
			return false;
		}
	}

	source.isvalid = true;
	return true;	
}

function GetDate(op)
{
	var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
	var m = op.match(yearLastExp);
	if (m == null) {
		return null;
	}
	day = m[3];
	month = m[1];
	year = (m[5].length == 4) ? m[5] : parseInt(m[6], 10) + 2000 - (parseInt(m[6], 10) < 2029 ? 0 : 100);
	month -= 1;
	var date = new Date(year, month, day);
	return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date : null;
}

function DateFormat(obj)
{
	
	var vDate=obj.value;
	var strSeperator ="/";
	
	//Find ot the length and slash char
	if (vDate.length  >= 8 && ((vDate.indexOf("/") == -1) && (vDate.indexOf("-") == -1))) // mmddyyyy
	{
		var mDay = vDate.substr(2,2);
		var mMonth = vDate.substr(0,2);
		var mYear = vDate.substr(4,4)
		obj.value = mMonth+strSeperator+mDay+strSeperator+mYear;
	}
}

var defaultButton;

function RegisterDefaultButton(ctl)
{
	defaultButton = document.getElementById(ctl);
	
	if(defaultButton)
	{
		document.body.onkeypress = HandleEnter;
	}
}

function HandleEnter()
{
	try
	{
		if(defaultButton && event.keyCode == 13)
		{
			var act = document.activeElement;
			
			if(UseDefaultButton(act))
			{
				window.event.cancelBubble = true;
				window.event.returnValue = false;
				defaultButton.focus();
				defaultButton.click();
			}
		}
	}
	catch(e) {}
}

function UseDefaultButton(ele)
{
	var bRet = true;
	
	if(ele)
	{
		var tag = ele.tagName.toUpperCase();
		
		if(tag == "INPUT")
		{
			var type = ele.type.toUpperCase();
			if(type == "BUTTON" || type == "IMAGE" || type == "RESET")
				bRet = false;
		}
		else if(tag == "A")
			bRet = false;
		else if(tag == "BUTTON")
			bRet = false;
		else if(tag == "SELECT")
			bRet = false;
	}
	
	return bRet;	
}

function SetActiveControl(id)
{
	var ele = document.getElementById(id);
	
	if(ele)
		ele.focus();
}