﻿function CarParkObject(ident,cpname,cpstreet,cpsuburb,cppostcode,cpphone,cpregionid,cpcityid,cpmonthlyavailable,cpmaxheight,cpspecials,cpservices,cpspecialinstructions,cpadditionalinfo,cpstaticmap,cpmaplongitude,cpmaplatitude,cpzoomlevel,cplongitude,cplatitude, cpdefaultimage, cpalttext, cspecialregionid) {

    this.identity = ident;
    this.name = cpname;
    this.street = cpstreet;
    this.suburb = cpsuburb
    this.postcode = cppostcode;
    this.phone = cpphone;
    this.regionid = cpregionid;
    this.cityid = cpcityid;
    this.monthlyavailable = cpmonthlyavailable;
    this.maxheight = cpmaxheight;
    this.specials = cpspecials;
    this.services = cpservices;
    this.specialinstructions = cpspecialinstructions;
    this.additionalinfo = cpadditionalinfo;
    this.staticmap = cpstaticmap;
    this.maplongitude = cpmaplongitude;
    this.maplatitude = cpmaplatitude;
    this.zoomlevel = cpzoomlevel;
    this.longitude = cplongitude;
    this.latitude = cplatitude;
    this.defaultimage = cpdefaultimage;
    this.alttext = cpalttext;
    this.specialregionid = cspecialregionid;
    
    this.getFullDetails = function() {
        
        var str = "";
        str += "longitude : " + this.longitude + ", "; 
        str += "latitude : " + this.latitude + ", "; 
        str += "street : " + this.street + ", "; 
        str += "town : " + this.town + ", "; 
        str += "state : " + this.state + ", ";  
        str += "postcode : " + this.postcode + ", ";
        str += (this.services) ? "services length : " + this.services.length : "no services";
        
        return str;
    }
    
    this.getShortDetails = function() {
        var str = "";
        if (this.name.length > 0) {
            str += this.name + ', ';
        }
        if (this.street.length > 0) {
            str += this.street + ', ';
        }
        if (this.suburb.length > 0) {
            str += this.street + ', ';
        }
        if (this.postcode.length > 0) {
            str += this.postcode;
        }
        if (str.length > 2) {
            if (str.substring(str.length -2) == ', ') {
                str = str.substring(0,str.length -2);
            }
        }
        
        return str;
    }
    
    this.getEMSLonLat = function() {
        return new EMS.LonLat(this.longitude, this.latitude);
    }
    
    this.hasService = function(serviceId) {
        if (this.services) {
            for (var i=0;i<this.services.length;i++) {
                if (this.services[i] == serviceId)
                return true;
            }
        }
        return false;
    }
}

function findCarParkByLonLat(lon, lat) {
    for (var i=0;i<locationsArray.length;i++) {
        if (locationsArray[i].latitude == lat && locationsArray[i].longitude == lon) {
            return locationsArray[i];
        }
    }
}

function findCarParkById(id) {
    for (var i=0;i<locationsArray.length;i++) {
        if (locationsArray[i].identity == id) {
            return locationsArray[i];
        }
    }
}

function findCarParksByRegion(regionid) {
    var arrRegions = new Array();
    for (var i=0;i<locationsArray.length;i++) {
        if (locationsArray[i].regionid == regionid) {
            arrRegions.push(locationsArray[i]);
        }
    }
    return arrRegions;  
}

function findCarParksBySpecialRegion(regionid) {
    var arrRegions = new Array();
    for (var i=0;i<locationsArray.length;i++) {
        if (locationsArray[i].specialregionid == regionid) {
            arrRegions.push(locationsArray[i]);
        }
    }
    return arrRegions;  
}