﻿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.20 * 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.20 * 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, 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);
						WebService.SubmitLudexTransaction(SiteID, TID, 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, MB) {
	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;
		var MaxBid = MB;

		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.");
        }
        if (MyBid > 350) {
           MySource.value = "";
           alert("You may only bid upto 350 LudoCoins.");
        }
        if (MaxBid > 0) {
            if (MyBid > MaxBid) {
                MySource.value = MaxBid;
                alert("You may only bid upto " + MaxBid + " LudoCoins.");
            }
        }
	}
}

function BidSubmit(MemberID, AuctionID, TB, HighBid, LudoCoins, CInfo1TB, CInfo1FormatHF, CInfo1Error, CInfo2TB, CInfo2Error) {
	var BidAmtTB = document.getElementById(TB);
	var err = 0;
	if (BidAmtTB.value == "") {
	    err = 1;
	    BidAmtTB.value = "";
	    alert("Please enter a bid amount.");
	    return false;
	}
	
	if (isNaN(BidAmtTB.value)) {
	    var BidAmt = 0;
	}
	else {
	    var BidAmt = parseInt(BidAmtTB.value);
	}
	if (BidAmt < HighBid) {
	    err = 1;
	    BidAmtTB.value = "";
	    alert("Please bid a higher amount than the current highest bid.");
	    return false;
	}
	if (BidAmt > LudoCoins) {
	    err = 1;
	    BidAmtTB.value = "";
	    alert("You have insufficient LudoCoins to bid this amount.");
	    return false;
    }

    var CInfo1 = "";
    var CInfo2 = "";
    if (CInfo1Error != "") {
        var CInfo1Format = document.getElementById(CInfo1FormatHF).value;
        CInfo1 = document.getElementById(CInfo1TB).value;
        if (CInfo1 == "") {
            err = 1;
            alert(CInfo1Error + " (Enter Value)");
            return false;
        }
        else if (CInfo1Format != "") {
            //validate against format pattern
            //first check the length
            var newLength = CInfo1Format.length;
            if (newLength != CInfo1.length) {
                err = 1;
                alert(CInfo1Error + " (Not Valid)");
                return false;
            }
            //now check each character
            for (var x = 0; x < newLength; x++) {
                aChar = CInfo1Format.substring(x, x + 1);
                bChar = CInfo1.substring(x, x + 1);

                var letter = /^[a-zA-Z]+$/;
                //letter
                if (aChar == "@") {
                    if (bChar.match(letter)) {
                        //do nothing 
                    }
                    else {
                        err = 1;
                        alert(CInfo1Error + " (Not Valid)");
                        return false;
                    }
                }
                //number
                else if (aChar == "#") {
                    if (isNaN(bChar)) {
                        err = 1;
                        alert(CInfo1Error + " (Not Valid)");
                        return false;
                    }
                }
                else {
                    if (bChar != aChar) {
                        err = 1;
                        alert(CInfo1Error + " (Not Valid)");
                        return false;
                    }
                }
            }
            if (CInfo2Error != "") {
                var CInfo2 = document.getElementById(CInfo2TB).value;
                if (CInfo2 == "") {
                    err = 1;
                    alert("Please enter a value in the text box");
                    return false;
                }
            }
        }
    }
    else if (CInfo2Error != "") {
        CInfo2 = document.getElementById(CInfo2TB).value;
        if (CInfo2 == "") {
            err = 1;
            alert("Please enter a value in the text box");
            return false;
        }
    }

	if (err == 0) {
		BidAmtTB.value = "";
		WebService.SubmitAuctionBid(MemberID, AuctionID, BidAmt, CInfo1, CInfo2, BidResult);
	}
}

function BuySubmit(MemberID, RaffleID, TicketsLB, LudoCoins, TicketPrice, MaxTickets, NumTickets, CInfo1TB, CInfo1FormatHF, CInfo1Error, CInfo2TB, CInfo2Error) {
	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 CInfo1 = "";
	var CInfo2 = "";
	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;
		        if (TicketsAllowed == 1) {
		            alert("You can only buy an additional " + TicketsAllowed + " ticket. Please select fewer tickets to buy.");
		        }
		        else {
		            alert("You can only buy an additional " + TicketsAllowed + " tickets. Please select fewer tickets to buy.");
		        }
		        return false;
		    }
		    else if (CInfo1Error != "") {
		        var CInfo1Format = document.getElementById(CInfo1FormatHF).value;
		        CInfo1 = document.getElementById(CInfo1TB).value;

		        if (CInfo1 == "") {
		            alert(CInfo1Error + " (Enter Value)");
		            return false;
		        }
		        else if (CInfo1Format != "") {
		            //validate against format pattern
		            //first check the length
		            var newLength = CInfo1Format.length;
		            if (newLength != CInfo1.length) {
		                alert(CInfo1Error + " (Not Valid)");
		                return false;
		            }
		            //now check each character
		            for (var x = 0; x < newLength; x++) {
		                aChar = CInfo1Format.substring(x, x + 1);
		                bChar = CInfo1.substring(x, x + 1);

		                var letter = /^[a-zA-Z]+$/;
		                //letter
		                if (aChar == "@") {
		                    if (bChar.match(letter)) {
		                        //do nothing 
		                    }
		                    else {
		                        alert(CInfo1Error + " (Not Valid)");
		                        return false;
		                    }
		                }
		                //number
		                else if (aChar == "#") {
		                    if (isNaN(bChar)) {
		                        alert(CInfo1Error + " (Not Valid)");
		                        return false;
		                    }
		                }
		                else {
		                    if (bChar != aChar) {
		                        alert(CInfo1Error + " (Not Valid)");
		                        return false;
		                    }
		                }
		            }
		            if (CInfo2Error != "") {
		                var CInfo2 = document.getElementById(CInfo2TB).value;
		                if (CInfo2 == "") {
		                    alert(CInfo2Error + " (Enter Value)");
		                    return false;
		                }
		                else {
		                    WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
		                    return false;
		                }
		            }
		            WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
		            return false;
		        }
		    }
		    else if (CInfo2Error != "") {
		        CInfo2 = document.getElementById(CInfo2TB).value;
		        if (CInfo2 == "") {
		            alert(CInfo2Error + " (Enter Value)");
		            return false;
		        }
		        else {
		            WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
		            return false;
		        }
		    }
		    else {
		        //alert("You have selected " + CountTickets + " to buy.");
		        WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
		        return false;
		    }
		}
		else {
		    alert("You have already bought the maximum of " + MaxTickets + " tickets allowed for this Raffle.");
			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 if (CInfo1Error != "") {
	        var CInfo1Format = document.getElementById(CInfo1FormatHF).value;
	        CInfo1 = document.getElementById(CInfo1TB).value;

	        if (CInfo1 == "") {
	            alert(CInfo1Error + " (Enter Value)");
	            return false;
	        }
	        else if (CInfo1Format != "") {
	            //validate against format pattern
	            //first check the length
	            var newLength = CInfo1Format.length;
	            if (newLength != CInfo1.length) {
	                alert(CInfo1Error + " (Not Valid)");
	                return false;
	            }
	            //now check each character
	            for (var x = 0; x < newLength; x++) {
	                aChar = CInfo1Format.substring(x, x + 1);
	                bChar = CInfo1.substring(x, x + 1);

	                var letter = /^[a-zA-Z]+$/;
	                //letter
	                if (aChar == "@") {
	                    if (bChar.match(letter)) {
	                        //do nothing 
	                    }
	                    else {
	                        alert(CInfo1Error + " (Not Valid)");
	                        return false;
	                    }
	                }
	                //number
	                else if (aChar == "#") {
	                    if (isNaN(bChar)) {
	                        alert(CInfo1Error + " (Not Valid)");
	                        return false;
	                    }
	                }
	                else {
	                    if (bChar != aChar) {
	                        alert(CInfo1Error + " (Not Valid)");
	                        return false;
	                    }
	                }
	            }
	            if (CInfo2Error != "") {
	                var CInfo2 = document.getElementById(CInfo2TB).value;
	                if (CInfo2 == "") {
	                    alert(CInfo2Error + " (Enter Value)");
	                    return false;
	                }
	                else {
	                    WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
	                    return false;
	                }
	            }
	            WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
	            return false;
	        }
	    }
	    else if (CInfo2Error != "") {
	        CInfo2 = document.getElementById(CInfo2TB).value;
	        if (CInfo2 == "") {
	            alert(CInfo2Error + " (Enter Value)");
	            return false;
	        }
	        else {
	            WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, BuyResult);
	            return false;
	        }
	    }
	    else {
	        //alert("You have selected " + CountTickets + " to buy.");
	        WebService.SubmitRaffleBuy(MemberID, RaffleID, Tickets, TicketPrice, MaxTickets, CInfo1, CInfo2, 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, MaxTickets) {
    var NumTickets = document.getElementById(NumTicketsTB);
	if (MaxTickets > 0) {
	    if (NumTickets < MaxTickets) {

	        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;

	                            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;
	                                    }
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        }
	    }
	    else {
	        alert("In this Raffle, the maximum number of tickets you can send as a gift is " + MaxTickets + ".");
	        return false;
	    }
	}
}
