// JavaScript Document
function toggleAgent() {
	
	var am_agent = document.getElementById('am_agent');
	var agent_info = document.getElementById('agent_info');
	var business_info = document.getElementById('business_info');
	var user_type = document.getElementById('user_type');
	
	if (am_agent.checked == true) {
		Effect.BlindUp(business_info);
		Effect.BlindDown(agent_info);
		user_type.value = 2;
	} else {
		Effect.BlindDown(business_info);
		Effect.BlindUp(agent_info);
		user_type.value = 1;
	}

}

function addElement(targetDiv) {
	
	var targetElm = document.getElementById(targetDiv);
	var numChildren = targetElm.childNodes.length;
	
	var newChild = targetElm.lastChild.cloneNode(true);
	
	for (var i = 0; i < newChild.childNodes.length; i++) {
		for (var j = 0; j < newChild.childNodes[i].childNodes.length; j++) {
			if (newChild.childNodes[i].childNodes[j].tagName.toLowerCase() == 'input') {
				newChild.childNodes[i].childNodes[j].value = '';
			}
		}
	}
	
	if (numChildren > 1) {
		newChild.removeChild(newChild.lastChild);
	}
	
	
	var removeElmDiv = document.createElement('div');
	var linkElm = document.createElement('a');
	linkElm.setAttribute('href', '#');
	linkElm.onclick = function() {
		var pNode = linkElm.parentNode.parentNode;
		pNode.parentNode.removeChild(pNode);
		return false;
	}
	var linkText = document.createTextNode('Remove');
	linkElm.appendChild(linkText);
	
	removeElmDiv.appendChild(linkElm);
	newChild.appendChild(removeElmDiv);
	
	
	newChild.style.display = 'none';
	newChild.setAttribute('id', 'temp');
	targetElm.appendChild(newChild);
	Effect.Appear('temp');
	newChild.removeAttribute('id');
	
	
}

function removeElement(targetDiv) {
		targetElm.removeChild(targetDiv);
}

function remove_Element(targetDiv) {
	alert(targetDiv.innerHTML);
	var targetElm = document.getElementById(targetDiv);
	var numChildren = targetElm.childNodes.length;
	var childToRemove = targetElm.lastChild;
	
	childToRemove.setAttribute('id', 'temp');
	
	if (numChildren != 1) {
		targetElm.removeChild(childToRemove);
	}
	
}

function postRealtorCom(tourid) {
	if (confirm('Are you sure you want to add this tour to realtor.com?')) {
		// Fetch Data
		var street = $('vt_address').value;
		var city = $('vt_city').value;
		var state = $('vt_state').value;
		var zip = $('vt_zip').value;
		var mlsid = $('vt_mlsno').value;
		
		new Ajax.Request('realtorcom2.html',
			{
				method: 'post',
				parameters: {street: street, city: city, state: state, zip: zip, mlsid: mlsid, tourid: tourid},
				onSuccess: function(transport) {
					var myjson = transport.responseText.evalJSON(false);
					returnResponse(myjson);
				},
				onFailure: function() { alert('Something went wrong...') }
			});
	}
}

function returnResponse(myjson) {
	var price = myjson.price;
	var status = myjson.status;
	var message = myjson.message;
	if (status == 'SUCCESS') {
		alert('Success!  The tour has been succesfully added to realtor.com');
		$('realtor_com_add_btn').disabled = true;
		$('realtor_com_add_btn').value = 'This tour has been added to realtor.com';
	} else {
		alert("Error\n\n" + message);
	}
}

function displayTourCCs() {
	for (var i = 0; i < $('office_tour_ccs').childNodes.length; i++) {
		$('office_tour_ccs').childNodes[i].style.display = 'none';
	}
	$('office_tour_ccs_div_'+$('office_id').value).style.display = 'block';
}

function importStillPhoto(tour_id, foldername, filename) {
	$('photo_import_' + filename + '_link').toggle();
	$('photo_' + filename + '_loader').toggle();
	new Ajax.Request('tours.html?a=add_slideshow_image',
		{
			method: 'get',
			parameters: {tour_id: tour_id, foldername: foldername, filename: filename, type: 'import'},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				var tour_id = myjson.tour_id;
				var image_id = myjson.image_id;
				var filename = myjson.filename;
				addSlideshowPhoto(tour_id, image_id, filename);
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function importFloorplan(tour_id, foldername, filename) {
	$('floorplan_import_' + filename + '_link').toggle();
	$('floorplan_' + filename + '_loader').toggle();
	new Ajax.Request('tours.html?a=add_floorplan',
		{
			method: 'get',
			parameters: {tour_id: tour_id, foldername: foldername, filename: filename, type: 'import'},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				var tour_id = myjson.tour_id;
				var floorplan_id = myjson.floorplan_id;
				var filename = myjson.filename;
				addFloorplan(tour_id, floorplan_id, filename);
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function deleteSlideshowPhoto(tour_id, image_id) {
	new Ajax.Request('tours.html?a=delete_slideshow_image',
		{
			method: 'get',
			parameters: {tour_id: tour_id, image_id: image_id},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				if (myjson.success) {
					var childToRemove = $('image_' + image_id);
					new Effect.Fade(childToRemove, {afterFinish: function() { $('slideshow_photos').removeChild(childToRemove); }});
					initSlideshowSortable('slideshow_photos');
				}
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function deleteFloorplan(tour_id, floorplan_id) {
	new Ajax.Request('tours.html?a=delete_floorplan',
		{
			method: 'get',
			parameters: {tour_id: tour_id, floorplan_id: floorplan_id},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				if (myjson.success) {
					var childToRemove = $('floorplan_' + floorplan_id);
					new Effect.Fade(childToRemove, {afterFinish: function() { $('active_floorplans').removeChild(childToRemove); }});
					initFloorplanSortable('active_floorplans');
				}
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function addSlideshowPhoto(tour_id, image_id, filename) {	
	var div1 = document.createElement('div');
	div1.id = 'image_' + image_id;
	div1.style.styleFloat = "left"; // For IE
	div1.style.cssFloat = "left";
	div1.style.width = "120px";
	div1.style.height = "92px";
	div1.style.padding = "5px 5px";
	div1.style.textAlign = "center";
	div1.style.display = "none";
	var div2 = document.createElement('div');
	div2.style.padding = 0;
	var img = document.createElement('img');
	img.src = "/tours/slideshow_images/" + image_id + '-tn.jpg';
	img.width = 120;
	img.height = 80;
	img.border = 0;
	div2.appendChild(img);
	var div3 = document.createElement('div');
	div3.style.padding = 0;
	
	var a3a = document.createElement('a');
	a3a.href = '#';
	a3a.onclick = function () { editSlideshowPhoto(tour_id, image_id); return false; }
	var a3atext = document.createTextNode('Edit Caption');
	a3a.appendChild(a3atext);
	div3.appendChild(a3a);
	
	div3.appendChild(document.createTextNode(' | '));
	
	var a3 = document.createElement('a');
	a3.href = '#';
	a3.onclick = function () { deleteSlideshowPhoto(tour_id, image_id); return false; }
	var a3text = document.createTextNode('Delete');
	a3.appendChild(a3text);
	div3.appendChild(a3);
	div1.appendChild(div2);
	div1.appendChild(div3);
	$('slideshow_photos').appendChild(div1);	
	new Effect.Appear('image_' + image_id);
	initSlideshowSortable('slideshow_photos');
	if (filename) {
		$('photo_import_' + filename + '_link').toggle();
		$('photo_' + filename + '_loader').toggle();	
	}
}

function editSlideshowPhoto(tour_id, image_id) {
	new Ajax.Request('tours.html?a=get_slideshow_image_caption',
		{
			method: 'get',
			parameters: {image_id: image_id},
			onSuccess: function(transport) {
				var caption = transport.responseText;
				
				var pos = findPos($('image_' + image_id));
				var div1 = document.createElement('div');
				div1.style.width = '164px';
				div1.style.height = '20px';
				div1.style.background = '#FFFFFF';
				div1.style.position = 'absolute';
				div1.style.left = parseInt(pos[0] + 5) + 'px';
				div1.style.top = parseInt(pos[1] + 84) + 'px';
				div1.style.border = '1px solid #555555';
				div1.style.padding = '0';
				div1.style.margin = '2px 0';
				
				var div2 = document.createElement('div');
				div2.id = 'image_caption_' + image_id;
				div2.style.height = '16px';
				div2.style.lineHeight = '16px';
				div2.style.cssFloat = 'left';
				div2.style.padding = '2px';
				div2.appendChild(document.createTextNode(caption));
				div1.appendChild(div2);
				
				$('slideshow_photos').appendChild(div1);
				var editor = new Ajax.InPlaceEditor('image_caption_' + image_id, 'tours.html?a=edit_slideshow_image_caption', { formClassName: 'captionEditor', onComplete: function () { $('slideshow_photos').removeChild($('image_caption_' + image_id).parentNode); }, cancelLink: false, callback: function(form, value) { return 'image_id=' + image_id + '&value=' + escape(value); }});
				editor.enterEditMode('click');
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function addUserImage(user_id, image_id, image_width, image_height) {	
	var div1 = document.createElement('div');
	var img1 = document.createElement('img');
	img1.src = '/tours/user_images/' + image_id + '-tn.jpg';
	img1.width = image_width;
	img1.height = image_height;
	img1.alt = 'Image';
	div1.appendChild(img1);
	 
	var a1 = document.createElement('a');
	a1.src = '#';
	a1.style.display = 'block';
	a1.style.textAlign = 'center';
	a1.onclick = function () { deleteUserImage(user_id, image_id); }
	a1.appendChild(document.createTextNode('Delete Image'));
	
	$('image_holder').style.width = image_width + 'px';
	$('image_holder').style.height = image_height + 'px';
	$('image_holder').appendChild(div1);
	$('image_holder').appendChild(a1);
	new Effect.Appear('image_holder');
	$('upload_btn').toggle();
	
}

function deleteUserImage(user_id, image_id) {
	new Ajax.Request('users.html?a=delete_image',
		{
			method: 'get',
			parameters: {user_id: user_id, image_id: image_id},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				if (myjson.success) {
					new Effect.Fade($('image_holder').firstChild, {afterFinish: function() { $('image_holder').removeChild($('image_holder').firstChild); $('image_holder').removeChild($('image_holder').firstChild); $('image_holder').style.width = 0; $('image_holder').style.height = 0; $('upload_btn').toggle(); }});
				}
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function addOfficeImage(office_id, image_id, image_width, image_height) {	
	var div1 = document.createElement('div');
	var img1 = document.createElement('img');
	img1.src = '/tours/real_estate_office_images/' + image_id + '-tn.jpg';
	img1.width = image_width;
	img1.height = image_height;
	img1.alt = 'Image';
	div1.appendChild(img1);
	 
	var a1 = document.createElement('a');
	a1.src = '#';
	a1.style.display = 'block';
	a1.style.textAlign = 'center';
	a1.onclick = function () { deleteOfficeImage(office_id, image_id); }
	a1.appendChild(document.createTextNode('Delete Image'));
	
	$('image_holder').style.width = image_width + 'px';
	$('image_holder').style.height = image_height + 'px';
	$('image_holder').appendChild(div1);
	$('image_holder').appendChild(a1);
	new Effect.Appear('image_holder');
	$('upload_btn').toggle();
	
}

function deleteOfficeImage(office_id, image_id) {
	new Ajax.Request('real_estate_offices.html?a=delete_image',
		{
			method: 'get',
			parameters: {office_id: office_id, image_id: image_id},
			onSuccess: function(transport) {
				var myjson = transport.responseText.evalJSON(false);
				if (myjson.success) {
					new Effect.Fade($('image_holder').firstChild, {afterFinish: function() { $('image_holder').removeChild($('image_holder').firstChild); $('image_holder').removeChild($('image_holder').firstChild); $('image_holder').style.width = 0; $('image_holder').style.height = 0; $('upload_btn').toggle(); }});
				}
			},
			onFailure: function() { alert('Something went wrong...') }
		});
}

function addFloorplan(tour_id, floorplan_id, filename) {	
	var div1 = document.createElement('div');
	div1.id = 'floorplan_' + floorplan_id;
	div1.style.styleFloat = "left"; // For IE
	div1.style.cssFloat = "left";
	div1.style.width = "120px";
	div1.style.height = "92px";
	div1.style.padding = "5px 5px";
	div1.style.textAlign = "center";
	div1.style.display = "none";
	var div2 = document.createElement('div');
	div2.style.padding = 0;
	var img = document.createElement('img');
	img.src = "/tours/floorplans/" + floorplan_id + '.jpg';
	img.width = 120;
	img.height = 80;
	img.border = 0;
	div2.appendChild(img);
	var div3 = document.createElement('div');
	div3.style.padding = 0;
	var a3a = document.createElement('a');
	a3a.href = '#';
	a3a.onclick = function() { window.open('tours.html?a=add_floorplan_images&floorplan_id=' + floorplan_id + '&tour_id=' + tour_id, 'add_floorplan_images');return false; };
	a3atext = document.createTextNode('Edit');
	a3a.appendChild(a3atext);
	a3text = document.createTextNode(' | ');
	var a3b = document.createElement('a');
	a3b.href = '#';
	a3b.onclick = function () { deleteFloorplan(tour_id, floorplan_id); return false; }
	var a3btext = document.createTextNode('Delete');
	a3b.appendChild(a3btext);
	div3.appendChild(a3a);
	div3.appendChild(a3text);
	div3.appendChild(a3b);
	div1.appendChild(div2);
	div1.appendChild(div3);
	$('active_floorplans').appendChild(div1);	
	new Effect.Appear('floorplan_' + floorplan_id);
	initSlideshowSortable('active_floorplans');
	if (filename) {
		$('floorplan_import_' + filename + '_link').toggle();
		$('floorplan_' + filename + '_loader').toggle();	
	}
}

function addVirtualTourView(tour_id) {
	new Ajax.Request('tours.html?a=add_virtual_tour_view',
	{
		method: 'get',
		parameters: {tour_id: tour_id},
		onSuccess: function(transport) {
			var myjson = transport.responseText.evalJSON(false);
			var tour_id = myjson.tour_id;
			var view_id = myjson.view_id;
			var view_name = myjson.view_name;
			var view_filename = myjson.view_filename;
			
			var liView = document.createElement('li');
			liView.id = 'view_' + view_id;
			
			var divHandle = document.createElement('div');
			divHandle.addClassName('handle');
			divHandle.style.width = '20px';
			divHandle.style.height = '16px';
			divHandle.style.background = '#ffffff';
			
			var handleImg = document.createElement('img');
			handleImg.src = '/images/drag_img.gif';
			handleImg.width = 11;
			handleImg.height = 11;
			handleImg.alt = 'Drag';
			
			divHandle.appendChild(handleImg);
			liView.appendChild(divHandle);
			
			var divName = document.createElement('div');
			divName.id = 'view_name_' + view_id;
			
			divName.appendChild(document.createTextNode(view_name));
			liView.appendChild(divName);
			
			
			var divFileName = document.createElement('div');
			divFileName.id = 'view_filename_' + view_id;
			
			divFileName.appendChild(document.createTextNode(view_filename));
			liView.appendChild(divFileName);
			
			var aRemove = document.createElement('a');
			aRemove.href = '#';
			aRemove.onclick = function () { deleteVirtualTourView(tour_id, view_id); return false; }
			aRemove.appendChild(document.createTextNode('Remove'));
			liView.appendChild(aRemove);
			
			$('virtual_tour_views').appendChild(liView);
			
			initVirtualTourViewsSortable('virtual_tour_views');
			new Ajax.InPlaceEditor('view_name_' + view_id, 'tours.html?a=edit_virtual_tour_view_name', { callback: function(form, value) { return 'view_id=' + view_id + '&value=' + escape(value); }});
			new Ajax.InPlaceEditor('view_filename_' + view_id, 'tours.html?a=edit_virtual_tour_view_filename', { callback: function(form, value) { return 'view_id=' + view_id + '&value=' + escape(value); }});
		},
		onFailure: function() { alert('Something went wrong...') }
	});

}

function deleteVirtualTourView(tour_id, view_id) {
	new Ajax.Request('tours.html?a=delete_virtual_tour_view',
	{
		method: 'get',
		parameters: {tour_id: tour_id, view_id: view_id},
		onSuccess: function(transport) {
			var myjson = transport.responseText.evalJSON(false);
			if (myjson.success) {
				var childToRemove = $('view_' + view_id);
				new Effect.Fade(childToRemove, {afterFinish: function() { $('virtual_tour_views').removeChild(childToRemove); }});
				initVirtualTourViewsSortable('virtual_tour_views');
			}
		},
		onFailure: function() { alert('Something went wrong...') }
	});
}

function initVirtualTourViewsSortable(id) {
	Sortable.create(id, {tag: 'li', overlap: 'vertical', contraint: false, handle: 'handle', 
	onUpdate:function() {
		new Ajax.Request("tours.html?a=reorder_virtual_tour_views", 
			{
				method: "post",
				parameters: {data: Sortable.serialize("virtual_tour_views") }
			});
	}});	
}

function initSlideshowSortable(id) {
	Sortable.create(id, {tag: 'div', overlap: 'horizontal', contraint: false, 
	onUpdate:function() {
		new Ajax.Request("tours.html?a=reorder_slideshow_images", 
			{
				method: "post",
				parameters: {data: Sortable.serialize("slideshow_photos") }
			});
	}});	
}

function initFloorplanSortable(id) {
	Sortable.create(id, {tag: 'div', overlap: 'horizontal', contraint: false, 
	onUpdate:function() {
		new Ajax.Request("tours.html?a=reorder_floorplans", 
			{
				method: "post",
				parameters: {data: Sortable.serialize("active_floorplans") }
			});
	}});	
}

function availChange() {
	var sel = document.getElementById("availability");
	var combo = document.getElementById("combo");
	var key = document.getElementById("key");
	var owner_info = document.getElementById("owner_info");
	
	if (sel.selectedIndex == 0) {
		combo.style.display = "none";
		key.style.display = "block";
		owner_info.style.display = "none";
	}
	
	if (sel.selectedIndex == 1) {
		combo.style.display = "block";
		key.style.display = "none";
		owner_info.style.display = "none";
	}
	
	if ((sel.selectedIndex == 3) || (sel.selectedIndex == 5)) {
		combo.style.display = "none";
		key.style.display = "none";
		owner_info.style.display = "block";
	}
	
	if ((sel.selectedIndex == 2) || (sel.selectedIndex == 4) || (sel.selectedIndex == 6)) {
		combo.style.display = "none";
		key.style.display = "none";
		owner_info.style.display = "none";
	}

}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function checkOrderForm() {
	if($('terms_agreed').checked === false) {
		alert('You must accept the Terms & Conditions.');
		return false;
	}	
	var requiredFields = [];
	if ($('vt_address').value == '') {
		requiredFields.push({field: 'address', msg: 'Address'});	
	}
	if ($('vt_city').value == '') {
		requiredFields.push({field: 'city', msg: 'City'});
	}
	if ($('vt_zip').value == '') {
		requiredFields.push({field: 'zip', msg: 'Zip'});
	}
	if ($('vt_mlsno').value == '' && $('realtor_com').checked == true) {
		requiredFields.push({field: 'vt_mlsno', msg: '\nMLS Number (MLS# required for realtor.com)'});
	}
	if ($('order_vt').checked == false && $('order_fp').checked == false) {
		requiredFields.push({field: 'vt_mlsno', msg: '\nYou must check virtual tour, floor plans, or both.'});
	}
	if (requiredFields.length > 0) {
		var msg = "You must fill out the following fields:\n";
		for (var i = 0; i < requiredFields.length; i++) {
			msg += requiredFields[i].msg + "\n";	
		}
		alert(msg);
		return false;
	} else {
		return true;	
	}
}