/*function adjustWidth(obj, credits)	
{
	if (document.getElementById(obj).checked)
		$('#bar').width($('#bar').width() + (credits*0.75));
	else
		$('#bar').width($('#bar').width() - (credits*0.75));
}*/
function adjustWidth(thesub, credits)
{
	var bg = $('#sub_' + thesub).css('background-image');
	var width = 233*(credits/500);
	
	if (bg != 'none') {
		credits += existing
		width += 233*(credits/500);
	}
	else
	{
		credits = 
		width -= 233*(credits/500);	
	}
	if (width > 233)
		width = 233;
	$('#bar').width(width);
}


/*function setWidth(credits)
{
	$('#bar').width(credits*0.75);	
}
*/
function setWidth(credits)
{
	var width = 233*(credits/500);
	if (width > 233)
		width = 233;
	if (width < 0)
		width = 0;
	$('#bar').width(width);	
}

function setFormField(obj, val) {
	var newValue = val;
	document.getElementById(obj).value = newValue;
}

function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
}


function setBoxStatus(obj) {
	if (document.getElementById(obj).checked) {
		document.getElementById(obj).checked = false;
	}
	else {
		document.getElementById(obj).checked = true;
	}
}

function showPriceRows(country) 
{
	if (country != "ALL")
	{
		$("#base_price").show();
		$("#production_cost").show();
		$("#retail_price").show();
	}
	else
	{
		$("#base_price").hide();
		$("#production_cost").hide();
		$("#retail_price").hide();	
	}
}

/*
 * Loads a page dynamically using Ajax. Will also initialise Thickbox.
 *
 * @var container The container (div) into which the results will get loaded.
 * @var url The url of the page to be used to load the content.
 * @var vars An associative array with key/value pairs.
 *
 */
function loadPage(container, url, vars)
{
	var queryString = "?";
	// dynamically create the query string to be parsed by "url"...
	for(var key in vars)
		queryString += key + "=" + vars[key] + "&";
	
	$("#loader").show();
	$("#" + container).hide();
	// fetch the content...
	$("#" + container).load('../ajax/' + url + '.php' + queryString, function() {
		$("#loader").hide();
		$("#" + container).fadeIn("slow");
		tb_init('a.thickbox, area.thickbox, input.thickbox');
	});	
}


function compareFields (val1, val2)
{
	return val1 == val2;
}


function updateCredits(checkObj, obj, val) 
{
	if (document.getElementById(checkObj).checked) 
	{
		$("#" + obj).val(parseInt($("#" + obj).val()) + val);
	}
	else
	{
		$("#" + obj).val(parseInt($("#" + obj).val()) - val);
	}
}

function loadNewsCategories(container, className, name, include_all) 
{	
	$("#" + container).load('../components/news_categories.php?class=' + className + '&name=' + name + '&include_all=' + include_all, function() {
		$("#" + container).show();
	});
}


function loadData(dropDown, rule, ruleData, className) 
{
	$("#" + dropDown).load('../components/schedule_rules.php?rule=' + rule + '&ruleData=' + ruleData + '&className=' + className, function() {
		$("#loading_dropdown").hide();
		$("#" + dropDown).fadeIn("fast");
	});		
}


function loadComponent(loader, container, url, vars)
{
	var queryString = "?";
	// dynamically create the query string to be parsed by "url"...
	for(var key in vars)
		queryString += key + "=" + vars[key] + "&";
		
	$("#" + loader).show();
	$("#" + container).hide();
	$("#" + container).load('../components/' + url + '.php' + queryString, function() {
		$("#" + loader).hide();
		$("#" + container).fadeIn("fast");
	});
}


function joinCafe(user_id, cafe_id) 
{	
	$("#cafe" + cafe_id).fadeOut("slow");
	$("#cafe" + cafe_id).load('../cafes/join_cafe.php?user_id=' + user_id + "&cafe_id=" + cafe_id, function() {
		$("#cafe" + cafe_id).fadeIn("slow");
	});
}


function changeNewsPublish(news_id, publish) 
{	
	$("#news" + news_id).fadeOut("slow");
	$("#news" + news_id).load('../news/change_publish.php?news_id=' + news_id + "&publish=" + publish, function() {
		$("#news" + news_id).fadeIn("slow");
	});
}


function addItem(available_id, selected_id)
{
    return !$('#'+available_id+' option:selected').remove().appendTo('#'+selected_id);
}


function removeItem(available_id, selected_id)
{
    return !$('#'+selected_id+' option:selected').remove().appendTo('#'+available_id);
}


function selectAll(selected_id)
{
    $('#'+selected_id+' option').each(function(i) {  
         $(this).attr("selected", "selected");  
    });
}

//error handling with Json
function handleReturn(json)
{
    if (json.code > 0)
    {
        showError(json.message);
        return false;
    }else if (json.code != -1)
    {
        showSuccess(json.message);
        return true;
    }else return true;    
}

function showError(message)
{
    showStatusMessage('global_error',message);
}

function showSuccess(message)
{
    showStatusMessage('global_success',message);
}

function showStatusMessage(div_id, message)
{
    $("#"+div_id+"_message").html(message);
    $("#"+div_id).slideDown("fast", function(){
        setTimeout(function() { $("#"+div_id).slideUp("fast"); } ,6000); }        
    );
}

function addToCart(product_type, product_id, product_quantity)
{
    $.post("../shopping_cart/ajaxaddtocart.php", {type : product_type, id : product_id, quantity : product_quantity}, function(data)    
    {         
       if(handleReturn(data))
       {
           loadCart();
       }
    }, "json");
}

function addEventToCart(event_id)
{
    event_attendees = $("#attendees").val();
    $.post("../shopping_cart/ajaxaddtocart.php", {type : 'E', id : event_id, quantity : event_attendees}, function(data)    
    {     
       document.location = "book_event.php?event_id="+event_id+"&code="+data.code+"&message="+escape(data.message);                   
    }, "json");
}

function loadCart()
{
    $("#cart_panel").load("../shopping_cart/ajaxpanelcart.php");
}

function fetchAreaTerm(country_id)
{
	$("#area_term").load("../components/fetch_area_term.php?country_id=" + country_id);	
}

function hideCountry()
{
    if ($("#product_global:checked").val()=="yes") $("#country_tb").attr('class','invisible');
    else $("#country_tb").attr('class','');    
}