
/* Reverse Mortgage Zip locator Starts */

function onSubmit(form) {
 var formName= form.name;
	if ((form.zip.value == 'Enter Zip Code') || (form.zip.value == '')) {
		alert("Please enter a valid Zip Code.");
		form.zip.focus();
		form.zip.select();
		return false;
	}
	// zip should be a number and 5 digits
	if ((isNaN(form.zip.value)) || form.zip.value.length < 5) {
		alert("Please enter a valid Zip Code.");
		form.zip.focus();
		form.zip.select();
		return false;
	}
	form.lookupText.value=form.zip.value;
	submitZipForm(formName);
	resetZip(form);
}
function resetZip(form) {
	form.zip.value="Enter Zip Code";
	form.zip.style.color="#999";
	form.zip.style.marginTop="6px";
	form.zip.style.marginBottom = "4px";
	form.zip.style.border="#ccc 1px solid";
}

/* Reverse Mortgage Zip locator ends */

/* Secure Email Webform Starts */


function validateEmailUs(form) {
	// required check
	if (form.inquirySelect[form.inquirySelect.selectedIndex].value == "0") {
		alert("The following fields are required: Inquiry Type, Question/Comment, First name, Last name and email.");
		form.inquirySelect.focus();		
		return false;
	}
	if ((form.UserComments.value == "") || (form.UserComments.value == "Comments")) {
		alert("The following fields are required: Inquiry Type, Question/Comment, First name, Last name and email.");
		form.UserComments.focus();
		form.UserComments.select();
		return false;
	}
	if (form.first_name_box.value == "") {
		alert("The following fields are required: Inquiry Type, Question/Comment, First name, Last name and email.");
		form.first_name_box.focus();
		form.first_name_box.select();
		return false;
	}
	if (form.last_name_box.value == "") {
		alert("The following fields are required: Inquiry Type, Question/Comment, First name, Last name and email.");
		form.last_name_box.focus();
		form.last_name_box.select();
		return false;
	}	
	// zip should be a number and 5 digits
	if (form.zip_box.value != "") {
		if (!isNum(form.zip_box.value) || form.zip_box.value.length < 5) {
			alert("Please enter a valid Zip Code.");
			form.zip_box.focus();
			form.zip_box.select();
			return false;
		}
	}
	// check for phone number
	if (form.areaCode_box.value != ""
		|| form.lineNumber_box.value != ""
		|| form.prefix_box.value != "")
	{
		// if phone number present, check for validity
		if (!isNum(form.areaCode_box.value)
				|| !isNum(form.lineNumber_box.value)
				|| !isNum(form.prefix_box.value)) 
		{
			alert("Please enter a valid Phone Number.");
			form.areaCode_box.focus();
			form.areaCode_box.select();
			return false;
		}
		if (form.areaCode_box.value.length < 3 
				|| form.prefix_box.value.length < 3
				|| form.lineNumber_box.value.length < 4) 
		{
			alert("Please enter Phone Number in xxx xxx xxxx format.");
			form.areaCode_box.focus();
			form.areaCode_box.select();
			return false;
		}
	}
	if (form.email_box.value == "") {
		alert("The following fields are required: Inquiry Type, Question/Comment, First name, Last name and email.");
		form.email_box.focus();
		form.email_box.select();
		return false;
	}
	if ((form.email_box.value != "") && ((form.email_box.value.indexOf("@",0)<0) || (form.email_box.value.indexOf(".",0)<0))){
		alert("Please enter a valid e-mail address.");
		form.email_box.focus();
		form.email_box.select();
		return false;
	}
	// If Account Number entered.... then atleast two of the three other information should be available
	if (form.accountNo_box.value != "") {
		//Check for Date of Birth
				
		if ((form.dobmm_box.value != "MM") && (form.dobdd_box.value == "DD")) {
		 
			alert("Please enter the Date of your birth.");
			form.dobdd_box.focus();
			form.dobdd_box.select();
			return false;		
		}
		
		else if ((form.dobmm_box.value == "MM") && (form.dobdd_box.value != "DD")) {
		
			alert("Please enter the Month of your birth.");
			form.dobmm_box.focus();
			form.dobmm_box.select();
			return false;	
		}
		
		else if ((form.dobmm_box.value != "MM") && (form.dobdd_box.value != "DD")) { // Check if atleast one other information is available
			
			var mm = form.dobmm_box.value;
			var dd = form.dobdd_box.value;	
			if ((mm == 0) || (dd == 0)) {		
			
				alert("Please enter a valid Date of Birth (MM-DD)");
				return false;
			}
			if (mm > 12) {
			
				alert("Please enter a valid Date of Birth (MM-DD)");
				return false;
			}
			if (((mm == 2) && (dd > 30)) || ((mm%2 > 0) && (dd > 30)) || ((mm%2 == 0) && (dd > 31))) {
			
				alert("Please enter a valid Date of Birth (MM-DD)");
				return false;
			}
			
			if ((form.fourDigits_box.value == "") && (form.onlineUserID_box.value == "")) {
			
				alert("Please enter at least 2 of the following 3 fields: Date of Birth, last 4 digits of Social Security number  and/or Online User ID");
				form.fourDigits_box.focus();
				form.fourDigits_box.select();
				return false;	
			}
			if ((form.fourDigits_box.value.length != 0) && ((form.fourDigits_box.value.length < 4) || (!isNum(form.fourDigits_box.value)))) {
			
				alert("Last 4 Digits of Social Security # are required");
				form.fourDigits_box.focus();
				form.fourDigits_box.select();
				return false;	
				
			}
		}	
		else if ((form.dobmm_box.value == "MM") && (form.dobdd_box.value == "DD")) {
			if ((form.fourDigits_box.value == "") || (form.onlineUserID_box.value == "")) {
			
				alert("Please enter at least 2 of the following 3 fields: Date of Birth, last 4 digits of Social Security number  and/or Online User ID");
				form.fourDigits_box.focus();
				form.fourDigits_box.select();
				return false;	
			}
			if ((form.fourDigits_box.value.length < 4) || (!isNum(form.fourDigits_box.value))) {
			
				alert("Last 4 Digits of Social Security # are required");
				form.fourDigits_box.focus();
				form.fourDigits_box.select();
				return false;	
				
			}
		}		
	}
	
}

var numb = '0123456789';

function isValid(parm, val) {
	if (parm == "") return true;
	for (i = 0; i < parm.length; i++) {
	if (val.indexOf(parm.charAt(i), 0) == -1)
		return false;
	}
	return true;
}
			
function isNum(parm) {
	return isValid(parm,numb);
}
function checkDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Please enter valid date format (mm/dd/yyyy). Year must be between" + minYear + " and current date.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter valid date format (mm/dd/yyyy). Year must be between" + minYear + " and current date.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter valid date format (mm/dd/yyyy). Year must be between" + minYear + " and current date.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter valid date format (mm/dd/yyyy). Year must be between" + minYear + " and current date.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter valid date format (mm/dd/yyyy). Year must be between" + minYear + " and current date.")
		return false
	}
	return true
}
function checkLen(form,x,y) {	
	if (y.length==x.maxLength) {
		var next=x.tabIndex;		
		for (i=0; i < form.length; i++) {
			if (form.elements[i].tabIndex > next) {
				form.elements[i].focus();
				return true;
			}
		}
	}
}
function checkMon(form,x,y) {
	if (y.length != 0) {
		if (y.length < x.maxLength) {
			if (y == 0) {
				alert("Please enter valid Month");
				x.focus();
				return false;
			}
		}
		if (isNum(y) && (y != 00) && (y != 0) && (y < 13)) {
			if (y.length < x.maxLength) {
				x.value = "0" + y;
			}
		} // Validate that month is from 1 - 12 
		else {
			// pop-up message
			alert("Please enter valid Month");
			x.focus();
			return false;
		}		
	}
}
function checkDate(form,x,y,z) {	
	if ((z.length > 0) && (y.length == 0)) {
		alert("Please enter valid Date");
		x.focus();
		return false;
	}
	if (y.length != 0) {
		if (y.length==x.maxLength) {
			if (y == 0) {
				alert("Please enter valid Date");
				x.focus();
				return false;
			}
		}		
		if (isNum(y) && (y != 00) && (y != 0) && (((z == 2) && (y < 30)) || ((z%2 > 0) && (y < 31)) || ((z%2 == 0) && (y < 32)))) {
			if (y.length < x.maxLength) {
				x.value = "0" + y;
			}		
		} // validate that month is from 1 - 31	
		else {
			// pop-up message
			alert("Please enter valid Date");
			x.focus();
			return false;
		}			
	}
}
function checkDOB (form,x,y) {		

	if ((y.length != 0) && (y.length < x.maxLength) && (y != 0)) {
		x.value = "0" + y;
		
		console.debug("x: " + x);
		console.debug("x.maxLength: " + x.maxLength);
		console.debug("y: " + y);
		console.debug("y.length: " + y.length);
	}
	else 
	{
		if(x.value == "" && x.name == "dobmm_box")
		{
			x.value = "MM";
		}	
		else if(x.value == "" && x.name == "dobdd_box")
		{
			x.value = "DD";
		}	
		
		console.debug("x.name: " + x.name);
		console.debug("x.value: " + x.value);
		
	}
	
}

function validate(form) {

   var returnVal= validateEmailUs(form);
   var formName= form.name;
   //alert("returnVal "+ returnVal);
   if (returnVal == false) {
             
          return false;
   } else if(typeof(returnVal) =="undefined") {
   
  
     // save in hidden fields
     	form.inquiryType.value = form.inquirySelect.value;
     	form.comments.value = form.UserComments.value;
     	form.company.value = form.company_box.value;
     	form.firstName.value = form.first_name_box.value;
     	form.lastName.value = form.last_name_box.value;
     	form.street.value = form.street_box.value;
     	form.city.value = form.city_box.value;
     	form.state.value = form.state_box.value;
     	form.zip.value = form.zip_box.value;
     	form.country.value = form.country_box.value;
     	form.areaCode.value = form.areaCode_box.value;
     	form.prefix.value = form.prefix_box.value;
     	form.lineNumber.value = form.lineNumber_box.value;
     	form.email.value = form.email_box.value;
     	form.accountNo.value = form.accountNo_box.value;
     	form.dobmm.value = form.dobmm_box.value;
     	form.dobdd.value = form.dobdd_box.value;
     	form.fourDigits.value = form.fourDigits_box.value;
     	form.onlineUserID.value = form.onlineUserID_box.value;
     	
     	console.debug("Inquiry type" + form.inquiryType.value)
     	console.debug("firstName" + form.firstName.value)
     	
     	
     	// initialize the screen fields
		
     	form.inquirySelect.selectedIndex = 0;
     	form.UserComments.value = "";
     	form.company_box.value = "";
     	form.first_name_box.value = "";
     	form.last_name_box.value = "";
     	form.street_box.value = "";
     	form.city_box.value = "";
     	form.state_box.selectedIndex = 0;
     	form.zip_box.value = "";
     	form.country_box.value = "";
     	form.areaCode_box.value = "";
     	form.prefix_box.value = "";
     	form.lineNumber_box.value = "";
     	form.email_box.value = "";
     	form.accountNo_box.value = "";
     	form.dobmm_box.value = "";
     	form.dobdd_box.value = "";
     	form.fourDigits_box.value = "";
     	form.onlineUserID_box.value = "";
     	
     	console.debug("Inquiry type" + form.inquiryType.value)
	console.debug("firstName" + form.firstName.value)
     	
     	
     	submitContactUsForm(formName,'secureEmail','wfthankyou','wferror','wfexception');
        return false;
       	  
   }
   
 }

function checkDOBOnFocus(form,obj)
{
	n = Trim(obj.value);
	obj_name = obj.name;
	id = obj.id;
		
	if (obj_name == 'dobmm_box')
	{
		if(n == 'MM')
		{
			document.getElementById(id).value = "";
		}
		else
		{
			dobmm_box.focus();
		}
	}
	
	if (obj_name == 'dobdd_box')
	{
		if(n == 'DD')
		{
			document.getElementById(id).value = "";
		}
		else
		{
			dobdd_box.focus();
		}
	}
/*		
	var formName= form.name;
	console.debug("form: " + form);
	console.debug("formNAme: " + formName);
	
	if (obj_name == 'UserComments')
	{
		n_value = document.getElementById(id).innerHTML;
		
		console.debug("n:" + n);
		
		if(document.getElementById(id).innerHTML != "Comments")
		{
			document.formName.UserComments.focus();
		}
		else
		{			
			console.debug("inside....");
			document.getElementById(id).innerHTML = "";
		}
	}
		
*/		
	n_val = textTrim(Trim_new(obj.value));
    if (obj_name == 'UserComments')
	{
	   if(n_val == "Comments")
	   {		
			var comments=textTrim(Trim_new(document.getElementById('UserComments').value));

				if(comments == "Comments" || comments == "")

				{
					document.getElementById('UserComments').value = "";
				}
				
		}		
  	}
	
	
}

function checkOnBlur(obj)
{
		n = Trim(obj.value);
	obj_name = obj.name;
	id = obj.id;
	
		if(n == "" && obj_name == "UserComments")
		{
			document.getElementById(id).value = "Comments";
		}	
		
		console.debug("obj_name: " + obj_name);
		console.debug("n.value: " + n);
		
	
}
function Trim(s)
{
  // Remove leading spaces
  while (s.substring(0,1) == ' ')
  {
    s = s.substring(1,s.length);
  }
  // Remove trailing spaces
  while (s.substring(s.length-1,s.length) == ' ')
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function Trim_new(stringToTrim) {

	  stringToTrim = stringToTrim.replace( /^\s+/g, "" );// strip leading

	  return stringToTrim.replace( /\s+$/g, "" );// strip trailing	

}

function textTrim(s) 
{

		s = s.replace(/[\n\r\t\s]+$/, "");

		s = s.replace(/^[\n\r\t\s]+/, "");

		return s;

}


 
/* Secure Email Webform ends */


/* Account Sign in Starts */

// JavaScript Document
function accountlist()
{
 if(document.getElementById("contentlist").style.display == "none")
 {
 	document.getElementById("contentlist").style.display ="block";
 }
 else
 {
 	document.getElementById("contentlist").style.display ="none";
 }
}
function changedisplay(disptxt)
{
	document.getElementById("display").innerHTML =disptxt;
	document.getElementById("contentlist").style.display ="none";
}

function setSelValue(selValue)
{
	document.loginform.accountType.value = selValue;
}

/* Sign In Webform */

 function validateSignIn() {
 // keep this method content synchronized with index.jsp
   var selectBox = document.loginform.accountType;
   userInput = selectBox.options[selectBox.selectedIndex].value;
   if ("H" == userInput) {
   
   // home loans is selected
   
      document.loginform.action="https://carenet.fnfismd.com/metlife/";
     
    } else {
    // Bank Account selected
       //document.loginform.action="https://stawebclient.metlifebank.com/MetLifeWebClient/login.do";
       document.loginform.action="https://www.metlifebanksecure.com/MetLifeWebClient/login.do";
     
     }
    return true;
 } 

/* Sign In Webform Ends */

/* Account Sign in Ends */
