//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 == 'Telephone' || 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 = 296
var sliderwidth = 106

//Loan Amount
var maxAmount = 10
var minAmount = 1


// Initial values on page load
var Amount = 1


// set default values 
function CalculatorInit()
{

var AmountRange = maxAmount - minAmount
document.getElementById("ShowAmount").innerHTML = minAmount	
var AmountFactor = (end) /  AmountRange

var startAmount = Amount * AmountFactor
document.getElementById("Amount").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
		}
		

        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 txtEmployment = document.getElementById("txtEmployment");
	var txtResidentType = document.getElementById("txtResidentType");
	var txtForenames = document.getElementById("txtForenames");
	var txtSurname = document.getElementById("txtSurname");
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var txtLocation = document.getElementById("txtLocation");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");
	
	if(chkPrivacyPolicy.checked!= true || txtLoanAmount.value == "0" || txtLocation.value== "0" || txtResidentType.value== "0" || txtEmployment.value == "0" || !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_debttest(txtLoanAmount.value))?" - Your total debt\n":"";
		msg += (!vrule_debttest(txtEmployment.value))?" - Employment status\n":"";
		msg += (!vrule_debttest(txtResidentType.value))?" - Residential status\n":"";
		msg += (!vrule_debttest(txtLocation.value))?" - Your location\n":"";
		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";
		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";
		msg += (!vrule_telephone(txtMobiletelno.value))?" - A valid telephone 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 contactus() {
	
	var txtForenames = document.getElementById("txtForenames");
	var txtSurname = document.getElementById("txtSurname");
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var txtHometelno = document.getElementById("txtHometelno");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");
	
	if(chkPrivacyPolicy.checked!= true || !vrule_name(txtForenames.value) || !vrule_name(txtSurname.value) || (!vrule_telephone(txtHometelno.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) && !vrule_telephone(txtHometelno.value))?" - A valid telephone 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
	{
	if (txtMobiletelno.value == "Telephone 2"){
		document.getElementById("txtMobiletelno").value = ""
		}
	if (txtHometelno.value == "Telephone 1"){
		document.getElementById("txtHometelno").value = ""
		}
	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );
	document.getElementById("txtHometelno").value = vtidy_telephone(txtHometelno.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 ) {
	
	return value.replace( /[^0-9]/g, "");
}

function vrule_debttest( value ) {
	
	if (value == "0")
	{ return false;}
	else
	{return value;}
}
