// JavaScript Document
function newImage(arg) 
{
	if (document.images) 
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function preloadImages() 
{
	if (document.images) 
	{		
		myAccountsRoll = newImage("./images/menu/myAccountsRoll.gif");
		myProfileRoll = newImage("./images/menu/myProfileRoll.gif");
		resourcesRoll = newImage("./images/menu/resourcesRoll.gif");
		contactBNYRoll = newImage("./images/menu/contactBNYRoll.gif");
		homeAboutRoll = newImage("./images/menu/homeAboutRoll.gif");
		homeEnrollRoll = newImage("./images/menu/homeEnrollRoll.gif");
		homeResourcesRoll = newImage("./images/menu/homeResourcesRoll.gif");
		menuAcSummaryRoll = newImage("./images/menu/menuAcSummaryRoll.gif");
		menuReportsRoll = newImage("./images/menu/menuReportsRoll.gif");
		whiteSquare = newImage("./images/bullets/whiteSquare.gif");
		whiteSquareSmall = newImage("./images/bullets/whiteSquareSmall.gif");		
		plus = newImage("./images/plus.gif");	
		minus = newImage("./images/minus.gif");	
		leaf = newImage("./images/leaf.gif");	
		bleaf = newImage("./images/bleaf.gif");			
	}
}
function getCalendarDate()
{
   var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
 
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthday    = now.getDate();
   var year        = now.getYear();
 
   if (year < 2000) { year = year + 1900;}
 
   var dateString = monthname + ' ' + monthday + ', ' + year;
 
   return dateString;
} // function getCalendarDate()

function settime () 
{
	var curtime = new Date(); 
	var curhour = curtime.getHours();
	var curmin = curtime.getMinutes();
	var cursec = curtime.getSeconds();
	var time = "";
	var calendarDate = getCalendarDate();
	
	time = calendarDate + " " + (curhour) + ":" +
			(curmin < 10 ? "0" : "") + curmin; 
	document.frmDate.clock.value = time ;
}



function SSNValidation(ssn) 
{
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	var msg;
	if (matchArr == null || numDashes == 1) 
	{	
		return false;
	}
	else if (parseInt(matchArr[1],10)==0) 
	{		
		return false;
	}
	else 
	{
		return true;
	}
}
function RTrim(str)
{
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
		   {
            // We have a string with trailing blank(s)...
            var i = s.length - 1;       // Get length of string
            // Iterate from the far right of string until we
            // don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;
            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }
        return s;
}
function LTrim(str)
{
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(0)) != -1)
		   {
            // We have a string with leading blank(s)...
            var l = s.length - 1;       // Get length of string
            var i =0;
            // Iterate from the beginning of string until we don't have any more whitespace...
            while (i <= l && whitespace.indexOf(s.charAt(i)) != -1)
                i++;
            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(i, l+1);
        }
        return s;
}
function Trim(str)
{
      return RTrim(LTrim(str));
}

function  validateShareAccess()
{
	var tmp, i
	var missing = new Array()
	i = 0
	var SSN = document.FRM.SSN.value;
	var confirmSSN = document.FRM.confirmSSN.value;
	var txtPIN = document.FRM.txtPIN.value;
	var cboCompany = document.FRM.cboCompany.selectedIndex;
	var Account = document.FRM.Account.value;
	var PIN2 = document.FRM.PIN2.value;
	if (SSN != "" || txtPIN != "")
	{
		if (Trim(SSN) == '')
		{
			//alert ("Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN");
			
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN";
			document.FRM.SSN.focus();
			document.FRM.SSN.value = "";
			return false;		
		}
		else if (!SSNValidation(SSN)) 
		{
			//missing[i] = "Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN\n"; i++	
			//alert ("Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN");
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN";
			document.FRM.SSN.focus();
			document.FRM.SSN.value = "";
			return false;
		}
		else if (Trim(confirmSSN) == '')
 		{
 			//alert ("Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN");
 			
 			document.getElementById("hideErrorMesg").style.display = "block";
 			document.getElementById("errMsg").innerText = "Invalid ConfirmSSN. Must be 9 digits in the form NNN-NN-NNNN";
 			document.FRM.confirmSSN.focus();
 			document.FRM.confirmSSN.value = "";
 			return false;		
 		}
 		else if (!SSNValidation(confirmSSN)) 
 		{	
 			document.getElementById("hideErrorMesg").style.display = "block";
 			document.getElementById("errMsg").innerText = "Invalid ConfirmSSN. Must be 9 digits in the form NNN-NN-NNNN";
 			document.FRM.confirmSSN.focus();
 			document.FRM.confirmSSN.value = "";
 			return false;
 		}
 		else if(Trim(SSN) != Trim(confirmSSN))
 		{	
 			document.getElementById("hideErrorMesg").style.display = "block";
 			document.getElementById("errMsg").innerText = "SSN and ConfirmSSN do not match. Please enter correct SSN and ConfirmSSN.";
 			document.FRM.SSN.focus();
 			document.FRM.SSN.value = "";
 			return false;
 		}
		else if(Trim(txtPIN)=='')
		{	
			//missing[i] = " Enter a valid Personal Identification Number (PIN)\n"; i++			 
			//alert ("Enter a valid Personal Identification Number (PIN)");
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Enter a valid Personal Identification Number (PIN)";
			document.FRM.txtPIN.focus();
			document.FRM.txtPIN.value = "";
			return false;
		}				
	}
	else if (SSN == "" && (Account != "" || cboCompany != "" || PIN2 != ""))
	{
		if (cboCompany == 0)
		{			  
			//alert ("Please select a Company");
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Please select a Company";
			return false;
			//missing[i] = " Please select a Company\n"; i++		
		}
		else if (Trim(Account)=='')
 		{	
			//missing[i] = " Enter a valid Account Number\n"; i++	
			//alert ("Enter a valid Account Number");
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Enter a valid Account Number";
			document.FRM.Account.focus();
			document.FRM.Account.value = "";
			return false;
		}	
		else if(Trim(PIN2)=='')
		{	
			//missing[i] = " Enter a valid Personal Identification Number (PIN)\n"; i++
			//alert ("Enter a valid Personal Identification Number (PIN)");
			document.getElementById("hideErrorMesg").style.display = "block";
			document.getElementById("errMsg").innerText = "Enter a valid Personal Identification Number (PIN)";
			document.FRM.PIN2.focus();
			document.FRM.PIN2.value = "";
			return false;					 
		}			
	}
	else
	{
		//missing[i] = " Enter valid Personal Information or Account Information\n"; i++
		//alert ("Enter valid Personal Information or Account Information");
		document.getElementById("hideErrorMesg").style.display = "block";
		document.getElementById("errMsg").innerText = "Enter valid Personal Information or Account Information";
		return false;
	}
	if (i>0)
	{
		//alert("The following information is missing:\n\n-" + missing.join("-"))
		document.getElementById("hideErrorMesg").style.display = "block";
		document.getElementById("errMsg").innerText = "The following information is missing:\n\n-" + missing.join("-");
		return 0;
	}
    else
    {
		window.document.FRM.submit();
    }
}

// for validation of start date and end date on account history page
// call is date function to validate the value of the date
function validateDateAcHistory ()
{
	var sDateAH = Trim(document.dateAcHistory.sDate.value);
	var eDateAH = Trim(document.dateAcHistory.eDate.value);
	if (sDateAH == "")
	{
		alert ("Please Enter Start Date");
		return false;	
	}
	else
	{
		if (!isDate(sDateAH)){return false;};
	}
	if (eDateAH == "")
	{
		alert ("Please Enter End Date");
		return false;
	}
	else
	{
		if (!isDate(eDateAH)){return false;};
	}
	if(eDateAH < sDateAH)
	{
		alert("Start date should be less than End date");
		return false;
	}
	
	//isDate(eDateAH);
	
}
// this function sets the height of the footer
// Call this function on all pages for the body onload event
function setFooterHeight ()
{
	var winHeight = document.documentElement.clientHeight ;
	var pageHeight = document.body.offsetHeight;	
	var diff = (pageHeight  - winHeight);	
	if (diff < 3)
	{
		var footerHeight = Math.abs(pageHeight  - winHeight) + 19 ;
	}
	else
	{	
		footerHeight = 21;
	}		
	document.getElementById("f1").style.height = footerHeight + "px";
	document.getElementById("f2").style.height = footerHeight + "px";
	document.getElementById("f3").style.height = footerHeight + "px";
	document.getElementById("f4").style.height = footerHeight + "px";	
}

function initForTop() 
{
  setTimeout("onresize = setTop", 1000);
}

function setTop() 
{
  location.reload();
}

// function to validate the date
// pass the date value

function isDate (dateValue)
{
	var dateStr = dateValue;

    var datePat = /^(\d{1,2})(\/|)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as  mm/dd/yyyy");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;        
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days.");
            return false;
        }
    }
    return true; // date is valid
}
// function to validate whether account holding is selected in account history page
function validateHoldingSelect()
{
	var selHolding = document.formSelectHolding.selectHolding.selectedIndex;
	if (selHolding == 0)
	{
		alert ("Please Select a Holding");
		return false;
	}
	else
	{
		return true;
	}
	
}
// this function is to open a page from tab
// pass the url of the page
function clickTab(url)
{
	window.location=url;
}

var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial(printId,winWidth,winHeight,landscape)
{
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		
		html += '\n</HE' + 'AD>\n<BODY style=margin:0px>\n';
		
		var printReadyElem = document.getElementById(printId);
		
		if (printReadyElem != null)
		{
				html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printReady section in the HTML");
			return;
		}
			
		html += '\n</BO' + 'DY>\n</HT' + 'ML>';
		if (landscape == "yes")
		{
			if (confirm("This page prints best if your printer setting is Landscape\nDo you want to proceed?"))
			{
				var printWin = window.open("","printSpecial","menubar=no,width="+winWidth+",height="+winHeight+",location=no,resizable=yes,scrollbars=no,status=no");
				printWin.document.open();
				printWin.document.write(html);
				printWin.document.close();				
				if (gAutoPrint)
					printWin.print();
			}
		}
		else
		{
				var printWin = window.open("","printSpecial","menubar=no,width="+winWidth+",height="+winHeight+",location=no,resizable=yes,scrollbars=no,status=no");
				printWin.document.open();
				printWin.document.write(html);
				printWin.document.close();				
				if (gAutoPrint)
					printWin.print();			
		}			
	}
	else
	{
		alert("Sorry, the print ready feature is only available in modern browsers.");
	}
}
// Function to clear Form Fields
function clearfield(formfield)
{
	if (formfield.type == "text")
	{
		if ((formfield.name == "txtSearch") && (formfield.value == "Enter Symbol"))
		{
			formfield.value = "";
		}
	}
}

// calling the function to preload images 
	preloadImages();
// calling the function to preload images

//Function used to validate required fields
//This function takes the value and alert message that should be popped up
function checkContactFormFields()
{
	
	var value, flag; 
	value = document.contactForm.txtSHName.value;
	flag = checkForValue(value, "Please Enter Shareholder Names(s)");
	if (flag == 0)
	{
		document.contactForm.txtSHName.focus();
		return false;
	}
	
	value = document.contactForm.txtEmail.value;
	flag = checkForValue(value, "Please Enter Email Address");
	if (flag == 0)
	{
		document.contactForm.txtEmail.focus();
		return false;
	}
	
	flag = checkemail(value, "Please Enter Valid Email Address");
	if (flag == 0)
	{
		document.contactForm.txtEmail.focus();
		return false;
	}
	
	
	value = document.contactForm.txtAreaComments.value;
	flag = checkForValue(value, "Please Enter Enter Your Question, Specific Request or Comments");
	if (flag == 0)
	{
		document.contactForm.txtAreaComments.focus();
		return false;
	}
	
	return true;
}

function clearContact()
{
	document.contactForm.txtSHName.value = ""
	document.contactForm.txtEmail.value = ""
	document.contactForm.selCompanyInquiry.selectedIndex = 0
	document.contactForm.txtAreaComments.value = ""
}

function checkForValue(value, msg)
{
	if(value.length == 0)
	{
		alert(msg);
		return 0;
	}
}

function checkemail(str, msg)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if (!filter.test(str))	
	{
		alert(msg);
		return 0;
	}
}

function ValidateEditProfile()
{
	var value, flag; 
	value = document.editProfile.txtStreetAddr.value;
	flag = checkForValue(value, "Please Enter Street Address");
	if (flag == 0)
	{
		document.editProfile.txtStreetAddr.focus();
		return false;
	}
	
	value = document.editProfile.txtCity.value;
	flag = checkForValue(value, "Please Enter City");
	if (flag == 0)
	{
		document.editProfile.txtCity.focus();
		return false;
	}
	
	value = document.editProfile.txtState.value;
	flag = checkForValue(value, "Please Enter State");
	if (flag == 0)
	{
		document.editProfile.txtState.focus();
		return false;
	}
	
	value = document.editProfile.txtZip.value;
	flag = checkForValue(value, "Please Enter Zip");
	if (flag == 0)
	{
		document.editProfile.txtZip.focus();
		return false;
	}
	
	value = document.editProfile.txtPhone.value;
	flag = checkForValue(value, "Please Enter Phone");
	if (flag == 0)
	{
		document.editProfile.txtPhone.focus();
		return false;
	}
	
	value = document.editProfile.txtEmailAddr.value;
	flag = checkForValue(value, "Please Enter Email Address");
	if (flag == 0)
	{
		document.editProfile.txtEmailAddr.focus();
		return false;
	}
	
	flag = checkemail(value, "Please Enter Valid Email Address");
	if (flag == 0)
	{
		document.editProfile.txtEmailAddr.focus();
		return false;
	}
}

function validateEnrollStep2()
{
	
	var value, flag; 
	value = document.newAccountStep2.txtSSNTID.value;
	flag = checkForValue(value, "Please Enter SSN/Tax ID");
	if (flag == 0)
	{
		document.newAccountStep2.txtSSNTID.focus();
		return false;
	}
	/*if (!SSNValidation(value)) 
	{		
		alert ("Invalid SSN. Must be 9 digits in the form NNN-NN-NNNN");		
		document.newAccountStep2.txtSSNTID.focus();		
		return false;
	}*/
	
	
	flag = IsNumeric(value)
	if (flag == false)
	{
		alert("Enter Valid SSN/Tax ID");
		document.newAccountStep2.txtSSNTID.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtPRFname.value;
	flag = checkForValue(value, "Please Enter First Name");
	if (flag == 0)
	{
		document.newAccountStep2.txtPRFname.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtPRLName.value;
	flag = checkForValue(value, "Please Enter Last Name");
	if (flag == 0)
	{
		document.newAccountStep2.txtPRLName.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtDOB.value;
	flag = checkForValue(value, "Please Enter Date of Birth ");
	if (flag == 0)
	{
		document.newAccountStep2.txtDOB.focus();
		return false;
	}	
	
	value = document.newAccountStep2.txtEmailAddr.value;
	flag = checkForValue(value, "Please Enter Email Address ");
	if (flag == 0)
	{
		document.newAccountStep2.txtEmailAddr.focus();
		return false;
	}
	
	flag = checkemail(value, "Please Enter Valid Email Address");
	if (flag == 0)
	{
		document.newAccountStep2.txtEmailAddr.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtStreetAddr.value;
	flag = checkForValue(value, "Please Enter Street Address");
	if (flag == 0)
	{
		document.newAccountStep2.txtStreetAddr.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtCity.value;
	flag = checkForValue(value, "Please Enter City");
	if (flag == 0)
	{
		document.newAccountStep2.txtCity.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtState.value;
	flag = checkForValue(value, "Please Enter State");
	if (flag == 0)
	{
		document.newAccountStep2.txtState.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtZip.value;
	flag = checkForValue(value, "Please Enter Zip");
	if (flag == 0)
	{
		document.newAccountStep2.txtZip.focus();
		return false;
	}
	
	value = document.newAccountStep2.txtHomePhone.value;
	flag = checkForValue(value, "Please Enter Home Phone");
	if (flag == 0)
	{
		document.newAccountStep2.txtHomePhone.focus();
		return false;
	}
}

function IsNumeric(val)
{
	if(val<0) return false;
	return(parseFloat(val,10)==(val*1));
}


function validateCaContact()
{
	var value, flag; 
		
	value = document.caContact.txtCorporation.value;
	flag = checkForValue(value, "Please Enter Corporation");
	if (flag == 0)
	{
		document.caContact.txtCorporation.focus();
		return false;
	}
	
	value = document.caContact.txtEmailAddr.value;
	flag = checkForValue(value, "Please Enter Contact Email Address");
	if (flag == 0)
	{
		document.caContact.txtEmailAddr.focus();
		return false;
	}
	
	flag = checkemail(value, "Please Enter Valid Email Address");
	if (flag == 0)
	{
		document.caContact.txtEmailAddr.focus();
		return false;
	}
	
	value = document.caContact.txtAreaComments.value;
	flag = checkForValue(value, "Please Enter Contact Enter Your Question, Specific Request or Comments ");
	if (flag == 0)
	{
		document.caContact.txtAreaComments.focus();
		return false;
	}
}

function clearCaContact()
{
	document.caContact.selSendTO.selectedIndex = 0
	document.caContact.txtCorporation.value = ""
	document.caContact.txtEmailAddr.value = ""
	document.caContact.txtAreaComments.value = ""
}

// for validation of start date and end date on check history page
// call is date function to validate the value of the date
function validateCheckHistory()
{	
	var sDateAH = Trim(document.checkHistory.sDate.value);
	var eDateAH = Trim(document.checkHistory.eDate.value);
	if (sDateAH == "")
	{
		alert ("Please Enter Start Date");
		return false;	
	}
	else
	{
		if (!isDate(sDateAH)){return false;};
	}
	if (eDateAH == "")
	{
		alert ("Please Enter End Date");
		return false;
	}
	else
	{
		if (!isDate(eDateAH)){return false;};
	}
	
	if(eDateAH < sDateAH)
	{
		alert("Start date should be less than End date");
		return false;
	}
}

//Function to check if the selected period is valid
function validateRefreshProxy()
{	
	var sDateAH1 = Trim(document.checkHistory.SelProxydate.options[document.checkHistory.SelProxydate.selectedIndex].value);
	var eDateAH1 = Trim(document.checkHistory.sDate.value);
    var sDateAH2 = new Date(sDateAH1); 
	var eDateAH2 = new Date(eDateAH1); 
		
	if (sDateAH1 == "")
	{
		alert ("Please Select a Start Date");
		return false;	
	}
	else
	{
		if (!isDate(sDateAH1)){return false;};
	}
	if (eDateAH1 == "")
	{
		alert ("Please Enter Thru Date");
		return false;
	}
	else
	{
		if (!isDate(eDateAH1)){return false;};
	}
	
	if(eDateAH2 < sDateAH2)
	{   
	    alert("Proxy period date should be less than thru date.");
		return false;
	}
	
}

//Function to check the fields in Advanced Search
function SearchCheck()
{	
	var accNum = document.getElementById("SearchByAccountNumber").value;
	var taxID = document.getElementById("SearchByTaxId").value;
	var accHol = document.getElementById("SearchByAcHolder").value;
	var cerNum = document.getElementById("SearchByCerNo").value;
	var crossRef = document.getElementById("SearchByCrossRef").options[document.getElementById("SearchByCrossRef").selectedIndex].value;
	
	
		if(accNum == "" && taxID == "" && accHol == "" && cerNum == "" && crossRef == "- Select Value -")
		{	
			alert ("Please select atleast one criteria to search");
			document.advSearch.SearchByAccountNumber.focus();
			return false;
		}
		else
		{
			if(Trim(taxID) != "")
			{	
				if(!SSNValidation(taxID))
				{
					alert("Invalid TaxID. Must be 9 digits in the form NNN-NN-NNNN");
					document.advSearch.SearchByTaxId.focus();
					return false;
				}
			}
			window.advSearch.submit();
		}
		
	
}

//Function to set time for all Employee Plans Pages
function ep_settime() 
{
	var curtime = new Date(); 
	var curhour = curtime.getHours();
	var curmin = curtime.getMinutes();
	var cursec = curtime.getSeconds();
	var time = "";
	var calendarDate = getCalendarDate();
	
	time = calendarDate + " " + (curhour) + ":" +
			(curmin < 10 ? "0" : "") + curmin + " EST"; 
	document.frmDate.clock.value = time ;
}

function ep_clearContact()
{
	document.contactForm.txtSHName.value = "";
	document.contactForm.txtEmail.value = "";
	document.contactForm.txtCompanyInquiry.value ="";
	document.contactForm.txtAreaComments.value = "";
}
