// --------------------------------------------------------------------------
// GoTo page from select
// --------------------------------------------------------------------------
function gotoPageFromSelect(selectObject){
  var theURL = selectObject.options[selectObject.selectedIndex].value;
  if (theURL){
    document.location.href=theURL
  }
}


// --------------------------------------------------------------------------
// load calculations
// --------------------------------------------------------------------------


function strRound( number, places ) {
    if ( number < 1 ) {
        result = "0" + Math.round( number * Math.pow(10, places) );
    }
    else {
        result = "" + Math.round( number * Math.pow(10, places) );
    }

    var dp = result.length - places;
    result = result.substring(0, dp) + "." + 
             result.substring(dp, result.length);
        
    if ( number < 0 ) {
        result = "-" + result;
    }

    return result;
}


function calc_loan() {
  var borrowed = document.forms[0].borrowed.value
  var rate = document.forms[0].rate.value
  var years = document.forms[0].years.value
  var total = borrowed * (1 + rate/100)^years;
  document.forms[0].total.value = total; 

}
  
  


function loanRepay() {
    if (document.forms.loan_calc.borrowed.value =="" || document.forms.loan_calc.rate.value == ""){
      alert('Please enter a loan amount and interest rate.')
    }else{
    var principal = parseFloat(Number(document.forms.loan_calc.borrowed.value.replace(/(,|\.)/g, "")));
    
    var apr = parseFloat(document.forms.loan_calc.rate.value) / 100;
    
    var maturity = parseFloat(document.forms.loan_calc.years.value*12);
     
    var monthlyrate = Math.pow(apr + 1, 1/12) - 1;
    var monthlypayment = ( principal * monthlyrate ) / ( 1 - Math.pow(1 + monthlyrate, -maturity) );
    
      if (document.getElementById){
        document.getElementById('monthlyValue').innerHTML = '&pound;' + commaFormat(strRound(monthlypayment, 2), ",");
        document.getElementById('totalValue').innerHTML = '&pound;' + commaFormat(strRound(monthlypayment * maturity, 2), ",");
      }
    }
}



function commaFormat(num , sep) 
{ 
	var n = '' + num;
	var numl  = n.length;
	var i = n.indexOf('.');
	var fract = '';

	if (i > -1){
		numl -= n.length-i;
		fract = n.substring(i);
	}
	if (numl > 3){
		var s, dl = numl%3;
		var str = (dl > 0) ? n.substring(0,dl):'';
		var max = Math.floor(numl/3);
		for (i=0 ; i < max ; i++){
			s = n.substring(dl+i*3,dl+i*3+3);
			str += (dl==0 && i==0) ? s : sep+s;
 		}
		return str+fract;
	}else return n;
}







function calcValue() {
    if (document.forms.value_calc.borrowed.value == "" || document.forms.value_calc.loftcost.value == ""){
      alert('Please enter a property and conversion value.')
    }else{

    var principal = parseFloat(Number(document.forms.value_calc.borrowed.value.replace(/(,|\.)/g, "")));
    var loftCost = parseFloat(Number(document.forms.value_calc.loftcost.value.replace(/(,|\.)/g, "")));
    var apr = parseFloat(4) / 100;
    var maturity = parseFloat(document.forms.value_calc.years.value);
    var withLoftValue = Math.pow(apr + 1, maturity)*(principal+loftCost);
    var withoutLoftValue = Math.pow(apr + 1, maturity)*principal;
    
    if (document.getElementById){
      document.getElementById('withoutConversion').innerHTML = '&pound;' +  commaFormat(strRound(withoutLoftValue, 2), ",");
      document.getElementById('withConversion').innerHTML = '&pound;' + commaFormat(strRound(withLoftValue, 2), ",");
    }
    
    }
    
}




// --------------------------------------------------------------------------
// image controls
// --------------------------------------------------------------------------


function changePhoto(direction){
  if (!currentIndex) currentIndex = 0
  if (imageArray && descriptionArray && imageArray.length == descriptionArray.length){
    currentIndex = currentIndex + direction
    if (currentIndex > imageArray.length-1) currentIndex = 0
    if (currentIndex < 0) currentIndex = imageArray.length-1
    if (document.getElementById){
      document.getElementById('photo_description').innerHTML = ((currentIndex+1) + '/' + descriptionArray.length + ' ' + descriptionArray[currentIndex])
      document.getElementById('theImage').src = path + imageArray[currentIndex] + '.jpg';
    }
  }
  
}



//--------------------------------------------------------------------------
//  STYLE SHEET SWAPPER
//--------------------------------------------------------------------------

function styleSwap(object, state){
  if (state == 'on'){
    object.className ='inputBodyFocus';
  }else{
    object.className ='inputBody';
  }
}




  
//--------------------------------------------------------------------------
//  QUOTE WRITER
//--------------------------------------------------------------------------

function writeQuote(){
  var textArray = new Array();
  textArray[0] = "&quot;<i>I would like to convey my thanks to your company for a job well done and for your professionalism at all times</i>&quot;<br>Mr Blackman, Essex ";
  textArray[1] = "&quot;<i>We have been very pleased with the skill and workmanship of the joiners and plumbing. We will have no hesitation in recommending your company to both neighbours and friends. Thank you</i>&quot;<br>Mr C R Earl, Surrey"; 
  textArray[2] = "&quot;<i>Thank you to you and all your team in doing a great job on my house. I would have no problem recommending your company to others.</i>&quot;<br>Mr Flately, Lancashire ";
  textArray[3] = "&quot;<i>You have done exactly what you have said you would do, when you said you would do it.</i>&quot;<br>Mr Flately, Lancashire ";
  textArray[4] = "&quot;<i>If there was a problem or design issues to overcome you have discussed it with me and agreed a way forward.</i>&quot;<br>Mr Flately, Lancashire ";
  textArray[5] = "&quot;<i>The men on the job have been there constantly and have worked hard. No skipping off to finish other jobs elsewhere.</i>&quot;<br>Mr Flately, Lancashire ";
  textArray[6] = "&quot;<i>The standard of workmanship is very high.</i>&quot;<br>Mr Flately, Lancashire ";
  textArray[7] = "&quot;<i>We are delighted with the finished result. It has changed family life completely</i>&quot;<br>Mrs Williams, Croydon ";
  textArray[8] = "&quot;<i>The project went smoothly from start to finish and the design and standard of workmanship cannot be faulted.</i>&quot;<br>Mr Richardson, Herts ";
  textArray[9] = "&quot;<i>Your staff and contract labour were a pleasure to deal with and I would like you to pass on our thanks to all involved with our project.</i>&quot;<br>Mr Richardson, Herts ";
  itemIndex = Math.floor(Math.random()*textArray.length);
  var HTMLToWrite = textArray[itemIndex];
  document.write(HTMLToWrite);
}

  
function showOther(object, value) {
    var required_value = value || 'Other';

    if (object.value == required_value) {
        document.getElementById(object.name + "_div").style.visibility = 'visible';
    }
    else if (object.value != required_value) {

        document.getElementById(object.name + "_div").style.visibility = 'hidden';

        // if they've changed back from "other" clear the field unless it's still set to "Please indicate"
        if (document.getElementById(object.name + "_other").value != 'Please indicate...') {
            document.getElementById(object.name + "_other").value = 'Please indicate...' 
        }
    }
}

function clearField(object) {

    if (object.value != 'Please indicate...') {
        return false;
    }
    else {
          object.value = '';
    }
}
