requiredFields = new Array();
requiredFields[0] = new Array("givenName", "- Given Name\n", "", "");
requiredFields[1] = new Array("surname", "- Surname\n", "", "");
requiredFields[2] = new Array("residentialAddress", "- Residential Address\n", "", "");
requiredFields[3] = new Array("residentialCity", "- Residential City/Town\n", "", "");
requiredFields[4] = new Array("residentialProvince", "- Residential Province\n", "", "");
requiredFields[5] = new Array("residentialPostalCode", "- Residential Postal Code\n", "", "");
requiredFields[6] = new Array("mailingAddress", "- Mailing Address\n", "mailingAddressSameAsResidential", true);
requiredFields[7] = new Array("mailingCity", "- Mailing City/Town\n", "mailingAddressSameAsResidential", true);
requiredFields[8] = new Array("mailingProvince", "- Mailing Province\n", "mailingAddressSameAsResidential", true);
requiredFields[9] = new Array("mailingPostalCode", "- Mailing Postal Code\n", "mailingAddressSameAsResidential", true);
requiredFields[10] = new Array("legalCheckbox", "- Legal Disclaimer Agreement\n", "", "");
//requiredFields[11] = new Array("existingMembershipId", "- Membership ID\n", "paymentType", "Renewal");
//requiredFields[12] = new Array("membershipPassword", "- Password\n", "paymentType", "Renewal");

function priceRef(theVar)
{
   unitPrice = 0;
   for (unitIter=0; unitIter<unitList.length; unitIter++)
   {
   	if (theVar == unitList[unitIter][0])
   	{
   		unitPrice = unitList[unitIter][1];
   	}
   } 
   return unitPrice;
   
}

function isNumberKey(e, theUnit) 
{
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode!=8 && unicode!=9){
	  if (unicode<48||unicode>57)
	  {
		return false;
	  }
	}
	
	return true;
}

function calcNewValue(theUnit)
{
	//theUnitCalc = document.getElementById(theUnit.id + "Calc");
	//theUnitCalc.value = priceRef(theUnit.id)*theUnit.value;
	
	calcTotal();
}

function calcTotal()
{
   totalPrice = 0;
   paymentAmountOneYearObj = document.getElementById("paymentAmountOneYear");
   paymentAmountFiveYearObj = document.getElementById("paymentAmountFiveYear");
   donationObj = document.getElementById("donation");
   
   if (paymentAmountOneYearObj.checked)
   {
   	totalPrice = totalPrice + 10;
   } else if (paymentAmountFiveYearObj.checked)
   {
   	totalPrice = totalPrice + 35;
   } else {
   
   }
   
   if (validateInt(donationObj.value)) {
   	totalPrice = totalPrice + parseInt(donationObj.value);
   }
   
   document.getElementById("totalCalc").value = totalPrice;
}

function validateInt(iString) {
    return (("" + parseInt(iString)) == iString);
}


function checkForm(theForm)
{
    errorCompilation = "";   
    
    // STAGE 1 CHECK : CHECK FOR CORRECT VALUES
    for (errorIter=0; errorIter<requiredFields.length; errorIter++)
    {
   	checkElement = theForm.elements[requiredFields[errorIter][0]];
   	conditionElement = requiredFields[errorIter][2];
   	performCheck = false;
   	
   	
   	if (conditionElement == "")
   	{
   		performCheck = true;
   	} else {
   		conditionElementItem = theForm.elements[conditionElement];
   		if (conditionElementItem.type == "checkbox")
   		{
   			if (conditionElementItem.checked == requiredFields[errorIter][3])
   			{
   				performCheck = true;
   			}
   		} else if (conditionElementItem instanceof NodeList) {
			for(var i = 0; i < conditionElementItem.length; i++) {
				if ((conditionElementItem[i].checked) && (conditionElementItem[i].value == requiredFields[errorIter][3])) {
   					performCheck = true;
				}
			}
   		} else {
			if (conditionElementItem.value == requiredFields[errorIter][3])
			{
				performCheck = true;
			}
   		}
   	}
   	if (performCheck)
   	{
		if (checkElement.value == "")
		{
			errorCompilation += requiredFields[errorIter][1];
		}
	}
    } 
    
    
    
    
    if (errorCompilation != "") 
    {
        alert("Missing Required Fields:\n" + errorCompilation);
        return false;
    }
    
    return true;
    

}

function emailForm()
{ 
    theFormUse = document.getElementById("bccForm");
    if (checkForm(theFormUse))
    {
    	theFormUse.action = "";
    	theFormUse.submit();
    }
}


function emailPolicyRegistrationForm()
{ 
    theFormUse = document.getElementById("bccForm");

    theFormUse.action = "";
    theFormUse.submit();
}

function checkValidEmail(inEmail)
{
	filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(inEmail)) {
	  // Yay! valid
	  return true;
	}
	else {return false;}
}

function printForm()
{ 
    window.print();
}

function changePaymentType(changeOption)
{
	switch (changeOption.value)
	{
		case "Cash":
			//alert("cash");
			document.getElementById("confirmationButtonsStandard").className = '';
			document.getElementById("confirmationButtonsCreditCard").className = 'hiddenObj';
			break;
		case "Cheque":
			//alert("cheque");
			document.getElementById("confirmationButtonsStandard").className = '';
			document.getElementById("confirmationButtonsCreditCard").className = 'hiddenObj';
			break;
		case "Credit Card":
			//alert("credit card");
			document.getElementById("confirmationButtonsStandard").className = 'hiddenObj';
			document.getElementById("confirmationButtonsCreditCard").className = '';
			break;
			
	}
}

function changeContributionType(changeOption)
{
	switch (changeOption.value)
	{
		case "Other":
			document.getElementById("countributionAmountCustomContainer").className = '';
			break;
		default:
			document.getElementById("countributionAmountCustomContainer").className = 'hiddenObj';
			break;			
	}
}

function changeMailingAddressType(changeOption)
{
	switch (changeOption.checked)
	{
		case true:
			document.getElementById("mailingAddressContainer").className = '';
			break;
		default:
			document.getElementById("mailingAddressContainer").className = 'hiddenObj';
			break;			
	}
}

function disableLegalSubmitButtons(changeOption)
{
	switch (changeOption)
	{
		case true:
			document.getElementById("btnSubmitByEmail").disabled = false;
			document.getElementById("btnSubmitByPrint").disabled = false;
			document.getElementById("btnSubmitByCreditCard").disabled = false;			
			break;
		default:
			document.getElementById("btnSubmitByEmail").disabled = true;
			document.getElementById("btnSubmitByPrint").disabled = true;
			document.getElementById("btnSubmitByCreditCard").disabled = true;
			break;		
	}
}

function changeApplicationType(changeOption)
{
	switch (changeOption.value)
	{
		case "New":
			document.getElementById("renewalDetails").className = 'hiddenObj';
			break;
		case "Renewal":
			document.getElementById("renewalDetails").className = '';
			break;
		default:
			break;
	}
}

function resetClickForm()
{
	changeApplicationType(document.getElementById("hdnPaymentType"));
	changeMailingAddressType(document.getElementById("mailingAddressSameAsResidential"));
	changeContributionType(document.getElementById("hdnContributionAmount"));
	changePaymentType(document.getElementById("hdnPaymentOption"));
	disableLegalSubmitButtons(document.getElementById("legalCheckbox").checked);

}

function requestCredentials() {
        $.post(
            './ajax/retrieveMembershipByEmail.php',
            { emailAddress: $("#requestInfoEmailAddress").val() },
            function(responseText) {

				$('#request-credentials-form').dialog('close');
				$('#confirmation-form').dialog('open');

            	var eventDataArray = jQuery.parseJSON(responseText);
            	if (eventDataArray["result"] == 1)
            	{
            		$('#confirmation-form').html("Your Membership ID and Password has been emailed to the email address provided.");
            	} else {
            		$('#confirmation-form').html("An Error has Occured.");
            	}

            },
            "html"
        );
}


