// AidArena Donating
// By: David Rugendyke


// Loads the multiselect box with charities + searching
function charitiesSelLoad(){
	
	var searchText = $('searchField').get('value');
	var charSel = $('charityId').get('value');

	var query = 'do=loadCharitiesSelect&search='+searchText+'&charSel='+charSel;
	new Request({ url: 'donate_ajax.php', method: 'post', data: query, onRequest: loader.detect(),
			   onComplete: function(resp) { 
			   		//alert(resp);
			   		$('charitySelDiv').set('html', resp);
					// Preselect the charity and load its items if we are visiting this page again
					if(charSel) {
						appealsSelLoad();
						charitiesSelLoadData();
						appealsHighlight();
					}
					// Attach an event to load data about the charity	
					$('charitySel').addEvent('click', function() {
						charitiesSelLoadData();
						appealsSelLoad();
					});
					loader.detect();
				}}).send();  

}

// Loads the multiselect box with appeals
function appealsSelLoad(){
	
	var charId = $('charitySel').get('value');
	var appSel = $('charityAppeal').get('value');

	var query = 'do=loadAppealsSelect&id='+charId+'&appSel='+appSel;
	new Request({ url: 'donate_ajax.php', method: 'post', data: query, onRequest: loader.detect(),
			   onComplete: function(resp) { 
			   		$('appealsSelDiv').set('html', resp);
					// Auto select the first one
					appealsHighlight();
					// Hightlight the appeal when clicked
					$('appealsSel').addEvent('click', function() {
							appealsHighlight();
					});
					loader.detect();
				}}).send();  

}

// Hightlights an appeal when clicked
function appealsHighlight(){
	
	var appId = $('appealsSel').get('value');
	$('charityAppeal').set('value', appId);
	
	if(!appId || appId == '0') {
		if($('charityBio')) {
			$('charityBio').setStyle('background-color', '#FDFFDD');
		}
	}

	
	if(appId) {
		$$('.appealRow').each(function(el) {
				if('appeal_'+appId == el.get('id')) { 
					el.setStyle('background-color', '#FDFFDD'); 	
					$('charityBio').setStyle('background-color', '');
				}else{
					el.setStyle('background-color', ''); 	
				}
			});
	}

}

// Loads the information about a charity when clicked in the multiselect box
function charitiesSelLoadData(){
	
	var charId = $('charitySel').get('value');
	
	if(charId) { 
		$('form_next_step').set('disabled', false);
		
		var query = 'do=loadCharitiesData&id='+charId;
		new Request({ url: 'donate_ajax.php', method: 'post', data: query, onRequest: loader.detect(),
				   onComplete: function(resp) { 
						$('donateCharityBlock').set('html', resp);
						$('charityId').set('value', charId);
						appealsHighlight();
						loader.detect();
					}}).send();  
	}else{
		$('form_next_step').set('disabled', true);
	}

}


// Loads the multiselect box with the parent categories + searching
function categoriesLoad(){
	
	var catParentId = $('cat_parent_id').get('value');

	var searchText = $('searchField').get('value');
	
	var type = $('donateType').get('value');
	
	$('searchSubField').set('disabled', true);

	var query = 'do=loadCategoriesParentSelect&pid=0&catParentId='+catParentId+'&search='+searchText+'&type='+type;
	new Request({ url: 'donate_ajax.php', method: 'post', data: query, onRequest: loader.detect(),
			   onComplete: function(resp) { 
			   		//alert(resp);
			   		$('catSelDiv').set('html', resp);
					
					// Automatically load a child cat list if the parent id already exists in the page
					if(catParentId) {
						// Enable the sub search field
						$('searchSubField').set('disabled', false);
						categoriesSubLoad(catParentId);
						// Disable the next button
						$('form_next_step').set('disabled', true);
					}
							
					// Attach an event to load data about the child cats	
					$('catSel').addEvent('click', function() {
														   
							var pid = $('catSel').get('value');
							// Enable the sub search field
							$('searchSubField').set('disabled', false);
							// Load the sub-categories
							categoriesSubLoad(pid);
							// Set the category ID
							$('category_id').set('value', pid);
							// Disable the next button
							$('form_next_step').set('disabled', true);
					});

					loader.detect();
				}}).send();  
}


// Categories Sub multiselect loader
function categoriesSubLoad(pid){
	
	var cat_id = $('category_id').get('value');
	var searchText = $('searchSubField').get('value');
	
	if(!pid) { var pid = $('catSel').get('value'); }
	
	var type = $('donateType').get('value');
	
	if(pid) {
	
		$('form_next_step').set('disabled', true);
		
		var query = 'do=loadCategoriesSubSelect&pid='+pid+'&cat_id='+cat_id+'&search='+searchText+'&type='+type;
		new Request({ url: 'donate_ajax.php', method: 'post', data: query, onRequest: loader.detect(),
				   onComplete: function(resp) { 
						//alert(resp);
						$('catSubSelDiv').set('html', resp);
						
						if(cat_id) {
							$('form_next_step').set('disabled', false);
						}
						
						// If there were no sub-categories, enable the next button
						if(resp.test("No matching", "i")) {
							$('form_next_step').set('disabled', false);
						}
						
						// Attach an event to enable the next button
						$('catSubSel').addEvent('click', function() {
																  
								var subId = $('catSubSel').get('value');
								
								if(subId) {
									// Set the category ID
									$('category_id').set('value', subId);
									$('form_next_step').set('disabled', false);
								}
	
						});
						
						loader.detect();
					}}).send();  
		
	}
}


// Min reservce price check
function reserveCheck(){
	
	var val = parseFloat($('reserve_price').get('value'));

	if(val < 9 || val == 'NaN') {
		alert('The reserve is $9 minimum');
		$('reserve_price').set('value', '9.00')
	}

}

// Show / Hide time fields in settings
function timeToggle(el, show){
	
	var display = $(el).getStyle('display');

	if(show == 'block') {
		$(el).setStyle('display', 'none');
	}else{
		$(el).setStyle('display', 'block');
	}
}

// The type of shipping method from a drop down - settings page
function shippingMethodPriceCalc() {
	
	var shipMethod = $('shipMethodSelect').get('value');
	var ausPostCost = 7.25;

	if(shipMethod == 'Courier') {
		$('courier').setStyle('display', 'block');
		$('ausPost').setStyle('display', 'none');	
		$('ausPostTip').setStyle('display', 'none');
	}
	
	if(shipMethod == 'Registered Post' || shipMethod == 'Standard Post') {
		$('courier').setStyle('display', 'none');
		$('ausPost').setStyle('display', 'block');				
		//alert('You\'ve selected Australia Post, please note this is an additional $'+ausPostCost);

		var shipLocalVal = parseFloat($('shippingCostLocal').get('value'));
		if(isNaN(shipLocalVal)) {	shipLocalVal = 0;	}
		$('shippingCostLocal').set('value', shipLocalVal+ausPostCost);
		
		var shipIntVal = parseFloat($('shippingCostInterstate').get('value'));
		if(isNaN(shipIntVal)) {	shipIntVal = 0;	}
		$('shippingCostInterstate').set('value', shipIntVal+ausPostCost);
		
		$('ausPostTip').setStyle('display', 'block');
	}
}

// Various show/hide options for the shipping settings
function buyerShow() {
					
	$$('.shipRow').each(function(el) {
			el.setStyle('display', 'block');
			el.setStyle('display', 'table-row');
		});
		
}

function pickupShow() {
					
	$$('.shipRow').each(function(el) {
			el.setStyle('display', 'none');
		});
}

function paypalShow() {
	
	$$('.shipRow').each(function(el) {
			el.setStyle('display', 'block');
			el.setStyle('display', 'table-row');
		});	
}

function donorShow() {
	
	$$('.shipRow').each(function(el) {
			el.setStyle('display', 'none');
		});
}









