function trimstring(s){
   var i=0;
   while ((i<s.length) && (s.charAt(i)==' ')){
     i++;
     }
   if (i>0) {s=s.substring(i);}
   i = s.length;
   while ((s.charAt(i-1)==' ') && (i>=0)){
     i--;
     }
   if (i<s.length) { s=s.substring(0,i);}
   return s;
   }

// Standard Netscape function
function isEmail(s){
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")){
      i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != ".")){
      i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

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 isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);} 
var cur_min_rent = 100;
function validate_rent_low(){ 
	var lo = document.getElementById('rent_low');
	var hi = document.getElementById('rent_high');
	if(parseInt(lo.value) > parseInt(hi.value)){
		alert("Minimum rent cannot be higher than Maximum.\nChange is cancelled.");
		lo.value = cur_min_rent;
		return false;
	}else{
		cur_min_rent = lo.value;
		return true;
	}
}
var cur_max_rent = 1000;
function validate_rent_high(){ 
	var lo = document.getElementById('rent_low');
	var hi = document.getElementById('rent_high');
	if((parseInt(lo.value)) > (parseInt(hi.value))){
		alert("Maximum rent cannot be lower than Minimum.\nChange is cancelled.");
		hi.value = cur_max_rent;
		return false;
	}else{
		cur_max_rent = hi.value;
		return true;
	}
}

function validate_search_custom(sForm){ 
	var city = sForm.city.value;
	var state = sForm.state.value;
	var zip = sForm.zip.value;
	if((city == '') && (zip == '')){
		alert("A city OR zip code is required");
		return false;
	}else if((city != '') && (state == '')){
		alert("Please select a state");
		return false;
	}
	return true;
}

function validate_login_timeout(lForm){ 
	var password = lForm.password.value;
	if(password == ''){
		alert("Please enter a password");
		return false;
	}
	return true;
}

function validate_login(lForm){ 
	var username_val = trimstring(lForm.username.value);
	var password_val = trimstring(lForm.password.value);
	if(username_val == ''){
		alert("Please enter a Username");
		return false;
	}else if(password_val == ''){
		alert("Please enter a password");
		return false;
	}
	return true;
}

function validate_inquiry(iForm){ 
	var comment = iForm.comment.value;
	if(comment == ''){
		alert("Please enter a comment");
		return false;
	}
	return true;
}

function validate_reward_find(iForm){ 
	var complex = iForm.complex.value;
	var city = iForm.city.value;
	var state = iForm.state.value;
	if(complex == ''){
		alert("Please enter a portion of the complex name");
		return false;
	}
	if(state == ''){
		alert("Please enter the state where this apartment is located");
		return false;
	}
	return true;
}

function validate_reward(lForm){ 
	var rent = trimstring(lForm.rentamount.value);
	var lease = trimstring(lForm.rentmonth.value);
	var firstname = trimstring(lForm.firstname.value);
	var lastname = trimstring(lForm.lastname.value);
	var address = trimstring(lForm.aptaddress1.value);
	var phone = trimstring(lForm.phone.value);
	var move_date = trimstring(lForm.aptmoveindate.value);
	var reported = lForm.reported.checked;
	if(rent == ''){
		alert("Please enter a value for the rent amount");
		return false;
	}else if(lease == ''){
		alert("Please enter a value for the lease term");
		return false;
	}else if(firstname == ''){
		alert("Please enter your first name");
		return false;
	}else if(lastname == ''){
		alert("Please enter your last name");
		return false;
	}else if(phone == ''){
		alert("Please enter your phone number");
		return false;
	}else if(address == ''){
		alert("Please enter your NEW address");
		return false;
	}else if(move_date == ''){
		alert("Please enter a move in date");
		return false;
	}else if(!IsNumeric(lease)){
		alert("Please enter a Numeric value for number of months in the lease");
		return false;
	}else if(!IsNumeric(rent)){
		alert("Please enter a Numeric value for monthly rent amount");
		return false;
	}else if(!reported){
		alert("Please verify that you understand and agree to the Terms and Conditions required to collect the reward");
		return false;
	}
	return true;
}

function toggleLayer(objId, status) {
    var obj = document.getElementById(objId);
    if (status == "on") 
        obj.style.display = "block";
    else
        obj.style.display = "none";
}

// Filter out troublesome chars in email address.
// This has the unfortunate side-effect of not working
// with cursor re-positioning inside the text field, but
// should be tolerable for a short string like email addr. 
function limitSendAFriendEAddrText(limitField) {
    var flen = limitField.value.length;
    var cchar;
    var ostr = '';
    for (i=0; i<flen; i++){
        cchar = limitField.value.charAt(i);
        if (
            (cchar == ' ')  ||
            (cchar == ',')  ||
            (cchar == '\'') ||
            (cchar == '<')  ||
            (cchar == ';')  ||
            (cchar == '`')  ||
            (cchar == '"')
           ){
            cchar = '';
        }
        ostr = ostr + cchar;
    }
    limitField.value = ostr;
}

function limitSendAFriendMessageText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
	limitField.value = limitField.value.substring(0, limitNum);
    }
}

function has_value(value){
  value = value.replace(/\s/g,'');
  if( (value == undefined) || (value == null) || (value == '') ){
    return false;
  }
  return true;
}

function validate_new_account(){ 
  var ctl = document.getElementById('acct_username');
  if( ! has_value(ctl.value) ){
    alert('Please enter a Username');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  ctl = document.getElementById('acct_email');
  if( ! has_value(ctl.value) ){
    alert('Please enter an Email Address');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  ctl = document.getElementById('acct_password');
  if( ! has_value(ctl.value) ){
    alert('Please enter a password');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  var pass1 = ctl.value;
  ctl = document.getElementById('acct_password_verify');
  if( ! has_value(ctl.value) ){
    alert('Please enter the Password verification');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  if(pass1 != ctl.value){
    alert('Password & Verify Password are different, please enter the same value for your password and the Password Verification');
    ctl.value = '';
    ctl = document.getElementById('acct_password');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  return true;
}

function validate_edit_account(){ 
  ctl = document.getElementById('acct_email');
  if( ! has_value(ctl.value) ){
    alert('Please enter an Email Address');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  ctl = document.getElementById('acct_password');
  if( ! has_value(ctl.value) ){
    alert('Please enter a password');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  var pass1 = ctl.value;
  ctl = document.getElementById('acct_password_verify');
  if( ! has_value(ctl.value) ){
    alert('Please enter the Password verification');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  if(pass1 != ctl.value){
    alert('Password & Verify Password are different, please enter the same value for your password and the Password Verification');
    ctl.value = '';
    ctl = document.getElementById('acct_password');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  return true;
}

function validate_inquiry_2(){ 
  ctl = document.getElementById('inqName');
  if( ! has_value(ctl.value) ){
    alert('Please enter your name');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  ctl = document.getElementById('inqComment');
  if( ! has_value(ctl.value) ){
    alert('Please enter a comment');
    ctl.value = '';
    ctl.focus();
    return false;
  }
  return true;
}

function toggleAdvSearch(){
  mainDiv = document.getElementById('searchForm');
  myDiv = document.getElementById('advSearch');
  arrow = document.getElementById('as-arrow');
  if(myDiv.style.display == 'block'){
    myDiv.style.display = 'none';
    mainDiv.style.backgroundImage = "url(/images/2008/search-customizer-fill.jpg)";
    arrow.src = '/images/2008/advanced-search.gif';
    ASCookie.searchAdvanced = false;
  }else{
    myDiv.style.display = 'block';
    mainDiv.style.backgroundImage = "url(/images/2008/search-customizer-fill2.jpg)";
    arrow.src = '/images/2008/advanced-search-down.gif';
    ASCookie.searchAdvanced = true;
  }
  setCookie();
  return false;
}

