// Display an error message and select erroneous field
function FieldError(fld) {
   alert("Please enter a valid " + fld.title + "!");
   fld.focus();
}

// Validate a Credit Card {
function ValidCC(cc) {
   var j = 0;
   var w = 0;
   var sum = 0;
   if (cc.length == 16) {
     for (j=0;j<cc.length-1;j++) {
        w = cc.substr(cc.length-(j+2),1)*(2-(j%2));
        sum += ((w<10) ? w : (w-9));
     }
     return (cc.substr(cc.length-1,1)==((10-sum%10)%10));
   }
   return 0;
}

// Validate form fields based on style class
function ValidateByClass( formName ) {
   var formfields = document.forms(formName).elements.length;
   var i = 0;
   if (bb_canbail) return true;
   for (i=0; i<formfields; i++) {
      var fld = document.forms(formName).elements[i];
      switch (fld.className) {
         case "match" : // e-mail first time
         case "rfield" : // required field
            if (fld.value=="") {
               FieldError(fld);
               return false;
            }
            break;
         case "zipfield" : // zip code if in US
            if (fld.value=="" && formName.ShipTo_Country.value == "United States") {
               FieldError(fld);
               return false;
            }
            break;
         case "ccfield" : // credit card
            var pmt = document.forms(formName).BillTo_PmtType[0].checked;
            if (pmt) {
                var cc = fld.value;
                if (cc=="") {
                   FieldError(fld);
                   return false;
                }
                cc.replace(/\D/,"");
                if (ValidCC(cc)==0) {
                    FieldError(fld);
                    return false;
                }
            }
            break;
         case "month" : // month
         case "year" : // year
            var pmt = document.forms(formName).BillTo_PmtType[0].checked;
            if (pmt) {
                if (fld.value==-1) {
                   alert("Please select the expiration date " + fld.className + "!");
                   fld.focus();
                   return false;
                }
            }
            break;
         case "quant" : // quantity
           if (fld.type != "hidden") {
             var numb = parseInt(fld.value);
             if (isNaN(numb)) {
                alert("Please enter a number for the quantity greater than or equal to zero.");
                fld.focus();
                return false;
             }
             if (numb < 0) {
                alert("Please enter a number for the quantity greater than or equal to zero.");
                fld.focus();
                return false;
             }
           }
           break;
         default:
            break;
      }
      // special case for e-mail duplicate entry
      if (fld.name == "BillTo_Email") {
         if (fld.value != document.forms(formName).BillTo_EM2.value) {
            alert("Please enter your e-mail address exactly the same both times.");
            fld.focus();
            return false;
         }
     }
   }
   return true;
}

// Calculate the bookbag
    var bb_canbail = 0;
	var bb_total_qty = 0;
	var bb_total_price = 0;
	var bb_price_string = "";
	var sBookBagTotal = "";
	function formatMoney(value) {
		value -= 0;
		value = (Math.round(value * 100)) / 100;
		return((value == Math.floor(value)) ? ("$" + value + '.00') : ((value * 10 == Math.floor(value * 10)) ? ("$" + value + '0') : ("$" + value)));
	}
	for (i = 0; i < document.cookie.split("; ").length; ++i) {
		aCookie = unescape(document.cookie.split("; ")[i]);
		if (aCookie.substring(0,3) == "BUY") {
			var oneval = aCookie.split("~")[5] -0;
			var onecnt = aCookie.split("~")[1] -0;
			bb_total_price = bb_total_price + oneval*onecnt;
			bb_total_qty = bb_total_qty + onecnt;
		}
	}
	bb_price_string = formatMoney(bb_total_price);
	sBookBagTotal = (bb_total_qty > 0) ? bb_total_qty : "No";
	sBookBagTotal += (bb_total_qty == 1) ? " Item" : " Items";
	sBookBagTotal += " in <b>BookBag</b>";
	if ((bb_total_qty > 0) && (bb_price_string.length > 0))
		{ sBookBagTotal += "<br><b>Total: " + bb_price_string + "</b>"; }

    if (document.location.search.length) {
        if (document.location.search.slice(1,4) == "gaw") {document.cookie=document.location.search.slice(1)+";domain=justforkidsbooks.com";}
    }

// AutoBlink
// Puts Google's Autolink on the Blink :)
// (c) 2005 Chris Ridings   http://www.searchguild.com
// Redistribute at will but leave this message intact
// -
// 5th March - Implemented speed enhancement suggested by the guy who wrote
// http://www.dougaltoolbar.com/

var linkcount;
function checklinks() {
 if (!(linkcount==document.links.length)) {
   // Something changed the links!
   // Iterate for an id of _goog
   for (i=0; i < document.links.length; i++) {
	if (document.links[i].id.substring(0,5)=="_goog") {
	// If we find an id of _goog then do something
			// No affiliate id defined so remove the links
			var tr = document.links[i].parentTextEdit.createTextRange();
			tr.moveToElementText(document.links[i]);
			tr.execCommand("Unlink",false);
			tr.execCommand("Unselect",false);
			i--;
	}
   }
  }
  linkcount = document.links.length;
  setTimeout("checklinks()",500);
}
if (document.getElementById && document.createElement) {
	linkcount=document.links.length;
	setTimeout("checklinks()",500);
}
