﻿var cvp = -1;
var ank = -1;

var anketMesaj = '';
var anketMesaj1 = '';

function getAnketHTML() {

    if (parseFloat(cvp) > 0) {
        if (ank != getCookie('AnketID')) {
            $.ajax({
                type: "POST",
                url: $("#ajaxProPath").attr('value'),
                data: '{"ankID" : "' + ank + '", "cvpID" : "' + cvp + '"}',
                cache: false,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("X-AjaxPro-Method", "updateAnket");
                },
                success: function (s) {
                    if (s.indexOf('r.error = {"Message":"', 1) > 0)
                        alert(s.substring(s.indexOf('r.error = {"Message":"', 1) + 22, s.indexOf('","', 1)).replace('":"', ' : '));
                    else {
                        var o = null;
                        eval("o = " + s + "*" + "/");

                        var obj = document.getElementById('dvAnketContent');

                        if (obj != null)
                            obj.innerHTML = o;

                        var snc = document.getElementById('btnSonuc');
                        var ony = document.getElementById('btnOnay');
                        if (snc != null)
                            snc.className = "right-button red";
                        if (ony != null)
                            ony.className = "right-button";

                        WriteAnketCookie(ank);

                        cvp = -1;
                        ank = -1;
                    }
                },
                error: function (request, error) {
                    alert(error);
                }
            });
        }
        else {
            alert(anketMesaj);
        }
        return;
    }

    var snc = document.getElementById('btnSonuc');
    var ony = document.getElementById('btnOnay');
    if (snc != null)
        snc.className = "right-button";
    if (ony != null) {

        if (ony.className == "right-button red")
            alert(anketMesaj1);
        else
            ony.className = "right-button red";
    }

    $.ajax({
        type: "POST",
        url: $("#ajaxProPath").attr('value'),
        data: '{}',
        cache: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("X-AjaxPro-Method", "getAnketHTML");
        },
        success: function (s) {
            if (s.indexOf('r.error = {"Message":"', 1) > 0)
                alert(s.substring(s.indexOf('r.error = {"Message":"', 1) + 22, s.indexOf('","', 1)).replace('":"', ' : '));
            else {
                var o = null;
                eval("o = " + s + "*" + "/");

                var obj = document.getElementById('dvAnketContent');

                if (obj != null)
                    obj.innerHTML = o;
            }

        },
        error: function (request, error) {
            alert(error);
        }
    });


}

function getAnketSonucHTML() {
    cvp = -1;
    var snc = document.getElementById('btnSonuc');
    var ony = document.getElementById('btnOnay');
    if (snc != null)
        snc.className = "right-button red";
    if (ony != null)
        ony.className = "right-button";


    $.ajax({
        type: "POST",
        url: $("#ajaxProPath").attr('value'),
        data: '{}',
        cache: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("X-AjaxPro-Method", "getAnketSonucHTML");
        },
        success: function (s) {
            if (s.indexOf('r.error = {"Message":"', 1) > 0)
                alert(s.substring(s.indexOf('r.error = {"Message":"', 1) + 22, s.indexOf('","', 1)).replace('":"', ' : '));
            else {
                var o = null;
                eval("o = " + s + "*" + "/");

                var obj = document.getElementById('dvAnketContent');

                if (obj != null)
                    obj.innerHTML = o;
            }

        },
        error: function (request, error) {
            alert(error);
        }
    });
}


function setAnketID(id, aid) {
    cvp = id;
    ank = aid;
}



function WriteAnketCookie(anketID) {
    setCookie('AnketID', anketID, 365);
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
	    ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString())
} 
