// property search functionality start
function removeSelectedItems(list) {
    list = document.getElementById(list);
    if (list) {
        var i = 1;
        while (i < list.options.length) {
            if (list.options[i].selected) {
				list.removeChild(list.options[i]);
            } else {
                i++;
            }
        }
    }
    return false;
}

function addSelectedItem(sourceList, prefix, destList) {
    if (sourceList.selectedIndex > 0) {
        destList = document.getElementById(destList);
        if (destList) {
            var item = sourceList.options[sourceList.selectedIndex];
            var isNew = true;
            for (var i = 1; i < destList.options.length; i++) {
                if (destList.options[i].text == item.value) {
                    isNew = false;
                    break;
                }
            }
            if (isNew) {
                var option = document.createElement("option");
                option.text = item.value;
                option.value = prefix + item.value;
                destList.options.add(option);
            }
        }
    }
    return true;
}

function regionChange(regionList, type, suburbList, selectedItem) {
    suburbList = document.getElementById(suburbList);
    if (suburbList) {
        suburbList.options.length = 1;
    }
    
    if (regionList.selectedIndex > 0) {
        var region = regionList.options[regionList.selectedIndex].value;
        for (var i = 0; i < regions.length; i++) {
            if ((regions[i].type == type) && (regions[i].name == region)) {
                suburbsInRegion = suburbMap[regions[i].id];
                for (var j = 0; j < suburbsInRegion.length; j++) {
                    var suburb = suburbs[suburbsInRegion[j]];
                    if (suburb && suburb.count[type])  {
                        var option = document.createElement("option");
                        option.text = suburb.name + (suburb.showRegion ? " - " + region : "") + " (" + suburb.count[type] + ")";
                        option.value = suburb.name;
                        suburbList.options.add(option);
                        if (selectedItem && (suburb.name == selectedItem)) {
                            suburbList.selectedIndex = suburbList.options.length - 1;
                        }
                    }
                }
                break;
            }
        }
    }
}

function propertySearchSubmit(list) {
    list = document.getElementById(list);
    if (list) {
        for (var i = 0; i < list.options.length; i++) {
            list.options[i].selected = (i > 0);
        }
    }
}

function initPropertySearch(regionList1, suburbList1, type1, regionList2, suburbList2, type2) {
    var selectedItem = null;
    var regionList = document.getElementById(regionList1);
    var suburbList = document.getElementById(suburbList1);
    if (regionList) {
        if ((regionList.selectedIndex > 0) && suburbList) {
            selectedItem = suburbList.options[suburbList.selectedIndex].value;
            var newList = document.createElement("select");
            newList.name = suburbList.name;
            newList.id = suburbList.id;
            newList.className = suburbList.className;
            newList.onchange = suburbList.onchange;
            var option = document.createElement("option");
            option.text = "Suburb";
            newList.options.add(option);
            suburbList.parentNode.replaceChild(newList, suburbList);
            
            regionChange(regionList, type1, suburbList1, selectedItem);
        }
    }

    selectedItem = null;
    regionList = document.getElementById(regionList2);
    suburbList = document.getElementById(suburbList2);
    if (regionList) {
        if ((regionList.selectedIndex > 0) && suburbList) {
            selectedItem = suburbList.options[suburbList.selectedIndex].value;
            var newList = document.createElement("select");
            newList.name = suburbList.name;
            newList.id = suburbList.id;
            newList.className = suburbList.className;
            newList.onchange = suburbList.onchange;
            var option = document.createElement("option");
            option.text = "Suburb";
            newList.options.add(option);
            suburbList.parentNode.replaceChild(newList, suburbList);
            
            regionChange(regionList, type2, suburbList2, selectedItem);
        }
    }
}
// property search functionality end

function initPropertySearchNew(regionList1, suburbList1, type1) {
    var selectedItem = null;
    var regionList = document.getElementById(regionList1);
    var suburbList = document.getElementById(suburbList1);
    if (regionList) {
        if ((regionList.selectedIndex > 0) && suburbList) {
            selectedItem = suburbList.options[suburbList.selectedIndex].value;
            var newList = document.createElement("select");
            newList.name = suburbList.name;
            newList.id = suburbList.id;
            newList.className = suburbList.className;
            newList.onchange = suburbList.onchange;
            var option = document.createElement("option");
            option.text = "Suburb";
            newList.options.add(option);
            suburbList.parentNode.replaceChild(newList, suburbList);

            regionChange(regionList, type1, suburbList1, selectedItem);
        }
    }
}

function contactFormOpen(container, type, code) {
    var title = "Contact Form";
    switch(type) {
        case "property": title = "Ask a Question"; break;
        case "agent": title = "Contact Me"; break;
        case "branch": title = "Contact Us";
    }
    container.update(
'<h3>' + title + '</h3> \
<input type="text" class="detailForm" id="contactName" value="Your Name" onblur="if (!this.value) this.value = \'Your Name\';" onfocus="if (this.value == \'Your Name\') this.value = \'\';" /> \
<input type="text" class="detailForm" id="contactEmail" value="Your Email" onblur="if (!this.value) this.value = \'Your Email\';" onfocus="if (this.value == \'Your Email\') this.value = \'\';" /> \
<input type="text" class="detailForm" id="contactPhone" value="Your Phone #" onblur="if (!this.value) this.value = \'Your Phone #\';" onfocus="if (this.value == \'Your Phone #\') this.value = \'\';" /> \
<textarea id="contactMessage" rows="4" onblur="if (!this.value) this.value = \'Your Message\';" onfocus="if (this.value == \'Your Message\') this.value = \'\';">Your Message</textarea> \
<input type="button" value="Send" class="searchSortButton" onclick="return contactFormSubmit(\'' + type + '\', \'' + code + '\')" />').show();
}

function contactFormSubmit(type, code) {
    var name = $("contactName").value;
    if (!name || (name == "Your Name")) {
        alert("Please enter your name.");
        
        $("contactName").focus();
        return false;
    }
    var email = $("contactEmail").value;
    if (!email || !email.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        alert("Please enter your valid email.");
        $("contactEmail").focus();
        return false;
    }
    var message = $("contactMessage").value;
    if (!message || (message == "Your Message")) {
        alert("Please enter your message.");
        $("contactMessage").focus();
        return false;
    }
    
    Modalbox.show(
'<div id="dialogProgress" style="text-align:center"> \
  Mailing your form... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" onclick="Modalbox.hide(); return false" /> \
  </div> \
</div>', { title: "Action in progress...", width: 300, afterLoad: function() {
        var params = {
                action: "askAQuestion",
                name: name,
                email: email,
                phone: $("contactPhone").value,
                message: message
        };
        switch(type) {
            case "property": params["propertyId"] = code; break;
            case "agent": params["agentCode"] = code; break;
            case "branch": params["branchId"] = code;
        }
        new Ajax.Request("/bthandlers/mailer.ashx", {
            method: "post",
            parameters: params,
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = "Your form was mailed successfully."; break;
                    case "1": result = "Invalid email address. Enter a valid email address and try again."; break;
                    case "2": result = "Invalid input. Make sure you've filled the form out correctly and try again."; break;
                    case "3": result = "Unable to mail your form. Try again later.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
    } });
    return false;
}

function shareFormOpen(propertyId) {
    Modalbox.show(
'<div id="shareForm"> \
  <div class="shareWithFriendForm" id="shareFormBody"> \
  <input type="text" class="detailForm" id="shareName" value="Your Name" onblur="if (!this.value) this.value = \'Your Name\';" onfocus="if (this.value == \'Your Name\') this.value = \'\';" /> \
  <input type="text" class="detailForm" id="shareEmail" value="Your Email" onblur="if (!this.value) this.value = \'Your Email\';" onfocus="if (this.value == \'Your Email\') this.value = \'\';" /> \
  <input type="text" class="detailForm" id="shareFriendName" value="Your Friend\'s Name" onblur="if (!this.value) this.value = \'Your Friend\\\'s Name\';" onfocus="if (this.value == \'Your Friend\\\'s Name\') this.value = \'\';" /> \
  <input type="text" class="detailForm" id="shareFriendEmail" value="Your Friend\'s Email" onblur="if (!this.value) this.value = \'Your Friend\\\'s Email\';" onfocus="if (this.value == \'Your Friend\\\'s Email\') this.value = \'\';" /> \
  <textarea id="shareMessage" rows="4" onblur="if (!this.value) this.value = \'Your Message\';" onfocus="if (this.value == \'Your Message\') this.value = \'\';">Your Message</textarea> \
  Please enter the code below<br /> \
  <a href="#" onclick="$(\'shareForm\').down(2).next(\'img\').src = \'/bthandlers/captcha.ashx?dummy=\' + (new Date()).getTime(); return false">Give me another code</a><br /> \
  <img src="/bthandlers/captcha.ashx?dummy=' + (new Date()).getTime() + '" width="171" height="60" /> \
  <input type="text" class="detailForm" id="shareCaptcha" value="Confirmation code" onblur="if (!this.value) this.value = \'Confirmation code\';" onfocus="if (this.value == \'Confirmation code\') this.value = \'\';" /> \
  <input type="button" value="Send" class="searchSortButton" onclick="return shareFormSubmit(' + propertyId + ')" /> \
  </div> \
  <div id="dialogProgress" style="display:none; text-align:center"> \
    Mailing your form... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
    <div id="dialogDone" style="display:none; visibility:hidden"> \
      <br /> \
      <div id="dialogResult">&nbsp;</div> \
      <br /> \
      <input type="button" value="OK" class="searchSortButton" /> \
    </div> \
  </div> \
</div>', { title: "Send to a Friend", width: 300 });
    return false;
}




function setlbmainpic(pic)
{
lbmainpic=document.getElementById('mainpic');
lbmainpic.src="http://photolib.barfoot.co.nz/ListingPhotos/web800/"+pic+".jpg";
}

function galleryOpen(pic1,pic2,pic3,pic4,pic5,pic6,pic7,pic8,pic9,pic10,pic11,pic12,pic13,pic14,pic15,pic16,pic17,pic18) {
var thumbstr= '<li id="'+pic1+'" class="lbThumb selectedLbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic1+'\');" class="'+pic1+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic1+'.jpg" /></a></li>'
if (pic2) {thumbstr +='<li id="'+pic2+'"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic2+'\');" class="'+pic2+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic2+'.jpg" /></a></li>'}
if (pic3) {thumbstr +='<li id="'+pic3+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic3+'\');" class="'+pic3+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic3+'.jpg" /></a></li>'}
if (pic4) {thumbstr +='<li id="'+pic4+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic4+'\');" class="'+pic4+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic4+'.jpg" /></a></li>'}
if (pic5) {thumbstr +='<li id="'+pic5+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic5+'\');" class="'+pic5+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic5+'.jpg" /></a></li>'}
if (pic6) {thumbstr +='<li id="'+pic6+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic6+'\');" class="'+pic6+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic6+'.jpg" /></a></li>'}
if (pic7) {thumbstr +='<li id="'+pic7+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic7+'\');" class="'+pic7+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic7+'.jpg" /></a></li>'}
if (pic8) {thumbstr +='<li id="'+pic8+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic8+'\');" class="'+pic8+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic8+'.jpg" /></a></li>'}
if (pic9) {thumbstr +='<li id="'+pic9+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic9+'\');" class="'+pic9+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic9+'.jpg" /></a></li>'}
if (pic10) {thumbstr +='<li id="'+pic10+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic10+'\');" class="'+pic10+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic10+'.jpg" /></a></li>'}
if (pic11) {thumbstr +='<li id="'+pic11+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic11+'\');" class="'+pic11+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic11+'.jpg" /></a></li>'}
if (pic12) {thumbstr +='<li id="'+pic12+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic12+'\');" class="'+pic12+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic12+'.jpg" /></a></li>'}
if (pic13) {thumbstr +='<li id="'+pic13+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic13+'\');" class="'+pic13+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic13+'.jpg" /></a></li>'}
if (pic14) {thumbstr +='<li id="'+pic14+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic14+'\');" class="'+pic14+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic14+'.jpg" /></a></li>'}
if (pic15) {thumbstr +='<li id="'+pic15+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic15+'\');" class="'+pic15+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic15+'.jpg" /></a></li>'}
if (pic16) {thumbstr +='<li id="'+pic16+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic16+'\');" class="'+pic16+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic16+'.jpg" /></a></li>'}
if (pic17) {thumbstr +='<li id="'+pic17+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic17+'\');" class="'+pic17+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic17+'.jpg" /></a></li>'}
if (pic18) {thumbstr +='<li id="'+pic18+'" class="lbThumb"><a href="#" onmouseup="javascript:setlbmainpic(\''+pic18+'\');" class="'+pic18+'"><img src="http://photolib.barfoot.co.nz/ListingPhotos/thumb37h/'+pic18+'.jpg" /></a></li>'}
    Modalbox.show(
'<div id="wrapper"> \
		<div id="lbContainer"> \
	        <div id="lbTopbar"> \
	            <div id="lbScroller"> \
	                <div id="lbCounterContainer"> \
	                    <div class="lbprev"></div> \
						<div id="lbCounter">Photo <span id="lbPhotoCountCurrent"></span> of <span id="lbPhotoCountMax"></span></div> \
	                    <div class="lbnext"></div> \
	                </div> \
	            </div> \
        <div id="lbBodyContainer"> \
            <div id="lbMainPhotoOuter"> \
                <table cellspacing="0" cellpadding="0"> \
                    <tbody> \
                        <tr> \
                            <td id="lbMainPhotoContainer"><img id="mainpic" class="lbMainPhoto" class="" alt="" src="http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic1+'.jpg" /></td> \
                        </tr> \
                    </tbody> \
                </table> \
            </div> \
            <div id="lbThumbOuter"> \
                <div id="lbThumbContainer"> \
                	<ul id="lbThumbs">'+thumbstr+'</ul> \
	                <div id="lbThumbPadder"></div> \
                </div> \
            </div> \
		</div> \
</div>', { title: "Large Image Gallery", width: 932, height:627 });
    return false;
}

function galleryOpen2(startpic,pic1,pic2,pic3,pic4,pic5,pic6,pic7,pic8,pic9,pic10,pic11,pic12,pic13,pic14,pic15,pic16,pic17,pic18) {
var thumbstr= '<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic1+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic1+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'
if (pic2) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic2+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic2+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic3) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic3+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic3+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic4) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic4+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic4+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic5) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic5+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic5+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic6) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic6+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic6+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic7) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic7+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic7+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic8) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic8+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic8+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic9) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic9+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic9+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic10) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic10+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic10+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic11) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic11+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic11+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic12) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic12+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic12+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic13) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic13+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic13+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic14) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic14+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic14+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic15) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic15+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic15+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic16) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic16+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic16+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic17) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic17+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic17+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
if (pic18) {thumbstr +='<div><li><a href=" http://photolib.barfoot.co.nz/ListingPhotos/web800/'+pic18+'"><img src=" http://photolib.barfoot.co.nz/ListingPhotos/low/'+pic18+'" border="0" onerror="src=\'/images/no-image-small.jpg\'" /></a></li></div>'}
Modalbox.show(
'<div class="lbgalleryWrapper"> \
	<div id="lbgallery" class="lbcontent"> \
		<div id="lbslideshow"></div> \
		<div id="lbnavigation"> \
			<ul class="lbthumbs noscript">'+thumbstr+'</ul> \
		</div> \
		<div id="lbcontrols" class="lbcontrols" style="margin-top:20px"></div> \
	</div> \
</div> \
<script type="text/javascript">Modalslideshow=1;startimg='+startpic+';jQuery(document).ready(function() {var lbgallery = jQuery(\'#lbgallery\').galleriffic(\'#lbnavigation\', { \
delay:3000,numThumbs:18,imageContainerSel:\'#lbslideshow\',controlsContainerSel:\'#lbcontrols\',titleContainerSel:\'#lbimage-title\', \
descContainerSel:\'#lbimage-desc\',downloadLinkSel:\'#lbdownload-link\'}); \
});</script>', { title: "Large Image Gallery", width: 970, height:629 });
    return false;
}

function shareFormSubmit(propertyId) {
    var name = $("shareName").value;
    if (!name || (name == "Your Name")) {
        alert("Please enter your name.");
        $("shareName").focus();
        return false;
    }
    var email = $("shareEmail").value;
    if (!email || !email.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        alert("Please enter your valid email.");
        $("shareEmail").focus();
        return false;
    }
    var friendName = $("shareFriendName").value;
    if (!friendName || (friendName == "Your Friend's Name")) {
        alert("Please enter your friend's name.");
        $("shareFriendName").focus();
        return false;
    }
    var friendEmail = $("shareFriendEmail").value;
    if (!friendEmail || !friendEmail.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        alert("Please enter your friend's valid email.");
        $("shareFriendEmail").focus();
        return false;
    }
    var captcha = $("shareCaptcha").value;
    if (!captcha || (captcha == "Confirmation code")) {
        alert("Please enter confirmation code.");
        $("shareCaptcha").focus();
        return false;
    }

    var onclickHandler = function() {
        Modalbox.MBcaption.update("Send to a Friend");
        $("dialogProgress").hide();
        $("shareFormBody").style.visibility = "hidden";
        $("shareFormBody").show();
        Modalbox.resizeToContent({ afterResize: function() {
            $("shareFormBody").style.visibility = "visible";
        } });
        return false;
    };

    Modalbox.MBcaption.update("Action in progress...");
    $("shareFormBody").hide();
    $("dialogLoading").show();
    $("dialogDone").style.visibility = "hidden";
    $("dialogDone").hide();
    $("dialogProgress").show();
    
    Modalbox.resizeToContent({ afterResize: function() {
        new Ajax.Request("/bthandlers/mailer.ashx", {
            method: "post",
            parameters: {
                action: "sendToAFriend",
                name: name,
                email: email,
                friendName: friendName,
                friendEmail: friendEmail,
                message: $("shareMessage").value,
                captcha: captcha,
                propertyId: propertyId
            },
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclickHandler;
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = "Your form was mailed successfully."; break;
                    case "1": result = "Invalid email address. Enter a valid email address and try again."; break;
                    case "2": result = "Invalid input. Make sure you've filled the form out correctly and try again."; break;
                    case "3": result = "Unable to mail your form. Try again later."; break;
                    case "4": result = "Confirmation code mismatch. Try again."; $("shareForm").down(2).next("img").src = "/bthandlers/captcha.ashx?dummy=" + (new Date()).getTime();
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = (transport.responseText == "0" ? function() { Modalbox.hide(); return false; } : onclickHandler);
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
        } });
    return false;
}

function favouritesHandler(action, propertyId) {
    Modalbox.show(
'<div id="dialogProgress" style="text-align:center"> \
  ' + (action == "add" ? "Saving to your favourite properties" : "Removing from your favourite properties") + '... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" /> \
  </div> \
</div>', { title: "Action in progress...", width: 300, afterLoad: function() {
        new Ajax.Request("/bthandlers/favourites.ashx", {
            method: "post",
            parameters: {
                action: action,
                propertyId : propertyId
            },
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = function() {
                        Modalbox.hide();
                        return false;
                    };
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = (action == "add" ? "Property saved successfully." : "Property removed successfully."); break;
                    case "1": result = (action == "add" ? "Unable to save property. Either it's already on your list or your list has reached its maximum capacity." : 
                        "Unable to remove property. It's not on your list."); break;
                    case "2": result = "Invalid input parameters.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = ((action != "add") && (transport.responseText == "0") ? function() { 
                    Modalbox.hide(); 
                    window.location.reload();
                    return false;
                } : function() {
                    Modalbox.hide();
                    return false;
                });
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
    } });
    return false;
}

// following 2 functions basically mimic functionality of contactFormOpen(...) and contactFormSubmit(...); the only
// difference is that the form itself lives inside the modal box... we're duplicating alot of code here, this 
// should be refactored if there's enough time...
function contactFormOpenBox(type, code) {
    var title = "Contact Form";
    switch(type) {
        case "propertyAltAgent":
        case "property": title = "Ask a Question"; break;
        case "agent": title = "Contact Me"; break;
        case "branch": title = "Contact Us";
    }
    Modalbox.show(
'<div class="shareWithFriendForm" id="contactFormBody"> \
  <input type="text" class="detailForm" id="contactNameBox" value="Your Name" onblur="if (!this.value) this.value = \'Your Name\';" onfocus="if (this.value == \'Your Name\') this.value = \'\';" /> \
  <input type="text" class="detailForm" id="contactEmailBox" value="Your Email" onblur="if (!this.value) this.value = \'Your Email\';" onfocus="if (this.value == \'Your Email\') this.value = \'\';" /> \
  <input type="text" class="detailForm" id="contactPhoneBox" value="Your Phone #" onblur="if (!this.value) this.value = \'Your Phone #\';" onfocus="if (this.value == \'Your Phone #\') this.value = \'\';" /> \
  <textarea id="contactMessageBox" rows="4" onblur="if (!this.value) this.value = \'Your Message\';" onfocus="if (this.value == \'Your Message\') this.value = \'\';">Your Message</textarea> \
  <input type="button" value="Send" class="searchSortButton" onclick="return contactFormSubmitBox(\'' + type + '\', \'' + code + '\')" /> \
</div> \
<div id="dialogProgress" style="display:none; text-align:center"> \
  Mailing your form... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" /> \
  </div> \
</div>', { title: title, width: 300 });
    return false;
}

function contactFormSubmitBox(type, code) {
    var name = $("contactNameBox").value;
    if (!name || (name == "Your Name")) {
        alert("Please enter your name.");
        $("contactNameBox").focus();
        return false;
    }
    var email = $("contactEmailBox").value;
    if (!email || !email.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        alert("Please enter your valid email.");
        $("contactEmailBox").focus();
        return false;
    }
    var message = $("contactMessageBox").value;
    if (!message || (message == "Your Message")) {
        alert("Please enter your message.");
        $("contactMessageBox").focus();
        return false;
    }
    
    var title = Modalbox.options.title;
    var onclickHandler = function() {
        Modalbox.MBcaption.update(title);
        $("dialogProgress").hide();
        $("contactFormBody").style.visibility = "hidden";
        $("contactFormBody").show();
        Modalbox.resizeToContent({ afterResize: function() {
            $("contactFormBody").style.visibility = "visible";
        } });
        return false;
    };

    Modalbox.MBcaption.update("Action in progress...");
    $("contactFormBody").hide();
    $("dialogLoading").show();
    $("dialogDone").style.visibility = "hidden";
    $("dialogDone").hide();
    $("dialogProgress").show();

    Modalbox.resizeToContent({ afterResize: function() {
        var params = {
                action: "askAQuestion",
                name: name,
                email: email,
                phone: $("contactPhoneBox").value,
                message: message
        };
        switch(type) {
            case "propertyAltAgent": params["altAgent"] = "true";
            case "property": params["propertyId"] = code; break;
            case "agent": params["agentCode"] = code; break;
            case "branch": params["branchId"] = code;
        }
        new Ajax.Request("/bthandlers/mailer.ashx", {
            method: "post",
            parameters: params,
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclickHandler;
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = "Your form was mailed successfully."; break;
                    case "1": result = "Invalid email address. Enter a valid email address and try again."; break;
                    case "2": result = "Invalid input. Make sure you've filled the form out correctly and try again."; break;
                    case "3": result = "Unable to mail your form. Try again later.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = (transport.responseText == "0" ? function() { Modalbox.hide(); return false; } : onclickHandler);
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
        } });
    return false;
}

function alertFormOpen(params, commercial) {
    Modalbox.show(null, { title: "Action in progress...", width: 500, beforeLoad: function() {
        new Ajax.Request("/bthandlers/alerts.ashx", {
            method: "post",
            parameters: {
                action: "checkLogin"
            },
            onFailure: function(transport) {
                Modalbox.show(
'<div style="text-align:center">Unable to contact the server. Try again later.<br /><br /> \
    <input type="button" value="OK" class="searchSortButton" onclick="Modalbox.hide(); return false" /> \
</div>');
            },
            onSuccess: function(transport) {
                Modalbox.show(
'<div id="alertFormBody"> \
  With Property Alert we will keep you up to date with all the latest properties as they come to market. Enter your \
  details below and we will email you with new properties that meet your current search criteria. For more information \
  about Property alerts <a href="#" onclick="window.open(\'/Global/Help/Stories/Property-Alerts.aspx\'); return false">click here</a>.<br /><br />' +
  (transport.responseText ? 'You are logged in as: <strong>' + transport.responseText + '</strong><br /><br />' : 
  '<label for="alertEmail">Your email address: </label><input type="text" id="alertEmail" maxlength="100" /><br /><br /> \
  <label for="alertPassword">Your password: </label><input type="password" id="alertPassword" maxlength="32" /><br /><br /> \
  If you are a first time user enter a new password here and confirmation code below. If you have forgotten your \
  password <a href="#" onclick="return remindFormOpen()">click here</a>.<br /><br /> \
  <a href="#" onclick="$(\'alertFormBody\').down().next(\'img\').src = \'/bthandlers/captcha.ashx?dummy=\' + (new Date()).getTime(); return false">Give me another code</a><br /> \
  <img src="/bthandlers/captcha.ashx?dummy=' + (new Date()).getTime() + '" width="180" height="60" /><br /><br /> \
  <label for="alertCaptcha">Confirmation code: </label><input type="text" id="alertCaptcha" /><br /><br />') +
  '<label for="alertFrequency">Receive updates: </label><select id="alertFrequency"><option>Daily</option><option>Weekly</option><option>Monthly</option></select><br /><br /> \
  <input type="checkbox" id="alertOpenHomes" style="float:left;display:inline; width:20px; "/><div>Also receive a weekly list of open homes that meet your search criteria </div><br /><br /> \
  <label for="alertName">Property Alert name: </label><input type="text" id="alertName" maxlength="100" style="width:200px;" /><br /><br /> \
  <input type="button" value="Save" class="searchSortButton" onclick="return alertFormSubmit(\'' + params + '\', ' + (transport.responseText ? true : false) + ', ' + commercial + ')" /> \
</div> \
<div id="dialogProgress" style="display:none; text-align:center"> \
  Saving your Property Alert... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" /> \
  </div> \
</div>', { title: "New Property Alert" });
            }
        });
        return false;
    } });
    return false;
}

function alertFormSubmit(params, loggedIn, commercial) {
    var email = null;
    var password = null;
    var captcha = null;
    if (!loggedIn) {
        email = $("alertEmail").value;
        if (!email || !email.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
            alert("Please enter your valid email.");
            $("alertEmail").focus();
            return false;
        }
        password = $("alertPassword").value;
        if (!password) {
            alert("Please enter your password.");
            $("alertPassword").focus();
            return false;
        }
        captcha = $("alertCaptcha").value;
    }
    var name = $("alertName").value;
    if (!name) {
        alert("Please enter alert name.");
        $("alertName").focus();
        return false;
    }

    var onclickHandler = function() {
        Modalbox.MBcaption.update("New Property Alert");
        $("dialogProgress").hide();
        $("alertFormBody").style.visibility = "hidden";
        $("alertFormBody").show();
        Modalbox.resizeToContent({ afterResize: function() {
            $("alertFormBody").style.visibility = "visible";
        } });
        return false;
    };
    
    Modalbox.MBcaption.update("Action in progress...");
    $("alertFormBody").hide();
    $("dialogLoading").show();
    $("dialogDone").style.visibility = "hidden";
    $("dialogDone").hide();
    $("dialogProgress").show();
    
    Modalbox.resizeToContent({ afterResize: function() {
        new Ajax.Request("/bthandlers/alerts.ashx?" + params, {
            method: "post",
            parameters: {
                action: "add",
                email: email,
                password: password,
                captcha: captcha,
                frequency: $("alertFrequency").selectedIndex,
                openHomes: ($("alertOpenHomes").checked ? 1 : 0),
                name: name,
                commercial: (commercial ? 1 : 0)
            },
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclickHandler;
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = "Your property alert was saved successfully."; break;
                    case "1": result = "Invalid login or password. Try again."; break;
                    case "2": 
                        result = "Confirmation code mismatch. Try again."; 
                        $("alertFormBody").down().next("img").src = "/bthandlers/captcha.ashx?dummy=" + (new Date()).getTime();
                        break;
                    case "3": 
                        result = "Unable to create new user account. Try again later please."; 
                        $("alertFormBody").down().next("img").src = "/bthandlers/captcha.ashx?dummy=" + (new Date()).getTime();
                        break;
                    case "4": result = "Unable to create new property alert. Try again later please."; break;
                    case "6": result = "Invalid input. Make sure you've filled the form out correctly and try again.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = (transport.responseText == "0" ? function() { Modalbox.hide(); return false; } : onclickHandler);
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
        } });
    return false;
}

function alertsHandler(action, alertId) {
    Modalbox.show(
'<div id="dialogProgress" style="text-align:center"> \
  ' + (action == "activate" ? "Activating selected property alert" : "Deleting selected property alert") + '... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" /> \
  </div> \
</div>', { title: "Action in progress...", width: 300, afterLoad: function() {
        new Ajax.Request("/bthandlers/alerts.ashx", {
            method: "post",
            parameters: {
                action: action,
                alertId : alertId
            },
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = function() {
                        Modalbox.hide();
                        return false;
                    };
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = (action == "activate" ? "Property alert activated successfully." : "Property alert deleted successfully."); break;
                    case "4": result = (action == "activate" ? "Unable to activate property alert." : "Unable to delete property alert."); break;
                    case "6": result = "Invalid input parameters.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = ((transport.responseText == "0") ? function() { 
                    Modalbox.hide();
                    var pos = window.location.href.indexOf("?");
                    if (pos >= 0) {
                        window.location.replace(window.location.href.substring(0, pos));
                    } else {
                        window.location.replace(window.location.href);
                    }
                    return false;
                } : function() {
                    Modalbox.hide();
                    return false;
                });
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
    } });
    return false;
}

function remindFormOpen() {
    Modalbox.show(
'<div class="shareWithFriendForm" id="remindFormBody"> \
  <input type="text" class="detailForm" id="remindEmail" value="Your Email" maxlength="100" onblur="if (!this.value) this.value = \'Your Email\';" onfocus="if (this.value == \'Your Email\') this.value = \'\';" /> \
  Please enter the code below \
  <img src="/bthandlers/captcha.ashx?dummy=' + (new Date()).getTime() + '" width="171" height="60" /> \
  <input type="text" class="detailForm" id="remindCaptcha" value="Confirmation code" maxlength="32" onblur="if (!this.value) this.value = \'Confirmation code\';" onfocus="if (this.value == \'Confirmation code\') this.value = \'\';" /> \
  <input type="button" value="Send" class="searchSortButton" onclick="return remindFormSubmit()" /> \
</div> \
<div id="dialogProgress" style="display:none; text-align:center"> \
  Retrieving your password... please wait.<div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden"> \
    <br /> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="OK" class="searchSortButton" /> \
  </div> \
</div>', { title: "Password Retrieval", width: 300 });
    return false;
}

function remindFormSubmit() {
    var email = $("remindEmail").value;
    if (!email || !email.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
        alert("Please enter your valid email.");
        $("remindEmail").focus();
        return false;
    }
    var captcha = $("remindCaptcha").value;
    if (!captcha || (captcha == "Confirmation code")) {
        alert("Please enter confirmation code.");
        $("remindCaptcha").focus();
        return false;
    }
    
    var onclickHandler = function() {
        Modalbox.MBcaption.update("Password Retrieval");
        $("dialogProgress").hide();
        $("remindFormBody").style.visibility = "hidden";
        $("remindFormBody").show();
        Modalbox.resizeToContent({ afterResize: function() {
            $("remindFormBody").style.visibility = "visible";
        } });
        return false;
    };

    Modalbox.MBcaption.update("Action in progress...");
    $("remindFormBody").hide();
    $("dialogLoading").show();
    $("dialogDone").style.visibility = "hidden";
    $("dialogDone").hide();
    $("dialogProgress").show();

    Modalbox.resizeToContent({ afterResize: function() {
        new Ajax.Request("/bthandlers/alerts.ashx", {
            method: "post",
            parameters: {
                action: "retrievePassword",
                email: email,
                captcha: captcha
            },
            onFailure: function(transport) {
                $("dialogResult").update("Unable to contact the server. Try again later.");
                $("dialogLoading").hide();
                $("dialogDone").show();
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclickHandler;
                    $("dialogDone").style.visibility = "visible";
                } });
            },
            onSuccess: function(transport) {
                var result = "Unknown server error. Try again later.";
                switch(transport.responseText) {
                    case "0": result = "Your password was mailed successfully."; break;
                    case "2": 
                        result = "Confirmation code mismatch. Try again."; 
                        $("remindFormBody").down().next("img").src = "/bthandlers/captcha.ashx?dummy=" + (new Date()).getTime(); 
                        break;
                    case "5": 
                        result = "Unable to retrieve your password. The account does not exist.";
                        $("remindFormBody").down().next("img").src = "/bthandlers/captcha.ashx?dummy=" + (new Date()).getTime(); 
                        break;
                    case "6": result = "Invalid input. Make sure you've filled the form out correctly and try again.";
                }
                $("dialogResult").update(result);
                $("dialogLoading").hide();
                $("dialogDone").show();
                var onclick = (transport.responseText == "0" ? function() { Modalbox.hide(); return false; } : onclickHandler);
                Modalbox.resizeToContent({ afterResize: function() {
                    $("dialogDone").down().next("input").onclick = onclick;
                    $("dialogDone").style.visibility = "visible";
                } });
            } });
        } });
    return false;
}
function openlb(url) {
   var objLink = document.createElement('a');
   objLink.setAttribute('href',url);
   objLink.setAttribute('rel','lightbox[lightshow]');
   //objLink.setAttribute('title',caption);
   Lightbox.prototype.start(objLink);
}

function documentFormOpen(listingID, mode, scID) {

    if (mode == "new") {
        Modalbox.show(
'<div id="documentForm"> \
    <iframe id="ifrFile" scrolling="no" frameborder="0" hidefocus="true" style="display:block;width:330px;height:180px;overflow:hidden" src="/FileUpload.aspx?lid=' + listingID + '&mode=new"></iframe> \
</div> \
<div id="dialogProgress" style="display:none; text-align:left"> \
  <div id="dialogText">Saving your Property document... please wait.</div><div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden;"> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="CLOSE" class="searchSortButton" style="float:right;"/> \
  </div> \
</div>', { title: "Add A Document", width: 350, afterHide: function() { window.location.href = window.location.href; } });

    } else if(mode == "edit") {
    Modalbox.show(
'<div id="documentForm"> \
    <iframe id="ifrFile" scrolling="no" frameborder="0" hidefocus="true" style="display:block;width:330px;height:180px;overflow:hidden" src="/FileUpload.aspx?lid=' + listingID + '&mode=edit&scid=' + scID + '"></iframe> \
</div> \
<div id="dialogProgress" style="display:none; text-align:left;"> \
  <div id="dialogText">Updating your Property document... please wait.</div><div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden;"> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="CLOSE" class="searchSortButton" style="float:right;"/> \
  </div> \
</div>', { title: "Update Property Document", width: 350, afterHide: function() { window.location.href = window.location.href; } });

    } else if (mode == "delete") {
    Modalbox.show(
'<div id="documentForm"> \
    <iframe id="ifrFile" scrolling="no" frameborder="0" hidefocus="true" style="display:block;width:330px;height:80px;overflow:hidden" src="/FileUpload.aspx?lid=' + listingID + '&mode=delete&scid=' + scID + '"></iframe> \
</div> \
<div id="dialogProgress" style="display:none; text-align:left"> \
  <div id="dialogText">Deleting the Property document... please wait.</div><div id="dialogLoading" class="MB_loading">&nbsp;</div> \
  <div id="dialogDone" style="display:none; visibility:hidden;"> \
    <div id="dialogResult">&nbsp;</div> \
    <br /> \
    <input type="button" value="CLOSE" class="searchSortButton" style="float:right;"/> \
  </div> \
</div>', { title: "Delete Property document", width: 350, afterHide: function() { window.location.href = window.location.href; } });
    }
  
    return false;
}


function saveDocumentProgress() {
    $("documentForm").hide();
    $("dialogProgress").show();
    $("dialogLoading").show();
    Modalbox.resizeToContent();
}

function saveDocumentResult(result) {
    $("dialogResult").update(result);
    $("dialogText").hide();
    $("dialogLoading").hide();
    $("dialogDone").show();

    Modalbox.resizeToContent({ afterResize: function() {
        $("dialogDone").down().next("input").onclick = function() { Modalbox.hide(); return false; };
        $("dialogDone").style.visibility = "visible";
    }});
}

function btnCancel_onClick(){
    window.parent.Modalbox.hide();
    return false;
}

function btnDelete_onClick(){
    $("hiddenFileUpload").submit();
    window.parent.saveDocumentProgress();
    return false;
}

function documentUploadValidation(mode) {
    var docType = $("docType");
    if (!docType.selectedIndex) {
        alert("Please select a document type.");
        docType.focus();
        return false;
    }
    var docDesc = $("docDescription").value;
    if (!docDesc) {
        alert("Please enter document description.");
        $("docDescription").focus();
        return false;
    }
    var filePath = $("filePath").value;
    if ((mode == "new") && (!filePath)) {
        alert("Please select a file.");
        $("filePath").focus();
        return false;
    }
    if (filePath) {
        if ( (getExt(filePath) != "pdf") && (getExt(filePath) != "rtf")){
            alert("Invalid file type. Only PDF and RTF files are supported.");
            $("filePath").focus();
            return false;
        }
     }
    //cobyz:After the validation, we submit the Form
     //$("hiddenFileUpload").submit();
    document.getElementById("hiddenFileUpload").submit();
    window.parent.saveDocumentProgress();
    return false; 
}

function getExt(file){
	return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : "";
}
