	/* Populate Giving Opportunities */ 
	function renderGivingOpportunities(xml){
		var oddEven = "odd";
	
    	$(xml).find('opportunity').each(function(){
    		
	    	var id = $(this).attr('ID');
	        var name = $(this).attr('Name');
	        var description = $(this).attr('Description');
	        var partnerName = $(this).attr('PartnerName');
	        var amount1 = $(this).attr('Amount1');
	        var amount2 = $(this).attr('Amount2');
	        var amount3 = $(this).attr('Amount3');
	        var amount4 = $(this).attr('Amount4');
	        var amount5 = $(this).attr('Amount5');
	        var slots = $(this).attr('Capacity');
	        var estimated = $(this).attr('Estimated');
	        
	        var amount = "";
	        if (amount1 != '')
	        	amount = '$' + amount1;
	        else
	        	amount = "Any Amount"
	        if (amount2 != '')
	        	amount = '$' + amount1 + " - $" + amount2;
	        if (amount3 != '')
	        	amount = '$' + amount1 + " - $" + amount3;
	        if (amount4 != '')
	        	amount = '$' + amount1 + " - $" + amount4;
	        if (amount5 != '')
	        	amount = '$' + amount1 + " - $" + amount5;
	        
      		var oppRow = $('<tr class="standard ' + oddEven + '" id="opp' + id + '"></tr>');
      		var oppDetailRow = $('<tr class="detailRow expand-child" id="oppDetail' + id + '" style="display: none"></tr>');
			
		
			
			$('<td nowrap></td>')
				.html(name)
				.appendTo(oppRow);
			
			$('<td nowrap></td>')
				.html(partnerName)
				.appendTo(oppRow);
			
			$('<td nowrap></td>')
				.html(amount)
				.appendTo(oppRow);
				
			$('<td colspan="5" style="text-align:center;"></td>')
				.html('<img src="images/spinning_big.gif" width="32" height="32" />')
				.appendTo(oppDetailRow);
				
			oppRow.click(function(){
				if (activeRow != ""){
					$("#oppDetail" + activeRow).hide();
					$("#opp" + activeRow).removeClass("selectedRow");
				}
				$(this).addClass("selectedRow");
      			$('#oppDetail' + id).show();
      			
      			
      			$.get("includes/details_giving.php",{oid:id},function(temp){
					$('#oppDetail' + id).html(temp);
				
					$('#details' + id).html(description);
				
					if (slots == '' || slots > 0) {
						var amountList = $('#amountList' + id);
						
						if (estimated != "1"){
						
							if (amount2 == ''){
								$('<li></li>')
									.html('<b>$</b><input class="amountInput" type="text" id="amount' + id + '" value="' + amount1 + '" />') 
									.appendTo(amountList);
								
								$('#amount' + id).constrainInput({allowedCharsRegex: "\\d+"});
							
							} else {
								$('<li></li>')
									.html('<input type="radio" id="amount1' + id + '" name="amount' + id + '" value="' + amount1 + '" checked /> $' + amount1) 
									.appendTo(amountList);
								$('<li></li>')
									.html('<input type="radio" id="amount2' + id + '" name="amount' + id + '" value="' + amount2 + '" /> $' + amount2) 
									.appendTo(amountList);
								if (amount3 != '')
									$('<li></li>')
										.html('<input type="radio" id="amount3' + id + '" name="amount' + id + '" value="' + amount3 + '" /> $' + amount3) 
										.appendTo(amountList);
								if (amount4 != '')
									$('<li></li>')
										.html('<input type="radio" id="amount4' + id + '" name="amount' + id + '" value="' + amount4 + '" /> $' + amount4) 
										.appendTo(amountList);
								if (amount5 != '')
									$('<li></li>')
										.html('<input type="radio" id="amount5' + id + '" name="amount' + id + '" value="' + amount5 + '" /> $' + amount5) 
										.appendTo(amountList);
								$('<li></li>')
									.html('<input type="radio" id="amount6' + id + '" name="amount' + id + '" value="0" /> <b>$</b><input class="amountInput" type="text" id="amountOther' + id + '" />') 
									.appendTo(amountList);
								
								$('#amountOther' + id).click(function(){
								
									$('#amount6' + id).attr("checked", "checked");
								
								});
							
								$('#amountOther' + id).constrainInput({allowedCharsRegex: "\\d+"});
							
							}
						}
						$('#add' + id).click(function(){
							var amt = '0';
							if (estimated != '1'){
								if (amount2 == '') {
									amt = $('#amount' + id).val();
								}else{
									amt = $('input[name="amount' + id + '"]:checked').val();
									if (amt == '0')
										amt = $('#amountOther' + id).val();
								}
							}
							if ((amt != '' && amt != '0') || estimated == '1'){
							
								$.get("addToCart.php",
									{sid:sessionID,oid:id,quantity:1, amount:amt},
									function(){
										$.get("getCart.php",function(xml){renderCart(xml)}); 
									});
							
								$('#oppDetail' + id)
									.find('.options')
									.effect("transfer", {to: "#cartbox"},800);
								$('#oppDetail' + id)
									.find('.options')
									.html('This opportunity has been<br>added to your basket.');
							}
						});
					} else {
					
						$('#oppDetail' + id)
							.find('.options')
							.html('There are no more slots available.');
						
					}
      			});
      			
      			activeRow = id;
      		});
      		

				
			
		
			
        	oppRow
            	.appendTo('#opportunities');
        	oppDetailRow
            	.appendTo('#opportunities');
           	
           	if (oddEven == "odd")
           		oddEven = "even";
           	else
           		oddEven = "odd";
   		});
   		
   		$("#opportunities").tablesorter(
   			{widgets: ['zebra']}
   		); 
   			
   		$("#opportunities tr.standard").hover(
   			function(){
			    $(this).addClass("highlight");
   			},
   			function(){
			    $(this).removeClass("highlight");
   			});


		$("#loading").hide();
		$("#opportunities").show();
    }





/* Populate Service Opportunities */ 
	function renderServiceOpportunities(xml){
		
		var oddEven = "odd";
	
    	$(xml).find('opportunity').each(function(){
    		
	    	var id = $(this).attr('ID');
	        var name = $(this).attr('Name');
	        var espanol = $(this).attr('Espanol');
	        var description = $(this).attr('Description');
	        var partnerName = $(this).attr('PartnerName');
	        var startDate = $(this).attr('StartDate');
	        var endDate = $(this).attr('EndDate');
	        var startTime = $(this).attr('StartTime');
	        var endTime = $(this).attr('EndTime');
	        var slots = $(this).attr('Capacity');
	        var closed = $(this).attr('Closed');
	        
	        if (espanol == '1')
	        	name = name + ' <img src="images/esp.gif" width="20" height="11" align="top" />';
	        
      		var oppRow = $('<tr class="standard ' + oddEven + '" id="opp' + id + '"></tr>');
      		var oppDetailRow = $('<tr class="detailRow expand-child" id="oppDetail' + id + '" style="display: none"></tr>');
			
			if (id == "294") {
				$('<td nowrap></td>')
					.html(startDate + "&nbsp;&nbsp;&nbsp; to")
					.appendTo(oppRow);
					
				$('<td nowrap></td>')
					.html(endDate)
					.appendTo(oppRow);
			} else {
				$('<td nowrap></td>')
					.html(startDate)
					.appendTo(oppRow);
				
				$('<td nowrap></td>')
					.html(startTime + " to " + endTime)
					.appendTo(oppRow);
			}
			
			$('<td nowrap></td>')
				.html(name)
				.appendTo(oppRow);
			
			$('<td nowrap></td>')
				.html(partnerName)
				.appendTo(oppRow);
			
			
			if (slots > 0 && closed == 0){
				$('<td></td>')
					.html(slots)
					.appendTo(oppRow);
			} else {
				$('<td></td>')
					.html('Closed')
					.appendTo(oppRow);
			}
			
			$('<td colspan="5" style="text-align:center;"></td>')
				.html('<img src="images/spinning_big.gif" width="32" height="32" />')
				.appendTo(oppDetailRow);
				
			oppRow.click(function(){
				if (activeRow != ""){
					$("#oppDetail" + activeRow).hide();
					$("#opp" + activeRow).removeClass("selectedRow");
				}
				$(this).addClass("selectedRow");
      			$('#oppDetail' + id).show();
				
				
				$.get("includes/details.php",{oid:id},function(temp){
					$('#oppDetail' + id).html(temp);
				
					$('#details' + id).html(description);
				
					if (slots > 0 && closed == 0) {
						var quantitySelect = $('#quantity' + id);
				
						for (i=1;i<=slots;i++) {
							$('<option></option>').html(i).val(i)
								.appendTo(quantitySelect);
							if (id == "294")
								break;
						}
				
						$('#add' + id).click(function(){
							$.get("addToCart.php",
								{sid:sessionID,oid:id,quantity: quantitySelect.val(), amount:1},
								function(){
									$.get("getCart.php",function(xml){renderCart(xml)}); 
								});
							
							$('#oppDetail' + id)
								.find('.options')
								.effect("transfer", {to: "#cartbox"},800);
							$('#oppDetail' + id)
								.find('.options')
								.html('This opportunity has been<br>added to your basket.');
						
						});
					} else {
						if (closed == 0)
							$('#oppDetail' + id)
								.find('.options')
								.html('There are no more slots available.');
						else
							$('#oppDetail' + id)
								.find('.options')
								.html('Registration has closed for this opportunity.');
							
					}
				});
				
				
      			activeRow = id;
      		});
				
			
			
        	oppRow
            	.appendTo('#opportunities');
        	oppDetailRow
            	.appendTo('#opportunities');
           	
           	if (oddEven == "odd")
           		oddEven = "even";
           	else
           		oddEven = "odd";
   		});
   		
   		$("#opportunities").tablesorter(
   			{
   			
   			widgets: ['zebra'],
   			headers: { 
                0: { 
                    sorter:'custDate' 
                } ,
                3: {
                	sorter:'text'
                }
            } 
   			}
   			
   		); 
   			
   		$("#opportunities tr.standard").hover(
   			function(){
			    $(this).addClass("highlight");
   			},
   			function(){
			    $(this).removeClass("highlight");
   			});

		$("#loading").hide();
		$("#opportunities").show();
    }
    
    
    
	
	/* Populate Cart */ 
	function renderCart(xml){
		var giveRow = "odd";
		var serveRow = "odd";
		var cartEmpty = "1";
		var giveEmpty = "1";
		var serveEmpty = "1";
		
    	$(xml).find('cart').each(function(){	
			var id = $(this).attr('ID');
			var otID = $(this).attr('OpportunityTypeID');
	        var name = $(this).attr('Name');
	        var quantity = $(this).attr('Quantity');
	        var amount = $(this).attr('Amount');
	        var estimated = $(this).attr('Estimated');
	        var slotsAvailable = $(this).attr('SlotsAvailable');
	        
	        if ((slotsAvailable != "" && parseInt(slotsAvailable) >= parseInt(quantity)) || slotsAvailable == "") {
	   			
	   			if (otID == "1" && giveEmpty == "1") {
					$("#cartGive tbody").empty();
					giveEmpty = "0";
	   			}
	   			if (otID == "2" && serveEmpty == "1") {
					$("#cartServe tbody").empty();
					serveEmpty = "0";
	   			}
	   			
	        	cartEmpty = "0";
	      		var cartRow = $('<tr></tr>');
    	  		var trashCell = $('<td width="16"></td>')
      				.html('<img class="trash" src="images/trash_icon.gif" width="16" height="16" border="0" />');
      		
      		
	      		$('<td width="1"></td>')
					.html("-")
					.appendTo(cartRow);
			
				$('<td></td>')
					.html(name)
					.appendTo(cartRow);
				if (otID == 2){
					$('<td width="2" style="text-align:right"></td>')
						.html(quantity)
						.appendTo(cartRow);
				} else {
					if (estimated == '1') 
						$('<td width="2" style="text-align:right"></td>')
							.appendTo(cartRow);
					else
						$('<td width="2" style="text-align:right"></td>')
							.html('$' + amount)
							.appendTo(cartRow);
					
				}
				trashCell.appendTo(cartRow);
					
				trashCell.click(function(){
					trashCell.html('<img src="images/spinning.gif" width="16" height="16" border="0" />');
					$.get("removeFromCart.php",{cid:id},
						function(){
							$.get("getCart.php",function(xml){renderCart(xml)});
						});
				 
				
	      		});
			
				if (otID == 2){
	    	    	cartRow.addClass(serveRow)
    	    	    	.appendTo('#cartServe');
    	        	if (serveRow == 'odd')
    	        		serveRow = 'even';
	    	        else
    		        	serveRow = 'odd';
    		    } else {
	        		cartRow.addClass(giveRow)
    	        		.appendTo('#cartGive');
	    	        if (giveRow == 'odd')
    		        	giveRow = 'even';
    		        else
    	    	    	giveRow = 'odd';
   		 		}
   		 	} else {
   		 		$.get("removeFromCart.php",{cid:id});
   		 	}
   		});
   		
   		if (cartEmpty == "0")
   			$("#checkoutlink").show();
   		if (serveEmpty == "1") {
   			var serveRow = $('<tr></tr>');
   			
   			$("#cartServe tbody").empty();
   			$('<td colspan="3" style="text-align:center; font-size:10px;"></td>')
   				.html("There are no service opportunities<br>in your basket.")
   				.appendTo(serveRow);
   			serveRow.appendTo('#cartServe');
   		}
   		if (giveEmpty == "1") {
   			var giveRow = $('<tr></tr>');
   			
   			$("#cartGive tbody").empty();
   			$('<td colspan="3" style="text-align:center; font-size:10px;"></td>')
   				.html("There are no giving opportunities<br>in your basket.")
   				.appendTo(giveRow);
   			giveRow.appendTo('#cartGive');
   		}
   		
   			
   		
   		
		$("#loadingCart").hide();
		$("#cartTables").show();
    }
    
    
    
    
    
    
    
 (function($){
 $.fn.constrainInput = function(config){
    var settings = $.extend({
       allowedCharsRegex: ".*"
    }, config);
       var re = new RegExp(settings.allowedCharsRegex);
    $.each(this, function(){
      var input = $(this);
      
      var keypressEvent = function(e){
        e= e || window.event;
        var k = e.charCode || e.keyCode || e.which;
        if(e.ctrlKey || e.altKey || k == 8){//Ignore
          return true;
        } else if ((k >= 41 && k <= 122) ||k == 32 || k > 186){//typeable characters
          return (re.test(String.fromCharCode(k)));
        }
        return false;
      }
      input.bind("keypress",keypressEvent);
    });
    return this;
  };
})(jQuery);