

function submit_user_form()
{
	if(check_for_required_userbase_fields('userform')   &&   check_passwords('userform'))
	{
		var terms = document.getElementById("agreetoterms");
		if(terms)
		{
			if(terms.checked)
				document.getElementById('userform').submit();
			else
				alert("");
		}
		else
		{
			document.getElementById('userform').submit();
		}
	}
	else { return false; }
}

function check_and_submit_form(form_id)
{
	if(check_for_required_fields(form_id))
	{
		document.getElementById(form_id).submit();
	}
	else { return false; }
}

function check_for_required_fields(form_id)
{
	var onlyinputs = document.getElementById(form_id).getElementsByTagName('input');
	var selects = document.getElementById(form_id).getElementsByTagName('select');
	var textareas = document.getElementById(form_id).getElementsByTagName('textarea');
	var inputs = new Array;
	var i = 0;
	for(i = 0; i < onlyinputs.length; i++)
	{
		inputs[i] = onlyinputs[i];
	}
	var j = 0;
	for(j = 0; j < selects.length; j++)
	{
		inputs[i + j] = selects[j];
	}
	var k = 0;
	for(k = 0; k < textareas.length; k++)
	{
		inputs[i + j + k] = textareas[k];
	}

	var items_missing = 0;
	var email_format_incorrect = 0;
	var radios = new Object;
	var radios_checked = new Object;
	var unchecked_radio = '';

	for(i = 0; i < inputs.length; i++)
	{
		if(inputs[i].type == 'radio')
		{
			radios[inputs[i].name] = 1;
			if(inputs[i].checked)
				radios_checked[inputs[i].name] = 1;
		}
		if(inputs[i].className.indexOf('required') != -1   &&   (inputs[i].value == '' || inputs[i].value == undefined))
		{
			inputs[i].style.background	= '#ffdd00';
			inputs[i].style.color		= '#000';
			items_missing = 1;
		}
		else if(inputs[i].className.indexOf('emailformat') != -1   &&   !inputs[i].value.match( /.+@.+\..+/ ))
		{
			inputs[i].style.background	= '#ffdd00';
			inputs[i].style.color		= '#000';
			email_format_incorrect = 1;
		}
		else
		{
			inputs[i].style.background	= inputs[i].type == 'radio' || inputs[i].type == 'checkbox' || inputs[i].type == 'button' || inputs[i].type == 'submit' ? 'transparent' : '#ffffff';
			inputs[i].style.color		= '#000';
		}
	}

	for (var j in radios)
	{
		if(!radios_checked[j])
			unchecked_radio = j;
	}

	if(items_missing)
	{
		alert("Please fill in the required item(s).");
	}
	else if(email_format_incorrect)
	{
		alert("Please enter a valid email address.");
	}
	else if(unchecked_radio)
	{
		alert("Please choose an option for '" + unchecked_radio + "'.");
	}
	else
	{
		return 1;
	}

	return 0;
}

function focus_username_field()
{
	if(document.getElementById("ubun"))
	{
		document.getElementById("ubun").focus();
	}
}

function show_hide_rows()
{
	var MSIE = navigator.userAgent.indexOf("MSIE") == -1 ? 0 : 1; // IE doesn't support table-row...
	var Enabled = MSIE  ? 'block' : 'table-row';

	if(document.getElementById("ub_datatype").value == 'varchar')
		document.getElementById("ub_fieldmax_row").style.display = Enabled;
	else
		document.getElementById("ub_fieldmax_row").style.display = 'none';

	var fieldtype = document.getElementById("ub_fieldtype").value;

	var mandatory_row = document.getElementById("ub_mandatory_row").style;
	var limitallowedchars_row = document.getElementById("ub_limitallowedchars_row").style;
	var allowedchars_row = document.getElementById("ub_allowedchars_row").style;
	var allowedcharsmsg_row = document.getElementById("ub_allowedcharsmsg_row").style;
	var listitems_row = document.getElementById("ub_listitems_row").style;

	if(fieldtype.indexOf('freeform') != -1)
	{
		mandatory_row.display = Enabled;
		limitallowedchars_row.display = Enabled;
		allowedchars_row.display = Enabled;
		allowedcharsmsg_row.display = Enabled;
		listitems_row.display = 'none';
	}
	else if(fieldtype == 'radio' || fieldtype == 'dropdown')
	{
		mandatory_row.display = Enabled;
		limitallowedchars_row.display = 'none';
		allowedchars_row.display = 'none';
		allowedcharsmsg_row.display = 'none';
		listitems_row.display = Enabled;
	}
	else if(fieldtype == 'checkbox')
	{
		mandatory_row.display = 'none';
		limitallowedchars_row.display = 'none';
		allowedchars_row.display = 'none';
		allowedcharsmsg_row.display = 'none';
		listitems_row.display = 'none';
	}
}

function set_cookie(name, value, hours_to_live, path, domain, secure)
{
	var expireDate = "";
	if(hours_to_live)
	{
		expireDate = (new Date((new Date()).getTime() + hours_to_live*3600000)).toGMTString();
	}

	var curCookie = name + "=" + escape(value) +
	((hours_to_live) ? "; expires=" + expireDate : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	document.cookie = curCookie;
}

function get_cookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function delete_cookie(name, path, domain)
{
	if(get_cookie(name))
	{
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
	else
	{
		alert('No such cookie.');
	}
}

function add_to_cart(wishlistid,itemid,showalerts,amount)
{
	var set_it = 1;
	var cart = get_cookie('selected_items');

	var newitem = wishlistid + '-' + itemid;
	var newitem_with_amount = newitem;
	if(amount && amount.match(/^\d+(\.\d+)?$/))
		newitem_with_amount += ':' + amount;

	if(cart)
	{
		//if(cart.indexOf(',' + newitem + ',') != -1)

		var re = new RegExp('(^|,)' + newitem + '(,|:|$)');
		if(cart.match(re))
		{
			//if(!showalerts || window.confirm('This item is already in your cart.  Add it again?')) // can't use JS's confirm() nor prompt() in a frame in IE.
			if(!showalerts)
			{
				set_it = 1;
			}
			else
			{
				set_it = 0;
			}
		}

		cart = cart + ',' + newitem_with_amount;
	}
	else
	{
		cart = newitem_with_amount;
	}

	if(set_it)
	{
		set_cookie('selected_items', cart, 168, '/');
		if(showalerts)
			alert('Item added to cart.');
	}
}

function remove_from_cart(wishlistid,itemid,showalerts)
{
	var set_it = 1;
	var cart = get_cookie('selected_items');
	var newitem = wishlistid + '-' + itemid;
	if(cart)
	{
		// in Javascript, regexes created with "new RegExp" -- but not via foo = /bar/ -- must
		// have all backslashes escaped.  So printing that from Perl means double-escaping.
		var re = new RegExp('(^|,)' + newitem + '(:[\\d\\.]+)?(,|$)');
		if(cart.match(re))
		{
			//if(!showalerts || window.confirm('Are you sure you want to remove this item from your cart?')) // can't use JS's confirm() nor prompt() in a frame in IE.
			if(!showalerts)
			{
				cart = cart.replace(re, ',');
				cart = cart.replace(/,{2,}/, ',');
				cart = cart.replace(/^,/, '');
				cart = cart.replace(/,$/, '');
				set_cookie('selected_items', cart, 168, '/');
			}
		}
	}
}

function update_quantities()
{
	var i = 1;
	var qty_orig	= document.getElementById('enc_qty_' + i + '_original');
	var qty_new	= document.getElementById('enc_qty_' + i + '_new');

	while(qty_orig)
	{
		if(qty_new.value != qty_orig.value)
		{
			var wishlistid	= document.getElementById('enc_qty_' + i + '_wishlistid').value;
			var itemid	= document.getElementById('enc_qty_' + i + '_itemid').value;

			set_quantity(wishlistid,itemid,qty_new.value);
		}

		i++;
		qty_orig = document.getElementById('enc_qty_' + i + '_original');
		qty_new  = document.getElementById('enc_qty_' + i + '_new');
	}

	location.reload();
}

function set_quantity(wishlistid,itemid,qty_new)
{
	var qty_in_cart = quantity_in_cart(wishlistid,itemid);
	if(qty_new > qty_in_cart)
	{
		while(qty_new > qty_in_cart)
		{
			add_to_cart(wishlistid,itemid,0);
			qty_in_cart = quantity_in_cart(wishlistid,itemid);
		}
	}
	else if(qty_new < qty_in_cart)
	{
		while(qty_new < qty_in_cart)
		{
			remove_from_cart(wishlistid,itemid,0);
			qty_in_cart = quantity_in_cart(wishlistid,itemid);
		}
	}
}

function quantity_in_cart(wishlistid,itemid)
{
	var cart = get_cookie('selected_items');
	var newitem = wishlistid + '-' + itemid;
	var quantity = 0;
	if(cart)
	{
		var items = cart.split(',');
		for(i = 0; i < items.length; i++)
		{
			items[i] = items[i].replace(/:[\d\.]+/,''); // remove any price that might be included.
			if(items[i] == newitem)
			{
				quantity++;
			}
		}
	}
	return quantity;
}

function swap_ecard_image(newimage)
{
	var ecard = document.getElementById("ecardimage");
	if(ecard.src.match(/\/$/))
		ecard.src += newimage;
	else
		ecard.src = ecard.src.replace(/[^\/]+$/, newimage);
	scroll(0,0);
}

function submit_ecard_form(action)
{
	if(action == 'send')
		document.getElementById('ecard-send-flag').value = 1;
	document.getElementById('makeecard').submit();
}

function calc_redemption(whichform)
{
	var fee_percent = 10;
	var amount_requested = document.getElementById('amountrequested' + whichform).value;
	amount_requested = amount_requested.replace(/\$/, '');

	var amount_available = document.getElementById('unredeemed_profit').value;
	amount_available = amount_available/1;

	if(amount_requested > amount_available)
	{
		amount_requested = amount_available;
		document.getElementById('amountrequested' + whichform).value = amount_available;
	}

	var fee_amount = amount_requested * (fee_percent / 100);
	var fee_amount_twodecimals = Math.round(fee_amount*100)/100;
	if(!fee_amount_twodecimals.toString().match(/\./))
		fee_amount_twodecimals = fee_amount_twodecimals + '.00'; // add decimal and zeros.
	if(fee_amount_twodecimals.toString().match(/\.\d$/))
		fee_amount_twodecimals = fee_amount_twodecimals + '0'; // add a single zero.
	document.getElementById('processingfee' + whichform).value = '$' + fee_amount_twodecimals;

	var amount_received = amount_requested - fee_amount;
	var amount_received_twodecimals = Math.round(amount_received*100)/100;
	if(!amount_received_twodecimals.toString().match(/\./))
		amount_received_twodecimals = amount_received_twodecimals + '.00'; // add decimal and zeros.
	if(amount_received_twodecimals.toString().match(/\.\d$/))
		amount_received_twodecimals = amount_received_twodecimals + '0'; // add a single zero.
	document.getElementById('amountreceived' + whichform).value = '$' + amount_received_twodecimals;
}

function schedule_onload_action(newfunc)
{
	var already_scheduled = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = newfunc;
	}
	else
	{
		window.onload = function()
		{
			already_scheduled();
			newfunc();
		}
	}
}

// Disabled because it's causing a "Can't execute code
// from a freed script" error in IE7...
//
//schedule_onload_action(focus_username_field);

