function switchPaymentType(type){
    var paypalMessage = "";
    if(type=="ipaypal"){
        showDiv('express_pay');
        hideDiv('direct_pay');
        paypalMessage = " and pay through PayPal";


    }else{
       showDiv('direct_pay');
        hideDiv('express_pay');

        getObj(type).checked="checked";
    }
    getObj(type).checked="checked";
    getObj("expressPayMessage").innerHTML=paypalMessage;
}

function paymentConfirmation(applicationType, displayPayment) {
    if(getObj("p_creditCardNumber").value != "" || getObj("ipaypal").checked) {
        getObj("paymentType").value = "p";
        if(getObj("depositOnly").value==1) {
            getObj("paymentType").value = "d";
        }

        /*totalCostField exists when there is only one option allowed for
         * Payment. In this case set the payment ammount and corresponding
         * message to the value of the totalCostField.
         */
        var payment, paymentAmount, orgID;


        /* totalCostSelect is available when there are multiple options
         * for payment.  When the Payment is required then. the options
         * are:
         * Pay Deposit:   selectedIndex = 0
         * Pay TotalCost: selectedIndex = 1
         *
         * when payment is optional then the following options are available
         * No Payment:    selectedIndex = 0
         * Pay Deposit:   selectedIndex = 1
         * Pay TotalCost: selectedIndex = 2
         */
		 
        if(getObj("totalCostSelect")) {
            //get the selected Index of the totolCostSelected
            var selPayment = getObj("totalCostSelect").selectedIndex;
            payment = getObj("totalCostSelect")[selPayment].text;
            paymentAmount = getObj("totalCostSelect").value;

            // if PaymentRequired and Selected==O then it is a deposit
            if((displayPayment=="R")){
                if(selPayment == 0){
                    getObj("paymentType").value = "d";
                }
                if(selPayment == 1){
                    getObj("paymentType").value = "p";
                }
            }
            if((displayPayment=="O")){
                if(selPayment == 1){
                    getObj("paymentType").value = "d";
                }
                if(selPayment == 2){
                    getObj("paymentType").value = "p";
                }
            }
        }
		
		if(getObj("totalCostField")){
            payment = "$" +  getObj("totalCostField").value;
            paymentAmount = getObj("totalCostField").value;
			// set paymentType to "p" if it is unset. Make sure deposit only isn't set to p
			if(getObj("paymentType").value==""){
				getObj("paymentType").value = "p";
			}
        }

        if (getObj("totalCostField") || getObj("totalCostSelect")){
            getObj("paymentAmount").value = paymentAmount;
        }
	
        if(payment && payment != false && payment!= 0 && !(selPayment==0 && displayPayment=="O")) {
            if(getObj("ipaypal").checked==true){
                if(getObj("orgID")){
                    orgID = getObj("orgID").value;
                }
                else{
                    orgID = 0;
                }
                if(getObj("eid")){
                    eid = getObj("eid").value;
                }
                window.open('../setExpressCheckout.z?eid='+eid+'&orgID='+orgID+'&pa='+paymentAmount);
            }
            
            var payBox = "<div class='payconfirm heading2 center'>You are about to pay $"+ paymentAmount +
            ".<br /><br />Are you sure you want to do this?<br /><br /></div>" +
            "<div class='payconfirm center'>"+
            "<a href='javascript:hidePopWin(true)'>Yes (Make Payment)</a><br/><br/>"+
            "<a href='javascript:hidePopWin(false)'>No (Cancel Payment)</a></div>"+
            "<div class='payconfirm center'><br/>Payments are non-refundable unless "+
            "otherwise indicated by the " +applicationType + " organizer. "+
            "The name associated with this charge on your financial statement "+
            "will be 'GRACEFUL TOOLS'</div>";
            showPopWin(payBox, 400, 200,
                function ()
                {
                    if(applicationType=="appointment"){
                        submitSignup(false, '');
                    }
                    else if(applicationType=="event"){
                        submitReg(false, '');
                    }
                }
                );
            return;
        }
    } else if(displayPayment=="R") { // if payment is required, give error
            alert("Online payment is required for this " + applicationType +"."+
                " Please fill out the payment information and resubmit your request.");
    } else { // if payment payment isn't required return true
			return true;
    }
}

/**
 * Function to check discount Code.
 */
function checkDiscountCode(type, id, code, paymentType)
{
    if(code==""){
        return;
    }
    var xmlHttp = makeXMLHttpRequest();

    xmlHttp.open("GET","checkDiscount.z?type="+type+"&id="+id+
           "&code="+code,true);
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState != 4)
            return;

        var discountGranted = parseFloat(xmlHttp.responseText);
        if(discountGranted > 0)
        {
            alert("You have been granted a discount of $"+
                    discountGranted.toString());
            
                document.getElementById("dcimage").style.display="inline";    
                
            if(paymentType=="O" || paymentType=="R"){
               
                updatePaymentTable(type, id, code, paymentType);
            }
        }
        else
            alert("Sorry, the discount code you entered "+
                "avails you no additional discounts.");
    };
    xmlHttp.send(null);
}
/**
 * Function to check discount Code. Needs to be activated
 */
function updatePaymentTable(type, id, code, paymentType)
{
    var xmlHttp = makeXMLHttpRequest();

// make sure we have the correct url for both production and test servers    
var urlPrefix = window.location.protocol + "//" + window.location.host + "/";
var pathArray = window.location.pathname.split( '/' );
var acceptablePrefixAdditions = ["graceful", "~timsu", "~jonparis"];


for(var i = 0; i < acceptablePrefixAdditions.length; i++){
	if (pathArray[0] == acceptablePrefixAdditions[i]){
		urlPrefix += pathArray[0] + "/";
	}else if(pathArray[1] == acceptablePrefixAdditions[i]){
		urlPrefix += pathArray[1] + "/";
	}
}

    xmlHttp.open("GET",urlPrefix + "updatePaymentTable.z?type="+type+"&id="+id+
           "&code="+code,true);
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState != 4)
            return;
        
        var initialText = xmlHttp.responseText.substring(0,16);

        if(initialText == "<!--ntgttable-->")
        {        	
        	
            if(paymentType=="O" || paymentType=="R"){
			var paymentBody = getObj("gtpaymentbody");
			paymentBody.innerHTML=xmlHttp.responseText;
            }
        }
    };
    xmlHttp.send(null);
}

