/*
File: internship/scripts/intern-submit-end-script.js

MAKE SURE YOU KEEP TRACK OF EVERYTHING YOU DO!

Author: Dwight VanTuyl
DD-MMM-YYYY: 14-Mar-2008
Purpose: Form manipulation specifically for the end submit tab of the submit form.
+++++++++++++
Modifications
+++++++++++++
Editor:		
DD-MMM-YYYY:
Watcha did:	
~~~~~~~~~~~~~
*/

Ext.namespace('Intern.submit');

function mw_trim(str){
	// trim Left and Right, replace \s+ with \s. added by martin w, 1 jul 2008
	str = str.replace(/\s+/g, " ");
	str = str.replace(/^\s+|\s+$/g, "");
	return str;
}

Intern.submit.end = {

  //send form values to preview announcement processor
  preview: function() {
    
    //check if the form is valid then send to processor
    if(Intern.submit.validate.validateAll() && Intern.submit.end.checkDependencies()){
    
      //Set form submit to preview
      var form = $("form[name='internForm']");
      form.attr('action', '/internship/submit/intern-submit-previewProcess.cfm');
      form.attr('target', '_blank');
      
      //geocode intern location and submit form
      Intern.submit.end.geocodeIntLocation();
    }else{
      return false;
    }
  },
  
  //send form values to database processor
  submit: function() {
  
    //check if the form is valid then send to processor
    if(Intern.submit.validate.validateAll() && Intern.submit.end.checkDependencies()){
    
      //get rid of accidental navigate away confirmation
      needToConfirm = false;
      
      //Set form submit to database
      var form = $("form[name='internForm']");
      form.attr('action', '/internship/submit/intern-submit-databaseProcess.cfm');
      form.attr('target', '_top');
      
      //geocode intern location and submit form
      Intern.submit.end.geocodeIntLocation();
    }else{
      return false;
    }
  },
  
  checkDependencies: function(){
  	/* 
	these are the conditions where one radiobutton/checkbox determines whether
	or not a bunch of other fields are required. e.g. if you check hasDeadline,
	you actually have to provide a deadline.
	*/
		//alert("dependency checking... ");
		
		// if type-of-work select is "other", the text field int_typeOther is required.
		if(
			$("[name='int_typeSelect'] option:selected").val() == "other" 
			&& 
			$("[name='int_typeOther']").val() == "" 
		){
			alert("You have selected the Type of Work 'other'. Please specify what type it is in the text field.");
			Intern.submit.global.gotoTab(3);
			$("[name='int_typeOther']").focus();
			return false;
		}
		
		// if compensation radiobutton is "paid", salary text field is required
		if(
			$("[name='int_comp']:checked").val() == "Paid"
			&&
			$("[name='int_salary']").val() == ""
		){
			alert("You have selected Compensation 'Paid'. Please specify what the salary is in the text field.");
			Intern.submit.global.gotoTab(3);
			$("[name='int_salary']").focus();
			return false;
		}
		
		// if you select hasLocation = T, then city is required 
		// (there is no null option for countries, and state/region is not always applicable.)
		if(
			$("[name='int_hasLocation']:checked").val() == "T"
			&&
			$("[name='int_city']").val() == ""
		){
			alert("You have indicated that the work is to be performed at a specific location.\nPlease enter the name of the closest city.");
			Intern.submit.global.gotoTab(3);
			$("[name='int_city']").focus();
			return false;
		}
		
		if(
			$("[name='app_hasDeadline']:checked").val() == "T"
			&&
			(
				$("[name='app_deadlineDate']").val() == ""
				||
				$("[name='app_deadlineDate']").val() == "Select Date"
			)
		){		
			alert("You have indicated that there is a deadline for the application.\nPlease give the date for this deadline.");
			Intern.submit.global.gotoTab(4);
			$("[name='app_deadlineDate']").focus();
			return false;
		}		
		//alert("dependency check ok!");
		return true;
  },
  
  //Geocode location and store in form for submission to database
  geocodeIntLocation: function(){
    var hasLocation = $("input[name='int_hasLocation']").val();
    var displayMap 	= $("input[name='int_displayMap']").val();
    
    if(hasLocation == 'T' && displayMap == 'T'){
      //Get form internship location values
      var int_country = $("[name='int_country'] option:selected").val();
      var int_region;
      if(jQuery.trim(int_country) == 'USA'){
        int_region = $("[name='int_regionSelect'] option:selected").val();
      }else{
        int_region = $("input[name='int_regionText']").val();
      }
      var int_city = $("[name='int_city']").val();
      
      //geocode location values and store geocode in form
      geocoder = new GClientGeocoder();
      geocoder.getLocations("'" + int_city + ", " + int_region + ", " + int_country + "'", function(responce){
        if(responce && responce.Status.code == 200){
          place = responce.Placemark[0];
          lat 	= place.Point.coordinates[1];
          lng 	= place.Point.coordinates[0];
          $("input[name='int_geocode']").val(lat + "," + lng);
          
          //submit form when responce comes back from google
          var form = $("form[name='internForm']");
          form.submit();
          return true;
        }else{
          //if don't get a responce back from google then don't use the google map feature
          // rewrote the way map errors are handled, jul 1 2008, martin w
		  var full_location = mw_trim(int_country +" "+ int_region +" "+ int_city)  
		  alert("GoogleMaps could not find the location '" + 
		  full_location +
		  "'. Please try entering the name of the location differently, or uncheck 'Display Map'.");
          Intern.submit.global.gotoTab(3);
		  $("input[name='int_displayMap']").focus();
		  return false;
        }
      });
    }
  }
}