function trim(str)
{
	return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function deleteConfirm(message)
{
	if(confirm("Do you really want to delete "+message+"?"))
	{
		return true;
	}
	
	return false;
}

function roundDecimals(number, decimals)
{
	var numberStr = String(number);
	
	if(numberStr.indexOf(".") > 0)
	{
		var result1 = number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return padWithZeros(result3, decimals);
	}
	
	return number;
}

function padWithZeros(rounded_value, decimal_places)
{
	var value_string = rounded_value.toString();
	var decimal_location = value_string.indexOf(".");
	
	if (decimal_location == -1)
	{
		decimal_part_length = 0;
		value_string += decimal_places > 0 ? "." : "";
	}
	else
		decimal_part_length = value_string.length - decimal_location - 1;
	
	var pad_total = decimal_places - decimal_part_length;
	
	if (pad_total > 0)
		for (var counter = 1; counter <= pad_total; counter++) 
			value_string += "0";
	
	return value_string;
}

function removeElement(id)
{
	element = document.getElementById(id);
	element.parentNode.removeChild(element);
	return element;
}

function clearInnerHTML(obj)
{
	while(obj.firstChild)
		obj.removeChild(obj.firstChild);
}

function checkEmailFormat(email)
{
	var s = '[A-Za-z0-9][-\\w]*(\\.[A-Za-z0-9][-\\w]*)*';
	var re = new RegExp('^'+s+'@'+s+'\\.[a-zA-Z]{2,4}$');
	email = email.replace(new RegExp('/\(.*?\)/'), '' );
	return re.test(email);
}

function focusInput(inputElement, defaultText)
{
	if(trim(inputElement.value) == defaultText)
	{
		inputElement.value = "";
		inputElement.style.color = "#000000";
	}
}

function blurInput(inputElement, defaultText)
{
	if(trim(inputElement.value) == defaultText || trim(inputElement.value) == "")
	{
		inputElement.value = defaultText;
		inputElement.style.color = "#999999";
	}
}

function focusPassword()
{
	document.getElementById('frm_password_title').style.display = "none";
	document.getElementById('password').style.display = "inline";
	document.getElementById('password').focus();
}

function blurPassword(passwordElement)
{
	if(trim(passwordElement.value) == "")
	{
		passwordElement.value = "";
		passwordElement.style.display = "none";
		document.getElementById('frm_password_title').style.display = "inline";
	}
}

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where.toLowerCase())
		{
			case 'beforebegin':
				this.parentNode.insertBefore(parsedNode,this)
				break;
			case 'afterbegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeend':
				this.appendChild(parsedNode);
				break;
			case 'afterend':
				if (this.nextSibling) 
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else
					this.parentNode.appendChild(parsedNode);
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}

	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}


function checkIfTaxExempt(selectedValue)
{
	var error = 1;
	var goodVals = new Array('Church', 'School', 'Parks and Recreation department', 'College or University', 'Government (Non-Military)', 'Military', 'Youth Sport');
	var msg = "Sorry, the organization you selected can not be selected as tax exempt.";
	
	for(i=0;i<goodVals.length;i++)
	{
		if(selectedValue == goodVals[i])
		{
			error = 0;
		}
	}
	
	if(error == 1)
	{
		alert(msg);
		document.checkout_shipping.frm_indiana_exempt_0.checked = true;
	}
	else
	{
		alert('Please enter your exempt number in the space provided.  Remember to fax a copy of your certificate to 812-634-2036.  If you continue to have problems please call us at 800-264-4519 and we will be happy to help');
		document.checkout_shipping.frm_tax_exemption_number.disabled = false;
	}

	return false;
}

function changeOrgRadio()
{
	document.checkout_shipping.frm_indiana_exempt_0.checked = true;
	document.checkout_shipping.frm_tax_exemption_number.disabled = true;
	return;
}

function checkStatus(status, id)
{
	var elementName = 'frm_product_related_'+id;
	if(status == "i")
	{
		alert('You can not add this item as a related item because it is either inactive or discontinued');
		document.getElementById(elementName).checked = false;
		return false;
	}
	else
	{
		return true;
	}
}
function validateAddCard()
{
	if(document.add_card.frm_cc_name.value == '')
	{
		alert('Please enter the name on the card');
		return false;
	}

	if(document.add_card.frm_cc_type.value == '')
	{
		alert('Please choose the type of card you are using');
		return false;
	}

	if(document.add_card.frm_cc_number.value == '')
	{
		alert('Please enter a valid card number');
		return false;
	}
	
	if(document.add_card.frm_Month.value == '' || document.add_card.frm_Year.value == '')
	{
		alert('Please enter a valid exipriation date');
		return false;
	}

	if(document.add_card.frm_cc_ccv.value == '' || document.add_card.frm_cc_ccv.value.length < 3)
	{
		alert('Please enter a valid CCV number');
		return false;
	}

	return true;
}