// JavaScript Document

function getQuote()
{
	var theForm = document.forms['calc'];

	var adjust = 1.0;
	var borrowAmount = 0.0;

	var propertyValue = theForm.property_value.value;
	var outstandingValue = theForm.outstanding_balance.value;
	var bodyTxt = "";

	if ( "" == theForm.property_value.value || isNaN(theForm.property_value.value) )
	{
		alert("Please enter your property value");
		return false;
	}

	if ( "" == theForm.outstanding_balance.value || isNaN(theForm.outstanding_balance.value) )
	{
		alert("Please enter your outstanding value");
		return false;
	}

	if ( "Excellent" == theForm.credit_rating.value )
	{
		adjust = 0.85;
	}
	else if ( "Good" == theForm.credit_rating.value )
	{
		adjust = 0.8; 
	}
	else if ( "Average" == theForm.credit_rating.value )
	{
		adjust = 0.75;
	}
	else if ( "Poor" == theForm.credit_rating.value )
	{
		adjust = 0.65;
	}

	borrowAmount = (adjust * propertyValue) - outstandingValue;
	borrowAmount = borrowAmount.toFixed(2);


	if ( document.getElementById )
	{
		document.getElementById("drawarea").style.display = 'none';
		document.getElementById("resultarea").style.display = 'block';
		document.getElementById("borrowAmount").innerHTML = "&pound;" + borrowAmount;
	}
	else
	{
		alert('You can borrow: &pound;' + borrowAmount);
	}
}

function resetCalc()
{
	var theForm = document.forms['calc'];

	theForm.outstanding_balance.value = '';
	theForm.property_value.value = '';

	document.getElementById("drawarea").style.display = 'block';
	document.getElementById("resultarea").style.display = 'none';
}


// CHECKING THE APPLICATION FORM IS ENTERED CORRECTLY
function checkForm()
{
var ctitle, cfirstname, csurname, cdob_day, cdob_month, cdob_year, chometel, cemail, cgender, cmarital_status, cdependants, _houseno, caddress_line1, caddress_city, caddress_postcode, caddress_years, caddress_months, caddress_type, cloan_purpose, camount, cproperty_type, cpayment, carrears, cemployment_status, cincome;
with(window.document.form)
{
ctitle = title;
ctitle.style.backgroundColor = '#ffffff';
cfirstname = firstname;
cfirstname.style.backgroundColor = '#ffffff';
csurname = surname;
csurname.style.backgroundColor = '#ffffff';
cdob_day = dob_day;
cdob_day.style.backgroundColor = '#ffffff';
cdob_month = dob_month;
cdob_month.style.backgroundColor = '#ffffff';
cdob_year = dob_year;
cdob_year.style.backgroundColor = '#ffffff';
chometel = hometel;
chometel.style.backgroundColor = '#ffffff';
cemail = email;
cemail.style.backgroundColor = '#ffffff';
cgender = sex;
cgender.style.backgroundColor = '#ffffff';
cmarital_status = marital_status;
cmarital_status.style.backgroundColor = '#ffffff';
cdependants = dependants;
cdependants.style.backgroundColor = '#ffffff';
caddress_houseno = address_houseno;
caddress_houseno.style.backgroundColor = '#ffffff';
caddress_line1 = address_line1;
caddress_line1.style.backgroundColor = '#ffffff';
caddress_city = address_city;
caddress_city.style.backgroundColor = '#ffffff';
caddress_postcode = address_postcode;
caddress_postcode.style.backgroundColor = '#ffffff';
caddress_years = address_years;
caddress_years.style.backgroundColor = '#ffffff';
caddress_months = address_months;
caddress_months.style.backgroundColor = '#ffffff';
caddress_type = address_type;
caddress_type.style.backgroundColor = '#ffffff';
cloan_purpose = loan_purpose;
cloan_purpose.style.backgroundColor = '#ffffff';
camount = amount;
camount.style.backgroundColor = '#ffffff';
cproperty_type = property_type;
cproperty_type.style.backgroundColor = '#ffffff';
cpayment = payment;
cpayment.style.backgroundColor = '#ffffff';
carrears = arrears;
carrears.style.backgroundColor = '#ffffff';
cemployment_status = employment_status;
cemployment_status.style.backgroundColor = '#ffffff';
cincome = income;
cincome.style.backgroundColor = '#ffffff';
}
   if (trim(ctitle.value) == "")
   {
	  ctitle.style.backgroundColor = '#F8C5C5';
	  alert('Please select your title');
	  ctitle.focus();
	  return false;
   }   	
	   
   else if (trim(cfirstname.value) == "")
   {
	  cfirstname.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your first name');
	  cfirstname.focus();
	  return false;
   }
   else if(trim(csurname.value) == "")
   {
	  csurname.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your surname');
	  csurname.focus();
	  return false;
   }
   else if(trim(cdob_day.value) == "")
   {
	  cdob_day.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your date of birth');
	  cdob_day.focus();
	  return false;
   }
   else if(trim(cdob_month.value) == "")
   {
	  cdob_month.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your date of birth');
	  cdob_month.focus();
	  return false;
   }
   else if(trim(cdob_year.value) == "")
   {
	  cdob_year.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your date of birth');
	  cdob_year.focus();
	  return false;
   }   
   else if((isNaN(chometel.value))||(chometel.value==""))
   {
	  chometel.style.backgroundColor = '#F8C5C5';
	  alert('Please enter a valid telephone number');
	  chometel.focus();
	  return false;
   }
   
   else if(!isEmail(trim(cemail.value)))
   {
	  cemail.style.backgroundColor = '#F8C5C5';
	  alert('Please enter a valid email address');
	  cemail.focus();
	  return false;
   }	

   else if(trim(cgender.value) == "")
   {
	  cgender.style.backgroundColor = '#F8C5C5';
	  alert('Please select your gender');
	  cgender.focus();
	  return false;
   }
   else if(trim(cmarital_status.value) == "")
   {
	  cmarital_status.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your marital status');
	  cmarital_status.focus();
	  return false;
   }
   else if(trim(cdependants.value) == "")
   {
	  cdependants.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your number of dependants');
	  cdependants.focus();
	  return false;
   }

   else if(trim(caddress_houseno.value) == "")
   {
	  caddress_houseno.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your house number');
	  caddress_houseno.focus();
	  return false;
   }
   else if(trim(caddress_line1.value) == "")
   {
	  caddress_line1.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your street name');
	  caddress_line1.focus();
	  return false;
   }
   else if(trim(caddress_city.value) == "")
   {
	  caddress_city.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your city');
	  caddress_city.focus();
	  return false;
   }
   else if(trim(caddress_postcode.value) == "")
   {
	  caddress_postcode.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your postcode');
	  caddress_postcode.focus();
	  return false;
   }
   else if(trim(caddress_years.value) == "")
   {
	  caddress_years.style.backgroundColor = '#F8C5C5';
	  alert('Please select your time at current address');
	  caddress_years.focus();
	  return false;
   }
   else if(trim(caddress_months.value) == "")
   {
	  caddress_months.style.backgroundColor = '#F8C5C5';
	  alert('Please enter the time at current address');
	  caddress_months.focus();
	  return false;
   }
   else if(trim(caddress_type.value) == "")
   {
	  caddress_type.style.backgroundColor = '#F8C5C5';
	  alert('Please select your accomodation type');
	  caddress_type.focus();
	  return false;
   }   
   else if(trim(cloan_purpose.value) == "")
   {
	  cloan_purpose.style.backgroundColor = '#F8C5C5';
	  alert('Please select your loan purpose');
	  cloan_purpose.focus();
	  return false;
   }

   else if ((isNaN(money(camount.value)))||(camount.value==""))
   {
	  camount.style.backgroundColor = '#F8C5C5';
	  alert('Please enter a valid loan amount');
	  camount.focus();
	  return false;
   }   
   
   else if(trim(cproperty_type.value) == "")
   {
	  cproperty_type.style.backgroundColor = '#F8C5C5';
	  alert('Please select your property type');
	  cproperty_type.focus();
	  return false;
   }
   else if((isNaN(cpayment.value))||(cpayment.value==""))
   {
	  cpayment.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your monthly mortgage or rent payment');
	  cpayment.focus();
	  return false;
   }
   else if(trim(carrears.value) == "")
   {
	  carrears.style.backgroundColor = '#F8C5C5';
	  alert('Please select if you have any current arrears');
	  carrears.focus();
	  return false;
   }
   else if(trim(cemployment_status.value) == "")
   {
	  cemployment_status.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your current employment status');
	  cemployment_status.focus();
	  return false;
   }
   else if((isNaN(cincome.value))||(cincome.value==""))
   {
	  cincome.style.backgroundColor = '#F8C5C5';
	  alert('Please enter your net monthly income');
	  cincome.focus();
	  return false;
   }   
   
   
   
   else
   {
	  cfirstname.value = trim(cfirstname.value);
	  csurname.value = trim(csurname.value);
	  chometel.value = trim(chometel.value);
	  cemail.value = trim(cemail.value);
	  caddress_houseno.value = trim(caddress_houseno.value);
	  caddress_line1.value = trim(caddress_line1.value);
	  caddress_city.value = trim(caddress_city.value);
	  caddress_postcode.value = trim(caddress_postcode.value);
	  cpayment.value = trim(cpayment.value);
	  cincome.value = trim(cincome.value);
	  return true;
   }
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

function money(str)
{
	str2 = str.replace(/£/, '');
	return str2.replace(/\,/g, '');
}

function isEmail(str)
{
	var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(str);
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

    if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

function openW(mypage,myname,w,h,features) {
 if(screen.width){
 var winl = (screen.width-w)/2;
 var wint = (screen.height-h)/2;
 
}
 else{winl = 0;wint =0;}
 if (winl < 0) winl = 0;
 if (wint < 0) wint = 0;
 var settings = 'height=' + h + ','; settings += 'width=' + w + ','; settings += 'top=' + wint + ','; settings += 'left=' + winl + ','; settings += features; win = window.open(mypage,myname,settings); win.window.focus();
}

