var layerRef = "document.getElementById('";
var styleRef = "').style.";
var fbBasket = "";
var fpNormalAmount = "";

/* Set Donation Type onBodyLoad */
function SetIndex()
{
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0)
		{
			var  val = parms[i].substring(pos+1);
			switch(val) {
				
				case "ZABIHA":
					document.Form.dPrefs.selectedIndex = 18
					document.Form.MC_zabihacountry.selectedIndex = 0
					document.Form.MC_zZabihaDetails.selectedIndex = 0
					document.Form.desc.value = "Zabiha";
					document.Form.cartId.value = "Zabiha";
					document.Form.MC_zZabihaQ.value = "";
					document.getElementById('zZselect').style.visibility = 'visible'
					document.getElementById('zZselect').style.display = 'block'
					document.getElementById('zZabiha').style.visibility = 'visible'
					document.getElementById('zZabiha').style.display = 'block'
					document.getElementById('zAmount').style.visibility = 'hidden'
					document.getElementById('zAmount').style.display = 'none'
					document.getElementById('zOrphan').style.visibility = 'hidden'
					document.getElementById('zOrphan').style.display = 'none'
					document.getElementById('zFoodbank').style.visibility = 'hidden'
					document.getElementById('zFoodbank').style.display = 'none'
				break

				case "ZKT":
					document.Form.dPrefs.selectedIndex = 0
					document.Form.desc.value = "Zakat";
				break
				
				case "LEBN":
					document.Form.dPrefs.selectedIndex = 1
					document.Form.desc.value = "Lebanon Humanitarian Crisis";
				break
				
				case "INEQ":
					document.Form.dPrefs.selectedIndex = 2
					document.Form.desc.value = "Indonesia Earthquake Fund";
				break
				
				case "SAEQ":
					document.Form.dPrefs.selectedIndex = 3
					document.Form.desc.value = "South Asia Earthquake Fund";
				break    
				
				case "FoodBank":
					document.Form.dPrefs.selectedIndex = 4
					document.Form.normalAmt.selectedIndex = 0
					document.Form.MC_orpcountry.selectedIndex = 0
					document.Form.desc.value = "ICNA Relief Food Bank";
					document.Form.amount.value = "";
					document.Form.cartId.value = "FoodBank";
					document.Form.futurePayType.value = "";
					document.Form.option.value = "";
					document.Form.startDelayMult.value = "";
					document.Form.startDelayUnit.value = "";
					document.getElementById('zAmount').style.visibility = 'hidden'
					document.getElementById('zAmount').style.display = 'none'
					document.getElementById('zOrphan').style.visibility = 'hidden'
					document.getElementById('zOrphan').style.display = 'none'
					document.getElementById('zFoodbank').style.visibility = 'visible'
					document.getElementById('zFoodbank').style.display = 'block'
				break
				
				case "Orphan":
					document.Form.dPrefs.selectedIndex = 5
					document.Form.fbtype.selectedIndex = 0
					document.Form.delay.selectedIndex = 0
					document.Form.nFamilies.value = "";
					document.Form.desc.value = "Orphan / Child Sponsorship";
					document.Form.amount.value = "";
					document.Form.cartId.value = "Orphan";
					document.Form.futurePayType.value = "regular";
					document.Form.option.value = "0";
					document.Form.startDelayMult.value = "1";
					document.Form.startDelayUnit.value = "1";
					document.getElementById('zAmount').style.visibility = 'hidden'
					document.getElementById('zAmount').style.display = 'none'
					document.getElementById('zOrphan').style.visibility = 'visible'
					document.getElementById('zOrphan').style.display = 'block'
					document.getElementById('zFoodbank').style.visibility = 'hidden'
					document.getElementById('zFoodbank').style.display = 'none'
				break
				default:
				document.Form.dPrefs.selectedIndex = 19
			}
		}
	}
}

/*	Donation Form Check	*/
function Form_Validator(theForm)
{
	if (theForm.dPrefs.value != "zOrphan" & theForm.dPrefs.value != "zFoodbank" & theForm.dPrefs.value != "zZabiha")  
	{
		if (theForm.amount.value == "")
		{
			alert("Please enter a valid amount in \"Donation Amount\".");
			theForm.amount.focus();
			return (false);
		}
		if (theForm.amount.value.length > 5)
		{
			alert("More than 5 digits are not allowed in \"Donation Amount\".");
			theForm.amount.focus();
			return (false);
		}
		var checkOK = "0123456789-.";
		var checkStr = theForm.amount.value;
		var allValid = true;
		var validGroups = true;
		var decPoints = 0;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
			if (ch == ".")
			{
				allNum += ".";
				decPoints++;
			}
			else
			allNum += ch;
		}
		if (!allValid)
		{
			alert("Please enter only digits in the \"Donation Amount\".");
			theForm.amount.focus();
			return (false);
		}
		if (decPoints > 1 || !validGroups)
		{
			alert("Please enter a valid number in the \"Donation Amount\".");
			theForm.amount.focus();
			return (false);
		}
		var chkVal = allNum;
		var prsVal = parseFloat(allNum);
		if (chkVal != "" && !(prsVal >= "10.00"))
		{
			alert("Please enter a value greater than or equal to \"10.00\" in the \"Donation Amount\".");
			theForm.amount.focus();
			return (false);
		}
	}

	if (theForm.dPrefs.value == "zOrphan")
	{
		if (theForm.normalAmt.value == "0")
		{
			alert("Please select one of the options for Amount.");
			theForm.normalAmt.focus();
			return (false);
		}
	}
	if (theForm.dPrefs.value == "zFoodbank")
	{
		if (theForm.fbtype.value == "zSelect")
		{
			alert("Please select one of the options for Food Basket.");
			theForm.fbtype.focus();
			return (false);
		}
	}

	/* Number of Families */
	if (theForm.dPrefs.value == "zFoodbank")  
	{
		if (theForm.nFamilies.value == "")
		{
			alert("Please enter number of families.");
			theForm.nFamilies.focus();
			return (false);
		}
		if (theForm.nFamilies.value > 10)
		{
			alert("More than 10 families are not allowed in \"Number of Families\".");
			theForm.nFamilies.focus();
			return (false);
		}
		var checkOK2 = "0123456789-.";
		var checkStr2 = theForm.nFamilies.value;
		var allValid2 = true;
		var validGroups2 = true;
		var decPoints2 = 0;
		var allNum2 = "";
		for (i1 = 0;  i1 < checkStr2.length;  i1++)
		{
			ch2 = checkStr2.charAt(i1);
			for (j1 = 0;  j1 < checkOK2.length;  j1++)
			if (ch2 == checkOK2.charAt(j1))
			break;
			if (j1 == checkOK2.length)
			{
				allValid2 = false;
				break;
			}
			if (ch2 == ".")
			{
				allNum2 += ".";
				decPoints2++;
			}
			else
			allNum2 += ch2;
		}
		if (!allValid2)
		{
			alert("Please enter only digits in the \"Number of Families\".");
			theForm.nFamilies.focus();
			return (false);
		}
		if (decPoints2 > 1 || !validGroups2)
		{
			alert("Please enter a valid number in the \"Number of Families\".");
			theForm.nFamilies.focus();
			return (false);
		}
		var chkVal2 = allNum2;
		var prsVal2 = parseFloat(allNum2);
		if (chkVal2 != "" && !(prsVal2 >= "1"))
		{
			alert("Please enter a value greater than or equal to \"1\" in the \"Number of Families\".");
			theForm.nFamilies.focus();
			return (false);
		}
	}


	if (theForm.dPrefs.value == "zFoodbank" & theForm.fbtype.value != "zSelect" & theForm.delay.value == "none")
	{
		alert("Please select one of the options One Time or Monthly.");
		theForm.delay.focus();
		return (false);
	}
	
/* Zabiha Options */

	if (theForm.dPrefs.value == "zZabiha")
	{
		if (theForm.MC_zZabihaQ.value == "")
		{
			alert("Please enter the Quantity")
			theForm.MC_zZabihaQ.focus();
			return (false);
		}
		if (theForm.MC_zabihacountry.value == "zZselect")
		{
			alert("Please select one of the Zabiha Country & Zabiha Details.");
			theForm.MC_zabihacountry.focus();
			return (false);
		}	
		if (theForm.MC_zZabihaDetails.value == "NONE")
		{
			alert("Please select one Goat/Sheep or Cow/Buffalo.");
			theForm.MC_zZabihaDetails.focus();
			return (false);
		}
		if (theForm.MC_zabihacountry.value == "zZselect" & theForm.MC_zZabihaDetails.value != "NONE")
		{
			alert("Please select Zabiha country.");
			theForm.MC_zabihacountry.focus();
			return (false);
		}
	}
/* Zabiha Options End */

	if (theForm.name.value == "")
	{
		alert("Please enter your name.");
		theForm.name.focus();
		return (false);
	}
	if (theForm.address.value == "")
	{
		alert("Please enter your address.");
		theForm.address.focus();
		return (false);
	}
	if (theForm.address.value.length > 200)
	{
		alert("Please enter a valid address.");
		theForm.address.focus();
		return (false);
	}
	if (theForm.MC_city.value == "")
	{
		alert("Please enter city name.");
		theForm.MC_city.focus();
		return (false);
	}
	if (theForm.MC_city.value.length > 30)
	{
		alert("Please enter a valid city name");
		theForm.MC_city.focus();
		return (false);
	}
	if (theForm.MC_state.value == "")
	{
		alert("Please enter state name.");
		theForm.MC_state.focus();
		return (false);
	}
	if (theForm.MC_state.value.length > 30)
	{
		alert("Please enter a valid state name");
		theForm.MC_state.focus();
		return (false);
	}
	if (theForm.postcode.value == "")
	{
		alert("Please enter your zip/postal code. This field can not be left blank, type NA if no zip/postal code");
		theForm.postcode.focus();
		return (false);
	}
	if (theForm.postcode.value.length > 30)
	{
		alert("Please enter a valid zip/postal code");
		theForm.postcode.focus();
		return (false);
	}
	if (theForm.country.value == "")
	{
		alert("Please select country name");
		theForm.country.focus();
		return (false);
	}
	if (theForm.tel.value == "")
	{
		alert("Please enter your Phone Number for us to contact you.");
		theForm.tel.focus();
		return (false);
	}
	if (theForm.email.value == "")
	{
		alert("Please type your email address for transaction confirmation receipt");
		theForm.email.focus();
		return (false);
	}
	return (true);
}

/* Function for Change of Description */
function prefDetails(div){

	var arrayDiv = "";
	arrayDiv = ['zOrphan','zFoodbank','zZabiha','zOther'];
	sDetail = div.options[div.selectedIndex].value;

	/* define WorldPay variables */
	if (sDetail == "zOther") {
		document.Form.desc.value = "Other";
		document.Form.cartId.value = "Other";
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
		document.Form.normalAmount.value = "";
		document.Form.amount.value = "";
	}	
	if (sDetail == "zZabiha") {
		document.Form.desc.value = "Global Zabiha Program";
		document.Form.cartId.value = "Zabiha";
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
		document.Form.normalAmount.value = "";
		document.Form.amount.value = "";
	}
	if (sDetail == "zOrphan") {
		document.Form.desc.value = "Orphan / Child Sponsorship";
		document.Form.amount.value = "";
		document.Form.cartId.value = "Orphan";
		document.Form.futurePayType.value = "regular";
		document.Form.option.value = "0";
		document.Form.startDelayMult.value = "1";
		document.Form.startDelayUnit.value = "1";
		document.Form.fbtype.selectedIndex = 0
		document.Form.delay.selectedIndex = 0
		document.Form.nFamilies.value = "";
	}
	if (sDetail == "zFoodbank") {
		document.Form.desc.value = "ICNA Relief Food Bank";
		document.Form.amount.value = "";
		document.Form.cartId.value = "FoodBank";
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
		document.Form.normalAmt.selectedIndex = 0
		document.Form.MC_orpcountry.selectedIndex = 0
	}
	if (sDetail != "zFoodbank" & sDetail != "zOrphan" & sDetail != "zZabiha") {
		document.Form.desc.value = sDetail;
		document.Form.cartId.value = "Donation";
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
		document.Form.normalAmount.value = "";
		document.Form.amount.value = "";
	}

	/* define tables to display accordingly */
	if (sDetail != "zOrphan" & sDetail != "zFoodbank" & sDetail != "zZabiha") {
		document.getElementById('zAmount').style.visibility = 'visible'
		document.getElementById('zAmount').style.display = 'block'
	}
	else {
		document.getElementById('zAmount').style.visibility = 'hidden'
		document.getElementById('zAmount').style.display = 'none'
	}
	for(var i = 0; i <arrayDiv.length; i++){
	
		eval(layerRef + arrayDiv[i] + styleRef + "visibility = 'hidden'");
		eval(layerRef + arrayDiv[i] + styleRef + "display = 'none'");
	}
	eval(layerRef + sDetail + styleRef + "visibility = 'visible'");
	eval(layerRef + sDetail + styleRef + "display = 'block'");
}

/* Function for FuturePay Option for Orphan Sponsorship */
function fPayAmount(nAmt) {

	fpAmt = nAmt.options[nAmt.selectedIndex].value;

	if (fpAmt == "0") {
		document.Form.intervalMult.value = "";
		document.Form.intervalUnit.value = "";
		document.Form.normalAmount.value = "";
		document.Form.amount.value = "";
		fpNormalAmount = "";
	}
	if (fpAmt == "30") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "30";
		document.Form.amount.value = "";
		fpNormalAmount = "30";
	}
	if (fpAmt == "360") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "4";
		document.Form.normalAmount.value = "360";
		document.Form.amount.value = "";
		fpNormalAmount = "360";
	}
	if (fpAmt == "60") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "60";
		document.Form.amount.value = "";
		fpNormalAmount = "60";
	}
	if (fpAmt == "720") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "4";
		document.Form.normalAmount.value = "720";
		document.Form.amount.value = "";
		fpNormalAmount = "720";
	}
	if (fpAmt == "90") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "90";
		document.Form.amount.value = "";
		fpNormalAmount = "90";
	}
	if (fpAmt == "1080") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "4";
		document.Form.normalAmount.value = "1080";
		document.Form.amount.value = "";
		fpNormalAmount = "1080";
	}
	if (fpAmt == "120") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "120";
		document.Form.amount.value = "";
		fpNormalAmount = "120";
	}
	if (fpAmt == "20") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "20";
		document.Form.amount.value = "";
		fpNormalAmount = "20";
	}
	if (fpAmt == "40") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "40";
		document.Form.amount.value = "";
		fpNormalAmount = "40";
	}
	if (fpAmt == "150") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "150";
		document.Form.amount.value = "";
		fpNormalAmount = "150";
	}
	if (fpAmt == "390") {
		document.Form.intervalMult.value = "1";
		document.Form.intervalUnit.value = "3";
		document.Form.normalAmount.value = "390";
		document.Form.amount.value = "";
		fpNormalAmount = "390";
	}
}

/* Orphan Sponsorship Country Select */
function cChange(cSelect) {

	country = cSelect.options[cSelect.selectedIndex].value;
	
	if (country == "NONE") {
		document.Form.desc.value = "Orphan/Child Sponsorship"
	}
	if (country == "SAEQ") {
		document.Form.desc.value = "Orphan/Child Sponsorship for South Asia Earthquake"
	}
	if (country == "PAK") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Pakistan"
	}
	if (country == "KAS") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Kashmir"
	}
	if (country == "IND") {
		document.Form.desc.value = "Orphan/Child Sponsorship for India"
	}
	if (country == "AFG") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Afghanistan"
	}
	if (country == "BAN") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Bangladesh"
	}
	if (country == "KEN") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Kenya"
	}
	if (country == "SIL") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Sierra Leone"
	}
	if (country == "SRL") {
		document.Form.desc.value = "Orphan/Child Sponsorship for Sri Lanka"
	}
}

/*	Food Bank Details Change Function	*/
function fbDetails(div2){

	var arrayDiv2 = "";
	fDetail = div2.options[div2.selectedIndex].value;
	arrayDiv2 = ['zSelect','zBasket1','zBasket2','zBasket3'];
	
	/* define WorldPay variables */
	if (fDetail == "zSelect") {
		document.Form.normalAmount.value = "";
		document.Form.amount.value = "";
		document.Form.nFamilies.value = "";
		fpNormalAmount = "";
	}
	if (fDetail == "zBasket1") {
		document.Form.normalAmount.value = "25";
		document.Form.amount.value = "25";
		fbBasket = "25";
		fpNormalAmount = "25";
	}
	if (fDetail == "zBasket2") {
		document.Form.normalAmount.value = "35";
		document.Form.amount.value = "35";
		fbBasket = "35";
		fpNormalAmount = "35";
	}
	if (fDetail == "zBasket3") {
		document.Form.normalAmount.value = "50";
		document.Form.amount.value = "50";
		fbBasket = "50";
		fpNormalAmount = "50";
	}
	
	for(var i = 0; i <arrayDiv2.length; i++){
	
		eval(layerRef + arrayDiv2[i] + styleRef + "visibility = 'hidden'");
		eval(layerRef + arrayDiv2[i] + styleRef + "display = 'none'");
	}
	eval(layerRef + fDetail + styleRef + "visibility = 'visible'");
	eval(layerRef + fDetail + styleRef + "display = 'block'");
}

/* Food Bank Number of Families x Food Basket */
function fbFamilies() {
	var fMulti = document.Form.nFamilies.value;
	if (fMulti > 10) {
		alert("More than 10 families are not allowed")
		document.Form.nFamilies.focus();
	}
	else {
		document.Form.desc.value = "ICNA Relief Food Bank: F:"+fMulti+", B:"+fbBasket
		fpNormalAmount = fMulti * fbBasket
	}
}


/*	Food Bank Details / Delay Function	*/
function fbDelay(div3){

	var arrayDiv3 = "";
	fDelay = div3.options[div3.selectedIndex].value;
	arrayDiv3 = ['none','oneTime','monthly'];

	if (fDelay == "none") {
		document.Form.intervalMult.value = ""
		document.Form.intervalUnit.value = ""
		document.Form.normalAmount.value = ""
		document.Form.amount.value = ""
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
	}
	if (fDelay == "oneTime") {
		document.Form.intervalMult.value = ""
		document.Form.intervalUnit.value = ""
		document.Form.normalAmount.value = ""
		document.Form.futurePayType.value = "";
		document.Form.option.value = "";
		document.Form.startDelayMult.value = "";
		document.Form.startDelayUnit.value = "";
		document.Form.amount.value = fpNormalAmount
	}
	if (fDelay == "monthly") {
		document.Form.intervalMult.value = "1"
		document.Form.intervalUnit.value = "3"
		document.Form.amount.value = ""
		document.Form.futurePayType.value = "regular";
		document.Form.option.value = "0";
		document.Form.startDelayMult.value = "1";
		document.Form.startDelayUnit.value = "1";
		document.Form.normalAmount.value = fpNormalAmount
	}
	
	for(var i = 0; i <arrayDiv3.length; i++){
	
		eval(layerRef + arrayDiv3[i] + styleRef + "visibility = 'hidden'");
		eval(layerRef + arrayDiv3[i] + styleRef + "display = 'none'");
	}
	eval(layerRef + fDelay + styleRef + "visibility = 'visible'");
	eval(layerRef + fDelay + styleRef + "display = 'block'");
}

/* Other Memo Function */
function memo() {
	if (document.Form.zMemo.value == "") {
		alert("Please type memo details!!!")
		document.Form.zMemo.focus();
	}
	else {
		document.Form.desc.value = "Other: "+document.Form.zMemo.value
	}
}

/*	Form Reset Function	*/
function resetForm() {
	
	/* reset all the menus */
	document.Form.dPrefs.selectedIndex = 0;
	document.Form.normalAmt.selectedIndex = 0;
	document.Form.fbtype.selectedIndex = 0;
	document.Form.delay.selectedIndex = 0;
	
	/* reset all variables */
	document.Form.intervalMult.value = "";
	document.Form.intervalUnit.value = "";
	document.Form.normalAmount.value = "";
	document.Form.amount.value = "";
	document.Form.futurePayType.value = "";
	document.Form.option.value = "";
	document.Form.startDelayMult.value = "";
	document.Form.startDelayUnit.value = "";
	document.Form.cartId.value = "Donation";
	document.Form.desc.value = "General Donation";
	fpNormalAmount = "";
	
	/* reset all table display properties */
	document.getElementById("zFoodbank").style.visibility = "hidden"
	document.getElementById("zFoodbank").style.display= "none"
	document.getElementById("zOrphan").style.visibility = "hidden"
	document.getElementById("zOrphan").style.display= "none"
	document.getElementById("zBasket1").style.visibility = "hidden"
	document.getElementById("zBasket1").style.display= "none"
	document.getElementById("zBasket2").style.visibility = "hidden"
	document.getElementById("zBasket2").style.display= "none"
	document.getElementById("zBasket3").style.visibility = "hidden"
	document.getElementById("zBasket3").style.display= "none"
	document.getElementById("monthly").style.visibility = "hidden"
	document.getElementById("monthly").style.display= "none"
	document.getElementById("zAmount").style.visibility = "visible"
	document.getElementById("zAmount").style.display= "block"
	document.getElementById("zSelect").style.visibility = "visible"
	document.getElementById("zSelect").style.display= "block"
}

