// JavaScript Document

//-------------------------------------------------------------
//FUNCTION: moreInfo
//PARAMS:	url of window content, width and height of window
//PURPOSE:  creates a customized popup window centered to the users 
// 		    screen size
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function moreInfo(url, w, h) {
	var l = (window.screen.width-w)/2;
	var t = (window.screen.height-h)/2;
	if (l < 0) l = 0;
	if (t < 0) t = 0;
    popup = window.open(url,'target','width='+w+',height='+h+',top='+t+',left='+l+',scrollbars=1,resizable=1');
	popup.window.focus();
}



// CARDHOLDER FORM FUNCTIONS

//-------------------------------------------------------------
//FUNCTION: setCard1Name
//PARAMS:	none
//PURPOSE:  set Carholder 1 Name = Primary Name if usePrimary 
//			is checked
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setCard1Name(suffix) {
   //if ((!gift) || (gift && suffix=='_recipient')){
    if (suffix!='_donor'){
	if (document.cardholderform.varCardUsePrimary.checked == true) {
		// set Carholder 1 Name = Primary Name
		var firstName = document.cardholderform.varPrimaryFirst.value;
		//var firstName = document.cardholderform['varPrimaryFirst'+suffix].value;
		document.cardholderform.varCard1First.value= firstName;
		var middleName = document.cardholderform.varPrimaryMiddle.value;
        //var middleName = document.cardholderform['varPrimaryMiddle'+suffix].value;
		document.cardholderform.varCard1Middle.value = middleName;
		var lastName = document.cardholderform.varPrimaryLast.value;
        //var lastName = document.cardholderform['varPrimaryLast'+suffix].value;
		document.cardholderform.varCard1Last.value = lastName;		
		// debug: alert("Primary Carholder Name is:" + firstName + " " + middleName + " " + lastName);
	}
   }
}

//-------------------------------------------------------------
//FUNCTION: setCard1Title
//PARAMS:	i = index of radio button which is calling the function
//PURPOSE:  set Carholder 1 Name = Primary Name if usePrimary 
//			is checked
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setCard1Title(i, suffix) {
	if (document.cardholderform.varCardUsePrimary.checked == true) {
		// set Cardholder 1 title = Primary title
		//if ((!gift) || (gift && suffix=='_recipient')){
         if (suffix!='_donor'){
            document.cardholderform["varCard1Title"][i].checked = true;
            if (i < 4) {
                document.cardholderform.varCard1TitleOther.value = "";
            }
		}
		if (i < 4) {
		  document.getElementById("varPrimaryTitleOther" + suffix).value = "";
		}
		//if (i == 4) {
			// Other field is selected 
			// set Cardholder Other Title = Primary Other Title 
			//var otherTitle = document.cardholderform.varPrimaryTitleValue.value;
			//document.cardholderform.varCard1TitleOther.value = otherTitle;
			//document.cardholderform.varPrimaryTitleValue.focus();
		//} else {
        
			// Other field is NOT selected
			// clear Other fields
			//document.cardholderform.varCard1TitleOther.value = "";
			//document.cardholderform.varPrimaryTitleOther.value = "";
		//}
		document.cardholderform.varCardUsePrimary.checked = true;
	}
}

//-------------------------------------------------------------
//FUNCTION: setOtherButton
//PARAMS:	none
//PURPOSE:  select Other radio button when the Other text field 
// 			is in focus
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setOtherButton(suffix) {
	// select Primary Other button when Other title text in focus
	//document.cardholderform["varPrimaryTitle"][4].checked = true;
    document.cardholderform["varPrimaryTitle"+suffix][4].checked = true;
	if (document.cardholderform.varCardUsePrimary.checked == true) {
		// select Cardholder 1 Other button when Other title text in focus
		//if ((!gift) || (gift && suffix=='_recipient')){
          if (suffix!='_donor'){
    		document.cardholderform["varCard1Title"][4].checked = true;
    	}
	}	
}

//-------------------------------------------------------------
//FUNCTION: setOtherValue
//PARAMS:	none
//PURPOSE:  set Cardholder 1 Other title text to Primary Other title text
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setOtherValue (suffix) {
	if (document.cardholderform.varCardUsePrimary.checked == true) {
		// set Cardholder 1 Other title text to Primary Other title text
		//if ((!gift) || (gift && suffix=='_recipient')){
          if (suffix!='_donor'){
    		//var otherTitle = document.cardholderform.varPrimaryTitleOther.value;
            var otherTitle = document.getElementById("varPrimaryTitleOther" + suffix).value;
    		document.cardholderform.varCard1TitleOther.value = otherTitle;
    	}
	}
	
}

//-------------------------------------------------------------
//FUNCTION: setCard1OtherButton
//PARAMS:	none
//PURPOSE:  select Card 1 Other radio button when Card 1 Other 
//			text field is in focus and deselect usePrimary
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setCard1OtherButton() {
	// select Cardholder 1 Other button when Other title text in focus
	document.cardholderform["varCard1Title"][4].checked = true;
	// deselect usePrimary
	document.cardholderform.varCardUsePrimary.checked = false;
}

//-------------------------------------------------------------
//FUNCTION: deselectUsePrimary
//PARAMS:	i = index of radio button which is calling the function
//PURPOSE:  called when user tries to change Cardholder 1 title field,
// 			delsects usePrimary
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function deselectUsePrimary(i) {
	if (i != 4) {
		document.cardholderform.varCard1TitleOther.value = "";
		//document.cardholderform.varPrimaryTitleValue.value = "";
	}	
	document.cardholderform.varCardUsePrimary.checked = false;	
}

//-------------------------------------------------------------
//FUNCTION: deselectCard1Name
//PARAMS:	none
//PURPOSE:  called when user tries to change a Cardholder 1 name 
//			field, delsects usePrimary
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function deselectCard1Name() {
	document.cardholderform.varCardUsePrimary.checked = false;		
}

//-------------------------------------------------------------
//FUNCTION: toggleCard1Name
//PARAMS:	none
//PURPOSE:  called when user selects usePrimary, sets the name
// 			and title of card 1 = primary
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function toggleCard1Name() {
	if (document.cardholderform.varCardUsePrimary.checked == true) {
		// set the card name
		setCard1Name();
		// set title, including other		
		for (i=0; i < 5; i++) {
			// loop through buttons to determine which is selected - if it's four then populate Other fields
			if (document.cardholderform["varPrimaryTitle"][i].checked == true) {
				// check of button
				document.cardholderform["varCard1Title"][i].checked = true;
				// set Other title text field
				document.cardholderform.varCard1TitleOther.value = document.cardholderform.varPrimaryTitleOther.value;
			}
		}
		
	} else {
		document.cardholderform.varCard1First.value="";
		document.cardholderform.varCard1Middle.value="";
		document.cardholderform.varCard1Last.value="";
		document.cardholderform.varCard1TitleOther.value = "";
		for(i=0; i < 5; i++){
			document.cardholderform["varCard1Title"][i].checked = false;
		}
	}
}


// GROUPS FORM - FRIENDS
//
//
//-------------------------------------------------------------
//FUNCTION: setFriendsPrice
//PARAMS:	name (acronymn of friends group), price (dependent on term), 
// 			and total number of friends included for selected membership
//PURPOSE:  Displays the price of the friends group if the checkbox is checked
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setFriendsPrice(friendsName, friendsPrice, totalIncluded) {
	//  number of friends currently displayed as being included with the membership ie. FREE
	var currentIncluded = getFreeFriends();
	if (document.groups[friendsName].checked) {
		// check box is selected
		if (totalIncluded > currentIncluded) {
			// set current friends to included
			document.groups[friendsName + "_displayprice"].value = "FREE";
			document.groups[friendsName + "_price"].value = 0;
		} else {
			// calculate current friends price based on term
			document.groups[friendsName + "_displayprice"].value = "$" + friendsPrice;
			document.groups[friendsName + "_price"].value = "$" + friendsPrice;		
		}
	} else {
		// check box is NOT selected
		//alert(document.groups[friendsName+ "_displayprice"].value);
		if(document.groups[friendsName+ "_displayprice"].value == "FREE") {
			// the deselect friends was included - need to adjust the other prices so that their free friends is visible
			// clear price
			document.groups[friendsName + "_displayprice"].value = "";
			document.groups[friendsName + "_price"].value = "";			
			// get a list of all selected friends
			var friendsList = new Array();
			friendsList = getPricedFriends();
			//check array count
			var countFriends = friendsList.length;
    		//alert(countFriends);
			// alert(friendsList[0] + " " + friendsList[1] + " " + friendsList[2] + " " + friendsList[3] + " " + friendsList[4] + " " + friendsList[5]);
			// set the price of the first returned friend in the list to FREE
			if (countFriends > 0) {  // to prevent null object error BT FEB 2007
    			document.groups[friendsList[0]+ "_price"].value = 0;
	       		document.groups[friendsList[0]+ "_displayprice"].value = "FREE";
	       	}
		} else {
			// the deselected friend had a price - clear price
			document.groups[friendsName + "_displayprice"].value = "";
			document.groups[friendsName + "_price"].value = "";
		}
	}
}

//-------------------------------------------------------------
//FUNCTION: getPricedFriends
//PARAMS:	none
//PURPOSE:  get a list of all selected friends groups which are not free
//RETURNS:	array, names of all the priced friends
//AUTHOR:	amya
//-------------------------------------------------------------
function getPricedFriends() {
//function getSelectedFriends() {
	var friends = new Array ("fsa", "fcc", "ficc", "ftc", "fea", "fmss", "fae", "fa");
	var pricedFriends = new Array();
	j = 0;
	for (i=0; i < 8; i++) {
		if (document.groups[friends[i]].checked) {
			// current friend is checked
			if (document.groups[friends[i]+ "_displayprice"].value != "FREE") {
				// current value is not included - add to list
				pricedFriends[j] = friends[i];
				j++;
			}
		}
	}
	// return list of all selected non-free friends
	return pricedFriends;
}

//-------------------------------------------------------------
//FUNCTION: getFreeFriends()
//PARAMS:	none
//PURPOSE:  get a list of all selected friends groups which are free
//RETURNS:	integer, number of free friends currently selected
//AUTHOR:	amya
//-------------------------------------------------------------
function getFreeFriends() {
	var numFree = 0;
	var friends = new Array ("fsa", "fcc", "ficc", "ftc", "fea", "fmss", "fae", "fa");
	for (i=0; i < 8; i++) {
		if (document.groups[friends[i]+ "_displayprice"].value == "FREE") {
			// increment if value is free
			numFree++;
		}
	}
	return numFree;
}



// GROUPS FORM - EXPLORERS
//
//
//-------------------------------------------------------------
//FUNCTION: setExplorersPrice
//PARAMS:	explorerNum, state (on or off)
//PURPOSE:  Displays the price and checks the checkbox if there is 
//			name entered for an explorer
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setExplorersPrice(explorerNum, explorersPrice, totalIncluded, state) {
	var currentIncludedExplorers = getFreeExplorers();
	// alert (currentIncludedExplorers);
	// alert("set " + explorerNum +" "+ explorersPrice +" "+ totalIncluded);
	if (state == "1") {
		// onFocus
		if (totalIncluded > currentIncludedExplorers) {
			document.groups[explorerNum + "_price"].value = 0;	
			document.groups[explorerNum + "_displayprice"].value =  "FREE";	
			document.groups[explorerNum + "_check"].checked = true;
		} else {
			if (document.groups[explorerNum + "_displayprice"].value != "FREE") {
				// prevent a price change if value is already free
				document.groups[explorerNum + "_displayprice"].value = "$" + explorersPrice;	
				document.groups[explorerNum + "_price"].value = explorersPrice;	
				document.groups[explorerNum + "_check"].checked = true;
			}
		}
	} 	
	if (state == "0") {		
		// onBlur
		if (document.groups[explorerNum].value == "") {
			// name is empty
			if (document.groups[explorerNum + "_displayprice"].value == "FREE") {
				document.groups[explorerNum + "_displayprice"].value = "";	
				document.groups[explorerNum + "_price"].value = "";	
				document.groups[explorerNum + "_check"].checked = false;
				// reset another one to free
				// get a list of all explorers friends
				var explorersList = new Array();
				explorersList = getPricedExplorers();
				//alert(explorersList[0] + " " + explorersList[1] + " " + explorersList[2] + " " + explorersList[3]);
				// set the price of the first returned friend in the list to FREE
				document.groups[explorersList[0]+ "_displayprice"].value = "FREE";
				document.groups[explorersList[0]+ "_price"].value = 0;
			} else {
				// name field is null 
				document.groups[explorerNum + "_price"].value = "";	
				document.groups[explorerNum + "_displayprice"].value = "";	
				document.groups[explorerNum + "_check"].checked = false;
			}
		}
	}
}

//-------------------------------------------------------------
//FUNCTION: checkExplorers
//PARAMS:	explorerNum
//PURPOSE:  Displays the price and focuses the explorer name 
//			text field if the checkbox is checked
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function checkExplorers(explorerNum, explorersPrice, totalIncluded) {
	var currentIncludedExplorers = getFreeExplorers();
	//alert(currentIncludedExplorers);
	//alert("check " + explorerNum +" "+ explorersPrice +" "+ totalIncluded);
	if (document.groups[explorerNum + "_check"].checked == true) {
		// checked
		if (totalIncluded > currentIncludedExplorers) {
			// set price to FREE
			document.groups[explorerNum + "_displayprice"].value = "FREE";	
			document.groups[explorerNum + "_price"].value = 0;	
			// focus on name field
			document.groups[explorerNum].focus();
		} else {
			if (document.groups[explorerNum + "_displayprice"].value != "FREE") {
				// set price
				document.groups[explorerNum + "_displayprice"].value = "$" + explorersPrice;	
				document.groups[explorerNum + "_price"].value = explorersPrice;	
				// focus on name field
				document.groups[explorerNum].focus();
			}
		}
	} else {
		// not checked
		if (document.groups[explorerNum + "_displayprice"].value == "FREE") {
			//alert("was free");
			// 	need to set another explorer to free
			// clear current name and price
			document.groups[explorerNum].value = "";
			document.groups[explorerNum + "_price"].value = "";	
			document.groups[explorerNum + "_displayprice"].value = "";	
			//alert ("hi");
			var explorersList = new Array();
			explorersList = getPricedExplorers();	
			//alert(explorersList[0]+ " " + explorersList[1] + " " + explorersList[2] + " " + explorersList[3]);
			// set the price of the first returned friend in the list to FREE
			document.groups[explorersList[0]+ "_price"].value = 0;
			document.groups[explorersList[0]+ "_displayprice"].value = "FREE";
		} else {
			// name field is null 
			document.groups[explorerNum].value = "";
			document.groups[explorerNum + "_price"].value = "";	
			document.groups[explorerNum + "_displayprice"].value = "";	
		}
	}
}
//-------------------------------------------------------------
//FUNCTION: getPricedExplorers
//PARAMS:	none
//PURPOSE:  get a list of all selected friends groups which are free
//RETURNS:	array, list of all selected priced explorers
//AUTHOR:	amya
//-------------------------------------------------------------
function getPricedExplorers() {
	//function getSelectedExplorers() {
	var selectedExplorers = new Array();
	j = 0;
	for (i=1; i < 5; i++) {		
		if (document.groups["explorerName"+ i + "_check"].checked) {
			// explorer is checked
			if (document.groups["explorerName"+ i + "_displayprice"].value != "FREE") {
		 		// value is not free
				selectedExplorers[j] = "explorerName"+ i;
				j++;
			}
		}
	}
	// return list of all selected priced explorers
	return selectedExplorers;
}

//-------------------------------------------------------------
//FUNCTION: getFreeExplorers
//PARAMS:	none
//PURPOSE:  get a list of all selected free explorers
//RETURNS:	integer, number of free explorers currently selected
//AUTHOR:	amya
//-------------------------------------------------------------
function getFreeExplorers() {
//function getNumIncludedExplorers() {
	var numFreeEx = 0;
	for (i=1; i < 5; i++) {		
		if (document.groups["explorerName" + i + "_displayprice"].value == "FREE") {
			// increment if value is free
			numFreeEx++;
		}
	}
	return numFreeEx;
}





// GROUPS FORM - DONATIONS
//
//
//-------------------------------------------------------------
//FUNCTION: setDonationFreq
//PARAMS:	frequ
//PURPOSE:  Deselcts old values when a switch in donation frequency is made
//RETURNS:	nothing
//AUTHOR:	amya
//-------------------------------------------------------------
function setDonationFreq(freq) {
	if (freq == "onetime") {
		// clear montly checkbox, radio buttons and other value
		document.groups["varMonthlyDonation"].checked = false;
		for (i=0; i < document.groups["varMonthlyValue"].length; i++) {
			document.groups["varMonthlyValue"][i].checked = false;
		}
		document.groups["varOtherMonthlyValue"].value = "";
	} else if (freq == "monthly") {
		// clear one-time checkbox, radio buttons and other value
		document.groups["varOneTimeDonation"].checked = false;
		for (i=0; i < document.groups["varOneTimeValue"].length; i++) {
			document.groups["varOneTimeValue"][i].checked = false;
		}
		document.groups["varOtherOneTimeValue"].value = "";
	}
}

function setDonationOther(freq) {
		if (freq == "monthly") {
			document.groups["varMonthlyValue"][4].checked = true;
			document.groups["varMonthlyDonation"].checked = true;
			document.groups["varOneTimeDonation"].checked = false;
			setDonationFreq("monthly");
		} else if (freq == "onetime") {
			document.groups["varOneTimeValue"][4].checked = true;
			document.groups["varMonthlyDonation"].checked = false;
			document.groups["varOneTimeDonation"].checked = true;
			setDonationFreq("onetime");
			
		}
}


function setDonationAmount(freq, value) {
	if (freq == "monthly") {
		// adjust checkboxes
		document.groups["varMonthlyDonation"].checked = true;
		document.groups["varOneTimeDonation"].checked = false;		
		setDonationFreq("monthly");
		if (value == "num") {
			// clear other
			document.groups["varOtherMonthlyValue"].value = "";	
		}
	} else if (freq == "onetime") {
		document.groups["varMonthlyDonation"].checked = false;
		document.groups["varOneTimeDonation"].checked = true;
		setDonationFreq("onetime");
		if (value == "num") {
			// clear other
			document.groups["varOtherOneTimeValue"].value = "";	
		}
	}
}

//moved from php
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		question_01_over = newImage("graphics/question_01-over.gif");
		preloadFlag = true;
		//alert('done');
	}
}
preloadImages();

function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=356,height=233,scrollbars=no');
return false;
}

