var myCard1;
var myCard2;
var mySelection;
var myTemplateID;
var myPhotoID;

function getStory(storyLabel) {
	location.href = "stories/countries/" + storyLabel + "/";
}

function copyShipping(myForm) {

	myForm.custBillingPrefix.selectedIndex = myForm.custPrefix.selectedIndex;
	myForm.custBillingPrefixOther.value = myForm.custPrefixOther.value;
	myForm.custBillingSuffix.selectedIndex = myForm.custSuffix.selectedIndex;
	myForm.custBillingSuffixOther.value = myForm.custSuffixOther.value;
	
	myForm.custBillingFirstName.value = myForm.custFirstName.value;
	myForm.custBillingMiddleName.value = myForm.custMiddleName.value;
	myForm.custBillingLastName.value = myForm.custLastName.value;

	myForm.custBillingAdd1.value = myForm.custAdd1.value;
	myForm.custBillingAdd2.value = myForm.custAdd2.value;
	myForm.custBillingCity.value = myForm.custCity.value;
	myForm.custBillingState.selectedIndex = myForm.custState.selectedIndex;
	if (myForm.custBillingState.selectedIndex == 0) {
		document.getElementById("billingStateBox").className = "cfLabel2";
		document.getElementById("custBillingProvince").style.display = "block";
	}
	myForm.custBillingProvince.value = myForm.custProvince.value;
	myForm.custBillingZip.value = myForm.custZip.value;
	myForm.custBillingCountry.selectedIndex = myForm.custCountry.selectedIndex;
	
}

var formSubmitted = false;

function checkForm() {
	
	if (formSubmitted) return false;
	else {
		formSubmitted = true;
		return true;
	}
	
}

var node_list = [];
var othernode_list = [];

function calcTotal() {
	
	var myTotal = 0;
	
	for (var i = 0; i<node_list.length; i++) {
		
		var myValue = 0;
		var e = document.getElementById(node_list[i]);
		
		if (e.options[e.selectedIndex].value == "other") {
			var myValue = document.getElementById(othernode_list[i]).value;
		} else {
			var myValue = e.options[e.selectedIndex].value;
		}
		
		myTotal += Math.abs(myValue);
		
	}
	
	document.getElementById('donationTotal').value = myTotal;
	
}

function goMulti() {

	document.getElementById('mfBodyMultiple').style.display = "block";
	document.getElementById('mfBodySingle').style.display = "none";
	document.getElementById('mfType').value = "multi";
	document.getElementById('mfNavSingle').className = "mfNav";
	document.getElementById('mfNavMultiple').className = "mfNav selected";
	
}

function goSingle() {

	document.getElementById('mfBodyMultiple').style.display = "none";
	document.getElementById('mfBodySingle').style.display = "block";
	document.getElementById('mfType').value = "single";
	document.getElementById('mfNavSingle').className = "mfNav selected";
	document.getElementById('mfNavMultiple').className = "mfNav";
	
}

function showDiv(divID) {
	
	var e = document.getElementById(divID);
	e.style.display = "block";
	
}

function hideDiv(divID) {
	
	var e = document.getElementById(divID);
	e.style.display = "none";
	
}

function checkCardInfo(idx,e) {
	switch(idx) {
		case 0:
			hideDiv("recipientInfo");
			hideDiv("senderInfo");
			hideDiv("printedInfo");
			hideDiv("emailInfo");
			hideDiv("yourEmailInfo");
			hideDiv("honorTemplateSelector");
			hideDiv("memoryTemplateSelector");
			break;
		case 1:
			showDiv("recipientInfo");
			showDiv("senderInfo");
			hideDiv("emailInfo");
			showDiv("printedInfo");
			hideDiv("yourEmailInfo");
			hideDiv("honorTemplateSelector");
			hideDiv("memoryTemplateSelector");
			break;
		case 2:
			showDiv("recipientInfo");
			showDiv("senderInfo");
			hideDiv("printedInfo");
			showDiv("emailInfo");
			showDiv("yourEmailInfo");
			break;
	}
}

function pickCards(e) {
	
	for (var i=0; i<e.form.giftType.length; i++) if (e.form.giftType[i].checked) myGiftType = e.form.giftType[i].value;
	
	if (e.form.cardType.selectedIndex == 2) {
		if (myGiftType == 1) {
			showDiv("honorTemplateSelector");
			hideDiv("memoryTemplateSelector");
		} else {
			hideDiv("honorTemplateSelector");
			showDiv("memoryTemplateSelector");				
		}
	}	
}

function goCards(e) {
	
	location.href = "cart-cards.php";
	e.stop();
	
}

function go2(e) {
	
	setSelection(this.form.ecardTemplateID,this.form.ecardPhotoID);
	gotoSet(2);
	e.stop();
	
}

function go1(e) {
	
	gotoSet(1);
	e.stop();
	
}

function goSubmit(e) {
	
	if (testEcards(this.form)) this.form.submit();
	
}

function gotoSet(idx) {
	
	$('prevButton').removeEvents();
	$('nextButton').removeEvents();
	
	switch(idx) {
		case 1:
			myCard1.slideIn();
			myCard2.slideOut();
			mySelection.slideOut();
			$('prevButton').addEvent("click",goCards);
			$('nextButton').addEvent("click",go2);
			$('nextButton').value = $('nextButton').originalValue;
			break;
		case 2:
			myCard1.slideOut();
			myCard2.slideIn();
			mySelection.slideIn();
			$('prevButton').addEvent("click",go1);
			$('nextButton').addEvent("click",goSubmit);
			$('nextButton').originalValue = $('nextButton').value;
			$('nextButton').value = "Save E-card and Continue";
			
			break;
	}
}

function setSelection(templateID,photoID) {
	
	// find the selected template and photo ids
	for (i=0; i<templateID.length; i++) {
		if (templateID[i].checked) {
			myTemplateID = templateID[i].value;
			break;
		}
	}

	for (i=0; i<photoID.length; i++) {
		if (photoID[i].checked) {
			myPhotoID = photoID[i].value;
			break;
		}
	}
	
	// get the images
	var myPhotoURL = document.getElementById('photo_' + myPhotoID).src;
	var myTemplateURL = document.getElementById('template_' + myTemplateID).src;
	
	// now put the images in the selection fieldset
	document.getElementById('card_selection_images').innerHTML = '<img src="' + myTemplateURL + '" /><img src="' + myPhotoURL + '" />';
	
}

function showOther(mySelect) {

	eName = mySelect.name.replace(/donationAmount/,"donationAmountOther");
	eNameDisp = eName + "_disp";
	
	ed = document.getElementById(eNameDisp);
	e = document.getElementById(eName);

	if (mySelect.options[mySelect.selectedIndex].value == "other") {
		ed.style.display = "inline";
		e.value = "";
	} else {
		ed.style.display = "none";
		e.value = "";
	}
	
}

function startGallery() {
	var myGallery = new gallery($('myGallery'), {
		timed: true,
		delay: 5000,
		fadeDuration: 1500,
		showInfopane: false,
		showCarousel: false
	});
}


function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}

function previewCard(myForm) {
	
	var imageFile = $$('#photo_' + myPhotoID).getProperty("alt");
	var cardFile = $$('#template_' + myTemplateID).getProperty("alt");
	var giftNames = "";
	
	for (i=0; i<myForm.elements.length; i++) {
		if (myForm.elements[i].type == "checkbox") if (myForm.elements[i].checked == true) giftNames += myForm.elements[i].title + ",";
	}
	
	// remove last comma
	giftNames = giftNames.substr(0,giftNames.length - 1);
	
	// alert('ec/' + cardFile + '?senderName=' + escape(myForm.ecardFrom.value) + '&recipientName='+ escape(myForm.ecardTo.value) + '&message=' + escape(myForm.ecardMessage.value) + '&giftNames=' + giftNames + '&image=' + escape(imageFile));
	openWin('ec/' + cardFile + '?e=0&o=0&senderName=' + escape(myForm.ecardFrom.value) + '&recipientName='+ escape(myForm.ecardTo.value) + '&message=' + escape(myForm.ecardMessage.value) + '&giftNames=' + giftNames + '&image=' + escape(imageFile),700,600);
	
}

function get_checked_gifts(myForm) {
	
	var giftNames = "";
	
	for (i=0; i<myForm.elements.length; i++) {
		if (myForm.elements[i].type == "checkbox") if (myForm.elements[i].checked == true) giftNames += myForm.elements[i].title + ",";
	}
	
	return giftNames;
	
}

function get_radio_value(myGroup) {
	if (myGroup.value == undefined) {
		for (var i=0; i < myGroup.length; i++)
			if (myGroup[i].checked)
				return myGroup[i].value;
		return "";
	} else return myGroup.value;
}

function testEcards(myForm) {

	var errorText = "";
	var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9-])+(\.([a-zA-Z0-9]{2,4})+)+$/;
	//var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9-])+\.([a-zA-Z0-9]{2,4})+$/;

	// check for required forms
	if (myForm.ecardFrom.value == "") errorText += "You must enter a sender name.\n";
	// if (myForm.ecardFromEmail.value == "") errorText += "You must enter a sender email address.\n";
	if (!filter.test(myForm.ecardFromEmail.value)) errorText += "You must enter a valid sender email address.\n";
	if (myForm.ecardTo.value == "") errorText += "You must enter a recipient name.\n";
	// if (myForm.ecardToEmail.value == "") errorText += "You must enter a recipient email address.\n";
	if (!filter.test(myForm.ecardToEmail.value)) errorText += "You must enter a valid recipient email address.\n";
	
	if (get_checked_gifts(myForm) == "") errorText += "You must select at least ONE gift.\n";
	
	if (errorText != "") {
		alert(errorText);
		return false;
	} else {
		return true;
	}
	
}

function monthlyYes() {
	
	document.getElementById("monthlyGiver").value = 1;
	document.getElementById("myForm").submit();
	
}

function monthlyNo() {
	
	document.getElementById("monthlyGiver").value = 0;
	document.getElementById("myForm").submit();

}

function checkCountry(countryObj) {
	
	if (countryObj.options[countryObj.selectedIndex].value != "USA") {
		
		if (countryObj.name == "custCountry") {
		
			countryObj.form.custState.selectedIndex = 0;
			document.getElementById("stateBox").className = "cfLabel2";
			document.getElementById("custProvince").style.display = "block";
			
		} else {
			
			countryObj.form.custBillingState.selectedIndex = 0;
			document.getElementById("billingStateBox").className = "cfLabel2";
			document.getElementById("custBillingProvince").style.display = "block";
			
		}
		
	} else {
		
		if (countryObj.name == "custCountry") {
		
			countryObj.form.custState.selectedIndex = 1;
			countryObj.form.custProvince.value = "";
			document.getElementById("stateBox").className = "cfLabel";
			document.getElementById("custProvince").style.display = "none";
			
		} else {
			
			countryObj.form.custBillingState.selectedIndex = 1;
			countryObj.form.custBillingProvince.value = "";
			document.getElementById("billingStateBox").className = "cfLabel";
			document.getElementById("custBillingProvince").style.display = "none";
			
		}
		
		
	}
	
}

function checkLength(e) {
	
	cc = document.getElementById("charCount");

	if (e.value.length > 500) {
		e.value = e.value.substr(0,500);
	}

	cc.innerHTML = e.value.length;
	
}
