//Declare all the variables that appear on the sliders, the start and end values of each one, and the 

//list values



//Functions to toggle field contents on and off

function clearfield(fieldname,numeric) {



var field = document.getElementById(fieldname)

if (numeric != 'false'){

    if (isNaN(field.value) == true){

    field.value = ''

  }

}

else

{

	if (field.value == 'First name' || field.value == 'Surname' || field.value == 'Mobile' || field.value == 'Telephone' || field.value == 'Email (optional)' || field.value == 'Telephone 1' || field.value == 'Telephone 2')

    {

    field.value = ''

    }

}

}



function populate(original,fieldname,numeric) {

 

var field = document.getElementById(fieldname)



if (numeric != 'false'){

    if (field.value == '' || isNaN(field.value) == true){

    field.value = original

  }

}

else

{

    if (field.value == ''){

    field.value = original

}

}  

  

}



//Global

var start = 0

var end = 450

var sliderwidth = 106



//Loan Amount

var maxAmount = 50

var minAmount = 1

var minAffAmount = 5



// Initial values on page load

var Amount = 2





// set default values 

function CalculatorInit()

{



var AmountRange = maxAmount - minAmount

document.getElementById("ShowAmount").innerHTML = minAmount	

document.getElementById("ShowAmountAff").innerHTML = minAffAmount	

var AmountFactor = (end) /  AmountRange



var startAmount = Amount * AmountFactor

document.getElementById("Amount").style.left = 0 + "px"

document.getElementById("AmountAff").style.left = 0 + "px"



}



		

var DragHandler = {



    // private property.

    _oElem : null,





    // public method. Attach drag handler to an element.

    attach : function(oElem) {

        oElem.onmousedown = DragHandler._dragBegin;



        // callbacks

        oElem.dragBegin = new Function();

        oElem.drag = new Function();

        oElem.dragEnd = new Function();



        return oElem;

    },





    // private method. Begin drag process.

    _dragBegin : function(e) {

        var oElem = DragHandler._oElem = this;



        if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }

        if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0'; }



        var x = parseInt(oElem.style.left);

        var y = parseInt(oElem.style.top);



        e = e ? e : window.event;

        oElem.mouseX = e.clientX;

        oElem.mouseY = e.clientY;



        oElem.dragBegin(oElem, x, y);



        document.onmousemove = DragHandler._drag;

        document.onmouseup = DragHandler._dragEnd;

        return false;

    },





    // private method. Drag (move) element.

    _drag : function(e) {

	





        var oElem = DragHandler._oElem;



        var x = parseInt(oElem.style.left);

        var y = parseInt(oElem.style.top);



        e = e ? e : window.event;

		var position = x + (e.clientX - oElem.mouseX)

		

		if (position >= start && position <= end)

		{

        oElem.style.left = position + 'px';

		}

		

		if (oElem.id == "Amount"){

		

			var rangeAmount = maxAmount - minAmount

			var factor = rangeAmount/(end-start)

			var AmountReal = (parseInt(oElem.style.left)-start) * factor

			AmountReal = AmountReal

			AmountDisplay = Math.round(AmountReal) + minAmount

			document.getElementById("ShowAmount").innerHTML = AmountDisplay		

			document.getElementById("txtLoanAmount").value = AmountDisplay*1000	

		}

		if (oElem.id == "AmountAff"){

		

			var rangeAmount = 75 - 5

			var factor = rangeAmount/(end-start)

			var AmountReal = (parseInt(oElem.style.left)-start) * factor

			AmountReal = AmountReal

			AmountDisplay = Math.round(AmountReal) + 5

			document.getElementById("ShowAmountAff").innerHTML = AmountDisplay		

			document.getElementById("txtAffordableAmount").value = AmountDisplay*10	

		}



        oElem.mouseX = e.clientX;

        oElem.mouseY = e.clientY;

        oElem.drag(oElem, x, y);



        return false;

    },





    // private method. Stop drag process.

    _dragEnd : function() {

        var oElem = DragHandler._oElem;



        var x = parseInt(oElem.style.left);

        var y = parseInt(oElem.style.top);



        oElem.dragEnd(oElem, x, y);



        document.onmousemove = null;

        document.onmouseup = null;

        DragHandler._oElem = null;

    }



}





function debttest() {

	

	var txtLoanAmount = document.getElementById("txtLoanAmount");

	var txtLocation = document.getElementById("txtLocation");

	var txtEmployment = document.getElementById("txtEmployment");

	var txtForenames = document.getElementById("txtForenames");

	var txtSurname = document.getElementById("txtSurname");

	var txtMobiletelno = document.getElementById("txtMobiletelno");

	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");

	var txtEmail = document.getElementById("txtEmail");

	

	if(chkPrivacyPolicy.checked!= true || txtLoanAmount.value == "0" || txtLocation.value== "0" || txtEmployment.value == "0" || !vrule_name(txtForenames.value) || !vrule_name(txtSurname.value) || !vrule_telephone(txtMobiletelno.value) || !vrule_email(txtEmail.value)){		

		var msg = "Please make sure that you have completed the following:\n\n";

		msg += (!vrule_debttest(txtLoanAmount.value))?" - Your total debt\n":"";

		msg += (!vrule_debttest(txtLocation.value))?" - Your location\n":"";

		msg += (!vrule_debttest(txtEmployment.value))?" - Employment status\n":"";

		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";

		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";

		msg += (!vrule_telephone(txtMobiletelno.value))?" - Telephone or Mobile number\n":"";

		msg += (!vrule_email(txtEmail.value))?" - A valid email address\n":"";

		msg += (!chkPrivacyPolicy.checked==true)?" - Agree to the privacy policy\n":"";



		msg += "\nPlease supply these missing details and then try again.";

		alert(msg);	

		return false;

	}

	else

	{

	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );

	return true

	}

	}

	

function contactus() {

	

	var txtForenames = document.getElementById("txtForenames");

	var txtSurname = document.getElementById("txtSurname");

	var txtMobiletelno = document.getElementById("txtMobiletelno");

	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");

	

	if(chkPrivacyPolicy.checked!= true || !vrule_name(txtForenames.value) || !vrule_name(txtSurname.value) || !vrule_telephone(txtMobiletelno.value)){		

		var msg = "Please make sure that you have completed the following:\n\n";

		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";

		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";

		msg += (!vrule_telephone(txtMobiletelno.value))?" - Telephone or Mobile number\n":"";

		msg += (!chkPrivacyPolicy.checked==true)?" - Agree to the privacy policy\n":"";



		msg += "\nPlease supply these missing details and then try again.";

		alert(msg);	

		return false;

	}

	else

	{

	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );

	return true

	}

	}



function vrule_name( value ) {

	

	if (value.length == 0 || value == "First name" || value == "Surname") return false; // name is required

	

	telNoPattern = /^[a-zA-Z\-\' ]*$/;

	

	return telNoPattern.test(value);

}



function vrule_text( value ) {

	

	if (value.length == 0){return false; }

	else

	{return true}

}



function vrule_telephone( value ) {

	

	if (value == "Telephone"){

		return false

		}

	else

	{

	value = vtidy_telephone( value );

	

	telNoPattern = /^\d{11}$/;

	return telNoPattern.test(value);

	}

}



function vtidy_telephone( value ) {

	// strip out any spaces and other junk

	return value.replace( /[^0-9]/g, "");

}



function vrule_email( value ) {

	

	if (value.length == 0 || value=="Email (optional)") return true; // email address is optional



	telNoPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

	

	return telNoPattern.test(value);

}



function vrule_debttest( value ) {

	

	if (value == "0")

	{ return false;}

	else

	{return value;}

}
