function wrapmegamenu(getmenuidquery, megamenuattr, menuholderid, instancename, keepmegamenulink) {
    this.getmenuidquery = getmenuidquery;
    this.attrname = megamenuattr;
    this.menuholderid = menuholderid;
    this.myinstancename = instancename;
    this.CachedMenu = new Array();
    this.CachedMenuName = new Array();
    this.CachedScript = new Array();
    this.menuclosebutton = "<span class='close'></span>"
    this.megamenuholder = function() { return this.megamenuinner().parent() };
    this.megamenuinner = function() { return jQuery("#" + this.menuholderid).find(".MeageMenuHolder") };
    this.keepmegamenulink = keepmegamenulink;
    this.autojob = null;
    this.currentmenu = null;
    this.TimeoutInt = 1000;
    this.posturl = function() {
        return window.location.href;
    }
    this.GetCacheMenu = function(menuid) {
        var i = 0;
        for (i = 0; i < this.CachedMenuName.length; i++) {
            if (this.CachedMenuName[i] == menuid) {
                return this.CachedMenu[i];
                break;
            }
        }
        return null;
    }
    this.GetCacheScript = function(menuid) {
        var i = 0;
        for (i = 0; i < this.CachedMenuName.length; i++) {
            if (this.CachedMenuName[i] == menuid) {
                return this.CachedScript[i];
                break;
            }
        }
        return null;
    }
    this.AddCacheMenu = function(menuid, menustring, script) {
        var i = 0;
        var found = false;
        menuid = menuid + "";
        for (i = 0; i < this.CachedMenuName.length; i++) {
            if (this.CachedMenuName[i] == menuid) {
                found = true;
            }
        }
        if (!found) {
            this.CachedMenuName[i + 1] = menuid;
            this.CachedMenu[i + 1] = menustring;
            this.CachedScript[i + 1] = script;
        }

    }

    this.GetMageMenu = function(event) {
        var thebutton = jQuery(event.currentTarget);
        var instance = event.data.instance;
        //clear timeout job
        instance.killautojob();
        var menuid = thebutton.attr("myid");
        if (instance.currentmenu == null || instance.currentmenu != menuid) {
            var cachedmenu = instance.GetCacheMenu(menuid);
            var cachedscript = instance.GetCacheScript(menuid);
            if (!cachedmenu || cachedmenu == null) {
                jQuery.post(instance.posturl(), { data: "sender=" + instance.myinstancename + "&" + instance.getmenuidquery + "=" + menuid }, function(data) {
                    instance.AddCacheMenu(data.menuid, data.menu, data.script);
                    instance.currentmenu = menuid;
                    instance.megamenuinner().html(instance.menuclosebutton + data.menu);
                    instance.initCloseButton();
                    instance.setReadMoreLink(thebutton.attr("href"));
                    instance.megamenuholder().attr("class", "MeageMenuOuter " + thebutton.attr("class"))
                    instance.megamenuholder().show();
                    if (data.script != "") {

                        eval(data.script)
                    }

                }, "json");
            }
            else {
                instance.megamenuinner().html(instance.menuclosebutton + cachedmenu);
                instance.initCloseButton();
                instance.setReadMoreLink(thebutton.attr("href"));
                instance.megamenuholder().attr("class", "MeageMenuOuter " + thebutton.attr("class"))
                instance.megamenuholder().show();
                instance.currentmenu = menuid;
                eval(cachedscript);

            }
        }

        return false;
    }
    this.setReadMoreLink = function(link) {
        var megamenuholder = this.megamenuholder();
        megamenuholder.find("a.readmore").attr("href", link);
    }
    this.CloseMageMenu = function() {
        if (this.megamenuholder().attr("hover") == "false" && combohover == false) {
            this.currentmenu = null;
            this.megamenuholder().hide();

        }
    }
    this.killautojob = function() {
        clearTimeout(this.autojob)
		this.autojob = null;
    }
    this.startautojob = function() {
        var instance = this;
        this.autojob = setTimeout(function() { instance.CloseMageMenu() }, instance.TimeoutInt)
    }
    this.outhover = function(event) {
        var instance = event.data.instance;
        instance.startautojob();
    }
    this.menuouthover = function(event) {
        var instance = event.data.instance;
        instance.megamenuholder().attr("hover", "false");
        
    }
    this.menuhover = function(event) {
        var instance = event.data.instance;
		instance.killautojob();
        instance.megamenuholder().attr("hover", "true");
    }
    this.autoclosemegamenu = function(event) {
        var instance = event.data.instance;
        instance.startautojob();
    }
    this.initCloseButton = function() {
        var megamenuholder = this.megamenuholder();
        megamenuholder.find("span.close").bind("click", { instance: this }, function(event) {
            var instance = event.data.instance;
            instance.currentmenu = null;
            instance.megamenuholder().hide();
        })
    }
    this.init = function() {
        var allmenumenu = jQuery("#" + this.menuholderid).find("a[" + this.attrname + "]");
        var i = 0;
		var megamenuholder = this.megamenuholder();
        megamenuholder.hide();
        for (i = 0; i < allmenumenu.length; i++) {
            var themenuitem = allmenumenu.eq(i);
            if (themenuitem.attr(this.attrname) != "") {
                if (!this.keepmegamenulink) {
                    themenuitem.bind("click", { instance: this }, this.GetMageMenu);
                    // themenuitem.attr("href", "javascript:void(0);")
                } else {
                    themenuitem.bind("mouseenter", { instance: this }, this.GetMageMenu);
                    themenuitem.bind("mouseleave", { instance: this }, this.outhover);
                }
            }
        }
        megamenuholder.bind("mouseover", { instance: this }, this.menuhover).bind("mouseout", { instance: this }, this.menuouthover);
        if (this.keepmegamenulink) {
            megamenuholder.bind("mouseleave", { instance: this }, this.autoclosemegamenu);
        }
        var instance = this;
        jQuery("body").bind("click", function() { instance.startautojob() });
    }
}
var combohover = false;
function wrapcontactus(formholderid, myinstancename) {
    this.myinstancename = myinstancename;
    this.CAPTCHAKey = "6LfdpcQSAAAAAN3c3ksk6KhNeQJfoaEe4GBz0FBI"
    this.formholderid = formholderid;
    this.formholder = function() {
        return jQuery("#" + this.formholderid);
    }

    this.posturl = function() {
        return window.location.href;
    }
    this.ShowCAPTCHA = function() {
        if (jQuery("#newCAPTCHAControl").length == 0) {
            jQuery("#CAPTCHAControl").append("<div id='newCAPTCHAControl'></div>");
        }

        Recaptcha.create(this.CAPTCHAKey, "newCAPTCHAControl",
                {
                    theme: "white",
                    callback: Recaptcha.focus_response_field
                }
              );

    }
    this.getformdata = function() {
        if (jQuery(".contactussubform").length == 0) {
            this.formholder().wrap("<form class='contactussubform' />")
        }
        var returnvalue = jQuery(".contactussubform").serialize();
        if (jQuery(".contactussubform").length == 1) {
            this.formholder().unwrap();
        }

        return returnvalue;
    }
    this.postform = function() {
        var instance = this;
        jQuery.post(instance.posturl(), { data: "sender=" + instance.myinstancename + "&" + instance.getformdata() }, function(data) {
            var obj = jQuery.parseJSON(data)
            if (obj.status == 'true') {
                instance.formholder().html("<h2>Thank you for your enquiry, which we will do our best to answer as quickly as possible.</h2>")
            }
            else {
                Recaptcha.reload();
                instance.showError("The characters you entered didn't match the word verification. Please try again.")
            }
        }
        );
    }
    this.showError = function(message) {
        this.formholder().find(".errorholder").html(message);
    }
    this.AutoAddDefaultText = function() {
        var boxes = this.formholder().find("input[defaulttext],textarea[defaulttext]");
        var i = 0;
        for (i = 0; i < boxes.length; i++) {
            var thebox = boxes.eq(i);
            this.AddDefaultText(thebox);
        }
    }
    this.AddDefaultText = function(thebox) {
        var defaultext = thebox.attr("defaulttext");
        thebox.val(defaultext);
        thebox.addClass("defaulttext");
        thebox.unbind("focus").bind("focus", this.event_removedefaultText)
        thebox.unbind("blur").bind("blur", this.event_adddefaultText)
    }
    this.event_removedefaultText = function(event) {
        var thebox = jQuery(event.currentTarget);
        var defaultext = thebox.attr("defaulttext");
        if (thebox.val().toLowerCase() == defaultext.toLowerCase()) {
            thebox.val("");
            thebox.removeClass("defaulttext");
        }
    }
    this.event_adddefaultText = function(event) {
        var thebox = jQuery(event.currentTarget);
        var defaultext = thebox.attr("defaulttext");
        if (thebox.val() == "" || thebox.val().toLowerCase() == defaultext.toLowerCase()) {
            thebox.val(defaultext);
            thebox.addClass("defaulttext");
        }
    }
    this.event_selecthospital = function(event) {
        var thebox = jQuery(event.currentTarget);
        var instance = event.data.instance;
        instance.getContactInfo(thebox.val());
    }
    this.getContactInfo = function(hospitalid) {
        if (hospitalid > 0) {
            var instance = this;

            jQuery.post(instance.posturl(), { data: "sender=" + instance.myinstancename + "&getcontactinfo=" + hospitalid }, function(data) {
                var contactdetails = data;
                instance.formholder().find(".contatcdetails").html(contactdetails);
            })
        }
    }
    this.vaildatedata = function() {
        var allboxes = this.formholder().find("input,textarea,select");
        var i = 0;
        var message = "";
        for (i = 0; i < allboxes.length; i++) {
            var box = allboxes.eq(i);

            if (box.attr("required") == 'true' || box.attr("required") == true) {

                if (box.val() == "" || (box.attr("defaulttext") !== undefined && box.val().toLowerCase() == box.attr("defaulttext").toLowerCase())) {
                    var message = box.attr("errormsg");
                    if (box.attr("errormsg") == "" || box.attr("errormsg") == null) {
                        message = "You are required to complete all the form fields"
                    }
                    return message;
                }

            }
            if (box.attr("email") == 'true' && !isEmail(box.val())) {
                message = "Please enter a valid email address."
                return message;
            }
            if (box.attr("name") == "recaptcha_response_field" && box.val() == "") {
                message = "Please type the words in above picture."
                return message;
            }
        }
        return "";
    }
    this.init = function() {
        var thebox = this.formholder().find("select.ddlselecthospital");
        this.AutoAddDefaultText();
        this.getContactInfo(thebox.val());
        this.formholder().find(".cmdsend").bind("click", { instance: this }, this.send_event);
        thebox.bind("change", { instance: this }, this.event_selecthospital);
		thebox.bind("blur",function(){combohover =false;}).bind("focus",function(){combohover = true;})
        this.ShowCAPTCHA();
    }
    this.send_event = function(event) {
        var instance = event.data.instance;
        var message = instance.vaildatedata();
        if (message == "") {
            instance.postform();
        }
        else {
            instance.showError(message)
        }
        return false;
    }
    this.init();
}

// checks that an input string looks like a valid email address.

function isEmail(s) {
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (s.search(emailRegEx) != -1) {
        return true;
    };
    return false;
}
function sendmail(obj) {
    var thelink = jQuery(obj);
    var mail = thelink.attr("mail")
    var domain = thelink.attr("domain")
    var email = "mailto:" + mail + "@" + domain
    document.location = email
}
