function html_entity_decode(str) 
{
	var ta=document.createElement("textarea");
	ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return ta.value;
}

function colorboxsetup(){
	$(".horsepicture").colorbox({close:'close X'});
	$(".hipdataupdate").colorbox({width:"80%", height:"95%", iframe:true, opacity:0.55, close:'close X', onClosed:function() { $("#rnatable2").trigger("reloadGrid"); }});
	$(".catalogpage").colorbox({width:"80%", height:"90%", iframe:true, opacity:0.30, close:'close X'});
	$(".hipupdate").colorbox({width:"40%", height:"40%", iframe:true, opacity:0.65, close:'close X'});	
	//Examples of how to assign the ColorBox event to elements
	/*$("a[rel='example1']").colorbox();
	$("a[rel='example2']").colorbox({transition:"fade"});
	$("a[rel='example3']").colorbox({transition:"none", width:"75%", height:"75%"});
	$("a[rel='example4']").colorbox({slideshow:true});
	$(".example5").colorbox();
	$(".example6").colorbox({iframe:true, innerWidth:425, innerHeight:344});
	$(".example7").colorbox({width:"80%", height:"80%", iframe:true});
	$(".example8").colorbox({width:"50%", inline:true, href:"#inline_example1"});
	$(".example9").colorbox({
		onOpen:function(){ alert('onOpen: colorbox is about to open'); },
		onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
		onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
		onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
		onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
	});
	
	//Example of preserving a JavaScript event for inline calls.
	$("#click").click(function(){ 
		$('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
		return false;
	});*/
}

var default_colModel;

$(document).ready(
	function()     
	{         				  
		jQuery.jgrid.no_legacy_api = true;
		$('#clicklinktmsa').bind('click', function() { window.location = 'http://www.taylormadeadvantage.com' } );
		$.saledatefilter = 'all';
		$.horsetypefilter = 'all';		
		  
		$("#rnatable2").jqGrid({
			sortable:true,
			loadComplete: colorboxsetup,
			url: '/data/data.php?service='+$.service+'&saleid='+$.saleid,
			datatype: "xml",
			colNames:["Date","Hip", "Ped", "Update", "Horse Name", "DOB", "Sex", "Sire", "Dam", "Covering Sire", "Status", "LBD", "St", "Type", "Asking Price", "Hammer Price", "Price", "Location", "St Reg", "Video"],
			colModel:[
				{name:"date",index:"saledate", align:"center", fixed:true, width:60, xmlmap:"saledate", hidden:true, sorttype:"date", printable:true},
				{name:"hip",index:"hip", align:"center", fixed:true, width:50, xmlmap:"hip", hidden:true, sorttype:"int", printable:true},
				{name:"ped",index:"Ped",  align:"center", fixed:true, width:50, xmlmap:"ped", hidden:true, printable:false},
				{name:"update",index:"hip_update", fixed:true, width:65, align:"center", xmlmap:"update", hidden:true, printable:false},
				{name:"horsename",index:"horsename", fixed:true, width:110, align:"left", xmlmap:"horsename", hidden:true, printable:true},
				{name:"dob",index:"dob", align:"center", fixed:true, width:60, xmlmap:"dob", hidden:true, sorttype:"date", printable:true},
				{name:"sex",index:"sex", align:"center", fixed:true, width:30, xmlmap:"sex", hidden:true, printable:true},
				{name:"sirename",index:"sirename", align:"left",  fixed:true, width:110, xmlmap:"sirename", hidden:true, printable:true},
				{name:"damname",index:"damname", align:"left",  fixed:true, width:110, xmlmap:"damname", hidden:true, printable:true},
				{name:"coveringsire",index:"coveringsire", align:"left", fixed:true, width:90, xmlmap:"coveringsire", hidden:true, printable:true},
				{name:"status",index:"status", align:"center", xmlmap:"status", fixed:true, width:60, hidden:true, printable:true},
				{name:"lbd",index:"lbd", align:"center", fixed:true, width:60, xmlmap:"lbd", hidden:true, sorttype:"date", printable:true},
				{name:"state",index:"state", align:"center", fixed:true, width:30, xmlmap:"state", hidden:true, printable:true},
				{name:"type",index:"type", align:"center", xmlmap:"type", fixed:true, width:75, hidden:true, printable:true},
				{name:"askingprice",index:"askingprice", align:"right", xmlmap:"askingprice", hidden:true, fixed:true, width:90, sorttype:"float", printable:true},
				{name:"hammerprice",index:"hammerprice", align:"right", xmlmap:"hammerprice", hidden:true, fixed:true, width:90, sorttype:"float", printable:true},
				{name:"price",index:"price", align:"right", xmlmap:"price", hidden:true, fixed:true, width:75, sorttype:"float", printable:true},
				{name:"location",index:"location", align:"left", xmlmap:"location", hidden:true, printable:true, fixed:true, width:100},
				{name:"statereg",index:"statereg", align:"center", xmlmap:"statereg", fixed:true, width:50, hidden:true, printable:true},
				{name:"videolink",index:"videolink", align:"center", xmlmap:"videolink", fixed:true, width:50, hidden:true, printable:false}
			],
			width:985,
			height:'100%',
			rowNum:50,
			pager: '#psortrows',
			sortname: 'hip',
			sortorder: "asc",
			viewrecords: true,
			rowList:[25,50,75,100,150],
			xmlReader: {
					root : "data",
					row: "horse",
					repeatitems: false,
					id: "pkey",
					total: "totalpages",
					page: "currentpage",
					records: "records"
			},
			caption: $.salename
			
		});
		$("#rnatable2").jqGrid('navGrid','#psortrows',{add:false,edit:false,del:false,search:false,refresh:true});
		/*$("#rnatable2").jqGrid('navButtonAdd','#psortrows',{
			caption: "Columns",
			title: "Reorder Columns",
			onClickButton : function (){
				jQuery("#rnatable2").jqGrid('columnChooser');
			}
		});*/
		$("#rnatable2").jqGrid('navButtonAdd','#psortrows',{
			caption: "Printable Version (PDF)",
			title: "Printable Version",
			buttonicon: 'ui-icon-print',
			onClickButton : function (){
				current_colModel = $("#rnatable2").jqGrid('getGridParam', 'colModel');
				current_colNames = $("#rnatable2").jqGrid('getGridParam', 'colNames');
				numofcolumns = default_colModel.length;
				columnsdata = new Array();
				columnsheader = new Array();
				columnswidth = new Array();
				columnsalign = new Array();
				for (i=0; i<numofcolumns; i++)
				{
					
					if (current_colModel[i]['hidden']!=true && current_colNames[i]!='' && current_colModel[i]['printable']!=false)
					{
						columnsdata.push(current_colModel[i]['index']);
						columnsheader.push(current_colNames[i]);
						columnswidth.push(current_colModel[i]['width']);
						columnsalign.push(current_colModel[i]['align'].charAt(0));
					}
				}	
				window.open("pdf.php?columndata="+columnsdata.toString()+"&columnheader="+columnsheader.toString()+"&columnwidth="+columnswidth.toString()+"&align="+columnsalign.toString()+"&datefilter="+$.saledatefilter+"&typefilter="+$.horsetypefilter+"&saleid="+$.saleid ,null, "status=yes,toolbar=no,menubar=no,location=no,resizable=yes");
			}
		});
		
		/*$("#rnatable2").jqGrid('navButtonAdd','#psortrows',{
			caption: "Excel",
			title: "Export Excel",
			buttonicon: 'ui-icon-document',
			onClickButton : function (){
				current_colModel = $("#rnatable2").jqGrid('getGridParam', 'colModel');
				current_colNames = $("#rnatable2").jqGrid('getGridParam', 'colNames');
				numofcolumns = default_colModel.length;
				columnsdata = new Array();
				columnsheader = new Array();
				columnswidth = new Array();
				columnsalign = new Array();
				for (i=0; i<numofcolumns; i++)
				{
					
					if (current_colModel[i]['hidden']!=true && current_colNames[i]!=''  && current_colModel[i]['printable']!=false)
					{
						columnsdata.push(current_colModel[i]['index']);
						columnsheader.push(current_colNames[i]);
						columnswidth.push(current_colModel[i]['width']);
						columnsalign.push(current_colModel[i]['align'].charAt(0));
					}
				}	
				window.open("excel.php?columndata="+columnsdata.toString()+"&columnheader="+columnsheader.toString()+"&columnwidth="+columnswidth.toString()+"&align="+columnsalign.toString()+"&datefilter="+$.saledatefilter+"&typefilter="+$.horsetypefilter+"&saleid="+$.saleid ,null, "status=yes,toolbar=no,menubar=no,location=no,resizable=yes");
			}
		});	*/			
		//$("#rnatable2").jqGrid('sortableRows');
		default_colModel = $("#rnatable2").jqGrid('getGridParam', 'colModel').slice(0);		
		
		$("#search").keyup(function(){
			clearTimeout($.searchtimeout);
			$("#rnatable2").setGridParam({page:1});
			$("#rnatable2").jqGrid('setPostDataItem',"searchvalue", this.value);
			$.searchtimeout = setTimeout('$("#rnatable2").trigger("reloadGrid");', 750);
		});	
				
		$("#datefilter").change(function(){
			$("#rnatable2").setGridParam({page:1});
			$("#rnatable2").jqGrid('setPostDataItem',"saledate", this.value);
			$.saledatefilter = this.value;
			$("#rnatable2").trigger("reloadGrid");
		});	
		$("#typefilter").change(function(){
			$("#rnatable2").setGridParam({page:1});
			$("#rnatable2").jqGrid('setPostDataItem',"horsetype", this.value);
			$.horsetypefilter = this.value;
			$("#rnatable2").trigger("reloadGrid");
		});
		
		
		$.ajax({   
			type: "POST",
			url: "/data/data.php?service=getavailablesales",   
			data: "",
			async: true,
			cache: false,
			dataType: 'json',
			error: function(request, error) { alert('There was a problem with the data'); },
			success: 
				function(data, textStatus, XMLHttpRequest) 
				{  
					$('.salesselection .salestablelist').empty();
					
					for(i=0;i<data.info.length;i++)
					{
						$('.salesselection .salestablelist').append('<tr><td><a class="salesselectionlink" href="?saleid='+data.info[i].id+'&noprompt=true">'+data.info[i].name+'</a></td></tr>');
					}
					
					if (!$.noprompt && parseInt(data.count)>1)
					{
						$().colorbox({ width:'65%', open:true, inline:true, href:'.salesselection', opacity:0.30, close:'close X' }); 
					}					
					
					if (parseInt(data.count)>1)
					{
						$('.ui-jqgrid-title').append(' <span><a class="othersales" href="#">Other Sales</a></span>');
						$('.othersales').colorbox({ width:'65%', inline:true, href:'.salesselection', opacity:0.30, close:'close X' });
					}
				}
		});			
		
		$.ajax({   
			type: "POST",
			url: "/data/data.php?service=visiblecolumns&page="+$.page+"&table=rnaecattable&saleid="+$.saleid,   
			data: "",
			async: true,
			cache: false,
			error: function(request, error) { alert('There was a problem with the data'); },
			success: 
				function(data, textStatus, XMLHttpRequest) 
				{  
					showcolumns = Array();
					$(data).find("data").each( 
						function()
						{ 
							$(data).find("visiblecolumn").each( 
								function()
								{ 
									showcolumns.push($(this).text());
								} 
							);							
						} 
					);
					$("#rnatable2").jqGrid('showCol', showcolumns);
					//if (showcolumns.length==0) alert('There are no columns enabled for viewing in the sale configuration.');
				}
		});		

		$.ajax({   
			type: "POST",
			url: "/data/data.php?service=saledates&saleid="+$.saleid,   
			data: "",
			async: true,
			cache: false,
			dataType: "json",
			error: function(request, error) { alert('There was a problem with the data'); },
			success: function(data) {
					for(i=0;i<data.saledates.length;i++)
					{
						$('#datefilter').append($("<option></option>").attr("value", data.saledates[i].date).text(data.saledates[i].text));
						
					}				
				}
		});
		
		$.ajax({   
			type: "POST",
			url: "/data/data.php?service=horsetypes&saleid="+$.saleid,   
			data: "",
			async: true,
			cache: false,
			error: function(request, error) { alert('There was a problem with the data'); },
			success: 
				function(data, textStatus, XMLHttpRequest) 
				{  
					$(data).find("data").each( 
						function()
						{ 
							$(data).find("horsetype").each( 
								function()
								{ 
									horsetype = $(this).text();
									$('#typefilter').append($("<option></option>").attr("value", horsetype).text(horsetype));
								} 
							);							
						} 
					);
				}
		});		
	}
);

