////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function createDoFo()
{
	/*create the donor form array*/
	var donorFoAr = new Array();
	var lastRadioName = "";
		
	var i = 0;
	var j = 0;
	var r = 0; //radio counter
	var donorFoPath = document.forms.donorForm;
	while(i < donorFoPath.length) //length can be accesed by elements.length also
	{
		var curFieldName = donorFoPath.elements[i].name; //name of field
		var curFieldValue = donorFoPath.elements[i].value; //value of field
		var curFieldType = donorFoPath.elements[i].type; //type of field
		var curFieldId = donorFoPath.elements[i].id;
		
		document.getElementById(curFieldId).removeAttribute("class");
		document.getElementById(curFieldId).removeAttribute("className");
		
		if(curFieldType == "radio")
		{
			if(lastRadioName != curFieldName) r = 0;
			
			var isChecked = donorFoPath[curFieldName][r].checked;
			
			lastRadioName = curFieldName;
			r++;
		}
		if(isChecked == true || curFieldType != "radio"){
			var curFieldObj = new elementObject(curFieldName, curFieldValue, curFieldType);
			donorFoAr.push(curFieldObj);//add each field to the donorFormArray
		}
		i++;
	}
	return donorFoAr;
}

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function getRequired()
{
		var donorFoAr = new Array();
		donorFoAr = createDoFo();
		
		dm.addEl("img", "spinnyPageErrorsList", "spinnyPageErrorsListImg", ["src:images/spinny.gif"]);
		
		DWRUtil.useLoadingMessage();
		DWREngine._execute("payment/paymentResponder.cfc", null, 'getRequired', donorFoAr, giftArray, showRequired);	/* */
}

function showRequired(r)
{	
	dm.remElById("pageErrors"); //remove the actual errors if there are any
	setTimeout("dm.remElById('spinnyPageErrorsListImg')", 1000); //remove the loading img after a second
	dm.addEl("span", "pageErrorsList", "pageErrors");
	if(r.errorfieldarray.length == 0) //no errors
	{
		thefkey = r.fkey; //thefkey is a global declared in index.cfm
		getCheckSum();
		createModal('modalHolder');
	}
	else //if there were errors
	{
		dm.addEl("h3", "pageErrors", "processErrorsHead", ["text:Processing Errors"]);		
	}
	var lastPaySourceNum = 0;
	i = 0;
	while(i < r.errorfieldarray.length) //loop through the errors to display them
	{
		dm.addEl( "li", "pageErrors", "pageError"+i, ["text:"+r.errorfieldarray[i].fielderror, "class:"+curAltError, "fadeIn:20"] );
		document.getElementById(r.errorfieldarray[i].fieldname).setAttribute("class","errorInput");
		document.getElementById(r.errorfieldarray[i].fieldname).setAttribute("className","errorInput");
		if(curAltError == "listAltError1") curAltError = "listAltError2";
		else if(curAltError == "listAltError2") curAltError = "listAltError1";
		i++;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
/*function getStoreSession()
{
	var donorFoAr = createDoFo();
	
	DWRUtil.useLoadingMessage();
	DWREngine._execute("payment/paymentResponder.cfc", null, 'getStoreSession', donorFoAr, showStoreSession);
}

function showStoreSession(r)
{
	//alert(r.testvar);	
	//getTestSession();
}

function getTestSession()
{
	DWRUtil.useLoadingMessage();
	DWREngine._execute("payment/paymentResponder.cfc", null, 'getTestSession', showStoreSession);
}*/

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function getCheckSum()
{
	var total = calculateGiftTotal();
	
	DWRUtil.useLoadingMessage();
	DWREngine._execute("payment/paymentResponder.cfc", null, 'getChecksum', total, callAddress);
}

function callAddress(r)
{
	//Figure out variables here
	//getStoreSession(); //put variables in the session to be used for the receipt
	
	var Card_Address1 = "&Card_Address1=" + document.getElementById( 'donorAddress' ).value;
	var card_name = "&card_name=" + document.getElementById( 'donorFirstName' ).value + " " + document.getElementById( 'donorLastName' ).value;
	var card_city = "&card_city=" + document.getElementById( 'donorCity' ).value;
	var card_state = "&card_state=" + document.getElementById( 'donorState' ).value;
	var card_country = "&card_country=" + document.getElementById( 'donorCountry' ).value;
	var card_zip = "&card_zip=" + document.getElementById( 'donorZip' ).value;
	var card_phone1 = "&card_phone1=" + document.getElementById( 'donorHomePhone' ).value;
	var card_email = "&card_email=" + document.getElementById( 'donorEmail' ).value;
	var Order_Price = "&Order_Price=" + calculateGiftTotal();
	var Identifier = "Identifier=" + r.identifier;
	var Version = "&Version=" + r.version;
	var Order_CheckSum1 = "&Order_CheckSum1=" + r.cs1;
	var Order_CheckSum2 = "&Order_CheckSum2=" + r.cs2;
	var Order_Fkey = "&Order_Fkey=" + thefkey; //thefkey is a global
	var webProcessorURL =  r.webprocessorurl;
	//decide which address to use
	if( document.getElementById('checkPersonBox').checked ) var webAddress = webProcessorURL + Identifier + Version + Order_Price + Order_CheckSum1 + Order_CheckSum2 + Order_Fkey + card_email + card_phone1 + card_zip + card_country + card_state + card_city + card_name + Card_Address1;
	else var webAddress = webProcessorURL + Identifier + Version + Order_Price + Order_CheckSum1 + Order_CheckSum2 + Order_Fkey;
	
	//document.forms.submitForm.action = webAddress;
	//document.forms.submitForm.submit();
	document.getElementById('redirect').setAttribute('src',webAddress);
		/*'https://payments.qual.ais.msu.edu/Process?Identifier=69230dev&Order_Price=23&Order_CheckSum1=1048&Order_CheckSum2=138&Version=3.2&Card_Address1=Smith&card_name=Smith&card_city=Smith&card_state=Smith&card_zip=Smith&card_country=Smith&card_phone1=Smith&card_email=Smith'*/
	//setInterval("createReceipt()", 10000);
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function createReceipt()
{
	theFrame = document.getElementById('redirect');
	//alert(theFrame.contentWindow.document.getElementById('receiptDiv').innerHTML);
}

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function createModal(id, content)
{
	var overlay = document.getElementById('overlay');
	overlay.style.display = "block";
	var modal = document.getElementById(id);
	modal.style.display = "block";
}
