﻿
//Front end validation of registration form fields
function validFormFields() {
  //Grab field values from F form
  var strFirstName = document.Registration.FFirstName.value;
  var strLastName = document.Registration.FLastName.value;
  var strEmail = document.Registration.FEAdd.value;
  var strVEmail = document.Registration.FVEAdd.value  
  var strIndustry = document.Registration.FCareerFocus1.value;
  var strJobTitle = document.Registration.FTitle.value;
  var bCountryState = document.Registration.FCountryState
  if (bCountryState) { var strCountryState = document.Registration.FCountryState.value; }
  var strPostalCode = document.Registration.FZipCode.value;
  var strAlerts = valButton(document.Registration.FAlert);
  var strNewsletter = valButton(document.Registration.FNewsletter);
  //Start checking lengths of fields (lengths equal to those accepted by DB fields)
  var bAlertThrown = !(validFieldLength(strFirstName, "first name", 1, 50));
  if (!bAlertThrown) { bAlertThrown = !(validFieldLength(strLastName, "last name", 1, 50)); }
  if (!bAlertThrown) { bAlertThrown = !(validFieldLength(strEmail, "email", 1, 100)); }
  if (!bAlertThrown) { bAlertThrown = !(validFieldLength(strVEmail, "email", 1, 100)); }
  if (!bAlertThrown) { bAlertThrown = !(validFieldLength(strJobTitle, "job title", 1, 100)); }
  if (!bAlertThrown) { bAlertThrown = !(validFieldLength(strPostalCode, "postal code", 1, 10)); }
  //Check email validity
  if (!bAlertThrown) { bAlertThrown = !(validateEmail(document.Registration.FEAdd)); }
  //Make sure email verification is correct
  if (!bAlertThrown) {
    if (strEmail.toLowerCase() != strVEmail.toLowerCase()) {
      alert("Email verification failed. Please verify your email address.");
      bAlertThrown = true;
    }
  }
  //Verify industry value
  if (!bAlertThrown) {
    if (strIndustry == 174) {
      alert("Please select your industry of interest from the drop down list provided.");
      bAlertThrown = true;
    }
  }
  //Verify country/state value (if it exists)
  if (!bAlertThrown && bCountryState) {
    if (strCountryState == '') {
      alert("Please select your location of interest from the drop down list provided.");
      bAlertThrown = true;
    }
  }
  //Verify alerts radio button has a selected item
  if (!bAlertThrown) {
    if (strAlerts == null) {
      alert("Please select whether or not you would like to receive job alerts.")
      bAlertThrown = true;
    }
  }
  //Verify newsletter radio button has a selected item
  if (!bAlertThrown) {
    if (strNewsletter == null) {
      alert("Please select whether or not you would like to receive the career newsletter.")
      bAlertThrown = true;
    }
  }
  return (!bAlertThrown);
}

//determine if the user filled out any item on the form
function anyFieldFilled() {

  var strFirstName = document.Registration.FFirstName.value;
  var strLastName = document.Registration.FLastName.value;
  var strEmail = document.Registration.FEAdd.value;
  var strVEmail = document.Registration.FVEAdd.value;
  var strIndustry = document.Registration.FCareerFocus1.value;
  var strJobTitle = document.Registration.FTitle.value;
  var bCountryState = document.Registration.FCountryState;
  if (bCountryState) { var strCountryState = document.Registration.FCountryState.value; }
  var strPostalCode = document.Registration.FZipCode.value;
  
  if (strFirstName=='' && strLastName=='' && strEmail=='' && strVEmail=='' && strJobTitle=='' && strCountryState=='' && strPostalCode==''){    
    return false;
  }
  else{
    return true;
  }

}

//On click of the submit button, validate fields and then submit them to action page
function submitTasks(fwdURL) {
    if (anyFieldFilled() == true){
      var bValidFormInput = validFormFields();
      if (bValidFormInput) {
        document.Registration.submit();
        YAHOO.example.container.dialog1.hide();
        document.location.reload();
      }
    } else {
            document.ltboxtrackCLK.submit();
            YAHOO.example.container.dialog1.hide();
            window.open(fwdURL,'_blank','width=955,height=686,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,fullscreen=0,channelmode=0,screenX=50,left=200,screenY=50,top=0');
     }
}

YAHOO.namespace("example.container");
function init() {
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
	  this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		var response = o.responseText;
		response = response.split("<!")[0];
		document.getElementById("resp").innerHTML = response;
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1", 
							{ width : "415px;",
							  fixedcenter : true,
							  modal:true,
							  draggable:false,
							  visible : false, 
							  constraintoviewport : true
						  });

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.example.container.dialog1.validate = function() {
		var data = this.getData();
		if (data.firstname == "" || data.lastname == "") {
			alert("Please enter your first and last names.");
			return false;
		} else {
			return true;
		}
	};

	// Wire up the success and failure handlers
	YAHOO.example.container.dialog1.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.example.container.dialog1.render();
	YAHOO.example.container.dialog1.show();

	YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.dialog1.show, YAHOO.example.container.dialog1, true);
	YAHOO.util.Event.addListener("hide", "click", YAHOO.example.container.dialog1.hide, YAHOO.example.container.dialog1, true);
	
}
