// JavaScript Document

var hh = {
	getAreas:function(e) {
		var id = $(e).options[$(e).selectedIndex].value;
		if(id != 0) {
			new Ajax.Request('./properties/list_areas', { 
				method: 'post',
				parameters: {island_id: id},
				onCreate: function() {
					$('SearchAreaId').options.length = 0;
					$('SearchAreaId').options[0] = new Option('Loading ...','');
				},
				onSuccess: function(transport,json) {
					if(json.data.length > 0) {
						count = 0;
						json.data.each(function(itemObj) {
							$('SearchAreaId').options[count] = new Option(itemObj.name.unescapeHTML(),itemObj.id);
							count += 1;
						});						
					} else {
						$('SearchAreaId').options[0] = new Option('Island Has No Areas!','0');
					}
				},
				onFailure: function() { alert('Something went wrong...'); },
				onComplete: function() { 
					$('SearchCityId').options.length = 0;
					$('SearchCityId').options[0] = new Option('Please Select a Area','0');
				}
			});
		} else {
			$('SearchAreaId').options.length = 0;
			$('SearchAreaId').options[0] = new Option('Please Select a Island','0');
			$('SearchCityId').options.length = 0;
			$('SearchCityId').options[0] = new Option('Please Select a Area','0');
		}
	},
	getCities:function(e) {
		var id = $(e).options[$(e).selectedIndex].value;
		if(id != 0) {
			new Ajax.Request('./properties/list_cities', { 
				method: 'post',
				parameters: {area_id: id},
				onCreate: function() {
					$('SearchCityId').options.length = 0;
					$('SearchCityId').options[0] = new Option('Loading ...','');
				},
				onSuccess: function(transport,json) {
					if(json.data.length > 0) {
						count = 0;
						json.data.each(function(itemObj) {
							$('SearchCityId').options[count] = new Option(itemObj.name.unescapeHTML(),itemObj.id);
							count += 1;
						});
					} else {
						$('SearchCityId').options[0] = new Option('Island Has No Areas!','0');
					}
				},
				onFailure: function() { alert('Something went wrong...'); },
				onComplete: function() {  }
			});
		} else {
			$('SearchCityId').options.length = 0;
			$('SearchCityId').options[0] = new Option('Please Select a Area','0');
		}
	},
	updateMessage:function(div) {
		$(div).innerHTML = 'Loading ...';
	},
	showEditNotes:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show Form
		$(edit).show();
	},
	hideEditNotes:function(e, display, edit, msg) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	showService:function(e, id, info, list) {
		// Hide List
		$(list).hide();
		// Show info
		$(info).show();		
		// Update Info
		new Ajax.Updater(info,'../view_service_details', {
			parameters: {service_id: id},
			onCreate: function() {
				$(info).innerHTML = '<div class="red align-center pad-20"><strong>Loading ...</strong></div>';
			},
			onLoading: function(request) { },
			onComplete:function(request) { }
		});
	},
	hideService:function(e, info, list) {
		// Hide info
		$(info).hide();
		// Show List
		$(list).show();
	},
	removeRequest:function(e, id) {
		new Ajax.Request('../delete_request', { 
			method: 'post', 
			parameters: {request_id:id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						new Effect.DropOut($(e),{duration:1.0,afterFinish: function() { alert('The Request has been succesfully deleted!'); location.reload(); }});
						break;
					default:
						alert('Error while attempting to delete selected Request!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); }
		});
	},
	showRequestDetails:function(e, id, edit, list, form, msg) {
		// Hide List
		$(list).hide();
		// Show info
		$(edit).show();	
		// Update Loading msg
		$(msg).innerHTML = '<div class="red align-center pad-20"><strong>Loading ...</strong></div>';
		// Update Info
		new Ajax.Request('../edit_request_details', { 
			method: 'post', 
			parameters: {request_id: id},
			onSuccess: function(transport,json){
				if(json.data.length > 0) {
					json.data.each(function(itemObj) {
						$('PropertyHistoryRequestId').value = itemObj.id.unescapeHTML();
						$('PropertyHistoryRequestDetails').value = itemObj.request_details.unescapeHTML();
					});
				}
				$(form).style.visibility = 'visible';
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	},
	hideRequestDetails:function(e, edit, list, form, msg) {
		// Show display
		$(list).show();
		// Hide form
		$(edit).hide();
		// Hide form
		$(form).style.visibility = 'hidden';
		$('PropertyHistoryRequestId').value = '';
		$('PropertyHistoryRequestDetails').value = '';
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	requestNotify:function(e, id) {
		new Ajax.Request('../request_notification', { 
			method: 'post', 
			parameters: {request_id: id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						alert('Your notification has been sent successfully!');
						break;
					default:
						alert('Error while attempting to notify Hawaii Hideaways of selected Request!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	},
	showEditProfile:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show Form
		$(edit).show();
	},
	hideEditProfile:function(e, display, edit, msg) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	displayCalendar:function(m, y) {
		new Ajax.Updater('calendar-wrap','../calendar', {
			parameters: {month: m, year: y},
			onCreate: function() { },
			onLoading: function(request) { },
			onComplete:function(request) { }
		});
	},
	showAddDate:function(e, add) {
		$(add).show();
	},
	hideAddDate:function(e, add, msg) {
		$(add).hide();
	},
	addDate:function() {
		new Ajax.Updater('calendar-wrap','../add_date', {
			asynchronous:true, 
			evalScripts:true,
			onCreate:function() { $('update-msg').innerHTML = 'Loading ...'; },
			onComplete:function(request, json) {
				hh.hideAddDate(this,'add-date','update-msg');
				Form.reset('add_date-frm');
			}, 
			parameters:Form.serialize('add-date-frm'), 
			requestHeaders:['X-Update', 'calendar-wrap']
		})
	},
	editDate:function(update, form, display, edit) {
		new Ajax.Updater(update,'../edit_date_description', {
			asynchronous:true, 
			evalScripts:true,
			onCreate:function() { $('update-msg').innerHTML = 'Loading ...'; },
			onComplete:function(request, json) {
				hh.hideEditDateDescription(this, display, edit,'update-msg');
				Form.reset(form);
			}, 
			parameters:Form.serialize(form), 
			requestHeaders:['X-Update', update]
		})
	},
	showEditDateDescription:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show edit
		$(edit).show();
	},
	hideEditDateDescription:function(e, display, edit, msg) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	deleteDate:function(id, m, y) {
		new Ajax.Request('../delete_date', { 
			method: 'post', 
			parameters: {date_id: id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						alert('Your availability date has been deleted successfully!');
						new Ajax.Updater('calendar-wrap','../calendar', {
							parameters: {month: m, year: y},
							onCreate: function() { },
							onLoading: function(request) { },
							onComplete:function(request) { }
						});
						break;
					default:
						alert('Error while attempting to delete your availability date!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	}
}