var lastobj;
var popupdiv = 'popupdiv';
var popupdivinner = 'popupdivinner';
var origInner;

function changeOpacity (value) {

	$('maincontainer').style.opacity = value/10;
	$('maincontainer').style.filter = 'alpha(opacity=' + value*10 + ')';
	$('sitedropshadow').style.opacity = value/10;
	$('sitedropshadow').style.filter = 'alpha(opacity=' + value*10 + ')';

}

function submitMakeAppointment (objForm) {

        var sendname = objForm.sendname.value;
        var sendemail = objForm.sendemail.value;
        var phone = objForm.phone.value;
	var viewingdate = objForm.viewingdate.value;
	var viewingtime = objForm.viewingtime.value;
        var listingid = objForm.listingid.value;

        xmlhttp = createHTTPObject();

        var getString = 'sendname=' + sendname + '&sendemail=' + sendemail + '&phone=' + phone + '&viewingdate=' + viewingdate + '&viewingtime=' + viewingtime + '&listingid=' + listingid;

        xmlhttp.open("GET", "php/makeAppointment.php?" + getString,true);
        xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {

                        destroyPopupDiv('makeappointment');
                        createPopupDiv('makeappointment_confirm');

                }
        }

        xmlhttp.send(null);

}

function submitSendToFriend (objForm) {

	var sendname = objForm.sendname.value;
	var sendemail = objForm.sendemail.value;
	var friendname = objForm.friendname.value;
	var friendemail = objForm.friendemail.value;
	var listingid = objForm.listingid.value;

	xmlhttp = createHTTPObject();

	var getString = 'sendname=' + sendname + '&sendemail=' + sendemail + '&friendname=' + friendname + '&friendemail=' + friendemail + '&listingid=' + listingid;

        xmlhttp.open("GET", "php/emailListing.php?" + getString,true);
        xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {

			destroyPopupDiv('sendtofriend');
			createPopupDiv('sendtofriend_confirm');

                }
        }

        xmlhttp.send(null);		

}

function resetSendFriendDiv () {
	$('popupinner').innerHTML = origInner;
}

function createHTTPObject () {

        if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
                try {
                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                                xmlhttp = null;
                        }
                }
        }

        return xmlhttp;

}

function injectEmail (varEmail) {

        xmlhttp = createHTTPObject();

        xmlhttp.open("GET", "addToMailingList.php?email=" + varEmail,true);
        xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {
                        var xmlDoc     = xmlhttp.responseXML;
                        var status     = xmlDoc.documentElement.getElementsByTagName("status");
                        var retCode = status[0].getAttribute("code");
                        if(retCode == 'ERROR') {
                                alert('error');
                        } else {
                                createPopupDiv('Thanks for your interest in Centro Real Estate Group.<br/><br/>Your submission has been received.<br/><br/>' + varEmail + ' has been added to our database.', 'NOFADE');
                        }
                }
        }
        xmlhttp.send(null);
}

function init() {
        var myOptions = {
                corner:'all',
                color:'fromElement',
                bgColor:'customValue',
                customValue:'#DFEDD1',
                blend:true,
                border:'#4EAB4F',
                compact:false
        }
        var roundCorners = Rico.Corner.round.bind(Rico.Corner);
        roundCorners('featuremenucorner', myOptions );
}

function showListingImage (imgid) {
        if(lastobj) { lastobj.style.visibility = 'hidden'; } else { $('lg0').style.visibility = 'hidden'; }
        $(imgid).style.visibility = 'visible';
        lastobj = $(imgid);
}
function submitNewsletter (varForm) {
        injectEmail(varForm.newsemail.value);
}
function createPopupDiv (divname) {
	changeOpacity(7);
        new Effect.Appear(divname, { duration: 0.4 });
}
function destroyPopupDiv (divname) {
        changeOpacity(10);
        new Effect.Fade(divname, { duration: 0.4 });
}
function doAppear (varDivName) {
        new Effect.Appear(varDivName, { duration: 0.4 });
}
function doFade (varDivName) {
        new Effect.Fade(varDivName, { duration: 0.4 });
}

