/**
 * @author richardmerchant
 */

var productOptionAmounts = [];
var quantity;
var vatRate = parseFloat(20.0);

// event/product
var productOption = 0;
var productQuantity = 0;

$(function(){
	
	// update the mini basket
	if( $('#mini_basket_container').length > 0 )
	{
		getMiniBasket();
	}
	
	// add click function to use company details / contact details
	$('#company_info').click(function(){
		getCompanyInfo(this);
	});
	
	$('#contact_info').click(function(){
		getContactInfo(this);
	});
	
	$('#delegate_details_button').click(function(){
		getUserDelegateDetails();
	});
	
	$('#booker_details_button').click(function(){
		getUserBookerDetails();
	});
	
	$('#use_contact').click(function(){
		getFullContactInfo(this);
	});
	
	$('.product_option').find('select').change(function(){
//		$('.product_option :selected').each(function(index){
//			eventOption = parseInt($(this).val());
			checkButtonState();
//		});
	});

	$('.select_option').find('select').change(function(){
//		$('#quantity :selected').each(function(index){
//			eventQuantity = parseInt($(this).val());
			checkButtonState();
//		});
	});
	
	$('.product_option').find('input').change(function(){
		
		$('.product_option :checked').each(function(index){
			productOptionAmounts[index] = parseFloat( $(this).attr('rel') );
		});
		 
		quantity = $("select#quantity").val();
		var price = totalPrice(quantity);
		$(".price").text(price.toFixed(2));
	});
	
	if(	$('.product_option').length > 0 )
	{
		$('.product_option :selected').each(function(index){
			productOptionAmounts[index] = parseFloat( $(this).attr('rel') );
		});
		
		var price = totalPrice();
		$(".price").text(price.toFixed(2));
	}
	
	
	// check if message box is pressent
	if( $('#message_box').length > 0 )
	{
		setTimeout('$("#message_box").fadeOut("slow")', 10000);
		
		//when the close button at right corner of the message box is clicked
		$('#close_message').click(function()
		{
		  //the messagebox gets scrool down with top property and gets hidden with zero opacity
		  $('#message_box').animate({opacity:0 }, "slow");
		});
	}
})

function checkButtonState( productOption , productQuantity)
{
	var productOption = $('select.productOptions').val();
	var productQuantity = $('select#quantity').val();

	if(productOption > 0 && productQuantity > 0)
		$('.addToBasket').removeAttr("disabled");
	else
		$('.addToBasket').attr('disabled', 'disabled');
}

function totalPrice(quantity){
	
	if(quantity == 0)
		quantity = 1;
		
	if(productOptionAmounts.length < 1)
		return;
	
	var total = 0;
	
	for ( var i = 0; i < productOptionAmounts.length; i++)
	{
		
		total = total + productOptionAmounts[i];
	}
	
	return total * quantity;
}


function excludeVat(val)
{
	var newVal = ( 100 / ( 100 + vatRate ) ) * val;
	
	return newVal;
}

function submitProductionForm(formId)
{
	// check that the production form in not empty
	var numOfEle = 0;
	var errors = 0;
	
	$(".product_option :selected, .product_option :checked").each(function(){
		var optionValue = $(this).val();
		++numOfEle;
		if(optionValue == '0' || optionValue == '')
		{
			++errors;	
		}
	});
	
		if( numOfEle == errors )
		{
			$('.warning').text('You must select an event option').show(500);
		}
		else
		{
			$('.warning').hide(500).text('');
			$('#' + formId).submit();
		}

}

function showForm(formId)
{
//	$('#booker_details').show(200);
	$('#' + formId).show();
	$('.continue').hide();
	
}

function submitForm(id)
{
	$('#' + id).submit();
}

function getCompanyInfo(index)
{
	var checked = $(index).attr('checked');
	
	if(checked)
	{
		var data = new Object();
		data.func 		= 'getUserCompanyDetails';
		var dataString = $.toJSON(data);
		
		// get the user contact details
		$.post('/ajax.php', {
				data: dataString
			}, function(data){
				if (data.length > 0) {
					eval("var userObj=" + data);
					
					if(userObj.status.error == true)
					{
						$('.company_name_error').text(userObj.error);
						$('.company_name_error').fadeIn('1000', function(){
							setTimeout('$(".company_name_error").fadeOut("slow")', 3000);
						});
					}
					else
					{
						$("#company_name").val(userObj.companyname);
						$("#address_line1").val(userObj.addressline1);
						$("#address_line2").val(userObj.addressline2);
						$("#town").val(userObj.town);
						$("#county").val(userObj.county);
						$("#postcode").val(userObj.postcode);
						$("#country").val(userObj.country);
					}
				}
			});
	}
	else
	{
		$('#company_contact > p > input').each(function(){
			$(this).val("");
		})
	}
} 

function getContactInfo(index)
{
	var checked = $(index).attr('checked');
	
	if(checked)
	{
		var data = new Object();
		data.func 		= 'getUserContactDetails';
		var dataString = $.toJSON(data);
	
		// get the user contact details
		$.post('/ajax.php', {
				data: dataString
			}, function(data){
				if (data.length > 0) {
					eval("var userObj=" + data);
					
					if(userObj.status.error == true)
					{
						$('.contact_name_error').text(userObj.error);
						$('.contact_name_error').fadeIn('1000', function(){
							setTimeout('$(".contact_name_error").fadeOut("slow")', 3000);
						});
					}
					else
					{
						$("#contact_name").val(userObj.contactname);
						$("#contact_email").val(userObj.contactemail);
						$("#contact_tel").val(userObj.contacttel);
					}
				}
			});
	}
	else
	{
		$('#user_contact > p > input').each(function(){
			$(this).val("");
		})
	}
	
} 

function same_address()
{
	elem = $('#sameaddress');
	
	if (!$(elem).attr('checked')) 
	{
		$('#delivery_address').slideDown();
	}
	else
	{
		$('#delivery_address').slideUp();
	}
} 
