function popup(text)
{
  alert(text);
}

function popupwin(id, x, y)
{
  //alert(text);
  win = window.open('mortgages.info?id='+id, 'infowin', 'toolbar=0,location=0,directories=0,status=0,menubar=0,width='+x+',height='+y)
  win.focus()
}

var day;
var month;
var year;
var dateField;

function set( day, month, year )
{
  if( dateField != null )
  {
    dateField.value = buffer( day+"", 2, '0' ) + '/' + buffer( month+"", 2, '0' ) + '/' + year+"";
  }
}

function buffer( val, len, withChar )
{
  for( i = val.length ; i < len && val.length < len ; i++ )
  {
    val = withChar + val;
  }
  return val;
}

function openCalendar( formName, fieldName )
{
  // This is going to be the wrong way round for what we need.
  this.dateField = document.forms[formName].elements[fieldName];
  var val = dateField.value;
  if( val.charAt( 5 ) != '/' )
  {
    // then it's a readable way round
    var yearStr  = val.substring( 4, 7 );
    var monthStr = val.substring( 2, 3 );
    var dayStr   = val.substring( 0, 1 );
  }
  else
  {
    // else it's in reverse
    var yearStr  = val.substring( 0, 3 );
    var monthStr = val.substring( 4, 5 );
    var dayStr   = val.substring( 6, 7 );
  }
  var date = escape( yearStr + "-" + monthStr + "-" + dayStr );
  var url = 'calendar?startDate=' + date + "&selectedDate=" + date;
  myRef = window.open( url, 'calendar', 'toolbar=0,location=0,directories=0,status=0,menubar=0,width=200,height=230');
  myRef.focus();
  return false;
}

function popupMessage(text)
{
  alert(text);
}

function popupwin(id, x, y)
{
  //alert(text);
  win = window.open('mortgages.info?id='+id, 'infowin', 'toolbar=0,location=0,directories=0,status=0,menubar=0,width='+x+',height='+y)
  win.focus()
}

function popupWithSize(url, id, x, y)
{
  win = window.open( url, id, 'toolbar=0,location=0,directories=0,status=0,menubar=0,width='+x+',height='+y)
  win.focus()
}


function getRadioValue( radiofield )
{
    for (var i=0; i < radiofield.length; i++)
    {
        if( radiofield[i].checked )
        {
            return radiofield[i].value;
        }
    }
}


// PROPERTY DETAILS

function updateLeaseholdYearsRemaining()
{

    freeholdValue = getRadioValue( document.mainform.freehold )

    if( freeholdValue == "Yes" )
    {
        document.getElementById( "leaseholdYearsImage" ).src = "/images/mortgage_finder/propdetails_04-disabled.jpg";
        document.getElementById( "leaseholdYearsRemaining" ).disabled = true;
        document.getElementById( "leaseholdYearsRemainingText" ).disabled = true;
    }
    else
    {
        document.getElementById( "leaseholdYearsImage" ).src = "/images/mortgage_finder/propdetails_04.jpg";
        document.getElementById( "leaseholdYearsRemaining" ).disabled = false;
        document.getElementById( "leaseholdYearsRemainingText" ).disabled = false;
    }
}

function updateBuyToLetExtraQuestions(buyToLet)
{
    buyToLetValue = getRadioValue( document.mainform.stage )
    if( buyToLetValue == buyToLet )
    {
        document.getElementById( "btlExtraQuestions" ).style.display="";

    }
    else
    {
        document.getElementById( "btlExtraQuestions" ).style.display="none";
    }
}

function showBuyToLetRelativeQuestions()
{
    document.getElementById( "btlRelativeQuestion" ).style.display="";
}

function getDropdownValue( dropdown )
{
    for (var i=0; i < dropdown.options.length; i++)
    {
        if( dropdown.options[i].selected )
        {
            return dropdown.options[i].value;
        }
    }
}



function setDropdownValue( dropdown, value )
{
    for (var i=0; i < dropdown.options.length; i++)
    {
        if( dropdown.options[i].value == value )
        {
            dropdown.options[i].selected = true;
            return;
        }
    }
}

// PERSONAL CRITERIA PAGE

function updateRepaymentOptionsField()
{
    if( document.mainform.organisingSavings.checked )
    {
        repaymentTypes = document.mainform.repaymentType;
        setDropdownValue( repaymentTypes, "Interest Only" );
        repaymentTypes.disabled = true;
        document.getElementById( "repaymentTypeText" ).disabled = true;
        document.mainform.repaymentTypeImage.src = "/images/mortgage_finder/personalcriteria_06-disabled.jpg";
        document.getElementById( "CapitalDiv1" ).style.visibility = "hidden";
        document.getElementById( "CapitalDiv2" ).style.visibility = "hidden";
    }
    else
    {
        repaymentTypes = document.mainform.repaymentType;
        repaymentTypes.disabled = false;
        document.getElementById( "repaymentTypeText" ).disabled = false;
        document.getElementById( "repaymentTypeImage" ).src = "/images/mortgage_finder/personalcriteria_06.jpg";
    }
}

function updateCapitalRepaymentField()
{
    repaymentTypes = document.mainform.repaymentType;

    if( getDropdownValue( repaymentTypes ) == "Part and part" )
    {
        document.getElementById( "CapitalDiv1" ).style.visibility = "";
        document.getElementById( "CapitalDiv2" ).style.visibility = "";
    }
    else
    {
        document.getElementById( "CapitalDiv1" ).style.visibility = "hidden";
        document.getElementById( "CapitalDiv2" ).style.visibility = "hidden";
    }
}
