function doClose(num){
    if (num != "page1"){ 
    	document.getElementById('page1').style.display = 'none';
    }	
    if (num != "page2"){ 
    	document.getElementById('page2').style.display = 'none';
    }	
    if (num != "page3"){ 
    	document.getElementById('page3').style.display = 'none';
    }
    if (num != "page4"){ 
    	document.getElementById('page4').style.display = 'none';
    }    
}

function appContinue(page){
  if (page == "one"){
    document.getElementById('page1').style.display = "inline";	
    doClose('page1');
    document.getElementById('buttons').innerHTML = '<input type="button" value="Continue" onclick="appContinue(\'two\');">';
  }
  if (page == "two"){
    var req = new Array()
    req[0] = "Date"
    req[1] = "Property"
    req[2] = "Applicant"
    req[3] = "ApplicantSSN"
    req[4] = "ApplicantDOB"
    req[5] = "CoApplicant"
    req[6] = "CoApplicantSSN"
    req[7] = "CoApplicantDOB"
    for (i=0; i<=4; i++){ 
      if (document.getElementById(req[i]).value == ""){
      	document.getElementById(req[i] + "Req").style.color = "red";
      	var fail = true;      	
      } else {
      	document.getElementById(req[i] + "Req").style.color = "black";
      }
    }
    if (document.getElementById('CoApplicantOccupy').value == "Yes" ){
	for (i=5; i<=7; i++){ 
	  if (document.getElementById(req[i]).value == ""){
	      document.getElementById(req[i] + "Req").style.color = "red";
	      var fail = true;      	
	  } else {
      	      document.getElementById(req[i] + "Req").style.color = "black";
          }
	}  			
    } else {
    	for (i=5; i<=7; i++){ 
    	    document.getElementById(req[i] + "Req").style.color = "black";	
    	}    	
    }
    if (!fail){	    
    	document.getElementById('page2').style.display = "inline";
    	if (document.getElementById('ApplicantOccupy').value == "Yes"){
    		document.getElementById('OccupantName_1').value = document.getElementById('Applicant').value;
    		document.getElementById('OccupantDOB_1').value = document.getElementById('ApplicantDOB').value;
    		document.getElementById('OccupantSSN_1').value = document.getElementById('ApplicantSSN').value;
    	}
    	if (document.getElementById('CoApplicantOccupy').value == "Yes" ){
    		document.getElementById('Occupant2').style.display = "inline";
    		document.getElementById('OccupantName_2').value = document.getElementById('CoApplicant').value;
    		document.getElementById('OccupantDOB_2').value = document.getElementById('CoApplicantDOB').value;
    		document.getElementById('OccupantSSN_2').value = document.getElementById('CoApplicantSSN').value;
    	}
    	if (document.getElementById('CoApplicant').value != ""){
    		document.getElementById('CoAppEmploy').style.display = "inline";    		
    		document.getElementById('CoAppSig').style.display = "inline";    		
    	}    		
    	if (document.getElementById('Occupants').value > 1){ 
    	var max =  document.getElementById('Occupants').value;
    	  for (o = 2; o <= max; o++){ 
    	    document.getElementById("Occupant" + o).style.display = "inline";	
    	  }
    	}
    	doClose('page2');
    	document.getElementById('buttons').innerHTML = '<input type="button" value="Continue" onclick="appContinue(\'three\');">';
    } else {
    	alert ("Please complete all REQUIRED fields marked in red");
    }
  }
  if (page == "three"){
    var req = new Array()
    req[0] = "PresentAddress"
    req[1] = "PresentCity"
    req[2] = "PresentState"
    req[3] = "PresentZIP"
    req[4] = "PresentPhone"    
    req[5] = "PresentReason"   
    req[6] = "PresentFrom"
    req[7] = "PresentTo"
    req[8] = "PresentLandlord"    
    req[9] = "PresentLandlordPhone"   
    req[10] = "PresentPayment"         

    for (i=0; i<=10; i++){ 
      if (document.getElementById(req[i]).value == ""){
      	document.getElementById(req[i] + "Req").style.color = "red";
      	var fail = true;      	
      } else {
//      	document.getElementById(req[i] + "Req").style.color = "black";
      }
    }    
    var max =  document.getElementById('Occupants').value;
    for (o = 1; o <= max; o++){ 
    	if (document.getElementById("OccupantName_" + o).value == ""){ 
    	    document.getElementById("OccupantName_" + o + "Req").style.color = "red"; var fail = true; 
    	} else {
    	    document.getElementById("OccupantName_" + o + "Req").style.color = "black"; 
    	}	
    	if (document.getElementById("OccupantDOB_" + o).value == ""){ 
    	    document.getElementById("OccupantDOB_" + o + "Req").style.color = "red"; var fail = true; 
    	} else {
    	    document.getElementById("OccupantDOB_" + o + "Req").style.color = "black"; 
    	}
    	if (document.getElementById("OccupantSSN_" + o).value == ""){ 
    	    document.getElementById("OccupantSSN_" + o + "Req").style.color = "red"; var fail = true; 
    	} else {
    	    document.getElementById("OccupantSSN_" + o + "Req").style.color = "black"; 
    	}
    }
    
    if (!fail){	
    	document.getElementById('page3').style.display = "inline";	
    	doClose('page3');
    	document.getElementById('buttons').innerHTML = '<input type="button" value="Continue" onclick="appContinue(\'four\');">';
    } else {
    	alert ("Please complete all REQUIRED fields marked in red");
    }   
  }
  if (page == "four"){
    var req = new Array()
    req[0] = "EmergencyContactName"
    req[1] = "EmergencyContactHomePhone"
    req[2] = "EmergencyContactRelation"
    req[3] = "EmergencyContactWorkPhone"
    for (i=0; i<=3; i++){ 
      if (document.getElementById(req[i]).value == ""){
      	document.getElementById(req[i] + "Req").style.color = "red";
      	var fail = true;      	
      }
    }
    if (!fail){		
    	document.getElementById('page4').style.display = "inline";	
    	doClose('page4');
    	document.getElementById('buttons').innerHTML = '<input type="button" value="Continue" onclick="appContinue(\'five\');">';
    } else {
    	alert ("Please complete all REQUIRED fields marked in red");
    }
  }
  if (page == "five"){
    var req = new Array()
    req[0] = "Signature1"        
    
    if (document.getElementById(req[0]).value == ""){
        document.getElementById(req[0] + "Req").style.color = "red";
    	var fail = true;      	
    } else {
      	document.getElementById(req[0] + "Req").style.color = "black";
    }
    
    if (document.getElementById('CoApplicant').value != ""){
    	req[1] = "Signature2"
    	if (document.getElementById(req[1]).value == ""){
            document.getElementById(req[1] + "Req").style.color = "red";
    	    var fail = true;      	
    	} else {
      	    document.getElementById(req[1] + "Req").style.color = "black";
        }
    }    
    
    if (!fail){		
    	document.getElementById('title').innerHTML = 'Please Review Your Application';
    	document.getElementById('page1').style.display = "inline";	
    	document.getElementById('page2').style.display = "inline";	
    	document.getElementById('page3').style.display = "inline";	
    	document.getElementById('page4').style.display = "inline";	
    	document.getElementById('buttons').innerHTML = '<input type="submit" name="submit" value="Submit Application">';
    } else {
    	alert ("Please complete all REQUIRED fields marked in red");
    }	
  }  
}

function doRequire(field) {
  if (document.getElementById(field).value = ""){
  	var fail = true
  	return fail
  }	
}
