﻿
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());
    }
}

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;
};

function YourNameWebpandora() {
    this.place = -1;
    this.service = -1;
    this.name = '';
    this.email = '';
    this.submit = true;
};

function YourNumberWebpandora() {
    this.place = -1;
    this.service = -1;
    this.firstName = '';
    this.lastName = '';
    this.secondName = '';
    this.date = '';
    this.months = '';
    this.year = '';
    this.email = '';
    this.submit = true;
};

function YourPresentWebpandora() {
    this.place = -1;
    this.service = -1;
    this.holiday = -1;
    this.who = -1;
    this.style = -1;
    this.indAge = false;
    this.age = -1;
    this.addr = -1;
    this.from = -1;
    this.cCount = 5;
    this.date = 1;
    this.month = 1;
    this.email = '';
    this.submit = true;
};

function YourSlipWebpandora() {
    this.place = -1;
    this.service = -1;
    this.email = '';
    this.submit = true;
};



WebpandoraHelper = {
    getPlace: function() {
        return this.Instance.place;
    },
    open: function(place, service) {
        encoding = getEncoding();
        if (!this.Instance) {
            document.write("<div id='form'></div>");

            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: place, method: "GetStyles", "encoding": encoding, date: new Date() }));

//            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');

            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);
                head.appendChild(she);
                var scr = document.createElement('script');
                scr.setAttribute('type', 'text/javascript');
                scr.setAttribute('src', hostName + '/js/remote_your_present.js');
                head.appendChild(scr);
            }
        }

        this.Instance = new Webpandora(place, service);

        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"), "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"), "encoding": encoding, date: new Date() }));
        } else {
            SetProgressImage();
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: this.Instance.place, service: this.Instance.service, "encoding": encoding, date: new Date() }));
        }
    },
    send: function() {

        if (this.Instance.service == 3 && $.browser.msie && $('#form #txtDescription').val().length > 370) {
            alert('Описание не должно превышать 370 символов');
            return;
        }
        var data = this.getData(this.Instance.place, this.Instance.service);
        var next = true;
        if (this.Instance.service == 9) {
            next = UserTestHelper.ValidateEmail(data);
        }
        if (next) {
            SetProgressImage();
            $.getScript(hostName + '/partners/remote/form.ashx?' + $.param(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, service) {
        var result = null;
        switch (service) {
            case 1:
                result = CreateYourNameObject(place);
                break;
            case 2:
                result = CreateYourNumberObject(place);
                break;
            case 7:
                result = CreateYourPresentObject(place);
                break;
            case 3:
                result = CreateSlipObject(place);
                break;
            case 9:
                result = CreateUserTestObject(place, this.Instance);
                break;
        }
        return result;
    },
    showDyplom: function(order, key) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "ShowDyplom", o: order, "k": key, "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 YourNameWebpandora();
    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 CreateYourNameObject(place) {
    var result = new YourNameWebpandora();
    result.service = 1;
    result.place = place;
    result.name = $('#form input#txtName').val();
    result.email = $('#form input#txtEmail').val();
    result.encoding = encoding;
    result.submit = true;
    return result;
};


function CreateYourNumberObject(place) {
    var result = new YourNumberWebpandora();
    result.service = 2;
    result.place = place;
    result.firstName = $('#form input#txtFirstName').val();
    result.lastName = $('#form input#txtLastName').val();
    result.secondName = $('#form input#txtSecondName').val();
    result.date = $('#form select#ddlDate').val();
    result.months = $('#form select#ddlMonths').val();
    result.year = $('#form select#ddlYear').val();
    result.email = $('#form input#txtEmail').val();
    result.encoding = encoding;
    result.submit = true;
    return result;
};


function CreateYourPresentObject(place) {
    var result = new YourPresentWebpandora();
    result.service = 7;
    result.place = place;
    result.holiday = $('#form select#ddlHolidays').val();
    result.who = $('#form select#ddlWhoCongratulate').val();
    result.style = $('#form select#ddlStyleCongratulation').val();

    result.indAge = GetRadioValue("rblIndicateAge") == "rblIndicateAgeYes" ? true : false;

    if (result.indAge)
        result.age = $('#form select#ddlAge').val();
    else
        result.age = -1;

    result.addr = GetRadioValue("cblAddress");
    result.from = GetRadioValue("rblFromCongratulate");
    result.cCount = GetRadioValue("rblQuatrainCount");

    result.date = $("#form select#ddlDate").val();
    result.month = $("#form select#ddlMonths").val();

    $("table#cblAdditionalParameters input:checked").each(function() {
        eval("result.ap_" + this.value + " = " + $("#parval_" + this.value).val());
    });

    result.email = $('#form input#txtEmail').val();
    result.encoding = encoding;
    result.submit = true;

    return result;
};

function CreateSlipObject(place) {
    var result = new YourSlipWebpandora();
    result.service = 3;
    result.place = place;
    result.day = $('#form select#ddlDay').val();
    result.month = $('#form select#ddlMonths').val();
    result.year = $('#form select#ddlYear').val();

    result.daySleep = $('#form select#ddlDaySleep').val();
    result.monthSleep = $('#form select#ddlMonthSleep').val();
    result.yearSleep = $('#form select#ddlYearSleep').val();
    result.rblSex = GetRadioValue("rblSex");


    result.ckbDoNotRemember = $('#form #ckbDoNotRemember').attr('checked') ? "on" : "off";

    result.description = $('#form #txtDescription').val();



    result.email = $('#form input#txtEmail').val();
    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>');
}
// <img src="' + hostName + '/images/time-1.gif" alt="Загрузка" width="250" height="40" />
function ReloadContent(siid) {
    var place = WebpandoraHelper.getPlace();
    WebpandoraHelper.open(place, siid);
    return false;
}

function MenuAlign() {

    var mWidth = $('#ddmst').width();
    var dWidth = $('#par_container').width();

    var offLeft = (dWidth - mWidth) / 2;
    $('#ddmst').css("left", offLeft + "px");
}

