function toggleadd(e) {
  var contact_fields = new Array('addr1','addr2','city','state','zip','country','phone_countrycode','phone','mobile_phone_countrycode','mobile_phone','email','website');
  if (e) {
    for (f in contact_fields) {
      try {
        document.getElementById('pers_'+contact_fields[f]).value=document.getElementById('bus_'+contact_fields[f]).value;
      }
      catch (ex) {
        alert(f+': '+contact_fields[f]);
      }
    }
  }
  else {
    for (f in contact_fields) {
      try {
        document.getElementById('pers_'+contact_fields[f]).value='';
      }
      catch (ex) {
        alert(f+': '+contact_fields[f]+': '+document.getElementById('pers_'+contact_fields[f]).type);
      }
    }
  }
}

function postnote(appID) {
  var http = new XMLHttpRequest();
  var note1 = document.getElementById('note1_'+appID).value;
  var note2 = document.getElementById('note2_'+appID).value;
  var note3 = document.getElementById('note3_'+appID).value;
  var note4 = document.getElementById('note4_'+appID).value;
  var url = "note.php";
  var params = "note1="+escape(note1);
  params += "&note2="+escape(note2);
  params += "&note3="+escape(note3);
  params += "&note4="+escape(note4);
  params += "&dataset_rowID="+appID;
  //debug alert (params);
  http.open("POST", url, true);

  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = function() {//Call a function when the state changes.
  	if(http.readyState == 4 && http.status == 200) {
  		alert(http.responseText);
  	}
  }
  http.send(params);
}


function updatestatus(appID,status,judge_id,is_final) {
  var http = new XMLHttpRequest();
  var url = "status.php";
  var params = "appID="+appID;
  params += "&status="+status;
  params += "&judge_id="+judge_id;
  if (is_final) {
	  params += "&type=final";
	  if (status==0)
	  {
		to_clear = document.getElementsByName('final_status_'+appID);
		//alert(to_clear.length);
		to_clear[0].checked = false;
		to_clear[1].checked = false;
	  }
  }
  /* debug */
  //alert (params);
  http.open("POST", url, true);

  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = function() {//Call a function when the state changes.
  	if(http.readyState == 4 && http.status == 200) {
  		alert(http.responseText);
  	}
  }
  http.send(params);
}


var badfields = [];

function dovalidate() {

  //remove red borders before re-validating
  if (badfields.length>0) {
    for (i in badfields) {
      if (document.getElementById(badfields[i])!=null) {
	document.getElementById(badfields[i]).parentNode.style.border = 'none';
      }
      else {
	cb = document.theForm[badfields[i]];
	cb[0].parentNode.style.border = 'none';
      }
    }
    badfields=[];
  }
	  
  var req_txt = ['first', 'last', 'birth_country', 'residence_country', 'title', 'howlong', 'bus_addr1', 'bus_city', 'bus_state', 'bus_zip', 'bus_country', 'bus_phone', 'bus_email', 'pers_addr1', 'pers_city', 'pers_state', 'pers_zip', 'pers_country', 'pers_phone', 'pers_email', 'bio', 'vitae', 'letter'];

  var req_radio = ['citizenship','contact_method_email', 'contact_method_phone', 'job_status'];
  var req_other_radio = [['heard','heard_other','other'],['prevapp','prevapp_year','yes']];
  var req_sel = []; //did have state dropdowns, but changed to text
  var req_cbox = ['role', 'medium'];
  var req_cbox_pick1 = ['visualarts', 'architecture', 'knowledge']; //AND CHECK OTHER 
  var req_other_cbox = ['role_other', 'medium_other', 'visualarts_other', 'architecture_other', 'knowledge_other'];
  
  for (e in req_txt) {
    if(document.getElementById(req_txt[e]).value=='') {
		badfields[badfields.length]=req_txt[e];
	}
  }

  //job_status/job_employer
  //if employer field's corresponding radio button is checked, check if it's blank
  //otherwise delete it so there aren't multiple job_employer params being submitted
  //job_status_del removed below, once form validates i.e. just before submit
  job_status_ids = new Array(10,12,14);
  job_status_del = new Array();
  for (i in job_status_ids) {
    if 	(document.theForm[job_status_ids[i]-1].checked) {
      if (document.theForm[job_status_ids[i]].value=='') {
    	//err+="Missing field "+document.theForm[job_status_ids[i]].name+"\n";
    	badfields[badfields.length]=document.theForm[job_status_ids[i]].name;
      }
    }
    else {
      job_status_del[job_status_del.length] = document.theForm[job_status_ids[i]];
    }
  }
  
  //radio buttons
  var one_selected;
  for (e in req_radio) {
	  thisradio = eval('document.theForm.'+req_radio[e]);
	  one_selected=0;
	  for(var r=0;r < thisradio.length;r++)
	  {
		if(thisradio[r].checked)
		{
		  one_selected=1;
		  break;
		}
	  }
	  if (one_selected==0) {
		  //err+='None selected for '+req_radio[e]+'\n';
		  badfields[badfields.length]=req_radio[e];
  	  }
  }
  
  //checkboxes
  for (e in req_cbox) {
    cb = document.theForm[req_cbox[e]+'[]'];
    one_selected=0;
    for (i=0; i<cb.length; i++) {
      if (cb[i].checked) {
        one_selected=1;
        break;
      }
    }
    if (one_selected==0) {
      badfields[badfields.length]=req_cbox[e]+'[]';
    }    
  }
  
  //must check at least one box in visualarts/architecture/perfarts
  one_selected=0;
  for (e=0; e<req_cbox_pick1.length; e++) {
    cb = document.theForm[req_cbox_pick1[e]+'[]'];  
    for (i=0; i<cb.length; i++) {
      if (cb[i].checked) {
        one_selected=1;
        break;
      }
    }
  }
  if (one_selected==0) {
    badfields[badfields.length]=req_cbox_pick1[0]+'[]';
  }
  
  //select fields
  for (e in req_sel) {
    sel = document.getElementById(req_sel[e]);
    if (sel.selectedIndex==0) {
      badfields[badfields.length]=req_sel[e];
    }
  }
  
  //radio buttons with "other" option
  for (e in req_other_radio) {
	  thisradio = eval('document.theForm.'+req_other_radio[e][0]);
	  one_selected=-1;
	  for(i=0; i<thisradio.length; i++)
	  {
		if(thisradio[i].checked)
		{
		  one_selected=i;
		  break;
		}
	  }
	  if (one_selected<0) {
		  //err+='None selected for '+req_other_radio[e]+'\n';
		  badfields[badfields.length]=req_other_radio[e][0];
  	  }
  	  else {
  	  	thistxt = document.getElementById(req_other_radio[e][1]);
  	  	if(thisradio[one_selected].value==req_other_radio[e][2]) {
  	  	  if (thistxt.value=='') {
  	  	    badfields[badfields.length]=req_other_radio[e][0];
  	  	  }
  	  	}
  	  	else {
  	  	  thistxt.value='';
  	  	}
  	  }
  }
  
  //checkboxes with "other" option
  for (e=0; e<req_other_cbox.length; e++) {
    cb = document.theForm[req_other_cbox[e].replace("_other","")+'[]'];
    for (i=0; i<cb.length; i++) {
      if (cb[i].checked && cb[i].value=='other') {
	    if (document.getElementById(req_other_cbox[e]).value=='') {
	      badfields[badfields.length]=req_other_cbox[e];
	    }    
      }
      if (!cb[i].checked && cb[i].value=='other') {
        document.getElementById(req_other_cbox[e]).value='';
      }
    }
  }
  
  //work_sample* -- if file or url, type must be selected
  for (i=1; i<=3; i++) {
    if (document.getElementById('work_sample'+i+'_url').value=='' && document.getElementById('work_sample'+i+'_file').value=='') {
      badfields[badfields.length]='work_sample'+i+'_url';
    }
    else {
      rb = eval('document.theForm.work_sample'+i+'_type');
      one_selected=0;
	  for(var r=0;r < rb.length;r++)
	  {
		if(rb[r].checked)
		{
		  one_selected=1;
		  break;
		}
	  }
	  if (one_selected==0) {
		  badfields[badfields.length]='work_sample'+i+'_type';
  	  }
    }
  }
      

  //must accept affidavit 
  if (document.getElementById('affidavit').checked==false) {
    badfields[badfields.length]='affidavit';
  }
  
  //IF ERROR ALERT USER
  if (badfields.length>0) {
    var err='';
    for (i in badfields) {
      if (document.getElementById(badfields[i])!=null) {
        document.getElementById(badfields[i]).parentNode.style.border = '2px solid red';
      }
      else {
        alert(badfields[i]);
        cb = document.theForm[badfields[i]];
        cb[0].parentNode.style.border = '2px solid red';
      }
      err += badfields[i] + "\n";
    }
    alert("Your application has missing information.  Please complete the fields highlighted in red."); // + " / " + err);
  }
  else {
	for (i in job_status_del) {
		el = job_status_del[i];
		el.parentNode.removeChild(el);
	}
	//alert('yay!');
	document.theForm.submit();
  }

}

