function SetAddNow(ISBN){
//This function sets the "Add to cart" button based on the current shopping cart contents.

	//Check quantity in form

	//alert(ISBN + 'CartImage');

	//if quantity > 0 set graphic to "Remove from cart" and onclick to remove item from cart
	if (document.getElementById(ISBN).value > 0){
		document.getElementById(ISBN + 'Add').style.visibility = 'hidden';
		document.getElementById(ISBN + 'Remove').style.visibility = 'visible';
	}
	else{	//else set graphic to "Add to cart" and onclick to add the item
		document.getElementById(ISBN + 'Add').style.visibility = 'visible';
		document.getElementById(ISBN + 'Remove').style.visibility = 'hidden';
	}


	

}//End function SetAddNow


function SameAsShipping(){
	document.getElementById("BillingName").value = document.getElementById("ShippingName").value;
	document.getElementById("BillingAddressLine1").value = document.getElementById("ShippingAddressLine1").value;
	document.getElementById("BillingAddressLine2").value = document.getElementById("ShippingAddressLine2").value;
	document.getElementById("BillingCity").value = document.getElementById("ShippingCity").value;
	document.getElementById("BillingState").value = document.getElementById("ShippingState").value;
	document.getElementById("BillingZIP").value = document.getElementById("ShippingZIP").value;

	document.getElementById("BillingName").readonly = true;
	document.getElementById("BillingAddressLine1").readonly = true;
	document.getElementById("BillingAddressLine2").readonly = true;
	document.getElementById("BillingCity").readonly = true;
	document.getElementById("BillingState").readonly = true;
		//document.getElementById("BillingZIP").disabled = true;

}// End function SameAsShipping

function NotSameAsShipping(){
	document.getElementById("BillingName").disabled = false;
	document.getElementById("BillingAddressLine1").disabled = false;
	document.getElementById("BillingAddressLine2").disabled = false;
	document.getElementById("BillingCity").disabled = false;
	document.getElementById("BillingState").disabled = false;
	document.getElementById("BillingZIP").disabled = false;
}// End function NotSameAsShipping

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}//end function get_cookie

function IndexOf(ISBNArray, ISBN) {
	for (key in ISBNArray){
		if (ISBNArray[key] == ISBN)
			{return key;}
	}//end for to loop through array
}//End function IndexOf

function UpdateCart (){
	//This function is called to update the item quantitied displayed in the shopping cart pages.
	//It draws upon the cookies that are already set.
	try
	{
		ISBNArray = get_cookie("ISBNList").split(',');
		QuantityArray = get_cookie("Quantities").split(',');

		for (key in ISBNArray){
			if (QuantityArray[key] < 0) QuantityArray[key] =0;
			document.getElementById(ISBNArray[key]).value = QuantityArray[key];
			SetAddNow(ISBNArray[key]);
		}//end for to loop through array

	}
		catch(err)
	{//This catch resets all form varialbes to 0 if no cookies are found
		for (key in document.ShoppingCart ){
			document.getElementById(key).value='0';
		}
	}
				
	
}//End Function UpdateCart

function AddBook(ISBN, Quantity, nDays, Behavior) {
 var today = new Date();
 var expire = new Date();
 var OldISBNList = get_cookie("ISBNList");
 var OldQuantity = get_cookie("Quantities");
 
 if (Behavior==null) Behavior = 'Add';
 
 if (nDays==null || nDays==0) nDays=365;
 expire.setTime(today.getTime() + 3600000*24*nDays);

  if (OldISBNList != null){ //If a cookie is found that contains an ISBN list
	if (OldISBNList.indexOf(ISBN) == -1) //If the passed ISBN is NOT in the cookie
		{
		ISBN = OldISBNList + "," + ISBN;
		Quantity = OldQuantity + "," + Quantity;
		
		}//End if to check for existing cookie
		
		else {//The book is already in the cart
			var ISBNArray = OldISBNList.split(',');
			var QuantityArray = OldQuantity.split(',');
			
			var ISBNINdex = IndexOf(ISBNArray, ISBN);

			if (Behavior == 'Add'){
				QuantityArray[ISBNINdex] = String( parseInt(Quantity) + parseInt(QuantityArray[ISBNINdex]));
			}else {
				QuantityArray[ISBNINdex] = parseInt(Quantity);
			}
			
			if (QuantityArray[ISBNINdex] < 0) QuantityArray[ISBNINdex] =0;

			//alert(QuantityArray[ISBNINdex]);
			
			Quantity=QuantityArray.join();
			ISBN=ISBNArray.join();
		}//End else to process a book that's aready in the array
  }//End if to check for null cookie

 document.cookie = "Quantities="+escape(Quantity)
                 + ";expires="+expire.toGMTString()
                 + ";path=/";
                 
 document.cookie = "ISBNList="+escape(ISBN)
                 + ";expires="+expire.toGMTString()
                 + ";path=/";

UpdateCart();

}//End function AddBook


function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==""||value=="Month"||value=="Year")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_CC_form(thisform)
{
	with (thisform)
	{
		if (validate_required(CCN,"No Credit Card was Specified")==false
			||validate_required(ExpMonth,"Please specify the Expiration Month")==false
			||validate_required(ExpYear,"Please specify the Expiration Year")==false
				)
			//CCN.value='Woops!';
		  {CCN.focus();return false}
	}
}


function validate_Order_form(thisform)
{
	with (thisform)
	{	if (thisform.action.indexOf('CreditCard.asp') > 0){
			if (FirstName.value==false
				||LastName.value==false
				||Email.value==false
				||ShippingName.value==false
				||ShippingAddressLine1.value==false
				||ShippingCity.value==false
				||ShippingState.value==false
				||ShippingZIP.value==false
				||BillingName.value==false
				||BillingAddressLine1.value==false
				||BillingCity.value==false
				||BillingState.value==false
				||BillingZIP.value==false
		   	   )
				//CCN.value='Woops!';
			  {	alert('Please fill in all required fields to continue. Required fields are in red.');
				return false;}
		}//end if to only validate if submitting to credit card page
	}//end with to use current form
}//End Function validate_Order_form


