/////////////////////////////////////////////////////////////
// Non-page specific utility functions
function byId(id)
{
	return document.getElementById(id);
}

function checkAll(form_name)
{
	for (var i = 0; i < byId(form_name).elements.length;i++)
	{
		var e = byId(form_name).elements[i];
		if ((e.name != 'check_all') && (e.type=='checkbox'))
		{
			e.checked = byId(form_name).check_all.checked;
		}
		
	}
	
}

function switchDivDisplay(showDiv)
{
	var divs = document.getElementsByTagName("div");
	for (i = 0; i < divs.length; i++)
	{
		if(divs[i].id.substring(0, 11) == 'tab_display')
		{
			divs[i].style.display = 'none';
		}
		
	}
	if(byId('tab_display' + showDiv) != null)
	{
		byId('tab_display' + showDiv).style.display = 'block';
	}
	return false;
}

function divDisplay(condition, divId)
{
	if(condition)
	{
		byId(divId).style.display = 'block';
	}
	else
	{
		byId(divId).style.display = 'none';
	}
	
}

function formConfirmAction(formName, action, message)
{
	if(confirm(message))
	{
		byId('action').value = action;
		byId(formName).submit();
		return true;
	}
	return false;
}

function formConfirmExport(formName, action, exportURL, message)
{
	if(confirm(message))
	{
		if(action == 'export')
		{
			var id_string = "0";
			for (var i = 0; i < byId(formName).elements.length;i++)
			{
				var e = byId(formName).elements[i];
				if ((e.name != 'check_all') && (e.type=='checkbox'))
				{
					if(e.checked == 1)
					{
						id_string += "," + e.value;
					}
					
				}
				
			}
			location.href = exportURL + "?ids=" + id_string;
			return true;
		}
		else
		{
			byId(formName).submit();
			return true;
		}
		
	}
	return false;
}

//////////////////////////////////////////////////////////////
// Page specific utility functions

function applyPromoCode()
{
	document.getElementById("action").value = "apply_discount_code";
	document.getElementById("form_payment_update").submit();
	return false;
}

function paymentUpdate()
{
	if(document.getElementById('new_payment_type').checked == 1)
	{
		if(document.getElementById('cc_num').value == '')
		{
			alert('You must fill in the credit card number field in order to continue.');
			document.getElementById('cc_num').focus();
			return false;
		}
		if(document.getElementById('cc_code').value == '')
		{
			alert('You must fill in the credit card code field in order to continue.');
			document.getElementById('cc_code').focus();
			return false;
		}
		document.getElementById('form_payment_update').submit();
	}
	else
	{
		document.getElementById('form_payment_update').submit();
	}
	return false;
}

function set_action(action)
{
	document.getElementById("action").value = action;	
	document.getElementById("form_view_cart").submit();
}

function checkShippingMethodUpdate()
{
	var thisform = document.getElementById('form_shipping_method_update');
	
	var myOption = -1;
	var firstTime = 0;
	var i=thisform.method.length-1;
	
	for (; i > -1; i--) 
	{
		firstTime = 1;
		if (thisform.method[i].checked) 
		{
			myOption = i; 
			i = -1;
		}
		
	}
	if(firstTime == 0 && document.getElementById('method').checked)
	{
		myOption = 0;
	}
	if (myOption == -1) 
	{
		alert('You must select a shipping method in order to continue.');
		return false;
	}
	
	document.getElementById('form_shipping_method_update').submit();
	return true;
}

function checkContact()
{
	if(document.getElementById('Email').value == '')
	{
		alert('You must fill in the email field in order to continue.');
		document.getElementById('Email').focus();
		return false;
	}
	document.getElementById('form_contact').submit();
	return true;
}

function checkAccountUpdate()
{
	if(document.getElementById('first_name').value == '')
	{
		alert('You must fill in the first name field in order to continue.');
		document.getElementById('first_name').focus();
		return false;
	}
	if(document.getElementById('last_name').value == '')
	{
		alert('You must fill in the last name field in order to continue.');
		document.getElementById('last_name').focus();
		return false;
	}
	if(document.getElementById('email').value == '')
	{
		alert('You must fill in the email field in order to continue.');
		document.getElementById('email').focus();
		return false;
	}
	document.getElementById('form_account_update').submit();
	return true;
}

function checkBillingAddressUpdate()
{
	if(document.getElementById('billing_first_name').value == "")
	{
		alert("You must enter a first name in order to continue");
		document.getElementById('billing_first_name').focus();
		return false;
	}
	
	if(document.getElementById('billing_last_name').value == "")
	{
		alert("You must enter a last name in order to continue");
		document.getElementById('billing_last_name').focus();
		return false;
	}
	
	if(document.getElementById('billing_address1').value == "")
	{
		alert("You must enter an address in order to continue");
		document.getElementById('billing_address1').focus();
		return false;
	}
	
	if(document.getElementById('billing_city').value == "")
	{
		alert("You must enter a city in order to continue");
		document.getElementById('billing_city').focus();
		return false;
	}
	
	if(document.getElementById('billing_zip').value == "")
	{
		alert("You must enter a zip in order to continue");
		document.getElementById('billing_zip').focus();
		return false;
	}
	
	document.getElementById("form_billing_address_update").submit();
	
	return true;
}

function checkShippingAddressUpdate()
{
	if(document.getElementById('shipping_first_name').value == "")
	{
		alert("You must enter a first name in order to continue");
		document.getElementById('shipping_first_name').focus();
		return false;
	}
	
	if(document.getElementById('shipping_last_name').value == "")
	{
		alert("You must enter a last name in order to continue");
		document.getElementById('shipping_last_name').focus();
		return false;
	}
	
	if(document.getElementById('shipping_address1').value == "")
	{
		alert("You must enter an address in order to continue");
		document.getElementById('shipping_address1').focus();
		return false;
	}
	
	if(document.getElementById('shipping_city').value == "")
	{
		alert("You must enter a city in order to continue");
		document.getElementById('shipping_city').focus();
		return false;
	}
	
	if(document.getElementById('shipping_zip').value == "")
	{
		alert("You must enter a zip in order to continue");
		document.getElementById('shipping_zip').focus();
		return false;
	}
	
	document.getElementById("form_shipping_address_update").submit();
	
	return true;
}


function checkNewsletter()
{
	if(document.getElementById('first_name').value == "")
	{
		alert("You must enter a first name in order to continue");
		document.getElementById('first_name').focus();
		return false;
	}
	
	if(document.getElementById('last_name').value == "")
	{
		alert("You must enter a last name in order to continue");
		document.getElementById('last_name').focus();
		return false;
	}
	
	if(document.getElementById('email').value == "")
	{
		alert("You must enter an email address in order to continue");
		document.getElementById('email').focus();
		return false;
	}
	
	document.getElementById("form_newsletter").submit();
	
	return true;
}

function checkEventSignup()
{
	if(document.getElementById('first_name').value == '')
	{
		alert('You must fill in the first name field in order to continue.');
		document.getElementById('first_name').focus();
		return false;
	}
	if(document.getElementById('last_name').value == '')
	{
		alert('You must fill in the last name field in order to continue.');
		document.getElementById('last_name').focus();
		return false;
	}
	if(document.getElementById('email').value == '')
	{
		alert('You must fill in the email field in order to continue.');
		document.getElementById('email').focus();
		return false;
	}
	if(document.getElementById('address').value == '')
	{
		alert('You must fill in the address field in order to continue.');
		document.getElementById('address').focus();
		return false;
	}
	if(document.getElementById('city').value == '')
	{
		alert('You must fill in the city field in order to continue.');
		document.getElementById('city').focus();
		return false;
	}
	if(document.getElementById('state').value == '')
	{
		alert('You must select a state in order to continue.');
		document.getElementById('state').focus();
		return false;
	}
	if(document.getElementById('zip').value == '')
	{
		alert('You must fill in the zip field in order to continue.');
		document.getElementById('zip').focus();
		return false;
	}
	if(document.getElementById('phone').value == '')
	{
		alert('You must fill in the phone field in order to continue.');
		document.getElementById('phone').focus();
		return false;
	}
	try
	{
		if(document.getElementById('store_id').value == '')
		{
			alert('You must select an event location in order to continue.');
			document.getElementById('store_id').focus();
			return false;
		}
	}
	catch(e){}
	try
	{
		if(document.getElementById('rules').checked != 1)
		{
			alert('You must agree to the sweepstakes rules in order to continue.');
			document.getElementById('rules').focus();
			return false;
		}
	}
	catch(e){}
	document.getElementById('form_signup').submit();
	return true;
}
