﻿function IsInt(v)
{
    y = parseInt(v) + "";
    if (v + "" == y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function StringToDate(DateStr)
{
    var converted = Date.parse(DateStr);
    var myDate = new Date(converted);
    //    if (isNaN(myDate))
    //    {
    //        //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; 
    //        var arys = DateStr.split('-');
    //        myDate = new Date(arys[0], --arys[1], arys[2]);
    //    }
    return myDate;
}

Array.prototype.del = function(n)
{
    if (n < 0) return this;
    return this.slice(0, n).concat(this.slice(n + 1, this.length));
}

function C(N, R)
{
    return P(N, R) / P(R, R);
}

//排列
function P(N, R)
{

    if (R > N || R <= 0 || N <= 0)
    {
        alert("arguments wrong!");
        return -1;
    }
    var s = [];
    var iRlt = 1;
    var t;
    s.push(N);
    while ((t = s[s.length - 1]) != N - R)
    {
        try
        {
            iRlt *= t;
        }
        catch (e)
        {
            alert("overflow!");
            return -1;
        }
        s.pop()
        s.push(t - 1);
    }
    return iRlt;
}

//获取双位整数
function MakeDoubleNumber(obj)
{
    if (obj < 10)
    {
        return "0" + obj;
    }
    else { return obj; }
}

//插入到list
function AddList(voteline, voteprice)
{
    if (ValidateVoteString(voteline))
    {
        var list = document.getElementById("listRecords");

        if (list.options.length > 14)
        {
            alert("每个方案的投注项个数不能超过15个");
            return;
        }
        var varItem = new Option(voteline, voteprice);

        list.options.add(varItem);

        ReflashRecords();
    }
}

//删除list
function RemoveList()
{
    var list = document.getElementById('listRecords');
    var i;
    for (i = list.length - 1; i >= 0; i--)
    {
        if (list.options[i].selected)
        { list.remove(i); }
    }

    ReflashRecords();
}


function CleanList()
{
    var list = document.getElementById('listRecords');
    var i;
    for (i = list.length - 1; i >= 0; i--)
    {
        list.remove(i);
    }

    ReflashRecords();
}

//返回list所有项
function GetAllListItem()
{
    var list = document.getElementById('listRecords');

    var strItems = "";

    for (var i = 0; i < list.options.length; i++)
    {
        strItems += list.options[i].text + ";";
    }
    strItems = strItems.replace(/大/g, "D");
    strItems = strItems.replace(/小/g, "X");
    strItems = strItems.replace(/单/g, "O");
    strItems = strItems.replace(/双/g, "E");
    return strItems.substring(0, strItems.length - 1);
}
//返回list所有项
function GetAllListValue()
{
    var list = document.getElementById('listRecords');

    var strItems = 0;

    for (var i = 0; i < list.options.length; i++)
    {
        strItems += parseInt(list.options[i].value);
    }
    
    return strItems;
}

function ReflashRecords()
{
    var inputMultiple = document.getElementById("InputMultiple").value;

    var recordscount = GetAllListValue();

    document.getElementById("SpanTotalRecordsMoney").innerText = inputMultiple * recordscount;
}

//打开合买代购方案页
function OpenProjectWindow(lotID, game, ptype, isInPlaceHolder)
{
    var list = document.getElementById('listRecords');

    if (list.options.length == 0)
    {
        alert("请将投注项添加到列表");
        return;
    }

    if (isInPlaceHolder == true)
    {
        var eventID = document.getElementById("ctl00_ContentPlaceHolderMasterLottery_SpanCurrentEventID").innerText;
        var eventName = document.getElementById("ctl00_ContentPlaceHolderMasterLottery_SpanCurrentEventName").innerText;
        var records = GetAllListItem();
        var multiple = document.getElementById("InputMultiple").value;
    }
    else
    {
        var eventID = document.getElementById("SpanCurrentEventID").innerText;
        var eventName = document.getElementById("SpanCurrentEventName").innerText;
        var records = GetAllListItem();
        var multiple = document.getElementById("InputMultiple").value;
    }

    window.open("/Lottery/Project.aspx?eid=" + eventID + "&ename=" + eventName + "&game=" + game + "&mul=" + multiple + "&lot=" + lotID + "&ptype=" + ptype + "&records=" + records, "", "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no,titlebar=no")
    CleanList();
}

//合买份数
function GetProjectUnitPrice()
{
    var projectPrice = document.getElementById("LabelProjectPrice").innerText;
    var unitPriceControl = document.getElementById("SpanUnitPrice");

    //拆分份数
    var projectParts = document.getElementById("InputProjectParts");

    if (isNaN(projectParts.value) == true)
    {
        alert("拆分的份数必须为整数");
        projectParts.value = projectPrice;
        unitPriceControl.innerText = "1";
        return false;
    }
    if (projectParts.value < 1)
    {
        alert("拆分的份数不能小于1");
        projectParts.value = projectPrice;
        unitPriceControl.innerText = "1";
        return false;
    }


    var unitPrice = projectPrice / projectParts.value;

    if (IsInt(unitPrice) == false)
    {
        alert("单价必须为整数");
        projectParts.value = projectPrice;
        unitPriceControl.innerText = "1";
        return false;
    }

    unitPriceControl.innerText = unitPrice;
}

//保底份数
function GetProjectReservePrive()
{
    var projectParts = document.getElementById("InputProjectParts").value;
    var unitParts = document.getElementById("SpanUnitPrice").innerText;
    var reserveParts = document.getElementById("InputReserveParts");
    var reservePrice = document.getElementById("SpanReservePrice");

    if (IsInt(reserveParts.value) == false)
    {
        alert("保底份数必须为整数");
        reserveParts.value = "0";
        reservePrice.innerText = "0";
        return false;
    }
    if (isNaN(reserveParts.value) == true)
    {
        alert("保底的份数必须为整数");
        reserveParts.value = "0";
        reservePrice.innerText = "0";
        return false;
    }
    if (reserveParts.value < 0)
    {
        alert("保底的份数必须大于等于0");
        reserveParts.value = "0";
        reservePrice.innerText = "0";
        return false;
    }


    if (parseInt(reserveParts.value) > parseInt(projectParts))
    {
        alert("保底份数不能超过方案总份数");
        reserveParts.value = "0";
        reservePrice.innerText = "0";
        return false;
    }

    reservePrice.innerText = unitParts * reserveParts.value;

}

//自购份数
function GetProjectBuyPrice()
{
    var projectParts = document.getElementById("InputProjectParts").value;
    var unitParts = document.getElementById("SpanUnitPrice").innerText;
    var buyParts = document.getElementById("InputBuyParts");
    var buyPrice = document.getElementById("SpanBuyPrice");

    if (IsInt(buyParts.value) == false)
    {
        alert("自购份数必须为整数");
        buyParts.value = "0";
        buyPrice.innerText = "0";
        return false;
    }
    if (isNaN(buyParts.value) == true)
    {
        alert("自购份数必须为整数");
        buyParts.value = "0";
        buyPrice.innerText = "0";
        return false;
    }
    if (buyParts.value < 0)
    {
        alert("自购份数必须大于等于0");
        buyParts.value = "0";
        buyPrice.innerText = "0";
        return false;
    }


    if (parseInt(buyParts.value) > parseInt(projectParts))
    {
        alert("保底份数不能超过方案总份数");
        buyParts.value = "0";
        buyPrice.innerText = "0";
        return false;
    }

    buyPrice.innerText = unitParts * buyParts.value;
}

//自购份数
function GetProjectJoinPrice()
{
    var unitParts = document.getElementById("LabelJoinProject_UnitPrice").innerText;
    var buyParts = document.getElementById("InputJoinProject_BuyParts").value;

    var buyPrice = document.getElementById("SpanJoinProjectBuyPrice");

    buyPrice.innerText = unitParts * buyParts;
}

function ReflashTotalMoney()
{
    var inputMultiple = document.getElementById("InputMultiple").value;

    var recordscount = GetAllListValue();

    document.getElementById("SpanTotalRecordsMoney").innerText = inputMultiple * recordscount;
}
