function check_index_form(form_obj, searchid) {
	var valid_search = false;
	var el = document.getElementById(searchid);
	if( !el ) {
		alert( "Please enter a city, statecode or zipcode to continue." );
		return false;	
	}
	
	el.value = el.value.toString().trim();
	var digits5 = /^.*[0-9]{5}.*$/;
	var digits6 = /^.*[0-9]{6}.*$/;
	if( digits5.test(el.value) && !digits6.test(el.value) ) {
		valid_search = true;
	}
	
	var city_state = /^.*\w+,\s*\w+.*$/;
	if( city_state.test(el.value) ) {
		valid_search = true;
	}

	if( !valid_search ) {
		alert( "Please enter a city, statecode or zipcode to continue." );
		el.focus();
		return false;	
	}
	
	valid_search = false;
	for( i=0; i<form_obj.elements.length; i++ ) {
		if( form_obj[i].checked == 1 ) {
			valid_search = true;
		}
	}

	if( !valid_search ) {
		alert( "Please select at least one area of interest to continue." );
		return false;	
	}
	
	return true;
}

function check_personalize_form(form_obj) {
	return true;
    if( !check_select('min_price', "Please select a minimum price to continue.") ) return false;
    if( !check_select('max_price', "Please select a maximum price to continue.") ) return false;
    if( !check_select('min_bedrooms', "Please select \"Beds\" to continue.") ) return false;
    if( !check_select('min_bathrooms', "Please select \"Baths\" to continue.") ) return false;
    if( !check_select('time_frame', "Please select a time frame to continue.") ) return false;
    if( !check_select('pre_qual', "Please select whether you are pre-approved to continue.") ) return false;
    return true;
}

function check_signup_form() {
	if( !valid('firstname', 'Please enter your name to continue.', 1) ) return false;
	if( !valid('lastname', 'Please enter your name to continue.', 1) ) return false;
	if( !emailCheck('email') ) return false;
    if( !check_select('working_agent', "Please select whether you are already working with an agent to continue.") ) return false;
    
    var el = document.getElementById( 'agree' );
    if(!el || el.checked == false) {
    	alert("You must read and agree to our Terms of Service, Privacy Policy, and Guidelines.");
    	return false;
	}
	    
    return true;
}

function redirect_to_realtor_page(searchid) {
	if( get_query_value('newsearch') != 1 ) {
		//only redirect if ?newsearch=1
		return true;	
	}

	var el = document.getElementById(searchid);
	if( el && el.value && el.value.length > 0 ) {
		window.location.href = "realtor-page.php?search=" + encodeURIComponent(el.value);
		return false;
	}

	return true;
}

function check_select( name, error_message ) {
    if( !document.getElementById(name) || document.getElementById(name).selectedIndex <= 0 ) {
        alert(error_message);
        if( document.getElementById(name) ) {
        	document.getElementById(name).focus();	
		}
		return false;
	}
	return true;				
}

function valid(id, error_msg, min_length) {
    var el = document.getElementById(id);
    if( !el ) 
        return true;

	el.value = el.value.toString().trim();
    if( el.value.length < min_length ) {
        alert(error_msg);
        el.focus();
        return false;
    }
    
    return true;
}

function emailCheck(id) {
	if( !document.getElementById('email') ) {
       alert("Please enter a valid E-mail Address.")
       return false;
    }
    
    var el = document.getElementById('email');
    str = el.value = el.value.toString().trim();
	
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1) {
       alert("Please enter a valid E-mail Address.")
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("Please enter a valid E-mail Address.")
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Please enter a valid E-mail Address.")
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert("Please enter a valid E-mail Address.")
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Please enter a valid E-mail Address.")
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert("Please enter a valid E-mail Address.")
        return false
     }

     if (str.indexOf(" ")!=-1){
        alert("Please enter a valid E-mail Address.")
        return false
     }

     return true
}

function get_query_value( name ) {
    var regex = new RegExp( "[\?&]"+name+"=([^&#]*)" );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

