﻿
var hostName = 'http://myinfocenter.ru';
var encoding = 'UTF-8';

String.prototype.endsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(this.length - t.length));
    } else {
        return
        (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
}

UserTestHelper = {
    SetTest: function(testID) {
        WebpandoraHelper.Instance.currentAnswer = -1;
        WebpandoraHelper.Instance.currentNumber = 1;
        WebpandoraHelper.Instance.answers = new Array();
        WebpandoraHelper.Instance.testID = testID;
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: WebpandoraHelper.Instance.place, service: WebpandoraHelper.Instance.service, "testID": testID, decorated: "true", "encoding": encoding, date: new Date() }));
        this.RenderQuest();

    },

    SetTestByName: function(testName) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetTestID", "testName": testName, decorated: "true", "encoding": encoding, date: new Date() }));
    },

    SetCategory: function(category) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: WebpandoraHelper.Instance.place, service: WebpandoraHelper.Instance.service, "categoryID": category, "encoding": encoding, date: new Date() }));
    },

    RenderQuest: function() {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "RenderQuest", testID: WebpandoraHelper.Instance.testID, questionNumber: WebpandoraHelper.Instance.currentNumber, "encoding": encoding, date: new Date() }));
    },

    AddAnswer: function() {
        $("input#answers").val($("input#answers").val() + WebpandoraHelper.Instance.currentAnswer + ".");
        WebpandoraHelper.Instance.currentNumber++;
        this.RenderQuest();
        WebpandoraHelper.Instance.currentAnswer = -1;
    },

    SetAnswer: function(answer, sender) {
        WebpandoraHelper.Instance.currentAnswer = answer;
        if (sender) {
            $("a[rel='image']").each(function() {
                $(this).removeClass("active");
            });
            $(sender).addClass("active");
        }
    },

    NextQuest: function() {
        if (!this.CheckInput()) {
            return false;
        } else if (WebpandoraHelper.Instance.currentAnswer > 0) {
            $("div#genderBirthDay").hide();
            this.AddAnswer();
        } else {
            alert("Необходимо выбрать один из вариантов ответа");
        }
        return false;
    },
    //    CheckEmail: function(email) {
    //        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "CheckEmail", "email": email, place: WebpandoraHelper.Instance.place, "encoding": encoding, date: new Date() }));
    //    },
    CheckInput: function() {
        var res = true;
        var gender = $('input[name*=rblGender]:checked').val();
        if (!gender) {
            alert("Необходимо выбрать пол");
            res = false;
        }
        var day = $('select[name*=ddlDate]').val();
        var month = $('select[name*=ddlMonths]').val();
        var year = $('select[name*=ddlYear]').val();

        if ('Invalid Date' == new Date(month + '/' + day + '/' + year)) {
            alert('Неверная дата');
            res = false;
        }
        return res;
    },
    ChangeGender: function() {
        WebpandoraHelper.Instance.gender = $('input[name*=rblGender]:checked').val();
    },
    ChangeDate: function() {
        WebpandoraHelper.Instance.date = $("select[id*='ddlDate']").val();
    },
    ChangeMonth: function() {
        WebpandoraHelper.Instance.months = $("select[id*='ddlMonths']").val();
    },
    ChangeYear: function() {
        WebpandoraHelper.Instance.year = $("select[id*='ddlYear']").val();
    },
    ValidateEmail: function(data) {
        var email = data.email;
        if (!email || email == '') {
            alert('Укажите почтовые данные!');
            return false;
        }
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "CheckEmail", "email": email, place: WebpandoraHelper.Instance.place, "encoding": encoding, date: new Date() }), function() {
            if (validemail) {
                SetProgressImage();
                $.getScript(hostName + '/partners/remote/form.ashx?' + $.param(data));
            }
        });
    }
};

function Webpandora(place, service) {
    this.place = place;
    this.service = service;
};

function UserTest(testID) {
    this.testID = testID;
    this.currentAnswer = -1;
    this.place = -1;
    this.service = -1;
};

WebpandoraHelper = {
    getPlace: function() {
        return this.Instance.place;
    },

    open: function(place) {
        encoding = getEncoding();
        if (!this.Instance) {
            document.write("<div id='form'></div>");
   
//            var link = document.createElement('link');
//            link.setAttribute('rel', 'stylesheet');
//            link.setAttribute('media', 'all');
//            link.setAttribute('type', 'text/css');
//            link.setAttribute('href', hostName + '/styles/table_tree.css');
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: place, method: "GetStyles", "encoding": encoding, date: new Date() }));
            var she = document.createElement('script');
            she.setAttribute('type', 'text/javascript');
            she.setAttribute('src', hostName + '/js/show_hide_element.js');

            var head = document.getElementsByTagName("head")[0];
            if (head) {
//                head.appendChild(link);

                var decor = document.createElement('link');
                decor.setAttribute('id', 'decor');
                decor.setAttribute('rel', 'stylesheet');
                decor.setAttribute('media', 'all');
                decor.setAttribute('type', 'text/css');
                decor.setAttribute('href', hostName + '/styles/test/1.css');
                head.appendChild(decor);
                head.appendChild(she);
            }
            this.Instance = new Webpandora(place, 9);
        }



        if (GetHashString("t")) {
            SetProgressImage();
            this.Instance.service = 9;
            UserTestHelper.SetTestByName(GetHashString("t"));
        } else if (GetHashString("o") && GetHashString("k")) {
            SetProgressImage();
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetResult", o: GetHashString("o"), k: GetHashString("k"), decorated: "true", "encoding": encoding, date: new Date() }));
        } else if (GetHashString("scid") && GetHashString("siid")) {
            SetProgressImage();
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetOrderPage", scid: GetHashString("scid"), siid: GetHashString("siid"), decorated: "true", "encoding": encoding, date: new Date() }));
        } else {
            SetProgressImage();
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: this.Instance.place, service: this.Instance.service, decorated: "true", "encoding": encoding, date: new Date() }));
        }
    },

    send: function() {
        var data = this.getData(this.Instance.place);
        //        SetProgressImage();
        //        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param(data));
        if (data) {
            UserTestHelper.ValidateEmail(data);
        }
        return false;
    },

    showResponses: function(page) {

        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetResponses", service: this.Instance.service, "page": page, "encoding": encoding }));
        if (document.getElementById('feedback').style.display = "none") {
            var left = $('#form div:first-child').offset().left + ($('#form div:first-child').width() / 2) - ($('div#feedback').width() / 2);
            $('div#feedback').css("left", parseInt(left) + "px");
            show_hide('feedback');
        }
    },

    exit: function() {
    },

    getData: function(place) {
        var result = null;
        result = CreateUserTestObject(place, this.Instance);
        return result;
    },
    showDyplom: function(order, key) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "ShowDyplom", o: order, "k": key, decorated: "true", "encoding": encoding }));
    },

    showExample: function(exampleID) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetExample", service: this.Instance.service, "example": exampleID, "encoding": encoding }));
    }
};

function CreateUserTestObject(place, ins) {
    var result = new UserTest(ins.testID);
    result.service = 9;
    result.place = place;
    result.testID = ins.testID;

    if (!ins.date) {
        result.date = $("select[id*='ddlDate']").val();
    } else {
        result.date = ins.date;
    }

    if (!ins.months) {
        result.months = $("select[id*='ddlMonths']").val();
    } else {
        result.months = ins.months;
    }

    if (!ins.year) {
        result.year = $("select[id*='ddlYear']").val();
    } else {
        result.year = ins.year;
    }

    result.email = $("#form input[id*='txtEmail']").val();
    result.answers = $("#form input[id*='answers']").val();

    if (!ins.gender) {
        result.gender = GetRadioValue("rblGender");
    } else {
        result.gender = ins.gender;
    }

    result.encoding = encoding;
    result.submit = true;
    return result;
};

function getEncoding() {
    if (document.charset)
        return document.charset;
    else if (document.characterSet)
        return document.characterSet;
    return undefined;
}

function BuyMoney(sys, order) {
    $.getScript(hostName + '/partners/remote/buy_money.aspx' + $.param({ pay: order, curr: $('#' + sys).val() }),
         function(html) {
             $('#form').html('');
             $('#form').html(html);
         });
}

function GetRadioValue(groupName) {
    return $(":radio[name*=" + groupName + "]").filter(":checked").val();
}

function sp(params) {

    var ParamsArr = params.split("&");
    WebpandoraHelper.showResponses(getParamValue(ParamsArr, "?page"))

}

function getParamValue(ParamsArr, val) {
    for (i = 0; i < ParamsArr.length; i++) {
        var ArItem = ParamsArr[i];
        var ItemAr = ArItem.split("=");

        if (ItemAr[0] == val) return ItemAr[1];
    }
    return "_";
}

function SetResponses() {
    $('#feedback .link').each(function() {
        var pag = parseInt($.trim($(this).html()));
        $(this).attr('href', '#');
        $(this).click(function() {
            WebpandoraHelper.showResponses(pag);
            return false;
        });
    });
}

function ReplaceSymbols() {

    var content = $('#form').html();
    content = content.replace(/\$n\$/gi, '\n');
    content = content.replace(/\$rn\$/gi, '\r\n');
    content = content.replace(/\$r\$/gi, '\r');
    $('#form').html(content);
}

function GetQueryString(key) {
    var queryString = window.location.search.substring(1);
    var params = queryString.split("&");
    for (i = 0; i < params.length; i++) {
        var curKey = params[i].split("=");
        if (curKey[0] == key) {
            return curKey[1];
        }
    }
    return undefined;
}

function GetHashString(key) {
    var hashString = window.location.hash.substring(1);
    var params = hashString.split("&");
    for (i = 0; i < params.length; i++) {
        var curKey = params[i].split("=");
        if (curKey[0] == key) {
            return curKey[1];
        }
    }
    return undefined;
}

function StartDownload(order, key) {
    var url = hostName + '/get_dyplom.ashx?o=' + order + '&k=' + key;
    window.open(url, 'Download');
    return false;
}

function SetProgressImage() {
    $('#form').html('');
    $('#form').html('<div style="height: 1000px; max-width: 750px; min-width: 550px; text-align: center; background: #cecece url(\'' + hostName + '/images/time-1.gif\') no-repeat 50% 50%;"></div>');
}

function ReloadContent(siid) {
    var place = WebpandoraHelper.getPlace();
    WebpandoraHelper.open(place, siid);
    return false;
}

function menuOver(el) {
    $(el).animate({ height: "100%", "z-index": "300" }, { queue: false, duration: 200 });
}
function menuOut(el) {
    $(el).animate({ height: "40px", "z-index": "300" }, { queue: false, duration: 200 });
}

function MenuAlign() {

    var mWidth = $('#ddmst').width();
    var dWidth = $('#par_container').width();

    var offLeft = (dWidth - mWidth) / 2;
    $('#ddmst').css("left", offLeft + "px");
}

$(window).resize(function() {
    MenuAlign();
});

