﻿var B144_HOST_DOMAIN = "http://www.b144.co.il";

var isIE = false;

if (navigator.appName == 'Microsoft Internet Explorer') {
    isIE = true;
}



function B144API(containerID) {
    if (B144API.instance != null)
        return B144API.instance;
    B144API.instance = this;
    this.Type = "B144API";
    this.Message = "";
    this.MessageIndex = 0;
    this.Elements = new Array();
    this.DefaultInterval = 100;
    this.QueueInterval = 200;
    if (containerID != null) {
        return this.AddElement("B144Div", containerID);
    }
    this.HF = null;
}
function B144_Element(index, containerID) {
    this.Type = "Element";
    this.ID;
    this.Index = index;
    this.Container = document.getElementById(containerID);
    this.Frame = null;
    this.initialized = false;
    this.documentUrl = "";
    this.city = "";
    this.street = "";
    this.streetNo = "";

    //Notice: message and form are optional!
    this.Init = function(width, height, form) {
        this.BuildFrame();
        this.ResizeFrame(width, height);
        this.DoSubmit(form)
    }
    this.BuildFrame = function() {
        if (this.frame == null) {
            var frameID = "B144" + this.Type + "Frame" + this.Index;
            if (isIE) {
                var ifrm = document.createElement("<IFRAME name='" + frameID + "' frameborder='0'>");
                ifrm.id = frameID;
            }
            else {
                var ifrm = document.createElement("IFRAME");
                ifrm.setAttribute("name", frameID);
                ifrm.setAttribute("id", frameID);
                ifrm.setAttribute("frameborder", "0");
            }
            ifrm.frameborder = "0";
            ifrm.scrolling = "no";
            ifrm.style.margin = "0px";
            ifrm.style.padding = "0px";

            this.Frame = this.Container.appendChild(ifrm);
        }
    }
    this.ResizeFrame = function(width, height) {
        this.Frame.width = width;
        this.Frame.height = height;
    }
    this.DoSubmit = function(f) {
        var isTemp = false;
        if (f == null) {
            f = document.createElement("FORM");
            this.Container.appendChild(f);
            isTemp = true;
        }
        f.method = "POST";
        f.target = this.Frame.id;
        f.action = this.DocumentUrl + "&CallerURL=" + document.location.href.split("#")[0];
        f.submit();
        this.initialized = true;
        if (isTemp)
            this.Container.removeChild(f);
    }

    this.DoMapSubmit = function(city, street, houseNo, formID) {
        if (formID != null) {
            this.BuildFrame();
            var form = document.getElementById(formID);
            if (form == null) {
                alert("Wrong form ID")
                return;
            }
            form.method = "Post";
        }
        f = form;
        var isTemp = false;
        if (f == null) {
            f = document.createElement("FORM");
            this.Container.appendChild(f);
            isTemp = true;
        }
        f.method = "POST";
        f.target = this.Frame.id;
        var addInput = function(name, value, f) {
            if (isIE) {
                var input = document.createElement("<INPUT type='hidden' name='" + name + "' id='" + name + "' >");
            }
            else {
                var input = document.createElement("INPUT");
                input.setAttribute("name", name);
                input.setAttribute("id", name);
                input.setAttribute("type", "hidden");
            }
            input.value = value;
            form.appendChild(input);
        }
        if (city != null)
            addInput("cityCode", city, f);
        if (street != null)
            addInput("streetCode", street, f);
        if (houseNo != null)
            addInput("houseNum", houseNo, f);
        f.action = this.DocumentUrl + "#ID=" + this.ID + "&CallerURL=" + document.location.href.split("#")[0];
        f.submit();
        this.initialized = true;
        if (isTemp)
            this.Container.removeChild(f);
    }
    this.DoRouteSubmit = function(city, street, houseNo, formID) {
        if (formID != null) {
            this.BuildFrame();
            var form = document.getElementById(formID);
            if (form == null) {
                alert("Wrong form ID")
                return;
            }
            form.method = "Post";
        }
        f = form;
        var isTemp = false;
        if (f == null) {
            f = document.createElement("FORM");
            this.Container.appendChild(f);
            isTemp = true;
        }
        f.method = "POST";
        f.target = this.Frame.id;
        var addInput = function(name, value, f) {
            if (isIE) {
                var input = document.createElement("<INPUT type='hidden' name='" + name + "' id='" + name + "' >");
            }
            else {
                var input = document.createElement("INPUT");
                input.setAttribute("name", name);
                input.setAttribute("id", name);
                input.setAttribute("type", "hidden");
            }
            input.value = value;
            form.appendChild(input);
        }
        if (city != null)
            addInput("city", decodeURIComponent(city), f);
        if (street != null)
            addInput("street", decodeURIComponent(street), f);
        if (houseNo != null)
            addInput("house_Num", decodeURIComponent(houseNo), f);
        f.action = this.DocumentUrl + "#ID=" + this.ID + "&CallerURL=" + document.location.href.split("#")[0];
        f.submit();
        this.initialized = true;
        if (isTemp)
            this.Container.removeChild(f);
    }
}
B144API.instance == null;
B144API.prototype.AddElement = function(type, startParams) {
    return this.Elements[this.Elements.length] = eval("new B144_" + type + "(" + this.Elements.length + ",'" + startParams + "');");
}

//Initialization methods
B144API.prototype.SubmitForm = function(formID, city, street, houseNo) {
    if (formID != null) {
        this.BuildFrame();
        var form = document.getElementById(formID);
        if (form == null) {
            alert("Wrong form ID")
            return;
        }
        form.method = "Post";
        this.DoMapSubmit(city, street, houseNo, form);
        this.form = null;
    }
}

function B144_RouteDiv(index, containerID) {
    this.InheriteFrom = B144_Element;
    this.InheriteFrom(index, containerID);
    if (this.Container == null) {
        alert("Cannot initiate the B144Div: No element '" + containerID + "' id!")
        return null;
    }
    this.DocumentUrl = B144_HOST_DOMAIN + "/route_filter.aspx";

}
function B144_MapDiv(index, containerID) {
    this.InheriteFrom = B144_Element;
    this.InheriteFrom(index, containerID);
    if (this.Container == null) {
        alert("Cannot initiate the B144Div: No element '" + containerID + "' id!")
        return null;
    }
    this.DocumentUrl = B144_HOST_DOMAIN + "/b144_map.aspx";
}
