$(document).ready(function(){

	$('#addevent').click( function(){
		
		if( $('.eventdates li.selected').length == 0 ){
			alert($('#alertmsg').html());	
		} else {
			addtobasket( $('.eventdates li.selected').attr('objectid') );
		}
		
	});

	$('.events li').click( function(){
	
		$('.events li.selected').removeClass("selected");
	
		$(this).addClass("selected");
		
		loadeventdates( $(this).attr('objectid') );
	
	});

	$('#basket').load('/ajrgfx/basket/add.asp')

	if( $('#orderform').length > 0 ){
	
		initformcookie(document.forms['orderform']);

		if( $('#usealtdeliveryaddress').is(':checked') ){
			$('#deliveryaddress').show();
		}
	
	}
	
});

function loadeventdates( documentid ){

	$('#eventdates').load('/ajrgfx/ajax/eventdates.asp?documentid=' + documentid, function(){

		$('.eventdates li').click( function(){
	
			$('.eventdates li.selected').removeClass("selected");
	
			$(this).addClass("selected");
	
		});
		
	});

}

function addtobasket(documentid){

	$.ajax({
	    type: 'GET',
		url: '/ajrgfx/basket/add.asp?productid='+documentid,
		success: function(data) {

			$('#basket').load('/ajrgfx/basket/add.asp');
				
			var html ='';
				
			html += '<div id="basketalert" style="position: absolute; z-index: 999; top: 50%; left: 50%; width: 250px;">';
			html += '  <div style="position: relative; left: -125px; text-align: center; border: 1px solid #999; padding: 10px; background-color: #d4edf9; font-family: arial; font-size: 16px; line-height: 20px">';
			html += '    The selected event has been<br/>added to your event basket';
			html += '  </div>';
			html += '</div>';
				
//			$('body').after( html );
				
//			setTimeout( function(){ 
//				$('#basketalert').fadeOut()	
//			}, 3000 ); 

			 
		}
	});
	
}

function updateitemquantity(index,quantity){

	if( isNaN(quantity) ){

		document.location=document.location;

	} else {
	
		$.ajax({
		    type: 'GET',
			url: '/ajrgfx/basket/update.asp?index='+index+'&quantity='+quantity,
			success: function(data) {
				
				document.location=document.location;
			 
			}
		});	
	
	}

}

function removebasketitem(index){

	$.ajax({
	    type: 'GET',
		url: '/ajrgfx/basket/update.asp?index='+index+'&quantity=0',
		success: function(data) {
			
			$('#basket').load('/ajrgfx/basket/add.asp');
		 
		}
	});	

}

function setbuymoreurl(){

	$.cookie('platon.buymoreurl', location.href)
}

function getbuymoreurl(){
	
	if($.cookie('platon.buymoreurl')){
		document.location = $.cookie('platon.buymoreurl')
	} else {
		document.location = "/"
	}
	
}

function toggledeliveryaddress(me){

	if(me.checked){
	
		me.value = 1;
		
		$('#deliveryaddress').show();
	
	} else {
	
		me.value = 0;
		
		$('#deliveryaddress').hide();
	
	}
	
}


function validateorderform(me){

	$(me).find('input.alert').each( function(){
	
		$(this).css('border','none');
		$(this).removeClass('alert')
	
	});
	
	var doSubmit = true;

	$(me).find('input.required').each( function(){
	
		if( $(this).attr('type') == 'checkbox' ){
		
			if( ! $(this).attr('checked') ){
			
				alert( $(this).attr('title') );
				
				doSubmit = false;
			
			}
		
		}
		
		if( $(this).attr('class').indexOf('email') > 0 ){
		
			if( !emailCheck( $(this).val() ) ){

				$(this).css('border','1px solid red');
				$(this).addClass('alert')
			
				doSubmit = false;
			
			}
		
		} else {
		
			if ( $(this).val() == '' ){
		
				$(this).css('border','1px solid red');
				$(this).addClass('alert')
				
				doSubmit = false;
		
			}
			
		}
		
	});

	$('.alert').focus( function(){
		$(this).css('border','none');
		$(this).removeClass('alert');
	})
	
	if ( doSubmit ){ me.submit() }

}

