var ulShowing,
	liShowing,
	pContentId,
	items;

function showItem(ul, li)
{	
	if(liShowing == null)
	{
		showMenu(ul, li);
	}
	else if(liShowing == li)
	{
		hideMenu(ul, li);
	}
	else
	{
		hideMenu(ulShowing, liShowing);
		showMenu(ul, li);
	}
}

function showMenu(ul, li)
{
	liShowing = li;
	ulShowing = ul;
	$("#" + li).addClass("S");
	$("#" + ul).addClass("S");
}

function hideMenu(ul, li)
{
	liShowing = null;
	ulShowing = null;
	$("#" + li).removeClass("S");
	$("#" + ul).removeClass("S");
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
	{
		return false;
	}

	return true;
}
function ShowCategory(cn)
{
	AjaxLoad("Ajax.aspx?CN=" + cn,"mainBodyContainer");
}

function setItems(itemTotal)
{
	if(typeof(items) == 'undefined')
	{
		items = itemTotal;
	}
}

function updateItems(quantity)
{
	items = items + parseInt(quantity);
	document.getElementById('itemTotal').innerHTML = items;
}

function changeLIBackground(showingID, elementToChange, isAParent, imageID)
{
	if (isAParent !== '') //If the element is a parent
	{
		if (parseInt($('#' + showingID).val(), 10) === 0) //If the element is not showing their children
		{
			$('#' + showingID).val('1'); //Set the tracking input field to 1
			//$('#' + elementToChange).css('list-style-image', 'url(./assets/media/arrowdown.jpg)'); //Change the image of the element
			$('#' + imageID).attr('src', './assets/media/arrowdown.jpg'); //Change the image to a down arrow

			if (showingID.substring(0, 14) === 'menuIsShowing1') //If the element is a first level
			{
				for (var i = 1 ; i <= parseInt($('#level1RowCount').val(), 10) ; i = i + 1) //Loop through all the first levels
				{
					if (showingID !== String('menuIsShowing1' + i) && parseInt($('#menuIsShowing1' + i).val(), 10) === 1) //If any of the first levels are open besides the one clicked
					{
						$('#menuIsShowing1' + i).parent('li').children('ul').children().each(function() //Loop through all the children elements
						{
							if (parseInt($(this).children('input').val(), 10) === 1) //If one of the children elements is open
							{
								$(this).children('input').val('0'); //Change its value to 0 to represent it being closed
								$(this).children().children().children().attr('src', './assets/media/arrow.jpg'); //Change the image to show its closed
								$(this).children('ul').slideToggle(); //Close the list
							}
						});				
						
						$('#menuIsShowing1' + i).val('0'); //Change their value to 0
						$('#menuIsShowing1' + i).parent('li').children().children().children().attr('src', './assets/media/arrow.jpg'); //Change the image back to a regular arrow
						$('#menuIsShowing1' + i).parent('li').children('ul').slideToggle(); //Collapse the menu
					}
				}
			}
			else if (showingID.substring(0, 14) === 'menuIsShowing2') //If the element is a second level
			{
				for (var i = 1 ; i <= parseInt($('#level2RowCount').val(), 10) ; i = i + 1) //Loop through all the second levels
				{
					if (showingID !== String('menuIsShowing2' + i) && parseInt($('#menuIsShowing2' + i).val(), 10) === 1) //If any of the second levels are open besides the one clicked
					{
						$('#menuIsShowing2' + i).val('0'); //Change their value to 0
						$('#menuIsShowing2' + i).parent('li').children().children().children().attr('src', './assets/media/arrow.jpg'); //Change the image back to a regular arrow
						$('#menuIsShowing2' + i).parent('li').children('ul').slideToggle(); //Collapse the menu
					}
				}
			}
		}
		else //If the element is showing children
		{
			$('#' + showingID).val('0'); //Set the trcking input field to 0
			$('#' + imageID).attr('src', './assets/media/arrow.jpg'); //Change the image back to a regular arrow
		}
	}
}

function removeFormatPhone(elementID)
{
	if ($('#' + elementID).val().substring(0, 1) === '(') //If the value begins with the '(' character
	{
		var normalPhone = $('#' + elementID).val().substring(1, 4) + $('#' + elementID).val().substring(6, 9) + $('#' + elementID).val().substring(10, 14); //Remove the formatting
		$('#' + elementID).val(normalPhone); //Put the value back in the element
	}
}

function formatPhone(elementID, errorElementID, country)
{
	if ((country === 'US' || country === 'CA' || country === 'Canada')  && $('#' + elementID).val() !== '' && $('#' + elementID).val().substring(0, 1) !== '(' && $('#' + elementID).val() !== '' &&  parseInt($('#' + elementID).val().length, 10) === 10) //If the element is not blank, and doesn't have a certain set of characters
	{
		var formatedPhone; //Create a variable
		formatedPhone = '(' + $('#' + elementID).val().substring(0, 3) + ') ' + $('#' + elementID).val().substring(3, 6) + '-' + $('#' + elementID).val().substring(6, 10); //Format the number
		$('#' + elementID).val(formatedPhone); //Put the number in the element
		$('#' + errorElementID).html(''); //Remove any error message that may be in the designated error div
	}
	else if (parseInt($('#' + elementID).val().length, 10) !== 10 && $('#' + elementID).val() !== '') //If the element value is not a lenght of 10 or blank
	{
		$('#' + errorElementID).css('color', 'red'); //Make the error div show red text
		$('#' + errorElementID).html(''); //Put in a error message
	}
	else //If the element value is blank
	{
		$('#' + errorElementID).html(''); //Remove any error messages that may have been show in the designated error message div
	}
}

function checkEmail (emailID, errorContainerID)
{
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		str = $('#' + emailID).val();
	
	if (str.match(emailRegEx))
	{
		$('#' + errorContainerID).html('');
		//RegistrationEmail();
	}
	else
	{
		$('#' + errorContainerID).html('Please enter a valid email address');
		$('#' + errorContainerID).css('color', 'red');
		$('#' + errorContainerID).css('text-align', 'center');
		$('#' + errorContainerID).css('font-weight', 'bold');
	}
}

function ConfirmNewsletter() {
	value = $('#footerEmailUpdates').val();
	$('#footerEmailUpdates').attr('class', '');
	$('#footerEmailUpdates').val(value);
	$('#signUpSuccess').css('color', 'green');
	$('#signUpSuccess').css('font-weight', 'bold');
	$('#signUpSuccess').fadeIn(3000);
	$('#signUpSuccess').fadeOut(3000);
}

function generateCreditCardExpirationDate(container)
{
	var myDate = new Date(), //Create a date variable that is taken from the clients PC
		year = myDate.getYear(), //Get the year from the date variable
		selectMonth = '<select id="tbCCEXPMM" name="tbCCEXPMM"><optgroup label="Expiration Month">', //Create the starting of the select element
		selectYear = '<select id="tbCCEXPYYYY" name="tbCCEXPYYYY"><optgroup label="Expiration Year">'; //Create the starting of the select element

	if (parseInt(year) < 1000) //If the year that was retrieved was less than 1000
	{
		year = year + 1900; //Add 1900 to the year
	}

	for (var a = 1 ; a < 13 ; a = a + 1) //Loop through the following code 12 times
	{
		if (a === 1 || a === 2 || a === 3 || a === 4 || a === 5 || a === 6 || a === 7 || a === 8 || a === 9) //If a is either 1, 2, 3, 4,5 ,6 ,7 8, 9 do the following
		{
			selectMonth = selectMonth + '<option value="0' + a + '">0' + a + '</option>'; //Append the next portion to the proper select element
		}
		else //If a is not 1, 2, 3, 4, 5, 6, 7, 8, 9 do the following
		{
			selectMonth = selectMonth + '<option value="' + a + '">' + a + '</option>'; //Append the next portion to the proper select element
		}
	}

	selectMonth = selectMonth + '</optgroup></select>'; //Add the ending of the proper select element

	for (var i = year; i < (year + 7) ; i = i + 1) //Loop through the following 6 times
	{
		selectYear = selectYear + '<option value="' + i + '">' + i + '</option>'; //Append the next portion to the proper select element
	}

	selectYear = selectYear + '</optgroup>'; //Add the ending of the proper select element

	$('#' + container).html(selectMonth + selectYear); //Post the code in the container
	$('#' + container).append('<span class="red" style="margin-left: 9px;">*</span>');
}

$(document).ready(function()
{
	$('#footerEmailUpdates').keypress(function(e)
	{
		if (parseInt(e.which, 10) === 13)
		{
			var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
				str = document.getElementById('footerEmailUpdates').value,
				value;

			if (str.match(emailRegEx))
			{
				
				value = $('#footerEmailUpdates').val();
				url = 'Ajax.aspx?CN=15DA9A428E17&EMAIL=' + value;
				//alert(url);
				AjaxLoad(url, '', 'ConfirmNewsletter();');
			}
			else
			{
				value = $('#footerEmailUpdates').val();
				$('#footerEmailUpdates').attr('class', 'error');
				$('#footerEmailUpdates').val(value);
				$('#signUpSuccess').hide();
			}
		}
	});

	$('#submitFooteremailUpdates').click(function()
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
			str = document.getElementById('footerEmailUpdates').value,
			value;

		if (str.match(emailRegEx))
		{
			value = $('#footerEmailUpdates').val();
			url = 'Ajax.aspx?CN=15DA9A428E17&EMAIL=' + value;
			//alert(url);
			AjaxLoad(url, '', 'ConfirmNewsletter();');
		}
		else
		{
			value = $('#footerEmailUpdates').val();
			$('#footerEmailUpdates').attr('class', 'error');
			$('#footerEmailUpdates').val(value);
			$('#signUpSuccess').hide();
		}
	});
	

	$('.alphaNumeric').alphanumeric({allow:' '}); //Make fields with alphaNumeric class accept only letters and numbers
	$('.qty').numeric(); $('.numeric').numeric(); //Make fields with qty class accept only numeric input
	$('.phone').numeric(); //Make fields with class phone accept only numeric input
	$('.fax').numeric();  //Make fields with class of fax accept only numeric input
	$('#tbPEXT1').numeric(); //Make fields with id of tbPEXT1 accept only numeric input
	$('.alpha').alpha({allow:' '}); //Make the fields with class of alpha accept only text data
	$('.email').alphanumeric({allow:'@.-_'}); //Makes the fields with the class of email accept: a-zA-Z, 0-9, @, -, _, .
	$('.numeric').numeric();

	$('input').each(function ()
	{
		var currentText = $(this).val();

		$(this).change(function ()
		{
			if (($(this).val() !== currentText) && ($(this).val() !== ''))
			{
				$(this).addClass('black');
			}
			else
			{
				$(this).removeClass('black');
			}
		});
	});
});

function noBack()
{
	if (history.length > 0)
	{
		history.go(+1);
	}
}