
function ShowPopUpWindow(strURL, bCentrePopup, bResizable, bScrolling, Height, Width)
{
	//create unique id so each time a popup is clicked a new window opens.
	var day = new Date();
	var id = day.getTime();
	var strWindowFormat='';
	
	if (bResizable)
	{
		strWindowFormat = strWindowFormat+"resizable=1";
	}
	else
	{
		strWindowFormat = strWindowFormat+"resizable=0";
	}
	//add extra default settings
	if (bCentrePopup)
	{
		if (Width=='')
		{
			Width=100;
		}
		if (Height=='')
		{
			Height=100;
		}
		var windowLeft = (screen.width - Width) / 2;
		var windowTop = (screen.height - Height) / 2;
		strWindowFormat = strWindowFormat+",left=" + windowLeft + ",top=" + windowTop;
	}
	if (bScrolling)
	{
		strWindowFormat = strWindowFormat+",scrollbars=1";
	}
	strWindowFormat = strWindowFormat+",width=" + Width + ",height=" + Height;
	//switch for status bar
	strWindowFormat = strWindowFormat+",status=0";
	//switch for help icon on the windows tool bar
	strWindowFormat = strWindowFormat+",help=0"
	//switch for address bar
	strWindowFormat = strWindowFormat+",location=0"
	//switch for menu bar
	strWindowFormat = strWindowFormat+",menubar=0"
	//alert(strWindowFormat);
	window.open(strURL, 'popUpWindow'+id, strWindowFormat);
}
function register_startup() {
	register_RefreshForCountrySelection();
}
function registration_checkform() 
{
    // TODO - Implement validation that can be extended into the client system.
	var obj = document.getElementById("addresscountrycode");
	var index = combo_getselectedindex(obj);
	var strcountry = obj[index].value;
	
	if (strcountry == "US" || strcountry == "VI" || strcountry == "UM" || strcountry == "AN" || strcountry == "TR")
	{
		alert (strAN);
		return false;
	}
	else
		return true;
}



function register_RefreshForCountrySelection() 
{
	var objcountry = document.getElementById("addresscountrycode");
	var objstate = document.getElementById("state");
	
	if (objstate && objcountry) {
		var index = combo_getselectedindex(objcountry);
		var strcode = (index < 0 ? "" : objcountry[index].value);
		var stateindex = combo_getselectedindex(objstate);
		var statevalue = "";
		
		
		if (stateindex > 0)
			statevalue = objstate.options[stateindex].value;
				
		combo_clear(objstate);
		var count = 0;
		combo_addtext(objstate, "", "");
		for (var a = 0; a < arrStates.length; a++) {
			if (arrStates[a][2] == strcode) {
				count ++;
				combo_addtext(objstate, arrStates[a][1], arrStates[a][0]);
			}
		}
		if (count == 0) {
			objstate.options[0].text = varNA
			objstate[0].selected = true;
		}
		
		// select state:
		if (statevalue != "") {
			for (var a = 0; a < objstate.options.length; a++) {
				if (objstate.options[a].value == statevalue)
					objstate.options[a].selected = true;
			}
		}
	}
}

function WagerNowNextRace(rootname)
{
    ShowWagerWindow(rootname + 'betslip/GetWagerDialog.mth', true, false, 750, 410);
	return false;
}

function WagerNow(ProgramID,RaceID)
{
    var strURLBase = '../BetSlip/GetWagerDialog.mth';
    var URLParams = "";
    
    URLParams = '?programid='+ProgramID+'&raceid=' + RaceID;
    ShowWagerWindow(strURLBase + URLParams, true, true, 750, 410);
	return false;
}

function ShowWagerWindow(strURL, bResizable, bScrolling, Height, Width)
{
	//create unique id so each time a popup is clicked a new window opens.
	var day = new Date();
	var id = 'WagerDialog';
	var strWindowFormat='';
	
	if (bResizable)
	{
		strWindowFormat = strWindowFormat+"resizable=1";
	}
	else
	{
		strWindowFormat = strWindowFormat+"resizable=0";
	}
	
	var windowLeft = (screen.width - Width) ;
	var windowTop = (screen.height - Height) / 2;
	strWindowFormat = strWindowFormat+",left=" + windowLeft + ",top=" + windowTop;
	
	if (bScrolling)
	{
		strWindowFormat = strWindowFormat+",scrollbars=1";
	}
	strWindowFormat = strWindowFormat+",width=" + Width + ",height=" + Height;
	//switch for status bar
	strWindowFormat = strWindowFormat+",status=0";
	//switch for help icon on the windows tool bar
	strWindowFormat = strWindowFormat+",help=0"
	//switch for address bar
	strWindowFormat = strWindowFormat+",location=0"
	//switch for menu bar
	strWindowFormat = strWindowFormat+",menubar=0"
	//alert(strWindowFormat);

    var Wager_Window = window.open(strURL, 'popUpWindow'+id, strWindowFormat);
}


function ReplayStreaming(PathOffset,RaceID)
{
    var strURLBase = PathOffset + 'viewreplaystreaming.mth?raceid=';
    ShowPopUpWindow(strURLBase + RaceID, true, true, true, 500, 750);
	return false;
}

function LiveStreaming(PathOffset,VenueID,RaceID)
{
    var strURLBase = PathOffset+'viewlivestreaming.mth?venueid=' + VenueID + '&raceid=' + RaceID;
	ShowPopUpWindow(strURLBase, true, true, true, 500, 750);
	return false;
}

function WithdrawalChargeInfo(strURL)
{
	ShowPopUpWindow(strURL, true, false, true, 500, 600);
	return false;
}
function CardVerificationInfo(strURL)
{
	ShowPopUpWindow(strURL, true, false, true, 340, 380);
	return false;
}
function register_RefreshForCountrySelection() 
{
	var objcountry = document.getElementById("addresscountrycode");
	var objstate = document.getElementById("state");
	
	if (objstate && objcountry) {
		var index = combo_getselectedindex(objcountry);
		var strcode = (index < 0 ? "" : objcountry[index].value);
		var stateindex = combo_getselectedindex(objstate);
		var statevalue = "";
		
		
		if (stateindex > 0)
			statevalue = objstate.options[stateindex].value;
				
		combo_clear(objstate);
		var count = 0;
		combo_addtext(objstate, "", "");
		for (var a = 0; a < arrStates.length; a++) {
			if (arrStates[a][2] == strcode) {
				count ++;
				combo_addtext(objstate, arrStates[a][1], arrStates[a][0]);
			}
		}
		if (count == 0) {
			objstate.options[0].text = varNA
			objstate[0].selected = true;
		}
		
		// select state:
		if (statevalue != "") {
			for (var a = 0; a < objstate.options.length; a++) {
				if (objstate.options[a].value == statevalue)
					objstate.options[a].selected = true;
			}
		}
	}
}
function combo_getselectedindex(objcombo) {
	var intindex = -1;
	var intoption = 0;
	
	while (intoption < objcombo.options.length) 
	{
		if (objcombo.options[intoption].selected == true)
		{
			intindex = intoption;			
		}
		intoption++;
	}
	
	return intindex;
}
function combo_clear(objcombo) {
	while (objcombo.options.length > 0)
		combo_removeitem(objcombo, 0);
}
function combo_removeitem(objcombo, intindex) {
	if (objcombo.options.remove)
		objcombo.options.remove(intindex);
	else
		objcombo.options[intindex] = null;
}
function combo_addtext(objcombo, strtext, strvalue) {
	var oNewOpt = new Option();
	var intpos = objcombo.options.length;
	oNewOpt.value = strvalue;
	objcombo.options[objcombo.options.length] = oNewOpt;
	objcombo.options[intpos].text = strtext;
}


function jsToggleHelp()
{
    var HelpDiv = document.getElementById('HelpText');
     
    if(HelpDiv.className=='helpShow')
      {
         HelpDiv.className='helpHide';
      }
      else
        {  
          HelpDiv.className='helpShow';
        }
}

// Create an embedded media player in the specified div, playing the
// specified media.
function makePlayer(elementid, media)
{
    obj = document.getElementById(elementid);
    
    obj.innerHTML = '<object '
                  + '  id="R2DMediaPlayer" width="360" height="290" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"'
	              + '  codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"'
	              + '  standby="Loading Microsoft Windows Media Player components..."'
	              + '  type="application/x-oleobject">'
	              + '  <param name="FileName" value="' + media + '"/>'
	              + '  <param name="ShowControls" value="False"/>'
	              + '  <param name="ShowStatusBar" value="False"/>'
	              + '  <param name="AutoStart" value="1"/>'
	              + '  <param name="DisplaySize" value="0"/>'
	              + '  <embed '
	              + '    id="R2DMediaPlayer" name="R2DMediaPlayer" width="360" height="290" type="application/x-mplayer2"'
	              + '    pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"'
	              + '    autostart="1" displaysize="0" showcontrols="0" showstatusbar="0"'
	              + '    src="' + media + '"></embed>'
                  + '</object>';
    
}


// Close the player by setting its HTML to an empty string, and then
// show the replacement content.
function closeLivePlayer(elementid) {
    obj.innerHTML = '';
    obj.style.display = 'none';
    
    replacementdiv = document.getElementById(elementid);
    replacementdiv.style.display = 'block';
    
    return false;
}

function getStreamingVideoStats(elementID) {

    var Player = document.getElementById(elementID);

	var Bandwidth = 0;
	var ErrorCorrection = "";
	var LostPackets = 0;
	var ReceivedPackets = 0;
	var RecoveredPackets = 0;
	var ReceptionQuality = 0;
	var SourceProtocol = -1;
	var BufferingProgress = 0;
	
	// assume ERR unless we manage to get real values from the Player Control.
	var ResponseCode = "ERR";
	
	try {

	    if(typeof Player != "undefined") {
    		
		    if(typeof Player.LostPackets != "undefined") {
		        LostPackets = Player.LostPackets;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.ReceivedPackets != "undefined") {
		        ReceivedPackets = Player.ReceivedPackets;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.RecoveredPackets != "undefined") {
		        RecoveredPackets = Player.RecoveredPackets;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.ReceptionQuality != "undefined") {
		        ReceptionQuality = Player.ReceptionQuality;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.Bandwidth != "undefined") {
		        Bandwidth = Player.Bandwidth;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.ErrorCorrection != "undefined") {
		        ErrorCorrection = Player.ErrorCorrection;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.SourceProtocol != "undefined") {
		        SourceProtocol = Player.SourceProtocol;
		        ResponseCode = "OK";
		    }
    		
		    if(typeof Player.BufferingProgress != "undefined") {
		        BufferingProgress = Player.BufferingProgress;
		        ResponseCode = "OK";
		    }
    		
	    }
	}
	catch(err)
	{
	}
	
	ResponseString = ResponseCode + ":" + LostPackets + ":" + ReceivedPackets + ":" + RecoveredPackets + ":";
	ResponseString += ReceptionQuality + ":" + Bandwidth + ":" + ErrorCorrection + ":" + SourceProtocol + ":";
	ResponseString += BufferingProgress;
	
	return ResponseString;
	
}
			
			
			




/// AJAX Timers
var TrackTimer=null;
var RaceTimer=null;
var RaceMTPTimer=null;
var Next5RacesTimer=null;
var STM10Timer=null;
var STMAllTimer=null;
var co10Timer=null;
var coAllTimer=null;
			
function ajaxTimer(elementid)
{
    var timerId=0;

    function fakeClick()
    {
        document.getElementById(elementid).onclick();
    }

    this.timerId = setInterval(fakeClick,30000);

    this.stopTimer = function()
    {
        clearInterval(this.timerId);
    }
}


function jsLoadTracks()
{
    //Get the program?

    //Trigger the ajax track load
    document.getElementById('gettracks').onclick();

    //Disable Race listing timer
    if(RaceTimer!=null)
    {
        RaceTimer.stopTimer();
    }


    //Enable Track listing timer?
    TrackTimer = new ajaxTimer('gettracks');

}

function jsLoadRaces(sTrackCode)
{
    //Set the program (venue) id used by the ajax call
    document.getElementById('tracktoview').value=sTrackCode;

    //Get the selected track name

    //Trigger the ajax race load
    document.getElementById('getraces').onclick();

    //Disable Track listing timer
    if(TrackTimer!=null)
    {
        TrackTimer.stopTimer();
    }

    //Enable Race listing timer
    RaceTimer = new ajaxTimer('getraces');
}

function jsGetBetSlip()
{
    // triger the ajax betslip load
    document.getElementById('getbetslip').onclick();
}         
            
function jsLoadBettingPage(brandpath, iRaceID)
{
    
    window.location.href= brandpath + 'betting/GetRaceCard.mth?raceid=' + iRaceID;
}

function jsGetRacesMTP(root) {
    jsPerformAjax(root + "AjaxDispatcher/GetRacesMTP.mth", "/ajax/racesmtp", "racemtptable");    
}
			
function jsLoadRacesMTP()
{
    //Enable Race MTP listing timer
    RaceMTPTimer = new ajaxTimer('getracesmtp');
}

function jsGetNext5Races(root) {
    jsPerformAjax(root + "AjaxDispatcher/GetNext5.mth", "/ajax/next5races", "next5table");    
}

function jsLoadNext5Races()
{
    //Enable next 5 Races listing timer
    Next5RacesTimer = new ajaxTimer('getnext5races');
}

function jsGetSTM10(root) {
    jsPerformAjax(root + "AjaxDispatcher/GetSTM10.mth", "/ajax/showthemoney", "stm10table");    
}

function jsLoadSTM10()
{
    //Enable stm10 listing timer
    STM10Timer = new ajaxTimer('getstm10');
}


function jsLoadSTMAll()
{
    //Trigger the ajax track load
    document.getElementById('getstmall').onclick();

    //Enable stmall listening timer
    STMAllTimer = new ajaxTimer('getstmall');
}

function jsGetco10(root) {
    jsPerformAjax(root + "AjaxDispatcher/Getcarryover10.mth", "/ajax/carryover", "co10table");    
}

function jsLoadco10()
{
    // enable co10 listening timer
    co10Timer = new ajaxTimer('getco10');
}
function jsLoadcoAll()
{
    document.getElementById('getcoall').onclick();
    coAllTimer = new ajaxTimer('getcoall');
}

function jsPerformAjax(method, node, targetid) {
    var Ajax = new sack(method);
    Ajax.onCompletion = function onCompletionGetRacesMTP() {
        var XMLNodeList1 = this.xmlhttp.responseXML.selectNodes(node);
        var elementObj1 = document.getElementById(targetid);
        if (jsCheckResponse(this.xmlhttp.responseText) || jsCheckError(elementObj1.innerHTML)) {
            elementObj1.innerHTML = getXMLNodeListSerialisation(XMLNodeList1);
        }
    };
    Ajax.runAJAX();
}

function jsCheckResponse(response) {
    return response.indexOf('valid=\"true\"') != -1;
}

// check if the current html is a validation error as we always want to replace that even if the response is empty
function jsCheckError(currenthtml) {    
    return currenthtml.indexOf('class=\"validation\"') != -1;
}
	
function jsStopAll()
{
//Stop track and race update calls

 if(TrackTimer!=null)
    {
        TrackTimer.stopTimer();
        alert('TrackTimer stopped');
    }
    
    if(RaceTimer!=null)
    {
        RaceTimer.stopTimer();
        alert('RaceTimer stopped');
    }

    if(RaceMTPTimer!=null)
    {
        RaceMTPTimer.stopTimer();
        alert('RaceMTPTimer stopped');
    }
    
    if(Next5RacesTimer!=null)
    {
        Next5RacesTimer.stopTimer();
        alert('Next5RacesTimer stopped');
    }
    if(STM10Timer!=null)
    {
        STM10Timer.stopTimer();
        alert('STM10Timer stopped');
    }
    if (STMAllTimer!=null)
    {
    STMAllTimer.stopTimer();
    alert('STMAllTimer stopped');
    }
    if(co10Timer!=null)
    {
        co10Timer.stopTimer();
        alert('co10Timer stopped');
    }
    if (coAllTimer!=null)
    {
        coAllTimer.stopTimer();
        alert('coAllTimer stopped');
    }
    if(TodaysWagersTimer!=null)
    {
        TodaysWagersTimer.stopTimer();
        alert('TodaysWagersTimer stopped');
    }
}
