
function get_page_cb_data() {
    var data = InstantMessengerProduct.getCustomizations();
    var query = [];
    for (var name in data) {
        query.push(name + "=" + data[ name ]);
    }
    query.push("_di=true")
    return query.join("&");
}

function hideProductForm() {
    var early_exit = false;
    if (InstantMessengerProduct.pdtype == "WA") {
        var c = InstantMessengerProduct.static_customizations;
        if (!(c["photopath"] && c["photow"] && c["photoh"])) {
            if (document.photoForm.userfile.value) {
                alert('Click "upload" to continue');
            } else {
                alert("Please select a photo");
            }
            early_exit = true;
        }
    }
    if (InstantMessengerProduct.pdtype == "PW") {
        var c = InstantMessengerProduct.getCustomizations();
        if (!c["pwink_qry"]) {
            early_exit = true;
        }
    }
    if (!early_exit) {
        $("prv_product_form").style.display = "none";
        $("prv_free_download").style.display = "block";
    }
}

function doPhotoUpload() {
    WinkController.setPhoto("product-preview", "");
    try {
        WinkController.stop("product-preview");
    } catch (e) { }
    var radios = document.forms["photoForm"].rights;
    for (var i = 0; i < radios.length; i++) {
        if (!document.forms["photoForm"].userfile.value) {
            alert("Please select a photo");
            return false;
        }
        var radio = radios.item(i);
        if (radio.checked && radio.value != "true") {
            alert("You must own the rights to this photo\n and agree to our Terms of Service");
            return false;
        }
    }
    var waw = InstantMessengerProduct.static_customizations.aap_width;
    var wah = InstantMessengerProduct.static_customizations.aap_height;
    PhotoUploader.upload(waw, wah);
    return false;
}

var InstantMessengerProduct = {
    productnumber: null,
    pathnumber: null,
    pdtype: null,
    rptype: null,
    installer: null,
    host: null,
    usageid: null,
    static_customizations: {},
    registration_prompt: true,
    getCustomizations: function() {
        var customizations = {}; 
        for (var name in InstantMessengerProduct.static_customizations) {
            var value = InstantMessengerProduct.static_customizations[name];
            customizations[name] = value;
        }
        if (InstantMessengerProduct.pdtype == "PW") {
            customizations["pwink_qry"] = $("winktext").value;
            customizations["pwink_title"] = $("winktext").value;
        }
        if (InstantMessengerProduct.pdtype == "WA") {
            if (!(customizations["photopath"] && customizations["photow"] && customizations["photoh"])) {
                alert("Please select a photo");
                return false;
            }
        }
        return customizations;
    },
    getUsage: function() {
        var customizations = InstantMessengerProduct.getCustomizations();
        if (!customizations) { return; }
        var native_installer = InstantMessengerProduct._createInstaller();
        var url = "/platform/im/usage.pd?prodnum=" + InstantMessengerProduct.productnumber;
        url += "&path=" + InstantMessengerProduct.pathnumber;
        url += "&rptype=" + InstantMessengerProduct.rptype;
        for (var name in customizations) {
            var value = customizations[name];
            url += "&pers_" + name + "=" + escape(value);
        }
        if (!native_installer) {
            url += "&installer=0";
        }
        var req = new Requester(url, "GET", true, true);
        req.onsuccess = function(req) {
            var i = parseInt(req.responseText);
            if (!i) {
                alert("There was an error - please try again");
                return;
            }
            InstantMessengerProduct.usageid = req.responseText;
            InstantMessengerProduct.install();
        }
        req.onerror = function(req) {
            alert(req.responseText)
            alert("There was an error - please try again");
        }
        req.sendRequest(null);
    },
    download: function() {
        if (InstantMessengerProduct.usageid) {
            InstantMessengerProduct.install();
        } else {
            InstantMessengerProduct.getUsage();
        }
    },
    downloadPrompt: function(blnSignedIn) {
        if (blnSignedIn || !InstantMessengerProduct.registration_prompt) {
            InstantMessengerProduct.download();
        } else {
            register("BDY-IMDOWNLOAD", {hlb: "InstantMessengerProduct.download"})
            InstantMessengerProduct.registration_prompt = false;
        }
    },
    install: function() {
        var native_installer = InstantMessengerProduct._createInstaller();
        var url = InstantMessengerProduct.host + "/platform/im/download.pd?";
        url += "usageid=" + InstantMessengerProduct.usageid;
        url += "&prodnum=" + InstantMessengerProduct.productnumber;
        url += "&path=" + InstantMessengerProduct.pathnumber;
        url += "&pdtype=" + InstantMessengerProduct.pdtype;
        url += "&rptype=" + InstantMessengerProduct.rptype;
        if (native_installer) {
        //alert("native installer")
            try {
                InstantMessengerProduct.installer.InstallContent(url);
            } catch(err) {
                if (err.number == -2130705634) {
                    alert("Please make sure you are signed into Windows Live Messenger to download this content")
                } else {
                    alert("Please make sure you are signed into Windows Live Messenger to download this content")
                }
                var o = InstantMessengerProduct.installer;
                o.parentNode.removeChild(o);
                InstantMessengerProduct.installer = null;
            }
        } else {
            //InstantMessengerProduct.redirectToConfirm(true);
            InstantMessengerProduct._installExternal(url);
        }
    },
    _installExternal: function(url) {
        InstantMessengerProduct.redirectToConfirm(true);
    },
    redirectToConfirm: function(external) {
        var url = "/platform/im/confirm.pd?";
        url += "path=" + InstantMessengerProduct.pathnumber;
        url += "&prodnum=" + InstantMessengerProduct.productnumber;
        url += "&usageid=" + InstantMessengerProduct.usageid;
        if (external) {
            url += "&inst=true";
        }
        var webvars = getWebvars();
        if (webvars.search) {
            url += "&search=" + webvars.search;
        }
        window.location.href = url;
    },
    _createInstaller: function() {
        if (InstantMessengerProduct.installer) {
            return true;
        }
        var o = document.createElement("object");
        o.setAttribute("height", "0");
        o.setAttribute("width", "0");
        o.setAttribute("classid", "clsid:F06608C7-1874-4EEA-B3B2-DF99EBB144B8");
        o.setAttribute("codeType", "application/x-oleobject");
        o.setAttribute("id", "MessengerContentInstaller");
        document.body.appendChild(o);
        var has_native = (typeof o != 'undefined' && o != null && o.InstallContent != null);
        if (has_native) {
            InstantMessengerProduct.installer = o;
        }
        return has_native;
    },
    OnContentInstalled: function(result) {
        if (result) {
            alert("There was an error - please try again");
        } else {
            InstantMessengerProduct.redirectToConfirm();
        }
    }
};

