function checkGifAid(formName){
	var continueSubmit = fillHiddenFields();
	if(continueSubmit){
		insertRecord(formName);
	}
}

function insertRecord(formName){
	var cartIdObj = document.getElementById('cartId');
	var transactionDateObj = document.getElementById('transactionDate');
	var titleObj = document.getElementById('M_title');
	var firstNameObj = document.getElementById('M_first_name');
	var lastNameObj = document.getElementById('M_last_name');
	var amountObj = document.getElementById('amount');
	var descObj = document.getElementById('M_desc');
	var descriptionVar = descObj.value;
	if(descriptionVar == ''){
		descriptionVar = 'Donation';
	}
	var emailObj = document.getElementById('M_email');
	var telObj = document.getElementById('M_tel');
	var addressObj = document.getElementById('M_address');
	var townObj = document.getElementById('M_town');
	var countyObj = document.getElementById('M_county');
	var postcodeObj = document.getElementById('M_postcode');
	var countryObj = document.getElementById('M_country');
	var hiddenCurrencyObj = document.getElementById('currency');
	var giftAidObj = document.getElementById('M_giftaid');
	var ajaxVar = new ajaxVars();
	ajaxVar.vars['action'] = "runFunction";
	ajaxVar.vars['func'] = "insertRecord";
	ajaxVar.vars['nosplit'] = 1;
	ajaxVar.vars['cartId'] = cartIdObj.value;
	ajaxVar.vars['transactionDate'] = transactionDateObj.value;
	ajaxVar.vars['title'] = titleObj.value;
	ajaxVar.vars['firstName'] = firstNameObj.value;
	ajaxVar.vars['lastName'] = lastNameObj.value;
	ajaxVar.vars['amount'] = amountObj.value;
	ajaxVar.vars['description'] = descriptionVar;
	ajaxVar.vars['email'] = emailObj.value;
	ajaxVar.vars['telephoneNumber'] = telObj.value;
	ajaxVar.vars['address'] = addressObj.value;
	ajaxVar.vars['town'] = townObj.value;
	ajaxVar.vars['county'] = countyObj.value;
	ajaxVar.vars['currency'] = hiddenCurrencyObj.value;
	ajaxVar.vars['giftAid'] = giftAidObj.value;
	ajaxVar.fnVars['formName'] = formName;
	ajaxVar.gotoFunction = 'insertRecordDone';
	ajaxVar.AjaxPostSQL();
}

function insertRecordDone(dataArr, fnVars){
	formObj = eval("document." + fnVars['formName']);
	if(formObj){
		if(formObj.M_giftaid.checked == true){
			formObj.submit();
		}else{
			openFlashDiv();
			return false;
		}
	}
}

function fillHiddenFields(){
	// Fill the desc
	var descObj = document.getElementById('M_desc');
	var hiddenDescObj = document.getElementById('desc');
	hiddenDescObj.value = descObj.value;
	if(descObj.value == ''){
		hiddenDescObj.value = 'Donation';
	}
	// Fill the name
	var firstNameObj = document.getElementById('M_first_name');
	var lastNameObj = document.getElementById('M_last_name');
	var hiddenNameObj = document.getElementById('name');
	hiddenNameObj.value = firstNameObj.value + ' ' + lastNameObj.value;
	// Fill the email
	var emailObj = document.getElementById('M_email');
	var hiddenEmailObj = document.getElementById('email');
	hiddenEmailObj.value = emailObj.value;
	// Fill the tel
	var telObj = document.getElementById('M_tel');
	var hiddenTelObj = document.getElementById('tel');
	hiddenTelObj.value = telObj.value;
	// Fill the address
	var addressObj = document.getElementById('M_address');
	var townObj = document.getElementById('M_town');
	var countyObj = document.getElementById('M_county');
	var hiddenAddressObj = document.getElementById('address');
	hiddenAddressObj.value = addressObj.value + ' ' + townObj.value + ' ' + countyObj.value;
	// Fill the postcode
	var postcodeObj = document.getElementById('M_postcode');
	var hiddenPostcodeObj = document.getElementById('postcode');
	hiddenPostcodeObj.value = postcodeObj.value;
	return true;
}

function gotoWorldPay (formName, flag){
	closeFlashDiv();
	formObj = eval("document." + formName);
	if(formObj){
		if(flag){
			formObj.M_giftaid.checked = true;
			formObj.submit();
		}else{
			formObj.submit();
		}
	}
}

function closeFlashDiv(){
	var FlashDiv = document.getElementById('FlashDiv');
	var innerPopFlash = document.getElementById('innerPopFlash');
	var mainFlashDiv = document.getElementById('mainFlashDiv');
	
	innerPopFlash.style.visibility = 'hidden';
	innerPopFlash.style.display = 'none';
	
	FlashDiv.style.height = "0px";
	FlashDiv.style.width = "0px";
	FlashDiv.style.visibility = 'hidden';
	FlashDiv.style.display = 'block';	
}

function openFlashDiv(){
	var FlashDiv = document.getElementById('FlashDiv');
	var innerPopFlash = document.getElementById('innerPopFlash');
	
	var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
	
	var leftPos = (windowWidth/2) -100;
	
	innerPopFlash.style.visibility = 'visible';
	innerPopFlash.style.position = 'absolute';
	innerPopFlash.style.display = 'block';
	innerPopFlash.style.zIndex = '1001';
	innerPopFlash.style.top = ((document.body.scrollHeight - windowHeight) + 150) + "px";
	innerPopFlash.style.left  = leftPos + "px";
    
    FlashDiv.style.visibility = 'visible';
	FlashDiv.style.position = 'absolute';
	FlashDiv.style.display = 'block';
	FlashDiv.style.zIndex = '1000';
	FlashDiv.style.height = document.body.scrollHeight + 'px';
	FlashDiv.style.width = document.body.scrollWidth + 'px';
	FlashDiv.style.top = '0px';
	FlashDiv.style.left = '0px';
	FlashDiv.style.filter = "Alpha(Opacity=75)"; //for IE   
	FlashDiv.style.opacity = "0.75"; //for FF
	FlashDiv.style.backgroundColor = "#343434";
}


/*

<div id="FlashDiv" style="position: absolute; text-align: center; padding-top: 100px; visibility: hidden; display: none; z-index: 1000;"></div> 
<div id="innerPopFlash" style="background-color:#fe7900;width:980px;visibility:hidden;display:none;position:absolute;text-align:center;z-index:999;" align="center">
	<div id="mainFlashDiv" style="width:980px;height:400px;">
		<div>If you are a UK taxpayer, the value of your donation can increase by nearly a third under the Gift Aid Scheme — at no additional cost to you! Please tick the box below to join the Gift Aid scheme.</div>
		<div><img src="images/spacer.gif" height="10"></div>
		<div>". formElement('submit', 'save', translate_text("Continue with Gift Aid"), 'buttons', " style=\"cursor: pointer;\" onclick=\"gotoWorldPay('worldpay', 1);\" ")." ". formElement('submit', 'save', translate_text("Continue without Gift Aid"), 'buttons', " style=\"cursor: pointer;\" onclick=\"gotoWorldPay('worldpay', 0);\" ")."</div>
		<div><img src="images/spacer.gif" height="10"></div>
		<div>". formElement('submit', 'save', translate_text("Cancel"), 'buttons', " style=\"cursor: pointer;\" onclick=\"closeFlashDiv();\" ")."</div>
		<div><img src="images/spacer.gif" height="10"></div>
	</div>
	<div ><img src="images/spacer.gif" height="20"></div>
</div>


*/