//use browser sniffing to determine if IE or Opera (ugly, but required)
var isOpera, isIE = false;
if(typeof(window.opera) != 'undefined'){isOpera = true;}
if(!isOpera){ var isIE = /*@cc_on!@*/false; } // dirty browser detect
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

function revealButton() {

}

// error box close
function closeErrorBox(){
	if(document.getElementById('ErrorContainer')){
		document.getElementById('ErrorContainer').style.display = 'none';
	}
}

// handle the submission of the form

window.addEvent('domready', function(){
	if($('appForm')){
	$('appForm').addEvent('submit', function(e) {
		//prevent the submit event
		new Event(e).stop();

		// outputs results if no errors
		
		//var url = "../ajax/validate_online_app.php";
		//var postString = this;	
		//new Ajax(url, {method: 'post',data: postString,onComplete: showResponse}).request();
		
		var url = "../ajax/validate_online_app.php";
		var postString = this;	
		
		var request = new Request({
			url: url,
			method: 'post',
			data: postString,
			onComplete: showResponse}).send();

		
	})};
	if($('appFormPart2')){
	$('appFormPart2').addEvent('submit', function(e) {
		//prevent the submit event
		new Event(e).stop();
		
		var sols = document.getElementsByName('solution');
		var allDisabled=true;
		for(var i=0; i<sols.length; i++){
			if(sols[i].disabled==false){
				allDisabled=false;
			}
		}
		
		if(allDisabled){
			this.elements['solution'][0].disabled=false;
			this.elements['solution'][0].checked=true;
		}
		//alert(this.elements['solution'][0].checked);

		// outputs results if no errors
		
		//var url = "../ajax/validate_online_app.php";
		//var postString = this;	
		//new Ajax(url, {method: 'post',data: postString,onComplete: showResponse}).request();
		
		var url = "../ajax/validate_online_app.php";
		var postString = this;	
		
		var request = new Request({
			url: url,
			method: 'post',
			data: postString,
			onComplete: showResponse}).send();


	})};
});



// show the error response if there is one else submit the form

function showResponse(request){
	if (request!='') {
		var response = JSON.decode(request || false);
	
		if(!$('ErrorContainer')){
			alert('There was an error processing the form, however the error cannot be displayed as the Error Container is missing!');
		}
		else{
			var errBox = $('ErrorContainer');
			errBox.innerHTML = response['errors'];
		
			var newTop = window.screen.availHeight/2-200;
			var newLeft = window.screen.availWidth/2-400;
		
			if (isIE){
				errBox.style.top = (newTop+document.documentElement.scrollTop)+'px';
				errBox.style.left = newLeft+'px';
			}
			else{
				errBox.style.top = (newTop+window.pageYOffset)+'px';
				errBox.style.left = newLeft+'px';
			}
			
			errBox.style.display = 'block';
			errBox.makeDraggable();
		}
	}
	else{
		if($('appForm')){
			$('appForm').submit();
		}
		else if($('appFormPart2')){
			$('appFormPart2').submit();
		}
		else{
			alert('I\'m sorry, the form to submit could not be found.');
		}
	}
}

/*
Function showBox
	Shows or hides an element depending on the value of a form element.

Arguments
toShow - The element to show/hide.
yesNo - A form element object, can either be a checkbox or a simple yes/no select box.
*/
function showBox(toShow,yesNo){
	var show=false;
	switch(yesNo.type){
		case 'select-one': //Allow a drop down menu with options 'yes' and 'no'
			show = (yesNo.options[yesNo.selectedIndex].value == 'yes');
		break;
		default://checkbox
			show=yesNo.checked;
		break;
	}
	if(show){
		$(toShow).setStyle('display','block');
		}else{
		$(toShow).setStyle('display','none');	
		}
}
function checkNumeric(element){
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(element.value)||element.value==''){
		return true;
	}
	else{
		element.value=0;
		return false;
	}
}
	
function updateDebt(){
	totalDebt=0;
	noCreditors = 0;
	for(i=0;i<25;i++){
		debt = "debt"+i;
		creditor = "creditor"+i;
		if($(debt).value>0){
			
			totalDebt = totalDebt+Number($(debt).value);
			if(noCreditors==0){
				postString=debt+"="+$(debt).value+"&"+creditor+"="+$(creditor).value;
			}else{
				postString+="&"+debt+"="+$(debt).value+"&"+creditor+"="+$(creditor).value;
				
			}	
			
			noCreditors++;
		}
		
	}
	
	
	
	var url = "../ajax/creditors_online.php";
//	new Ajax(url, {method: 'post',data: postString}).request();

		var request = new Request({
			url: url,
			method: 'post',
			data: postString}).send();

	
	
	
	$('totaldebts').value = totalDebt;
	$('totaldebts2').value = totalDebt;
	$('MainDebtor_NumberOfCreditors').value = noCreditors;
	showForm2();
}	
	
function updateIncome(){
	
	totalIncome = calcPCM($('MainDebtor_Inc').value,$('MainDebtor_Inc_Freq').value);
	totalIncome+= calcPCM($('JointDebtor_Inc').value,$('JointDebtor_Inc_Freq').value);
	if($('Ben_CB').value>=1){
		totalIncome+=87;	
	}
	if($('Ben_CB').value>1){
		totalIncome+=(($('Ben_CB').value-1) *57);	
	}
	
	
	totalIncome+= calcPCM($('Ben_CTC').value,$('Ben_CTC_Freq').value);
	totalIncome+= calcPCM($('Ben_WTC').value,$('Ben_WTC_Freq').value);
	totalIncome+= calcPCM($('Ben_IS').value,$('Ben_IS_Freq').value);
	totalIncome+= calcPCM($('Ben_JSA').value,$('Ben_JSA_Freq').value);
	totalIncome+= calcPCM($('Ben_DLA').value,$('Ben_DLA_Freq').value);
	totalIncome+= calcPCM($('Ben_IB').value,$('Ben_IB_Freq').value);
	totalIncome+= calcPCM($('Ben_HB').value,$('Ben_HB_Freq').value);
	totalIncome+= calcPCM($('Ben_CTB').value,$('Ben_CTB_Freq').value);
	totalIncome+= calcPCM($('Ben_PP').value,$('Ben_PP_Freq').value);
	totalIncome+= calcPCM($('Ben_PC').value,$('Ben_PC_Freq').value);
	totalIncome+= calcPCM($('Ben_SP').value,$('Ben_SP_Freq').value);
	totalIncome+= calcPCM($('Ben_CA').value,$('Ben_CA_Freq').value);
	totalIncome+= calcPCM($('Ben_OTH').value,$('Ben_OTH_Freq').value);
	

	
	
	$('incomeCalcBox').setStyle('display','none');
	$('showForm').setStyle('display','block');
	$('MainDebtor_Income').value = totalIncome.toFixed(2);
	$('MainDebtor_Income2').value = totalIncome.toFixed(2);
	
	totalBens = 0;
	
	if($('Ben_CB').value>=1){
		totalBens+=87;	
	}
	if($('Ben_CB').value>1){
		totalBens+=(($('Ben_CB').value-1) *57);	
	}
	
	totalBens+= calcPCM($('Ben_CTC').value,$('Ben_CTC_Freq').value);
	totalBens+= calcPCM($('Ben_WTC').value,$('Ben_WTC_Freq').value);
	totalBens+= calcPCM($('Ben_IS').value,$('Ben_IS_Freq').value);
	totalBens+= calcPCM($('Ben_JSA').value,$('Ben_JSA_Freq').value);
	totalBens+= calcPCM($('Ben_DLA').value,$('Ben_DLA_Freq').value);
	totalBens+= calcPCM($('Ben_IB').value,$('Ben_IB_Freq').value);
	totalBens+= calcPCM($('Ben_HB').value,$('Ben_HB_Freq').value);
	totalBens+= calcPCM($('Ben_CTB').value,$('Ben_CTB_Freq').value);
	totalBens+= calcPCM($('Ben_PC').value,$('Ben_PC_Freq').value);
	totalBens+= calcPCM($('Ben_CA').value,$('Ben_CA_Freq').value);
	totalBens+= calcPCM($('Ben_OTH').value,$('Ben_OTH_Freq').value);
	
	$('MainDebtor_Benefits').value = totalBens.toFixed(2);
	
	
	
	
	
	var url = "../ajax/benefits_online.php";
		
		postString='Ben_CTC='+ $('Ben_CTC').value;
		postString+='&Ben_CTC_Freq='+ $('Ben_CTC_Freq').value;
		postString+='&Ben_CB='+ $('Ben_CB').value;
		postString+='&Ben_WTC='+ $('Ben_WTC').value;
		postString+='&Ben_WTC_Freq='+ $('Ben_WTC_Freq').value;
		
		postString+='&Ben_IS='+ $('Ben_IS').value;
		postString+='&Ben_IS_Freq='+ $('Ben_IS_Freq').value;
		
		postString+='&Ben_JSA='+ $('Ben_JSA').value;
		postString+='&Ben_JSA_Freq='+ $('Ben_JSA_Freq').value;
		
		
		postString+='&Ben_DLA='+ $('Ben_DLA').value;
		postString+='&Ben_DLA_Freq='+ $('Ben_DLA_Freq').value;
		
		postString+='&Ben_IB='+ $('Ben_IB').value;
		postString+='&Ben_IB_Freq='+ $('Ben_IB_Freq').value;
		
		postString+='&Ben_HB='+ $('Ben_HB').value;
		postString+='&Ben_HB_Freq='+ $('Ben_HB_Freq').value;
		
		postString+='&Ben_CTB='+ $('Ben_CTB').value;
		postString+='&Ben_CTB_Freq='+ $('Ben_CTB_Freq').value;
		
		postString+='&Ben_PP='+ $('Ben_PP').value;
		postString+='&Ben_PP_Freq='+ $('Ben_PP_Freq').value;
		
		
		postString+='&Ben_PC='+ $('Ben_PC').value;
		postString+='&Ben_PC_Freq='+ $('Ben_PC_Freq').value;
		
		postString+='&Ben_SP='+ $('Ben_SP').value;
		postString+='&Ben_SP_Freq='+ $('Ben_SP_Freq').value;
		
		postString+='&Ben_CA='+ $('Ben_CA').value;
		postString+='&Ben_CA_Freq='+ $('Ben_CA_Freq').value;
		
		postString+='&Ben_OTH='+ $('Ben_OTH').value;
		postString+='&Ben_OTH_Freq='+ $('Ben_OTH_Freq').value;
		
		postString+='&MainDebtor_Inc='+ $('MainDebtor_Inc').value;
		postString+='&MainDebtor_Inc_Freq='+ $('MainDebtor_Inc_Freq').value;
		
		postString+='&JointDebtor_Inc='+ $('JointDebtor_Inc').value;
		postString+='&JointDebtor_Inc_Freq='+ $('JointDebtor_Inc_Freq').value;
		
//		new Ajax(url, {method: 'post',data: postString}).request();
		
		var request = new Request({
			url: url,
			method: 'post',
			data: postString}).send();


	
}
function showCalc(){
	$('incomeCalcBox').setStyle('display','block');
	$('showForm').setStyle('display','none');
	
}
function showCalc2(){
	$('debtCalcBox').setStyle('display','block');
	$('showForm').setStyle('display','none');
	//$('tipper').setStyle('display','none');
}
function showForm(){
	$('incomeCalcBox').setStyle('display','none');
	$('showForm').setStyle('display','block');	
}	
function showForm2(){
	$('debtCalcBox').setStyle('display','none');
	$('showForm').setStyle('display','block');	
}
function calcPCM(income,period){
	income=Number(income);
	returnVal=0;
	switch (period){
		
		case "2":
			returnVal= income;
			break;
		case "0":
			returnVal= ((income*5)*(52/12));
			break;
		
		case "1":
			returnVal= income*(52/12);
			break;
		case "biWeekly":
			returnVal= income*(26/12);
			break;
		case "qdWeekly":
			returnVal= income*(13/12);
			break;
		
		}
	

	return returnVal;
}	
function showBen(){
	
	if($('MainDebtor_EmploymentStatus').value=='Retired'){
		$('mdBen').setStyle('display','block');
		$('mdBenInfo').setStyle('display','block');
	}else{
		$('mdBen').setStyle('display','none');
		$('mdBenInfo').setStyle('display','none');
	}
}	
function relationships(){
	if($('Relationship_Status').value=='Single living with Parents'||$('Relationship_Status').value=='Separated living with my Parents'||$('Relationship_Status').value=='Divorced living with my Parents'){
		$('Housing_Status').value = 'Living with Parents/House share';
		$('Housing_Status').disabled = 'disabled';
		
		
		}else{
		$('Housing_Status').disabled = '';	
			
		}
	if($('Relationship_Status').value=='Single living with Partner'||$('Relationship_Status').value=='Separated living with my Partner'||$('Relationship_Status').value=='Divorced living with my Partner'){
	
		//$('partnerIncome').setStyle('display','block');
		//$('JointDebtor_Income').setStyle('display','block');
	
	}else{
		//$('JointDebtor_Income').setStyle('display','none');
	}	
	
	
	
}
function partnersIncome(){
	if($('partnerIncluded').value=='no'){
		$('JointDebtor_Income').setStyle('display','block');
		
		}
	if($('partnerIncluded').value=='yes'){
		$('JointDebtor_Income').setStyle('display','none');
		$('JointDebtor_Income').value = 0;
		
		}	
		
	
}
function otherIncome(){
	if($('otherIncluded').value=='no'){
		$('Other_Income').setStyle('display','none');
		$('Other_Income').value = 0;
		}
	if($('otherIncluded').value=='yes'){
		$('Other_Income').setStyle('display','block');
		
		
		}	
		
	
}




function housingStatus(){
	if($('Housing_Status').value=='Homeowner'||$('Equity').value>0){
			$('mortgageDiv').setStyle('display','block');
			//$('Secured_Debts').setStyle('display','block');
			
		
		}else{
			
			$('Secured_Debts').value=0;
			$('Date').value=0;
			$('Lender_Name').value=0;
			$('Arrears').value=0;
			
			
			//$('Secured_Debts').setStyle('display','none');
			$('mortgageDiv').setStyle('display','none');
			
		}
	if($('Housing_Status').value=='Private Tenant'||$('Housing_Status').value=='Council Tenant'){
			$('rentdiv').setStyle('display','block');
		
		}else{
			$('Rent').value=0;
			$('rentdiv').setStyle('display','none');
			
			
		}	
	if($('Housing_Status').value=='Living with Parents/House share'){
			$('LWP').setStyle('display','block');
		
		}else{
			
			$('Board').value=0;
			$('BillsInc').value=0;
			$('LWP').setStyle('display','none');
		}	
	
	
	
}	
function vehicles(){
	
	vehicle = $('Vehicles').value;
	
	if($('Vehicles_HP').value=='no'||$('Vehicles_HP').value=='0'||vehicle==0){
		$('VehiclesDiv').setStyle('display','none');
		$('VehiclesDiv').innerHTML = '';
				
		}else{
			
			
			code='<p>Please enter the details of your finance payments below</p>\n';
			for(i = 1; i<=vehicle;i++){
				
				code+="Monthly Payment:<input type='text' name='payments[]' value='0' size='4' maxlength='4' onkeyup='checkNumeric(this);' /> ";
				code+="Payments Remaining:<input type='text' name='paymentsRemaining[]' value='0' size='4' maxlength='4' onkeyup='checkNumeric(this);' /> ";
				code+="Optional/Final Purchase Fee:<input type='text' name='purchaseFee[]' value='0' size='4' maxlength='4' onkeyup='checkNumeric(this);' /> <br />\n";
				
				
				}
			$('VehiclesDiv').innerHTML=code;	
			$('VehiclesDiv').setStyle('display','block');
			
			
			}
	if(vehicle>0){
		$('mileagediv').setStyle('display','block');
		$('Vehicles_HPdiv').setStyle('display','block');
		$('Vehicles_HP').setStyle('display','block');
	}else{
		$('Vehicles_HPdiv').setStyle('display','none');
		$('Vehicles_HP').setStyle('display','block');
		$('mileagediv').setStyle('display','none');
		$('mileage').value=0;
	}	
}	

function children(){
	numChildren = $('Dependent_Children').value;
	
	if(numChildren==0){
		
		$('childrenDiv').setStyle('display','none');
		$('childrenDiv').innerHTML = '';
		
		}else{
			
			code='<p>Please enter the dates of birth below</p>\n';
			for(i = 1; i<=numChildren;i++){
				
				code+="Date of Birth (e.g. 25-Dec-2007) <input type='text' name='child[]' value='' size='11' maxlength='11'  /><br />&nbsp;<br /> ";
				
				
				}
			$('childrenDiv').innerHTML=code;	
			$('childrenDiv').setStyle('display','block');
			
			
			
			
			}
	
	
	}
	
function checkLimits(itemName){
	// AJAX call to check against stored session details
	alert(itemName.name);	
	var url = "../ajax/limits.php";
	var postString = "itemName="+itemName.name;	
	//new Ajax(url, {method: 'post',data: postString,onComplete: limitsReturn}).request();

	var request = new Request({
		url: url,
		method: 'post',
		data: postString,
		onComplete: limitsReturn}).send();


	
}
function limitsReturn(request){
// could add a tick or cross to show where we are outside guidelines
	if(request){
		requestSplit = request.split('|');
		itemName = requestSplit[0];
		message = requestSplit[1];
		$(itemName+'info').innerHTML=message;
	}	


}	