﻿function UpdateCurrencyType(Target, Source) {
    var SelectOption = document.getElementById(Source);
    var SelectedText = SelectOption.options[SelectOption.selectedIndex].text;
    var LabelDropdownList = document.getElementById(Target);
    if (SelectedText == 'Select') {
        LabelDropdownList.innerHTML = '';
    }
    else {
        LabelDropdownList.innerHTML = SelectedText;
    }
}
function UpdateMaxLudos(Target, Source, P, L, Total, RemLC, OV) {
    var MySource = document.getElementById(Source);
    MySource.style.background = 'White';
    if (isNaN(MySource.value)) {
        var MyVal = 0;
    }
    else {
        var MyVal = parseFloat(MySource.value);
    }
    MyVal = MyVal.toFixed(2);
    var Perc = P;
    var LudoCoins = L;
    var OverHead = OV;
    //var MaxLudos = Math.floor(((100 / (100 - Perc)) * MyVal) - MyVal);

    //var MyVal2 = (MyVal - ((MyVal / 100) * OverHead));
    //var MaxLudos = Math.floor(((100 / (100 - Perc)) * MyVal2) - MyVal2);

    var MaxLudos = Math.floor(0.15 * MyVal);
    
    if (LudoCoins < MaxLudos) {
        MaxLudos = LudoCoins;
    }
    var MyTarget = document.getElementById(Target);
    MyTarget.value = MaxLudos;
    var TotalLbl = document.getElementById(Total);
    var TotalLblValue = parseFloat(MyVal) + parseFloat(MaxLudos);
    TotalLblValue = TotalLblValue.toFixed(2);
    TotalLbl.innerHTML = TotalLblValue;
    var RemLudos = parseInt(LudoCoins) - parseInt(MaxLudos);
    var RemLudosLbl = document.getElementById(RemLC);
    RemLudosLbl.innerHTML = RemLudos;
}
function UpdateTotalAndMaxLudos(CashAmt, P, L, LudosAmt, Total, RemLC, OV) {
    var MyCashAmt = document.getElementById(CashAmt);
    MyCashAmt.style.background = 'White';
    if (isNaN(MyCashAmt.value)) {
        var MyVal = 0;
    }
    else {
        var MyVal = parseFloat(MyCashAmt.value);
    }
    MyVal = MyVal.toFixed(2);
    var Perc = P;
    var LudoCoins = L;
    var OverHead = OV;
    //var MaxLudos = Math.floor(((100 / (100 - Perc)) * MyVal) - MyVal);

    //var MyVal2 = (MyVal - ((MyVal / 100) * OverHead));
    //var MaxLudos = Math.floor(((100 / (100 - Perc)) * MyVal2) - MyVal2);

    var MaxLudos = Math.floor(0.15 * MyVal);
    
    if (LudoCoins < MaxLudos) {
        MaxLudos = LudoCoins;
    }
    var LudoCoinsTB = document.getElementById(LudosAmt);
    if (isNaN(LudoCoinsTB.value)) {
        var LudoCoinsAmount = 0;
    }
    else {
        var LudoCoinsAmount = parseFloat(LudoCoinsTB.value);
    }
    if (MaxLudos < LudoCoinsAmount) {
        LudoCoinsAmount = MaxLudos;
    }
    LudoCoinsTB.value = LudoCoinsAmount;
    var TotalLbl = document.getElementById(Total);
    var TotalLblValue = parseFloat(MyVal) + parseFloat(LudoCoinsAmount);
    TotalLblValue = TotalLblValue.toFixed(2);
    TotalLbl.innerHTML = TotalLblValue;

    var RemLudos = parseInt(LudoCoins) - parseInt(LudoCoinsAmount);
    var RemLudosLbl = document.getElementById(RemLC);
    RemLudosLbl.innerHTML = RemLudos;
}
function LudexSubmit(MemberID, CasinoID, CurrencyTypeID, DepositAmtID, LCAmtID, CasinoAccNoID, MPEID, TID, LudexUserNameID, SiteID) {
    var CurrencyTypeLbl = document.getElementById(CurrencyTypeID);
    var CurrencyType = CurrencyTypeLbl.innerHTML;

    var CasinoAccNoTB = document.getElementById(CasinoAccNoID);
    if (validate_required(CasinoAccNoTB, 'Please enter your Casino Account number') == false) {
		CasinoAccNoTB.style.background = 'Yellow';
		CasinoAccNoTB.focus();
		return false;
	}
    else {
		var CasinoAccNo = CasinoAccNoTB.value;
		var DepositAmtTB = document.getElementById(DepositAmtID);
		if (validate_required(DepositAmtTB, 'Please enter a deposit amount') == false) {
			DepositAmtTB.style.background = 'Yellow';
			DepositAmtTB.focus();
			return false;
		}
		else {
			if (validate_number(DepositAmtTB, 'Please enter a numeric deposit amount') == false) {
				DepositAmtTB.style.background = 'Yellow';
				DepositAmtTB.focus();
				return false;
			}
			else {
				var DepositAmt = parseFloat(DepositAmtTB.value);
				var LCAmtTB = document.getElementById(LCAmtID);
				if (validate_required(LCAmtTB, 'Please enter a LudoCoin amount') == false) {
					LCAmtTB.style.background = 'Yellow';
					LCAmtTB.focus();
					return false;
				}
				else {
					if (validate_number(LCAmtTB, 'Please enter a numeric LudoCoin amount') == false) {
						LCAmtTB.style.background = 'Yellow';
						LCAmtTB.focus();
						return false;
					}
					else {
						var LCAmt = parseInt(LCAmtTB.value);
						var LudexUserNameTB = document.getElementById(LudexUserNameID);
						if (validate_required(LudexUserNameTB, 'Please enter your LudoCoins Username') == false) {
							LudexUserNameTB.style.background = 'Yellow';
							LudexUserNameTB.focus();
							return false;
						}
						else {
							var LudexUserName = LudexUserNameTB.value;
							WebService.SubmitLudexTransaction(SiteID, TID, LudexUserName, MemberID, CasinoID, CurrencyType, DepositAmt, LCAmt, CasinoAccNo, LudexResult);
							$find(MPEID).hide();
							return false;
						}
                    }
                }
            }
        }
    }
}
function validate_required(field, alerttxt) {
    with (field) {
        if (value == null || value == '' || value.length == 0) {
            alert(alerttxt);
            return false;
        }
        else {
            return true;
        }
    }
}
function validate_number(field, alerttxt) {
    with (field) {
        if (isNaN(value)) {
            alert(alerttxt);
            return false;
        }
        else {
            return true;
        }
    }
}

function WatchSubmit(MemberID, AuctionID, BtnID) {
	WebService.SubmitAuctionWatch(MemberID, AuctionID);
	var WatchBtn = document.getElementById(BtnID);
	var CurrentImage = WatchBtn.src;
	if (CurrentImage.indexOf('WatchRem') == -1) {
		WatchBtn.src = "../Include/Images/WatchRem.jpg";
	}
	else {
		WatchBtn.src = "../Include/Images/Watch.jpg";
	}
}
function StartCountDown(myDiv, myTargetDate, NowDate) {
	var dthen = new Date(myTargetDate);
	var dnow = new Date(NowDate);
	ddiff = new Date(dthen - dnow);
	gsecs = Math.floor(ddiff.valueOf() / 1000);
	CountBack(myDiv, gsecs);
}

function Calcage(secs, num1, num2) {
	s = ((Math.floor(secs / num1)) % num2).toString();
	if (s.length < 2) {
		s = "0" + s;
	}
	return (s);
}

function Calcage2(secs, num1, num2) {
	s = ((Math.floor(secs / num1)) % num2).toString();
	return (s);
}

function CountBack(myDiv, secs) {
	var DisplayStr;
	var DisplayFormat = "%%D%% Days<br />%%H%% Hours<br />%%M%% Minutes<br />%%S%% Seconds";
	DisplayStr = DisplayFormat.replace(/%%D%%/g, Calcage2(secs, 86400, 100000));
	DisplayStr = DisplayStr.replace(/%%H%%/g, Calcage(secs, 3600, 24));
	DisplayStr = DisplayStr.replace(/%%M%%/g, Calcage(secs, 60, 60));
	DisplayStr = DisplayStr.replace(/%%S%%/g, Calcage(secs, 1, 60));
	if (secs > 0) {
		document.getElementById(myDiv).innerHTML = DisplayStr;
		setTimeout("CountBack('" + myDiv + "'," + (secs - 1) + ");", 990);
	}
	else {
		document.getElementById(myDiv).innerHTML = "Closed";
	}
}
function UpdateMaxBid(TB, LC, HB) {
	var MySource = document.getElementById(TB);
	if (MySource.value == "") {
		MySource.value = "";
	}
	else {
		if (isNaN(MySource.value)) {
			var MyBid = 0;
			MySource.value = "";
		}
		else {
			var MyBid = parseInt(MySource.value);
		}
		var LudoCoins = LC;
		var HighBid = HB;

		if (MyBid < HighBid) {
			MySource.value = "";
			alert("Please bid a higher amount than the current highest bid.");
		}
		if (LudoCoins < MyBid) {
			MySource.value = "";
			alert("You have insufficient LudoCoins to bid this amount.");
		}
	}
}

function BidSubmit(MemberID, AuctionID, TB, HighBid, LudoCoins) {
	var BidAmtTB = document.getElementById(TB);
	if (isNaN(BidAmtTB.value)) {
		var BidAmt = 0;
	}
	else {
		var BidAmt = parseInt(BidAmtTB.value);
	}
	var err = 0;
	if (BidAmt < HighBid) {
		err = 1;
	}
	if (BidAmt > LudoCoins) {
		err = 2;
	}

	if (err == 0) {
		BidAmtTB.value = "";
		WebService.SubmitAuctionBid(MemberID, AuctionID, BidAmt, BidResult);
	}
	else if (err == 1) {
		BidAmtTB.value = "";
		alert("Please bid a higher amount than the current highest bid.");
		return false;
	}
	else {
		BidAmtTB.value = "";
		alert("You have insufficient LudoCoins to bid this amount.");
		return false;
	}
}

function BuySubmit(MemberID, RaffleID, TicketsLB, LudoCoins, TicketPrice, MaxTickets, NumTickets) {
	var sel = document.getElementById(TicketsLB).options;
	var optsLength = sel.length;
	var Tickets = "";
	var CountTickets = 0;
	for (var i = 0; i < optsLength; i++) {
		if (sel[i].selected) {
			Tickets = Tickets + sel[i].value + ", ";
			CountTickets = CountTickets + 1;
		}
	}
	var TotalPrice = CountTickets * TicketPrice;
	if (MaxTickets > 0) {
		if (NumTickets < MaxTickets) {
			if (CountTickets == 0) {
				alert("Please select one or more tickets to buy.");
				return false;
			}
			else if (TotalPrice > LudoCoins) {
				alert("You do not have enough LudoCoins to buy " + CountTickets + " tickets, please select less tickets to buy.");
				return false;
			}
			else if ((CountTickets + NumTickets) > MaxTickets) {
				var TicketsAllowed = MaxTickets - NumTickets;
				alert("You may only buy up to " + TicketsAllowed + " tickets, please select less tickets to buy.");
				return false;
			}
			else {
				//alert("You have selected " + CountTickets + " to buy.");
				WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, BuyResult);
				return false;
			}
		}
		else {
			alert("You may only buy up to " + MaxTickets + " tickets.");
			return false;
		}
	}
	else {
		if (CountTickets == 0) {
			alert("Please select one or more tickets to buy.");
			return false;
		}
		else if (TotalPrice > LudoCoins) {
			alert("You do not have enough LudoCoins to buy " + CountTickets + " tickets, please select less tickets to buy.");
			return false;
		}
		else {
			//alert("You have selected " + CountTickets + " to buy.");
			WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, BuyResult);
			return false;
		}
	}
}
function ClaimSubmit(MemberID, RaffleID, BtnID) {
	WebService.SubmitRaffleClaim(MemberID, RaffleID);
	var ClaimBtn = document.getElementById(BtnID);
	var CurrentImage = ClaimBtn.src;
	ClaimBtn.src = "../Include/Images/Claimed.jpg";
}
function BuyGiftSubmit(MemberID, RaffleID, NumTicketsTB, LudoCoins, TicketPrice, FirstNameTB, LastNameTB, FriendFNameTB, FriendLNameTB, FriendEmailTB, MPEID) {
    var FName = document.getElementById(FirstNameTB);
    if (validate_required(FName, 'Please enter your First Name') == false) {
        FName.style.background = 'Yellow';
        FName.focus();
        return false;
    }
    else {
        var FirstName = FName.value;

        var LName = document.getElementById(LastNameTB);
        if (validate_required(LName, 'Please enter your Last Name') == false) {
            LName.style.background = 'Yellow';
            LName.focus();
            return false;
        }
        else {
            var LastName = LName.value;

            var FFName = document.getElementById(FriendFNameTB);
            if (validate_required(FFName, 'Please enter your Friends First Name') == false) {
                FFName.style.background = 'Yellow';
                FFName.focus();
                return false;
            }
            else {
                var FriendFirstName = FFName.value;


                var FLName = document.getElementById(FriendLNameTB);
                if (validate_required(FLName, 'Please enter your Friends Last Name') == false) {
                    FLName.style.background = 'Yellow';
                    FLName.focus();
                    return false;
                }
                else {
                    var FriendLastName = FLName.value;
                

                    var FE = document.getElementById(FriendEmailTB);
                    if (validate_required(FE, 'Please enter your Friends Email address') == false) {
                        FE.style.background = 'Yellow';
                        FE.focus();
                        return false;
                    }
                    else {
                        var FriendEmail = FE.value;

                        var NumTickets = document.getElementById(NumTicketsTB);
                        if (validate_required(NumTickets, 'Please enter the number of tickets') == false) {
                            NumTickets.style.background = 'Yellow';
                            NumTickets.focus();
                            return false;
                        }
                        else {
                            if (validate_number(NumTickets, 'Please enter a numeric amount of tickets') == false) {
                                NumTickets.style.background = 'Yellow';
                                NumTickets.focus();
                                return false;
                            }
                            else {
                                var NumTick = parseInt(NumTickets.value);

                                var TotalPrice = NumTick * TicketPrice;

                                if (TotalPrice > LudoCoins) {
                                    alert("You do not have enough LudoCoins to buy " + CountTickets + " tickets, please select less tickets to buy.");
                                    return false;
                                }
                                else {
                                    WebService.SubmitRaffleGift(MemberID, RaffleID, NumTick, TicketPrice, FriendFirstName, FriendLastName, FriendEmail, FirstName, LastName, BuyResult);
                                    $find(MPEID).hide();
                                    return false;
                                }
                            }
                        }
                    }
                }
            }

        }
    }
}