var http_request = false;

function makePOSTRequest(url, parameters) {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/html");
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    
    if (!http_request) {
        alert("Cannot create XMLHTTP instance");
        return false;
    }
      
    
    http_request.open("POST", url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.onreadystatechange = alertContents;
    http_request.send(parameters);
    return true;
}


function notifyAutoHomeFinderFormUser(msg) {
    var autoHomeFinderDiv = document.getElementById("autoHomeFinderDiv");
    var div = document.createElement("div");
    var text = document.createTextNode(msg);

    var oldMsg = document.getElementById("notificationDiv");

    if (oldMsg != null) {
        autoHomeFinderDiv.removeChild(oldMsg);
    }

    div.setAttribute("id", "notificationDiv");
    //div.style.backgroundColor = "#DDDDDD";
    div.style.color = "red";
    div.appendChild(text);

    autoHomeFinderDiv.appendChild(div);
}

function submitAutoHomeFinderForm() {
    var firstAndLastName = document.getElementById("autoHomeFinderName").value;
    var email = document.getElementById("autoHomeFinderEmail").value; 
    var phone = document.getElementById("autoHomeFinderPhone").value;
    var firstName = "";
    var lastName = "";

    //var hasName = true;
    //var hasEmail = true;
    //var hasPhone = true;

    if (firstAndLastName == "your name" || firstAndLastName.length < 4) {
        notifyAutoHomeFinderFormUser("Please fill in your name.");
        //hasName = false;
        return;
    } else {
        var nameParts = firstAndLastName.split(" ");

        firstName = nameParts[0];

        //if only first name was given
        if (nameParts.length == 1) {
            lastName = "unknown";

        //if first and last name were given
        } else {
            for (var i = 1; i < nameParts.length; i++) {
                lastName += nameParts[i] + " ";
            }
        }
    }

    if (email == "email" || email.length < 7) {
        notifyAutoHomeFinderFormUser("Please fill in your email address.");
        //hasEmail = false;
        return;
    }

    if (phone == "phone" || phone.length < 7) {
        notifyAutoHomeFinderFormUser("Please fill in your phone number.");
        //hasPhone = false;
        return;
    }

    //Send out form here...
    var poststr = "fname=" + encodeURI(firstName) +
    "&lname=" + encodeURI(lastName) +
    "&email=" + encodeURI(email) +
    "&phone=" + encodeURI(phone) +
    "&formname=" + encodeURI("auto_home_finder_search") +
    "&domain=" + encodeURI("www.ecorealtyinc.ca") +
    "&referer=" + encodeURI(window.location) +
    "&search=" + encodeURI(document.getElementById("searchValue").value);

    makePOSTRequest("/submitForm.do", poststr);
}


function alertContents() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            var autoHomeFinderDiv = document.getElementById("autoHomeFinderDiv");
    
            while (autoHomeFinderDiv.hasChildNodes()) {
                autoHomeFinderDiv.removeChild(autoHomeFinderDiv.firstChild)
            }
    
            autoHomeFinderDiv.appendChild(document.createTextNode("Thank-you!  We will contact you shortly to help you with your home search."));
            autoHomeFinderDiv.style.backgroundColor = "#FAF8E9";
            autoHomeFinderDiv.style.padding = "0.5em";
        } else {
            alert("There was a problem with the request. Please try again in a few minutes. Response status: "+ http_request.status);
        }
    }
}


function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}

function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

if (window.XMLHttpRequest) {
    //document.write("<script type=\"text/javascript\" src=\"/js/bubbleTips.js\"> </script>");
    //document.write(unescape("%3Cscript src='/js/bubbleTips.js' type='text/javascript'%3E%3C/script%3E"));
    document.write("<div id=\"autoHomeFinderDiv\">Find More Listings <img id=\"autoHomeFinderTooltip\" title=\"Get access to even more MLS listings and have us email you new listings the instant they come on the market.\" src=\"/gui/images/icons/tooltip_icon.png\" /><br/><input id=\"autoHomeFinderName\" type=\"text\" name=\"name\" size=\"15\" maxlength=\"45\" value=\"your name\" onclick=\"clickclear(this, 'your name')\" onblur=\"clickrecall(this,'your name')\" /><input id=\"autoHomeFinderEmail\" type=\"text\" name=\"email\" size=\"20\" maxlength=\"45\" value=\"email\" onclick=\"clickclear(this, 'email')\" onblur=\"clickrecall(this,'email')\" /><input id=\"autoHomeFinderPhone\" type=\"text\" name=\"phone\" size=\"10\" maxlength=\"15\" value=\"phone\" onclick=\"clickclear(this, 'phone')\" onblur=\"clickrecall(this,'phone')\" /><input type=\"button\" id=\"autoHomeFinderDivButton\" value=\"Send Me Listings\" onclick=\"javascript:submitAutoHomeFinderForm()\" /></div>");
}

window.onload = function () { 
    BubbleTips.activateTipOn('autoHomeFinderTooltip');
    BubbleTips.activateTipOn('priceSearchTooltip');
    BubbleTips.activateTipOn('bedBathSearchTooltip');
}
