//************************************************************************
// COMMON VALIDATION FUNCTIONS
//************************************************************************
function isLetter (c){
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

function isDigit (c){
//alert("c in isDigit is " + c)
	if(c == "-"){
		//alert("c is a dash")
		return (true);
	}else{
		if((c >= "0") && (c <= "9")){
			return (true);
		}else{
			return (false);
		}
	}
}

//************************************************************************
function CheckEmail(form){
var EmailOk  = true;
var Temp     = form.Email;
var AtSym    = Temp.value.indexOf('@');
var Period   = Temp.value.lastIndexOf('.');
var Space    = Temp.value.indexOf(' ');
var Length   = Temp.value.length - 1;   // Array is from 0 to length-1
	if ((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1)){                
	// '@' cannot be in first position
	                 // Must be atleast one valid char btwn '@' and '.'
	                 // Must be atleast one valid char after '.'
	                        // No empty spaces permitted
      alert('Please enter a valid e-mail address!');
      Temp.focus();
      return (false);
   }
 return (true);
}
//*********************************************************
//Verifies that lookup information was entered correctly
//*********************************************************
function LookAcct(form){

	if (document.form1.Account.value.length==0){
		alert("Please enter your Account Number");
		document.form1.Account.focus();
		return (false);
	}else{
		
		for(i=0;i<document.form1.Account.value.length;i++){
			if(!isDigit(document.form1.Account.value.charAt(i))){
				alert("Your Account Number must be numeric only. Refer to the 'How do I find my account number link' for instructions. If you cannot locate your account number please go directly to the form to enter all your information.");
				document.form1.Account.focus();
				return (false);
			}
		}
		
	
	
	}

	if (document.form1.Search_3.value.length==0){
		alert("Please enter the first 3 letters of your last name.");
		document.form1.Search_3.focus();
		return (false);
	}


return (true);
}

function LookAcctv2(frm2){
//alert("lookact is running")
	if (frm2.Account.value.length==0){
		alert("Please enter your Account Number");
		frm2.Account.focus();
		frm2.Account.select();
		return (false);
	}else{

		for(i=0;i< frm2.Account.value.length;i++){
			if(!isDigit(frm2.Account.value.charAt(i))){
				alert("Your Account Number must be numeric only. Refer to the label to your right for instructions. If you cannot locate your account number please go directly to the form to enter all your information.");
			frm2.Account.focus();
			frm2.Account.select();
				return (false);
			}
		}
	}
	if (frm2.LN3.value.length==0){
		alert("Please enter your last name.");
		frm2.LN3.focus();
		return (false);
	}
}

//**********************************************************
// Functions used in regular expressions
//**********************************************************
function getIndex(what) {
    for (var i=0;i<document.forms[0].elements.length;i++)
        if (what == document.forms[0].elements[i])
            return i;
    return -1;
}


function regCheck(thefield,type){
//alert(str)
//	1 - alpha only
//	2 - numeric only
//	3 - alphanumeric only
//	4 - Email
//	5 - alpha-numeric with spaces
//	6 - alpha with spaces
//	7 - zip, zip plus 4
//alert(thefield.form.name)
var str = thefield.value
var regexp
var str_msg
if(type == 1){
	regexp = /^[A-Za-z]*$/;
	str_msg = "Only alpha characters are allowed in this field. A thru Z or a thru z!";
}

if(type == 2){
	regexp = /^\d*$/;
	str_msg = "Only numeric characters are allowed in this field. 0 thru 9!";
}

if(type == 3){
	regexp = /^[a-zA-Z0-9]*$/;
	str_msg = "Only alpha and numeric characters are allowed in this field. A thru Z or a thru z or 0 thru 9!";
}

if(type == 4){	
	regexp = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	str_msg = "Only characters used in email addresses are allowed in this field!";
}
if(type == 5){	
	regexp = /^[a-zA-Z0-9\s.\-]+$/;
	str_msg = "Only alpha numeric characters and spaces are allowed in this field!";
}
if(type == 6){	
	regexp = /^[a-zA-Z\s.\-]+$/;
	str_msg = "Only alpha characters and spaces and dashes  are allowed in this field!";
}
if(type == 7){	
	regexp = /^\d{5}(-\d{4})?$/;
	str_msg = "Only valid zip and zip plus 4 formats are allowed in this field!";
}
if(type == 8){	
	regexp = /^[a-zA-Z][0-9][a-zA-Z]\s?[0-9][a-zA-Z][0-9]$/;
	str_msg = "Only valid Canadian Postal Code formats are allowed in this field!";
}
if(type == 9){	
	//regexp = /[A-Z]{1,2}\d(?:\d|[A-Z])? \d[A-Z]{2}/;
	regexp = /^(\d{5}((|-)-\d{4})?)|([A-Za-z]\d[A-Za-z][\s\.\-]?(|-)\d[A-Za-z]\d)|[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}$/;
	str_msg = "This may be an invalid Foreign Postal Code please re-check!";
}


if(!str.length == 0){
//*******************************
	var a = regexp.test(str)
	//alert(a)
	if(a){
	}else{
		alert(str_msg)
		//alert(getIndex(thefield))
		var theindex = getIndex(thefield);
		if(!type == 9){
			document.forms[0].elements[theindex].value = ""
		}
//		document.forms[0].elements[theindex].style.backgroundColor = 'Red';
		
		//document.forms[0].elements[theindex].focus()
		thefield.focus();
	}
				
}
  return a
}
//*******************************************************************************
function closeWin(){
window.close()
}
//****************************


function txtIsFilled(ctl, name){
	if (ctl.value.length==0){
      alert("Please enter your " + name + ".");
      ctl.focus();
      return (false);
   }else{
	return (true);
   }
}
//***********************************************
function ddlIsFilled(ctl, name){
	if (ctl.options[ctl.selectedIndex].value.length==0){
      alert("Please enter your " + name + ".");
      ctl.focus();
      return (false);
   }else{
	return (true);
   }
}

function txtValidState(ctl){
	state_array = new Array("AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY","AE","AA","AP");
	var s = 0;
	
	if(!ctl.value.length == 0){
		for (i=0; i < state_array.length; i++){
			var str_state = ctl.value;
			str_state = str_state.toUpperCase();
			if(state_array[i] == str_state){
				s = 1;
				break;
			}
		}
	}
		
		
	if(s==0){
		alert("Please enter a VALID 2 character state abbreviation.");
		ctl.focus();
		return (false);
	}else{
		return (true);
	}
}

//****************************************************
function regEXCheck(ctl,isReq,type){

	if(!ctl.value.length == 0){
		if (!regCheck(ctl,type)){ 
			return (false);
		}else{
			return (true);
		}
	}else{
		if(isReq){
			alert("Please enter a VALID " + ctl.name + ".");
			ctl.focus();
			return (false);
		}else{
 			return (true);
 			
		}
	}
}

//*****************************************************
function chkIsFilled(ctl,msg){
	var x = 0;
 	for (var j=0;j < ctl.length; j++){
 		if (ctl[j].checked == true){
 			x= x + 1;
 		}
 	}
 	if(x == 0){
 		alert(msg);
 		ctl[0].focus();
 		return (false);
 	}else{
 		return (true);
 	}
}
//**********************************************
function chkIsFilledNoResponse(ctl){
	var x = 0;
 	for (var j=0;j < ctl.length; j++){
 		if (ctl[j].checked == true){
 			x= x + 1;
 		}
 	}
 	if(x == 0){
  		return (false);
 	}else{
 		return (true);
 	}
}


function chkOther(ctl,valOther,ctlOther,msg){
//alert(ctl)
//alert(valOther)
//alert(ctlOther)
//"You have selected Other - Please specify"
	var ctlVal = ctlOther
	var x = 0;
 	for (var j=0;j < ctl.length; j++){
 		if (ctl[j].checked == true){
 			//
 			if ((ctl[j].value == valOther) && (ctlOther.value.length == 0)){
 				x= x + 1;
 			}
 		}
 	}
 
 	if(x == 0){
 		return (true);
 	}else{
 		alert(msg)
 		//alert("test")
 		ctlOther.focus();
 		return (false)
 		
 	}
 	
}
function txtIsPhone(ctl,isReq,msg){
	
	if (!ctl.value.length==0){
 		for(i=0;i<ctl.value.length;i++){
 			if(!isDigit(ctl.value.charAt(i))){
 			   alert(msg);
 			   ctl.focus();
 			   return (false);
 			}else{
 				return (true);
 			}
 		}
 	}else{
 		if(isReq){ // if Field is required
 			alert(msg);
 			ctl.focus();
 			return (false);
 		}else{
 				return (true);
 			}
 	}

}