﻿window.onload=fillInRows;
function fillInRows()
{
	hasLoaded = true;
}

function deleteCurrentRow(obj, sIddaaId)
{
	if (hasLoaded) 
	{
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		var subObj = document.getElementById("hidden" + sIddaaId);
		deleteRows(rowArray);
		delRow = subObj.parentNode.parentNode;
		rIndex = delRow.sectionRowIndex;
		rowArray = new Array(delRow);
		deleteRows(rowArray);
		macId = window.frames["Matches"].IddaaMatchIds[sIddaaId];
		controlArray[macId] = undefined;
		//controlArray[sIddaaId] = undefined;
		selTeamArray[sIddaaId] = undefined;
		gameTypeArray[sIddaaId] = undefined;
		delete couponHashmap[sIddaaId];
		recalculateBet();
		matchNum--;
		initValidation(0);
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}
function cc()
{
	if (addedRowCount>1)
	{
		controlArray = new Array();
		selTeamArray = new Array();
		gameTypeArray = new Array();
		couponHashmap = new Object();
		systemHashmap = new Object();
		
		addedRowCount = 1;
		totalBet = 1;
		matchNum = 0;
		maxBet = 0;

		document.getElementById("couponNum").value = "";
		document.getElementById("couponValue").value = "";
		document.getElementById("maximumGain").value = "";
		document.getElementById("totalBet").innerHTML = "";
		document.getElementById("matchNum").innerHTML = "";
		
		var systemc;
		for (var i=1; i<16; i++)
		{
			systemc = document.getElementById('system'+i);
			systemc.checked=false;
		}
		
		var divc = document.getElementById('divCoupon');
		divc.innerHTML = "<table width=226 border=0 cellpadding=0 cellspacing=0 id=tblCoupon valign=top class=rowData><tr><td colspan=4 align=center valign=top></td></tr></table>";
	}
}

function addRowsToTable(ks,si)
{

	var rows;
	var columns;
	rows = ks.split("~"); 
	
	for (var i=0; i<rows.length; i++)
	{
		columns = rows[i].split("*");
		
		if (columns[0] != "" && columns[0] != undefined && columns[4]) 
		{
			addRowToTable(columns[0], columns[5], columns[6], columns[2], columns[4], columns[3], columns[7],columns[1]);
		}
	}
	var systemObj;
	systemObj = document.getElementById("system"+si);
	if (si != "" && si != 0)
	{
		systemObj.checked = true;
		systemHashmap[si] = si;
		initValidation(0);
	}
	else
		si = "";
		
}

function addRowToTable(iddaaId, takim1, takim2, oran, selTeam, gameType, mbs) {
    macId = window.frames["Matches"].IddaaMatchIds[iddaaId];
	//if (hasLoaded)
    //{
    if (rowControl(iddaaId, oran, selTeam, gameType, macId))
        //if (rowControl(iddaaId, oran, selTeam, gameType))
		{
		    totalBet = totalBet * oran;
			var tbl = document.getElementById(TABLE_NAME);
			var nextRow = tbl.rows.length;
			var iteration = nextRow + ROW_BASE;
			var num = nextRow;
			var banko = 0;
			var row = tbl.insertRow(num);
			if (arguments.length == 8) 
				banko = arguments[7];
			if (mbs=="")
				mbs = "4";
			
			addedRowCount++;
			row.setAttribute("id","row_" + nextRow)
			row.setAttribute("height","25")
			row.setAttribute("align", "left");
			
			var cellLeft = row.insertCell(0);
			var textNode = document.createTextNode("");
			cellLeft.appendChild(textNode);
			
			var cellRight1 = row.insertCell(1);
			var e2 = document.createElement("input");
			
			e2.setAttribute("type", "checkbox");
			e2.setAttribute("name", "txtRow2" + iddaaId);
			e2.setAttribute("id", "txtRow3" + iddaaId);
			
			e2.onclick = function () {setBanko(iddaaId, this)};
			cellRight1.appendChild(e2);
			
			var cellRightID = row.insertCell(2);
			cellRightID.innerHTML = iddaaId + " "
			
			var cellRightMatch = row.insertCell(3);
			cellRightMatch.innerHTML = takim1 + " - " + takim2;
			
			var cellRight1 = row.insertCell(4);
			var e2 = document.createElement("input");
			e2.setAttribute("type", "image");
			e2.setAttribute("name", "txtRow1" + iddaaId);
			e2.setAttribute("id", "txtRow1" + iddaaId);
			e2.setAttribute("src", MK_IMG + "/sil-2.gif");
			e2.onclick = function () {deleteCurrentRow(this, iddaaId)};
			cellRight1.appendChild(e2);
			
			nextRow = tbl.rows.length;
			iteration = nextRow + ROW_BASE;
			num = nextRow;
			
			var row1 = tbl.insertRow(num);
			addedRowCount++;
			row1.setAttribute("id", "row_" + nextRow)
			
			
			var cell1 = row1.insertCell(0);
			var h1 = document.createElement("input");
			h1.setAttribute("type", "hidden");
			h1.setAttribute("name", "hidden" + iddaaId);
			h1.setAttribute("id", "hidden" + iddaaId);
			cell1.appendChild(h1);
	
			var cell2 = row1.insertCell(1);
			cell2.innerHTML = " ";
		  
			var cell3 = row1.insertCell(2);
			var gameTypeImgName = "";
			if (gameType=='UO') gameTypeImgName = "AU" 
			else gameTypeImgName = gameType;
			cell3.innerHTML = "<img src=" + MK_IMG + "/" + gameTypeImgName + ".gif>";
		
			var cell4 = row1.insertCell(3);
			cell4.setAttribute("id", "changeTeam" + iddaaId);
			cell4.innerHTML = "<span id=selTeam"+iddaaId+">" + selTeam + "</span> - <span id=oran"+iddaaId+">" + oran + "</span>" ;
		
			var cell5 = row1.insertCell(4);
			cell5.innerHTML = "";

			//controlArray[iddaaId] = selTeam;
			controlArray[macId] = selTeam;
			selTeamArray[iddaaId] = selTeam;
			gameTypeArray[iddaaId] = gameType;
			couponHashmap[iddaaId] = new CouponRow(iddaaId, oran, gameType, selTeam, banko, mbs);
			couponHashmap[iddaaId].matchStr = takim1 + "-" + takim2;
			var totalBetObj = document.getElementById("totalBet");
			totalBetObj.innerHTML = formatValue(totalBet,2)
			matchNum++;
			initValidation(0);
		}
	//}
}

function rowControl(iddaaId, oran, selTeam, gameType, macId)
{

    //if (controlArray[iddaaId]==undefined) controlArray[iddaaId] = -1;
    if (controlArray[macId] == undefined) controlArray[macId] = -1;
    
	if (gameTypeArray[iddaaId] == undefined) gameTypeArray[iddaaId] = -1;
	if (gameTypeArray[iddaaId] == -1 || (gameTypeArray[iddaaId] == "MS" && gameType == "MS"))
	{
		//if (selTeamArray[iddaaId] != selTeam && controlArray[iddaaId] != -1 && controlArray[iddaaId] != 3)
	    if (selTeamArray[iddaaId] != selTeam && controlArray[macId] != -1 && controlArray[macId] != 3)
		{
			var k=0;
			var changeTeamRow = document.getElementById("selTeam" + iddaaId);
			changeTeamRow.innerHTML = changeTeamRow.innerHTML + "/" + selTeam
						
			var changeBetRow = document.getElementById("oran" + iddaaId);
			changeBetRow.innerHTML = changeBetRow.innerHTML + "/" + oran
			couponHashmap[iddaaId].selTeam1 = selTeam
			couponHashmap[iddaaId].bet1 = oran
			//controlArray[iddaaId] = 3
			controlArray[macId] = 3
			selTeamArray[iddaaId] = selTeam
			initValidation(0);
			return false;
		}
		//else if (controlArray[iddaaId] == -1)
		else if (controlArray[macId] == -1) 
			return true;
		else
			return false;
	}
	else
		return false;
	return true;
}

function recalculateBet()
{
	var retVal = 1;
	for (e in couponHashmap)
	{
			retVal	= retVal * couponHashmap[e].bet
	}  
	totalBet = retVal
	var totalBetObj = document.getElementById("totalBet");
	totalBetObj.innerHTML = formatValue(totalBet,2)
}

function formatValue(decimalValue, decimalNum)
{
	var retVal;
	retVal = Math.round(decimalValue * Math.pow(10,decimalNum)) / Math.pow(10,decimalNum);
	retVal = formatCurrency(retVal);
	return retVal
}

function setBanko(bIddaaId, chObj)
{
			if(chObj.checked)
				couponHashmap[bIddaaId].banko = 1;
			else
				couponHashmap[bIddaaId].banko = 0;	
				
			initValidation(0)	
}

function setSystem(systemObj)
{
			if(systemObj.checked)
			{
				systemHashmap[systemObj.value] = systemObj.value;
			}	
			else
			{
				delete systemHashmap[systemObj.value];
			}
				
			initValidation(0)	
}

function initValidation(isAlert)
{
	var count=0;
	var bCount=0;
	var MStr = new Array();
	var MBSStr =  new Array();
	var ORStr =  new Array();
	var ORStr1 =  new Array();
	var BStr =  new Array();
	var SStr =  new Array();
	var retVal = "";
	var validCouponNum = 0;
	var validTotalBet = 0;
	var validMaxBet = 0;
	var validationArray = new Array();
	var doubleMatchColumnNum = 1;
	var doubleMatchNum = 0;
	var bankoDoubleMatchNum = 0;
	
	for (e in couponHashmap) {
        MStr[count] = couponHashmap[e].id;
		MBSStr[count] = couponHashmap[e].mbs;
		ORStr[count] = couponHashmap[e].bet;
		ORStr1[count] = couponHashmap[e].bet1;
		
		if (couponHashmap[e].banko != 0) {
            BStr[bCount] = couponHashmap[e].id;
			bCount++;
        }
		if ( couponHashmap[e].selTeam1 != undefined) {
            doubleMatchNum ++;
			doubleMatchColumnNum = doubleMatchColumnNum * 2;
        }
        if (couponHashmap[e].banko != 0 && couponHashmap[e].selTeam1 != undefined) {
		    bankoDoubleMatchNum ++;
        }
		count++;
	}
	
	for (i=count; i<15; i++) {
		MStr[i] = "0";
		MBSStr[i] = "0";
		ORStr[i] = "0";
		ORStr1[i] = "0";
		BStr[i]	= "0";
	}
	
	for (i=bCount; i<15; i++) {
        BStr[i]	= "0";
	}
	count = 0
	
	for (e in systemHashmap) {
        SStr[count] = systemHashmap[e]
		count++;
	}
	
	for (i=count; i<15; i++) {
        SStr[i] = "0";	
	}
	
	//alert(MStr + "," + MBSStr + "," + ORStr + "," + BStr + "," + SStr + "," + doubleMatchColumnNum + "," + doubleMatchNum + "," + bankoDoubleMatchNum + "," + bCount + "," + ORStr1);

	retVal = ValidateCoupon(MStr, MBSStr, ORStr, BStr, SStr, doubleMatchColumnNum, doubleMatchNum, bankoDoubleMatchNum, bCount, ORStr1);

	var iMisli = 1;
	var misli = document.getElementById("multiplier");

	if (misli) {
	    iMisli = parseInt(misli.value, 10);
	}

	if (retVal=="NOK"){
	    document.getElementById("couponNum").value = "1"
	    document.getElementById("couponValue").value = iMisli;
	    document.getElementById("maximumGain").value = formatValue(totalBet * iMisli, 2);
	} else {
		validationArray = retVal.split("-");
		document.getElementById("couponNum").value = validationArray[1];
		document.getElementById("couponValue").value = (parseInt(validationArray[1],10) * iMisli);
		document.getElementById("maximumGain").value = formatValue(parseFloat(validationArray[2]) * iMisli, 2);
		document.getElementById("totalBet").innerHTML = formatValue(validationArray[3],2);
	}	
	var matchNumObj
	document.getElementById("matchNum").innerHTML = matchNum;
	return retVal;
}

function initUrlConnection() {
	document.getElementById("createCoupon").disabled = true;
	
	var validationStr = "";
	var validationArray = "";
	var usr = "";
	var weekId = "";
	var systemStr="";	
	var URLStr="";
	var prmStr="";
	var couponNum = 0;
	var tmpSysStr = "";
	
	for (e in couponHashmap) {
        prmStr = prmStr + couponHashmap[e].id + "|" + couponHashmap[e].gameType + "|" + couponHashmap[e].selTeam + "|" + couponHashmap[e].bet + "|";
		prmStr = prmStr + couponHashmap[e].selTeam1 + "|" + couponHashmap[e].bet1 + "|" + couponHashmap[e].mbs + "|" + couponHashmap[e].banko + "$";
	}

	for (e in systemHashmap) {
		if (systemHashmap[e].length == 1)
			tmpSysStr = "0" + systemHashmap[e];
		else
			tmpSysStr = systemHashmap[e];
		systemStr = systemStr + tmpSysStr + "-";
	}
	
	systemStr = systemStr.substr(0,systemStr.length-1,1);	
	var usrObj,weekObj;
	usrObj = document.getElementById("userName");
	usr = usrObj.value;
	weekObj = document.getElementById("weekId");
	weekId = weekObj.value;
	validationStr = initValidation(1);
	if (validationStr != "NOK") {
		validationArray = validationStr.split("-");
		URLStr = ICP + "?usr=" + usr;
		URLStr = URLStr + "&weekId=" + weekId;
		URLStr = URLStr + "&couponNum=" + validationArray[1];
		URLStr = URLStr + "&totalBet=" + formatValue(validationArray[2],2);
		URLStr = URLStr + "&maxBet=" + formatValue(validationArray[2],2);
		URLStr = URLStr + "&systemStr=" + systemStr;
		URLStr = URLStr + "&prmStr=" + escape(prmStr).replace(/\+/g, "%2B");
		maxbet = formatValue(validationArray[2],2);
		document.getElementById("url").value = URLStr;
		getUrl(URLStr);
		return false;
	}
	else
	{
		alert("Kuponunuz Hatalı");
		document.getElementById("createCoupon").disabled = false;
		return false;
	}
}

function convertGametype(myGameType)
{
    var retVal = "";

    switch (myGameType) {
        case "MS":
            retVal = "F";
            break;
        case "H":
            retVal = "H";
            break;
        case "CS":
            retVal = "DC";
            break;
        case "IM":
            retVal = "SF";
            break;
        case "SK":
            retVal = "SC";
            break;
        case "UO":
            retVal = "UO";
            break;
        case "IY":
            retVal = "S";
            break;
        case "TG":
            retVal = "GS";
            break;
    }		
	return retVal;		
}

function convertSelTeam(mySelTeam, gameType)
{	
	var retVal = "";
	if (mySelTeam == "1" || mySelTeam == "1-X")	
		retVal = "8";
	else if (mySelTeam == "X" || mySelTeam == "1-2" || (mySelTeam == "2" && gameType == "UO"))	
		retVal = "10";
	else if (mySelTeam == "2" || mySelTeam == "X-2")	
		retVal = "12";
	else if (mySelTeam == "1/1")	
		retVal = "1";		
	else if (mySelTeam == "X/1")	
		retVal = "2";		
	else if (mySelTeam == "2/1")	
		retVal = "3";		
	else if (mySelTeam == "1/X")	
		retVal = "4";		
	else if (mySelTeam == "X/X")	
		retVal = "5";						
	else if (mySelTeam == "2/X")	
		retVal = "6";		
	else if (mySelTeam == "1/2")	
		retVal = "7";		
	else if (mySelTeam == "X/2")	
		retVal = "8";					
	else if (mySelTeam == "2/2")	
		retVal = "9";			
	else if (mySelTeam == "1:0")	
		retVal = "1";	
	else if (mySelTeam == "0:0")	
		retVal = "2";
	else if (mySelTeam == "0:1")	
		retVal = "3";
	else if (mySelTeam == "2:0")	
		retVal = "4";
	else if (mySelTeam == "1:1")	
		retVal = "5";
	else if (mySelTeam == "0:2")	
		retVal = "6";
	else if (mySelTeam == "2:1")	
		retVal = "7";
	else if (mySelTeam == "2:2")	
		retVal = "8";
	else if (mySelTeam == "1:2")	
		retVal = "9";
	else if (mySelTeam == "3:0")	
		retVal = "10";
	else if (mySelTeam == "3:3")	
		retVal = "11";
	else if (mySelTeam == "0:3")	
		retVal = "12";
	else if (mySelTeam == "3:1")	
		retVal = "13";												
	else if (mySelTeam == "4+:4+")	
		retVal = "14";
	else if (mySelTeam == "1:3")	
		retVal = "15";
	else if (mySelTeam == "3:2")	
		retVal = "16";
	else if (mySelTeam == "2:3")	
		retVal = "17";
	else if (mySelTeam == "4+:0")	
		retVal = "18";
	else if (mySelTeam == "0:4+")	
		retVal = "19";
	else if (mySelTeam == "4+:1")	
		retVal = "20";							
	else if (mySelTeam == "1:4+")	
		retVal = "21";
	else if (mySelTeam == "4+:2")	
		retVal = "22";
	else if (mySelTeam == "2:4+")	
		retVal = "23";
	else if (mySelTeam == "4+:3")	
		retVal = "24";				
	else if (mySelTeam == "3:4+")	
		retVal = "25";

	return retVal;		
}


function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function replaceTRChar(replaceStr)
{
	var retVal = "";
	retVal = replaceStr.replace('İ','I');
	retVal = retVal.replace('Ğ','G');
	retVal = retVal.replace('Ö','O');
	retVal = retVal.replace('Ü','U');
	retVal = retVal.replace('Ş','S');
	retVal = retVal.replace('Ç','C');
	retVal = retVal.replace('ı','i');
	retVal = retVal.replace('ğ','g');
	retVal = retVal.replace('ö','o');
	retVal = retVal.replace('ş','s');
	retVal = retVal.replace('ç','c');
	retVal = retVal.replace('ü','u');
	return retVal;
}