﻿/*
	Property Details Scripts
	-----------------------
*/

/* Swap Photo */
var cimage = null;
function swapphoto(o) {
    if (cimage == null) {
        cimage = o.parentNode.parentNode.firstChild;
    }
    if (o.parentNode != cimage) {
        document.getElementById("propertyfullpic").src = o.firstChild.src;
        document.getElementById("propertyfullpic").className = o.firstChild.className;
        o.parentNode.className = "current";
        cimage.className = "";
        cimage = o.parentNode;
    }
}

/* Tabs */
var cTab = null;
function showtab(o) {
    if (cTab != o) {
        if (cTab == null) {
            var ul = o.parentNode.parentNode;
            cTab = ul.getElementsByTagName("A")[0];
        }

        document.getElementById("tab" + o.firstChild.nodeValue.replace(/\s/g, "")).className = "";
        document.getElementById("tab" + cTab.firstChild.nodeValue.replace(/\s/g, "")).className = "hidden";

        cTab.parentNode.className = "";
        o.parentNode.className = "on";

        cTab = o;
    }
}

/* Map */

function propertymap(lat, lng) {

    this.lat = lat;
    this.lng = lng;

    // Init map
    if (this.lat != null) {
        document.getElementById("tabMap").className = "";
        this.pmap = new geomap();
        this.pmap.loadsinglepoint(this.lat, this.lng);
        window.setTimeout(function() { document.getElementById("tabMap").className = "hidden" }, 500);
    }
}

// Map
function geomap() {

    this.map = null;
    this.points = null;
    this.cIcon = null;
    this.bounds = new GLatLngBounds();
    this.cmarker = null;

    if (GBrowserIsCompatible()) {
        // Create a map and a geocoder object
        this.geocoder = new GClientGeocoder();
        // functions
        var self = this;

        this.initmap = function() {
            this.map = new GMap2(document.getElementById("PropertyMap"));

            // Create icon
            this.cIcon = new GIcon(G_DEFAULT_ICON);
            this.cIcon.iconSize = new GSize(17, 28);
            this.cIcon.image = "../_resources/files/icons/map-marker.png";
            this.cIcon.shadow = "../_resources/files/icons/map-shadow.png";
            this.cIcon.shadowSize = new GSize(23, 28);
            this.cIcon.iconAnchor = new GPoint(8, 27);
            this.cIcon.infoWindowAnchor = new GPoint(8, 14);

        }

        this.initmap();

        this.loadsinglepoint = function(lat, lng) {
            self.map.clearOverlays();
            var point = new GLatLng(lat, lng);
            self.map.setCenter(point, 15);
            // Add marker
            var marker = new GMarker(point); //, { icon: self.cIcon });
            self.map.addOverlay(marker);
            self.map.setCenter(point);
            // Add controls
            self.map.addControl(new GLargeMapControl3D());
            self.map.addControl(new GMapTypeControl());
            var options = { searchFormHint: "Search local area" };
            self.map.addControl(new google.maps.LocalSearch(options));
        }

        this.loadpoints = function() {
            //self.map.clearOverlays();
            self.map.setUIToDefault();

            for (var i = 0; i < self.points.length; i++) {
                // Add marker to map
                self.points[i].addtomap();
                // Add event to list item
                if (wcbs.isIE) {
                    document.getElementById(self.points[i].ref).attachEvent("onmouseover", self.points[i].showinfo);
                }
                else {
                    document.getElementById(self.points[i].ref).addEventListener("mouseover", self.points[i].showinfo, false);
                }
            }

            self.map.setCenter(self.bounds.getCenter());
            self.map.setZoom(self.map.getBoundsZoomLevel(self.bounds) - 1);

        }

    }
}

// Location object
function maplocation(l, m) {

    this.ref = l.ref;
    this.title = l.title;
    this.price = l.price;
    this.img = l.img;
    this.text = l.text;
    this.lat = l.lat;
    this.lng = l.lng;
    this.mapcontrol = m;
    this.index = m.points.length;
    this.marker = null;

    var self = this;

    this.addtomap = function() {

        var cpoint = new GLatLng(self.lat, self.lng);

        self.marker = createmarker(cpoint, "<div class='mapdetails'><img src='" + self.img + "' /><h3>" + self.price + "<br /><span>" + self.title + "</span></h3><p>" + self.text + "</p></div>", self.mapcontrol.cIcon);
        self.mapcontrol.map.addOverlay(self.marker);

        self.mapcontrol.bounds.extend(cpoint)

    }

    this.showinfo = function() {
        if (self.mapcontrol.cmarker != self) {
            GEvent.trigger(self.mapcontrol.points[self.index].marker, "click");
            self.mapcontrol.cmarker = self;
        }
    }

}

// Creat marker
function createmarker(point, markerhtml, markerOptions) {

    var marker
    if (markerOptions) {
        marker = new GMarker(point, markerOptions);
    }
    else {
        marker = new GMarker(point);
    }
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(markerhtml);
    });

    return marker;

}


/* Enquiry Form */
function showenquiry() {
    document.getElementById("propertydetailscontent").className = "hidden";
    document.getElementById("propertydetailsenquiry").className = "";
}

function cancelenquiry() {
    document.getElementById("propertydetailsenquiry").className = "hidden";
    document.getElementById("propertydetailscontent").className = "";
}

function sendenquiry(pid, ela) {
    // validate
    var name, tel, email, details;
    name = document.getElementById("txtName").value;
    tel = document.getElementById("txtTel").value;
    email = document.getElementById("txtEmail").value;
    details = document.getElementById("txtComments").value;

    // Validate form
    var valForm = new Array();
    if (name == "") {
        valForm[valForm.length] = '\r\n Name is a required field';
    }
    if (tel == "") {
        valForm[valForm.length] = '\r\n Tel is a required field';
    }
    if (email == "") {
        valForm[valForm.length] = '\r\n e-mail is a required field';
    }
    else if (!ValidateItem('email', email)) {
        valForm[valForm.length] = '\r\n Please enter a valid e-mail address';
    }
    if (details == "") {
        valForm[valForm.length] = '\r\n Details is a required field';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Disable the buttons
        document.getElementById("btnSend").readOnly = true;
        document.getElementById("btnSend").disabled = true;
        // Send
        if (ela) {
            PageMethods.Send_Enquiry_ELA(pid, name, tel, email, details, sendenquirysuccess, ajaxfailure);
        }
        else {
            PageMethods.Send_Enquiry(pid, name, tel, email, details, sendenquirysuccess, ajaxfailure);
        }
    }
}

function sendenquirysuccess(value, ctx, methodName) { 
    // Reload this pages section
    if (value == "False") {
        // Failed
        document.getElementById("enquiryform").innerHTML = "<h2>Enquiry Not Sent</h2><p>Your enquiry could not be sent at this time. Please try again later.</p>";
    }
    else {
        // Success
        document.getElementById("enquiryform").innerHTML = "<h2>Enquiry Received</h2><p>Thank you, your enquiry has been received and we will be in contact shortly.</p>";
    }
}

function sendfriend(pid, ela) {
    // validate
    var name, email, fstring;
    var friends = new Array();
    name = document.getElementById("txtTName").value;
    email = document.getElementById("txtTEmail").value;
    fstring = document.getElementById("txtTFriends").value;
    friends = fstring.split("\n");

    // Validate form
    var valForm = new Array();
    if (name == "") {
        valForm[valForm.length] = '\r\n Your Name is a required field';
    }
    if (email == "") {
        valForm[valForm.length] = '\r\n Your e-mail is a required field';
    }
    else if (!ValidateItem('email', email)) {
        valForm[valForm.length] = '\r\n Please enter a valid e-mail address';
    }
    if (fstring == "") {
        valForm[valForm.length] = '\r\n At least one friend email address is a required field';
    }
    else if (friends.length > 5) {
        valForm[valForm.length] = '\r\n Please enter a maximum of 5 e-mail addresses';
    }
    else {
        for (var i = 0; i < friends.length; i++) {
            if (!ValidateItem('email', friends[i])) {
                valForm[valForm.length] = '\r\n Friend e-mail ' + (i + 1) + ' is invalid';
            } 
        }
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Disable the buttons
        document.getElementById("btnTSend").readOnly = true;
        document.getElementById("btnTSend").disabled = true;
        // Send
        if (ela) {
            PageMethods.Send_Friends_ELA(pid, name, email, friends, sendfriendsuccess, ajaxfailure);
        }
        else {
            PageMethods.Send_Friends(pid, name, email, friends, sendfriendsuccess, ajaxfailure);
        }
    }
}

function sendfriendsuccess(value, ctx, methodName) {
    // Reload this pages section
    if (value.length == 0) {
        // Failed
        document.getElementById("notmessage").innerHTML = "<p>An e-mail was successfully sent to the specified e-mail addresses.</p>";
    }
    else {
        // Success
        document.getElementById("notmessage").innerHTML = "<p>The following email addresses could not be reached.</p><p>" + value.join("<br />") + "</p>";
    }
    // Enable the buttons
    document.getElementById("btnTSend").readOnly = false;
    document.getElementById("btnTSend").disabled = false;
}

/*
Validations
*/
function ValidateItem(ftype, fvalue)
{
    switch(ftype)
    {
	    case "email":
            var objRegExp = /^.+@[^\.].*\.[a-z]{2,}$/;
            //check for format
            return objRegExp.test(fvalue);
    }
} 

/*
    AJAX Method Failure
*/
function ajaxfailure(ex, ctx, methodName) { 
    alert(ex.get_message()); 
    // Hide any passed loader animations
    document.getElementById("btnSend").readOnly = false;
    document.getElementById("btnSend").disabled = false;
    document.getElementById("btnCancel").readOnly = false;
    document.getElementById("btnCancel").disabled = false;
} 


