var gifts = 0; // index
var giftsCount = 0;
var deliverDefault = new Date();

var tzList = {
	"-11": "Samoa, Midway",
	"-10": "Hawaii, French Polynesia",
	"-9": "Alaska",
	"-8": "US Pacific",
	"-7": "US Mountain",
	"-6": "US Central",
	"-5": "US Eastern",
	"-4": "New Foundland, Venezuela, Chile",
	"-3": "Brazil, Argentina, Greenland",
	"-2": "Mid-Atlantic",
	"-1": "Azores, Cape Verda Is.",
	"0": "England, Ireland, Portugal",
	"+1": "Europe",
	"+2": "Central Europe",
	"+3": "Moscow, Saudi Arabia",
	"+4": "Oman",
	"+5": "Pakistan",
	"+6": "India",
	"+7": "Indonesia",
	"+8": "China, West Australia",
	"+9": "Japan",
	"+10": "East Australia",
	"+11": "Solomon Islands, Micronesia",
	"+12": "Marshall Islands, Fiji"
}

var timeList = {
	"morning": "09:00",
	"afternoon": "14:00",
	"evening": "19:00"
}

function createGiftRegion(gid, theEmail, theGiftDate) {
	var theRegion = document.createElement("div");
	theRegion.className = "giftRegion";
	
	var theFields = document.createElement("fieldset");
	var theFieldsLabel = document.createElement("legend");
	theFieldsLabel.appendChild(document.createTextNode("Gift"));
	theFields.appendChild(theFieldsLabel);

	var theEmailName = "gift_email["+(gid)+"]";
	var theEmailLine = document.createElement("div");
	var theEmailLabel  = document.createElement("label");
	theEmailLabel.setAttribute("for", theEmailName);
	theEmailLabel.appendChild(document.createTextNode("email"));
	var theEmailField = document.createElement("input");
	theEmailField.setAttribute("name", theEmailName);
	theEmailField.setAttribute("size", "16");
	theEmailField.value = theEmail;
	var theEmailConfirm = document.createElement("input");
	theEmailConfirm.setAttribute("name", "gift_email_conf["+gid+"]");
	theEmailConfirm.setAttribute("size", "16");
	var theEmailCheck = function () { confirmEmail(theEmailField.value, theEmailConfirm.value); };
	theEmailField.onchange = theEmailCheck;
	theEmailConfirm.onchange = theEmailCheck;
	var theEmailConfirmLabel = document.createElement("label");
	theEmailConfirmLabel.setAttribute("for", "gift_email_conf["+gid+"]");
	theEmailConfirmLabel.appendChild(document.createTextNode("confirm email"));
	theEmailLine.appendChild(document.createTextNode("to: "));
	theEmailLine.appendChild(theEmailLabel);
	theEmailLine.appendChild(theEmailField);
	theEmailLine.appendChild(theEmailConfirmLabel);
	theEmailLine.appendChild(theEmailConfirm);

	var theGiverLine = document.createElement("div");
	var theGiverLabel = document.createElement("label");
	theGiverLabel.setAttribute("for", "gift_from["+gid+"]");
	theGiverLabel.appendChild(document.createTextNode("from:"));
	var theGiverExplain = document.createElement("span");
	theGiverExplain.className = "explain";
	theGiverExplain.appendChild(document.createTextNode("(how your name will be displayed)"));
	var theGiverField = document.createElement("input");
	theGiverField.setAttribute("name", "gift_from["+gid+"]");
	theGiverLine.appendChild(theGiverLabel);
	theGiverLine.appendChild(theGiverExplain);
	theGiverLine.appendChild(theGiverField);
	
	var theDelivName = "gift_deliv["+gid+"]";
	var theDelivLine = document.createElement("div");
	var theDelivLabel = document.createElement("label");
	theDelivLabel.setAttribute("for", theDelivName);
	theDelivLabel.appendChild(document.createTextNode("deliver on:"));
// 	var theDelivField = document.createElement("input");
// 	theDelivField.setAttribute("name", theDelivName);
// 	theDelivField.setAttribute("value", giftDate(theGiftDate));
	var theDelivTimeSelect = dateSelect(deliverDefault.getDate()+1);
	theDelivTimeSelect.setAttribute("name", "gift_date["+gid+"]");
	var theDelivTZSelect = tzSelect("-5");
	theDelivTZSelect.setAttribute("name", "gift_tz["+gid+"]");

	theDelivLine.appendChild(theDelivLabel);
	theDelivLine.appendChild(document.createTextNode(" December "));
	theDelivLine.appendChild(theDelivTimeSelect);
	theDelivLine.appendChild(document.createTextNode(" , 2008 "));
	theDelivLine.appendChild(theDelivTZSelect);

	var theEnableLine = document.createElement("div");
	var theDelivEnable = document.createElement("input");
	theDelivEnable.setAttribute("type", "checkbox");
	theDelivEnable.setAttribute("name", "giftDeliverNow");
	theDelivEnable.onclick = function(){theDelivTimeSelect.disabled = theDelivEnable.checked;};
	theDelivEnableLabel = document.createElement("label");
	theDelivEnableLabel.appendChild(document.createTextNode("or check this box to deliver the gift immediately"));
	
	theEnableLine.appendChild(theDelivEnableLabel);
	theEnableLine.appendChild(theDelivEnable);
	
	var theRemoveRegion = document.createElement("div");
	theRemoveRegion.className = "giftRemoveRegion";
	var theRemoveAction = document.createElement("span");
	theRemoveAction.className = "giftRemoveAction";
	theRemoveAction.appendChild(document.createTextNode("remove"));
	theRemoveAction.onclick = function () { removeGift(theRegion); };
	theRemoveRegion.appendChild(theRemoveAction);
	
	theFields.appendChild(theEmailLine);
	theFields.appendChild(theGiverLine);
	theFields.appendChild(theDelivLine);
	theFields.appendChild(theEnableLine);
	theFields.appendChild(theRemoveRegion);
	
	theRegion.appendChild(theFields);
	
	return theRegion;
}



function createGiftRegionNew(gid, theEmail, theGiftDate) {
	var theRegion = document.createElement("div");
	theRegion.className = "giftRegion";
	
	var theFields = document.createElement("div");


	var theEmailName = "gift_email["+(gid)+"]";
	var theEmailLine = document.createElement("div");
	var theEmailLabel  = document.createElement("div");
	theEmailLabel.className = "giftlabel";
	theEmailLabel.appendChild(document.createTextNode("Email this gift to:"));
	var theEmailField = document.createElement("input");
	theEmailField.setAttribute("name", theEmailName);
	theEmailField.setAttribute("size", "20");
	theEmailField.value = theEmail;
	var theEmailConfirm = document.createElement("input");
	theEmailConfirm.setAttribute("name", "gift_email_conf["+gid+"]");
	theEmailConfirm.setAttribute("size", "20");
	var theEmailCheck = function () { confirmEmail(theEmailField.value, theEmailConfirm.value); };
	theEmailField.onchange = theEmailCheck;
	theEmailConfirm.onchange = theEmailCheck;
	var theEmailConfirmLabel = document.createElement("div");
    	theEmailConfirmLabel.className = "giftlabel";
	theEmailConfirmLabel.appendChild(document.createTextNode("Retype same email:"));
	theEmailLine.appendChild(document.createTextNode(""));
	theEmailLine.appendChild(theEmailLabel);
	theEmailLine.appendChild(theEmailField);

    var theConfirmEmailLine = document.createElement("div");
	theConfirmEmailLine.appendChild(theEmailConfirmLabel);
	theConfirmEmailLine.appendChild(theEmailConfirm);

	var theGiverLine = document.createElement("div");
	var theGiverLabel = document.createElement("div");
    	theGiverLabel.className = "giftlabel";
	theGiverLabel.appendChild(document.createTextNode("Your name:"));
	var theGiverExplain = document.createElement("span");
	theGiverExplain.className = "explain";
	theGiverExplain.appendChild(document.createTextNode(""));
	var theGiverField = document.createElement("input");
	theGiverField.setAttribute("name", "gift_from["+gid+"]");
	theGiverLine.appendChild(theGiverLabel);
	theGiverLine.appendChild(theGiverExplain);
	theGiverLine.appendChild(theGiverField);
	
	var theDelivName = "gift_deliv["+gid+"]";
	var theDelivLine = document.createElement("div");
	var theDelivLabel = document.createElement("div");
    	theDelivLabel.className = "giftlabel";
	theDelivLabel.appendChild(document.createTextNode("Deliver gift on:"));
// 	var theDelivField = document.createElement("input");
// 	theDelivField.setAttribute("name", theDelivName);
// 	theDelivField.setAttribute("value", giftDate(theGiftDate));
	var theDelivTimeSelect = dateSelect(deliverDefault.getDate()+1);
	theDelivTimeSelect.setAttribute("name", "gift_date["+gid+"]");
	var theDelivTZSelect = tzSelect("-5");
	theDelivTZSelect.setAttribute("name", "gift_tz["+gid+"]");

	theDelivLine.appendChild(theDelivLabel);
	theDelivLine.appendChild(document.createTextNode(" December "));
	theDelivLine.appendChild(theDelivTimeSelect);
	theDelivLine.appendChild(document.createTextNode(" , 2008 "));
	theDelivLine.appendChild(theDelivTZSelect);

	var theEnableLine = document.createElement("div");
	var theDelivEnable = document.createElement("input");
	theDelivEnable.setAttribute("type", "checkbox");
	theDelivEnable.setAttribute("name", "giftDeliverNow");
	theDelivEnable.onclick = function(){theDelivTimeSelect.disabled = theDelivEnable.checked;};
	theDelivEnableLabel = document.createElement("label");
	theDelivEnableLabel.appendChild(document.createTextNode("or gift immediately"));
	
	theEnableLine.appendChild(theDelivEnableLabel);
	theEnableLine.appendChild(theDelivEnable);
	
	var theRemoveRegion = document.createElement("div");
	theRemoveRegion.className = "giftRemoveRegion";
	var theRemoveAction = document.createElement("span");
	theRemoveAction.className = "giftRemoveAction";
	theRemoveAction.appendChild(document.createTextNode("remove"));
	theRemoveAction.onclick = function () { removeGift(theRegion); };
	theRemoveRegion.appendChild(theRemoveAction);
	
	theFields.appendChild(theEmailLine);
	theFields.appendChild(theConfirmEmailLine);
	theFields.appendChild(theGiverLine);
	theFields.appendChild(theDelivLine);
	theFields.appendChild(theEnableLine);
	theFields.appendChild(theRemoveRegion);
	
	theRegion.appendChild(theFields);
	
	return theRegion;
}

function addGift(inEmail, inDeliver) {
	var theEmail = inEmail || "";
	var theGiftDate = (inDeliver) ? new Date(inDeliver) : deliverDefault;
	var theGifts = document.getElementById("gifts");
	if (theGifts) {
		theGifts.appendChild(createGiftRegion(gifts, theEmail, theGiftDate));
		gifts++;
		giftsCount++;
		adjustCopiesField(1);
	} else {
		console.log("could not append gift region");
	}
}



function addGiftNew(inEmail, inDeliver) {
	var theEmail = inEmail || "";
	var theGiftDate = (inDeliver) ? new Date(inDeliver) : deliverDefault;
	var theGifts = document.getElementById("gifts");
	if (theGifts) {
		theGifts.appendChild(createGiftRegionNew(gifts, theEmail, theGiftDate));
		gifts++;
		giftsCount++;
		adjustCopiesField(1);
	} else {
		console.log("could not append gift region");
	}
}




function addGiftNewFromField(inEmail, inDeliver) {
    
    var theCopiesField = document.getElementById("giftcopiesField");
    
    if (theCopiesField) {
		while (theCopiesField.value > giftsCount)
		{
		  
		    var theEmail = inEmail || "";
        	var theGiftDate = (inDeliver) ? new Date(inDeliver) : deliverDefault;
        	var theGifts = document.getElementById("gifts");
        	if (theGifts) {
        		theGifts.appendChild(createGiftRegionNew(gifts, theEmail, theGiftDate));
        		gifts++;
        		giftsCount++;
        		
        	} else {
        		console.log("could not append gift region");
        	}
		    
		}
		
		if (theCopiesField.value < giftsCount)
		{
		    alert('Please click the remove button next to the gift you would like to delete.')
		    theCopiesField.value = giftsCount;
		}
	}
	
}

function showBadge()
{
    var copies = selfCopies() + giftCopies();
    var badgeField = document.getElementById("buy3badge");
    var image = "<img src='/winter/buy3more.gif' width='93' height='52'>";
   
    if (copies < 4)
    {
        switch(copies)
        {
            case 0:
                image = "<img src='/winter/buy4more.gif' width='93' height='52'>";
                break;
            case 1:
                image = "<img src='/winter/buy3more.gif' width='93' height='52'>";
                break;
            case 2:
                image = "<img src='/winter/buy2more.gif' width='93' height='52'>";
                break;
            case 3:
                image = "<img src='/winter/buy1more.gif' width='93' height='52'>";
                break;
        }
         badgeField.innerHTML = image;
    }
    
    if (copies >= 4){
        
        if (copies/4 >= 1)
        {
            image = "<img src='/winter/1free.gif' width='93' height='52'>";
             badgeField.innerHTML = image;
        }
        if (copies/4 >= 2)
        {
            image = "<img src='/winter/2free.gif' width='93' height='52'>";
             badgeField.innerHTML = image;
        } 
        if (copies/4 >= 3)
         {
               image = "<img src='/winter/3free.gif' width='93' height='52'>";
                badgeField.innerHTML = image;
        } 
         if (copies/4 >= 4)
          {
               image = "<img src='/winter/4free.gif' width='93' height='52'>";
                badgeField.innerHTML = image;
        } 
         if (copies/4 >= 5)
            {
                image = "<img src='/winter/5free.gif' width='93' height='52'>";
                 badgeField.innerHTML = image;
            }
    }
    
   
}




function removeGift(region) {
	if (region) {
		var theParent = region.parentNode;
		if (theParent) {
			theParent.removeChild(region);
			giftsCount--;
			adjustCopiesField(-1);
			showBadge();
		}
	}
}

function adjustCopiesField(amt) {
	var theCopiesField = document.getElementById("giftcopiesField");
	if (theCopiesField) {
		theCopiesField.value = parseInt(theCopiesField.value)+amt;
//		theCopiesField.onchange();
		var theVisibleCopiesField = document.getElementById("giftcopies");
		if (theVisibleCopiesField) {
			replaceText(theVisibleCopiesField, theCopiesField.value);
		}
		updatePriceFields();
	}
}

function giftDate(d) {
		// these need to be properly 0-padded, too
	return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
}

function tzSelect(inSel) {
	var sel = inSel || null;
	var theSelect = document.createElement("select");
	for (var tz in tzList) {
		var theOption = document.createElement("option");
		theOption.setAttribute("value", tz);
		theOption.appendChild(document.createTextNode(tzList[tz] + " (" + tz + ")"));
		if (sel == tz) {
			theOption.selected = true;
		}
		theSelect.appendChild(theOption);
	}
	return theSelect;
}

function timeSelect(inSel) {
	var sel = inSel || null;
	var theSelect = document.createElement("select");
	for (var tm in timeList) {
		var theOption = document.createElement("option");
		theOption.setAttribute("value", timeList[tm]);
		theOption.appendChild(document.createTextNode(tm));
		if (sel == tm) {
			theOption.selected = true;
		}
		theSelect.appendChild(theOption);
	}
	return theSelect;
}

function dateSelect(inSel) {
	var sel = inSel || null;
	var theSelect = document.createElement("select");
	for (var day = 1; day < 32; day++) {
		var theOption = document.createElement("option");
		theOption.setAttribute("value", day);
		theOption.appendChild(document.createTextNode(day));
		if (sel == day) {
			theOption.selected = true;
		}
		if (day < sel)
		{
		   theOption.disabled = true; 
		}
		theSelect.appendChild(theOption);
	}
	return theSelect;
}

function giftCopies() {
	return giftsCount;
}

function confirmEmail(email, confirm) {
	if (email != "" && confirm != "" && email != confirm) {
		alert("Please double-check the confirmation for the email address: " + email + ". It doesn't match.");
	}
}


