// http://www.sitepoint.com/article/standards-compliant-world/3
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function promise() {
	msgWindow=open('/promise/','promise',"'resizable=no,scrollbars=auto,toolbar=0,width=700,height=350'");
	if (msgWindow.opener == null) msgWindow.opener = self;
	msgWindow.focus();
}

function addEvent(obj, evType, fn) { 
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, true); 
		return true; 
	} else if (obj.attachEvent) { 
		var r = obj.attachEvent('on' + evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
var totalValue = 0;
var credValue = 0;
var income = 0;
var rent = 0;
var expenses = 0;
var secpay = 0;
var secowed = 0;
var payin = 0;
var debtstotal = 0;
var unsecowed = 0;
var unsecpsy = 0;
var ccowed = 0;
var scowed = 0;
var ccpay = 0;
var scpay = 0;
var otherpay = 0;
var otherowed = 0;
var years = 0;
var term = 0;
var credValue = 0;
var recofs = 'no';
var reco = 0;
var creditors = 0;
var titleX = '';
var forename = '';
var surname = '';
var	housenumber = '';
var address1 = '';
var address2 = '';
var city = '';
var county = '';
var postcode = '';
var dayphone = '';
var email = '';

// clear the field of a zero or a NaN error
function clearField(id)
{	
	if(document.getElementById(id).value == 0 || document.getElementById(id).value == 'NaN')
		document.getElementById(id).value = '';
}
// take an elementId
function validateIntInput(id)
{
	v = document.getElementById(id).value;
	v = v.replace(/[^0-9]/i,'');
	document.getElementById(id).value = v;	
	
	return v;
}

function assignInt(x)
{
	if(x == '' || x <= 0)
		x = 0;
	else
		x = parseInt(x,10);
	return x;
}
function assignStr(id)
{
	var v = document.getElementById(id).value;
	return v;
}

function analyse() {
	
	titleX = assignStr('titleUniqueElement');
	forename = assignStr('forenameUniqueElement');
	surname = assignStr('surnameUniqueElement');
	housenumber = assignStr('housenumberUniqueElement');
	address1 = assignStr('address1UniqueElement');
	address2 = assignStr('address2UniqueElement');
	city = assignStr('cityUniqueElement');
	county = assignStr('countyUniqueElement');
	postcode = assignStr('postcodeUniqueElement');
	dayphone = assignStr('dayphoneUniqueElement');
	email = assignStr('emailUniqueElement');
	term = assignStr('termUniqueElement');

	income = validateIntInput('incomeUniqueElement');
	income = assignInt(income);
	
	rent = validateIntInput('rentUniqueElement');
	rent = assignInt(rent);
	
	expenses = validateIntInput('expensesUniqueElement');
	expenses = assignInt(expenses);	
	
	secpay = validateIntInput('secpayUniqueElement');		
	secpay = assignInt(secpay);
			
	unsecowed = validateIntInput('unsecowedUniqueElement'); 
	unsecowed = assignInt(unsecowed);
	
	secowed = validateIntInput('secowedUniqueElement'); 
	secowed = assignInt(secowed);
	
	ccowed = validateIntInput('ccowedUniqueElement'); 
	ccowed = assignInt(ccowed); 
	
	scowed = validateIntInput('scowedUniqueElement'); 
	scowed = assignInt(scowed);
	
	otherowed = validateIntInput('otherowedUniqueElement');
	otherowed = assignInt(otherowed);
	
	unsecpay = validateIntInput('unsecpayUniqueElement'); 
	unsecpay = assignInt(unsecpay);
	
	ccpay = validateIntInput('ccpayUniqueElement'); 
	ccpay = assignInt(ccpay); 
	
	scpay = validateIntInput('scpayUniqueElement'); 
	scpay = assignInt(scpay);
	
	otherpay = validateIntInput('otherpayUniqueElement');
	otherpay = assignInt(otherpay);
		
	totalValue = income - rent - expenses;

	if (totalValue <= 0) 
	{
		document.getElementById('totalUniqueElement').value = 0;
	} 
	else 
	{
		
		document.getElementById('totalUniqueElement').value = totalValue;
		
		payin = totalValue - secpay;
		
		document.getElementById('payinUniqueElement').value = payin;
		
		debtstotal = unsecowed + ccowed + scowed + otherowed;
		
		document.getElementById('debtstotalUniqueElement').value = debtstotal;
		
		term = validateIntInput('termUniqueElement');
		term = assignInt(term);
		
		years = term;
		
		document.getElementById('yearsUniqueElement').innerHTML = term;
				
		if(payin > 0 && debtstotal > 0) 
		{
			credValue = Math.round((payin * term * 12) / debtstotal * 100);
			
			recofs = 'yes'; 
			
			if (credValue > 100) 
			{
				credValue = 100;
			}
			
			document.getElementById('creditorsUniqueElement').value = credValue;
			
			document.getElementById('recofsUniqueElement').style.display = 'block';
			
			if (credValue > 35) 
			{
				reco = 'We think we can revive your finances with an IVA.';
				document.getElementById('recoUniqueElement').innerHTML = reco;
			} 
			else if (credValue >= 15) 
			{
				reco = 'We need to know a little more information about your situation. Please fill in your details and hit the call back button below and we will call you within 24 hours to discuss your options.';
				document.getElementById('recoUniqueElement').innerHTML = reco;
			} 
			else 
			{
				reco = 'Unfortunately an IVA does not seem to be suitable to you. If you have any other assets an IVA may still be possible. Please fill in your details and hit the call back button below and we will call you within 24 hours to discuss your options.';
				document.getElementById('recoUniqueElement').innerHTML = reco;
			}
		}
	}
}

addEvent(window, 'load', externalLinks);