function isNumeric(elem, helperMsg){
	var numericExpression = /^[1-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		return false;
		elem.value=0;
		elem.focus();
	}
}

function addBookToCart(quant,itemName,itemNumber,itemPrice)
{
var price = new Number(itemPrice);
var dbug=0;

itemPrice = price.toPrecision(4);
	if (dbug) {
		alert("ADDTOCART::"+" "+quant+" "+itemName+" "+itemNumber+" "+itemPrice+" ");
}
var anum=/(^\d+$)|(^\d+\.\d+$)/

if (document.book.quant.value == '') {
        alert ("Please enter a valid quantity.") ;
	book.quant.focus();
	return (false);
}

for (i=0;i<document.book.quant.value.length; i++) {
	ch = document.book.quant.value.substring(i , i+1);
	if (ch < "0" || ch > "9")
	{
		alert ("Please specify a number for quantity.");
		return false;
	}
}
if (document.book.quant.value <= 0) {
        alert ("Please enter a valid quantity.") ;
	return (false);
}
        window.open('https://www.paypal.com/cgi-bin/webscr?cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemName+'&item_number='+itemNumber+'&quantity='+quant+'&amount='+itemPrice+'&image_url=https://www.kidsknits.com/images/logo.gif','cartwin','width=600,height=400,scrollbars,location,resizable,status');
document.book.reset();
return (true);
}

function addToCart(color,quant,itemName,itemNumber,itemPrice)
{
var dbug=0;
	if (dbug) {
		alert("ADDTOCART::"+" "+color+" "+quant+" "+itemName+" "+itemNumber+" "+itemPrice);
}
var anum=/(^\d+$)|(^\d+\.\d+$)/

itemName=itemName+' '+color;

if (document.yarn.color.selectedIndex <=0)
{
	alert("Please select a color");
	return (false);
}
if (document.yarn.quant.value == '') {
        alert ("Please enter a valid quantity.") ;
	yarn.quant.focus();
	return (false);
}

for (i=0;i<document.yarn.quant.value.length; i++) {
	ch = document.yarn.quant.value.substring(i , i+1);
	if (ch < "0" || ch > "9")
	{
		alert ("Please specify a number for quantity.");
		return false;
	}
}
if (document.yarn.quant.value <= 0) {
        alert ("Please enter a valid quantity.") ;
	return (false);
}
if (document.yarn.quant.value == 20) {
	itemDiscount=itemPrice*.10;
	itemPrice=itemPrice-itemDiscount;
	itemPrice=itemPrice.toFixed(2);
}
        window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemName+'&item_number='+itemNumber+'&quantity='+quant+'&amount='+itemPrice,'cartwin','width=600,height=400,scrollbars,location,resizable,status');
document.yarn.reset();
return (true);
}

function addToCart2(color,quant,itemName,itemNumber,itemPrice,minQuant)
{
 var dbug=0;
	if (dbug) {
		alert("ADDTOCART::"+" "+color+" "+quant+" "+itemName+" "+itemNumber+" "+itemPrice);
}
var anum=/(^\d+$)|(^\d+\.\d+$)/

itemName=itemName+' '+color;

if (document.yarn.color.selectedIndex <=0)
{
	alert("Please select a color");
	return (false);
}
if (document.yarn.quant.value == '') {
        alert ("Please enter a valid quantity.") ;
	yarn.quant.focus();
	return (false);
}
if (quant < minQuant) {
        alert ("Please enter a valid quantity. This color has a "+minQuant+" ball minimum." ) ;
	yarn.quant.focus();
	return (false);
}
for (i=0;i<document.yarn.quant.value.length; i++) {
	ch = document.yarn.quant.value.substring(i , i+1);
	if (ch < "0" || ch > "9")
	{
		alert ("Please specify a number for quantity.");
		return false;
	}
}
if (document.yarn.quant.value <= 0) {
        alert ("Please enter a valid quantity.") ;
	return (false);
}
        window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemName+'&item_number='+itemNumber+'&quantity='+quant+'&amount='+itemPrice,'cartwin','width=600,height=400,scrollbars,location,resizable,status');
document.yarn.reset();
return (true);
}

function yarnAdd(itemName, itemNumber, obj1,array_index, itemPrice)
{
var obj,pick,size,intl,desc,instance;

obj = obj1.elements[0];  // the 1st dropdown object
pick = obj.selectedIndex;  // the item selected

// quant = obj.options[pick].value;  // get dropdown value
// quant = document.item1.quantity[array_index].options[document.item1.quantity[array_index].selectedIndex].value;

quant = document.item1.quantity[array_index].value;
if (isNumeric(document.item1.quantity[array_index], "Please enter a valid quantity")){
// alert(quant);
            window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemName+'&item_number='+itemNumber+'&quantity='+quant+'&amount='+itemPrice,'cartwin','width=600,height=400,scrollbars,location,resizable,status');
}

// make data work for all browsers...
desc = escape (itemName);
}

function quickAdd(itemName, itemNumber, itemPrice)
{
var obj,pick,size,intl,desc,instance;

// make data work for all browsers...
desc = escape (itemName);
window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+desc+'&item_number='+itemNumber+'&amount='+itemPrice+'&image_url=https://www.kidsknits.com/images/logo.gif','cartwin','width=600,height=400,scrollbars,location,resizable,status');

}

function quickKitOrder(itemDes, primColor, itemNumber, itemPrice,shipping)
{
var obj,colorCombo,pick,size,intl,desc,instance;
var f= document.wgHatForm;

if (primColor == 'NULL') {
	alert ("Please choose a colorway.") ; 
	return;
}

window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemDes+'&item_number='+itemNumber+' Colorway:'+primColor+'&amount='+itemPrice+'&image_url=http://www.kidsknits.com/images/logo.gif'+'&shipping='+shipping,'cartwin','width=600,height=400,scrollbars,location,resizable,status');

f.reset();
}

function quickOrder(itemDes, primColor, secColor, itemNumber, itemPrice,shipping)
{
var obj,colorCombo,pick,size,intl,desc,instance;
var f= document.wgHatForm;

colorCombo=primColor+"/"+secColor;

// make data work for all browsers...
//desc = escape (itemColor);

// alert(itemDes+","+colorCombo+","+secColor+","+itemPrice);
// alert(itemDes+","+primColor+","+secColor);
if (primColor == 'NULL') {
	alert ("Please choose a primary color.") ; 
	return;
}

if (secColor == 'NULL') {
	alert ("Please choose a secondary color.") ; 
	return;
}

window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+itemDes+'&item_number='+itemNumber+' Color Combo:'+colorCombo+'&amount='+itemPrice+'&image_url=http://www.kidsknits.com/images/logo.gif'+'&shipping='+shipping,'cartwin','width=600,height=400,scrollbars,location,resizable,status');

f.reset();
}

function cartAdd(itemName, itemNumber, obj1,array_index, itemPrice)
{
var obj,pick,size,intl,desc,instance;

// alert("instance is:  "+array_index);
obj = obj1.elements[0];  // the 1st dropdown object
pick = obj.selectedIndex;  // the item selected

// quant = obj.options[pick].value;  // get dropdown value
quant = document.item1.quantity[array_index].options[document.item1.quantity[array_index].selectedIndex].value;

// make data work for all browsers...
desc = escape (itemName);
window.open('https://www.paypal.com/cgi-bin&cmd=_cart&add=1&business=toolsmith%40xmlvision.com&item_name='+desc+'&item_number='+itemNumber+'&quantity='+quant+'&amount='+itemPrice,'cartwin','width=600,height=400,scrollbars,location,resizable,status');

}

function cartView()
{
	window.open('https://www.paypal.com/cart/display=1&business=toolsmith@xmlvision.com','cartwin','width=600,height=400,scrollbars,location,resizable,status');

}

function popPage(pageName)
{
var popUpWindow = window.open(pageName, 'stix and stones', 'width=560,height=480,scrollbars=yes,resizable=no,status=no,location=no,menubar=no')

popUpWindow.focus()
}

function calcPrice (price) {

var now = new Date();
if (document.quickpick.quickSize.options[document.quickpick.quickSize.selectedIndex].value == '') {
	alert ("Please choose a size") ; 
}

quantity =  document.quickpick.quickQuantity.options[document.quickpick.quickQuantity.selectedIndex].value;
price = document.quickpick.quickQuantity.options[document.quickpick.quickQuantity.selectedIndex].value * document.quickpick.quickSize.options[document.quickpick.quickSize.selectedIndex].value;

var priceStr = price.toString();
var dollar = "\$ ";
priceString= dollar + priceStr + ".00";

document.quickpick.totalText.value=priceString; 
}

function cartCtl() {
var f = document.quickpick;

if ((document.quickpick.quickSize.options[document.quickpick.quickSize.selectedIndex].value) =='') {
             	alert("Please select a size.");
} else {
		f.submit();
	}
//	self.location.href("/cgi-bin/shopCart.pl");
//	window.location.reload("/cgi-bin/shopCart.pl");
//		showCart("/cgi-bin/shopCart.pl");
}

function billtoCtl() {
var f = document.cart;

if ((document.cart.firstName.value) =='' )  {
             	alert("Please enter your first name.");
} else if ((document.cart.lName.value) =='') {
             	alert("Please enter your last name.");
} else if ((document.cart.sAddress.value) =='') {
             	alert("Please enter your street address.");
}
else if ((document.cart.city.value) =='') {
             	alert("Please enter your city.");
}
else if ((document.cart.state.value) =='') {
             	alert("Please enter your state.");
}
else if ((document.cart.zipCode.value) =='') {
             	alert("Please enter your zip code.");
}
else if ((document.cart.areaCode.value) =='') {
             	alert("Please enter your area code.");
}
else if ((document.cart.phoneExchange.value) =='') {
             	alert("Please confirm your phone number.");
}
else if ((document.cart.phoneNumber.value) =='') {
             	alert("Please confirm your phone number.");
}
else if ((document.cart.email.value) =='' ) {
             	alert("Please confirm your phone number.");
}
else {
		f.submit();
	}
}

// calling this from yarn pages...
function submit() {

var f=document.cart;

// window.alert("shit");
f.submit();
// window.location.href="shopcart.jsp";

}

function spawnDetail(url) {

	window.open(url,'cartwin','width=845,height=600');
}
function spawn(url) {

	window.open(url,'cartwin','width=600,height=400,scrollbars,location,resizable,status');
}

function spawnImage(url) {


	window.open(url,'cartwin','width=320,height=650');
}

function showCart(url) {

	window.location.href=url;
}
function valForm() {
var f = document.quickpick;

if ((document.quickpick.quickSize.options[document.quickpick.quickSize.selectedIndex].value) =='') {
             	alert("Please select a size.");
} else {
		f.submit();
	}
}

function billingForm() {
// var f = document.checkout;
	alert("testin");
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

function verifyColor(colorSel) {

//if (document.quickpick.quickSize.options[document.quickpick.quickSize.selectedIndex].value == '') {
//	alert ("Please choose a size") ; 
//}

	alert ("Please choose a size") ; 
if (colorSel == 'NULL') {
	alert("ok, you clicked");
}
}

function alertSel() {

	alert("Please choose a yarn type first.");

}

function primSel() {

	alert("Please choose a primary color yarn.");

}

