function setMsg(msg) {
	window.status = msg;
	return true;
}
	
function loadXMLIntro() {
	url = "/engine/ContentManagementServlet?contentType=04&showUnapproved=false";
	if (window.XMLHttpRequest) {
		req1 = new XMLHttpRequest();
		req1.onreadystatechange = processIntro;
		req1.open("GET", url, true);
		req1.send(null);
	} 
	else if (window.ActiveXObject) {
		req1 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req1) {
			req1.onreadystatechange = processIntro;
			req1.open("GET", url, true);
			req1.send();
		}
	}
}				

function loadXMLPicks() {
	url = "/engine/ContentManagementServlet?contentType=02&showUnapproved=false";
	if (window.XMLHttpRequest) {
		req2 = new XMLHttpRequest();
		req2.onreadystatechange = processPicks;
		req2.open("GET", url, true);
		req2.send(null);
	} 
	else if (window.ActiveXObject) {
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req2) {
			req2.onreadystatechange = processPicks;
			req2.open("GET", url, true);
			req2.send();
		}
	}
}			

function loadXMLCarryovers() {
	url = "/engine/ContentManagementServlet?contentType=03&showUnapproved=false";
	if (window.XMLHttpRequest) {
		req3 = new XMLHttpRequest();
		req3.onreadystatechange = processCarryovers;
		req3.open("GET", url, true);
		req3.send(null);
	} 
	else if (window.ActiveXObject) {
		req3 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req3) {
			req3.onreadystatechange = processCarryovers;
			req3.open("GET", url, true);
			req3.send();
		}
	}
}				

function loadXMLCarryovers2() {
	url = "/engine/ContentManagementServlet?contentType=03&showUnapproved=false";
	if (window.XMLHttpRequest) {
		req3 = new XMLHttpRequest();
		req3.onreadystatechange = processCarryovers2;
		req3.open("GET", url, true);
		req3.send(null);
	} 
	else if (window.ActiveXObject) {
		req3 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req3) {
			req3.onreadystatechange = processCarryovers2;
			req3.open("GET", url, true);
			req3.send();
		}
	}
}			

function loadXMLArticles() {
	url = "/engine/ContentManagementServlet?contentType=01&showUnapproved=false";
	if (window.XMLHttpRequest) {
		req4 = new XMLHttpRequest();
		req4.onreadystatechange = processArticles;
		req4.open("GET", url, true);
		req4.send(null);
	} 
	else if (window.ActiveXObject) {
		req4 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req4) {
			req4.onreadystatechange = processArticles;
			req4.open("GET", url, true);
			req4.send();
		}
	}
}				

function processIntro() {
	if (req1.readyState == 4) {
		if (req1.status == 200) {
			for(i=0;i<req1.responseXML.documentElement.childNodes.length;i++){
				n = req1.responseXML.documentElement.childNodes[i].nodeName;
				//alert(n);
				if(n=="#comment"){
document.getElementById('introHtml').innerHTML = req1.responseXML.documentElement.childNodes[i].nodeValue;
break;
				}
			}
		} else {
			setMsg("There was a problem displaying the intro.");
		}
	}
}

function processPicks() {
	if (req2.readyState == 4) {
		if (req2.status == 200) {
			for(i=0;i<req2.responseXML.documentElement.childNodes.length;i++){
				n = req2.responseXML.documentElement.childNodes[i].nodeName;
				//alert(n);
				if(n=="#comment"){
document.getElementById('picks').innerHTML = req2.responseXML.documentElement.childNodes[i].nodeValue;
break;
				}
			}
		} else {
			setMsg("There was a problem displaying picks.");
		}
	}
}

function processCarryovers() {
	if (req3.readyState == 4) {
		if (req3.status == 200) {
			response  = req3.responseXML.documentElement;
			xslt = new XML.Transformer("/xml/carryovers.xsl");
			xslt.transform(req3.responseXML,"carryOvers");
		} else {
			setMsg("There was a problem displaying carryovers.");
		}
	}
}

function processCarryovers2() {
	if (req3.readyState == 4) {
		if (req3.status == 200) {
			response  = req3.responseXML.documentElement;
			xslt = new XML.Transformer("/xml/carryovers.xsl");
			xslt.transform(req3.responseXML,"carryOvers2");
		} else {
			setMsg("There was a problem displaying carryovers.");
		}
	}
}

function processArticles() {
	if (req4.readyState == 4) {
		if (req4.status == 200) {
			response  = req4.responseXML.documentElement;
			xslt = new XML.Transformer("/xml/articles.xsl");
			xslt.transform(req4.responseXML,"articles");
		} else {
			setMsg("There was a problem displaying articles.");
		}
	}
}	

function loadXMLUpcoming() {
	url = "/engine/GetXmlDoc?actionCode=schedule";
	//url = "upcoming.php";
	if (window.XMLHttpRequest) {
		req5 = new XMLHttpRequest();
		req5.onreadystatechange = processUpcoming;
		req5.open("GET", url, true);
		req5.send(null);
	} 
	else if (window.ActiveXObject) {
		req5 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req5) {
			req5.onreadystatechange = processUpcoming;
			req5.open("GET", url, true);
			req5.send();
		}
	}
}

function processUpcoming() {
	if (req5.readyState == 4) {
		if (req5.status == 200) {
			response  = req5.responseXML.documentElement;
			xslt = new XML.Transformer("/xml/upcoming.xsl");
			xslt.transform(req5.responseXML,"upcomingRaces");
		} else {
			setMsg("There was a problem displaying upcoming race information.");
		}
	}
}	

function loadXMLTracklist(options) {
	if(options!=""){
		url = "/engine/TrackListServlet"+options+"&html=true";
	}else{
		url = "/engine/TrackListServlet?html=true";
	}
	$('#tracklist').load(url);
}

function _loadXMLTracklist(options) {
	//alert("loadXMLTracklist("+options+")");
	url = "/engine/TrackListServlet"+options;
	//alert("servlet url: "+url);
	if (window.XMLHttpRequest) {
		req6 = new XMLHttpRequest();
		req6.onreadystatechange = processTracklist;
		req6.open("GET", url, true);
		req6.send(null);
	} else if (window.ActiveXObject) {
		//alert("BBB");
		req6 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req6) {
			req6.onreadystatechange = processTracklist;
			req6.open("GET", url, true);
			req6.send();
		}
	}
}

function processTracklist() {
	//alert("processTracklist()");
	if (req6.readyState == 4) {
		//alert("req6.readyState: "+req6.readyState);
		if (req6.status == 200) {
			//alert("req6.status: "+req6.status);
			response  = req6.responseXML.documentElement;
			//alert("response: "+response);
			try{
				xslt = new XML.Transformer("/xml/tracklist.xsl");
				xslt.transform(req6.responseXML,"tracklist");
			} catch (e) {
				try{
					$('#tracklist').xslt(req6.responseXML, "/xml/tracklist.xsl");
				 } catch(e) {
					//alert("error: "+e.message);
				}
			}
		} else {
			//alert("There was a problem displaying upcoming race information.");
			setMsg("There was a problem displaying upcoming race information.");
		}
	}
}	

function cancelTicket(ticketId,rowType){
	//alert("cancelTicket("+ticketId+","+rowType+")");
if(confirm("Are you sure you want to cancel this ticket?")){
	var xmlDocument = $("<dummy><tickets nocheck='true' action='1'><ticket>"+ticketId+"</ticket></tickets></dummy>")
	$.ajax({
		type: "POST",
		url: "/engine/BamTicketActionServlet",
	    contentType: "text/xml",
	    processData: false,
	    data: "<tickets nocheck='true' action='1'><ticket>"+ticketId+"</ticket></tickets>",
		success: function(html){
			alert(html);
			if(html.indexOf("Ticket " + ticketId + ": Cancelled.")!=-1){
				$("#tcktSts"+ticketId).text("Cancelled");
				$("#tcktCncl"+ticketId).text("");
			}
		}
	});
	}
}		
	
function loadWeather(url) {
	//alert("loadWeather("+url+")");
	$("#weatherFrame").load(url);
}		

function selectRace2(sel){
    var opts = sel.options;
    var i = opts.selectedIndex;
    if ( i >= 0 ){
        var opts = sel.options;
		//alert(opts[i].value);
        window.location="handicapping.jsp?" + opts[i].value;
    }
}

function selectAdvRace2(sel){
    var opts = sel.options;
    var i = opts.selectedIndex;
    if ( i >= 0 ){
        var opts = sel.options;
		//alert(opts[i].value);
        window.location="adv_handicapping.jsp?" + opts[i].value;
    }
}

function selectAdvRace3(sel){
    var opts = sel.options;
    var i = opts.selectedIndex;
    if ( i >= 0 ){
        var opts = sel.options;
		//alert(opts[i].value);
        window.location="harness_handicapping.jsp?" + opts[i].value;
    }
}

function selectAdvRace4(sel){
    var opts = sel.options;
    var i = opts.selectedIndex;
    if ( i >= 0 ){
        var opts = sel.options;
		//alert(opts[i].value);
        window.location="gh_handicapping.jsp?" + opts[i].value;
    }
}

var horseTicketWindow;

var storeWindow;

function launchStore(raceid){
    var hostname = window.location.hostname;
    
    if( storeWindow == null || storeWindow.closed ){
		if(raceid==null){
			storeWindow = window.open('../store.jsp', 'Store', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=930,height=800');
        }else{
			storeWindow = window.open('../store.jsp?raceId='+raceid+'', 'Store', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=930,height=800');
        }
	} else{
		storeWindow.focus();
	}
}

function launchTicket(){
    var hostname = window.location.hostname;
    
    if( horseTicketWindow == null || horseTicketWindow.closed ){
        horseTicketWindow = window.open('../ticketWriter.jsp', 'Ticket', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=670,height=603');
	} else{
		horseTicketWindow.focus();
	}
}

function launchTicketAlt(){
    var hostname = window.location.hostname;
    
    if( horseTicketWindow == null || horseTicketWindow.closed ){
        horseTicketWindow = window.open('/ticketWriter.jsp', 'Ticket', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=670,height=603');
	} else{
		horseTicketWindow.focus();
	}
}
var printWindow;

function launchPrintVersion(){
	var location = window.location;
	
	if( printWindow == null ){
		if(location.toString().indexOf("?") == -1)
		{
		  printWindow = window.open(location+'?print=true', 'Printable_Version', 'toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes,width=930,height=603');
		}
		else
		{
			printWindow = window.open(window.location+'&print=true', 'Printable_Version', 'toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes,width=930,height=603');
		}
	        
	}
	else if(printWindow != null && printWindow.closed)
	{
		if(location.toString().indexOf("?") == -1)
		{
			printWindow = window.open(window.location+'?print=true', 'Printable_Version', 'toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes,width=930,height=603');
		}
		else
		{
		  printWindow = window.open(window.location+'&print=true', 'Printable_Version', 'toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes,width=930,height=603');
		  
		}
	}
	else{
		printWindow.focus();
	}
}


var videoWindow1;
var videoWindow2;
var videoWindow3;
var videoWindowReplay;
var videoWindowGH;
var raceReplay;

function openVideo1(){
    //var hostname = window.location.hostname;
    
    if( videoWindow1 == null || videoWindow1.closed ){
        videoWindow1 = window.open('/video.jsp', 'Video1', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
        //videoWindow1 = window.open('/video.jsp', 'Video1', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no');
	} else{
		videoWindow1.focus();
	}
}

function openVideo2(){
    //var hostname = window.location.hostname;
    
    if( videoWindow2 == null || videoWindow2.closed ){
        videoWindow2 = window.open('/video.jsp', 'Video2', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
        //videoWindow2 = window.open('/video.jsp', 'Video2', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no');
	} else{
		videoWindow2.focus();
	}
}

function openVideo3(){
    //var hostname = window.location.hostname;
    
    if( videoWindow3 == null || videoWindow3.closed ){
        videoWindow3 = window.open('/video.jsp', 'Video3', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
	} else{
		videoWindow3.focus();
	}
}

function openVideoReplay(filename, trackid){
    //var hostname = window.location.hostname;
    
    if( videoWindowReplay == null || videoWindowReplay.closed ){
    	videoWindowReplay = window.open('/video_replay.jsp?replayFileName='+filename+'&trackId='+trackid+'&action=video', 'Race_Replay', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
	} 
    else{
		videoWindowReplay.close();
		videoWindowReplay = window.open('/video_replay.jsp?replayFileName='+filename+'&trackId='+trackid+'&action=video', 'Race_Replay', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
		
    }
}

function openRaceReplay(){
    //var hostname = window.location.hostname;
    
    if( raceReplay == null || raceReplay.closed ){
    	raceReplay = window.open('/raceReplay.jsp', 'Race_Replay', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=421,height=580');
	} else{
		raceReplay.focus();
	}
}


function openGHVideo(){
    //var hostname = window.location.hostname;
    
    if( videoWindowGH == null || videoWindowGH.closed ){
        videoWindowGH = window.open('/ghVideo.jsp', 'VideoGH', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=344,height=346');
        //videoWindowGH = window.open('/video.jsp', 'VideoGH', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no');
	} else{
		videoWindowGH.focus();
	}
}

function doRefresh(){
    setTimeout( "refresh()", 60 * 1000 );
}

function refresh(){
    window.location.href = (location.href);
}

function launchTicketAdv(track,race){
    var hostname = window.location.hostname;
    
    if( horseTicketWindow == null || horseTicketWindow.closed ){
        horseTicketWindow = window.open('ticketWriter.jsp?track_code='+track+'&race_num='+race+'', 'Ticket', 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=670,height=603');
	} else{
		horseTicketWindow.focus();
	}
}

function launchPrint(url){
    var hostname = window.location.hostname;
    
    if( printWindow == null || printWindow.closed ){
        printWindow = window.open(''+url+'', 'Print', 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=1000,height=800');
	} else{
		printWindow.focus();
	}
}

function updateHCTab(selectedTab){
	try{
		var src = $("#hcPoolsTab").attr("src").replace(/-over/, "");
		$("#hcPoolsTab").attr("src",src);
	}catch(e){
	}
	try{
		var src = $("#hcExoticsTab").attr("src").replace("-over", "");
		$("#hcExoticsTab").attr("src",src);
	}catch(e){
	}
	try{
		var src = $("#hcResultsTab").attr("src").replace("-over", "");
		$("#hcResultsTab").attr("src",src);
	}catch(e){
	}
	if($("#"+selectedTab).attr("src").indexOf("-over")==-1){
		var src = $("#"+selectedTab).attr("src").match(/[^\.]+/) + "-over.gif";
		$("#"+selectedTab).attr("src", src);
    }
}

function showPool(id) {
	pools = new Array("pool_1A","pool_1B","pool_1C","pool_2","pool_3","pool_4","pool_5","no_wps");
	for(i=0;i<pools.length;i++){ 
		identity=document.getElementById(pools[i]);
		if(identity != null){
			identity.style.display = "none";
		}						
	}
	identity=document.getElementById(id);
	if(identity != null){
		identity.style.display = "block";
	}
}
function redirect(address)
{
	window.location.href  = address;

}
function logout() {
	var agree=confirm("Are you sure you wish to logout?");
	if (agree)
		document.getElementById('logoutForm').submit();
}

function _IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsNumeric(input) {
   return (input - 0) == input && input.length > 0;
}

function formatCurrency(obj) {
        userVal=(obj.value.replace(/[^\d\.]/g, "")*1).toFixed(2);
        for(i=userVal.length-6;i>0;i-=3)
        userVal = userVal.substr(0,i)+","+userVal.substr(i)
        return userVal;
}