
var Signup = {
	validate: function(frm, isUpdate) {
		var error = false;
		var msg = $('signup-message');
		msg.innerHTML = '';
		
		if(frm.fname.value == '')
			error = true;
		
		if(frm.lname.value == '')
			error = true;
		
		if(frm.email.value == '')
			error = true;

		if(frm.home_phone.value == '')
			error = true;
		
		if(frm.password.value == '' && !isUpdate)
			error = true;
		
		if(!error && frm.password.value != frm.password2.value) {
			msg.innerHTML = '* The passwords you entered do not match. <br/>Please re-enter your password.';
		} else if(error) {
			msg.innerHTML = '* All fields are required';
		} else {
			Register.request();
		}		
	}
}

var RecentlyViewed = {
	request: function() {
		
	},
	
	response: function() {
		
	}
}

var Login = {
	request: function() {
		Form.disable('login');
		Ajax.postForm('login', Login.response);
	},
	
	response: function(r) {
		Form.enable('login');

		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
		
		if(r.json.error) {
			alert(r.json.error);
			return;
		}
		
		document.location.href = document.location.href;

	}
}

var Register = {

	request: function(isup) {
		Form.disable('frmSignup');
		Ajax.postForm('frmSignup', Register.response);
	},
	
	response: function(r) {
		Form.enable('frmSignup');
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
		
		if(r.json.error) {
			alert(r.json.error);
		} else {
			window.location.reload();
		}
	}
}

var Comments = {
	save: function(ln, mls_id, comments) {
		Ajax.post('/webservices/saveComment', { ln: ln, mls_id : mls_id, comments: comments }, Comments.save_response);
	},
	
	save_response: function(r) {
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
		/* alert('Your comments have been saved'); */
	},
	
	email: function(ln, mls_id, comments) {
		Ajax.post('/webservices/emailComment', { ln: ln, mls_id : mls_id, comments: comments }, Comments.email_response);
	},
	
	email_response: function(r) {
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
		
		alert('Your comments have been saved');
		/* alert('You comments have been emailed to your agent'); */
	
	}
}

var Counties = {
	
	request: function(o, obj) {
	
		var opt = obj;
		opt.options.length = 0;
		opt.options[opt.options.length] = new Option('Loading...', '');

		var stateIds = new Array();
		for(var i = 0; i < o.options.length; i++) {
			if(o.options[i].selected == true) {
				stateIds.push(o.options[i].value);
			}
		}
		if(stateIds[0] == '') {
			opt.length = 0;
			opt.options[opt.options.length] = new Option('-- All Counties --', '');
			return false;
		}
		
		Ajax.post('/webservices/getCountiesByState', { ids: stateIds.join(', ') }, Counties.response, obj);

	},

	response: function(r) {
		if(r.failure || !r.json) {
            alert('[state]An error occured!');
            return;
        }
		
		this.options.length = 0;
		this.options[this.options.length] = new Option('-- All Counties --', '');
        for(var prop in r.json) {
			this.options[this.options.length] = new Option(r.json[prop], prop);
        }
        
	}
	
}

var Cities = {
	
	request: function(o, obj, state) {
	
		var opt = obj;
		opt.options.length = 0;
		opt.options[opt.options.length] = new Option('Loading...', '');

		if(state) {
    		Ajax.post('/webservices/getCitiesByState', { 'state' : state }, Cities.response, obj);
		  
		} else {
    		var countyIds = new Array();
    		for(var i = 0; i < o.options.length; i++) {
    			if(o.options[i].selected == true) {
    				countyIds.push(o.options[i].value);
    			}
    		}
    		if(countyIds[0] == '') {
    			opt.length = 0;
    			opt.options[opt.options.length] = new Option('-- All Cities --', '');
    			return false;
    		}
    		
    		Ajax.post('/webservices/getCitiesByCounty', { ids: countyIds.join(', ') }, Cities.response, obj);
		}
	},

	response: function(r) {
		if(r.failure || !r.json) {
            alert('[county]An error occured!');
            return;
        }
		
		this.options.length = 0;
		this.options[this.options.length] = new Option('-- All Cities --', '');
        for(var prop in r.json) {
			this.options[this.options.length] = new Option(r.json[prop], prop);
        }
        
	}
	
}

var Communities = {
	request: function(o, obj) {
	
		var opt = obj;
		opt.options.length = 0;
		opt.options[opt.options.length] = new Option('Loading...', '');

		var cityIds = new Array();
		for(var i = 0; i < o.options.length; i++) {
			if(o.options[i].selected == true) {
				cityIds.push(o.options[i].value);
			}
		}
		if(cityIds[0] == '') {
			opt.length = 0;
			opt.options[opt.options.length] = new Option('-- All Neighborhoods --', '');
			return false;
		}
		
		Ajax.post('/webservices/getCommunitiesByCity', { ids: cityIds.join(', ') }, Communities.response, obj);

	},

	response: function(r) {
		if(r.failure || !r.json) {
            alert('[city]An error occured!');
            return;
        }
		
		this.options.length = 0;
		this.options[this.options.length] = new Option('-- All Neighborhoods --', '');
        for(var prop in r.json) {
			this.options[this.options.length] = new Option(r.json[prop], prop);
        }
        
	}

}

function confirmDeleteSearch() {
	var conf = confirm("Are you sure you would like to delete this search?");
	return conf;
}

function confirmDeleteAlert() {
	var conf = confirm("Are you sure you would like to delete this alert?");
	return conf;
}

var SendToFriend = {
	send: function(frm) {
		Form.disable('emailfriend');
		Ajax.postForm('emailfriend', SendToFriend.send_response, frm);
	},
	
	send_response: function(r) {
		Form.enable('emailfriend');
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
	
		alert('This listing was successfully sent to ' + this.email.value);

		this.email.value = '';
		this.comments.value = '';
		
	}
}

