function focusOn(idElm) {
  var focEl = $(idElm);
  
  if(focEl) {
    focEl.focus();
  }
}


//-----------------------------------------------------------------
// FORMS
//-----------------------------------------------------------------
// hide inputs based on sales/lettings dropdown
function setSalesLettings(section) {
  if(section) {
    var statusTypeIdValue = $(section + '_I_status_type_id').value;
    
    var priceType         = $(section + '_I_price_type_id_wrapper');
    var price             = $(section + '_I_price_wrapper');
    var price2            = $(section + '_I_price_2_wrapper');
    var rent              = $(section + '_I_rent_wrapper');
    var rent2             = $(section + '_I_rent_2_wrapper');
    var furnishedType     = $(section + '_I_furnished_type_id_wrapper');
    var isLeaseholdValue  = ($(section + '_I_is_leasehold') ? $(section + '_I_is_leasehold').value : 0);
    var isLeasehold       = $(section + '_I_is_leasehold_wrapper');
    var lease             = $(section + '_I_lease_wrapper');
    var groundRent        = $(section + '_I_ground_rent_wrapper');
    var serviceCharge     = $(section + '_I_service_charge_wrapper');
    var outsideSpace      = $(section + '_I_outside_space_wrapper');
    var parking           = $(section + '_I_parking_wrapper');
    var eer_current       = $(section + '_I_eer_current_wrapper');
    var eer_potential     = $(section + '_I_eer_potential_wrapper');
    var eir_current       = $(section + '_I_eir_current_wrapper');
    var eir_potential     = $(section + '_I_eir_potential_wrapper');
    
    // sales
    if((statusTypeIdValue == 1)
      || (statusTypeIdValue == 2)
      || (statusTypeIdValue == 3)
      || (statusTypeIdValue == 4)
      || (statusTypeIdValue == 5)) {
      
      if(priceType) {
        priceType.show();
      }
      
      // if there is a price text field - use this to hide/display price
      if(priceType) {
        // if NOT "Price Upon Application"
        if(priceType.value != 4) {
          if(price) {
            price.show();
          }
        }
        else {
          if(price) {
            price.hide();
            price.value = '';
          }
        }
      }
      // otherwise just show it
      else {
        if(price) {
          price.show();
        }
      }
      
      if(price2) {
        price2.show();
      }

      if(rent) {
        rent.hide();
        rent.value = ''
      }
      
      if(rent2) {
        rent2.hide();
        rent2.value = ''
      }
      
      if(furnishedType) {
        furnishedType.hide();
      }
      
      if(isLeasehold) {
        isLeasehold.show();
      }
			
			if((isLeaseholdValue == 1)
				|| (isLeaseholdValue == 2)
				|| (isLeaseholdValue == 3)
				|| (isLeaseholdValue == 4)
				|| (isLeaseholdValue == 5)) {
				
				if(lease) {
					lease.show();
				}
				
				if(groundRent) {
					groundRent.show();
				}
				
				if(serviceCharge) {
					serviceCharge.show();
				}
			}
			else {
				if(lease) {
					lease.hide();
					lease.value = '';
				}
	
				if(groundRent) {
					groundRent.hide();
					groundRent.value = '';
				}
	
				if(serviceCharge) {
					serviceCharge.hide();
					serviceCharge.value = '';
				}
			}
      
      if(outsideSpace) {
        outsideSpace.show();
      }
      
      if(parking) {
        parking.show();
      }
      
      if(eer_current) {
        eer_current.show();
      }
      
      if(eer_potential) {
        eer_potential.show();
      }
      
      if(eir_current) {
        eir_current.show();
      }
      
      if(eir_potential) {
        eir_potential.show();
      }
    }
    
    // lettings
    else {
      if(priceType) {
        priceType.hide();
      }
      
      if(price) {
        price.hide();
        price.value = ''
      }
      
      if(price2) {
        price2.hide();
        price2.value = ''
      }

      if(rent) {
        rent.show();
      }
      
      if(rent2) {
        rent2.show();
      }
      
      if(furnishedType) {
        furnishedType.show();
      }
      
      if(isLeasehold) {
        isLeasehold.hide();
      }
      
      if(lease) {
				lease.hide();
				lease.value = '';
			}

			if(groundRent) {
				groundRent.hide();
				groundRent.value = '';
			}

			if(serviceCharge) {
				serviceCharge.hide();
				serviceCharge.value = '';
			}
      
      if(outsideSpace) {
        outsideSpace.hide();
      }

      if(parking) {
        parking.hide();
      }
      
      if(eer_current) {
        eer_current.hide();
        eer_current.value = '';
      }
      
      if(eer_potential) {
        eer_potential.hide();
        eer_potential.value = '';
      }
      
      if(eir_current) {
        eir_current.hide();
        eir_current.value = '';
      }
      
      if(eir_potential) {
        eir_potential.hide();
        eir_potential.value = '';
      }
    }
  }
}


// double check for archiving/deleting
function archiveDeleteCheck(section) {
  archiveElm = $(section + '_I_archived');
  if(archiveElm && (archiveElm.value == 1)) {
    if(!confirm('Are you sure you wish to archive this ' + section + '?\n\n Click "OK" if this is correct.')) {
      return false;
    }
  }
  
  deleteElm = $(section + '_I_deleted');
  if(deleteElm && (deleteElm.value == 1)) {
    if(!confirm('Are you sure you wish to DELETE this ' + section + '?\n\n Click "OK" if this is correct.')) {
      return false;
    }
  }
  
  return true;
}


// delete an image
function removeImage(imageId) {
  $(imageId + '_fake').value='';
  $(imageId + '_box').innerHTML='(remove)';
  $(imageId + '_box').className='input_image_removed';
}


//-----------------------------------------------------------------
// SEARCH PREFS
//-----------------------------------------------------------------
function toggleShowArchived(elm, section) {
  toggleShowArchivedDeleted(elm, 'archived', section);
}


function toggleShowDeleted(elm, section) {
  toggleShowArchivedDeleted(elm, 'deleted', section);
}


function toggleShowArchivedDeleted(elm, archDel, section) {
  var elm = $(elm);
  if(elm) {
    if(elm.checked) {
      setCookie('show_' + archDel + '_' + section, 1, 20);
    }
    else {
      setCookie('show_' + archDel + '_' + section, 0, 20);
    }

    window.location.href = window.location.href;
  }
}


// toggle search by preferences on/off
function setSearchPrefs(newVal) {
  setCookie('search_prefs', newVal, 20);
  
  window.location.href = window.location.href;
}


// pretend to be another user
function applicantId(applicantId, section) {
  applicantId = parseInt(applicantId);
  
  if(applicantId > 0) {
    setCookie('applicant_id', applicantId, 20);
    
    window.location.href = '/particular_' + section + '.php';
  }
  else {
    setCookie('applicant_id', 0, -20);
    delCookie('applicant_id');
    
    window.location.href = window.location.href;
  }
}


// add to email list for applicant
function toggleEmailList(cookieToken) {
  cookieElm = $(cookieToken);
  
  if(cookieElm) {
    if(cookieElm.checked) {
      setCookie(cookieToken, 1, 20);
    }
    else {
      setCookie(cookieToken, 0, 20);
    }
  }
}


// email list of particulars to applicant
function emailParticulars(applicantId) {
  var checkboxes = new Array();
  checkboxes = document.getElementsByClassName('email_particulars', 'input');
  var particular_ids = '';
  for (var i = 0; i < checkboxes.length; i++) {
    if(checkboxes[i].checked) {
      particular_ids += checkboxes[i].value + '_';
    }
  }
  
  if(particular_ids) {
    window.location.href = '/particular_admin_email_applicant.php?applicant_id=' + applicantId + '&particular_ids=' + particular_ids;
  }
  else {
    alert('No particulars selected.');
  }
}


function getElementsByClassName(className, tag, elm){
  var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
  var tag = tag || "*";
  var elm = elm || document;
  var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
  var returnElements = [];
  var current;
  var length = elements.length;
  for(var i=0; i<length; i++){
    current = elements[i];
    if(testClass.test(current.className)){
      returnElements.push(current);
    }
  }
  return returnElements;
}


//-----------------------------------------------------------------
// COOKIES
//-----------------------------------------------------------------
function getCookie(NameOfCookie) {
  if(document.cookie.length > 0) {
    begin = document.cookie.indexOf(NameOfCookie+" = ");
    if(begin != -1) {
      begin += NameOfCookie.length+1;
      end = document.cookie.indexOf(";", begin);
      if(end == -1) {
        end = document.cookie.length;
      }
      return unescape(document.cookie.substring(begin, end));
    }
  }

  return null;
}


function setCookie(NameOfCookie, value, expiredays) {
  var argumentNames = Array(null, null, null, 'path', 'domain');
  valueString = escape(value);
  
  if(expiredays != null) {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    valueString += "; expires = " + ExpireDate.toGMTString();
  }
  
  for(var i = 3; i < arguments.length; i++) {
    valueString += '; ' + argumentNames[i] + ' = ' + arguments[i];
  }
  
  document.cookie = NameOfCookie + " = " + valueString;
}


function delCookie(NameOfCookie) {
  if(getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + " = " + "; expires = Thu, 01-Jan-70 00:00:01 GMT";
  }
}