﻿var lastProdId, lastCommentId, lastQuestiontId, mouseX, mouseY;

function InitPage() {
    $('#MainDiv').show("slow", function () {
        $('#Wrapper .Header').slideDown("slow", function () {
            $('#MainContent').slideDown("slow");
            $('#Wrapper .Header H1').toggle("slow", function () {
                $('#Wrapper .Header H3').toggle("Slow");
            });
        });
    });
}
function closeHolder() {
    $('#productHolder').fadeOut(300);
    $('#loadingHolder').fadeOut(300);
}
function catClick() {
    
    $('#productHolder .prodsContainer').html('در حال بار گذاری...');
    $('#loadingHolder').fadeTo(700, 0.4, function () {
        $('#productHolder').fadeTo(700, 0.85);
    });
}
function UnloadPage() {
    try {
        $('.FlashHolder').slideUp(500, function () {
            $('#Wrapper').fadeOut(1000);
        });
    }
    catch (exception)
    { }
}
function ShowMenu() {
    $('#MainMenu').fadeTo("slow", 0.7);
}
function HideMenu() {
    $('#MainMenu').fadeOut("slow");
}
function ShowLogin() {
    $('#LoginForm').slideToggle("slow");
}
function CloseForm(formID) {
    $('#' + formID).fadeOut("slow", function () {
        endLoad();
    });
}
function PartsShow(partnerID) {
    $.getJSON("Server.aspx", { act: "getPartner", PartnerId: partnerID, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<img alt="لوگوی شرکت" src="Images/Partners/FullSize' + partnerID + '.jpg" />' +
            '<h3>' + data.Name + '</h3><br /><hr /><br />' + data.Comments + '<br /><br /><br />'+
            '<a href="' + data.URL + '">وب سایت ' + data.Name + '</a>';
            $('#frmPart_contents').html(br);
            $(".Locker").fadeTo(500, 0.6, function () {
                $("#frmPart").fadeIn(800);
            });
        }
    });
}
function CorpShow(CorpID) {
    $.getJSON("Server.aspx", { act: "getCorp", CorpId: CorpID, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<img alt="لوگوی شرکت" src="Images/Corp/FullSize' + CorpID + '.jpg" />' +
            '<h3>' + data.Name + '</h3><br /><hr /><br />' + data.Comments +
            '<a href="' + data.URL + '">وب سایت ' + data.Name + '</a>';
            $('#frmPart_contents').html(br);
            $(".Locker").fadeTo(500, 0.6, function () {
                $("#frmPart").fadeIn(800);
            });
        }
    });
}
function CertificateShow(CertificateID) {
    $.getJSON("Server.aspx", { act: "getCertificate", CertificateId: CertificateID, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<img alt="گواهینامه شرکت" src="Images/Certificates/FullSize' + CertificateID + '.jpg" />' +
            '<h3>' + data.Name + '</h3><br /><hr /><br />' + data.Comments +
            '<a>' + data.URL + '</a>';
            $('#frmPart_contents1').html(br);
            $(".Locker").fadeTo(500, 0.6, function () {
                $("#frmPart").fadeIn(800);
            });
        }
    });
}
function ProdPictureShow(slideID) {
    $.getJSON("Server.aspx", { act: "getSlide", SlideID: slideID, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<img style="max-width:300px !important; max-height:300px !important;" alt="' + data.Name + '" src="Images/Side_Sliderpic/' + data.Title.replace(/\ /g, '_') + '.jpg" />' +
            '<h3>' + data.Name + '</h3><br /><hr /><br /><p>' + data.Comment + '</p><br /><br /><a>برای کسب اطلاعات خرید<br />با دفتر فروش تماس حاصل<br />فرمایید</a>';
            $('#frmPart_contents').html(br);
            $(".Locker").fadeTo(500, 0.6, function () {
                $("#frmPart").fadeIn(800);
            });
        }
    });
} 
function SliderShowCM(slideID) {
    $.getJSON("Server.aspx", { act: "getSlide", SlideID: slideID, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<img style="max-width:300px !important; max-height:300px !important;" alt="' + data.Name + '" src="Images/Side_Sliderpic/' + data.Title.replace(/\ /g, '_') + '.jpg" />' +
            '<h3>' + data.Name + '</h3><br /><hr /><br /><p>' + data.Comment + '</p><br /><br /><a>برای کسب اطلاعات خرید<br />با دفتر فروش تماس حاصل<br />فرمایید</a>';
            $('#frmPart_contents').html(br);
            $(".Locker").fadeTo(500, 0.6, function () {
                $("#frmPart").fadeIn(800);
            });
        }
    });
} 
function UnderCnstruction() {
    $('#ComingSoon').toggle("slow");
    setTimeout('CloseForm(\'ComingSoon\')', 3000);
}
function startLoad() {
    //$('#MainDiv .pageLoader').fadeTo('normal', 0.7);
    $('#loadingHolder').stop().fadeTo(700, 0.65);
}
function endLoad() {
    //$('#MainDiv .pageLoader').fadeOut('fast');
    $('#loadingHolder').stop().fadeOut('fast');
    $(".Locker").stop().fadeOut('fast');
}
function closeForm(id) {
    endLoad();
    $('#' + id).fadeOut('slow');
}
function error(message) {
    startLoad();
    $('#errorMessage > .Message').html(message);
    $('#errorMessage').fadeIn('fast');
    $('#errorMessage').css('left', mouseX - 40);
    $('#errorMessage').css('top', mouseY - 20);
}
function getFiles(productID) {
    startLoad();
    $('#fc' + productID).html('لطفا کمی صبر کنید.');
    $.getJSON("Server.aspx", { act: "getFiles", prodId: productID, randNum: Math.random() }, function (data) {
        endLoad();
        if (data.res == "yes") {
            if (data.files.length == 0)
                $('#fc' + productID).html("فایلی برای این محصول وجود ندارد");
            else {
                $('#fc' + productID).html("");
                for (i = 0; i < data.files.length; i++)
                    $('#fc' + productID).html($('#fc' + productID).html() + '<a href="server.aspx?act=downloadFile&fileId=' + data.files[i].id + '" target="_blank" title="' + data.files[i].comment + '">' + data.files[i].name + '.' + data.files[i].type + '</a>&nbsp;');
            }
        }
        else
            $('#fc' + productID).html('شما وارد نشده اید و یا خطایی رخ داده است... لطفا بعدا تلاش نمایید');
    });
}
function getLearnings(productID) {
    startLoad();
    $.getJSON("Server.aspx", { act: "getLearnings", prodId: productID, randNum: Math.random() }, function (data) {
        endLoad();
        if (data.res == "yes") {
            if (data.learnings.length == 0)
                $('#lc' + productID).html("آموزشی برای این محصول وجود ندارد");
            else {
                $('#lc' + productID).html("");
                for (i = 0; i < data.learnings.length; i++)
                    $('#lc' + productID).html($('#lc' + productID).html() + '<a href="Learnings.aspx?id=' + data.learnings[i].id + '" title="' + data.learnings[i].comments + '">' + data.learnings[i].title + '</a>&nbsp;');
            }
        }
        else
            $('#lc' + productID).html('شما وارد نشده اید و یا خطایی رخ داده است... لطفا بعدا تلاش نمایید');
    });
}
function showAnswers(id) {
    if ($("#viewAnswers" + id).text() != "بستن پاسخها") {
        startLoad();
        $.getJSON("Server.aspx", { act: "getAnswers", prodId: id, randNum: Math.random() }, function (data) {
            endLoad();
            var comments = "";
            if (data.res == "no")
                comments = 'شما وارد نشده اید و یا خطایی رخ داده است... لطفا بعدا تلاش نمایید';
            else {
                if (data.answers.length == 0)
                    comments = "تا کنون پیامی ثبت نشده است";
                else
                    for (i = 0; i < data.answers.length; i++) {
                        comments += '<div class="Answer">' +
                        '<span class="AnswerUserName">' + data.answers[i].userName + '</span><span class="AnswerDate">' + data.answers[i].date + '</span>' +
                        '<div class="clear"></div><br/>' + data.answers[i].answer + '</div>';
                    }
            }
            $("#ac" + id).html(comments);
            $("#ac" + id).slideDown("Slow");
            $("#viewAnswers" + id).text("بستن پاسخها");
        });
    }
    else {
        $("#ac" + id).slideUp("Slow");
        $("#viewAnswers" + id).text("مشاهده پاسخها");
    }
}
function showProductCommentReplies(id) {
    if ($("#viewCommentReplies" + id).text() != "بستن پاسخها") {
        startLoad();
        $("#cr" + id).html("لطفا کمی صبر کنید.");
        $("#cr" + id).slideDown('normal');
        $.getJSON("Server.aspx", { act: "getCommentReplies", commentId: id, randNum: Math.random() }, function (data) {
            endLoad();
            var comments = "";
            if (data.length == 0)
                comments = "تا کنون پاسخی ثبت نشده است";
            for (i = 0; i < data.length; i++) {
                comments += '<span class="name">' +
                    data[i].name + '</span><span class="date">' +
                    data[i].date + '</span><div class="clear"></div>' +
                    '<div class="commentText">' + data[i].comment +
                    '</div><hr />';
            }
            $("#cr" + id).slideUp(100, function () {
                $("#cr" + id).html(comments);
                $("#cr" + id).slideDown('normal');
            });
            $("#viewCommentReplies" + id).text("بستن پاسخها");
        });
    }
    else {
        $("#cr" + id).slideUp("Slow", function () {
            $("#viewCommentReplies" + id).text("مشاهده پاسخهای کاربران");
        });
    }
}
function newComment(prodId) {
    lastProdId = prodId;
    startLoad();
    $.getJSON("Server.aspx", { act: "isLoggedIn", randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            $('#txtProdComment').val("");
            $('#NewCommentForm').css('left', mouseX - 30);
            $('#NewCommentForm').css('top', mouseY - 130);
            $('#NewCommentForm').fadeIn('fast', function () { $('#txtProdComment').focus(); });
        }
        else
            error('&nbsp;&nbsp;برای ارسال پیام ابتدا باید با نام کاربری خود وارد شده باشید (یا به اصطلاح <b>Login</b> کرده باشید)');
    });
}
function newReply(commentId) {
    lastCommentId = commentId;
    startLoad();
    $.getJSON("Server.aspx", { act: "isLoggedIn", randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            //inja
            $('#txtReply').val("");
            $('#NewReplyForm').css('left', mouseX - 30);
            $('#NewReplyForm').css('top', mouseY - 130);
            $('#NewReplyForm').fadeIn('fast', function () { $('#txtReply').focus(); });
        }
        else
            error('&nbsp;&nbsp;برای ارسال پیام ابتدا باید با نام کاربری خود وارد شده باشید (یا به اصطلاح <b>Login</b> کرده باشید)');
    });
}
function newQuestion() {
    startLoad();
    $.getJSON("Server.aspx", { act: "isLoggedIn", randNum: Math.random() }, function (data) {
        endLoad();
        if (data.res == "yes") {
            $('#txtQuestion').val("");
            $('#NewQuestionForm').css('left', mouseX - 80);
            $('#NewQuestionForm').css('top', mouseY - 70);
            $('#NewQuestionForm').fadeIn('fast', function () { $('#txtQuestion').focus(); });
        }
        else
            error('&nbsp;&nbsp;برای ارسال پیام ابتدا باید با نام کاربری خود وارد شده باشید (یا به اصطلاح <b>Login</b> کرده باشید)');
    });
}
function newQuestionAnswer(questionId) {
    lastQuestiontId = questionId;
    startLoad();
    $.getJSON("Server.aspx", { act: "isLoggedIn", randNum: Math.random() }, function (data) {
        endLoad();
        if (data.res == "yes") {
            $('#txtAnswer').val("");
            $('#NewAnswerForm').css('left', mouseX - 80);
            $('#NewAnswerForm').css('top', mouseY - 70);
            $('#NewAnswerForm').fadeIn('fast', function () { $('#txtAnswer').focus(); });
        }
        else
            error('&nbsp;&nbsp;برای ارسال پیام ابتدا باید با نام کاربری خود وارد شده باشید (یا به اصطلاح <b>Login</b> کرده باشید)');
    });
}
function sendComment() {
    if ($('#txtProdComment').val() != "") {
        startLoad();
        $.getJSON("Server.aspx", { act: "sendProdComment", comment: $('#txtProdComment').val(), prodId: lastProdId, randNum: Math.random() }, function (data) {
            endLoad();
            if (data.res == "yes") {
                closeForm('NewCommentForm');
            }
            else
                error('در ارسال پیام خطایی رخ داده است..');
        });
    }
    else
        error('لطفا پیام خود را وارد نمایید..');
}
function sendCommentReply() {
    if ($('#txtReply').val() != "") {
        startLoad();
        $.getJSON("Server.aspx", { act: "sendProdCommentReply", comment: $('#txtReply').val(), commentId: lastCommentId, randNum: Math.random() }, function (data) {
            endLoad();
            if (data.res == "yes") {
                closeForm('NewReplyForm');
            }
            else
                error('در ارسال خطایی رخ داده است..');
        });
    }
    else
        error('لطفا زیر پیام و یا پاسخ خود را وارد نمایید..');
}
function sendQuestion() {
    if ($('#txtQuestion').val() != "") {
        startLoad();
        $.getJSON("Server.aspx", { act: "sendFAQQuestion", question: $('#txtQuestion').val(), randNum: Math.random() }, function (data) {
            endLoad();
            if (data.res == "yes") {
                closeForm('NewQuestionForm');
            }
            else
                error('در ارسال سوال خطایی رخ داده است..');
        });
    }
    else
        error('لطفا سوال خود را وارد نمایید..');
}
function sendQuestionAnswer() {
    if ($('#txtAnswer').val() != "") {
        startLoad();
        $.getJSON("Server.aspx", { act: "sendFAQAnswer", answer: $('#txtAnswer').val(), questionId: lastQuestiontId, randNum: Math.random() }, function (data) {
            endLoad();
            if (data.res == "yes") {
                closeForm('NewAnswerForm');
            }
            else
                error('در ارسال پاسخ رخ داده است..');
        });
    }
    else
        error('لطفا پاسخ خود را وارد نمایید..');
}
function showFullNews(id) {
    window.location.href = "ShowNews.aspx?NewsID=" + id;
    //    startLoad();
    //    $.getJSON("Server.aspx", { act: "getNews", newsId: id, randNum: Math.random() }, function (data) {
    //        $('#MyForm .Headding .HeaddingText').html(data.title + " - " + data.date);
    //        $('#MyForm iframe').attr('src', 'News/' + id + '.html');
    //        //$('#lblNewsShowTitle').text(data.title);
    //        //$('#lblNewsShowDate').text(data.date);
    //        //$('#lblNewsShow').text(data.comment);
    //        endLoad();
    //        $('#MyForm').fadeIn("fast");
    //    });
}
function newUser() {
    $('#regForm').fadeIn('fast');
}
function newPassword() {
    $('#passForm').fadeIn('fast');
}
function HideAllMenus() {
    $('.lowerHeader div').hide();
}
function HideMenu(menuName) {
    if ($('#' + menuName).css('display') != 'none')
        $('#' + menuName).slideUp('fast');
}
function ShowMenu(menuName) {
    $('#' + menuName).slideDown('normal');
}
function getBranches(ostan) {
    $.getJSON("Server.aspx", { act: "getBranches", Ostan: ostan, randNum: Math.random() }, function (data) {
        if (data.res == "yes") {
            br = '<h3>' + "نمایندگان استان :" + ostan + '</h3>' + '</br>' + '</hr>';
            for (i = 0; i < data.Branches.length; i++) {
                br += '<div>';
                br += '<h4>' + data.Branches[i].fullName + '</h4>';
                br += data.Branches[i].city + ', ' + data.Branches[i].address + '<br />';
                br += "تلفن : " + data.Branches[i].phone + '<br />';
                br += "فکس : " + data.Branches[i].fax + '<br />';
                br += "همراه : " + data.Branches[i].cellPhone + '<br />';
                br += "نوع فعالیت : " + data.Branches[i].noeHamkari + '<br />';
                br += "توضیحات : " + data.Branches[i].comments + '<br />';
                br += '</div><hr />';
            }
            $('#Branches').html(br);
            $('#Branches').slideDown(1000);
        }
        else {
            //alert(data.res);
            br = '<h3>' + "نمایندگان استان :" + ostan + '</h3>' + '</br>' + '</hr>';
            br += "جهت کسب اطلاعات بیشتر با واحد نمایندگی تماس حاصل فرمایید . . .";
            $('#Branches').html(br);
            $('#Branches').slideDown(1000);
        }
    });
}
$(document).ready(function () {
    $('.FlashHolder').slideDown(500);
    $(document).mousemove(function (e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    });
});
