// multi email address check
function multiEmailCheck(fullstring){
	noError = true;
	pos = 0;
	fullstring = trimString(fullstring);
	endindex = fullstring.length - 1;
	lastchar = fullstring.substring(endindex);
	if (lastchar != ",") {
		fullstring = fullstring + ",";
	}
	endindex = fullstring.length - 1;
	cindex= fullstring.indexOf(",");
	// loop
	while (cindex <= endindex){
		if (fullstring == ","){ 
			break;
		}
		teststring = "";
		if (-1 == cindex) {
			break;
		} else {
			teststring = fullstring.substring(pos, cindex);
			teststring = trimString(teststring);
			if (!emailCheck (teststring)) {
				noError = false;
				break;
			}
			fullstring = fullstring.substring (cindex+1, endindex+1);
			fullstring = trimString(fullstring);
			endindex = fullstring.length - 1;
			pos = 0;
			cindex = fullstring.indexOf(",");
		}
	}
	//end loop
	
	return noError;
} // END multi email address check

// Multipolicy check
function validateMultiplepolicies(fullstring){
	//alert("new");
	errors = 0;
	status = false;
	pos = 0;
	//fullstring = df.text1.value;
	fullstring = trimString(fullstring);
	endindex = fullstring.length - 1;
	lastchar = fullstring.substring(endindex);
	if (lastchar != ",") {
		fullstring = fullstring + ",";
	}
	endindex = fullstring.length - 1;
	cindex= fullstring.indexOf(",");
	
	// loop
	//alert("here");
	while (cindex <= endindex){
		//alert("here" + fullstring + " cindex " + cindex + " end index "+ endindex);
		if (fullstring == ","){ 
			//alert("break 1");
			break;
		}
		teststring = "";
		if (-1 == cindex) {
			//alert("break 2");
			break;
		} else {
			teststring = fullstring.substring(pos, cindex);
			teststring = trimString(teststring);
			//alert ("test string '" +teststring +"'");
			
			if (validPol(teststring)!=""){
				//alert(" error found ");
				errors = errors + 1;
			}
			
			
			fullstring = fullstring.substring (cindex+1, endindex+1);
			fullstring = trimString(fullstring);
			endindex = fullstring.length - 1;
			pos = 0;
			cindex = fullstring.indexOf(",");
				
		}
	}
	//end loop
	
	return errors;
}
// END multipolicy check


// Valid one policy number
function validPol(polNumber) { 

      // 7 or 8 digits
	var err=""
	
	a=trimString(polNumber);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}
	var strOK="0123456789";
	
	if ( validEachLetter(a, strOK)>0 || a.length < 7 || a.length > 8 ) { 
		return "Policy Number should be 7 or 8 digits\n\ni.e. Policy Number \"1234567\"\ni.e. Policy Number \"0123456\" ";
	}

	return err;
	
} //validPol


//checkFormat

function checkFormat(t){

 if(t.indexOf('<')>-1 || t.indexOf('>')>-1 || t.indexOf('%')>-1 ){
    alert("Please do not include any of the following symbols in any of the data you enter \n\n '<', '>' or '%' .");
    return false;
 }else {
   return true;
 }
} 
//END checkFormat 




function trimString (str){

	if (str=="") return str;

	// from the beginning
	while (str.charAt(0) == ' ') 
		str = str.substring(1, str.length);

	// from the back
	while (str.charAt(str.length - 1) == ' ') 
		str = str.substring(0, str.length - 1);
	return str;
}  


// START PHONEEDIT
function phoneEdit(areacode, prefix, number, ext ,message){
	passtest = true;

	vareacode=trimString(areacode.value);
	vprefix=trimString(prefix.value);
	vnumber=trimString(number.value);
	vext=trimString(ext.value);


	var strOK="0123456789";
	
	if ( validEachLetter(vareacode, strOK)>0 ||
		validEachLetter(vprefix, strOK)>0 ||
		validEachLetter(vnumber, strOK)>0 ||
		validEachLetter(vext, strOK)>0 && vext!= "" ){
		
		alert(" "+message);
		areacode.focus();
		areacode.select();
		passtest = false;
		return passtest;
	}

	phonenumlength = vareacode.length + 
	vprefix.length + vnumber.length;

	if (phonenumlength != 10) {
		alert(" "+message);
		areacode.focus();
		areacode.select();
		passtest = false;
		return passtest;
	}

	return passtest;
}
// -- END PHONE EDIT

function validZIP(zipValue, country) { 
	var err=0;
    var flag="";
	var zipStrOK="0123456789-";
	
	a=trimString(zipValue);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}

    if (validEachLetter(a, zipStrOK)>0) { return 1; } 
	
    if ( (a.length != 5) && (a.length!=10) && (a.length!=6) ) {
		return 1;
	}

	
	
	if (a.length==5) {
		flag="1";
		if (isNaN(a)>0) err=1;
	}
	else if (a.length==10) {
		flag="2";
		if (a.substring(5,6)=='-') {
			b = a.substring(0,5);
			c = a.substring(6,11);
			//alert(b + " " + c);
			if ( (isNaN(b)>0) || (isNaN(c)>0) ) err=1;
		}
		else err=1;
	}
	else if (a.length==6) {
		//alert(a);
		flag="3";
		b = a.substring(1,2);
		c = a.substring(3,4);
		d = a.substring(5,6);
		//alert(b+ " " + c + " " + d);
		if (isNaN(b)>0 || isNaN(c)>0 || isNaN(d)>0) err=1;
		b = a.substring(0,1);
		if ( b.indexOf(zipStrOK) == 0) err=2;  
		c = a.substring(2,3);
		if ( c.indexOf(zipStrOK) == 0) err=3;  
		d = a.substring(4,5);
		//alert(b+ " " + c + " " + d);
		if ( d.indexOf(zipStrOK) == 0) err=4;  
	}
	//alert(flag + " " + err);
	
	if (country=="0000") {
		if (flag=="") return 1;
		if (err>=1) return 1;
	} 
	else if (country=="0001") {
		if ( (flag!="1") && (flag!="2") ) return 1;
		if (err>=1) return 1;
	}
	else if (country=="0002") {
		if (flag!="3") return 1;
		if (err>=1) return 1;
	}
	else {
		return 1;
	}
  	return err;
	
} //validZIP



<!-- e-mail check -->
/*

This is an e-mail address validation function. It allows the usual 
user@domain syntax, but in addition allows user@[ip] format as well as
"User with Spaces"@domain or [ip], all of which are legal syntax,
according to W3C. It also checks that the user hasn't done anything silly
like having multiple @'s or continuous .'s in the address (e.g.
jim@b@c.com and jim@c..b.co.uk).   */
// -->

<!-- Begin
function emailCheck (emailStr) {

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=0;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */	

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user
		
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */

		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("The username portion of the email address contains invalid characters.");
			return false;
	   	}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name portion of the email address contains invalid characters.");
			return false;
	   	}
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {

		// user is not valid

		alert("The username portion of the email address doesn't seem to be valid.");
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

	// this is an IP address

		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address portion of the email address is invalid!");
				return false;
   			}
		}
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name portion of the email address does not seem to be valid.");
			return false;
		}
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	   domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The email address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
		alert("This email address is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

//  End -->



/* BLANK FIELD CHECK*/
// START BLANK CHECK
function blankEdit(field, message){
	var test = true;
	if (field.value.length < 1){ 
		alert(" "+message);
		field.focus();
		field.focus();
		test = false;
	}
	return test;
}
// END BLANK CHECK

// validate Web User Name for Registration
function validUserName(userName) {
	
	var err="";
	a=trimString(userName);
	
	while (a.indexOf(" ")>0) {
		a = a.replace(" ", "");
	}

	a=a.toLowerCase();
	
	if (a.indexOf("'")>-1)
		return "User Name can't contain '";	if (a.indexOf('"')>-1)		return "User Name can't contain " + '"' + ".";

	if (a.length==1) {
		return "User Name can't be a single letter or digit.";
	}
	
	if ( (a=="manu") || (a=="manulife") || (a=="manlife") 
		|| (a=="manufacturers") || (a=="homeoffice") || (a=="headoffice") )
	{
		return "User Name can't be " + userName + ".";
	}
	
	return err;
}



// Validate one RDRAM code
function validRDRAM(rdram) { 

    // 3 digit
	var err=0;
	
	a=trimString(rdram);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}
	
	if (a.length != 3) {
		return 1;
	}
	
	var strOK="0123456789";
	if ( validEachLetter(a, strOK)>0 ) {
     		return 1;
     	}
  	return err;
	
} //validRDRAM

// validate a list of RDRAM code sepersated by ,
function validRDRAMList(rdram) { 

      // 3 digit 123,345
	var err="";
	
	var strOK="0123456789";

	a=trimString(rdram);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}	// remove leading and tailing ;	// from the beginning	while (a.charAt(0) == ',') 		a = a.substring(1, a.length);	// from the back	while (a.charAt(a.length - 1) == ',') 		a = a.substring(0, a.length - 1);
	
	// test through each entry 	
	c = a;
	while (c.indexOf(",")>0){
		b = c.substring(0, c.indexOf(","));
		//alert(b);
		if (b.length!=3)
			return "Each entry should be a 3 digit number."
		if (validEachLetter(b, strOK)>0)
			return "Each entry should be a 3 digit number.";
		c = c.substring(c.indexOf(",") + 1, c.length);
		//alert(c);	
	}
	// test last entry
	if (c.length!=3)
		return "Each entry should be a 3 digit number."
	if (validEachLetter(c, strOK)>0)
		return "Each entry should be a 3 digit number.";
	
  	return "OK" + a;
	
} //validRDRAM

// validate SSN
function validSNN(ssnValue) { 
    // ###-##-####
	var err=0;

	var strOK="0123456789";
	
	a=trimString(ssnValue);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}
	
	// 11
	if (a.length != 11) {
		return 1;
	}
	
	//SSN US
	if (a.substring(3,4)=='-' && a.substring(6,7)=='-') {
		b = a.substring(0,3);
		c = a.substring(4,6);
		d = a.substring(7,10);
		//alert(a+" "+b+" "+c+" "+d);
    	if (validEachLetter(b, strOK)>0 || validEachLetter(c, strOK)>0 || validEachLetter(d, strOK)>0)
			return 1;
  	} else return 1;
  		
  	return err;
	
} //validSNN

// validate TaxId
function validTIN(tinValue) { 
    // ##-#######
	var err=0;

	var strOK="0123456789";
	
	a=trimString(tinValue);
	while (a.indexOf(" ")>0){
		a = a.replace(" ", "");
	}
	
	// 10
	if (a.length != 10) {
		return 1;
	}
	
	// TIN
	if (a.substring(2,3) == '-') {
		b = a.substring(0,2);
		c = a.substring(3,10);
		//alert(b + " " + c);
		if (validEachLetter(b, strOK)>0 || validEachLetter(c, strOK)>0) return 1;
	} 
	else 
		return 1;
	
  	return err;
	
} //validTIN

// check leap year
function checkLeapYear(year) { 
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}

// validate date Month 23, 2001
function validDate(dateValue){ 
	
	var months = new Array();
	
	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";

	// Month 00, 2001
	var err=0;
	a=dateValue;
	while (a.indexOf(" ")>0) {
		a = a.replace(" ", "");
	}
	
	// Month00,2001
	if (a.length > 16) {
		return "Exceed Maximal Length!";
	}
	
	// month
	yr=a.substring(a.length - 4, a.length);
	day=a.substring(a.length - 7, a.length - 5);
	mth=a.substring(0, a.length - 7); 
	
	// yr and day are numbers
	if (isNaN(yr)>0 || isNaN(day)>0) {
		return "Year and Day are not numbers.";
	}

	d = parseFloat(day);
	y = parseInt(yr);
	m = mth.toLowerCase();
	
	// day between 1 and 31
	if (d<1 || d>31)
		return "Day should be between 1 and 31.";
	
	// mth in the list months
	found = false;
	for(var i=0;i<months.length; i++) {
		if (months[i].toLowerCase()==m) {
			found=true;
			break;
		}
	}
	if (found==false)
		return "Wrong month format.";	
	else m = months[i];	

    	// months with 30 days 
    	if (m=="April" || m=="June" || m=="September" || m=="November"){ 
    		if (d==31) return "Day should be between 1 and 30"; 
    	}

      // february, leap year 
    	if (m=="February") { 
    		// feb 
       	var g=parseInt(y/4);
       	if (isNaN(g)) { 
       		return "Wrong year!";
       	} 
       	if (d>29) return "Day should be between 1 and 29";
       	if (d==29 && (!checkLeapYear(y))) return "Only 28 days for a non-leap year!";
	} 

	return "OK"+m+" "+day+", " + yr;

} //validDate

// validate date MM/DD/YYYY
function validDateWithShortFormat(dateValue){ 

	// MM / DD/YYYY
	a=dateValue;
	while (a.indexOf(" ")>0) {
		a = a.replace(" ", "");
	}
	if (a == "") {
		return "Please enter the date";	
	}
	
	// MM/DD/YYYY
	if (a.length != 10) {
		return "Incorrect date format!";
	}
	
	// month
	start = a.indexOf("/");
	end = a.lastIndexOf("/");
	yr=a.substring(end + 1, a.length);
	day=a.substring(start + 1, end);
	mth=a.substring(0, start); 
	
	// yr and day are numbers
	if (isNaN(yr)>0 || isNaN(mth) > 0 || isNaN(day)>0) {
		return "Year, month and Day are not numbers.";
	}

	d = parseFloat(day);
	m = parseFloat(mth);
	y = parseInt(yr);
	
	// day between 1 and 31
	if (d<1 || d>31)
		return "Day should be between 1 and 31.";
		
	// month between 1 and 12
	if (m < 1 || m > 12)
		return "Month should be between 1 and 12";

    // months with 30 days 
    if (m==04 || m==06 || m==09 || m==11){ 
    		if (d==31) return "Day should be between 1 and 30"; 
    }

    // february, leap year 
	if (m==2) { 
    		// feb 
       	var g=parseInt(y/4);
       	if (isNaN(g)) { 
       		return "Wrong year!";
       	} 
       	if (d>29) return "Day should be between 1 and 29";
       	if (d==29 && (!checkLeapYear(y))) return "Only 28 days for a non-leap year!";
	} 

	return "OK";

} //validDateWithShortFormat
	
function validField(testStr, OKStr, fieldStr, fieldName, dF)	{
	var testFlag = 0;
	if ( testStr == "") {
		testFlag += 1;
	} else {
		testFlag += validEachLetter(testStr, OKStr);
	}
	//alert("validField" + testFlag);
	if (testFlag !=0) {
		alert(" Please enter " + fieldStr+ ".");
    	eval("dF"+"."+fieldName+".value=''");
		eval("dF"+"."+fieldName+".focus()");
	}
		
	return testFlag;
}
	
function validEachLetter(testStr, OKStr) {
	var allValid = 0;
	for( i = 0; i < testStr.length; i++) {
		ch = testStr.charAt(i);
		for(j = 0; j < OKStr.length; j++)
			if ( ch == OKStr.charAt(j)) break;
			if( j == OKStr.length) 
			{
				allValid = 1;
				break;
			}
	}
	return allValid;
}

function setRole(){
	top.uRoles='anonymous;';
}


// validate web password
function checkWebPassword(pwd){
	var list = "abcdefghijklmnopqrstuvwxyz";
	pwd = pwd.toLowerCase();
	var cal = 0;

	if(pwd.length < 6) {
		alert("The Password for Internet Access must be at least six characters long.");
		return false;
	}

	while(pwd.length>0) {
		if(list.indexOf(pwd.substr(0,1))>=0) cal= cal+1;		
		pwd = pwd.substring(1,pwd.length);
	}
		
	if(cal < 2) {
		alert("The Password for Internet Access must be at least six characters long and two of which must be alpha.");
		return false;
	}
	else return true;
}

// validate ivr user name
function validIVRUserName(ivrUserName) { 

      // must be numeric and greater than 6 digits and less than 80 digits
	var err=""
	
	a=trimString(ivrUserName);
	var strOK="0123456789";
	
	if ( validEachLetter(a, strOK)>0 || a.length < 6 || a.length > 80) { 
		return "The User Name for Telephone Based Self Service Access must be numeric and at least 6 digits.";
	}

	return err;
	
}

// validate ivr password
function validIVRPassword(ivrPassword) { 

      // must be numeric and greater than 6 digits and less than 50 digits
	var err=""
	
	a=trimString(ivrPassword);
	var strOK="0123456789";
	
	if ( validEachLetter(a, strOK)>0 || a.length < 6 || a.length > 50) { 
		return "The Password for Telephone Based Self Service Access must be numeric and at least 6 digits.";
	}

	return err;
	
}

// Open a file in a new window
function openFile(windowName, siteId, channelId, title, wSize, hSize) {
	if (wSize == null || wSize == "" || wSize == "0")
		wSize = "500";
	if (hSize == null || hSize == "" || hSize == "0")
		hSize = "500";
	var options = "scrollbars=yes,width=" + wSize + ",height=" +
		hSize + ",resizable=yes,";
	var bars = "directories=no,location=no,menubar=no,status=no";
	var features = options + bars;
	var target = "/USIPortal/Common/tools/info/0,0," + siteId + "_" + channelId +
			"__" + title + ",00.html";
	
	var newWin = open(target, windowName, features);
}
