JobSearch = {};

JobSearch.states = new Array("AL","ALABAMA","AK","ALASKA","AS","AMERICAN SAMOA","AZ","ARIZONA","AR","ARKANSAS","CA","CALIFORNIA",
"CO","COLORADO","CT","CONNECTICUT","DE","DELAWARE","DC","DISTRICT OF COLUMBIA","FM","FEDERATED STATES OF MICRONESIA","FL","FLORIDA",
"GA","GEORGIA","GU","GUAM","HI","HAWAII","ID","IDAHO","IL","ILLINOIS","IN","INDIANA","IA","IOWA","KS","KANSAS","KY","KENTUCKY","LA",
"LOUISIANA","ME","MAINE","MH","MARSHALL ISLANDS","MD","MARYLAND","MA","MASSACHUSETTS","MI","MICHIGAN","MN","MINNESOTA","MS",
"MISSISSIPPI","MO","MISSOURI","MT","MONTANA","NE","NEBRASKA","NV","NEVADA","NH","NEW HAMPSHIRE","NJ","NEW JERSEY","NM","NEW MEXICO",
"NY","NEW YORK","NC","NORTH CAROLINA","ND","NORTH DAKOTA","MP","NORTHERN MARIANA ISLANDS","OH","OHIO","OK","OKLAHOMA","OR","OREGON",
"PW","PALAU","PA","PENNSYLVANIA","PR","PUERTO RICO","RI","RHODE ISLAND","SC","SOUTH CAROLINA","SD","SOUTH DAKOTA","TN","TENNESSEE",
"TX","TEXAS","UT","UTAH","VT","VERMONT","VI","VIRGIN ISLANDS OF THE U.S.","VA","VIRGINIA","WA","WASHINGTON","WV","WEST VIRGINIA","WI",
"WISCONSIN","WY","WYOMING");

JobSearch.isValidState = function(state) {
   state = state.toUpperCase();
   for(var i=0; i<JobSearch.states.length; i++) {
      if(state == JobSearch.states[i]) { return true; }
   }
   return false;
}

JobSearch.trim = function(str) {
   str = str.replace(/^\s+/, '');
   str = str.replace(/\s+$/, '');
   return str;
}

JobSearch.procForm = function(which) {
var location = document.forms[which].location.value;
   
   location = location.replace(/\)/g, '');
   location = JobSearch.trim(location.replace(/\(/g, ''));

   if(!location) {
      alert("Please enter your search location" + '\n' + "(City, State) OR (ZIP Code).");
      document.forms[which].location.focus();
      return false;
   }
   if(!location.charAt(0).match(/[0-9]/)) {
      if(!location.match(/,/)) {
         alert("Please enter city and state separated by a comma.");
         document.forms[which].location.focus();
         return false;
      }
      if(location.match(/,$/)) {
         alert("Please enter city and state separated by a comma.");
         document.forms[which].location.focus();
         return false;
      }
      location = location.split(/,/);
      if(location.length != 2) {
         alert("Please enter city and state separated by a comma.");
         document.forms[which].location.focus();
         return false;
      }
      var city  = JobSearch.trim(location[0]);
      var state = JobSearch.trim(location[1]);

      if(!city || !state) {
         alert("Please enter city and state separated by a comma.");
         document.forms[which].location.focus();
         return false;
      }

      if(!JobSearch.isValidState(state)) {
         alert(state + " is not a valid state.");
         document.forms[which].location.focus();
         return false;
      }

      document.forms[which].city.value  = city;
      document.forms[which].state.value = state;

   } else {
      if(location.charAt(0).match(/[0-9]/) && !location.match(/^[0-9][0-9][0-9][0-9][0-9]$/)) {
         alert(location + " is not a 5 digit ZIP Code.");
         document.forms[which].location.focus();
         return false;
      } else {
         document.forms[which].zip.value = location;
      }
   }


   document.forms[which].submit();
   return true;
}
