$.fn.confirmEntry = function() {
    var confirmHtml = this;
    this.after( $confirmHtml );
    
    return this;
};

function minimumPrice() {
	if(parseInt($("input[name=estimated_value]").val()) < 10 && $("select[name=item_category]").val() != "Books"){
    		alert("Sorry, items require a minimum value of $10. Consider combining multiple smaller items into a single bundle!");
            $("input[name=estimated_value]").val(null);
    }
}

function confirmEmail() {
	
}

function clearConfirmEmail() {
	
}

function clearCustomerInformation() {$('input').val("");}

$(document).ready(function(){
/*
	// Smooth scroll to anchors
	$("a[href^='#']").click(function() {
    target = $(this).attr('href');
      $([document.documentElement, document.body]).animate({
          scrollTop: $( target ).offset().top
      }, 2000);
	});
*/
	// Add external link icon to external links
	$('.pageMainContent a').filter(function() {
   		return this.hostname && this.hostname !== location.hostname;
	}).not('a:has(img)').append("<span class='fa externalLinkIcon'>&nbsp;</span>");
    
    $('label input[type=radio]').next('input[type=text]').on('click',function(event){
    	$(this).prev('input[type=radio]').prop("checked", true).change();
    });

	$('body#auction.submit #estimated_value').on('input',function(event){
        if(parseInt($(this).val()) >= 50 && parseInt($(this).val()) < 100) {
            $("input[name=item_split][value='75/25']").prop('disabled', false);
            $("input[name=item_split][value='50/50']").prop('disabled', true);
            if($("input[name=item_split][value='50/50']").prop('checked')){
            	$("input[name=item_split][value='75/25']").prop('checked', true);
            }

        } else if(parseInt($(this).val()) >= 100){
        	$("input[name=item_split][value='75/25']").prop('disabled', false);
            $("input[name=item_split][value='50/50']").prop('disabled', false);

        } else{
            $("input[name=item_split][value='75/25']").prop('disabled', true);
            $("input[name=item_split][value='50/50']").prop('disabled', true);
            if($("input[name=item_split][value='Full Donation']").prop('checked') == false){
            	$("input[name=item_split][value='Full Donation']").prop('checked', true);
            }

        }
    });
    
    $('body#auction.submit input[name=item_split]').on('input',function(event){
     	if($("input[name=item_split][value='Full Donation']").prop('checked')) {
        	$("input#auction_item_reserve").prop('disabled', true);
            $("input#auction_item_reserve").val("");
        } else {
        	$("input#auction_item_reserve").prop('disabled', false);
        }
     });
    
     $('body#auction.submit #estimated_value').on('change',function(event){minimumPrice();});
     
	$("body#auction.submit select[name=item_category]").on('change',function(event){minimumPrice();});
    
    // Stuff for the Support Us page //
    
    if($('body').hasClass('support-us-now')){
    	$.getScript('/assets/js/jquery-ui.min-v1.12.1.js', function(){
            $( 'input[name=price][type=radio]' ).change( function(){
                if($( 'input#membership_sustainer' ).is(':checked')) {
                    $( 'div#sustainingMemberMessage' ).show({effect: 'blind', queue: false});
                    $( 'label[for=sustainingCheckbox]' ).animate({backgroundColor: 'rgb(194, 241, 252)'},{duration: 1000, queue: false});
                } else {
                    $( 'div#sustainingMemberMessage' ).hide( 'blind' );
                    $( 'label[for=sustainingCheckbox]' ).animate({backgroundColor: 'transparent'},{duration: 1000, queue: false});
                }
            } );
        }); 
    }
    
    // Stuff for Your Information checkout page //
    
    if($('body').is('#your-information')){
    	$( 'input#confirm_email_address, input#email_address' ).after('<p class="error emailConfirmError" for="email_address" style="display: none;">Email addresses dont match.</p>');
		$( 'input#confirm_email_address, input#email_address' ).on('blur', function(){
        	if( $( 'input#confirm_email_address' ).val() != $( 'input#email_address' ).val() ) {
    			console.log("Email addresses don't match");
    			$( 'input#confirm_email_address, input#email_address' ).css("box-shadow", "0 0 1.5px 1px red");
        		$('.emailConfirmError').show();
        
    		} else {
    			$( 'input#confirm_email_address, input#email_address' ).css("box-shadow", "none");
    			$('.emailConfirmError').hide();
    		}
        }).on('input', function(){
        	$( 'input#confirm_email_address, input#email_address' ).css("box-shadow", "none");
    		$('.emailConfirmError').hide();
        }); 
    }
    
   
    if($('body').hasClass('work-study')){
    $(' .workstudySessionsContainer .workstudySessionDetails ').hide();
    
    $(' button.wsDetails ').show();

	// Statically set the button width to whatever it current is so that the size doesn't change when the content changes
    $(' button.wsDetails ').width( $(' button.wsDetails ').width() );
    
    $(' button.wsDetails ').click(function() {
    	workstudySession = "workstudySession" + $(this).attr("-data-workstudy-session");
    	if($(".workstudySessionDetails." + workstudySession).is(':visible')) {
        $(".workstudySessionDetails." + workstudySession).hide({effect: 'blind', queue: false});
        $(this).html('<i class="fa fa-caret-down"></i>&nbsp;Show Details');
        } else {
    	$(".workstudySessionDetails." + workstudySession).show({effect: 'blind', queue: false});
        $(this).html('<i class="fa fa-caret-up"></i>&nbsp;Hide Details');
        }
    });
    }
    
    

   if($("body").hasClass("ProjectPage")){
   var minimumBidFloat = parseFloat($("input[name=minimum_bid]").val().replace(/[^0-9-.]/g, ''));
   
   $('body#student-built-projects.ProjectPage input#bid').on('change',function(event){

        if($('body#student-built-projects.ProjectPage input#bid').val() < minimumBidFloat){
     		alert("The minimum bid for this item is $" + minimumBidFloat);
            $('body#student-built-projects.ProjectPage input#bid').val(null);
        }
     });
   }
   
  
   if($("body").is("#programs.fjord-horse-experience-two-hour-pleasure-driving")){
   //Fjord Horse Peek booking button
   
   (function(config) {
   window._peekConfig = config || {};
   var idPrefix = 'peek-book-button';
   var id = idPrefix+'-js'; if (document.getElementById(id)) return;
   var head = document.getElementsByTagName('head')[0];
   var el = document.createElement('script'); el.id = id;
   var date = new Date; var stamp = date.getMonth()+"-"+date.getDate();    
   var basePath = "https://js.peek.com";
   el.src = basePath + "/widget_button.js?ts="+stamp;
   head.appendChild(el); id = idPrefix+'-css'; el = document.createElement('link'); el.id = id;
   el.href = basePath + "/widget_button.css?ts="+stamp;
   el.rel="stylesheet"; el.type="text/css"; head.appendChild(el);
   })({key: 'a2e08001-5dbc-439c-aa53-d1920157d425'});
   }
   
   
if($("body").is("#programs.daily-sailing")){
  (function(config) {
    window._peekConfig = config || {};
    var idPrefix = 'peek-book-button';
    var id = idPrefix+'-js'; if (document.getElementById(id)) return;
    var head = document.getElementsByTagName('head')[0];
    var el = document.createElement('script'); el.id = id;
    var date = new Date; var stamp = date.getMonth()+"-"+date.getDate();
    var basePath = "https://js.peek.com";
    el.src = basePath + "/widget_button.js?ts="+stamp;
    head.appendChild(el); id = idPrefix+'-css'; el = document.createElement('link'); el.id = id;
    el.href = basePath + "/widget_button.css?ts="+stamp;
    el.rel="stylesheet"; el.type="text/css"; head.appendChild(el);
  })({key: 'a2e08001-5dbc-439c-aa53-d1920157d425'});
 }
});

$(window).on('unload',function(event){
	if($("body").is("#auction.submit")){
        	console.log("Bye");
            if (typeof(Storage) !== "undefined") {
            	// Save donor info for next submission
                console.log("Saving info");
                sessionStorage.setItem("first_name", $("#first_name").val());
                sessionStorage.setItem("last_name", $("#last_name").val());
                sessionStorage.setItem("phone", $("#phone").val());
                sessionStorage.setItem("email", $("#email").val());
                sessionStorage.setItem("address", $("#address").val());
                sessionStorage.setItem("address_2", $("#address_2").val());
                sessionStorage.setItem("city", $("#city").val());
                sessionStorage.setItem("state", $("#state").val());
                sessionStorage.setItem("zip", $("#zip").val());
            }
      }
});

$(window).on('load',function(event){
	if($("body").is("#auction.submit")){
        	console.log("Hi!");
            // Retrieve donor info from last submission
            console.log("Retrieving info");
            if (sessionStorage.getItem("first_name")) {$("#first_name").val(sessionStorage.getItem("first_name"))};
           	if (sessionStorage.getItem("last_name")) {$("#last_name").val(sessionStorage.getItem("last_name"))};
            if (sessionStorage.getItem("phone")) {$("#phone").val(sessionStorage.getItem("phone"))};
            if (sessionStorage.getItem("email")) {$("#email").val(sessionStorage.getItem("email"))};
            if (sessionStorage.getItem("address")) {$("#address").val(sessionStorage.getItem("address"))};
            if (sessionStorage.getItem("address_2")) {$("#address_2").val(sessionStorage.getItem("address_2"))};
            if (sessionStorage.getItem("city")) {$("#city").val(sessionStorage.getItem("city"))};
            if (sessionStorage.getItem("state")) {$("#state").val(sessionStorage.getItem("state"))};
            if (sessionStorage.getItem("zip")) {$("#zip").val(sessionStorage.getItem("zip"))};
            
            }
        });

