// --------------------------------------------------------
// --- Return list of schools for given zip code in options
function checkZip()
{
	if($F('reg_zip').length == 5)
	{
		$('school_loading').show();
		var ajax = new Ajax.Updater({success: 'reg_school_wrapper'}, 'http://www.spotsie.com/includes/getschools.php', { method: 'post', parameters: 'zip=' + $F('reg_zip'), onComplete:function(request){ $('school_loading').hide(); }});
		// proper IE workaround for selection/innerHTML bug
		//var ajax = new Ajax.Updater({success: 'reg_school'}, 'http://www.spotsie.com/includes/getschools.php', {asynchronous:true, evalScripts:true, method: 'post', parameters: 'zip=' + $F('reg_zip'), onComplete:function(request){ $('school_loading').hide(); }});
	}
}

// --------------------------------------------------------
// --- Return list of schools for given zip code in options 
// ---  for the Browse page
function checkZipBrowse()
{
	if($F('f_zip').length == 5)
	{
		$('school_loading').show();
		var ajax = new Ajax.Updater({success: 'f_school_wrapper'}, 'http://www.spotsie.com/includes/getschools_browse.php', { method: 'post', parameters: 'zip=' + $F('f_zip'), onComplete:function(request){ $('school_loading').hide(); }});
		// proper IE workaround for selection/innerHTML bug
		//var ajax = new Ajax.Updater({success: 'reg_school'}, 'http://www.spotsie.com/includes/getschools_browse.php', {asynchronous:true, evalScripts:true, method: 'post', parameters: 'zip=' + $F('reg_zip'), onComplete:function(request){ $('school_loading').hide(); }});
	}
}

// --------------------------------------------------------
// --- Load Google Map for maps page of property (with search)
function gmapload(address) {
	var options = {
            	zoomControl : GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL,
            	idleMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM+1,
            	activeMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM+1,
            	showResultList : document.getElementById("results")
            	}

	new GSmapSearchControl(
            	document.getElementById("mapsearch"),
           	address,
            	options
            	);
}

// --------------------------------------------------------
// --- Add additional feature input to listings page
function addFeature(id)
{
	var new_id = id + 1;
	var new_item = '<input type="text" value="" name="extras[]" id="extras_'+new_id+'" size="45" class="input" style="margin-bottom: 3px;" /><a href="javascript:void(0);" onClick="addFeature('+new_id+');" id="list_add_feature_'+new_id+'"><img src="images/list_add_feature.gif" alt="Add another feature" width="16" height="16" class="list_add_feature" /></a>\n';
	var new_image = '<a href="javascript:void(0);" onClick="removeFeature('+id+');" id="list_add_feature_'+id+'"><img src="images/list_remove_feature.png" alt="Remove this feature" width="16" height="16" class="list_add_feature" /></a>';
	Element.remove($('list_add_feature_' + id));
	if(id != 0)
		new Insertion.After($('extras_' + id), new_image);
	new Insertion.Bottom($('extras_dd'), new_item);
}

// --------------------------------------------------------
// --- Remove additional feature input to listings page
function removeFeature(id)
{
	Element.remove($('extras_' + id));
	Element.remove($('list_add_feature_' + id));
}

// --------------------------------------------------------
// --- start callback on photo upload
function startCallback()
{
	Element.show($('photo_upload_progress'));
	return true;
}

// --------------------------------------------------------
// --- complete callback on photo upload
function completeCallback(response)
{
	Element.hide($('photo_upload_progress'));
	//Form.Element.enable('photo_upload_button');
	$('input_upload_photo').value = '';

	// check if response has error flag set, if so display error instead of adding image
	if(response.substr(0,11) == "AJAX_ERROR:") {
		alert(response.substr(11));
		return true;
	}

	var tables = document.getElementsByTagName("table");
	if(tables.length == 0 && response != '')
		$('photos_dd').innerHTML = '';

	// add image to the bottom of the list
	new Insertion.Bottom($('photos_dd'), response);
	var tables = document.getElementsByTagName("table");
	if(tables.length >= 4)
		Form.disable('photo_upload');
	return true;
}

// --------------------------------------------------------
// --- remove a photo from the upload queue
function removeListPhoto(pathname, filename)
{
	photo = 'photo_' + pathname;
	Element.remove($(photo));
	var tables = document.getElementsByTagName("table");
	if(tables.length < 4)
		Form.enable('photo_upload');
	if(tables.length == 0)
		$('photos_dd').innerHTML = '(No photos)';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/removephoto.php', { method: 'post', parameters: 'image=' + pathname + '&filename=' + filename });
}

// --------------------------------------------------------
// --- go to the next photo in the preview pane
function nextImage(image_array)
{
	image = document["imageThumb"].src;
	image = image.replace("_m", "_l");
	for (i = 0; i < image_array.length; i++) {
		if("http://www.spotsie.com/photos/" + image_array[i] == image) {
			loc = i + 1;
			break;
		}
	}
	if (loc >= image_array.length)
		loc = 0;
	$('image_preview').innerHTML = '<img name="imageThumb" src="http://www.spotsie.com/photos/' + image_array[loc].replace("_l", "_m") +'" alt="" />';
	$('image_counter').innerHTML = loc + 1 + " / " + image_array.length;
	$('image_preview').href = 'http://www.spotsie.com/photos/' + image_array[loc];
}

// --------------------------------------------------------
// --- go to the previous photo in the preview pane
function prevImage(image_array)
{
	image = document["imageThumb"].src;
	image = image.replace("_m", "_l");
	for (i = 0; i < image_array.length; i++) {
		if("http://www.spotsie.com/photos/" + image_array[i] == image) {
			loc = i - 1;
			break;
		}
	}
	if (loc < 0)
		loc = image_array.length - 1;
	$('image_preview').innerHTML = '<img name="imageThumb" src="http://www.spotsie.com/photos/' + image_array[loc].replace("_l", "_m") +'" alt="" />';
	$('image_counter').innerHTML = loc + 1 + " / " + image_array.length;
	$('image_preview').href = 'http://www.spotsie.com/photos/' + image_array[loc];
}

// --------------------------------------------------------
// --- Save a property for a user
function saveProperty(pid)
{
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=save&pid=' + pid, onComplete:function(request){ if(request.responseText) { alert(request.responseText); } else { $('user_favorite').innerHTML='<img src="images/icons/save_gray.png" alt="Save as Favorite" style="padding-right: 7px; vertical-align: bottom;" id="save_img" height="16" width="16">Property Saved!'; new Effect.Highlight('user_favorite', {startcolor: '#FFF4AC', endcolor:'#FFFFFF' }); } }});
}

// --------------------------------------------------------
// --- Remove a saved property for a user
function removeProperty(pid)
{
	if(!confirm('Are you sure you want to remove this saved listing?'))
		return 0;
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=remove&pid=' + pid, onComplete:function(request){ if(request.responseText) { alert(request.responseText); } else { Effect.Fade("property_" + pid); Effect.Fade("property_" + pid + "_d"); Effect.Fade("property_" + pid + "_s"); } }});
}

// --------------------------------------------------------
// --- Remove a friend for a user
function removeFriend(fid)
{
	self.onerror = function() { return true; }
	if(!confirm('Are you sure you want to remove this person from your friends list?'))
		return 0;
       Effect.Fade("friend_" + fid); 
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=removefriend&fid=' + fid, onComplete:function(request){ if(request.responseText) { Effect.Appear("friend_" + fid); alert(request.responseText); } }});
}

// --------------------------------------------------------
// --- Edit user school and zip in profile
function profileEditSchool()
{
	$('profile_edit_school').innerHTML = '<input value="" name="reg_zip" id="reg_zip" size="5" maxlength="5" class="input" style="width: 40px;" onkeyup="checkZip();" type="text">' +
						   '<div id="reg_school_wrapper" style="display: inline;">' +
							'<select name="reg_school" id="reg_school" style="width: 220px;"><option value="">(Enter school zip to the left)</option></select>' +
						   '</div>&nbsp;<img id="school_loading" src="images/loading.gif" style=\"display:none;\" alt="Loading..." height="16" width="16">';
}

// --------------------------------------------------------
// --- Toggle the Browse search bar
function toggleBrowseSearch(id, type, tab_val)
{
	default_val = $('search_main').value;
	if(id == 0) tab_0 = ' class="browse_tab_on"'; else tab_0 = '';
	if(id == 1) tab_1 = ' class="browse_tab_on"'; else tab_1 = '';
	if(id == 2) tab_2 = ' class="browse_tab_on"'; else tab_2 = '';

	$('browse_tabs').innerHTML = '<a id="toggle_0" href="javasc' + 'ript:vo' + 'id(0);" onclick="toggleBrowseSearch(0, \'school\', \'school\');"' + tab_0 +'>School</a>' +
					  '<a id="toggle_1" href="javasc' + 'ript:vo' + 'id(0);" onclick="toggleBrowseSearch(1, \'zip code\', \'zip\');"' + tab_1 +'>Zip code</a>' +
					  '<a id="toggle_2" href="javasc' + 'ript:vo' + 'id(0);" onclick="toggleBrowseSearch(2, \'city and state\', \'city\');"' + tab_2 +'>City and State</a>';
	
	if(id == 0) 		$('browse_input').innerHTML = '<input id="search_tab" name="search_tab" value="school" type="hidden"><input id="search_main" name="search_main" size="50" class="biginput" value="' + default_val + '" type="text">&nbsp;<input value="Search by school!" name="search" id="search_button" class="bigbutton" type="submit">';
	else if(id == 1)	$('browse_input').innerHTML = '<input id="search_tab" name="search_tab" value="zip" type="hidden"><input id="search_main" name="search_main" size="50" class="biginput" value="' + default_val + '" type="text">&nbsp;<input value="Search by zip code!" name="search" id="search_button" class="bigbutton" type="submit">';
	else 			$('browse_input').innerHTML = '<input id="search_tab" name="search_tab" value="city" type="hidden"><input id="search_main" name="search_main" size="40" class="biginput" value="' + default_val + '" type="text">&nbsp;<input name="search_sub" size="4" maxlength="2" value="" class="biginput" type="text">&nbsp;<input value="Search by city/state!" name="search" id="search_button" class="bigbutton" type="submit">'; 
}

// --------------------------------------------------------
// --- AJAX Comment handler check for first post to remove
// ---  instructions on posting
function checkCommentListInstructions() 
{
	if($('comment_instructions'))
		Element.remove($('comment_instructions'));
}

// --------------------------------------------------------
// --- AJAX Comment handler, degrades to php
function submitComment(pid)
{
	Element.show($('comment_loading'));
	$('submit_button').disabled = true;
	title = $('comment_title').value;
	body = $('comment_body').value;
	if(!title || !body) {
		Element.hide($('comment_loading'));
		$('submit_button').disabled = false;
		checkAddReportingMessage('comment_message', 'Sorry, you forgot to fill out one of the form fields! Please fill them both out to add your comment.', 'error');
		return true;
	}
	var sucmsg = 'Thanks for posting! Your comments are incredibly valuable for future tenants and landlords.';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=comment&pid=' + pid + '&title=' + title + '&body=' + body, onComplete:function(request){ if(!request.responseText) { checkAddReportingMessage('comment_message', 'There was a problem adding your comment. Please check the title and message and try again.', 'error'); } else { checkCommentListInstructions(); new Insertion.Bottom('comment_list', request.responseText); checkAddReportingMessage('comment_message', sucmsg, 'success'); new Effect.Highlight('last_comment', {startcolor: '#FFF4AC', endcolor:'#FFFFFF' }); $('last_comment').id = ''; $('comment_title').value = ''; $('comment_body').value = ''; } Element.hide($('comment_loading')); $('submit_button').disabled = false; }});
	return true;
}

// --------------------------------------------------------
// --- General message checker and adder
function checkAddReportingMessage(element, message, type, style)
{
	if(style == 1) style = '';
	else style = 'margin: 10px 0 0 0;';
	
	if(!$(element)) {
		new Insertion.Before('view_results', '<div id="' + element + '" class="' + type + '" style="' + style + '">' + message + '</div>');
		return true;
	}
	$(element).className = type;
	$(element).innerHTML = message;
	return true;
}

// --------------------------------------------------------
// --- Save a search for a user
function saveSearch()
{
	var title = prompt('Enter a title for this search:');
	if(title == null || title == '')
		return 0;
	var success = 'Your search has been saved! You can access it at any time in your <a href="account/searches/">saved searches</a> page.';
	var ajax = new Ajax.Request('http://www.spotsie.com/browse/save/', { method: 'post', parameters: 'ajax=1&save_title=' + escape(title), onComplete:function(request){ if(request.responseText) { checkAddSearchMessage('save_message', request.responseText, 'error'); } else { checkAddSearchMessage('save_message', success, 'success'); } }});
}

// --------------------------------------------------------
// --- Property manager hide a property
function managerHideProperty(pid)
{
	$('hidden_' + pid).innerHTML = '<a href="manager/listings/show/' + pid + '/" onclick="managerUnhideProperty(\'' + pid + '\'); return false;"><img src="images/icons/hide_1.png" alt="Unhide this property" title="Unhide this property" height="16" width="16" style="margin: 0 0 0 5px; padding: 0; border: none;" /></a>';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=managerhide&pid=' + pid, onComplete:function(request){ if(request.responseText) { checkAddReportingMessage('manager_message', request.responseText, 'error', 1); } }});
}

// --------------------------------------------------------
// --- Property manager unhide a property
function managerUnhideProperty(pid)
{
	$('hidden_' + pid).innerHTML = '<a href="manager/listings/hide/' + pid + '/" onclick="managerHideProperty(\'' + pid + '\'); return false;"><img src="images/icons/hide_0.png" alt="Hide this property" title="Hide this property" height="16" width="16" style="margin: 0 0 0 5px; padding: 0; border: none;" /></a>';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=managerunhide&pid=' + pid, onComplete:function(request){ if(request.responseText) { checkAddReportingMessage('manager_message', request.responseText, 'error', 1); } }});
}

// --------------------------------------------------------
// --- Property manager rent out a property
function managerRentProperty(pid)
{
	var success = 'Your property is now marked as "occupied." It will still be viewable by anyone, but will no longer be listed as currently available to be rented.';
	$('rented_' + pid).innerHTML = '<a href="manager/listings/open/' + pid + '/" onclick="managerUnrentProperty(\'' + pid + '\'); return false;"><img src="images/icons/rented_0.png" alt="Set as currently vacant" title="Set as currently vacant" height="16" width="16" style="margin: 0 0 0 5px; padding: 0; border: none;" /></a>';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=managerrent&pid=' + pid, onComplete:function(request){ if(request.responseText) { checkAddReportingMessage('manager_message', request.responseText, 'error', 1); } else { checkAddReportingMessage('manager_message', success, 'success', 1); } }});
}

// --------------------------------------------------------
// --- Property manager unrent out a property
function managerUnrentProperty(pid)
{
	var success = 'Your property is now available! This means that there is no one living in it for availability dates that you have listed for it. (Make sure to <a href="manager/listings/edit/' + pid + '/">change its availability</a> to reflect the appropriate dates that it will next be available.)';
	$('rented_' + pid).innerHTML = '<a href="manager/listings/rent/' + pid + '/" onclick="managerRentProperty(\'' + pid + '\'); return false;"><img src="images/icons/rented_1.png" alt="Set as occupied" title="Set as occupied" height="16" width="16" style="margin: 0 0 0 5px; padding: 0; border: none;" /></a>';
	var ajax = new Ajax.Request('http://www.spotsie.com/includes/actions.php', { method: 'post', parameters: 'action=managerunrent&pid=' + pid, onComplete:function(request){ if(request.responseText) { checkAddReportingMessage('manager_message', request.responseText, 'error', 1); } else { checkAddReportingMessage('manager_message', success, 'success', 1); } }});
}

// --------------------------------------------------------
// --- Toggle visibility of an answer in the help section
function toggleFAQ(ans_id)
{
	el = $(ans_id);
	if(el.style.display == 'none')
		el.style.display = 'block';
	else
		el.style.display = 'none';
}