/***********************************************************************
 * Used in detail.asp page, to open a new window for 
 * "Email this Item to your Friends" link
 ***********************************************************************/
function emailItem(sQryStr)
{
	var sName = "EmailItem";
	var sFeatures = "width=450,height=500,location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=no,status=yes"
	var sOpen = "/emailItem.asp" + "?" + sQryStr

	window.open(sOpen, sName, sFeatures);
}

/***********************************************************************
 * Used in detail.asp page, to open a new window for 
 * "Ask questions regarding this item" link
 ***********************************************************************/
function emailQuestion(sQryStr)
{
	var sName = "EmailQuestion";
	var sFeatures = "width=450,height=500,location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=no,status=yes"
	var sOpen = "/contact.asp";
	
	if (sQryStr != "")
	 sOpen = sOpen + "?" + sQryStr;

	window.open(sOpen, sName, sFeatures);
}

/***********************************************************************
 * Used in checkout.asp page, when pay type is Check and Money Order
 ***********************************************************************/
function check_onload()
{
	document.printForm.firstName.focus();
}

/***********************************************************************
 * Used in checkout.asp page, valid the form
 ***********************************************************************/
function checkForm(f)
{
	var fName = trimSpace(f.firstName.value);
	var lName = trimSpace(f.lastName.value);
	var address = trimSpace(f.tmpAddress.value);
	var phoneNum = trimSpace(f.phoneNumber.value);
	var email = trimSpace(f.emailAddress.value);
	
	if (fName == "")
	{
		alert("Please enter your first name.");
		f.firstName.focus();
		return false;
	}
	else if (lName == "")
	{
		alert("Please enter your last name.");
		f.lastName.focus();
		return false;
	}
	else if (address == "")
	{
		alert("Please enter your address.");
		f.address.focus();
		return false;
	}
	else if (phoneNum == "")
	{
		alert("Please enter your phone number.");
		f.phoneNumber.focus();
		return false;
	}
	else if (email == "")
	{
		alert("Please enter your email address.");
		f.emailAddress.focus();
		return false;
	}
	else
	{
		if (!checkEmail(email))
		{
			alert("Invalid E-mail Address! Please re-enter.")
			f.emailAddress.focus();
			f.emailAddress.select();
			return false;
		}
	}
	f.address.value = address.replace(/\n/g, '<br>');
	return true;
}

/***********************************************************************
 * Used in emailItem.asp page, valid the form
 ***********************************************************************/
function checkEmailItemForm(f)
{
	var email1 = trimSpace(f.txtEmail_1.value);
	var email2 = trimSpace(f.txtEmail_2.value);
	var email3 = trimSpace(f.txtEmail_3.value);
	var email4 = trimSpace(f.txtEmail_4.value);
	var email5 = trimSpace(f.txtEmail_5.value);
	var yourName = trimSpace(f.txtYourName.value);
	var yourEmail = trimSpace(f.txtYourEmail.value);
	var msg = trimSpace(f.tmp_txtMsgBox.value);
	var itemURL = f.txtItemURL.value;
	var errorMsg = "";
	var bError = false;
	
	if (email1 == "")
	{
		errorMsg = "- Please enter your friend's email address\n";
		if (!bError) f.txtEmail_1.focus();
		bError = true;
	}
	else
	{
		f.txtEmail_1.value = trimSpace(f.txtEmail_1.value);
		f.txtEmail_2.value = trimSpace(f.txtEmail_2.value);
		f.txtEmail_3.value = trimSpace(f.txtEmail_3.value);
		f.txtEmail_4.value = trimSpace(f.txtEmail_4.value);
		f.txtEmail_5.value = trimSpace(f.txtEmail_5.value);
		
		//Validate each non-empty email
		if (!checkEmail(email1))
		{
			errorMsg = "- Please enter a valid email address for your friend #1\n";
			if (!bError)
			{
				f.txtEmail_1.focus();
				f.txtEmail_1.select();
			}
			bError = true;
		}
		if (email2 != "" && !checkEmail(email2))
		{
			errorMsg = errorMsg + "- Please enter a valid email address for your friend #2\n";
			if (!bError)
			{
				f.txtEmail_2.focus();
				f.txtEmail_2.select();
			}
			bError = true;
		}
		if (email3 != "" && !checkEmail(email3))
		{
			errorMsg = errorMsg + "- Please enter a valid email address for your friend #3\n";
			if (!bError)
			{
				f.txtEmail_3.focus();
				f.txtEmail_3.select();
			}
			bError = true;
		}
		if (email4 != "" && !checkEmail(email4))
		{
			errorMsg = errorMsg + "- Please enter a valid email address for your friend #4\n";
			if (!bError)
			{
				f.txtEmail_4.focus();
				f.txtEmail_4.select();
			}
			bError = true;
		}
		if (email5 != "" && !checkEmail(email5))
		{
			errorMsg = errorMsg + "- Please enter a valid email address for your friend #5\n";
			if (!bError)
			{
				f.txtEmail_5.focus();
				f.txtEmail_5.select();
			}
			bError = true;
		}
	}
	
	if (yourName == "")
	{
		errorMsg = errorMsg + "- Please enter you name\n";
		if (!bError) f.txtYourName.focus();
		bError = true;
	}
	else
	{
		f.txtYourName.value = trimSpace(f.txtYourName.value);
	}
	
	if (yourEmail == "")
	{
		errorMsg = errorMsg + "- Please enter your email address\n";
		if (!bError) f.txtYourEmail.focus();
		bError = true;
	}
	else
	{
		f.txtYourEmail.value = trimSpace(f.txtYourEmail.value);
		
		if (!checkEmail(yourEmail))
		{
			errorMsg = errorMsg + "- Please enter your valid email address\n";
			if (!bError)
			{
				f.txtYourEmail.focus();
				f.txtYourEmail.select();
			}
			bError = true;
		}
	}
	
	if (msg == "")
	{
		errorMsg = errorMsg + "- Please enter your message\n";
		if (!bError) f.tmp_txtMsgBox.focus();
		bError = true;
	}
	
	if (bError)
	{
		alert(errorMsg);
		return false;
	}
	else
	{
		f.txtMsgBox.value = msg.replace(/\n/g, '<br>');
		return true;
	}
}

/***********************************************************************
 * Used in emailQuestion.asp page, valid the form
 ***********************************************************************/
function checkEmailQuestionForm(f)
{
	var yourName = trimSpace(f.txtYourName.value);
	var yourEmail = trimSpace(f.txtYourEmail.value);
	var msg = trimSpace(f.tmp_txtMsgBox.value);
	var itemURL = f.txtItemURL.value;
	var errorMsg = "";
	var bError = false;
	
	if (yourName == "")
	{
		errorMsg = "- Please enter you name\n";
		f.txtYourName.focus();
		bError = true;
	}
	else
	{
		f.txtYourName.value = trimSpace(f.txtYourName.value);
	}
	
	if (yourEmail == "")
	{
		errorMsg = errorMsg + "- Please enter your email address\n";
		if (!bError) f.txtYourEmail.focus();
		bError = true;
	}
	else
	{
		f.txtYourEmail.value = trimSpace(f.txtYourEmail.value);
		
		if (!checkEmail(yourEmail))
		{
			errorMsg = errorMsg + "- Please enter your valid email address\n";
			if (!bError)
			{
				f.txtYourEmail.focus();
				f.txtYourEmail.select();
			}
			bError = true;
		}
	}
	
	if (msg == "")
	{
		errorMsg = errorMsg + "- Please enter your message\n";
		if (!bError) f.tmp_txtMsgBox.focus();
		bError = true;
	}
	
	if (bError)
	{
		alert(errorMsg);
		return false;
	}
	else
	{
		f.txtMsgBox.value = msg.replace(/\n/g, '<br>');
		return true;
	}
}

/***********************************************************************
 * Basic Email Validation
 ***********************************************************************/
function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
	{
		return true;
	}
	return false;
}

/***********************************************************************
 * Eliminates any extra leading spaces and trailing spaces 
 ***********************************************************************/
function trimSpace(s) 
{
	//remove leading spaces
	while(s.charAt(0)==' ')
	{
		s = s.substring(1);
	}
	
	//remove trailing spaces
	while(s.charAt(s.length-1) == ' ') 
	{
		s = s.substring(0, s.length-1);
	}
	return s;
}

/***********************************************************************
 * Print this page, used in checkout.asp page
 ***********************************************************************/
function printPage()
{
	var bVersion = parseInt(navigator.appVersion);
	if (bVersion >= 4) window.print();
}
