﻿var $j = jQuery.noConflict();
var pageSum = 1;
var currentPageIndex = 1;
$j(document).ready(function() {
    changeChkBox();
    loadCheckJs();
    loadNews(0);
});

var userName = '';
function changeChkBox() {
    var chkGuest = document.getElementById('chkGuest');
    if (chkGuest.checked) {
        $j(chkGuest).next().show();
        $j('#txtFb_UserName').hide();
        userName = $j('#txtFb_UserName').val();
        $j('#txtFb_UserName').val('guest');
    } else {
        $j(chkGuest).next().hide();
        $j('#txtFb_UserName').show();
        if (userName != '') {
            $j('#txtFb_UserName').val(userName);
        } else {
            userName = $j('#txtFb_UserName').val();
        }
    }
}

pageIndex = ' + pageIndex+';
function loadNews(pageIndex) {
    var pageSize = 5;
    $j('#div_newsWait').show();
    $j.ajax({
        url: '/FeedBack.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=GetFeedBack&pageIndex=' + pageIndex + '&pageSize=' + pageSize,
        dataType: 'html',
        success: function(json) {
            $j('#div_newsWait').hide();
            if (json == '') {
                return;
            }
            var arrNews = json.split('┣');
            var newsContent = '';
            pageSum = parseInt(arrNews[0].split('├')[5]);
            loadPageControl(pageIndex + 1, pageSum);
            for (var i = 0; i < arrNews.length; i++) {
                var news = arrNews[i].split('├');
                newsContent += '<div class="fontbox2"><div class="flightSearch">';
                newsContent += '<fieldset>\
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="feedback_tb"><tr >\
                                        <td width="44%"><img src="/images/text.gif">&nbsp;&nbsp;<b>' + news[0] + '</b></td>\
                                        <td width="27%">name:<b>' + news[1] + '</b></td>\
                                        <td width="29%" align="right">Data:' + news[3] + '</td>\
                                        </tr></table>\
                                        </fieldset>';
                newsContent += '<fieldset>' + news[2] + '<br />';
                if ($j.trim(news[4]).length > 1) {
                    newsContent += '<div class="feedback_hf">' + news[4] + '<p style="text-align:right;"><br />By:<span class="red">Manager</span></p></div></fieldset></div></div>';
                } else {
                    newsContent += '<p></p></fieldset></div></div>';
                }
            }
            $j('#div_feedBack').html(newsContent);
        }
    });
}

// Init Page Control
function loadPageControl(pageIndex, totalPage) {
    if (pageIndex <= 0) {
        pageIndex = 1;
    } else if (pageIndex >= totalPage) {
        pageIndex = totalPage;
    }
    var showPage = 10;
    if (totalPage == 0 || totalPage == 1) {
        $j('#div_pageChange').html('<strong>1</strong>');
        return;
    }
    var pageHtml = '';
    if (pageIndex >= 10) {
        pageHtml += '<a href="javascript:pageClick(1, 0);">1</a>...';
    }
    var firstPage = parseInt(pageIndex / 10) * 10;
    if (pageIndex % 10 != 0) {
        firstPage += 1;
    }
    var isBreak = false;
    for (var i = 0; i < 10; i++) {
        if (pageIndex == firstPage) {
            pageHtml += '<strong>' + firstPage + '</strong>';
        } else {
            pageHtml += '<a href="javascript:pageClick(' + firstPage + ', 0);">' + firstPage + '</a>';
        }
        if (firstPage >= totalPage) {
            isBreak = true;
            break;
        }
        firstPage++;
    }
    if (!isBreak) {
        pageHtml += '...<a href="javascript:pageClick(' + totalPage + ', 0);">' + totalPage + '</a>';
    }
    $j('#div_pageChange').html(pageHtml);
}

// Page Change
function pageClick(pageIndex, step) {
    pageIndex = parseInt(pageIndex);
    if (isNaN(pageIndex)) {
        pageIndex = 1;
    }
    if (pageIndex > pageSum) {
        pageIndex = pageSum;
    }
    if (pageIndex < 1) {
        pageIndex = 1;
    }
    if (step != 0) {
        pageIndex = parseInt($j('#div_pageChange > strong').html());
    }
    if (step == 1 && pageIndex < pageSum) {
        pageIndex += step;
    } else if (step == -1 && pageIndex > 1) {
        pageIndex += step;
    }
    if (currentPageIndex == pageIndex) {
        return;
    }
    currentPageIndex = pageIndex;
    loadNews(pageIndex - 1);
}

var isIE = navigator.userAgent.indexOf("MSIE") > 0;
function checkCustomerEnter(e) {
    var keyNum = 0;
    if (isIE) {
        keyNum = e.keyCode;
    } else {
        keyNum = e.which;
    }
    var amount = document.getElementById('txtPageIndex').value;
    for (var i = 0; i < amount.length; i++) {
        var keyCode = amount.charCodeAt(i);
        if (keyCode < 48 || keyCode > 57) {
            amount = amount.replace(amount.charAt(i), '');
        }
    }
    document.getElementById('txtPageIndex').value = amount;
}

function ischekemail() {
    var useremail = $j('#txtFb_Email').val();
    var regEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (useremail.length > 0) {
        if (regEmail.test(useremail) && useremail.length <= 50) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

var isRepeat = false;
function submitForm() {
    if (ischekemail() == false) {
        alert('Format Error!');
        return false;
    }
    if (isRepeat) {
        alert('Not allowed to repeat the submission of data');
        return;
    }
    isRepeat = true;
    if (!checkForm()) {
        return;
    }
    var content = $j('#txtContent').val();
    content = (content || "").replace(/^\s+|\s+$/g, "");
    if (content == "") {
        alert('Please enter the content');
        return;
    }
    if (content.length > 2000) {
        alert('No more than 2,000 characters, please.');
        return;
    }
    var title = $j('#txtTitle').val();
    var userName = $j('#txtFb_UserName').val();
    var userEmail = $j('#txtFb_Email').val();
    $j('#submit_wait').show();
    $j.ajax({
        url: '/FeedBack.aspx?rd=' + Math.random(),
        type: 'post',
        data: 'operate=AddFeedBack&title=' + title + '&userName=' + userName + '&content=' + content + '&email=' + userEmail,
        dataType: 'json',
        success: function(data) {
            $j('#submit_wait').hide();
            if (data) {
                $j('#shenh > img').attr('src', '/Images/smil.gif');
                $j('#shenh > font').html('<p>Thanks for your time!<br>Feedback Posted Successfully!</p><p>We need a short time to do the censorship of the post. Your feedback will be shown soon after that.</p>');
            } else {
                $j('#shenh > img').attr('src', '/Images/smil2.gif');
                $j('#shenh > font').html('<p>Sorry,failed to submit your post, please try again later</p>');
            }
            centerwh($j('#shenh'));
            $j('#shenh').fadeIn(1000);
            $j('#shenh').jqDrag();
        }
    });
}

function centerwh(obj) {
    var h1 = document.body.clientHeight;
    var h2 = document.documentElement.clientHeight;
    var isXhtml = (h2 <= h1 && h2 != 0) ? true: false;
    var body = isXhtml ? document.documentElement: document.body;
    var wh = {
        left: 0,
        top: 0
    };
    var vwh = {
        w: 0,
        h: 0
    };
    vwh.w = $j.browser.msie ? parseInt(body.clientWidth) : parseInt(window.innerWidth);
    vwh.h = $j.browser.msie ? parseInt(body.clientHeight) : parseInt(window.innerHeight);
    wh.left = (vwh.w / 2) - parseInt($j(obj).css("width")) / 2;
    wh.top = body.scrollTop + (vwh.h / 2) - parseInt($j(obj).css("height")) / 2;
    $j(obj).css("top", wh.top);
    $j(obj).css("left", wh.left);
}
