function checkPlans() {
	if(!document.getElementById("buying").checked && !document.getElementById("selling").checked && !document.getElementById("relocating").checked && !document.getElementById("renting").checked) {
		return false;	
	}	
}


function is_required(key_name){       
    if(req_fields.length < 1 ) return false;                                            
    for(var i=0; i < req_fields.length; i++){ if(req_fields[i] == key_name) return true;}          
    return false;
}


function ValidateForm(theform)
{
    /*
     * MODULE Contact Information
     */
    // validate firstname
	if( is_required('firstname') && theform['firstname'].value=='')
	{
		if(theform['firstname'].focus) theform['firstname'].focus()
		alert('Please enter your firstname.')
		return false
	}
	
	if((theform['firstname'].value.search && theform['firstname'].value.search(new RegExp('^[a-zA-Z ]+$','g'))<0))
	{
		if(theform['firstname'].focus) theform['firstname'].focus()
		alert('The firstname may contain only letters and spaces.')
		return false
	}
		
	// validate lastname
	if(is_required('lastname') && theform['lastname'].value=='')
	{
		if(theform['lastname'].focus) theform['lastname'].focus()
		alert('Please enter your lastname.')
		return false
	}
	
	if((theform['lastname'].value.search && theform['lastname'].value.search(new RegExp('^[a-zA-Z ]+$','g'))<0))
	{
		if(theform['lastname'].focus) theform['lastname'].focus()
		alert('The lastname may contain only letters and spaces.')
		return false
	}
	
	//validate phone
	if(is_required('phone') && theform['phone'].value=='')
	{
		if(theform['phone'].focus) theform['phone'].focus()
		alert('Please enter your phone number.')
		return false
	}
	
	if((theform['phone'].value.search && theform['phone'].value.search(new RegExp('^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$','g'))<0))
	{
		if(theform['phone'].focus) theform['phone'].focus()
		alert('Please enter your VALID phone number (XXX-XXX-XXXX)')
		return false
	}
	
	
	//validate residence state
    if(theform['residence_state'].selectedIndex==-1 || theform['residence_state'].options[theform['residence_state'].selectedIndex].value=='')
	{
		if(theform['residence_state'].focus) theform['residence_state'].focus()
		alert('Please specify the contact state.')
		return false
	}
	
	//validate residence zipcode
    if(theform['residence_zipcode'].value!='' && (theform['residence_zipcode'].value.search	&& theform['residence_zipcode'].value.search(new RegExp('^(,?[0-9]{5}(-[0-9]{4})?)+$','g'))<0))
	{
		if(theform['residence_zipcode'].focus) theform['residence_zipcode'].focus()
		alert('Please enter a VALID contact zipcode')
		return false
	}
		
	/*
	 * MODULE Qualifying Information
	 */
	
	//validate plans
	if(is_required('buying') && (theform['buying'].value=='' || checkPlans(theform['buying'])==false))
	{
		if(theform['buying'].focus)	theform['buying'].focus()
		alert('Please specify your plans.')
		return false
	}
	
	if(theform['selling'].value=='')
	{
		if(theform['selling'].focus) theform['selling'].focus()
		alert('Please specify your plans.')
		return false
	}
	
	if(theform['relocating'].value=='')
	{
		if(theform['relocating'].focus)	theform['relocating'].focus()
		alert('Please specify your plans.')
		return false
	}
	
	if(theform['renting'].value=='')
	{
		if(theform['renting'].focus) theform['renting'].focus()
		alert('Please specify your plans.')
		return false
	}
	
	//validate timeframe
	if(is_required('timeframe') && (theform['timeframe'].selectedIndex==-1 || theform['timeframe'].options[theform['timeframe'].selectedIndex].value==''))
	{
		if(theform['timeframe'].focus)	theform['timeframe'].focus()
		alert('Please enter your expected moving timeframe.')
		return false
	}
	
	// validate working other
	if(is_required('working_other') && (theform['working_other_yes'].checked==false && theform['working_other_no'].checked==false))
	{
		if(theform['working_other_yes'].focus) theform['working_other_yes'].focus()
		alert('Please specify if you are working with another company\'s broker or agent.')
		return false
	}
	
	if(is_required('working_other') && (theform['working_other_yes'].checked==false && theform['working_other_no'].checked==false))
	{
		if(theform['working_other_no'].focus) theform['working_other_no'].focus()
		alert('Please specify if you are working with another company\'s broker or agent.')
		return false
	}
	
	//validate currently listed
	if(is_required('currently_listed') && (theform['currently_listed_yes'].checked==false && theform['currently_listed_no'].checked==false && theform['currently_listed_fsbo'].checked==false))
	{
		if(theform['currently_listed_yes'].focus) theform['currently_listed_yes'].focus()
		alert('Please specify if your current home is already listed for sale on the market.')
		return false
	}
	if(is_required('currently_listed') && (theform['currently_listed_yes'].checked==false && theform['currently_listed_no'].checked==false && theform['currently_listed_fsbo'].checked==false))
	{
		if(theform['currently_listed_no'].focus) theform['currently_listed_no'].focus()
		alert('Please specify if your current home is already listed for sale on the market.')
		return false
	}
	if(is_required('currently_listed') && (theform['currently_listed_yes'].checked==false && theform['currently_listed_no'].checked==false && theform['currently_listed_fsbo'].checked==false))
	{
		if(theform['currently_listed_fsbo'].focus) theform['currently_listed_fsbo'].focus()
		alert('Please specify if your current home is already listed for sale on the market.')
		return false
	}
	
	
	/*
	 * MODULE Home Search Criteria
	 */
	
	//validate hometype
	if(is_required('hometypes') && (theform['hometypes'].selectedIndex==-1 || theform['hometypes'].options[theform['hometypes'].selectedIndex].value==''))
	{
		if(theform['hometypes'].focus) theform['hometypes'].focus()
		alert('Please select property type(s)')
		return false
	}
	
	//validate minprice
	if(is_required('minprice') && (theform['minprice'].selectedIndex==-1 || theform['minprice'].options[theform['minprice'].selectedIndex].value==''))
	{
		if(theform['minprice'].focus) theform['minprice'].focus()
		alert('Please specify the minimum price.')
		return false
	}
	
	//validate maxprice
	if(is_required('maxprice') && (theform['maxprice'].selectedIndex==-1 || theform['maxprice'].options[theform['maxprice'].selectedIndex].value==''))
	{
		if(theform['maxprice'].focus) theform['maxprice'].focus()
		alert('Please specify the maximum price.')
		return false
	}
	
	//validate bedrooms
	if(is_required('bedrooms') && (theform['bedrooms'].selectedIndex==-1 || theform['bedrooms'].options[theform['bedrooms'].selectedIndex].value==''))
	{
		if(theform['bedrooms'].focus) theform['bedrooms'].focus()
		alert('Please specify the number of beds.')
		return false
	}
	//validate bathrooms
	if(is_required('bathrooms') && (theform['bathrooms'].selectedIndex==-1 || theform['bathrooms'].options[theform['bathrooms'].selectedIndex].value==''))
	{
		if(theform['bathrooms'].focus) theform['bathrooms'].focus()
		alert('Please specify the number of baths.')
		return false
	}
	
	// validate city
	if(is_required('city') && theform['city'].value=='')
	{
		if(theform['city'].focus) theform['city'].focus()
		alert('Please enter search city.')
		return false
	}
	
	//validate state
	if(is_required('state') && (theform['state'].selectedIndex==-1 || theform['state'].options[theform['state'].selectedIndex].value==''))
	{
		if(theform['state'].focus) theform['state'].focus()
		alert('Please specify the search state.')
		return false
	}
	
	//validate zipcode
	if(is_required('zipcode') && theform['zipcode'].value=='')
	{
		if(theform['zipcode'].focus) theform['zipcode'].focus()
		alert('Please enter search zipcode.')
		return false
	}
	
	if(theform['zipcode'].value!='' && (theform['zipcode'].value.search	&& theform['zipcode'].value.search(new RegExp('^(,?[0-9]{5}(-[0-9]{4})?)+$','g'))<0))
	{
		if(theform['zipcode'].focus) theform['zipcode'].focus()
		alert('Please enter a VALID search zipcode')
		return false
	}
	
	//validate radius
	if(is_required('radius') && (theform['radius'].selectedIndex==-1 || theform['radius'].options[theform['radius'].selectedIndex].value==''))
	{
		if(theform['radius'].focus)	theform['radius'].focus()
		alert('Please specify the search radius.')
		return false
	}
		
	return true
}

