


function sendListingToFriend(id)
{
	var fromEmail  = $('yourEmail').value;
	var toEmail = $('friendsEmail').value;
	
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			$('formEmailFriend').style.display = 'none';
			$('emailFriendNotice').style.display = 'block';
		},

		onFailure : function(resp) {
			
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=email_friend&from_email=' + fromEmail + '&to_email=' + toEmail + '&property_id=' + id

	});
}


function agentTypeChanged(type)
{
	var pic  = $('profilePic');
	var form = $('profileForm');
	
	if(type == 'Realtor' || type == 'Auctioneer'){
		$('profilePhotoLabel').innerHTML = type + ' Profile Photo:';
		$('profilePhotoUpload').style.display = 'block';
		if(pic){
			pic.style.display = 'block';
			if(form) form.style.width  = '570px';
		}
	}else{
		$('profilePhotoUpload').style.display = 'none';
		if(pic){
			pic.style.display = 'none';
			if(form) form.style.width  = '720px';
		}
	}
}


function shakeInput(input)
{
	input.style.borderColor = '#c00';
	Effect.Shake(input,7);
	
}


function checkPaymentForm()
{
	var result = true;
	var form = $('paymentForm');
	
	var inputs = form.getElements();
	var countryField = $('countryField');
	var country = countryField.options[countryField.selectedIndex].value;
	
	inputs.each(function(input) {
		
		if(input.name.substring(0, 9) != 'checkout['){
		
		}else if(input.name == 'checkout[country]' && input.options[input.selectedIndex].value == ''){
			shakeInput(input);
			result = false;
		}else if(input.name == 'checkout[state]'){
			if((country == 'United States' || country == 'Canada') && input.options[input.selectedIndex].value == ''){
				shakeInput(input);
				result = false;
			}else{
				input.style.borderColor = '';
			}
		}else if(input.name == 'checkout[province]'){
			if(country != 'United States' && country != 'Canada' && input.value == ''){
				shakeInput(input);
				result = false;
			}else{
				input.style.borderColor = '';
			}
		}else if(input.value == '' && input.name != '' && input.name != 'checkout[address_2]' && input.name != 'checkout[province]' && input.name != 'init'){
			shakeInput(input);
			result = false;
		}else if(input.name == 'checkout[card_num]' && input.value.length < 13){
			shakeInput(input);
			result = false;
		}else if(input.name == 'checkout[card_code]' && input.value.length < 3){
			shakeInput(input);
			result = false;
		}else{
			input.style.borderColor = '';
		}
	});
	
	
	if(!result){
		$('paymentErrors').innerHTML = '<ul><li>Please complete all required fields</li></ul>';
		$('paymentErrors').style.display = 'block';
	}else{
		$('paymentErrors').style.display = 'none';
		$('paymentErrors').innerHTML = '';
	}
	
	return result;
}



function updateImageSorting(productId, url)
{
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			window.location.href = url;
		},

		onFailure : function(resp) {
			window.location.href = url;
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Sortable.serialize('sortable_images') + '&action=update_image_order&property_id=' + productId

	});
}


function submitUpdateListingForm()
{

	$('listingErrors').style.display = 'none';
	$('listingResults').style.display = 'none';

	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('listingResults').innerHTML = data.output;
				$('listingResults').style.display = 'block';
			}else{
				$('listingErrors').innerHTML = data.output;
				$('listingErrors').style.display = 'block';
			}

			new Effect.ScrollTo('content',{offset:-10});
		},

		onFailure : function(resp) {
			$('listingErrors').innerHTML = "<p>An error occured during processing</p>";
			$('listingErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('listingForm')) + '&action=update_property'

	});

}


function submitAddListingForm1()
{
	$('listingErrors').style.display = 'none';
	$('listingResults').style.display = 'none';

	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('listingForm').submit();
			}else{
				$('listingErrors').innerHTML = data.output;
				$('listingErrors').style.display = 'block';
			}
			
		},

		onFailure : function(resp) {
			$('listingErrors').innerHTML = "<p>An error occured during processing</p>";
			$('listingErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('listingForm')) + '&action=insert_property_step1'

	});
}


function submitAddListingForm2()
{
	$('listingErrors').style.display = 'none';
	$('listingResults').style.display = 'none';

	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('listingForm').submit();
			}else{
				$('listingErrors').innerHTML = data.output;
				$('listingErrors').style.display = 'block';
				new Effect.ScrollTo('content',{offset:-10});
			}
			
		},

		onFailure : function(resp) {
			$('listingErrors').innerHTML = "<p>An error occured during processing</p>";
			$('listingErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('listingForm')) + '&action=insert_property_step2'

	});
}


function checkAuctionFields(show)
{
	$('auctionFields').style.display = show ? 'block' : 'none';	
	$('labelPrice').innerHTML = show ? 'Reserve:' : 'Price:';	
}


function limitNumerical(field, allowCommas, allowDecimals)
{
	var text = field.value;
	var len  = text.length;
	var out  = '';
	
	for(var i = 0; i < len; i++){
		if(	text.charAt(i) == '0' || text.charAt(i) == '1' || text.charAt(i) == '2' || 
			text.charAt(i) == '3' || text.charAt(i) == '4' || text.charAt(i) == '5' || 
			text.charAt(i) == '6' || text.charAt(i) == '7' || text.charAt(i) == '8' || text.charAt(i) == '9' || 
			(allowCommas && text.charAt(i) == ',') ||
			(allowDecimals && text.charAt(i) == '.')
		){
			out = out + text.charAt(i);
		}
	}
	
	field.value = out;
}


function changeCountry(select)
{
	var value  		= select.options[select.selectedIndex].value;
	var state  		= $('stateField');
	var province  	= $('provinceField');
	var fields 		= $('locationFields');
	if(value == '' || value == 'United States' || value == 'Canada'){
		province.style.display 	= 'none';
		state.style.display 	= 'block';
		if(fields) fields.style.display 	= 'none';
	}else{
		state.style.display 	= 'none';
		province.value = '';
		province.style.display 	= 'block';
		if(fields) fields.style.display 	= 'block';
		
	}
}


function imageUploadSubmit()
{
	$('imageUploadControls').style.display = 'none';
	$('imageUploadWarning').style.display = 'none';
	$('imageUploadLoading').style.display = 'block';
	return true;
}


function imageFrameLoaded(propertyId)
{
	$('imageUploadInput').value = '';
	
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();
			var count = data.count;
			
			$('imageCount').innerHTML = count;
			$('imageUploadLoading').style.display = 'none';
			if(count >= 12){
				$('imageUploadControls').style.display = 'none';
				$('imageUploadWarning').style.display = 'block';
			}else{
				$('imageUploadWarning').style.display = 'none';
				$('imageUploadControls').style.display = 'block';
			}
		},

		onFailure : function(resp) {
			//window.location.href = '/clienthome.html';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=get_image_count&property_id=' + propertyId

	});
}



function deletePropertyImage(propertyId, imageId)
{
	if(!confirm('Are you sure you want to delete this image?')) return false;
	
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				//var block = $('imageBlock_'+imageId);
				//if(block) block.style.display = 'none';
				window.location.href = '/manage-images.html?property_id=' + propertyId;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=delete_property_image&property_id=' + propertyId + '&image_id=' + imageId

	});
}


function outputManagementListings(page)
{
	new Ajax.Request("/agent.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				new Effect.ScrollTo('content',{offset:-10});
				$('content').innerHTML = data.output;
			}
		},

		onFailure : function(resp) {

		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=management_listings&page=' + page

	});
}


function deleteProperty(propertyId)
{
	var div = $('propertyManagement-'+propertyId);
	
	if(!confirm('Are you sure you want to delete this listing?')) return false;
	
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();
			
			if(!div){
				window.location.href = '/clienthome.php';
				return;
			}
			
			if(data.result == 'success'){
				div.style.display = 'none';
			}else{
				div.innerHTML = data.output;
			}


		},

		onFailure : function(resp) {
			
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=delete_property&property_id=' + propertyId

	});

}


function markSold(propertyId)
{
	return executePropertyCall('mark_sold', propertyId);
}


function markUnsold(propertyId)
{
	return executePropertyCall('mark_unsold', propertyId);
}


function disableProperty(propertyId)
{
	return executePropertyCall('disable_property', propertyId);
}


function enableProperty(propertyId)
{
	return executePropertyCall('enable_property', propertyId);
}


function executePropertyCall(command, propertyId)
{
	var div = $('propertyManagement-'+propertyId);
	
	new Ajax.Request("/property.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();
			
			if(!div){
				window.location.href = '/clienthome.php';
			}else{
				div.innerHTML = data.output;
			}
		},

		onFailure : function(resp) {
			
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : 'action=' + command + '&property_id=' + propertyId

	});

}


function submitProfile()
{

	$('profileErrors').style.display = 'none';
	$('profileResults').style.display = 'none';

	new Ajax.Request("/agent.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				
				if($('profilePhotoInput').value != ''){
					$('profileForm').submit();
				}else{
					$('profileResults').innerHTML = data.output;
					$('profileResults').style.display = 'block';
				}
			}else{
				$('profileErrors').innerHTML = data.output;
				$('profileErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('profileErrors').innerHTML = "<p>An error occured during processing</p>";
			$('profileErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('profileForm'))

	});

}



function submitPropertyContact()
{

	$('propertyContactErrors').style.display = 'none';

	new Ajax.Request("/contact.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('propertyContactForm').style.display = 'none';
				$('propertyContactResults').innerHTML = data.output;
				$('propertyContactResults').style.display = 'block';
			}else{
				$('propertyContactErrors').innerHTML = data.output;
				$('propertyContactErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('propertyContactErrors').innerHTML = "<p>An error occured during processing</p>";
			$('propertyContactErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('propertyContactForm'))

	});

}



function submitContact()
{

	$('contactErrors').style.display = 'none';

	new Ajax.Request("/contact.ajax.php", {

		onSuccess : function(resp) {
			var data = resp.responseText.evalJSON();

			if(data.result == 'success'){
				$('contactForm').style.display = 'none';
				$('contactResults').innerHTML = data.output;
				$('contactResults').style.display = 'block';
			}else{
				$('contactErrors').innerHTML = data.output;
				$('contactErrors').style.display = 'block';
			}


		},

		onFailure : function(resp) {
			$('contactErrors').innerHTML = "<p>An error occured during processing</p>";
			$('contactErrors').style.display = 'block';
		},

		method:'post',
		requestHeaders: {Accept: 'application/json'},
		parameters : Form.serialize($('contactForm'))

	});

}


function goTo(url)
{
	window.location.href = url;
}

function confirmGoTo(url)
{
	if( confirm("Are you sure you want to do this?") ){
		goTo(url);
	}
}

function bookmark(url, description)
{
	var msg = "Press CTRL+D to add a bookmark to this site.";
	
	if (navigator.appName == 'Microsoft Internet Explorer'){
		window.external.AddFavorite(url, description);
	}else if (navigator.appName=='Netscape'){
		alert(msg);
	}
}
