var agent = navigator.userAgent.toLowerCase();
var windowHeight=(screen.height<= 600 && agent.indexOf("mac")==-1)?500:677;
var sh = screen.availHeight - (screen.availHeight/2);
var sw = screen.availWidth - (screen.availWidth/2);
function sortResults(sortByValue, sortOrderValue)
{
	document.forms.rppform.sortby.value = sortByValue + ' ' + sortOrderValue;
	document.forms.rppform.submit();
}
function sortByAddress(sortOrderValue)
{
	document.forms.rppform.sortby.value = '(address+0) ' + sortOrderValue + ', address ' + sortOrderValue;
	document.forms.rppform.submit();
}
function CheckAll()
{
	if(document.getElementById("chkSelectedListing") != null)
	{
		var bool = document.getElementById("chkMaster").checked;
		var a = document.getElementsByName("chkSelectedListing");
		for(var i=0;i<a.length;i++)
		{
			a[i].checked = bool;
		}
	}
}
function GetMarkedListingCount()
{
	var count = 0;
	if(document.getElementById("chkSelectedListing") != null)
	{
		var ck = document.getElementsByName("chkSelectedListing");
		if (ck[0]==undefined)
		{
			if(ck.checked)
				count = 1;
			else
				count = 0;
		}
		else
		{
			for(var i=0;i<ck.length;i++)
			{
				if(ck[i].checked)
					count++;
			}
		}
	}
			
	return count;
}
function GetMarkedListings()
{
	var s = "";
	if(document.getElementById("chkSelectedListing") != null)
	{
		var ck = document.getElementsByName("chkSelectedListing");
		if (ck[0]==undefined)
		{
			if(ck.checked)
			s = ck.value;
		}
		else
		{
			for (var i=0;i<ck.length;i++)
			{
				if(ck[i].checked)
				{
					if(s=="")
						s += ck[i].value;
					else
						s += "," + ck[i].value;
				}
			}
		}
	}
	return s;
}
function PrintMarked()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("You must mark at least one listing to print.");
	}
	else
	{
		var sh = screen.availHeight - (screen.availHeight/2);
		var sw = screen.availWidth - (screen.availWidth/2);
		propDetailWindow = window.open("print_selected.html?brochure=yes&id=" + GetMarkedListings(),'PropertyDetail','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=725,height=' + windowHeight + ',top=50,left=50');
	}
}
function PrintSummary()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("You must mark at least one listing to print.");
	}
	else
	{
		var sh = screen.availHeight - (screen.availHeight/2);
		var sw = screen.availWidth - (screen.availWidth/2);
		propDetailWindow = window.open("print_summary.html?search=yes&brochure=yes&lid=" + GetMarkedListings(),'PropertyDetail','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=725,height=' + windowHeight + ',top=50,left=50');
	}
}
function EmailSelected()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("You must mark at least one listing to e-mail.");
	}
	else
	{
		var sh = screen.availHeight - (screen.availHeight/2);
		var sw = screen.availWidth - (screen.availWidth/2);
		propDetailWindow = window.open("email_property.html?id=" + GetMarkedListings(),'Email','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=525,height=600,top=50,left=50');
	}
}
function EmailSummary()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("You must mark at least one listing to e-mail.");
	}
	else
	{
		var sh = screen.availHeight - (screen.availHeight/2);
		var sw = screen.availWidth - (screen.availWidth/2);
		propDetailWindow = window.open("email_summary.html?search=yes&brochure=yes&lid=" + GetMarkedListings(),'Email','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=525,height=600,top=50,left=50');
	}
}
function MapSelected()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("No listings are marked. You must select at least one listing to use this feature.");
		return;
	}
	if (count>30)
	{
		alert("Please select 30 or fewer listings to map. You currently have " + count + " listings marked.");
		return;
	}
	propDetailWindow = window.open("map_selected.html?id=" + GetMarkedListings(),'PropertyMap','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=725,height=' + windowHeight + ',top=50,left=50');
}
function RouteSelected()
{
	var count = GetMarkedListingCount();
	if (count==0)
	{
		alert("No listings are marked. You must select a least one listing to use this feature.");
		return;
	}
	if(count==1)
	{
		alert("Only one listing is marked. To get a Driving Route, you will need to select at least two listings.");
		return;
	}
	if (count>30)
	{
		alert("Please select 30 or fewer listings to map. You currently have " + count + " listings marked.");
		return;
	}
	propDetailWindow = window.open("route_selected.html?id=" + GetMarkedListings(),'Route','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=700,height=670,top=50,left=50');
}

var currPhoto = 0;
			 
function NextPhoto()
{
	for(var i=0;i!=aPhoto.length;i++)
	{
		if(aPhoto[i]==photo.src)
		{
			if (i+1 == aPhoto.length)
			{
				currPhoto = 0;
				photo.src = aPhoto[0]
				UpdatePhotoPosition(1);
			}
			else
			{
				currPhoto = i + 1;
				photo.src = aPhoto[i+1];
				UpdatePhotoPosition(i+2);
			}
			break;
		}
	}
}
function PrevPhoto()
{
	for(var i=0;i!=aPhoto.length;i++)
	{
		if(aPhoto[i]==photo.src)
		{
			if (i == 0)
			{
				currPhoto = aPhoto.length - 1;
				photo.src = aPhoto[aPhoto.length - 1]
				UpdatePhotoPosition(aPhoto.length);	
			}
			else
			{
	    			currPhoto = i - 1;
				photo.src = aPhoto[i-1];
				UpdatePhotoPosition(i);	
			}
			break;
		}
	}
}
function UpdatePhotoPosition(n)
{
	var photoCount = aPhoto.length;
	document.getElementById("tdPhotoPosition").innerHTML = "Photo " + n + " of " + photoCount;
}


		function openWin(url)
		{
			var sh = screen.availHeight - (screen.availHeight/2);
			var sw = screen.availWidth - (screen.availWidth/2);
			
			var agent = navigator.userAgent.toLowerCase();
			var windowHeight=600;
			
			width = 535;
			
			window.open(url,null,'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=' + width + ',height=' + windowHeight + ',top=' + (sh-(windowHeight/2)) + ',left=' + (sw-(width/2)));
		}


function ModifyNotes(sid)
{
		window.open("visitor/backoffice/notes.html?sid=" + sid,'Notes','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=300,height=196,top=50,left=50');
}

function CompareSelected()
{
	var count = GetMarkedListingCount();
	if (count < 2)
	{
		alert("You must mark at least two listing to compare.");
	}
	else
	{
		var sh = screen.availHeight - (screen.availHeight/2);
		var sw = screen.availWidth - (screen.availWidth/2);
		propDetailWindow = window.open("/compare_selected.html?id=" + GetMarkedListings(),'Compare','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=625,height=620,top=50,left=50');
	}
}
