

function alert(a, width, height)
{
	// a = a.replace(/\n/g, '<br/>');
	
	if ( !width ) width = 400;
	if ( !height ) height = 200;
	
	$('#dialog').html('');
	$('#dialog').html(a);
	$('#dialog').dialog({width: width, height: height});
	$('#dialog').dialog('open');
}


function DeliveryAddress_copyBillingAddress()
{
	$('input', '#checkout_deliveryAddress').each(function()
	{
		var n = $(this).attr('id').replace(/da_/, '');
		$(this).val( $('#' + n).val() );
	});
	
	$('select', '#checkout_deliveryAddress').each(function()
	{
		var n = $(this).attr('id').replace(/da_/, '');
		$(this).val( $('#' + n).val() );
	});
	$('#da_country_cc').trigger('change');
	checkRequiredFields();
}








function setRequiredFields()
{
	// append a asterisk to the required fields labels
	$('body#front .required').each(function()
	{
		if ( $('img', $(this)).length == 0 )
		{
			$(this).append('<img src="/img/forms/required.gif" alt="Required Field" style="margin:0 0 5px 5px;" />');
		}
	});
	
}


function unsetRequiredFields(fieldset)
{
	$('label.required img', '#' + fieldset).remove();
}






// pop up help tip

function showTip(what)
{
	switch (what)
	{
		case 'orientation':
			a = $.get('/inc/layout/html/tip.orientation.html', function(a){alert(a, 560, 460);});
			break;
		case 'position':
			a = $.get('/inc/layout/html/tip.position.html', function(a){alert(a, 560, 460);});
			break;
		default:
			alert('error. no content.');
			break;
	}
}





// checkout page / admin create order

function checkRequiredFields()
{
	$('.checkoutBlock').each(function()
	{
		var id = $(this).attr('id');
		var ok = true;

		$('.required', $(this)).each(function()
		{
			eid = $('#' + $(this).attr('for')).attr('id');
			// console.log( 'checking: ' +  eid);
			ev = $('#' + $(this).attr('for')).val();
			// console.log('val: ' + ev);
			if ( ev == '' )
			{
				// console.log(' : EMPTY');
				ok = false;
			}
		});
		if ( ok == true)
		{
			$('#' + id.replace(/checkout_/, 'head_') + ' span').removeClass('fieldsBad').addClass('fieldsGood');
		}
		else
		{
			$('#' + id.replace(/checkout_/, 'head_') + ' span').removeClass('fieldsGood').addClass('fieldsBad');
		}
	});
}


function createAccountOption(me)
{
	if ( $(me).attr('checked') )
	{
		$('#account_username_label, #account_password_label').addClass('required');
		$('#username, #password').attr('disabled', false);
		setRequiredFields();
	}
	else
	{
		unsetRequiredFields('checkout_loginDetails');
		$('#account_username_label, #account_password_label').removeClass('required');
		$('#username, #password').val('').attr('disabled', 'disabled');
	}
}









function roundNumber(num, dec) 
{
    return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}





