//Error Handling Block
function handleError() {
	//alert('an error has occurred.');
	//return true;
}

window.onerror = handleError;

function WindowTakeFocus()
	{
	
	window.focus();
	}

//End Error Handling Block
function linkRedirect(url)
	{
	document.location.href = url;
	}

function ListimgImageZoom(imagePath)
	{
	window.open(imagePath, 'Zoom');
	}

function AddToMyFavorites(ListingID)
	{
	//Do Something;
	var redirURL = '/MainSite/MyAccount/default.aspx?ListingID=' + ListingID;
	document.location = redirURL;
	}

function BrokerFullBio(BID)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 400, popH = 500;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/MainSite/PopUps/BrokerFullBIO.aspx?BID=' + BID, 'BrokerFullBIO' + BID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}
	
function AddToOutlook(StartDate, EndDate, ListingID)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 10, popH = 10;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/MainSite/PopUps/OpenHouse.aspx?S=' + StartDate + '&E=' + EndDate + '&ListingID=' + ListingID, 'AddToOutlook' + ListingID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}

function NeighborhoodInfo(Neighborhood, ListingID)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 600, popH = 525;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/PopUps/Neighborhood.aspx?Neighborhood=' + Neighborhood + '&ListingID=' + ListingID, 'NeighborhoodInfo', 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}

function RequestMoreInformation(ListingID)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 500, popH = 525;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/RequestMoreInfo.aspx?ListingID=' + ListingID, 'RequestMoreInfo' + ListingID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}
function EmailThisListing(ListingID)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 500, popH = 525;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/EmailToAFriend.aspx?ListingID=' + ListingID, 'EmailThisListing' + ListingID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}

function ListingImageChange(ListingID, BuildingID, ImagePath, BuildingPath, imageObj, newimageObj, CurrentCountObj, MaxCountObj, MaxBuildingCountObj, NumImgPerPage, NextPrev, lblName, IsTownhouse) {
	//If ListingType is Not townhouse then Pic #1 is a building Pic.
	var GlobalImagePath = ImagePath;
	var imgID = ListingID;
	var hasBuildingPic = false;
	
	if (IsTownhouse == 'True')
		{
		GlobalImagePath = BuildingPath;
		imgID = BuildingID;
		}
	
	//If NextPrev is 0 then just swap the main image with the "newimageObj"
	if (NextPrev == 0) {
		if (newimageObj.src.indexOf('/images/spacer.gif') > 0) {
			//Do Nothing
		}
		else {
			imageObj.src = newimageObj.src.replace('/Thumbs','').replace('/88x65','/de');
			//alert(imageObj.src);
		}
	}
	else {
		//Otherwise bring up the next or previous page of images
		//NextPrev 1 is Next ;  NextPrev -1 is Previous
		var PageAttempt = (parseInt(NextPrev) + parseInt(CurrentCountObj.value));
		if (IsTownhouse == 'True')
			{
			var MaxImages = parseFloat(MaxBuildingCountObj.value);
			}
		else
			{
			var MaxImages = parseFloat(MaxCountObj.value);
			if (parseFloat(MaxBuildingCountObj.value) > 0)
				{
				hasBuildingPic = true;
				MaxImages += 1;
				}
			}
		
		var MaxPages = Math.ceil(MaxImages / NumImgPerPage);
		
		if (PageAttempt > MaxPages) {
			//If pressing next on last page, goto first page
			PageAttempt = 1;
			CurrentCountObj.value = 0;
		}
			
		if (PageAttempt < 1) {
			//If pressing previous on first page, goto last page
			PageAttempt = MaxPages;
			CurrentCountObj.value = parseInt(MaxPages) - 1;
			NextPrev = 1;
		}
		
		//now display all images in the current page
		if ((PageAttempt <= MaxPages) && (PageAttempt >= 1)) {
			document.getElementById(lblName).innerHTML = PageAttempt + ' of ' + MaxPages;
			
			CurrentCountObj.value = parseInt(NextPrev) + parseInt(CurrentCountObj.value);
			var PosVal = 0;
			var PagePicMax = MaxImages - CurrentCountObj.value * NumImgPerPage;
			var offSet = 0;
			
			if (hasBuildingPic)
				{
				//PagePicMax += 1;
				offSet = -1;
				}
				
			if ((hasBuildingPic) && (CurrentCountObj.value == '1'))
				{
				
				}

			// if only use 4 images in the page
			if (NumImgPerPage == 4) {
				if (PagePicMax + NumImgPerPage >= 1) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 3 + offSet;
					if ((hasBuildingPic) && (CurrentCountObj.value == '1'))
						{
						document.images['imgPicThumb01'].src = BuildingPath + BuildingID + '.' + 1 + '.jpg';
						}
					else
						{
						document.images['imgPicThumb01'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
						}
				}
				else document.images['imgPicThumb01'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 2) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 2 + offSet;
					document.images['imgPicThumb02'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb02'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 3) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 1 + offSet;
					document.images['imgPicThumb03'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb03'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 4) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 0 + offSet;
					document.images['imgPicThumb04'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb04'].src = '/images/spacer.gif';
			}
			else if (NumImgPerPage == 5) {
				PosVal = (CurrentCountObj.value * NumImgPerPage) - 4 + offSet;
				if (document.images['imgOH']) {
					if ((hasBuildingPic) && (CurrentCountObj.value == '1'))
						{
						document.images['imgOH'].src = BuildingPath + BuildingID + '.' + 1 + '.jpg';
						}
					else
						{
						document.images['imgOH'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
						}
					}
				else
					{
					if ((hasBuildingPic) && (CurrentCountObj.value == '1'))
						{
						document.images['imgFloorplan'].src = BuildingPath + BuildingID + '.' + 1 + '.jpg';
						}
					else
						{
						document.images['imgFloorplan'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
						}
					}

				if (PagePicMax + NumImgPerPage >= 2) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 3 + offSet;
					document.images['imgPicThumb01'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb01'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 3) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 2 + offSet;
					document.images['imgPicThumb02'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb02'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 4) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 1 + offSet;
					document.images['imgPicThumb03'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb03'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 5) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 0 + offSet;
					document.images['imgPicThumb04'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb04'].src = '/images/spacer.gif';
			}
			else if (NumImgPerPage == 6) {
				PosVal = (CurrentCountObj.value * NumImgPerPage) - 5 + offSet;
				if ((hasBuildingPic) && (CurrentCountObj.value == '1'))
					{
					document.images['imgOH'].src = BuildingPath + BuildingID + '.' + 1 + '.jpg';
					}
				else
					{
					document.images['imgOH'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
					}
				if (PagePicMax + NumImgPerPage >= 2) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 4 + offSet;
					document.images['imgFloorplan'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgFloorplan'].src = '/images/spacer.gif';

				if (PagePicMax + NumImgPerPage >= 3) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 3 + offSet;
					document.images['imgPicThumb01'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb01'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 4) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 2 + offSet;
					document.images['imgPicThumb02'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb02'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 5) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 1 + offSet;
					document.images['imgPicThumb03'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb03'].src = '/images/spacer.gif';
					
				if (PagePicMax + NumImgPerPage >= 6) {
					PosVal = (CurrentCountObj.value * NumImgPerPage) - 0 + offSet;
					document.images['imgPicThumb04'].src = GlobalImagePath + imgID + '.' + PosVal + '.jpg';
				}
				else document.images['imgPicThumb04'].src = '/images/spacer.gif';
			}
		}
	}
}
	
function findStyleRule(styleName) 
	{
	if (!document.styleSheets) return;

	for (i = 0; i < document.styleSheets.length; i++)
		{ 
		if (document.styleSheets[0].cssRules)  // Mozilla
			{
				for (j = 0; j < document.styleSheets(i).cssrules.length; j++) 
					{
					if (document.styleSheets(i).cssrules(j).selectorText == styleName) 
						{
						return document.styleSheets(i).cssrules(j);
						}
					}     
			}
		else // IE
			{
				for (j = 0; j < document.styleSheets(i).rules.length; j++) 
					{
					if (document.styleSheets(i).rules(j).selectorText == styleName) 
						{
						return document.styleSheets(i).rules(j);
						}
					}     
			}

		}
	}
	
//Popup Window Functions
function OpenHousePop(page, Description)
        {
        window.open(page, Description, 'toolbar=yes,location=no,scrollbars=yes,width=290,height=315');
        //window.open(page, Description, 'toolbar=yes,location=no,scrollbars=yes,width=607,height=' + eval(window.screen.height*.80).toString());
        }
function ManhattanMortgageArticlePop(page, Description)
        {
        window.open(page, Description, 'toolbar=yes,location=no,scrollbars=yes,width=800,height=550');
        //window.open(page, Description, 'toolbar=yes,location=no,scrollbars=yes,width=607,height=' + eval(window.screen.height*.80).toString());
        }
function ViewAllPhotos(ListingID)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = window.screen.availWidth;
		h = window.screen.availHeight;
		}

		var popW = 450, popH = 300;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;
		
        window.open('/mainsite/popups/ListingImages.aspx?ListingID=' + ListingID, 'ViewAllPhotos' + ListingID, 'toolbar=no,resizable=1,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
        }
function ViewAllFloorplans(ListingID)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = eval(window.screen.width*.75).toString(), popH = eval(window.screen.heighth*.75).toString();

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/ListingFloorplans.aspx?ListingID=' + ListingID, 'ViewAllFloorplans' + ListingID, 'toolbar=no,resizable=1,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
        }

function ViewAllBuildingFloorplans(BuildingID, Address)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = eval(window.screen.width*.75).toString(), popH = eval(window.screen.heighth*.75).toString();

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/ListingFloorplans.aspx?BuildingID=' + BuildingID + '&Address=' + Address, 'ViewAllFloorplans' + BuildingID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
        }
                
function ViewAllNHDBuildingFloorplans(BuildingID, Address)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = eval(window.screen.width*.75).toString(), popH = eval(window.screen.heighth*.75).toString();

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/ListingFloorplans.aspx?NHDBuildingID=' + BuildingID + '&Address=' + Address, 'ViewAllFloorplans' + BuildingID, 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
        }

function ListingPrinterFriendly(ListingID)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		//var popW = eval(window.screen.width*.75).toString(), popH = eval(window.screen.heighth*.75).toString();
		var popW = 660, popH = 550;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;
		if (ListingPrinterFriendly.arguments.length == 2) {
			if (ListingPrinterFriendly.arguments[1] != '1') {
				var oh = ListingPrinterFriendly.arguments[1];
				window.open('/mainsite/popups/ListingPrinterFriendly.aspx?ListingID=' + ListingID + '&OHDate=' + oh, 'ListingPrinterFriendly' + ListingID, 'toolbar=yes,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
			}
			else window.open('/mainsite/popups/ListingPrinterFriendly.aspx?ListingID=' + ListingID + '&PrintFlag=1', 'ListingPrinterFriendly' + ListingID, 'toolbar=yes,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
		}
		else {
	        window.open('/mainsite/popups/ListingPrinterFriendly.aspx?ListingID=' + ListingID, 'ListingPrinterFriendly' + ListingID, 'toolbar=yes,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	    }
        }
function FloorplanPrinterFriendly(ListingID)
        {
        var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = eval(window.screen.width*.75).toString(), popH = eval(window.screen.heighth*.75).toString();

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/mainsite/popups/FloorplanPrinterFriendly.aspx?ListingID=' + ListingID, 'FloorplanPrinterFriendly' + ListingID, 'toolbar=yes,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
        }

function BrokerContact(BID)
	{
     var w = 480, h = 340;

	if (document.all || document.layers) {
	w = screen.availWidth;
	h = screen.availHeight;
	}

	var popW = 5, popH = 5;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

    window.open('/mainsite/popups/BrokerContactDL.aspx?BID=' + BID, 'BrokerContactDownload' + BID, 'toolbar=no,location=no,scrollbars=no,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}
	
function OnlineMortgageApplication()
	{
	window.open('http://www.manhattanmortgage.com', 'ManhattanMortgage');
	}

function GenericPopup(strURL, strName, Width, Height)
	{
	var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = Width, popH = Height;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open(strURL, strName,'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}

function BrokerPageLink(BID)
	{
	//document.location = '/MainSite/Agents/Agent.aspx?BID=' + BID;
	document.location = '/' + BID;
	}
	
function ShowHideListingDescription(objName, Show)
	{
	var objToShowHide = findObj(objName, null)
	if (Show == true)
		{
		objToShowHide.style.display = 'inline';
		}
	else
		{
		objToShowHide.style.display = 'none';
		}
	}
//Function for array creation       
function definearray(x) {
    this.length = x;
    for(var t = 1; t <= x; t++)
        this[t] = 0;
    return this;
}
//Form Function
function do_select(which,form_name,prefix) {
	which = parseInt(which);
	if (document.forms[form_name].elements[prefix+which].value == 0) {
		document.forms[form_name].elements[prefix+which].value = 1;
	}
	else {
		document.forms[form_name].elements[prefix+which].value = 0;
	}
}

function hidden_box_init(form_name){
	//reset the hidden form 'temp' to 0    We put this here because ie3 should leave states persistent!
		for (i=0; i<document.forms[form_name].length; i++) {
			document.forms[form_name].elements[i].value=0;
		}
	
}

// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;

  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  return foundObj;
}

function EmailContactPopup(EmailContactID)
	{
		var w = 480, h = 340;

		if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
		}

		var popW = 650, popH = 500;

		var leftPos = (w-popW)/2, topPos = (h-popH)/2;

        window.open('/MainSite/Popups/Email_General.aspx?' + EmailContactID, 'EmailContact', 'toolbar=no,location=no,scrollbars=yes,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos );
	}
	
function printWindow(x){
		if (x==1)
		{
		document.all['maybeprint'].style.display="None";
		}else
		{
		document.all['maybeprint'].style.display="Block";
		}
		bV = parseInt(navigator.appVersion)
		if (bV >= 4) window.print()
	}
