/*********************************************************************************
 *	
 * doAjax - ajax jQuery javascript library function
 * 
 * Each ajax function can have a before, success and error function
 * If any of these functions are not send to doAjax, then the default function is used
 * 
 */
 
 
function doAjax(url, divId, parms, beforeFunction, successFunction, errorFunction)
{
	$.ajax({
		type: "POST",
		url: url,
		async: true,
		data: parms,
		beforeSend: function(){beforeAjax(divId, beforeFunction);},
		success: function(msg){successAjax(divId, msg, successFunction);},
		error: function(){errorAjax(divId, errorFunction);},
		timeout: 150000
	});
}

function doSyncAjax(url, divId, parms, beforeFunction, successFunction, errorFunction)
{
	$.ajax({
		type: "POST",
		url: url,
		async: false,
		data: parms,
		beforeSend: function(){beforeAjax(divId, beforeFunction);},
		success: function(msg){successAjax(divId, msg, successFunction);},
		error: function(){errorAjax(divId, errorFunction);},
		timeout: 150000
	});
}

function beforeAjax(divId, beforeFunction)
{
	$.blockUI();
	
	if(beforeFunction != null)
	{
		beforeFunction(divId);
	}
	else
	{
		beforeDefault(divId);
	}
}

function beforeDefault(divId)
{
	document.getElementById(divId + "Wait").style.display = "block";
	document.getElementById(divId + "Response").style.display = "none";
}

function successAjax(divId, msg, successFunction)
{
	if(successFunction != null)
	{
		successFunction(divId, msg);
	}	
	else
	{
		successDefault(divId, msg);
	}
	
	$.unblockUI();$.blockUI();$.unblockUI();
}

function successDefault(divId, msg)
{
	document.getElementById(divId + "Wait").style.display = 'none';
	document.getElementById(divId + "Response").innerHTML = msg;
	document.getElementById(divId + "Response").style.display = 'block';
}

function errorAjax(divId, errorFunction)
{
	msg = '<div class="messageBox"><p>The details could not be retrieved, please try again later.</p></div>';
	
	if(errorFunction != null)
	{
		errorFunction(divId, msg);
	}
	else
	{
		errorDefault(divId, msg);
	}
	
	$.unblockUI();$.blockUI();$.unblockUI();
}

function errorDefault(divId, msg)
{
	document.getElementById(divId + "Wait").style.display = 'none';
	document.getElementById(divId + "Response").innerHTML = msg;
	document.getElementById(divId + "Response").style.display = 'block';
}

/*********************************************************************************
 *	
 * 2. Each ajax action can have a do, before and success function
 *
 */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function calcProjValue(url, divId) 
{
	var projY1 = document.getElementById('projY1').value;
	var projY2 = document.getElementById('projY2').value;
	var projY3 = document.getElementById('projY3').value;
	var projDD = document.getElementById('projDD').value;
	var projMM = document.getElementById('projMM').value;
	var projYY = document.getElementById('projYY').value;
		parms='projY1=' + projY1
				+ '&projY2=' + projY2
				+ '&projY3=' + projY3
				+ '&projDD=' + projDD
				+ '&projMM=' + projMM
				+ '&projYY=' + projYY;
						
	doAjax(url, divId, parms, beforeCalcProjValue, successCalcProjValue);
}

function beforeCalcProjValue(divId)
{
	disableButton("projectedValueSubmit");
	
	document.getElementById(divId + "Response").style.display = "none";
	
	beforeDefault(divId);
}

function successCalcProjValue(divId, msg)
{
	enableButton("projectedValueSubmit");
	
	successDefault(divId, msg);
}

function navigate(url, divId, pageNo) 
{
	parms = 'pageNo=' + pageNo;
	doAjax(url, divId, parms);
}

function getSelectedClaimDetails(url, divId, claimNo, elementNo)
{
	parms = 'claimNo=' + claimNo + '&elementNo=' + elementNo;
	doAjax(url, divId, parms);
}

function changePIN(url, divId)
{

	var newPIN = document.getElementById('newpin').value;
	var confirmPIN = document.getElementById('confirmpin').value;
	
	parms = 'newPIN=' + newPIN + '&confirmPIN=' + confirmPIN;

	doAjax(url, divId, parms, beforeDefault, successChangePIN, errorChangePIN);

}

function successChangePIN(divId, msg)
{
	document.getElementById("newPINDiv").style.display = "none";
	msg = "<p>"+msg+"</p>";
	document.getElementById("newpin").value = "";
	document.getElementById("confirmpin").value = "";
	successDefault(divId, msg);
}

function ajaxChangeMailAddress(url, divId)
{
	var emailAddress = document.getElementById('emailAddress').value;
	parms = 'emailAddress=' + emailAddress;
	
	doAjax(url, divId, parms, emptyFunction, successUpdateDetails);
}

function ajaxChangeMaidenName(url, divId)
{
	var maidenName = document.getElementById('maidenName').value;
	parms = 'maidenName=' + maidenName;
	
	doAjax(url, divId, parms, emptyFunction, successUpdateDetails);
}

function ajaxChangePhoneNumber(url, divId)
{
	var phoneNumber = document.getElementById('phoneNumber').value;
	parms = 'phoneNumber=' + phoneNumber;
	
	doAjax(url, divId, parms, emptyFunction, successUpdateDetails);
}

function ajaxChangeOtherPhoneNumber(url, divId)
{
	var phoneNumber = document.getElementById('otherPhone').value;
	parms = 'otherPhone=' + phoneNumber;
	
	doAjax(url, divId, parms, emptyFunction, successUpdateDetails);
}

function ajaxCancelElectronicNotification(url, divId)
{
	parms = '';
	doAjax(url, divId, parms, emptyFunction, successElectronicNotification);
}

function successElectronicNotification(divId, msg)
{
	msg = "<p>"+msg+"</p>";
	document.getElementById(divId + "Response").innerHTML = msg;
	document.getElementById(divId + "Response").style.display = 'block';
}

function successUpdateDetails(divId, msg)
{
	// so the msg comes back red do this
	msg = "<p>"+msg+"</p>";
	document.getElementById(divId + "Response").innerHTML = msg;
	document.getElementById(divId + "Response").style.display = 'block';
}

function ajaxSubmitElectronicNotificationRequest(url, divId, emailAddress, activationCode)
{
	if(emailAddress != '')
	{
		parms = 'emailAddress=' + emailAddress;
	}
	else if(activationCode != '')
	{
		parms = 'activationCode=' + activationCode;
	}
	else
	{
		parms = ''
	}
	doAjax(url, divId, parms, emptyFunction, successElectronicNotification);
}

function ajaxRegistrationElectronicCorrespondence(url, divId, emailAddress)
{
	parms = 'emailAddress=' + emailAddress;
	
	doAjax(url, divId, parms, emptyFunction, successElectronicNotification);
}

function errorUpdateDetails(divId, msg)
{
	msg = "<p>"+msg+"</p>";
	errorDefault(divId, msg);
}

function errorChangePIN(divId, msg)
{
	msg = "<p>"+msg+"</p>";
	errorDefault(divId, msg);
}

function emptyFunction()
{

}

function getFundPrices(url, divId, fundId)
{
	if(document.getElementById("fund-"+fundId).style.display == 'none')
	{
		parms = 'fundId=' + fundId;
		doSyncAjax(url, divId, parms, beforeDefault, successGetPrices);
	}
}

function successGetPrices(divId, msg)
{
	timeout = msg.indexOf("timed out");
	if(timeout == -1)
	{
		successDefault(divId, msg);
	}
	else
	{
		window.location = "login/logon.do?timeout=true";
	}
}

