<!--
//********************Add page specific javascript here**************************  

//****************************************************************** start sendThis ********************************************************************
function sendThis(what, record, item, picURL)
{
var docURL;
var bOK = false;
if(what != "submit"){
var whatNext = document.getElementById("whatNext");
var whichRecord = document.getElementById("whichRecord");
whatNext.value = what;
whichRecord.value = record;
}

switch (what){
case "shop":
if(picURL.indexOf("mixnmatch") > 0){
docURL = "bb_main.php?pageid=2004";
}else if(picURL.indexOf("holidays2008") > 0){
docURL = "bb_main.php?pageid=5001";
}else if(picURL.indexOf("valentines2009") > 0){
	docURL = "bb_main.php?pageid=5002";
}else if(picURL.indexOf("giftsFavors") > 0){
docURL = "bb_main.php?pageid=7";
}else if(picURL.indexOf("sweetBites") > 0){
docURL = "bb_main.php?pageid=6";
}else if(picURL.indexOf("mothersday") > 0){
docURL = "bb_main.php?pageid=5003";
}else if(picURL.indexOf("gardenparty") > 0){
docURL = "bb_main.php?pageid=5004";
}else if(picURL.indexOf("winter") > 0){
docURL = "bb_main.php?pageid=5005";
}else if(picURL.indexOf("hanukah") > 0){
docURL = "bb_main.php?pageid=5006";
}else if(picURL.indexOf("christmas") > 0){
docURL = "bb_main.php?pageid=5007";
}else if(picURL.indexOf("newyears") > 0){
docURL = "bb_main.php?pageid=5008";
}else if(picURL.indexOf("bemine") > 0){
docURL = "bb_main.php?pageid=5010";
}else if(picURL.indexOf("hugskisses") > 0){
docURL = "bb_main.php?pageid=5011";
}else if(picURL.indexOf("loveyou") > 0){
docURL = "bb_main.php?pageid=5012";
}else if(picURL.indexOf("xoxo") > 0){
docURL = "bb_main.php?pageid=5013";
}else{
docURL = "bb_main.php?pageid=2";
}


bOK = validate_gift(record, item);
if(bOK){
var shopOn = document.getElementById("shopOn");
shopOn.value = docURL;
docURL = "bb_silentUpdate.php";
}

break;

case "add":
var whichItemToAdd = document.getElementById("whichItemToAdd");
docURL = document.URL
if(docURL.indexOf("#gift") > 0){
docURL = docURL.substring(0, (docURL.indexOf("#gift")));
}
docURL = docURL + "#gift-" + whichRecord.value;
whichItemToAdd.value = picURL;
bOK = validate_gift(record, item);
break;

case "update":
docURL = document.URL
if(docURL.indexOf("#gift") > 0){
docURL = docURL.substring(0, (docURL.indexOf("#gift")));
}
docURL = docURL + "#gift-" + whichRecord.value;
bOK = true;
break;

case "delete":
docURL = "bb_order.php?pageid=9001" ;
bOK = true;
break;

case "submit":

bOK = validateContact();
if(bOK){
docURL = "bb_order_submit.php?pageid=9002"
var shopOn = document.getElementById("shopOn");
shopOn.value = docURL;
docURL = "bb_silentUpdate.php";
}
break;
}
if(bOK){
document.forms["bbOrder"].action=docURL;
document.forms["bbOrder"].submit();
}
}
//****************************************************************** end sendThis ********************************************************************

//****************************************************************** start validateContact ********************************************************************

function validateContact(){
var bOK = true;

var thisFName = document.forms["bbOrder"].contact_fname.value;
var thisLName = document.forms["bbOrder"].contact_lname.value;
var thisPhone = document.forms["bbOrder"].contact_phone.value;
var thisEmail = document.forms["bbOrder"].contact_email.value;

if(thisFName.length == 0){
bOK = false;
alert("Please provide us with contact information including a first name.");
document.forms["bbOrder"].contact_fname.focus();
}
if(bOK){
if(thisLName.length == 0){
bOK = false;
alert("Please provide us with contact information including a last name.");
document.forms["bbOrder"].contact_lname.focus();
}
}

if(bOK){
if(thisPhone.length == 0){
bOK = false;
alert("Please provide us with a phone number.");
document.forms["bbOrder"].contact_phone.focus();
}else{
if(!validatePhoneNumber(thisPhone)){
bOK = false;
}
}
}

if(bOK){
if(thisEmail.length == 0){
bOK = false;
alert("Please provide us with an email address.");
document.forms["bbOrder"].contact_email.focus();
}else{
var temp = emailCheck(thisEmail);
if(temp.length > 0){
bOK = false;
alert(temp);
}
}
}
return bOK;
}
//****************************************************************** end validateContact ********************************************************************

//****************************************************************** start validateZIP ********************************************************************
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {

return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
return false;
   }
}
return true;
}
//****************************************************************** end validateZIP ********************************************************************

//****************************************************************** start showText ********************************************************************
function showText(recordId){
var thisField = "card_text-" + recordId;
var theSpanField = "cardTextEx-" + recordId
var boxText = document.forms["bbOrder"].elements[thisField].value
if (boxText.length > 0){
var theSpan = document.getElementById(theSpanField);
theSpan.innerHTML = "your text: " + boxText;
}
}
//****************************************************************** end showText ********************************************************************

//****************************************************************** start calcGift ********************************************************************

function calcGift(recordId, itemId){
var thisQuantity = "quantity-" + recordId;
var thisGiftSize = "giftSize-" + recordId;
var thisGiftPkg = "giftPkg-" + recordId;
var thisCustomText = "cookie_text-" + recordId;
var thisGiftCalc = "giftCalc-" + recordId;

var thisItemQuantityMultiplier = "itemQuantityMultiplier-" + recordId;
var thisWrappingCost = "wrappingCost-" + recordId;
var thisCustomTextmCost = "customTextmCost-" + recordId;

var cookieTotal

if(check_int(document.forms["bbOrder"].elements[thisQuantity].value)){
var quantity = document.forms["bbOrder"].elements[thisQuantity].value;
var amtText = document.getElementById(thisGiftCalc);

switch (itemId.charAt(0)){

case "h":
if(document.forms["bbOrder"].elements[thisGiftSize][0].checked){
cookieTotal = 52
}else{
cookieTotal = 78
}
document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
cookieTotal = cookieTotal * parseInt(quantity);

amtText.innerHTML="$" + cookieTotal + " plus shipping";
break;

case "m":
cookieTotal = 3.25
if(parseInt(quantity)<24){
quantity = 24;
document.forms["bbOrder"].elements[thisQuantity].value = 24;
alert("minimum quantity for mixnimatch is 24 cookies.")
}
document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
cookieTotal = cookieTotal * parseInt(quantity);
var temp = document.forms["bbOrder"].elements[thisCustomText].value;
if(temp.length > 0){
cookieTotal = cookieTotal + 6;
document.forms["bbOrder"].elements[thisCustomTextmCost].value = 6;
}else{
document.forms["bbOrder"].elements[thisCustomTextmCost].value = "";
}
if(document.forms["bbOrder"].elements[thisGiftPkg].checked){
cookieTotal = cookieTotal + 3;
document.forms["bbOrder"].elements[thisWrappingCost].value = 3;
}else{
document.forms["bbOrder"].elements[thisWrappingCost].value = "";
}
amtText.innerHTML="$" + cookieTotal + " plus shipping";
break;

case "s":
cookieTotal = 18
if(parseInt(quantity)<2){
quantity = 2;
document.forms["bbOrder"].elements[thisQuantity].value = 2;
alert("minimum quantity for sweet bites is 2 pounds.")
}
document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
cookieTotal = cookieTotal * parseInt(quantity);
if(document.forms["bbOrder"].elements[thisGiftPkg].checked){
cookieTotal = cookieTotal + 3;
document.forms["bbOrder"].elements[thisWrappingCost].value = 3;
}else{
document.forms["bbOrder"].elements[thisWrappingCost].value = "";
}
amtText.innerHTML="$" + cookieTotal + " plus shipping";
break;

case "g":
cookieTotal = 52
document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
cookieTotal = cookieTotal * parseInt(quantity);

amtText.innerHTML="$" + cookieTotal + " plus shipping";

break;
case "v":
	cookieTotal = 32
	document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
	cookieTotal = cookieTotal * parseInt(quantity);

	amtText.innerHTML="$" + cookieTotal //+ " plus shipping";
	
break;
case "d":
    cookieTotal = 32
    document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
    cookieTotal = cookieTotal * parseInt(quantity);

    amtText.innerHTML="$" + cookieTotal //+ " plus shipping";
    
break;
case "p":
    cookieTotal = 35
    document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
    cookieTotal = cookieTotal * parseInt(quantity);

    amtText.innerHTML="$" + cookieTotal //+ " plus shipping";
    
break;
case "w":
case "c":
case "k":
case "n":
//alert(itemId.charAt(0) + " ll ") 
    cookieTotal = 40
    document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
    cookieTotal = cookieTotal * parseInt(quantity);

    amtText.innerHTML="$" + cookieTotal //+ " plus shipping";
    
break;
case "b":
case "i":
case "l":
case "x":
//alert(itemId.charAt(0) + " ll ") 
    cookieTotal = 40
    document.forms["bbOrder"].elements[thisItemQuantityMultiplier].value = cookieTotal;
    cookieTotal = cookieTotal * parseInt(quantity);

    amtText.innerHTML="$" + cookieTotal //+ " plus shipping";
    
break;
default:
amtText.innerHTML="$ 0.00";

}	//switch
}	//if
}	//function

//****************************************************************** end calcGift ********************************************************************

//****************************************************************** start validate_gift ********************************************************************

function validate_gift(num, item){
var bOK = true;
var thisField;
thisField = "giftErrorMessage-" + num
var errMsg = document.getElementById(thisField);

thisField = "quantity-" + num
if (bOK == true && (!check_int(document.forms["bbOrder"].elements[thisField].value) || (isEmpty(document.forms["bbOrder"].elements[thisField].value)) || (parseInt(document.forms["bbOrder"].elements[thisField].value) == 0))){
errMsg.innerHTML = "gift " + item + " quantity needs to be filled in and be greater than 0.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}

thisField = "ship_name-" + num
if (bOK == true && (isEmpty(document.forms["bbOrder"].elements[thisField].value))){
errMsg.innerHTML = "gift " + item + " needs a name for shipping.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}

thisField = "ship_addr1-" + num
if (bOK == true && (isEmpty(document.forms["bbOrder"].elements[thisField].value))){
errMsg.innerHTML = "gift " + item + " needs an address for shipping.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}

thisField = "ship_city-" + num
if (bOK == true && (isEmpty(document.forms["bbOrder"].elements[thisField].value))){
errMsg.innerHTML = "gift " + item + " needs a city for shipping.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}

thisField = "ship_state-" + num
if (bOK == true && (parseInt(document.forms["bbOrder"].elements[thisField].value) == 0)){
errMsg.innerHTML = "gift " + item + " needs a state for shipping.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}


thisField = "ship_zip-" + num
if (bOK == true && (isEmpty(document.forms["bbOrder"].elements[thisField].value))){
errMsg.innerHTML = "gift " + item + " needs a zip code for shipping.";
document.forms["bbOrder"].elements[thisField].focus();
bOK = false;
}

if (bOK == true && (!validateZIP(document.forms["bbOrder"].elements[thisField].value))){
errMsg.innerHTML = "The zip code format for gift " + item + " appears incorrect. Format should be ##### or #####-####";
bOK = false;
}

return bOK;
}
//****************************************************************** end validate_gift ********************************************************************

//****************************************************************** start emailCheck ********************************************************************

function emailCheck (emailStr) {

var str = "";
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address.
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	str = "Email address seems incorrect (check @ and .'s)"
	return str;
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid
if (user.match(userPat)==null) {
    // user is not valid
    str = "The Email username doesn't seem to be valid."
    return str;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        str = "Destination IP address is invalid!"
		return str;
	    }
    }
    return str;
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	str = "The Email address domain name doesn't seem to be valid."
    return str;
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   str = "The Email address must end in a three-letter domain, or two letter country."
   return str;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This Email address is missing a hostname!"
   str = errStr
   return str;
}

// Got this far, everything's valid!
return str;
}
//****************************************************************** end emailCheck ********************************************************************


//****************************************************************** start validatePhoneNumber ********************************************************************
function validatePhoneNumber(phoneNo) {
 var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
 if (phoneNo.match(phoneRe)) {
   return true;
 } else {
   alert( "The phone number doesn't appear to be valid!" );
   return false;
 }
}

//****************************************************************** end validatePhoneNumber ********************************************************************

//****************************************************************** start checkLeadTime ********************************************************************
function checkLeadTime(record, cookieLeadTime, defaultDate){
var days
var tempMonth
var today = new Date();
var oneMinute = 60 * 1000;
var oneHour = oneMinute * 60;
var oneDay = oneHour * 24;
var leadDays = oneDay * parseInt(cookieLeadTime);
var regDate = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}";       //"(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}";
var thisDate = "pref_ship_date-" + record;
var tempDate = document.forms["bbOrder"].elements[thisDate].value;

if(tempDate.match(regDate)){

//days = difference/(1000*60*60*24);
today = today.getTime() + leadDays;
var pieces = tempDate.split("/")
switch (pieces[0]){
case "1":
tempMonth = "January";
break;
case "2":
tempMonth = "February";
break;
case "3":
tempMonth = "March";
break;
case "4":
tempMonth = "April";
break;
case "5":
tempMonth = "May";
break;
case "6":
tempMonth = "June";
break;
case "7":
tempMonth = "July";
break;
case "8":
tempMonth = "August";
break;
case "9":
tempMonth = "September";
break;
case "10":
tempMonth = "October";
break;
case "11":
tempMonth = "November";
break;
case "12":
tempMonth = "December";
break;
}
var inputDate = new Date(pieces[0] + " " + pieces[1] +", " + pieces[2]);
alert("input date: " + inputDate.getTime() + " Today: " + today)
if(inputDate.getTime() < today){
document.forms["bbOrder"].elements[thisDate].value = defaultDate;
alert("Our minimum lead time is " + cookieLeadTime + " days from today.");
}





}else{
alert("Your preferred delivery date does not appear to be in the correct format.");
document.forms["bbOrder"].elements[thisDate].value = defaultDate;
document.forms["bbOrder"].elements[thisDate].focus();
}
}


//****************************************************************** end checkLeadTime ********************************************************************

//****************************************************************** start validateRequest ********************************************************************

function validateRequest(){
var bOK = true;

var thisFName = document.forms["bbCustom"].custom_fname.value;
var thisLName = document.forms["bbCustom"].custom_lname.value;
var thisPhone = document.forms["bbCustom"].custom_phone.value;
var thisEmail = document.forms["bbCustom"].custom_email.value;

if(thisFName.length == 0){
bOK = false;
alert("Please provide us with contact information including a first name.");
document.forms["bbCustom"].custom_fname.focus();
}
if(bOK){
if(thisLName.length == 0){
bOK = false;
alert("Please provide us with contact information including a last name.");
document.forms["bbCustom"].custom_lname.focus();
}
}

if(bOK){
if(thisPhone.length == 0){
bOK = false;
alert("Please provide us with a phone number.");
document.forms["bbCustom"].custom_phone.focus();
}else{
if(!validatePhoneNumber(thisPhone)){
bOK = false;
}
}
}

if(bOK){
if(thisEmail.length == 0){
bOK = false;
alert("Please provide us with an email address.");
document.forms["bbCustom"].custom_email.focus();
}else{
var temp = emailCheck(thisEmail);
if(temp.length > 0){
bOK = false;
alert(temp);
}
}
}
if(bOK){
docURL="bb_order_custom.php?ready=yes";
document.forms["bbCustom"].action=docURL;
document.forms["bbCustom"].submit();
}
}
//****************************************************************** end validateRequest ********************************************************************
//****************************************************************** start returnToSite ********************************************************************
function returnToSite(){
var docURL = "index.php"
var shopOn = document.getElementById("shopOn");
shopOn.value = docURL;
docURL = "bb_silentUpdate.php";

document.forms["bbOrder"].action=docURL;
document.forms["bbOrder"].submit();

}
//****************************************************************** end returnToSite ********************************************************************
//  -->
