$(function() {

	// determine current action
	url = location.href;
	splits = url.split('/');
	action = splits[splits.length - 1];

	// prepare info buttons
	init_info();

	// only for show_cart!
	if(action == 'show_cart') {
		// prepare for less_than_page_limit 
		init_less_than_page_limit(); 
	}
});

function init_info() {
	jQuery('.infoButton').click(function() {
		// hide all of the infoDiv's
		jQuery('.shop_info_block').css('visibility', 'hidden');
		
		// get the current id number
		var curId = jQuery(this).attr('id');
		var curIdNum = curId.substr(curId.length -1, curId.length);

		// find the right infoDiv to display
		var infoDiv = jQuery('#infoDiv' + curIdNum);
		infoDiv.css('opacity', '0.0');
		infoDiv.css('visibility', 'visible');
		infoDiv.animate({ opacity: .85, width: '495px' }, 500);
		
		// set it to close itself
		infoDiv.click(function() {
			$(infoDiv).animate({ opacity: 0.0 }, 500, function() {
					$(infoDiv).css('visibility', 'hidden');
			});
	   });
	});
}

function check_captcha() {
	var code = jQuery('#post_captcha').val();
	
	// see if it's right!
	jQuery.post('/securimage/verifyimage.php', 
		   {
			   code: code
		   }, 
		   function(correct) {
				
				if(correct == 1) {
					jQuery('#join_form').submit();
				} else {
					alert('Please check and retype the letters from the image');
				}
				
		   });
	
	return false;	
}

var base_price = 0.00;
var extras_saved = false;
var less_than_page_limit_val = '';
var extra_price_timeout = null;

/* for Yes and No radio buttons */
function init_less_than_page_limit() {
	// save the price
	base_price = parseFloat(jQuery('input[name=AMOUNT]').attr('value'));
	// first, make the Checkout Now button check for less_than_page_limits! 
	jQuery('.payflow_form').submit(function() {
											if(extras_saved == false) {
											   	submit_payflow();
												return false; 
											} else {
												return true; 	
											}
										   });

	jQuery('.paypal_form').submit(function() {
											if(extras_saved == false) {
											   	submit_paypal();
												return false; 
											} else {
												return true; 	
											}
										   });
	
	jQuery('.less_than_page_limit').click(function() {
											  	change_page_limit( jQuery(this).attr('value') );
											  });
	jQuery('.screenplay_extra_pages').keyup(function() { 
												
												clearTimeout(extra_price_timeout);
												extra_price_timeout = setTimeout(
													function() {
														update_extra_price(jQuery('.screenplay_extra_pages').attr('value')); 
													}, 300);
												
													  });
	// unless there's a value greater than zero, hide the screenplay_extra_pages text box 
	var extra_pages_val = jQuery('.screenplay_extra_pages').val(); 
	if(extra_pages_val == '0') {
		hide_div_screenplay_extra_pages(); 
	} else {
		// don't hide, and go ahead and select no
		jQuery('.less_than_page_limit').attr('checked', 'checked');
		change_page_limit('no');
	}
}
// checks for radio button 
// saves extra_pages to Order 
function submit_payflow() {
	var extra_val = jQuery('.screenplay_extra_pages').attr('value');
	if(less_than_page_limit_val == '' || (less_than_page_limit_val == 'no' && extra_val <= 0)) {
		alert('Please answer the page count question - Thank You'); 
		return; 
	}
	update_extra_price(extra_val, function() {
																				 	extras_saved = true; 
																					// changed 01/12/09 for Payflow PRO!
																					//jQuery('.payflow_form').submit();
																					//location.href = 'https://ssl4.westserver.net/script-fix.com/client/store/billing_info?id=' + order_id;
																					// changed 06/04/09 for Payflow PRO!
																					location.href = 'https://www.script-fix.com/client/store/billing_info';
																				 });
	
}
function submit_paypal() {
	var extra_val = jQuery('.screenplay_extra_pages').attr('value');
	if(less_than_page_limit_val == '' || (less_than_page_limit_val == 'no' && extra_val <= 0)) {

		alert('Please answer the page count question - Thank You'); 
		return; 
	}
	update_extra_price(extra_val, function() {
																				 	extras_saved = true; 
																					jQuery('.paypal_form').submit();
																				 });
	
}

function show_div_screenplay_extra_pages() {
	jQuery('.div_screenplay_extra_pages').css('visibility', 'visible');	
}
function hide_div_screenplay_extra_pages() {
	// set value to zero as well! 
	jQuery('.screenplay_extra_pages').attr('value', '0');
	jQuery('.div_screenplay_extra_pages').css('visibility', 'hidden');	
}
function change_page_limit(change) {
	less_than_page_limit_val = change; 
	switch(change) {
		case 'yes': 
			hide_div_screenplay_extra_pages(); 
			update_extra_price('');
			break; 
		case 'no': 
			show_div_screenplay_extra_pages();
			update_extra_price(jQuery('.screenplay_extra_pages').attr('value'));
			break;
	}
}
function update_extra_price(extra_price, callback) {
		if(extra_price == '') extra_price = '0';

		var payflowAmount = jQuery('input[name=AMOUNT]'); 
		var paypalAmount = jQuery('input[name=amount]'); 
		var divPrice = jQuery('.price');
		var moneyNumeric = jQuery('#cart_total');
		
		extra_price = parseInt(extra_price); 
		if(extra_price < 0) return;

		jQuery.ajax({
						type: 'POST', 
						url: 'update_screenplay_extra_pages', 
						data: 'screenplay_extra_pages=' + extra_price, 
						success: function(cur_price) {
							payflowAmount.val(cur_price); 
							paypalAmount.val(cur_price); 

							divPrice.html('Total - $' + cur_price); 
							moneyNumeric.html('$' + cur_price);
							
							if(callback != undefined) {
								callback();
							}
						}
					});
}

function submit_payment_form() {
	var text_boxes = [ 'order_user_billing_first_name', 'order_user_billing_last_name', 'order_user_billing_city', 'order_user_billing_state'];//, 'order_non_user_email' ];
	var messages = ['first name', 'last name', 'city', 'state', 'email'];

	for(var i = 0; i < text_boxes.length; i++) {
		// check for blank text
		var cur_text = jQuery('#' + text_boxes[i]).val(); 
		if(cur_text == '' || cur_text == undefined) {
			alert('Please provide your ' + messages[i]);
			return;
		}
	}

	jQuery('#payment_form').submit(); 
}