var discountCodeCtlId = "ccDiscountCode";
var cardFieldCtlId = "ccCardField";
var cardNumberCtlId = "ccCardNumber";
var secCodeCtlId = "ccSecurityCode";
var expDateMonthCtlId = "ccExpDateMonth";
var expDateYearCtlId = "ccExpDateYear";
var cardHolderFNameCtlId = "ccCardHolderFName";
var cardHolderLNameCtlId = "ccCardHolderLName";
var cardHolderAddyOneCtlId = "ccCardHolderAddyOne";
var cardHolderAddyTwoCtlId = "ccCardHolderAddyTwo";
var cardHolderCityCtlId = "ccCardHolderCity";
var cardHolderStateCtlId = "ccCardHolderState";
var cardHolderZipPostalCtlId = "ccCardHolderZipPostal";
var submitPaymentAnchorId = "ccSubmitCCPayment";

var shippingAddyOneCtlId = "ccShippingAddyOne";
var shippingAddyTwoCtlId = "ccShippingAddyTwo";
var shippingCityCtlId = "ccShippingCity";
var shippingStateCtlId = "ccShippingState";
var shippingZipPostalCtlId = "ccShippingZipPostal";
var shippingFirstNameCtlId = "ccShippingFName";
var shippingLastNameCtlId = "ccShippingLName";

var postSubLinkElmId = "ccPostSubLink";

var shippingSameAsBillingCtlId = "ccShipSameAsBill";

var ccEmailAddyCtlId = "ccEmailAddress";

var ccSectionDivId = "ccSectionDiv";
var ccSectionTableId = "ccSectionTable";
var chargeSummaryTableId = "ccPricingSection";

var formFieldValue;
var ccSubmitButtonEnabled = true;

var submitButtonText = "";

var isCCInitialized = false;

function ShowErroredCCSubmission()
{
	SetCCHeaderRowText(ResourceID3624);	
}

function ShowSuccessfulCCSubmission(url)
{
	SetCCHeaderRowText(ResourceID3625, url);
}

function SetCCHeaderRowText(rowText, url)
{
	var urlExists = false;
	if (url && typeof(url) != "undefined")
		urlExists = true;
		
	var txtnode = Dom.createTextNode(rowText);
	var brkElmOne = Dom.createElement("br", "", "");
	
	var tbl = document.getElementById(ccSectionTableId);
	var headerRow = tbl.rows[0];
	var headerCell = headerRow.cells[0];
	Dom.removeChildNodes(headerCell);
	Dom.appendChild(txtnode, headerCell);
	Dom.appendChild(brkElmOne, headerCell);
	
	if (urlExists)
	{
		var anchor = Dom.createElement("a", postSubLinkElmId, "");
		var brkElmTwo = Dom.createElement("br", "", "");
		Dom.setAttribute(anchor, "href", url);
		Dom.setText(anchor, ResourceID2093);
		Dom.appendChild(anchor, headerCell);
		Dom.appendChild(brkElmTwo, headerCell);
	}
}

function DisableSubmitPaymentButton()
{
	ccSubmitButtonEnabled = false;
	var buttonElm = document.getElementById(submitPaymentAnchorId);
	submitButtonText = buttonElm.innerText;	
	buttonElm.innerText = ResourceID3560;
}

function EnableSubmitPaymentButton()
{
	ccSubmitButtonEnabled = true;
	var buttonElm = document.getElementById(submitPaymentAnchorId);
	buttonElm.innerText = submitButtonText;
}

function EnableDisableShippingAddressFields(isChecked)
{
	var shipFirstName = document.getElementById(shippingFirstNameCtlId);
	var shipLastName = document.getElementById(shippingLastNameCtlId);
	var shipAddyOneCtl = document.getElementById(shippingAddyOneCtlId);
	var shipAddyTwoCtl = document.getElementById(shippingAddyTwoCtlId);
	var shipCityCtl = document.getElementById(shippingCityCtlId);
	var shipStateCtl = document.getElementById(shippingStateCtlId);
	var shipZipCtl = document.getElementById(shippingZipPostalCtlId);

	var billAddyOneCtl = document.getElementById(cardHolderAddyOneCtlId);
	var billAddyTwoCtl = document.getElementById(cardHolderAddyTwoCtlId);
	var billCityCtl = document.getElementById(cardHolderCityCtlId);
	var billStateCtl = document.getElementById(cardHolderStateCtlId);
	var billZipCtl = document.getElementById(cardHolderZipPostalCtlId);
	
	var cHolderFNameCtl = document.getElementById(cardHolderFNameCtlId);
	var cHolderLNameCtl = document.getElementById(cardHolderLNameCtlId);

	if (isChecked)
	{
		shipFirstName.value = cHolderFNameCtl.value;
		shipLastName.value = cHolderLNameCtl.value;
		shipAddyOneCtl.value = billAddyOneCtl.value;	
		shipAddyTwoCtl.value = billAddyTwoCtl.value;
		shipCityCtl.value = billCityCtl.value;
		SetCCSelectBoxData(shipStateCtl, GetCCSelectBoxData(billStateCtl, false), false);
		shipZipCtl.value = billZipCtl.value;
	}
	
	shipFirstName.disabled = isChecked;
	shipLastName.disabled = isChecked;
	shipAddyOneCtl.disabled = isChecked;
	shipAddyTwoCtl.disabled = isChecked;
	shipCityCtl.disabled = isChecked;
	shipStateCtl.disabled = isChecked;
	shipZipCtl.disabled = isChecked;
}

function GetCCControlValue(inputCtl, isSelect)
{
	var ctlVal;
	if (isSelect)
		ctlVal = GetCCSelectBoxText(inputCtl)[0];
	else
		ctlVal = inputCtl.value;
	return ctlVal;
}

function ReplaceCCControlCellWithText(inputCtl, newCellText)
{
	var cellNode = 	GetCCParentNode(inputCtl);
	cellNode.removeChild(inputCtl);
	cellNode.innerText = newCellText;
}

function SetCCCtlAsReadOnly(inputCtl, isSelect)
{
	var ctlVal = GetCCControlValue(inputCtl, isSelect);
	ReplaceCCControlCellWithText(inputCtl, ctlVal);
}

function GetCCSelectBoxData(selCtl, getText)
{
	var arr = new Array();
	for (var i = 0; i < selCtl.options.length; i++)
	{
		var currOption = selCtl.options[i];
		if (currOption.selected)
		{
			if (getText)
				arr.push(currOption.text);
			else
				arr.push(currOption.value);
		}
	}
	return arr;
}

function SetCCSelectBoxData(selCtl, valArray, isText)
{
	for (var i = 0; i < selCtl.options.length; i++)
	{
		var currOption = selCtl.options[i];
		var currOptionText = currOption.text;
		var currOptionValue = currOption.value;
		
		for (var j = 0; j < valArray.length; j++)
		{
			var selVal = valArray[j];
			if ((isText && currOptionText == selVal) || (!isText && currOptionValue == selVal))
				currOption.selected = true;		
			else 
				currOption.selected = false;
		}
	}
}

function GetCCParentNode(elm)
{
	if(typeof elm.parentNode != "undefined")
		return elm.parentNode;
	else
		return elm.parentElement;
}

function GetCCSelectBoxText(selCtl)
{
	return GetCCSelectBoxData(selCtl, true);
}

function SetCCSectionToReadOnly()
{
	var cellNode;
	
	var discInputCtl = document.getElementById(discountCodeCtlId);
	if (discInputCtl)
		SetCCCtlAsReadOnly(discInputCtl, false);
	
	var cardTypeCtl = document.getElementById(cardFieldCtlId);
	if (cardTypeCtl)
		SetCCCtlAsReadOnly(cardTypeCtl, true);
		
	var cardNumCtl = document.getElementById(cardNumberCtlId);
	if (cardNumCtl)
	{
		cardNumCtl.value = '****************';
		SetCCCtlAsReadOnly(cardNumCtl, false);
	}
		
	var secCodeCtl = document.getElementById(secCodeCtlId);
	if (secCodeCtl)
	{
		secCodeCtl.value = '***';
		SetCCCtlAsReadOnly(secCodeCtl, false);
	}
		
	var expMonthCtl = document.getElementById(expDateMonthCtlId);
	var expYearCtl = document.getElementById(expDateYearCtlId);
	if (expMonthCtl && expYearCtl)
	{
		var monthVal = GetCCControlValue(expMonthCtl, true);
		var yearVal = GetCCControlValue(expYearCtl, true);
		ReplaceCCControlCellWithText(expMonthCtl, monthVal + "-" + yearVal);
	}
	
	var cHolderEmailCtl = document.getElementById(ccEmailAddyCtlId);
	if (cHolderEmailCtl)
		SetCCCtlAsReadOnly(cHolderEmailCtl, false);
			
	var cHolderFNameCtl = document.getElementById(cardHolderFNameCtlId);
	if (cHolderFNameCtl)
		SetCCCtlAsReadOnly(cHolderFNameCtl, false);
	
	var cHolderLNameCtl = document.getElementById(cardHolderLNameCtlId);
	if (cHolderLNameCtl)
		SetCCCtlAsReadOnly(cHolderLNameCtl, false);
		
	var cHolderAddyOneCtl = document.getElementById(cardHolderAddyOneCtlId);
	if (cHolderAddyOneCtl)
		SetCCCtlAsReadOnly(cHolderAddyOneCtl, false);
		
	var cHolderAddyTwoCtl = document.getElementById(cardHolderAddyTwoCtlId);
	if (cHolderAddyTwoCtl)
		SetCCCtlAsReadOnly(cHolderAddyTwoCtl, false);
		
	var cHolderCityCtl = document.getElementById(cardHolderCityCtlId);
	if (cHolderCityCtl)
		SetCCCtlAsReadOnly(cHolderCityCtl, false);
		
	var cHolderStateCtl = document.getElementById(cardHolderStateCtlId);
	if (cHolderStateCtl)
		SetCCCtlAsReadOnly(cHolderStateCtl, true);
	
	var cHolderZipCtl = document.getElementById(cardHolderZipPostalCtlId);
	if (cHolderZipCtl)
		SetCCCtlAsReadOnly(cHolderZipCtl, false);
		
	var checkBoxCtl = document.getElementById(shippingSameAsBillingCtlId);
	if (checkBoxCtl)
	{
		var cellNode = GetCCParentNode(checkBoxCtl);
		if (cellNode)
		{
			var rowNode = GetCCParentNode(cellNode);
			if (rowNode)
			{
				var tableNode = GetCCParentNode(rowNode);
				if (tableNode)
					tableNode.removeChild(rowNode);
			}
		}
	}
	
	var shipFNameCtl = document.getElementById(shippingFirstNameCtlId);
	if (shipFNameCtl)
		SetCCCtlAsReadOnly(shipFNameCtl, false);

	var shipLNameCtl = document.getElementById(shippingLastNameCtlId);
	if (shipLNameCtl)
		SetCCCtlAsReadOnly(shipLNameCtl, false);
	
	var shippingAddyOneCtl = document.getElementById(shippingAddyOneCtlId);
	if (shippingAddyOneCtl)
		SetCCCtlAsReadOnly(shippingAddyOneCtl, false);

	var shippingAddyTwoCtl = document.getElementById(shippingAddyTwoCtlId);
	if (shippingAddyTwoCtl)
		SetCCCtlAsReadOnly(shippingAddyTwoCtl, false);

	var shippingCityCtl = document.getElementById(shippingCityCtlId);
	if (shippingCityCtl)
		SetCCCtlAsReadOnly(shippingCityCtl, false);

	var shippingStateCtl = document.getElementById(shippingStateCtlId);
	if (shippingStateCtl)
		SetCCCtlAsReadOnly(shippingStateCtl, true);

	var shippingZipCtl = document.getElementById(shippingZipPostalCtlId);
	if (shippingZipCtl)
		SetCCCtlAsReadOnly(shippingZipCtl, false);
	
		
	var submitBtn = document.getElementById(submitPaymentAnchorId);
	if (submitBtn)
		GetCCParentNode(submitBtn).removeChild(submitBtn);	
}

function SubmitCCP()
{
	if (IsMicrositePreview())
	{
		if (isCCInitialized)
			alert(ResourceID6781);
		return;
	}
	
	var expMonthElm = document.getElementById(expDateMonthCtlId);
	var expYearElm = document.getElementById(expDateYearCtlId);
	var expMonthElmVal = GetCCSelectBoxData(expMonthElm)[0];
	var expYearElmVal = GetCCSelectBoxData(expYearElm)[0];
		
	var expYearIntVal = parseInt(expYearElmVal);
	var expMonthIntVal = parseInt(expMonthElmVal);
	
	if ((CurrentYear > expYearIntVal) || (CurrentYear == expYearIntVal && CurrentMonth > expMonthIntVal))
	{
		alert(ResourceID9434);
		return;
	}

	if (ccSubmitButtonEnabled)
	{
		// Disable the submit payment button....
		DisableSubmitPaymentButton();
		var xmlDoc = "<CPS>";

		// Discount Code
		var dcElm = document.getElementById(discountCodeCtlId);
		if (dcElm)
			xmlDoc += "<DC>" + dcElm.value + "</DC>";
		else
			xmlDoc += "<DC></DC>";

		// CC Type
		var ccTypeElm = document.getElementById(cardFieldCtlId);
		xmlDoc += "<CCT>" + GetCCSelectBoxData(ccTypeElm)[0] + "</CCT>";

		// CCN
		var ccnElm = document.getElementById(cardNumberCtlId);
		xmlDoc += "<CCN>" + ccnElm.value + "</CCN>";

		// SEC Code
		var secCodeElm = document.getElementById(secCodeCtlId);
		if (secCodeElm)
			xmlDoc += "<SC>" + secCodeElm.value + "</SC>";
		else
			xmlDoc += "<SC></SC>";

		// ExpMonth
		xmlDoc += "<EDM>" + expMonthElmVal + "</EDM>";

		// ExpYear
		xmlDoc += "<EDY>" + expYearElmVal + "</EDY>";

		// Email Addy
		var cardHolderEmailAddy = document.getElementById(ccEmailAddyCtlId);
		xmlDoc += "<CHEMAIL>" + cardHolderEmailAddy.value + "</CHEMAIL>";

		// First Name
		var cardHolderFElm = document.getElementById(cardHolderFNameCtlId);
		xmlDoc += "<CHFN>" + cardHolderFElm.value + "</CHFN>";

		// First Name
		var cardHolderLElm = document.getElementById(cardHolderLNameCtlId);
		xmlDoc += "<CHLN>" + cardHolderLElm.value + "</CHLN>";

		// Billing One
		var billingOneElm = document.getElementById(cardHolderAddyOneCtlId);
		xmlDoc += "<BALO>" + billingOneElm.value + "</BALO>";

		// Billing Two
		var billingTwoElm = document.getElementById(cardHolderAddyTwoCtlId);
		xmlDoc += "<BALT>" + billingTwoElm.value + "</BALT>";

		// Billing City
		var cityElm = document.getElementById(cardHolderCityCtlId);
		xmlDoc += "<BC>" + cityElm.value + "</BC>";

		// Billing State
		var stateElm = document.getElementById(cardHolderStateCtlId);
		xmlDoc += "<BS>" + GetCCSelectBoxData(stateElm)[0] + "</BS>";

		// Billing Zip
		var zipElm = document.getElementById(cardHolderZipPostalCtlId);
		xmlDoc += "<BZ>" + zipElm.value + "</BZ>";


		// Ship First Name
		var shipFElm = document.getElementById(shippingFirstNameCtlId);
		if (shipFElm)
			xmlDoc += "<SHIPFN>" + shipFElm.value + "</SHIPFN>";

		// Ship First Name
		var shipLElm = document.getElementById(shippingLastNameCtlId);
		if (shipLElm)
			xmlDoc += "<SHIPLN>" + shipLElm.value + "</SHIPLN>";


		// Shipping One
		var shippingOneElm = document.getElementById(shippingAddyOneCtlId);
		if (shippingOneElm)
			xmlDoc += "<SALO>" + shippingOneElm.value + "</SALO>";

		// Shipping Two
		var shippingTwoElm = document.getElementById(shippingAddyTwoCtlId);
		if (shippingTwoElm)
			xmlDoc += "<SALT>" + shippingTwoElm.value + "</SALT>";

		// Shipping City
		var shippingcityElm = document.getElementById(shippingCityCtlId);
		if (shippingcityElm)
			xmlDoc += "<SHIPCITY>" + shippingcityElm.value + "</SHIPCITY>";

		// Shipping State
		var shippingstateElm = document.getElementById(shippingStateCtlId);
		if (shippingstateElm)
			xmlDoc += "<SHIPSTATE>" + GetCCSelectBoxData(shippingstateElm)[0] + "</SHIPSTATE>";

		// Shipping Zip
		var shippingzipElm = document.getElementById(shippingZipPostalCtlId);
		if (shippingzipElm)
			xmlDoc += "<SHIPZIP>" + shippingzipElm.value + "</SHIPZIP>";

		xmlDoc += "</CPS>";

		var jw = new jsWS('ccsubmission.ashx', 'SubmitPayment');
		jw.SetAsync();
		jw.addArg('XmlDoc', xmlDoc);
		jw.addArg('FormFieldValue', formFieldValue);
		jw.send(PostCCSubmit);
	}
	else
		alert(ResourceID3557);
}

function PostCCSubmit(req)
{
	var success = "0";
	var url = "";
	
	try
	{
		var doc = XMLHelper.createXMLDocumentFromString(req.responseText);

		success = XMLHelper.getNodeTextValue(XMLHelper.selectSingleNode(doc.firstChild, "success"));
		var submissionId = XMLHelper.getNodeTextValue(XMLHelper.selectSingleNode(doc.firstChild, "submissionid"));
		url = XMLHelper.getNodeTextValue(XMLHelper.selectSingleNode(doc.firstChild, "postsublink"));

		// Assign the submission log id so the actual form submission will
		// assign that submission to the contact history record that gets
		// written.
		document.getElementById("ccSubmissionID").value = submissionId;
	}
	catch(e)
	{
		success = "0";
	}

	if (success == "1")
	{
		ShowSuccessfulCCSubmission(url);
		SetCCSectionToReadOnly();
	}
	else
	{
		ShowErroredCCSubmission();
		EnableSubmitPaymentButton();	
	}
}

function UpdateChargeSummary(formFieldElemName)
{
	if (IsMicrositePreview())
	{
		if (isCCInitialized)
			alert(ResourceID8676);
		return;
	}
	
	var formFieldElm;
	if (typeof(formFieldElemName) != "undefined" && formFieldElemName && formFieldElemName != discountCodeCtlId)
		formFieldElm = document.getElementById(formFieldElemName);
	var formFieldVal = "";
	var discountCode = "";
	if (typeof(formFieldElm) != "undefined" && formFieldElm)
	{
		for (var i = 0; i < formFieldElm.options.length; i++)
		{
			var option = formFieldElm.options[i];
			if (option.selected)
			{
				formFieldVal = option.value;
				i = formFieldElm.options.length;
			}
		}
		formFieldValue = formFieldVal;
	}

	var discCodeElm = document.getElementById(discountCodeCtlId);
	if (typeof(discCodeElm) != "undefined" && discCodeElm)
		discountCode = discCodeElm.value;

	var jw = new jsWS('ccsubmission.ashx', 'UpdateChargeSummary');
	jw.SetSync();
	jw.addArg('FormFieldVal', formFieldVal);
	jw.addArg('DiscountCode', discountCode);
	jw.send(PostUpdateChargeSummary);
}

function PostUpdateChargeSummary(req)
{
	var requestResult = req.responseText;
	if (requestResult.length > 0)
	{
		var chargeSummaryTableElm = document.getElementById(chargeSummaryTableId);
		var parent = GetCCParentNode(chargeSummaryTableElm);
		parent.removeChild(chargeSummaryTableElm);
		parent.innerHTML = requestResult;
	}
}
