﻿
function testKeyDouble(o) {
    var ptn = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/
    if (!ptn.exec(o.value)) o.value = "";

}

function testKeyInt(o) {
    //var ptn = /^[0-9]*[1-9][0-9]*$/
    var ptn = /^[0-9]+$/
    if (!ptn.exec(o.value)) o.value = "";

}
function _myCheck(o) {
    try {
        var _test = o.value;
        if (_test == "-") {
            return;
        }
        if (isNaN(_test)) {
            if (_judge(str) > 1) {
                var str = _test;
                o.value = str.replace(/[^\d.]/g, "");
            }
        }

    } catch (ex) {
        var _test = o.value;
        var str = _test;
        o.value = str.substring(0, str.length - 1);
    } finally {

    }
}
function showDiv() {
    document.getElementById('popDiv').style.display = 'block';
    document.getElementById('popIframe').style.display = 'block';
    document.getElementById('bg').style.display = 'block';
}
function closeDiv() {
    document.getElementById('popDiv').style.display = 'none';
    document.getElementById('bg').style.display = 'none';
    document.getElementById('popIframe').style.display = 'none';

}
function StockFocus(iThis) {
    if (iThis.value.replace(/(^\s*)|(\s*$)/g, "") == "名称/代码/拼音") {
        iThis.value = "";
        iThis.style.color = "#cccccc";
    }
}
function StockBlur(iThis) {
    if (iThis.value.replace(/(^\s*)|(\s*$)/g, "") == "") {
        iThis.value = "名称/代码/拼音";
        iThis.style.color = "#cccccc";
    }
    else {
        ShowDiv(0, 4);
    }
}
function StockZFFocus(iThis) {
    if (iThis.value.replace(/(^\s*)|(\s*$)/g, "") == "涨跌幅度") {
        iThis.value = "";
        iThis.style.color = "#cccccc";
    }
}
function StockZFBlur(iThis) {
    if (iThis.value.replace(/(^\s*)|(\s*$)/g, "") == "") {
        iThis.value = "涨跌幅度";
        iThis.style.color = "#cccccc";
    }
}
function ShowDiv(divid, divcount) {
    txtDisplay = $("#display1")[0];
    if (divid == 0) {
        txtDisplay.from = "-10";
        txtDisplay.to = "10";
    }
    else if (divid == 1) {
        txtDisplay.from = "-60";
        txtDisplay.to = "60";
    }
    else if (divid == 2 || divid == 3) {
        txtDisplay.from = "-100";
        txtDisplay.to = "100";
    }
    for (var i = 0; i < divcount * 1; i++) {
        if (divid == i) {

            //document.getElementById("t" + i).className = "on";
            document.getElementById("DateType").value = i;
            var stockNo = "";
            if (document.all("txtStock").value != "名称/代码/拼音") {
                stockNo = document.getElementById("txtStock").value;
            }
            $.ajax({
                url: "/AjaxPage/FuWu/GetWYAvgStockZF.aspx?action=ajax_getcomment&StockNo=" + stockNo + "&DateType=" + i + "&Random=" + Math.random(),
                type: 'GET',
                success: function() {
                    $("#WYAvgStockZF").html("网友预测" + arguments[0] + "%");
                    var avalue = parseInt(arguments[0]);
                    if (avalue > 0) {
                        $("#pValue").attr("style", "color:red");
                    }
                    else if (avalue >= 0) {
                    $("#pValue").attr("style", "color:#009800");
                    }
                    $("#pValue").html(arguments[0].replace(".00", "") + "%");
                    //alert(arguments[0]);
                    var fromValue = parseInt(txtDisplay.from);
                    var toValue = parseInt(txtDisplay.to);
                    var x = parseInt(arguments[0]);
                    var lineWidth = 190
                    //                    if (window.location.href.toString().indexOf("Index.html") > -1) {
                    //                        lineWidth = 190;
                    //                    }
                    var p = lineWidth * (toValue + x) / (toValue - fromValue);
                    $("#WYAvgStockZF")[0].style.left = p.toString() + "px";
                }
            });

        }
        else {

            //document.getElementById("t" + i).className = ""
        }
    }

}

function ShowAllForecast(divid, divcount) {
    for (var i = 0; i < divcount * 1; i++) {
        if (divid == i) {

            document.getElementById("t" + i).className = "on";
            $.ajax({
                url: "/AjaxPage/FuWu/ShowAllForecast.aspx?action=ajax_getcomment&ID=" + i + "&Random=" + Math.random(),
                type: 'GET',
                success: function() {
                    $("#AllForecast").html(arguments[0]);
                }
            });        

        }
        else {

            document.getElementById("t" + i).className = ""
        }
    }

}

function GoFuWuIndex() {
    window.location = '/FuWu/index.aspx'
}

function DoForecast() {
    if (($("#hiForecastType").length == 0 || ($("#hiForecastType").length > 0 && $("#hiForecastType")[0].value == "0" )) && (document.all("txtStock").value == "" || document.all("txtStock").value == "名称/代码/拼音")) {
        alert("请输入股票名称/代码/拼音！");
        return false;
    }
    if (document.all("display1").value == "0" || document.all("display1").value == "涨跌幅度") {
        alert("请拖动滑块预测涨跌幅度！");
        return false;
    }
    if (document.all("txtRemarke").value.replace(/(^\s*)|(\s*$)/g, "") == "") {
        alert("请输入观点！");
        return false;
    }

    Forecast();
    
}

function ShowForecast(id) {    
    var isDisplay = $("#Forecast_" + id).css("display");
    $(".ForecastTR").each(function() {
        $(this).hide();
    });

    if ($("#Forecast_" + id)[0]) {
        if (isDisplay == "none") {
            $("#Forecast_" + id).show();

            $.ajax({
            url: "/AjaxPage/FuWu/ShowForecast.aspx?action=ajax_getcomment&ID=" + id + "&Random=" + Math.random(),
                type: 'GET',
                success: function() {
                $("#Forecast_" + id).html(arguments[0]);
                }
            });
        }
        else {
            $("#Forecast_" + id).hide();
        }
    }


}
function ForecastToTwitter(id, stockNo,stockName, remarke) {
    
    //var content = "<p onclick=\"ShowForecast('"+id+"');\" style=\"cursor:hand;\"><img src=\"images/CImg.png\" width=\"14\" height=\"14\" /><a href=\"javascript:void(0)\">查看预测价格</a></p><p id='Forecast_"+id+"' class=\"ForecastTR\"></p>";
    //var topic = "";
    //var title = "<a href='#'>#" + stockName + "#</a>" + remarke;
    //var imgPath = "";
    //var imgExtName = "";
    
    $.post('/AjaxPage/FuWu/AddForecastToTwitter.aspx', { ID: id, StockNo: stockNo, StockName: stockName, Remarke: remarke }, ForecastToTwitterReturn);
}

function ForecastToTwitterReturn(result) {    
    if (typeof result != 'undefined') {
        if (parseInt(result) > 0) {
            //window.location.reload();
            alert("转发成功");
        }
        else if (parseInt(result) == -1) {
            showDiv();
        }
        else if(parseInt(result) == 0){
            alert("失败");
        }
    }
}

var carpeDefaultSliderLength = 188
var carpeSliderDefaultOrientation = 'horizontal'
var carpeSliderClassName = 'carpe_slider'
var carpeSliderDisplayClassName = 'carpe_slider_display'

// carpeGetElementsByClass: Cross-browser function that returns
// an array with all elements that have a class attribute that
// contains className
function carpeGetElementsByClass(className) {
    var classElements = new Array()
    var els = document.getElementsByTagName("*")
    var elsLen = els.length
    var pattern = new RegExp("\\b" + className + "\\b")
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i]
            j++
        }
    }
    return classElements;
}
// carpeLeft: Cross-browser version of "element.style.left"
// Returns or sets the horizontal position of an element.
function carpeLeft(elmnt, pos) {
    if (!(elmnt = document.getElementById(elmnt))) return 0;
    if (elmnt.style && (typeof (elmnt.style.left) == 'string')) {
        if (typeof (pos) == 'number') elmnt.style.left = pos + 'px';
        else {
            pos = parseInt(elmnt.style.left);
            if (isNaN(pos)) pos = 92;//默认所在的位置
        }
    }
    else if (elmnt.style && elmnt.style.pixelLeft) {
        if (typeof (pos) == 'number') elmnt.style.pixelLeft = pos;
        else pos = elmnt.style.pixelLeft;
    }
    return pos;
}
// carpeTop: Cross-browser version of "element.style.top"
// Returns or sets the vertical position of an element.
function carpeTop(elmnt, pos) {
    if (!(elmnt = document.getElementById(elmnt))) return 0;
    if (elmnt.style && (typeof (elmnt.style.top) == 'string')) {
        if (typeof (pos) == 'number') elmnt.style.top = pos + 20 + 'px';//在某一个垂直坐标运动
        else {
            pos = parseInt(elmnt.style.top);
            if (isNaN(pos)) pos = 0;
        }
    }
    else if (elmnt.style && elmnt.style.pixelTop) {
        if (typeof (pos) == 'number') elmnt.style.pixelTop = pos;
        else pos = elmnt.style.pixelTop;
    }
    return pos;
}
// moveSlider: Handles slider and display while dragging
function moveSlider(evnt) {
    
    var evnt = (!evnt) ? window.event : evnt; // The mousemove event
    if (mouseover) { // Only if slider is dragged
        x = slider.startOffsetX + evnt.screenX // Horizontal mouse position relative to allowed slider positions
        y = slider.startOffsetY + evnt.screenY // Horizontal mouse position relative to allowed slider positions
        if (x > slider.xMax) x = slider.xMax // Limit horizontal movement
        if (x < 0) x = 0 // Limit horizontal movement
        if (y > slider.yMax) y = slider.yMax // Limit vertical movement
        if (y < 0) y = 0 // Limit vertical movement
        carpeLeft(slider.id, x)  // move slider to new horizontal position
        carpeTop(slider.id, y) // move slider to new vertical position
        sliderVal = x + y // pixel value of slider regardless of orientation
        sliderPos = (slider.distance / display.valuecount) *
			Math.round(display.valuecount * sliderVal / slider.distance)
        v = Math.round((sliderPos * slider.scale + slider.from) * // calculate display value
			Math.pow(10, display.decimals)) / Math.pow(10, display.decimals)
        display.value = v // put the new value in the slider display element
        //$("#slider1")[0].innerHTML = "我是" + v + "%";
        document.getElementById("pValue").innerHTML = v + "%";
        //var avalue = parseInt(arguments[0]);
        if (v > 0) {
            $("#pValue").attr("style", "color:red");
        }
        else if (v >= 0) {
            $("#pValue").attr("style", "color:#009800");
        }
        $("#spanTxt").html("我的预测");
        $("#divTips").attr("style", "display:none");
        //$("#pValue")[0].innerHTML = v+"%";
        if (v > 0) {
            document.getElementById("ddlPriceType").value = 1;
        }
        else {
            document.getElementById("ddlPriceType").value = 2;
        }
        return false
    }
    return
}
// slide: Handles the start of a slider move.
function slide(evnt) {
    
    if (!evnt) evnt = window.event; // Get the mouse event causing the slider activation.
    slider = (evnt.target) ? evnt.target : evnt.srcElement; // Get the activated slider element.
    dist = parseInt(slider.getAttribute('distance')) // The allowed slider movement in pixels.
    slider.distance = dist ? dist : carpeDefaultSliderLength // Deafault distance from global var.
    ori = slider.getAttribute('orientation') // Slider orientation: 'horizontal' or 'vertical'.
    orientation = ((ori == 'horizontal') || (ori == 'vertical')) ? ori : carpeSliderDefaultOrientation
    // Default orientation from global variable.
    displayId = slider.getAttribute('display') // ID of associated display element.
    display = document.getElementById(displayId) // Get the associated display element.
    display.sliderId = slider.id // Associate the display with the correct slider.
    dec = parseInt(display.getAttribute('decimals')) // Number of decimals to be displayed.
    display.decimals = dec ? dec : 0 // Default number of decimals: 0.
    val = parseInt(display.getAttribute('valuecount'))  // Allowed number of values in the interval.
    display.valuecount = val ? val : slider.distance + 1 // Default number of values: the sliding distance.
    from = parseFloat(display.getAttribute('from')) // Min/start value for the display.
    from = from ? from : 0 // Default min/start value: 0.
    to = parseFloat(display.getAttribute('to')) // Max value for the display.
    to = to ? to : slider.distance // Default number of values: the sliding distance.
    slider.scale = (to - from) / slider.distance // Slider-display scale [value-change per pixel of movement].
    if (orientation == 'vertical') { // Set limits and scale for vertical sliders.
        slider.from = to // Invert for vertical sliders. "Higher is more."
        slider.xMax = 0
        slider.yMax = slider.distance
        slider.scale = -slider.scale // Invert scale for vertical sliders. "Higher is more."
    }
    else { // Set limits for horizontal sliders.
        slider.from = from
        slider.xMax = slider.distance
        slider.yMax = 0
    }
    slider.startOffsetX = carpeLeft(slider.id) - evnt.screenX // Slider-mouse horizontal offset at start of slide.//点下mouse后让移动条不要回到最左边
    slider.startOffsetY = carpeTop(slider.id) - evnt.screenY // Slider-mouse vertical offset at start of slide.
    mouseover = true
    document.onmousemove = moveSlider // Start the action if the mouse is dragged.
    document.onmouseup = sliderMouseUp // Stop sliding.
    return false
}
// sliderMouseUp: Handles the mouseup event after moving a slider.
// Snaps the slider position to allowed/displayed value.
function sliderMouseUp() {
    //if ($("#hiIsGoForecast").length > 0) {
        //window.location = "/FuWu/NotLoggedStockForecast.aspx";
    //}
    if (mouseover) {
        v = (display.value) ? display.value : 0 // Find last display value.
        pos = (v - slider.from) / (slider.scale) // Calculate slider position (regardless of orientation).
        if (slider.yMax == 0) {
            pos = (pos > slider.xMax) ? slider.xMax : pos
            pos = (pos < 0) ? 0 : pos
            carpeLeft(slider.id, pos) // Snap horizontal slider to corresponding display position.
        }
        if (slider.xMax == 0) {
            pos = (pos > slider.yMax) ? slider.yMax : pos
            pos = (pos < 0) ? 0 : pos
            carpeTop(slider.id, pos) // Snap vertical slider to corresponding display position.
        }
        if (document.removeEventListener) { // Remove event listeners from 'document' (W3C).
            document.removeEventListener('mousemove', moveSlider, false)
            document.removeEventListener('mouseup', sliderMouseUp, false)
        }
        else if (document.detachEvent) { // Remove event listeners from 'document' (IE).
            document.detachEvent('onmousemove', moveSlider)
            document.detachEvent('onmouseup', sliderMouseUp)
        }
    }
    mouseover = false // Stop the sliding.
}
function focusDisplay(evnt) {
    if (!evnt) evnt = window.event; // Get the mouse event causing the display activation.
    display = (evnt.target) ? evnt.target : evnt.srcElement; // Get the activated display element.
    lock = display.getAttribute('typelock') // Is the user allowed to type into the display?
    if (lock == 'on') {
        display.blur()
    }
    return
}
window.onload = function() // Set up the sliders and the displays.
{
    sliders = carpeGetElementsByClass(carpeSliderClassName) // Find the horizontal sliders.
    for (i = 0; i < sliders.length; i++) {
        sliders[i].onmousedown = slide // Attach event listener.
    }
    displays = carpeGetElementsByClass(carpeSliderDisplayClassName) // Find the displays.
    for (i = 0; i < displays.length; i++) {
        displays[i].onfocus = focusDisplay // Attach event listener.
    }
}

//删除预测
function deleteForecast(id) {
    if (window.confirm("你确定要删除吗？")) {
        $.post("/AjaxPage/FuWu/DeleteForecast.aspx", { Id: id }, deleteForecastReturn);
    }
}
function deleteForecastReturn(str) {
    if (str == "0") {
        alert("删除失败");
    }
    else if (str == "1") {
        alert("删除成功");
        window.location.reload();
    }
}

function reloadRandImage() {
    $('#imgRandCode')[0].src = '/ImgRndCode.aspx?' + Math.random();
    if ($('#ImgRndCode2').length > 0) {
        $('#ImgRndCode2')[0].src = '/ImgRndCode.aspx?' + Math.random();
    }
}
var msg_err_username = "请输入用户名";
var msg_err_password = "请输入密码";
function showDiv() {
    document.getElementById('popDiv').style.display = 'block';
    document.getElementById('popIframe').style.display = 'block';
    document.getElementById('bg').style.display = 'block';
}
function closeDiv() {
    document.getElementById('popDiv').style.display = 'none';
    document.getElementById('bg').style.display = 'none';
    document.getElementById('popIframe').style.display = 'none';

}
function showReg() {
    $("#tr1")[0].style.display = "";
    $("#tr2")[0].style.display = "";
    $("#tr3")[0].style.display = "";
    $("#tr4")[0].style.display = "";
    $("#tr5")[0].style.display = "";
}
function showWinReg() {
    document.getElementById("showWin").style.display = "";
    document.getElementById("RegInfoWin").style.display = "none";
}
function showWin() {
    document.getElementById("RegInfoWin").style.display = "";
    document.getElementById("showWin").style.display = "none";
}
//注册成功后
RegSuccSpecial = true;
function RegSuccSpecialFun() {
    if ($("#divReg").length > 0) {
        alert("注册成功，且已经登陆！");
        $("#divReg")[0].style.display = "none"
    }
    //$("#divReg")[0].style.display = "none";
    if ($("#CustomerLoginSpan").length > 0) {
        $("#CustomerLoginSpan")[0].innerHTML = "<a href=\"/FuWu/index.aspx\">进入我的万隆</a>"
    }
}

function SimpleLogin() {
    if (!$("#txtUserName").valid()) {
        alert(msg_err_username); return;
    }
    if (!$("#txtPassWord").valid()) {
        alert(msg_err_password); return;
    }
    var validate = {
        async: true,
        url: "/Ajaxpage/Register/Login.aspx", //后台处理程序
        type: "post", //数据发送方式
        dataType: "html", //接受数据格式 
        data: { //要传递的数据
            oper: 'Login',
            username: function() {
                return encodeURI($("#txtUserName").val());
            },
            password: function() {
                return $("#txtPassWord").val();
            },
            ValidFlag: false
        }
    };
    var _SuccCallBack = function(data, status) {
        var d = eval("(" + data + ")");
        if (d.res == 0)//失败
            alert(d.des);
        if (d.res == -1)//用户类型错误
            alert(d.des);
        if (d.res == -2)
            location.href = "/Register/RefuseLogin.aspx";
        if (d.res == 2)
            location.href = "/Register/Register_ByMobile2.aspx?M=" + d.des + "&type=1";
        if (d.res == 1)//登录成功
        {
            alert("登录成功");
            closeDiv();
            //$("#divReg")[0].style.display = "none";
            if ($("#CustomerLoginSpan").length > 0) {
                $("#CustomerLoginSpan")[0].innerHTML = "<a href=\"/FuWu/index.aspx\">进入我的万隆</a>"
            }
        }
    };
    AjaxUtil.Post(validate, null, _SuccCallBack, "SimpleLoginBt");
}
function Forecast() {
    var StockKey = $("#txtStock")[0].value;
    var StockZF = $("#display1")[0].value;
    var PriceType = $("#ddlPriceType")[0].value;
    var DateType = $("#DateType")[0].value;
    var Remarke = $("#txtRemarke")[0].value;
    $.post('/AjaxPage/FuWu/AddStockForecast.aspx', { StockKey: StockKey, StockZF: StockZF, PriceType: PriceType, DateType: DateType, Remarke: Remarke, ForecastType: $("#hiForecastType")[0].value }, ForecastTrun)
}
function ForecastTrun(str) {
    if (str == "1") {
        alert("预测成功");
        window.location.reload();
    }
    else if (str == "-2") {
        showDiv();
    }
    else if (str == "-1") {
        alert("没有该股票");
    }
    else if (str == "") {
        alert("没有足够的参数");
    }
}

function oneStock(c) {
    if (c.checked) {
//        document.getElementById("spanBigStock").className = "";
//        document.getElementById("spanOneStock").className = "on";
        document.getElementById("divStock").style.display = "";
        document.getElementById("hiForecastType").value = "0";
        ShowDiv(0, 4);
    }
}
function bigStock(c) {
    if (c.checked) {
        document.getElementById("divStock").style.display = "none";
        document.getElementById("hiForecastType").value = "1";
        document.getElementById("txtStock").value = "名称/代码/拼音";
        ShowDiv(0, 4);
    }
}

function textPwdOnFocus(pwdId, txtId) {
    document.getElementById(txtId).style.display = "none";
    document.getElementById(pwdId).style.display = "block";
    document.getElementById(pwdId).focus();
}

function txtPwdOnBlur(pwdId, txtId) {
    if ($("#" + pwdId).val() == "") {
        document.getElementById(txtId).style.display = "block";
        document.getElementById(pwdId).style.display = "none";
    }
}
function GoMore() {
    $.post('/AjaxPage/IsLoginAjax.aspx', null, GoMoreReturn);
}
function GoMoreReturn(str) {
    var val = parseInt(str);
    if (val == 1 || val == 2) {
        window.location = "/FuWu/MyConcernForecast.aspx";
    }
    else {
        showDiv();
    }
}
