﻿/////////Create Tavanmand Ajax : Tjax By Reza Mizbani 25-10-1387
var xmlhttp;
document.TJax = function()
{

}
document.TJax.Types = 
{
    basket : "basket" ,
    list : "list",
    addbasket : "addbasket",
    addlist : "addlist",
    voting : "voting" ,
    rating : "rating"
}
var loadedPanel = null;
document.TJax.Initialize = function() {

    var type = arguments[0]; /// Get Type
    if (type == undefined)
        return;
    var action = arguments[1]; /// Get Action
    var paramet = arguments[2]; /// Parameter

    var loadPanel = arguments[3]; /// Get A Div For Load Data

    var load = document.getElementById(type + "-load");

    var cont;
    if (loadPanel == null || loadPanel == undefined)
        cont = document.getElementById(type + "-cont");
    else
        cont = document.getElementById(loadPanel);

    //alert(loadPanel);

    if (action == 0) {

        if (loadPanel) {

            cont.oldStyle = cont.style;

            cont.style.width = cont.offsetWidth + "px";
            cont.style.height = cont.offsetHeight + "px";
            cont.pt = cont.style.paddingTop;
            if (type == document.TJax.Types.addbasket)
                cont.style.paddingTop = ((cont.offsetHeight / 4) - (load.offsetHeight / 2)) + "px";
            if (type == document.TJax.Types.voting)
                cont.style.paddingTop = ((cont.offsetHeight / 2) - (load.offsetHeight / 2)) + "px";
            cont.pr = cont.style.paddingRight;
            cont.style.paddingRight = "7px";

            cont.innerHTML = load.innerHTML;
            loadedPanel = cont;
        }
        else {
            load.style.display = "";
        }

        if (paramet)
            document.TJax.GetData(type, paramet)
        else
            document.TJax.GetData(type)

    }
    else if (action == 1) {

        var content = arguments[2];
        load.style.display = "none";
        
        if (cont == null) {
            cont = loadedPanel;
            cont.style.paddingTop = cont.pt;
            cont.style.paddingRight = cont.pr;
        }

        //alert(content);
        cont.innerHTML = content;
        //alert(content);
    }

}

document.TJax.GetData = function(getType)
{
    xmlhttp = null;
    var url = "/Eshop/Component/GetData.aspx";
    url = url + "?Type=" + getType + "&Date=" + new Date().getMinutes().toString() + new Date().getSeconds().toString();
    if(arguments[1] != null)
        url += "&" + arguments[1];
        
    //document.location.href = (url);
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null)
    {
        xmlhttp.onreadystatechange = document.TJax.state_Change;
        xmlhttp.open("GET",url);
        xmlhttp.send(null);
        xmlhttp.getType = getType;
    }
    else
    {
        //alert("Your browser does not support XMLHTTP.");
    }
}
document.TJax.state_Change = function () {
    if (xmlhttp != null && xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            document.TJax.Initialize(xmlhttp.getType, 1, xmlhttp.responseText);
        }
        else {
            alert("ASP.NET TJax client-side framework failed to load.");
        }
    }
}

