 //this method will remove the white spaces from both in front of and behind text -Brad
  function trim(string) {
      var returnString = "";
      var length = string.length;
      var textFound = false;
      var whitespace_queue = "";
      for(var i = 0; i<length; ++i) {
         if ((!string.charAt(i).match(" ")) && (!textFound)) {
            returnString = returnString + whitespace_queue;
            whitespace_queue = "";
            returnString = returnString + string.charAt(i);
            textFound = true;
         }
         else if ((!string.charAt(i).match(" ")) && (textFound)) {
            returnString = returnString + string.charAt(i);
         }
         else if ((string.charAt(i).match(" ")) && (textFound)) {
            whitespace_queue = whitespace_queue + string.charAt(i);
            textFound = false;
         }
      }
      return returnString;
   }

   function checkVal(str) {
      if ((str == null) || (trim(str) == "")) {
         return true;
      }
      else {
         return false;
      }
   }

   function isNumeric(sText) {
      var ValidChars = "0123456789.";
      var IsNumber=true;
      var Char;
      for (i = 0; i < sText.length && IsNumber == true; i++) {
         Char = sText.charAt(i);
         if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
         }
      }
      return IsNumber;
   }

function formatCurrency( num ) {
        var isNegative = false;
        num = num.toString().replace(/\$|\\,/g,'');
        if( isNaN( num ) ) {
          num = "0";
        }
        if ( num < 0 ) {
          num = Math.abs( num );
          isNegative = true;
        }
        cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
        num = Math.floor( ( num * 100 + 0.5 ) / 100 ).toString();
        if ( cents < 10 ) {
          cents = "0" + cents;
        }
        for ( i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++) {
          num = num.substring( 0 ,num.length - ( 4 * i + 3 ) ) + ',' + num.substring( num.length - ( 4 * i + 3 ) );
        }

        var result = '$' + num + '.' + cents;
        if ( isNegative ) {
          result = "-" + result;
        }
        return result;
      }

 function checkForm(f, bPressed) {
      if (checkVal(f.username.value)) {
          alert('UserName is Required');
          focus(f.username);
      }

      else if (checkVal(f.password.value)) {
          alert('Password is Required');
          focus(f.userpass);
     }
     else {
        f.buttonPressed.value=bPressed;
        f.submit();
     }
  }
  function checkLocationForm(f, bPressed) {
      if (checkVal(f.location.value)) {
           alert('Location is Required'); 
           focus(f.location); 
      }
      else {
       f.buttonPressed.value=bPressed;
        f.submit();
     }
  }

function submitForm(f, bPressed) {
  f.buttonPressed.value=bPressed;
  f.submit();
}

   function checkLandlordForm(f, bPressed) {
      if (checkVal(f.name.value)) {
           alert('Name is Required'); 
           focus(f.name); 
      }
      else {
       f.buttonPressed.value=bPressed;
        f.submit();
     }
  }

 function checkPropertyMaintenance(f, bPressed) {
    if (checkVal(f.price.value)) {
       alert('Price is Required');
       focus(f.price);
    }
    else if (checkVal(f.bedrooms.value)) {
       alert('# of Bedrooms is Required');
       focus(f.bedrooms);
    }
    else if (checkVal(f.bathrooms.value)) {
       alert('# of Bathrooms is Required');
       focus(f.bathrooms);
    }
    else if (checkVal(f.landlord_id.value)) {
       alert('Select a landlord');
       focus(f.landlord_id);
    }
    else if (checkVal(f.location_id.value)) {
       alert('Select a location');
       focus(f.location_id);
    }
    else if (checkVal(f.dateListed.value)) {
       alert('Date Listed is Required');
       focus(f.dateListed);
    }
    else if (checkVal(f.dateAvailable.value)) {
       alert('Date Available is Required');
       focus(f.dateAvailable);
    }
    else if (f.parking.value=='Y' && (f.parkingSpaces.value=='' || f.parkingSpaces.value=='0')) {
       alert('# Parking Spaces is required when parking is available');
       focus(f.parkingSpaces);
    }
    else if (!isNumeric(f.bedrooms.value)) {
       alert('Bedrooms must be a numberic value');
       focus(f.bedrooms);
    }
    else if (!isNumeric(f.price.value)) {
       alert('Price must be a numberic value');
       focus(f.bedrooms);
    }
    else {
      if (f.parking.value=='N') {
         f.parkingSpaces.value = '';
         f.parkingPrice.value = '';
      }
      f.price.value=Math.round(Math.ceil(f.price.value));
      f.bedrooms.value=Math.round(Math.ceil(f.bedrooms.value));
      submitForm(f, bPressed); 
    }
 }

 function checkDelete(f) {
    if (confirm("Are you sure you want to delete this property?")) {
       submitForm(f, 'remove');
    }
 }


 function clearSearch(f) {
   f.minDate.value='';
   f.maxDate.value='';
   f.bedrooms.value='';
   f.bathrooms.value='';
   f.minPrice.value='';
   f.maxPrice.value='';
   f.asap.checked=false;
   f.landlord_id.value = '';
   f.location_id.value = '';
   f.rented.checked=false;
   f.orderBy.value='price';
   var objCheckBoxes = f.elements['amenity_id'];
   var countCheckBoxes = objCheckBoxes.length;
   for(var i = 0; i < countCheckBoxes; i++)
      objCheckBoxes[i].checked = false;
 }

function selectAll(f, bPressed) {
  var objCheckBoxes = f.elements['property_id'];
  var countCheckBoxes = objCheckBoxes.length;
  if(!countCheckBoxes)
    objCheckBoxes.checked = 'Y';
  else
    // set the check value for all check boxes
    for(var i = 0; i < countCheckBoxes; i++)
      objCheckBoxes[i].checked = 'Y';
  submitForm(f, bPressed);
}
