var digits = "0123456789";
var digitsdot = "0123456789.";
function isInt(elm) {
	var elmstr = elm.value + "";
	for (var i = 0; i < elmstr.length; i++) {
		if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}
function stringInt(elm) {
	var elmstr = elm;
	for (var i = 0; i < elmstr.length; i++) {
		if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}
function stripNotInList(s,list)
{
	var i;
	var returnString = "";
	for (i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if (list.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function isDate(obj) 
{
	var yyyy = obj.value.substring(6,10);
	var mm = obj.value.substring(0,2);
	var dd = obj.value.substring(3,5);
	if ((yyyy < "1900") || (yyyy > "1999") || !stringInt(yyyy) )
	{
		alert("Invalid year");
		return false;
	}
	if ( (mm > "12") || (mm < "01") || !stringInt(mm) )
	{
		alert("Invalid month");
		return false;
	}
	if ( (dd > "31") || (dd < "01") || !stringInt(dd) )
	{
		alert("Invalid day");
		return false;
	}
	return true;
}

function checkAppr(objval,frm) {
	if (objval == 'Approved - Tier 1' || objval == 'Approved - Tier 2' || objval == 'Approved - Tier 3')
	{
		if (frm.applicant_agreed_on.value == '')
		{
			frm.applicant_agreed_on.value = formatDate(new Date,'MM/dd/yy');
		}
	}
	
}

  function addZero(vNumber){ 
    return ((vNumber < 10) ? "0" : "") + vNumber 
  } 
        
  function formatDate(vDate, vFormat){ 
	/* vFormat
	dd for Date of the month
	MM for month
	yy or yyyy for Year
	hh for hours
	mm for minutes
	ss for seconds
	*/
    var vDay                      = addZero(vDate.getDate()); 
    var vMonth            = addZero(vDate.getMonth()+1); 
    var vYearLong         = addZero(vDate.getFullYear()); 
    var vYearShort        = addZero(vDate.getFullYear().toString().substring(3,4)); 
    var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
    var vHour             = addZero(vDate.getHours()); 
    var vMinute           = addZero(vDate.getMinutes()); 
    var vSecond           = addZero(vDate.getSeconds()); 
    var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
    vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
    return vDateString 
  } 

function CheckForm()
{
	/*
	document.app.CREDIT.value = stripNotInList(document.app.CREDIT.value,digitsdot);
	if (isNaN(parseInt(document.app.CREDIT.value+""))) {
		document.app.CREDIT.value = "";
	} else {
		document.app.CREDIT.value = parseInt(document.app.CREDIT.value+"");
	}
	*/
	/* No need anymore - 3/5/03
	if (document.app.WORKBEF.selectedIndex == 0)
	{
		alert("Please select a procedure type");
		document.app.WORKBEF.focus(); return false;
	}
	*/
	if (document.app.FIRST.value.length < 2)
	{
		alert("Must enter First Name");
		document.app.FIRST.focus(); return false;
	}
	if (document.app.LAST.value.length < 2)
	{
		alert("Must enter Last Name");
		document.app.LAST.focus(); return false;
	}
	if ((document.app.SOC.value.length < 9) || !isInt(document.app.SOC))
	{
		alert("Must enter 9 digit Social Security Number");
		document.app.SOC.focus(); return false;
	}
	if (document.app.tempDOB.value.length != 10)
	{
		alert("Must enter Date of Birth in MM/DD/YYYY format");
		document.app.tempDOB.focus(); return false;
	} else if (isDate(document.app.tempDOB))
				document.app.DOB.value = '00000000';
			else {
				document.app.tempDOB.focus();
				return false;
			}


/* optional 3/3/2003 */
// added back 4/6/2004
//remove Maiden name check 1/13/2007
/*
	if (document.app.MMAID.value.length < 2)
	{
		alert("Must enter Mother's Maiden Name");
		document.app.MMAID.focus(); return false;
	}
*/
	if (document.app.CADRLN.value.length < 2)
	{
		alert("Must enter Billing Address");
		document.app.CADRLN.focus(); return false;
	}
	if (document.app.CITY.value.length < 2)
	{
		alert("Must enter City");
		document.app.CITY.focus(); return false;
	}
	if (document.app.STATE.value.length != 2)
	{
		alert("Must enter State");
		document.app.STATE.focus(); return false;
	}	
	if ( ! ( isInt(document.app.ZIP) &&
		( (document.app.ZIP.value.length == 5) || (document.app.ZIP.value.length == 9) ) ) )
	{
		alert("Must enter 5 or 9 digit ZIP Code");
		document.app.ZIP.focus(); return false;
	}
	
	if ((document.app.LADD.value.length < 1) && (document.app.adr_months.value.length < 1))
	{
		alert("Must enter time at residence");
		document.app.LADD.focus(); return false;
	}	
	
	if (!isInt(document.app.LADD))
	{
		alert("Years at address must be a number");
		document.app.LADD.focus(); return false;
	}
	
	if (!isInt(document.app.adr_months))
	{
		alert("Months at address must be a number");
		document.app.adr_months.focus(); return false;
	}
		
	if (document.app.MORT.value.length < 2 && document.app.MORT.value != '0')
	{
		alert("Must enter rent/mortgage payment");
		document.app.MORT.focus(); return false;
	}	
	
	if (!isMoney(document.app.MORT.value))
	{
		alert("Rent/Mortgage Payment must be a dollar amount e.g. 950.00");
		document.app.MORT.focus(); return false;
	}
	
//	document.app.PHNUM.value = stripNotInList(document.app.PHNUM.value,digits);
	if (document.app.PHNUM.value.length < 10)
	{
		alert("Must enter Phone");
		document.app.PHNUM.focus(); return false;
	}
//	document.app.EPHONE.value = stripNotInList(document.app.EPHONE.value,digits);
	if (document.app.EPHONE.value.length < 10)
	{
		alert("Must enter Business Phone");
		document.app.EPHONE.focus(); return false;
	}	
	
/*
	if (document.app.LADD.value == '1' || document.app.LADD.value == '0' || document.app.LADD.value == '01' || document.app.LADD.value == '00' || document.app.LADD.value == '2' || document.app.LADD.value == '02')
	{// If current stay less than 3 years check for Previous adddress

		if (document.app.FADRLN.value.length < 2)
		{
			alert("Must enter previous address");
			document.app.FADRLN.focus(); return false;
		}	

		if (document.app.PCITY.value.length < 2)
		{
			alert("Must enter previous city");
			document.app.PCITY.focus(); return false;
		}
	
		if (document.app.PSTATE.value.length != 2)
		{
			alert("Must enter previous state");
			document.app.PSTATE.focus(); return false;
		}	

		if (document.app.PZIP.value.length < 5)
		{
			alert("Must enter previous ZIP");
			document.app.PZIP.focus(); return false;
		}	
	}	
	*/
/* No such option - 3/4/03 */
// Option is being asked again

	if (document.app.HOUS_CD.options[0].selected)
	{
		alert("Must select Rent/Buy/Own");
		document.app.HOUS_CD.focus(); return false;
	}



	/*
	if (document.app.rel_name.value.length < 1)
	{
		alert("Must enter personal reference");
		document.app.rel_name.focus(); return false;
	}

	if (document.app.rel_phone.value.length < 10)
	{
		alert("Must enter personal reference phone");
		document.app.rel_phone.focus(); return false;
	}	
	*/
	if ((document.app.ENAME.value.length < 2)  &&  (!document.app.is_retired.checked))
	{
		alert("Must enter Employer");
		document.app.ENAME.focus(); return false;
	}
	if ((document.app.LEMP.value.length < 1) && (document.app.emp_months.value.length < 1) &&  (!document.app.is_retired.checked)) 
	{
		alert("Must enter Time on Job");
		document.app.LEMP.focus(); return false;
	}
	
	if (!isInt(document.app.LEMP)  &&  (!document.app.is_retired.checked)) {
		alert("Time on Job must be a number");
		document.app.LEMP.focus(); return false;	
	}
	
	if (!isInt(document.app.emp_months)  &&  (!document.app.is_retired.checked)) {
		alert("Time on Job must be a number");
		document.app.emp_months.focus(); return false;	 
	}
		
	if (document.app.PR_OCC_CD.value.length < 1  &&  (!document.app.is_retired.checked))
	{
		alert("Must enter Job Title");
		document.app.PR_OCC_CD.focus(); return false;
	}

	
	if (document.app.PAY.value.length < 1 || !isMoney(document.app.PAY.value))
	{
		alert("Salary must be a dollar amount e.g. 1230.50");
		document.app.PAY.focus(); return false;
	}
	if (document.app.other_income.value.length < 1 || !isMoney(document.app.other_income.value))
	{
		alert("Other Income must be a dollar amount e.g. 630.00");
		document.app.other_income.focus(); return false;
	}

//	document.app.HOUSINC.value = eval(eval(document.app.PAY.value) + 0.00 + eval(document.app.other_income.value));
	
/*	
	if ((document.app.HOUSINC.value.length < 1) || !isMoney(document.app.HOUSINC))
	{
		alert("Total Income must be a whole dollar amount");
		document.app.HOUSINC.focus(); return false;
	}
*/
	/*
	if ((document.app.LEMP.value == '0' || document.app.LEMP.value == '1' || document.app.LEMP.value == '01' || document.app.LEMP.value == '00')  &&  (!document.app.is_retired.checked)) {
		if (document.app.pre_employer.value.length < 1) {
			alert("Must enter Previous Employer");
			document.app.pre_employer.focus(); return false;
		}
		
		if (document.app.pre_job_title.value.length < 1) {
			alert("Must enter Previous Job Title");
			document.app.pre_job_title.focus(); return false;			
		}
		
		if (document.app.pre_emp_years.value.length == '') {
			alert("Must enter Years on Previous Job");
			document.app.pre_emp_years.focus(); return false;			
		
		}
	}
	*/
	if (document.app.co_first.value + document.app.co_last.value != '') { // Some co-applicant information is entered

		if (document.app.co_ssn.value.length != 9) {
			alert('Must enter 9-digit SSN for the co-applicant');
			document.app.co_ssn.focus(); return false;
		}
		
		if (document.app.co_dob.value.length < 10) {
			alert('Must enter date of birth in MM/DD/YYYY format');
			document.app.co_dob.focus(); return false;
		}

		if (!isDate(document.app.co_dob)) {
			document.app.co_dob.focus(); return false;
		}
		

		if (document.app.co_address.value == ''){
			alert('Must enter co-applicant\'s address');			
			document.app.co_address.focus(); return false;
		}
		
		if (document.app.co_city.value == ''){
			alert('Must enter co-applicant\'s city');			
			document.app.co_city.focus(); return false;
		}		
		
		if (document.app.co_state.value.length != 2){
			alert('Must enter co-applicant\'s state');			
			document.app.co_state.focus(); return false;
		}				
		
		if (document.app.co_zip.value.length < 5){
			alert('Must enter co-applicant\'s ZIP');			
			document.app.co_zip.focus(); return false;
		}
		
		if (document.app.co_years.value == '' && document.app.co_months.value == ''){
			alert('Must enter co-applicant\'s time at residence');			
			document.app.co_years.focus(); return false;
		}

		if (!isInt(document.app.co_years)) {
			alert('Years at residence must be a number');			
			document.app.co_years.focus(); return false;		
		}
		
		if (!isInt(document.app.co_months)) {
			alert('Months at residence must be a number');			
			document.app.co_months.focus(); return false;		
		}	


		if (document.app.co_phone.value.length < 10){
			alert('Must enter co-applicant\'s home phone');
			document.app.co_phone.focus(); return false;
		}
		
		if (document.app.co_rent.value.length < 1 || !isMoney(document.app.co_rent.value)){
			alert('Must enter co-applicant\'s rent/mortgage in dollars e.g. 1250.00 ');
			document.app.co_rent.focus(); return false;
		}
		
		if (document.app.co_employer.value.length < 1){
			alert('Must enter employer for the co-applicant');
			document.app.co_employer.focus(); return false;		
		}
		
		if (document.app.co_emp_phone.value.length < 10) {
			alert('Must enter work phone for the co-applicant');
			document.app.co_emp_phone.focus(); return false;
		}
		
		if (document.app.co_emp_years.value == '' && document.app.co_emp_months.value == ''){
			alert('Must enter time on job for co-applicant');
			document.app.co_emp_years.focus(); return false;		
		}
		
		if (!isInt(document.app.co_emp_years)) {
			alert('Years at job must be a whole number');			
			document.app.co_emp_years.focus(); return false;		
		}
		
		if (!isInt(document.app.co_emp_months)) {
			alert('Months at job must be a whole number');			
			document.app.co_emp_months.focus(); return false;		
		}		
		
		if (document.app.co_job.value.length < 1) {
			alert('Must enter position of the co-applicant')
			document.app.co_job.focus(); return false;
		}		
		
		if (document.app.co_salary.value == '' || !isMoney(document.app.co_salary.value)){
			alert('Must enter income for co-applicant in dollars e.g. 1525.45');
			document.app.co_salary.focus(); return false;		
		}	
		
		if (!isMoney(document.app.co_other_income.value)) {
			alert('Other Income must be in dollars e.g. 635.75');
			document.app.co_other_income.focus(); return false;							
		}

	}
		
	if (document.app.amt_req.value.length < 1 || !isMoney(document.app.amt_req.value))
	{
		alert("Amount Requested must be a dollar amount e.g. 2500.00");
		document.app.amt_req.focus(); return false;
	}		

/* optional 3/3/2003 
	if (document.app.work_address.value.length < 1)
	{
		alert("Please enter your work address");
		document.app.work_address.focus(); return false;
	}
	if (document.app.work_city.value.length < 1)
	{
		alert("Please enter your work city");
		document.app.work_city.focus(); return false;
	}
	if (document.app.work_state.value.length < 1)
	{
		alert("Please enter your work state");
		document.app.work_state.focus(); return false;
	}
	if (document.app.work_zip.value.length < 1)
	{
		alert("Please enter your work ZIP");
		document.app.work_zip.focus(); return false;
	}
*/

  if (document.app.dr_id && !document.app.admin_page) { // DR ID field exists 

		if (document.app.dr_id.value.length < 4)
		// (document.app.dr_id.value.length < 5 || !syntax_check(document.app.dr_id.value.substring(0,2),5) || !syntax_check(document.app.dr_id.value.substring(document.app.dr_id.value.length - 3,document.app.dr_id.value.length),4)))
		{
			alert("Please enter a valid ID");
			document.app.dr_id.focus();  return false;
		}
	
		if (document.app.dr_name.value.length < 1)
		{
			alert("Must enter Doctor's Last Name");
			document.app.dr_name.focus();  return false;
		}
	
		if (document.app.dr_staff.value.length < 1)
		{
			alert("Must enter a Contact");
			document.app.dr_staff.focus();  return false;
		}		
/*
		if (document.app.dr_phone.value.length < 10)
		{
			alert("Must enter Doctor's Phone Number, including area code");
			document.app.dr_phone.focus();  return false;
		}		


		if (document.app.dr_fax.value.length < 10)
		{
			alert("Must enter Doctor's fax Number, including area code");
			document.app.dr_fax.focus();  return false;
		}	
*/

	} // End of DR ID Check


	// I AGREE BUTTON CHECK
	if (document.app.iagree) {
		if (document.app.iagree.checked) {
			return true;
		}
		else
	
		{
		 alert("Please mark the checkbox if you have read and agree to the terms and conditions, before submitting this application.");
		 document.app.iagree.focus();
		 return false;
		}
	}

	return true;

}

function syntax_check(txt,syntax) {
  var checkOK = new Array();
	checkOK[0] = " 0123456789-()"
	checkOK[1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
	checkOK[2] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.#/\,";
	checkOK[3] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
	checkOK[4] = "0123456789";
	checkOK[5] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	checkOK[6] = "0123456789.,"; //money

  //var checkStr = obj.value;
  var checkStr = txt
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK[syntax].length;  j++)
	  if (ch == checkOK[syntax].charAt(j))
		break;
	if (j == checkOK[syntax].length)
	{
	  allValid = false;
	  break;
	}
	allNum += ch;
  }
  return allValid;
}

function isMoney( amt )
{
	return syntax_check( amt, 6 );
}



function updateIncome() {
/*
	document.app.PAY.value = stripNotInList(document.app.PAY.value,digitsdot);
	document.app.other_income.value = stripNotInList(document.app.other_income.value,digitsdot);	
	document.app.INCOME.value = stripNotInList(document.app.INCOME.value,digitsdot);
	document.app.HOUSINC.value = stripNotInList(document.app.HOUSINC.value,digitsdot);
	document.app.HOUSINC.value = parseInt(document.app.PAY.value) + parseInt(document.app.INCOME.value) + parseInt(document.app.other_income.value);
*/	
/*
	if (isNaN(parseInt(document.app.PAY.value))) {
		if (isNaN(parseInt(document.app.INCOME.value))) {
		} else {
			document.app.PAY.value = "";
			document.app.HOUSINC.value = parseInt(document.app.INCOME.value);
			document.app.HOUSINC.value = parseInt(document.app.INCOME.value+"");
		}
	} else {
		if (isNaN(parseInt(document.app.INCOME.value))) {
			document.app.PAY.value = parseInt(document.app.PAY.value);
			document.app.INCOME.value = "";
			document.app.HOUSINC.value = parseInt(document.app.PAY.value);
		} else {
			document.app.PAY.value = parseInt(document.app.PAY.value);
			document.app.INCOME.value = parseInt(document.app.INCOME.value);
			document.app.HOUSINC.value = parseInt(document.app.PAY.value) + parseInt(document.app.INCOME.value);
		}
	}
*/
}
