//****

function validate_about() {
  //clean any previous error
  try {$('about_error').removeChild($('about_error_i'))}
  catch (e) {}
  
  $('about_synopsis').innerHTML = ''
  var msg = document.createElement('ul')
  msg.className = 'synopsys'
  
  var error = ''
    
  //hoh age
  var age = $('hoh_age').value
  var agev = validateInt(age)
  var t1 = document.createElement('li')
  msg.appendChild(t1)
  
  if (agev) {
    error += 'Please tell us your age'
    //Event.observe('hoh_age', 'change', function() {})
  }
  else {
    if (age >= 120) {error += 'I don\'t think there are people that old!'}
    else {
      if (age < 15) {t1.innerHTML = 'You are ' + age + ' years old, (a typo?).'}
      else {t1.innerHTML = 'You are ' + age + ' years old.'}
    }
  }
  
  //citizenship
  var ctz = $('hoh_citizenship').value
  var t2 = document.createElement('li')
  if (ctz == 1) {t2.innerHTML = 'You are a US Citizen.'}
  else if (ctz == 2) {t2.innerHTML =  'You are a Permanent Resident (you have a green card).'}
  else if (ctz == 3) {t2.innerHTML = 'You are a refugee, or an asylee in the US.'}
  else {t2.innerHTML = '<span class="bold">You are an undocumented alien. Keep in mind that most programs require citizenship, permanent residency, or asylee/refugee status.</span>'}
	
	//there might be no 'blur' on the citizenship, so I'll add the unhide here also.
	if (ctz <= 3) {$('filing_status_hf').show() }
	else {$('filing_status_hf').hide()}
  
  //this will save us when they switch sections to correct this piece but
  //had already answered the how-many-children question
  if (ctz < 4) {
    if ($('children_citizenship_hf').style.display != 'none') {
      $('children_citizenship_hf').style.display = 'none'
    }
  }
  else {
    if ($('hoh_children').value > 0 && $('children_citizenship_hf').style.display == 'none') {
      $('children_citizenship_hf').style.display = 'block'
    }
  }
  
  msg.appendChild(t2)
  
  //the state
  //var state = $('hoh_state').options[$('hoh_state').selectedIndex].text
  var t3 = document.createElement('li')
  if ($('hoh_state').value == 1) {t3.innerHTML = 'You live in Connecticut.'}
  else {
    t3.innerHTML = '<span class="bold">You do not live in Connecticut. We will only \n\
                  check whether you qualify for the Federal Assistance Programs</span>. '
  }
  msg.appendChild(t3)
  
  //got a spouse?
  var t4 = document.createElement('li')
  if ($('hoh_spouse').value == 0) {
    t4.innerHTML = 'You do not have a spouse or partner living with you.'}
  else {t4.innerHTML = 'You have a spouse or partner living with you.'}
  msg.appendChild(t4)


  return do_synopsis('about', msg, error)
}

function validate_family() {
  //clean any previous error
  try {$('family_error').removeChild($('family_error_i'))}
  catch (e) {}
  
  $('family_synopsis').innerHTML = ''
  var msg = document.createElement('ul')
  msg.className = 'synopsys'
  
  var error = ''
  
  //children
  var children = $('hoh_children').value
  var childrenv = validateInt(children)
  if (childrenv) {
    error += 'Do you have any children?<br/>'
    //Event.observe('hoh_children', 'change', function() {switch_section('money')})
  }
  else {
    var t1 = document.createElement('li')
    if (children == 0) {t1.innerHTML = 'You have no children.'}
    else {
      if (children == 1) {
        t1.innerHTML = 'There is 1 child living with you,'
        //children's age
        if ($('has_children_under5').value != 0) {
          t1.innerHTML += ' and he/she is less than 5 years old.'
        }
        else {
          if ($('has_children_under13').value != 0) {
            t1.innerHTML += ' and he/she is less than 13 years old.'}
          else {t1.innerHTML += ' and he/she is age 13 or older.'}
        }
      }
      else {
        t1.innerHTML = 'You have ' + children + ' children living with you, '
        //children's age
        if ($('has_children_under5').value != 0) {
          t1.innerHTML += 'and at least one of them is less than 5 years old.'
        }
        else {
          if ($('has_children_under13').value != 0) {
            t1.innerHTML += 'and at least one of them is under 13 years old.'
          }
          else {t1.innerHTML += 'and all of them are age 13 or older.'}
        } 
      }
    }
    msg.appendChild(t1)
  }
  //adults
  var adults = $('adults').value
  var adultsv = validateInt(adults)
  if (adultsv) {
    error += 'Do other dependent adults live with you?<br/>'
    //Event.observe('adults', 'change', function() {switch_section('money')})    
  }
  else {
    var t2 = document.createElement('li')
    if (adults == 0) {t2.innerHTML = 'No other dependent adults live there.'}
    else if (adults == 1) {t2.innerHTML = 'There is one other dependent adult living there. '}
    else {t2.innerHTML = 'There are ' + adults + ' other dependent adults living there.'}
    msg.appendChild(t2)
  }
  
  //is disabled?
  var t3 = document.createElement('li')
  if ($('is_disabled').value == 0) {t3.innerHTML = 'No family member has a disability.'}
  else {t3.innerHTML = 'You, or someone in your house has a disability.'}
  msg.appendChild(t3)
  
  //is pregnant?
  var t4 = document.createElement('li')
  if ($('is_pregnant').value == 0) {
    t4.innerHTML = 'No one is pregnant, nor has been in the last twelve months.'}
  else {
    t4.innerHTML = 'You, or someone in your house is pregnant, or has been pregnant in the last twelve months.'}
  msg.appendChild(t4)
  
  return do_synopsis('family', msg, error)
}
  
function validate_money() {
  //clean any previous error
  try {$('money_error').removeChild($('money_error_i'))}
  catch (e) {}
  
  
  $('money_synopsis').innerHTML = ''
  var msg = document.createElement('ul')
  msg.className = 'synopsys'
  
  var error = ''
  
  //are you employed?
  var hohemp = $('hoh_is_employed').value
  var t1 = document.createElement('li')
  msg.appendChild(t1)
  
  if (hohemp == -1) {
    error += 'Are you employed?<br/>'
    //Event.observe('hoh_is_employed', 'change', function() {switch_section('theend')})
  }
  else if (hohemp == 0) {
    t1.innerHTML = 'You are currently not employed.'
    //are you in training?
    var training = $('hoh_is_trainingf').value
    if (training == 0) {
      t1.innerHTML += ' Nor are you participating in a training activity.'
    }
    else {t1.innerHTML += ' But you are getting training, which is great.'}
  }
  else {
    t1.innerHTML = 'You are employed. '
    //salary info
    var salary = $('salary_amount').value
	var salaryv = parseMoney(salary)
	
    if (!salaryv) {
		error += 'Please revise your income answer.<br/>'
      //Event.observe('salary_amount', 'change', function() {switch_section('theend')})
    }
    else {
	  if (salary != salaryv) {
		$('salary_amount').value = salaryv
		t1.innerHTML += 'You said you make <span class="bold">' + salary +
					    '</span> on a ' + $('spouse_salary_frequency').value + ' basis.' +
						'We think you meant <span class="bold">$' + salaryv +
						'</span>. If this is not so, please click to change it.'
	  }
	  else {
		t1.innerHTML += 'You make ' + salary + ' dollars on a ' + 
                      $('salary_frequency').value + ' basis. '
	  }
    }
  }
  // have we got a spouse?
  var t2 = document.createElement('li')
  
  if ($('hoh_spouse').value == 1) {
    //and, is him/her employed? 
    var spouseemp = $('spouse_is_employed').value
    if (spouseemp == -1) {
      error += 'Is your spouse employed? <br/>'
      //Event.observe('spouse_is_employed', 'change', function() {switch_section('theend')})
    }
    else if (spouseemp == 0) {
      t2.innerHTML = 'Your spouse is not employed.'
      //is the spouse in training?
      var training = $('spouse_is_trainingf').value
      if (training == 0) {
        t2.innerHTML += ' Nor is she/he taking part in a training activity.'}
      else {t2.innerHTML += ' But she/he is getting training. '}
    }
    else {
      t2.innerHTML = 'Your spouse is employed.'
      //spouse salary info
      var salary = $('spouse_salary_amount').value
      var salaryv = parseMoney(salary)
      if (!salaryv) {error += 'Please revise your spouse\'s income answer <br/>'}
      else {
		if (salary != salaryv) {
		  $('spouse_salary_amount').value = salaryv
		  t2.innerHTML += 'You said your spouse makes <span class="bold">' + salary +
						  '</span> on a ' + $('spouse_salary_frequency').value + ' basis.' +
						  'We think you meant <span class="bold">$' + salaryv +
						  '</span>. If this is not so, please click to change it.'
		}
		else {t2.innerHTML += 'Your spouse makes ' + salary + ' dollars on a ' +
                    $('spouse_salary_frequency').value + ' basis. '
		}
      }
    }   
    msg.appendChild(t2)
  }
  
  //otherincome
  var t3 = document.createElement('li')
  msg.appendChild(t3)
  
  if ($('otherincome').value == 1) {
    var amount = $('otherincome_amount').value
    var amountv = parseMoney(amount)
    if (!amountv) {error += 'Please revise your other income answer. <br/> '}
    else {
	  if (amount != amountv) {
		$('otherincome_amount').value = amountv
		t3.innerHTML = 'You said your other income was of <span class="bold">' + amount +
					   '</span>. on a ' + $('otherincome_frequency').value + ' basis. ' +
					   'We think you actually meant <span class="bold">$' + amountv +
					   '</span>. If this is not so, please click on this box to change it.'
	  }
      else {t3.innerHTML = 'You have other income in the amount of $' + amountv + ' on a ' + 
                      $('otherincome_frequency').value + ' basis. '
	  }
    }
  }
  else {t3.innerHTML = 'You don\'t have any income from other sources.'}

  //taxes	
	//only check if citizenship != undocumented
	if ($('hoh_citizenship').value <= 3) {
		var t4 = document.createElement('li')
	  var taxes = $('filing_status').value
		
		if (taxes == 0) {t4.innerHTML = 'You will not be filing an income tax return this year.'}
	  else if (taxes == 'single') {t4.innerHTML = 'You will be filing your taxes by yourself.'}
	  else if (taxes == 'joint') {t4.innerHTML = 'You will be filing your taxes with your spouse.'}
	  else {error += 'How will you file your taxes?'}
		msg.appendChild(t4)
	}

  return do_synopsis('money', msg, error)
}

function do_synopsis(section, synopsisMsg, errorMsg) {
  var error = section + '_error'
  var errori = section + '_error_i'
  var synopsis = section + '_synopsis'
  
  if (errorMsg != '') {
    var e = document.createElement('div')
    e.id = errori
    e.innerHTML = errorMsg
    $(error).appendChild(e)
    $(error).style.display = 'block'
    
    Event.observe(synopsis, 'click', function() {switch_section_goback(section)})

    eval('global_' + section + '_error = 1')
    return 1
  }
  else {
    $(error).style.display = 'none'
    $(synopsis).appendChild(synopsisMsg)
    
    //a bad place to put this.
    var _synbottom = document.createElement('div')
    _synbottom.className = 'synopsis_bottom'
    $(synopsis).appendChild(_synbottom)
    
    Event.observe(synopsis, 'click', function() {switch_section_goback(section)})
    
    Effect.BlindDown(synopsis)
    eval('global_' + section + '_error = 0')
  }
}

function submit() {
  //checks there are no errors and submits the form
  
  if (global_about_error != 0 || global_family_error != 0 || 
      global_money_error != 0) {
    try {$('submit_error').removeChild($('submit_error_txt'))}
    catch (e) {}
    
    var e = document.createElement('div')
    e.id = "submit_error_txt"
    e.innerHTML = 'It looks like you haven\'t finished. Please check each section\'s summary.'
    $('submit_error').appendChild(e)
    $('submit_error').style.display = 'block'
  } 
  else {$('TheForm').submit()}
}

//****
//****

function validateNumberField(fieldId) { }
  


function validateInt(f) {
  //it doesn't take floats. who has half a kid? err, nevermind. it just doesn't
  var e = null
  var valid = "0123456789"; 
  
  if (f == '') {e = 'please, fill it in.'}
  else {
    for (i = 0; i < f.length; i++) {
      char = f.charAt(i) 
      if (valid.indexOf(char) == -1)  {
        e = 'it needs a number.'
        break 
      }
    }
  }     
  if (e != null) {return e;}
  return
}

function parseMoney(f) {
  //it strips anything that is not number or a period
  var valid = "0123456789."; 
  var parsed = ''
  
  if (!f) {return }
  else {
    for (i = 0; i < f.length; i++) {
      var char = f.charAt(i) 
      if (valid.indexOf(char) != -1) {parsed += char }
    }
  }
  //think leading or trailing periods
  return parseFloat(parsed)
  
}

function isRadioChecked(radioObj) {
  for (var i = 0; i < radioObj.length; i++) {
    if (radioObj[i].checked) {return true}
  }
  return false
  
}