/***
 * Application specific functions
 */

// validates vendor search
function validateVendorSearch() {
  var valid = true;
  if ($F('vendor_search_service_id') == '') {
    alert('Please select a service.');
    valid = false;
  }
  /*else if ($F('vendor_search_state_id') != '') {
    if ($F('vendor_search_city_id') == '') {
      alert('Please select a city.');
      valid = false;
    } else if ($F('vendor_search_service_id') == '') {
      alert('Please select a service and city.');
      valid = false;
    }
  }*/
  return valid;
}

// validates job search
function validateJobSearch() {
  var valid = true;
  if ($F('job_search_position_id') == '') {
    alert('Please select a position.');
    valid = false;
  }
  /*else if ($F('job_search_state_id') != '') {
    if ($F('job_search_city_id') == '') {
      alert('Please select a city.');
      valid = false;
    } else if ($F('job_search_position_id') == '') {
      alert('Please select a position and city.');
      valid = false;
    }
  }*/
  return valid;
}

// responds to radio button selection of level type on new vendor
function gotoLevel(x) {
	location.href = 'https://www.legalvendor411.com/vendors/new?level=' + x
	return;
}

// responds to (un)check of (inter)national
function toggleLocations() {
  $F('vendor_national') == 1 || $F('vendor_international') == 1 ? $('locations').hide() : $('locations').show();
  return;
}

// responds to a select change on vendor recommendation
function gotoVendor() {
  location.href = '/vendors/recommend/' + $F('vendor_id')
	return;
}

// checks if legal notice was agreed to on join forms
function checkNotice() {
  return $F('legal_notice') == 1;
}

function getSecondLevelSelect(type, token) { 
  if ($F(type + '_search_state_id') == 53) { // international selected, show countries
    getCountrySelect(type, token);
  }
  else if ($F(type + '_search_state_id') == 54) { // national selected, do nothing
  }
  else { // state selected, show cities
    getCitySelect('state', type, token);
  }
}

// creates city select after choosing state
function getCitySelect(scope, type, token) {
  if (scope == 'state') {
    id = $F(type + '_search_state_id');
  } else {
    id = $F(type + '_search_country_id')
  }
  new Ajax.Request('/states/cities', {asynchronous:true, evalScripts:true, 
                    parameters:'id=' + id + '&scope='+ scope + '&type=' + type + '&authenticity_token=' + encodeURIComponent(token)}
                  );
  return false;
}

// creates city select after choosing state
function getCountrySelect(type, token) {
  new Ajax.Request('/states/countries', 
                   {asynchronous:true, evalScripts:true, 
                    parameters:'type=' + type + '&authenticity_token=' + encodeURIComponent(token)}
                  );
  return false;
}