﻿function ShareDate() {
    this.date = new Date();
    ShareDate.prototype.toString = function() {
        nd = formatDate(this.date, "yyyy-MM-dd");
        return nd;
    }
    ShareDate.prototype.addDay = function(days) {
        return this.date.setDate(this.date.getDate() + days);
    }
    ShareDate.prototype.addMonth = function(months) {
        return this.date.setMonth(this.date.getMonth() + months);
    }
    ShareDate.prototype.addYear = function(years) {
        return this.date.setFullYear(this.date.getFullYear() + years);
    }
    ShareDate.prototype.setHours = function(hours) {
        this.date.setHours(hours);
        this.date.setMinutes(0);
        return this.date;
    }
}
function addZero(vNumber) {
    return ((vNumber < 10) ? "0" : "") + vNumber
}
function formatDate(vDate, vFormat) {
    var vDay = addZero(vDate.getDate());
    var vMonth = addZero(vDate.getMonth() + 1);
    var vYearLong = addZero(vDate.getFullYear());
    var vYearShort = addZero(vDate.getFullYear().toString().substring(3, 4));
    var vYear = (vFormat.indexOf("yyyy") > -1 ? vYearLong : vYearShort)
    var vHour = addZero(vDate.getHours());
    var vMinute = addZero(vDate.getMinutes());
    var vSecond = addZero(vDate.getSeconds());
    var vDateString = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear);
    vDateString = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond);
    return vDateString;
}
//Image class
function ImageObject() {
    this.EndofDay = 0;
    this.PartOfDay = 1;
    this.WeekIntra = 2;
    this.IntervalType = "YEAR";
    this.IntervalTypeIndex = 6;
    this.FromDate = new ShareDate();
    this.ToDate = new ShareDate();
    this.QuoteType = this.EndofDay;
    this.ShowShareEvent = false;
    this.QuantityGraphVisibility = true;
    this.DeltaPercentageGraphVisibility = true;
    ImageObject.prototype.setIntevalDate = function() {
        this.FromDate = new ShareDate();
        switch (this.IntervalType) {
            case "SIX_YEAR":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addYear(-6);
                this.QuoteType = this.EndofDay;
                break;
            case "THREE_YEAR":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addYear(-3);
                this.QuoteType = this.EndofDay;
                break;
            case "YEAR":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addYear(-1);
                this.QuoteType = this.EndofDay;
                break;
            case "DAY":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.ToDate.addDay(1);
                this.QuoteType = this.PartOfDay;
                break;
            case "WEEK":
                this.FromDate = new ShareDate();
                this.FromDate.addDay(-7);
                this.ToDate = new ShareDate();
                this.QuoteType = this.EndofDay;
                break;
            case "WEEK_INTRA":
                this.FromDate = new ShareDate();
                this.FromDate.addDay(-7);
                this.ToDate = new ShareDate();
                this.QuoteType = this.WeekIntra;
                break;
            case "MONTH":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addMonth(-1);
                this.QuoteType = this.EndofDay;
                break;
            case "SIX_MONTH":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addMonth(-6);
                this.QuoteType = this.EndofDay;
                break;
            case "THREE_MONTH":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addMonth(-3);
                this.QuoteType = this.EndofDay;
                break;
            case "ALL":
                this.FromDate = new ShareDate();
                this.ToDate = new ShareDate();
                this.FromDate.addYear(-30);
                this.QuoteType = this.EndofDay;
                break;
            case "CUSTOM":
                var reg = /([\d]{4})-([\d]{2})-([\d]{2})/
                if (fromElement.value != "") {
                    this.FromDate = new ShareDate();
                    var year = fromElement.value.replace(reg, "$1");
                    var month = fromElement.value.replace(reg, "$2") - 1;
                    var day = fromElement.value.replace(reg, "$3");
                    this.FromDate.date.setFullYear(year, month, day);
                }
                else {
                    this.FromDate = new ShareDate();
                    this.FromDate.addYear(-1);
                }
                if (toElement.value != "") {
                    this.ToDate = new ShareDate();
                    var year = toElement.value.replace(reg, "$1");
                    var month = toElement.value.replace(reg, "$2") - 1;
                    var day = toElement.value.replace(reg, "$3");
                    this.ToDate.date.setFullYear(year, month, day);
                }
                else {
                    this.ToDate = new ShareDate();
                }
                var TwoWeeksBack = new ShareDate();
                TwoWeeksBack.addDay(-14);
                if (TwoWeeksBack.date - this.FromDate.date > 0)
                    this.QuoteType = this.EndofDay;
                else {
                    this.QuoteType = this.PartOfDay;
                    this.ToDate.addDay(1);
                }
                break;
        }
    }
}
//General functions 
function RunApplet() {
    if (!hasShareGraphImage()) {
        var applet = getApplet();
        applet.setQuantityGraphVisibility(oImage.QuantityGraphVisibility);
        applet.setDeltaPercentageGraphVisibility(oImage.DeltaPercentageGraphVisibility);
        applet.setDateInterval(oImage.FromDate.toString(), oImage.ToDate.toString(), oImage.QuoteType, oImage.IntervalTypeIndex);
    }
}
function getApplet() {
    if (document.sharegraphapplet)
        return document.sharegraphapplet;
    if (document.sharegraphappletie)
        return document.sharegraphappletie;
    if (document.applets[0])
        return document.applets[0]; alert("Can not find applet");
    return null;
}
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
}
function changeInterval(frmElement) {
    interval = frmElement[frmElement.selectedIndex].value;
    oImage.IntervalType = interval;
    oImage.IntervalTypeIndex = frmElement.selectedIndex;
    if (interval == "" || interval == "CUSTOM") {
        //resetDisableFromToDate(false);  
    }
    else {
        //resetDisableFromToDate(true);   
        PostPage();
    }
}
function setCustom(obj) {
    var optControl = document.getElementById("GraphOptionIntervalControl_wpyintervalType");
    if (optControl.selectedIndex != optControl.length - 1)
        optControl.selectedIndex = optControl.length - 1;
    oImage.IntervalType = "CUSTOM";
    oImage.setIntevalDate();
}
function setQuoteType(quoteType) {
    oImage.QuoteType = -1;
    if (!hasShareGraphImage())
        getApplet().setDisplayDateInterval(oImage.FromDate.toString(), oImage.ToDate.toString(), oImage.QuoteType);
    else
        PostPage();
}
function setQuantityVisibility(checkbox) {
    oImage.QuantityGraphVisibility = checkbox.checked;
    if (!hasShareGraphImage())
        getApplet().setQuantityGraphVisibility(oImage.QuantityGraphVisibility);
    else
        PostPage();
}
function setDeltaPercentVisibility(checkbox) {
    oImage.DeltaPercentageGraphVisibility = checkbox.checked;
    if (!hasShareGraphImage())
        getApplet().setDeltaPercentageGraphVisibility(oImage.DeltaPercentageGraphVisibility);
    else
        PostPage();
}
function setDefaultDeltaPercentVisibility(checkbox) {
    oImage.DeltaPercentageGraphVisibility = checkbox.checked;
    PostPage();
}
function hasShareGraphImage() {
    var imageObejct = document.images["ShareGraphHtmlImage"];
    if (imageObejct == null)
        return false;
    return true;
}
function setReleaseListId(formInputObject) {
    var visible = false;
    var value = "";
    if (formInputObject.type == "checkbox" || formInputObject.type == "radio") {
        visible = formInputObject.checked;
        value = formInputObject.value;
    }
    else if (formInputObject.tagName == "SELECT") {
        var n = formInputObject.selectedIndex;
        // Which menu item is selected   
        value = formInputObject[n].value;
        visible = true;
    }
    if (!hasShareGraphImage())
        setShareEventCollectionVisibility(value, visible)
    else
        PostPage();
}
function setModuleId(formInputObject) {
    var visible = false;
    var value = "";
    if (formInputObject.type == "checkbox" || formInputObject.type == "radio") {
        visible = formInputObject.checked;
        value = formInputObject.value;
    }
    else if (formInputObject.tagName == "SELECT") {
        var n = formInputObject.selectedIndex;
        // Which menu item is selected   
        value = formInputObject[n].value;
        visible = true;
    }
    if (!hasShareGraphImage()) {
        setShareEventCollectionVisibility(value, visible);
    }
    else {
        PostPage();
    }
}
function setShareEventCollectionVisibility(id, visibilty) {
    if (!hasShareGraphImage()) {
        getApplet().setShareEventCollectionVisibility(parseInt(id), visibilty);
    }
    else {
        PostPage();
    }
}
function setShareEvent(checkbox) {
    oImage.ShowShareEvent = checkbox.checked;
    PostPage();
}
function chooseTicker(formInputObject) {
    var visible = false;
    var value = "";
    if (formInputObject.type == "checkbox" || formInputObject.type == "radio") {
        visible = formInputObject.checked;
        value = formInputObject.value;
    }
    else if (formInputObject.tagName == "SELECT") {
        var n = formInputObject.selectedIndex;
        // Which menu item is selected
        value = formInputObject[n].value;
        visible = true;
    }
    if (!hasShareGraphImage())
        getApplet().setPriceGraphTickerVisibility(parseInt(value), visible);
    else
        PostPage();
}
function doSubmit(frm) {
    PostPage();
}
function PostPage() {
    oImage.setIntevalDate();
    if (!hasShareGraphImage()) {
        RunApplet();
    }
    else {
        /*    oForm.action = oForm.wpyGraphURL.value;   oForm.wpyGraphURL.name = "wG";   oForm.wpyGraphURL.value = "0";*/
        oForm.submit();
    }
}
function hideDelta() {
    oForm.wpydeltaPercentageVisiblity.checked = false;
    setDefaultDeltaPercentVisibility(oForm.wpydeltaPercentageVisiblity);
    setDefaultTickerId(1, false);
}
function setDefaultTickerId(id, visible) {
    if (!hasShareGraphImage()) {
        oForm.tid[id].checked = false;
        getApplet().setPriceGraphTickerVisibility(parseInt(id), visible);
    }
}
function ShareGraphInit() {
    oImage = new ImageObject();
    oForm = document.forms["frmShareGraph"];
    oForm.Name = "frmShareGraph";
    fromElement = oForm.elements["wpyfrom"];
    toElement = oForm.elements["wpyto"];
    var reqInterval = ""
}
function doSubmit(frm) {
    PostPage();
}
function PostPage() {
    oImage.setIntevalDate();
    if (!hasShareGraphImage())
        RunApplet();
    else
        oForm.submit();
}
function setShareEvent(checkbox) {
    oImage.ShowShareEvent = checkbox.checked;
    PostPage();
}
function setReleaseListId(formInputObject) {
    var visible = false;
    var value = "";
    if (formInputObject.type == "checkbox" || formInputObject.type == "radio") {
        visible = formInputObject.checked;
        value = formInputObject.value;
    }
    else if (formInputObject.tagName == "SELECT") {
        var n = formInputObject.selectedIndex;
        // Which menu item is selected    
        value = formInputObject[n].value;
        visible = true;
    }
    if (!hasShareGraphImage())
        getApplet().setShareEventCollectionVisibility(parseInt(value), visible)
    PostPage();
}
function setQuoteType(quoteType) {
    oImage.QuoteType = quoteType;
    PostPage();
}
function chooseTicker(checkBox) {
    if (!hasShareGraphImage())
        getApplet().setPriceGraphTickerVisibility(parseInt(checkBox.value), checkBox.checked);
    PostPage();
}
