/**
 * AJAX Javascript
 *
 * Page to initiate all AJAX functions
 *
 * @author gUncle Development [http://www.guncle.com]
 * @version 12/29/2011
 */


/**
 * Removes gift idea
 * @param id = id of wishlist item
 * @param user = id of user tied to wishlist item
 */
function wishlistRemove( id,user ) {
	if( confirm("Do you really want to remove this gift idea?") ) {
		$.ajax({
			type: "POST",
			url: "/ajax_wishlist.php",
			data: ({
				action: "remove",
				id: id,
				user: user
			}),
			cache: false,
			dataType: "text",
			success: function(response){
				$("#gift_ideas").html(response);
			}
		});
	}
}


/**
 * Marks gift as procured
 * @param id = id of procured gift
 * @param user = id of user tied to wishlist item
 */
function giftProcured( id,user ) {
	$.ajax({
		type: "POST",
		url: "/ajax_wishlist.php",
		data: ({
			action: "procure",
			id: id,
			user: user
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			$("#gift_ideas").html(response);
		}
	});
}


/**
 * Unmark a gift as procured
 * @param id = id of wishlist item
 * @param user = id of user tied to wishlist item
 */
function removeProcured( id,user ) {
	$.ajax({
		type: "POST",
		url: "/ajax_wishlist.php",
		data: ({
			action: "unprocure",
			id: id,
			user: user
		}),
		cache: false,
		dataType: "text",
		success: function(response){
			$("#gift_ideas").html(response);
		}
	});
}


/**
 * Toggles upload form
 * @param user = current user being viewed
 * @param id = id of wishlist item
 */
function showUploadForm( user,id ) {
	if( id == '' ) {
		$("#gift-upload").attr('src','wishlist_upload.php?user='+user);
		$("#gift-upload").show();
	}
	else {
		$("#gift-upload-"+id).attr('src','wishlist_upload.php?user='+user+'&id='+id);
		$("#wishlist-"+id).show();
		$("#gift-upload-"+id).show();
	}
}
function hideUploadForm( id ) {
	if( id == '' ) {
		window.parent.document.getElementById('gift-upload').style.display = 'none';
		window.parent.document.getElementById('gift-upload').src = 'blank.htm';
	}
	else {
		window.parent.document.getElementById('gift-upload-' + id).style.display = 'none';
		window.parent.document.getElementById('wishlist-' + id).style.display = 'block';
		window.parent.document.getElementById('gift-upload-' + id).src = 'blank.htm';
	}
}

/***********************************************************
sizingForm()
displays / hides sizing form
***********************************************************/
function showSizingForm( user ) {
	document.getElementById('sizing-form').src = 'sizing_update.php?user=' + user;
	document.getElementById('sizing-form').style.display = 'block';
}
function hideSizingForm() {
	window.parent.document.getElementById('sizing-form').style.display = 'none';
	window.parent.document.getElementById('sizing-form').src = 'blank.htm';
}


/***********************************************************
Element()
displays / hides element
***********************************************************/
function showElement(element) {
	if( document.getElementById(element) ) {
		document.getElementById(element).style.display='block';
	}
}

function hideElement(element) {
	if( document.getElementById(element) ) {
		document.getElementById(element).style.display='none';
	}
}


/***********************************************************
* Processing()
* displays / hides processing spinner
***********************************************************/
function showProcessing(id) {
	var processing = '<img src="/images/ajax_spinner.gif" width="16" height="16" alt="Processing" /> Processing...';
	if( id > 0 ) {
		document.getElementById('processing'+id).innerHTML = processing;
	}
	else {
		document.getElementById('processing').innerHTML = processing;
	}
}

/***********************************************************
* wishlist sort functions
***********************************************************/
function saveSortOrder(order) {
	$.ajax({
		type: "POST",
		url: "/ajax_wishlist.php",
		data: ({
			action: "sort",
			order: order
		}),
		cache: false,
		dataType: "text"
	});
}
