//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

// Tooltip show hide
function showdiv(name){

if (document.getElementById(name)){
var obj = document.getElementById(name)
}

if (obj.style.display=="none"){
obj.style.display="";

document.getElementById("faq-" + name).style.backgroundImage = "url(img/faq-minus.png)"

}else{
obj.style.display="none";

document.getElementById("faq-" + name).style.backgroundImage = "url(img/faq-plus.png)"
}
}


// Pop up window
function openpopup(popurl){
var winpops=window.open(popurl,"","width=615,height=485,scrollbars,resizable")
}


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 == 'Question' || field.value == 'Your name' || field.value == 'Telephone number' || field.value == 'Email' || field.value == 'Freeman Jones reference' || field.value == 'Your name' || field.value == 'Phone number' || field.value == 'Phone number 2 (optional)' || field.value == 'Your question')
    {
    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 = 50
var minAmount = 2


// Initial values on page load
var Amount = 2


// 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");document.getElementById('txtLoanAmountError').className="errormessage"
	var txtLocation = document.getElementById("txtLocation");document.getElementById('txtLocationError').className="errormessage"
	var txtEmployment = document.getElementById("txtEmployment");document.getElementById('txtEmploymentError').className="errormessage"
	var txtForenames = document.getElementById("txtForenames");document.getElementById('txtForenamesError').className="errormessage"
	var txtHometelno = document.getElementById("txtHometelno");document.getElementById('txtHometelnoError').className="errormessage"
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");document.getElementById('chkPrivacyPolicyError').className="errormessage"
	
	if(chkPrivacyPolicy.checked!= true || txtLoanAmount.value == "0" || txtLocation.value== "0" || txtEmployment.value == "0" || !vrule_name(txtForenames.value) || (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value))){		
	if (!vrule_debttest(txtLoanAmount.value)){document.getElementById('txtLoanAmountError').className="errorreveal";document.getElementById('txtLoanAmountError').innerHTML="Please tell us how much debt your in"}
	if (!vrule_debttest(txtLocation.value)){document.getElementById('txtLocationError').className="errorreveal";document.getElementById('txtLocationError').innerHTML="Please tell us where you live"}
	if (!vrule_debttest(txtEmployment.value)){document.getElementById('txtEmploymentError').className="errorreveal";document.getElementById('txtEmploymentError').innerHTML="Please select an employment status"}
	if (!vrule_name(txtForenames.value)){document.getElementById('txtForenamesError').className="errorreveal";document.getElementById('txtForenamesError').innerHTML="Please fill in your name"}
    if (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value) ){document.getElementById('txtHometelnoError').className="errorreveal";document.getElementById('txtHometelnoError').innerHTML="Please enter a home or mobile phone number" }
	if (!chkPrivacyPolicy.checked==true){document.getElementById('chkPrivacyPolicyError').className="errorreveal";document.getElementById('chkPrivacyPolicyError').innerHTML="Please confirm you've read our privacy policy"}
		return false;
	}
	else
	{
	
	if (txtHometelno.value == "Phone number")		
	{
		document.getElementById("txtHometelno").value = ""
	}
	
	if (txtMobiletelno.value == "Phone number 2 (optional)")		
	{
		document.getElementById("txtMobiletelno").value = ""
	}

	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );
	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );

	return true
	}
}
function validate() {
	
	var txtForenames = document.getElementById("txtForenames");document.getElementById('txtForenamesError').className="errormessage"
	var txtHometelno = document.getElementById("txtHometelno");document.getElementById('txtHometelnoError').className="errormessage"
	var txtMobiletelno =  document.getElementById("txtMobiletelno")
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");document.getElementById('chkPrivacyPolicyError').className="errormessage"
	
	if(chkPrivacyPolicy.checked!= true || !vrule_name(txtForenames.value) || (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value))){		
		if (!vrule_name(txtForenames.value)){document.getElementById('txtForenamesError').className="errorreveal";document.getElementById('txtForenamesError').innerHTML="Please fill in your name" };
		if (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value)){document.getElementById('txtHometelnoError').className="errorreveal";document.getElementById('txtHometelnoError').innerHTML="Please enter a valid home or mobile phone number" };
		if (!chkPrivacyPolicy.checked==true){document.getElementById('chkPrivacyPolicyError').className="errorreveal";document.getElementById('chkPrivacyPolicyError').innerHTML="Please tick to accept our privacy policy" };
		
		return false;
	}
	else
	{
	
		
	if (txtHometelno.value == "Telephone 1" || txtHometelno.value == "Telephone" || txtHometelno.value == "Telephone 2"){
		document.getElementById("txtHometelno").value = ""
	}
		
	if (txtMobiletelno.value == "Telephone 1" || txtMobiletelno.value == "Telephone" || txtMobiletelno.value == "Telephone 2"){
		document.getElementById("txtMobiletelno").value = ""
	}
		
	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );
	document.getElementById("txtMobiletelno").value = vtidy_telephone(txtMobiletelno.value );
	return true
	}
	}



function vrule_name( value ) {
	
	if (value.length == 0 || value == "First name" || value == "Surname" || value == "Your name") return false; // name is required	
	telNoPattern = /^[a-zA-Z\-\' ]*$/;
	return telNoPattern.test(value);
}

function vrule_reference( value ) {
	
	if (value.length == 0 || value == "Freeman Jones reference" || value.length < 7)	
	{return false}	
	else	
	{return true} 
	
}

function vrule_question( value ) {
	
	if (value.length == 0 || value == "Your question")	
	{return false}	
	else	
	{return true} 
	
}

function vrule_text( value ) {
	
	if (value.length == 0){return false; }
	else
	{return true}
}

function vrule_telephone( value ) {
	
	if (value == "Telephone 1" || value == "Telephone 2"){
		return false
		}
	else
	{
	value = vtidy_telephone( value );	
	telNoPattern = /^\d{11}$/;
	return telNoPattern.test(value);
	}
}

function vrule_telephone_question( value ) {
	
	if (value == "Telephone number" || value.length == 0){
		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_email_question( value ) {

	if (value.length == 0 || value=="Email") return false; // 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;}
}


// Tooltip show hide
function showdiv2(name){

if (document.getElementById(name)){
var obj = document.getElementById(name)
}

if (obj.style.display=="none"){
obj.style.display="block";

}else{
obj.style.display="none";


}
}

function hidediv2(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
obj.style.display="none";
}

