// global vars
// add_entry
	var total_cats = 0;
	var total_price = 0;
//	var cat_price = 50; // moving this to the overall html template -- sounds like the best idea to me
	
	var store_items = new Array('certificate', 'certificate_framed', 'statue');
	var store_prices = new Array(15, 30, 175);
	
function add_entry_toggle(cat_div) {
	var found_input = false;
	check = cat_div.firstChild;
	while(found_input == false) {
		if(check && check.nodeName == 'INPUT'){
			 found_input = true;
			break;
		}
		check = check.nextSibling;
	}
	
	if(check.checked == true) { // unchecking the box
		check.checked = false;
		
		Element.removeClassName(cat_div, 'add_entry-category_selected');
		
		total_cats--;
		add_entry_update_totals();
	} else { // checking the box
		check.checked = true;
		
		Element.addClassName(cat_div, 'add_entry-category_selected');
		
		total_cats++;
		add_entry_update_totals()
	}
}

function add_entry_update_totals() {
	total_price = total_cats * cat_price;
	$('total_cats').innerHTML = total_cats;
	$('total_price').innerHTML = '$' + total_price.toFixed(2);
}

function judgeEntry(input) {
	value = input.value;
	if(input.value == '') value = 'NULL';
	var found_container = false;
	var check = input.parentNode;
	while(found_container == false) {
		check = check.parentNode;
		if(Element.hasClassName(check, 'entries-entry_div')) found_container = true;
	}
	
	input.setAttribute('autocomplete','off');
	
	entry_id = check.id.substring(6);
	
	success = function() {
		Element.addClassName(input, 'saved');
	}
	
	new Ajax.Request('handler.php', {method:'post', postBody:'form_action=judge_entry&entry_id=' + entry_id + '&score=' + value, onSuccess:success});
}

function store_update_subtotal(input, price) {
	var tmp = input.name.split('_');
	var id = tmp[0];
	var total = 0;
	for (var i=0; i<store_items.length; i++) {
		item = store_items[i];
		item_price = store_prices[i];
		var count = $F(id + '_' + item);
		
		if(item == 'certificate' && count > 0) count--; // one free certificate
		
		total = total + (count * item_price);
	}
	
	$(id + '_subtotal').innerHTML = total;
	
}

function setEntryStatus(input) {
	value = input.value;
	var found_container = false;
	var check = input.parentNode;
	while(found_container == false) {
		check = check.parentNode;
		if(Element.hasClassName(check, 'entries-entry_div')) found_container = true;
	}
	entry_id = check.id.substring(6);
	
	success = function() {
		Element.addClassName(input, 'saved');
	}
	
	new Ajax.Request('handler.php', {method:'post', postBody:'form_action=set_entry_status&entry_id=' + entry_id + '&status=' + value, onSuccess:success});
	
}

/* this function must be updated to toggle paid each entry w/in an order, not the order itself. probably needs renamed, because i don't know what's happening w/ product orders at this point. i think they are individual too */
function togglePaid(obj) {
	order_id = obj.id.substr(5);
	
	var success = function(resp) {
		if(resp.responseText == 1) {
			obj.src = obj.src.replace('_0', '_1');
		} else {
			obj.src = obj.src.replace('_1', '_0');			
		}
	}
	
	new Ajax.Request('handler.php', {method:'post', postBody:'form_action=toggle_paid&order_id=' + order_id, onSuccess:success});
}

function toggleLinkPaid(obj, linktype) {
	link_id = obj.id.substr(5);
	
	var success = function(resp) {
		if(resp.responseText == 1) {
			obj.src = obj.src.replace('_0', '_1');
		} else {
			obj.src = obj.src.replace('_1', '_0');			
		}
	}
	
	new Ajax.Request('handler.php', {method:'post', postBody:'form_action=toggle_' + linktype + '_paid&link_id=' + link_id, onSuccess:success});
}

function orders_toggleDetails(obj) {
	var found_parent = false;
	var parent = obj;
	while(found_parent == false) {
		parent = parent.parentNode;
		if(Element.hasClassName(parent, 'orders-order')) found_parent = true;
	}
	
	
	Element.toggle(document.getElementsByClassName('orders-details', parent)[0]);
}

function orders_export_checkboxes(select_or_deselect) {
	$$('form#product_orders input[type=checkbox]').each(function(input){
		input.checked = (select_or_deselect == 'select');
	});
}

function users_toggleDetails(obj) {
	var found_parent = false;
	var parent = obj;
	while(found_parent == false) {
		parent = parent.parentNode;
		if(Element.hasClassName(parent, 'users-user')) found_parent = true;
	}
	
	
	Element.toggle(document.getElementsByClassName('users-details', parent)[0]);
}

function saveItemName() {
	new Ajax.Request('item_handler.php', {method:'post', postBody:'form_action=item_rename&item_id=' + $F('item_id') + '&item_name=' + $F('name_edit_value'), onSuccess:saveItemNameSuccess});
}

function saveItemNameSuccess() {
	$('name_value').innerHTML = $F('name_edit_value');
	Element.show('name');
	Element.hide('name_edit');
}

/* credit card stuff GPL from http://openscroll.org/hacks/CreditCard/creditCard.txt */
function typeOfCard(number) {
	/* 
	//	Card Prefixes
	//
	//	Mastercard	51-55
	//	Visa		4
	//	AmEx		34,37
	//	Discover	6011
	*/

	var firstNumber = number.substring(0,1);
	var firstThreeNumbers = number.substring(0,3);

	if (firstNumber == 4) {
		return "VISA";
	} 

	var firstTwoNumbers = number.substring(0,2);
	if (firstTwoNumbers > 50 && firstTwoNumbers < 56) {
		return "MASTERCARD";
	}

	if (firstTwoNumbers == 34 || firstTwoNumbers == 37) {
		return "AMEX";
	}

	var firstFourNumbers = number.substring(0,4);
	if (firstFourNumbers == 6011) {
		return "DISCOVER";
	}
}

// Function that determines whether a credit card number is valid
// Please note that a valid credit card number is not essentially a
// credit card in good standing.
function isValidCreditCard(number) {
	var total = 0;
	var flag = 0;
	for (var i=(number.length - 1);i>=0; i--) {
		if (flag == 1) {
			var digits = number.charAt(i) * 2;
			if (digits > 9) digits -= 9;
			total += digits;
//			var reminder = digits % 10;
//			var quotient = (digits - reminder) / 10;
//			total = total + parseInt(reminder);
//			total = total + parseInt(quotient);
			flag = 0;
		} else {
			total = total + parseInt(number.charAt(i));
			flag = 1;
		}
	}
	if ((total%10) == 0) {
		return true;
	} else {
		return false;
	}
}
