// $Id: resume_submit.js,v 3.0 2005/01/17 02:37:58 pgollucci Exp $

function resume_submit_question (opportunity_id) {

  var rv = confirm("Are you sure you want to abandon your resume submision?");

  if (rv) {
    window.location.href="/employment/index.asp?cmd=opp_details&opportunity_id=" + opportunity_id;
  }

  return;
}

function clearRadioField (question_id) {

  var field = eval("document.input.response_" + question_id);

  // special case only one answer
  if (field.length == null) {
    field.checked = false;
  }
  // general case ... multiple answers
  else {
    for (var i = 0; i < field.length; i++) {
      field[i].checked = false;
    }
  }

  return;
}  

function resume_validate (form, q_count, photo_count, photo_req, personal_statement_ind, desired_salary_ind, web_site_ind, notRetail, opportunity_id) {
  
  var errors = "";
  
  // first check that all the required fields are not blank
  if (isBlank(form.first_name.value)) {
    errors += "Your First Name is required.\n";
  }

  if (isBlank(form.last_name.value)) {
    errors += "Your Last Name is required.\n";
  }
	
  if (isBlank(form.e_mail.value)) {
    errors += "Your E-Mail is required.\n";
  }

  if (isBlank(form.addr1.value)) {
    errors += "The first line of your address is required.\n";
  }
  
  if (isBlank(form.city_name.value)) {
    errors += "Your city is required.\n";
  }
  
  if(document.input.Country.selectedIndex == 0 ||
     document.input.Country.selectedIndex == 1 ||
	 document.input.Country.selectedIndex == 4)
  {
  	errors += "Your country is required.\n";
  
  }
  
  if (isBlank(form.phone.value)) {
    errors += "Your phone number is required.\n";
  }

  if (notRetail && isBlank(document.getElementById('resume').value)) {
    errors += "Your resume is required.\n";
  }
	
  // REALLY cheap e-mail regex 
  // 1+ chars 
  // @
  // 1+ chars
  // .
  // 1+ chars
  var re = /.+\@.+\..+/;

  if (!isBlank(form.e_mail.value) && !form.e_mail.value.match(re)) {
    errors += "Your E-Mail is invalid\n";
  }

  var re_resume = /(pdf|doc|docx)$/i;
  
  if (!isBlank(document.getElementById('resume').value) && !document.getElementById('resume').value.match(re_resume)) {
    errors += "Your resume must be in a Microsoft Word or Adobe Acrobat PDF format.\n";
  }
 
 	// now the dynamic validation

	// personal statement
	if (personal_statement_ind == 1 && isBlank(form.personal_statement.value)) {
		errors += "A Personal statement is required.\n";
	}
	
	// Desired Salary
	if (desired_salary_ind == 1 && isBlank(form.desired_salary.value)) {
		errors += "A Desired Salary is required.\n";
	}
	
	// Web Site
	if (web_site_ind == 1 && isBlank(form.web_site.value)) {
		errors += "Web Site is required.\n";
	}

	// photos first photo is required if 1 or more is displayed
	if (photo_count == 1)
	{
		if (isBlank(form.photo_1.value))
		{
			errors += "At least one photo is required.\n";
		}
	}
	
	if (photo_count == 2)
	{
		if (isBlank(form.photo_1.value) || isBlank(form.photo_2.value))
		{
			errors += "At least two photos are required.\n";
		}
	}
	
	if (photo_count == 3)
	{
		if (isBlank(form.photo_1.value) || isBlank(form.photo_2.value) || isBlank(form.photo_3.value))
		{
			errors += "Three photos are required.\n";
		}
	}
	
	if (photo_count > 10) photo_count = photo_count - 10;

	var totalpics = 0;
	for (var j = 1; j <= 10; j++) {
		if ( document.getElementById('photo_' + j) != null ) {
			if ( document.getElementById('photo_' + j).value != '' ) {
				totalpics = totalpics + 1;
			}
		}
	}
	
	if (totalpics < photo_req)
	{
		errors += photo_req + " photo(s) are required.\n";
	}

	// custom question validation
	for (var i = 1; i <= q_count; i++) {
		// a response is required
		if (eval("form.response_required_ind_" + i + ".value") == 1) {
			var response = 0;

			var q_id = eval("form.question_id_" + i + ".value");
			var element = eval("form.response_" + q_id);

			// special case only one answer
			if (element.length == null) {
				response = element.checked == true ? 1 : 0;
			}
			// general case ... more then one answer
			else {
				for (var j = 0; j < element.length; j++) {
					if (element[j].checked == true) {
						response = 1;
						break;
					}
				}
			}

			if (!response) {
				errors += "You must answer Qualifier Question # " + i + ".\n";
			}
		}
	}
	
	// only certain photo file types are allowed
	// JPEG, TIFF, GIF, PSD or PICT
	var re_photo = /(jpeg|jpg|tiff|gif|psd|pict)$/i;

	for (var i = 1; i <= photo_count; i++) {
		if (!isBlank(eval("form.photo_" + i + ".value")) && !eval("form.photo_" + i + ".value").match(re_photo)) {
			errors += "Photo # " + i + " is not in one of the specified formats: JPEG, TIFF, GIF, PSD, PICT";
		}
	}
  
  // can't upload the same photo more then 1 time
  for (var i = 1; i <= photo_count; i++) {
    if (!isBlank(eval("form.photo_" + i + ".value"))) {
      for (var j = i+1; j <= photo_count; j++) {
        if (!isBlank(eval("form.photo_" + j + ".value"))) {
          if (eval("form.photo_" + j + ".value") == eval("form.photo_" + i + ".value")) {
            errors += "Photos # " + i + " and " + j + " must be different files.\n";
          }
        }
      }
    }
  }
  
  if (errors != "") {
    alert(errors);
    return false;
  }
  else {
    return true;
  }
}

