﻿//var isIE = (document.all == "Microsoft Internet Explorer") ? true : false;
var isIE = (document.all) ? true : false;
var popupDialog = {
    name: 'popupDialog',
    obj: null
}

popupDialog.opacity = function(opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("popupDialog.changeOpac(" + i + ")", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("popupDialog.changeOpac(" + i + ")", (timer * speed));
            timer++;
        }
    }
}

popupDialog.changeOpac = function(opacity) {
    this.obj.style.display = 'block';
    this.obj.style.opacity = (opacity / 100);
    this.obj.style.MozOpacity = (opacity / 100);
    this.obj.style.KhtmlOpacity = (opacity / 100);
    this.obj.style.filter = "alpha(opacity=" + opacity + ")";
    if(opacity==0)
        this.obj.style.display = 'none';
}

popupDialog.findPos = function(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curtop = obj.offsetTop;
        if (!isIE)
            curleft = obj.offsetLeft;
        else {
            //alert(obj.offsetParent.offsetWidth + ' ' + Math.abs(obj.offsetLeft) + ' ' + obj.offsetWidth);
            curleft = obj.offsetParent.offsetWidth - Math.abs(obj.offsetLeft) - obj.offsetWidth;
        }

        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

popupDialog.fnClose = function() {
    this.opacity(100, 0, 1000);
    //this.obj.style.display = 'none';

}

popupDialog.fnShowDialog = function(owner, width, height, fnUICreator, time, data, offsetLeft, offsetTop) {
    Ajax.data = data;
    var pos = this.findPos(owner);
    if (this.obj == null) {
        this.obj = document.createElement('div');
        this.obj.setAttribute('id', this.name);
        this.obj.style.fontSize = '13px';
        this.obj.style.position = 'absolute';
        document.body.appendChild(this.obj);
    }
    this.obj.style.overflow = 'hidden';
    this.obj.style.border = 'solid 1px red';
    this.obj.style.width = width + 'px';
    this.obj.style.height = height + 'px';

    if (window.innerHeight)
        this.obj.style.left = pos[0] + "px";
    else
        this.obj.style.left = pos[0] + offsetLeft + "px";
    this.obj.style.top = pos[1] + offsetTop + "px";
    //this.obj.style.display = 'none';
    popupDialog.changeOpac(0);
    this.obj.innerHTML = eval(fnUICreator + '()');
    this.opacity(0, 100, time);

    return false;
}

var Ajax =
{
    http2: null,
    data: null,
    loading: null,
    fn_CallBack: "",
    f_FailCallBack: ""
}

Ajax.CreateXMLHttpRequest = function() {
    if (Ajax.http2 == null) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            try {
                Ajax.http2 = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                try {
                    Ajax.http2 = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
        { }
            }
        }
        else {
            Ajax.http2 = new XMLHttpRequest();
            if (Ajax.http2.overrideMimeType) {
                Ajax.http2.overrideMimeType('text/html');
            }
        }
    }
}
Ajax.fnCallBackAjax = function() {
    if (Ajax.http2.readyState == 4) {
        Ajax.ShowLoading("hide");
        if (Ajax.http2.status == 200) {
            //Ajax.ShowLoading("hide");
            if (Ajax.http2.responseText != null) {
                eval(Ajax.fn_CallBack + '(Ajax.http2.responseText)');
            }
            else {

                eval(Ajax.f_FailCallBack + '()');
            }
        }
    }
}
Ajax.fnSendAjax = function(HttpHandlerURL, Data, f_CallBack, f_FailCallBack) {
    var data = Data + '&' + Ajax.data;
    Ajax.fn_CallBack = f_CallBack;
    Ajax.f_FailCallBack = f_FailCallBack;
    Ajax.CreateXMLHttpRequest();
    Ajax.ShowLoading("show");
    var t = Ajax.http2.readyState; //alert('s' + Ajax.http2);
    Ajax.http2.open("POST", HttpHandlerURL, true);
    Ajax.http2.onreadystatechange = Ajax.fnCallBackAjax;
    Ajax.http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    Ajax.http2.setRequestHeader("Content-length", data.length);
    Ajax.http2.send(data);
    return false;
}
Ajax.ShowLoading = function(action) {
    if (this.loading == null) {
        this.loading = document.createElement('div');
        this.loading.style.backgroundColor = 'red';
        this.loading.style.padding = '3px';
        this.loading.style.fontSize = '11px';
        this.loading.style.position = 'absolute';
        this.loading.style.top = '0px';
        this.loading.style.right = '0px';
        this.loading.innerHTML = 'در حال ارتباط با سرور ...';
        document.body.appendChild(this.loading);
    }
    if (action == "show")
        this.loading.style.display = "block";
    else if (action == "hide")
        this.loading.style.display = "none";
    return false;
}

function fnLogOut() {
    window.location = '/View/Pages/LogOut.aspx?u=' + window.location;
    return false;
}
function fnOpenLogin() {
    window.location = '/View/Pages/Login.aspx?u=' + window.location;
    return false;
}
function openViewDoc(str) {
    window.open(str, null, "height=450,width=600,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1,")
    return false;
}
function openViewCriticism(str) {
    window.open("CriticismForm.aspx?articleId=" + str, null, "height=450,width=680,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1,")
}
function fnViewDownload(str) {
    window.open("/View/Register/Download.aspx?ArticleId=" + str, null, "height=480,width=700,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1,status=0, top=100, left=100")
}
function fnViewPoll(str) {
    window.open("/View/Pages/Poll.aspx?ArticleId=" + str, null, "height=400,width=500,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1,status=0,")
}

//low search in top
function topSearch()
{
    var str = document.getElementById('topSearchStr').value;
	if( str == ""  )
	{
		alert("لطفا عبارت جستجو را وارد کنید")	
		return false;
	}
	if(str.length == 1)
	{
		//alert("طول کلمه باید بیشتر از یک باشد")
		return false;
	}
	document.location = '/View/Search/LowSearch.aspx?txt=' + str;
	return true;
}

// top search enter
function topSearchDoIt(e) {

    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13)
	{
	    topSearch()
	}
}

function SetSize(type) {

        //alert(document.body.offsetHeight);
        //alert(document.body.clientHeight);
        //alert(document.documentElement.clientHeight);
        //alert(window.innerHeight);

    if (document.location.toString().indexOf('ViewPages.aspx', 0) > 0)
        return;
    if (type == false) {
        document.getElementById('page1').style.height = '';
        document.getElementById('TblBottom').style.top = '';
        return;
    }
    if (document.getElementById('page1') == null)
        return;
    var clientH;
    if (window.innerHeight)
        clientH = window.innerHeight;
    else
        //DOCTYPE?
        clientH = Math.max(document.documentElement.clientHeight, document.body.offsetHeight)-5;
    var documentH = document.getElementById('page1').offsetHeight;
    if (documentH < clientH) {
        document.getElementById('page1').style.height = clientH + 'px';
        var top = parseInt(document.getElementById('TblBottom').style.top);
        document.getElementById('TblBottom').style.top = ((isNaN(top) ? 0 : top) + (clientH - documentH))+'px';
    }
}
function fnBugFormSend() {
    var username = document.getElementById('bugFormUserName').value;
    var email = document.getElementById('bugFormEmail').value;
    var bugTitle = document.getElementById('bugFormTitle').value;
    var bugContent = document.getElementById('bugFormContent').value;
    
    var exp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (email.match(exp) == null || email == "") {
        alert("پست الکترونیکی خود را بطور صحیح وارد کنید");
        return false;
    }
    else if (bugTitle == "" || bugContent == "") {
        alert("عنوان و یا محتوای اشکال را کامل کنید")
        return false;
    }
    Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=1&paddress=' + window.document.location.href + '&ptitle='+window.document.title+'&bTitle=' + bugTitle + '&bText=' + bugContent + '&uEmail=' + email + '&uName=' + username, 'CB_fnBugFormSend', 'CBF_fnBugFormSend');
}
function CB_fnBugFormSend(result) {
    popupDialog.obj.innerHTML = '<div class="sbox" style="height:100%;">' + result + '</div>';
    setTimeout('popupDialog.fnClose()', 2000);
}
function CBF_fnBugFormSend() {
    popupDialog.obj.innerHTML = ".خطا در درج نظر";
}

function fnCriticismFormSend() {
    var criticismTitle = document.getElementById('CriticismTitle').value;
    var criticismText = document.getElementById('CriticismText').value;
    var DisplayName = document.getElementById('DispalyName').checked;
    if (criticismTitle == "" || criticismText == "") {
        alert("عنوان و یا محتوای نقد را کامل کنید");
        return;
    }
    Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=4&cTitle=' + criticismTitle + '&cText=' + criticismText + '&DName=' + DisplayName, 'CB_fnCriticismFormSend', 'CBF_fnCriticismFormSend');
}
function CB_fnCriticismFormSend(result) {
    popupDialog.obj.innerHTML = '<div class="sbox" style="height:100%;">' + result + '</div>';
    setTimeout('popupDialog.fnClose()', 2000);
}
function CBF_fnCriticismFormSend() {
    popupDialog.obj.innerHTML = ".خطا در درج نظر";
}

function fnMagsOfferFormSend() {
    var magsName = document.getElementById('offerMagsName').value;
    var telNum = document.getElementById('offerMagsPhone').value;
    var magsAddress = document.getElementById('offerMagsAddres').value;
    if (magsName == "") {
        alert("عنوان مجله را وارد کنید");
        return;
    }
    Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=3&magsName=' + magsName + '&telNum=' + telNum + '&magsAddress=' + magsAddress, 'CB_fnMagsOfferFormSend', 'CBF_fnMagsOfferFormSend');
}
function CB_fnMagsOfferFormSend(result) {
    popupDialog.obj.innerHTML = '<div class="sbox" style="height:100%;">' + result + '</div>';
    setTimeout('popupDialog.fnClose()', 2000);
}
function CBF_fnMagsOfferFormSend() {
    popupDialog.obj.innerHTML = ".خطا در درج نظر";
}

function fnCreateBugForm() {
    // style="width:340px, height:258px; position:absolute; left:100px; top:0px;"
    var str = '<div class="box1"><div class="btitle"><img class="ImgCloseIco" src="/View/images/Icons/Temp1/close.gif" onClick="popupDialog.fnClose()"/>فرم ارسال اشکال یا پیشنهاد</div>'
str += '<div class="bbody">'
str += '<div class="bugrow">'
str += '<div class="rcol">نام و نام خانوادگی</div>'
str += '<input type="text" class="tb" id="bugFormUserName" value="" style="width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol">پست الکترونیکی</div>'
str += '<input type="text" class="tb" id="bugFormEmail" value="" style="direction: ltr; width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol">عنوان اشکال</div>'
str += '<input type="text" class="tb" id="bugFormTitle" style="width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol">محتوای اشکال</div>'
str += '<textarea class="tb" style="position: relative; float: right; width: 190px; height: 100px" id="bugFormContent" rows="8" cols="6"></textarea>'
str += '</div>'
str += '<div style="text-align: center; padding-top: 5px; clear: both;">'
str += '<input type="button" class="ImgSend" onclick="fnBugFormSend()" />&nbsp;'
str += '<input type="button" class="ImgCancel" onclick="popupDialog.fnClose()" />'
str += '</div>'
str += '</div>'
str += '</div>'
return str;
}

function fnCreateOfferMagsForm() {
    var str = '<div class="btitle"><img class="ImgCloseIco" src="/View/images/Icons/Temp1/close.gif" onClick="popupDialog.fnClose()"/>فرم ارسال مجله پیشنهادی</div>'
str += '<div class="bbody">'
str += '<div class="bugrow">'
str += '<div class="rcol1">نام مجله</div>'
str += '<input type="text" class="tb" id="offerMagsName" value="" style="width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol1">شماره تلفن</div>'
str += '<input type="text" class="tb" id="offerMagsPhone" value="" style="direction: ltr;width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol1">آدرس مجله</div>'
str += '<textarea class="tb" style="position: relative; float: right; width: 190px; height: 95px" id="offerMagsAddres" rows="8" cols="6"></textarea>'
str += '</div>'
str += '<div style="text-align: center; padding-top: 5px; clear: both;">'
str += '<input type="button" class="ImgSend" onclick="fnMagsOfferFormSend()" />&nbsp;'
str += '<input type="button" class="ImgCancel" onclick="popupDialog.fnClose()" />'
str += '</div>'
str += '</div>'
return str;
}

function fnCreateCriticismForm() {
var str = '<div class="btitle"><img class="ImgCloseIco" src="/View/images/Icons/Temp1/close.gif" onClick="popupDialog.fnClose()"/>ارسال نظر</div>'
str += '<div class="bbody">'
str += '<div class="bugrow">'
str += '<div class="rcol1">عنوان</div>'
str += '<input type="text" class="tb" id="CriticismTitle" maxlength="150" value="" style="width: 190px;" />'
str += '</div>'
str += '<div class="bugrow">'
str += '<div class="rcol1">متن</div>'
str += '<textarea class="tb" style="position: relative; float: right; width: 190px; height: 95px" id="CriticismText" ></textarea>'
str += '</div>'
str += '<div class="bugrow">'
str += '<input type="checkbox"  checked="checked" class="tb" id="DispalyName" />مایلم نام من نشان داده شود.'
str += '</div>'
str += '<div style="text-align: center; padding-top: 5px; clear: both;">'
str += '<input type="button" class="ImgSend" onclick="fnCriticismFormSend()" />&nbsp;'
str += '<input type="button" class="ImgCancel" onclick="popupDialog.fnClose()" />'
str += '</div>'
str += '</div>'
return str;
}

var ImgLoading = null;
function showLoading(e) {
    if (ImgLoading == null)
        ImgLoading = document.getElementById('ImgLoading');
    var offset;
    if (document.all) {
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
        offset = -3;
    } else {
        tempX = e.pageX
        tempY = e.pageY
        offset = 10;
    }
    if (tempX < 0) { tempX = 0 }
    if (tempY < 0) { tempY = 0 }
    ImgLoading.style.display = 'inline';
    ImgLoading.style.top = tempY + 10 + 'px';
    ImgLoading.style.left = tempX + offset + 'px';
}

var cat = 0;
var precat = 0;
var mag = 0;

var catobj;
var magObj;
function fnGetMagazineList(owner, categoryId, selectedMagazineId) {
    try {
        precat = cat;
        mag = 0;
        selectedMagazineId = 0;
        if (precat == categoryId) {
            var p = document.getElementById('c' + precat).innerHTML;
            p = p.substring(0, p.toLowerCase().indexOf('</a>', 0));
            p = p.replace('-', '+');
            document.getElementById('c' + precat).innerHTML = p;
            cat = 0;
            SetSize(true);
            return false;
        }
        catobj = owner;
        cat = categoryId;
        document.body.onmousemove = showLoading;
        Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=5&cId=' + categoryId + '&mId=' + selectedMagazineId, 'CB_fnGetMagazineList', 'CBF_fnGetMagazineList');
    }
    catch (error) {
        alert(error);
    }
    finally {
        return false;
    }
}


function CB_fnGetMagazineList(result) {
    document.getElementById('ImgLoading').style.display = 'none';
    document.body.onmousemove = '';
    if (precat != 0) {
        var p = document.getElementById('c' + precat).innerHTML;
        p = p.substring(0, p.toLowerCase().indexOf('</a>', 0));
        p = p.replace('-', '+');
        document.getElementById('c' + precat).innerHTML = p;
    }
    var cObj = document.getElementById('c' + cat);
    cObj.innerHTML = cObj.innerHTML.replace('+', '-') + result;
    document.getElementById('leftCol').innerHTML = '';

    var pos = popupDialog.findPos(cObj);
    if (document.body.scrollTop > pos[1])
        window.scroll(0, pos[1]);
    if (catobj != null)
        if (window.document.location.toString().indexOf('?', 0) < 0)
            window.document.location = catobj.href.replace('?', '#');
     //else
     //    MByCLoad(2)
    SetSize(false);
}

function fnGetMagazineInfo(owner, categoryId, selectedMagazineId) {
    try {
        if (mag != 0) {
            var g = document.getElementById('m' + mag);
            g.attributes['class'].value = 'magazineItem';
        }
        mag = selectedMagazineId;
        magObj = owner;
        owner.parentNode.attributes['class'].value = 'smagazineItem';
        document.body.onmousemove = showLoading;
        Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=6&cId=' + categoryId + '&mId=' + selectedMagazineId, 'CB_fnGetMagazineInfo', 'CBF_fnGetMagazineInfo');
    }
    catch (error) {
        alert(error);
    }
    finally {
        return false;
    }
}
function fnGetMagazineNumberList(owner, categoryId, selectedMagazineId) {
    try {
        if (mag != 0) {
            var g = document.getElementById('m' + mag);
            g.attributes['class'].value = 'magazineItem';
        }
        mag = selectedMagazineId;
        magObj = owner;
        owner.parentNode.attributes['class'].value = 'smagazineItem';
        document.body.onmousemove = showLoading;
        Ajax.fnSendAjax('/view/ashx/bugForm.ashx', 'type=7&cId=' + categoryId + '&mId=' + selectedMagazineId, 'CB_fnGetMagazineInfo', 'CBF_fnGetMagazineInfo');
    }
    catch (error) {
        alert(error);
    }
    finally {
        return false;
    }
}
function CB_fnGetMagazineInfo(result) {
    document.getElementById('ImgLoading').style.display = 'none';
    document.body.onmousemove = '';

    var leftCol = document.getElementById('leftCol');
    leftCol.innerHTML = result;

    var pos = popupDialog.findPos(leftCol);
    if (document.body.scrollTop > pos[1])
        window.scroll(0, pos[1]);

    if (magObj != null) {
        if (window.document.location.toString().indexOf('?', 0) < 0)
            window.document.location = magObj.href.replace('?', '#');
        //else
          //  window.document.location = window.document.location.toString().substring(0, window.document.location.toString().length) + '#' + magObj.href;
    }
}

function MByCLoad(stage) {
    var f = window.location.toString();
    var index = f.indexOf('#', 0);
    var index1 = f.indexOf('?', 0);
    if (index1 >= 0) {
        var q = f.substring(index1 + 1, f.length);
        q = q.split('&');
        cat = q[0].replace('CategoryId=', '');
        if (q.length > 1)
            mag = q[1].replace('MagazineId=', '');
    }
    if (index < 0) {
    }
    else {
       // window.document.location = f.replace('#', '?');
        return;
        var q = f.substring(index + 1, f.length);
        //q = '';
        q = q.split('&');

        q[0] = q[0].replace('CategoryId=', '');
        q[1] =  q[1].replace('MagazineId=', '');
        q[2] =  q[2].replace('type=', '');
        if(stage==1)
        {
            fnGetMagazineList(null, q[0], q[1]);
            }
        else
        {
        if(q[2]=='1')
            fnGetMagazineInfo(null, q[0], q[1]);
        else
            fnGetMagazineNumberList(null, q[0], q[1]);
            }
    }
}