var fixed_amount = 0;

function setFixedAmount(amount)
{
  fixed_amount = amount;
}

function createHelpWindow(cursor) 
{
  if(!cursor) var cursor = window.event;

  var helpBlock = document.getElementById("help_block");

  var x = 0;
  var y = 0;
  if (cursor.pageX || cursor.pageY) 
  {
    x = cursor.pageX;
    y = cursor.pageY;
  }
  else if (cursor.clientX || cursor.clientY) 
  {
    x = cursor.clientX + document.body.scrollLeft;
    y = cursor.clientY + document.body.scrollTop;
  }
  x += 15;
  y += 15;
  if (x > 700) x = x - 315;

  helpBlock.style.left = x + "px";
  helpBlock.style.top = y + "px";
  helpBlock.style.display="block";

}


function getPopupText(id, popup_text)
{
  var helpBlock = document.getElementById("help_block");
  helpBlock.innerHTML = popup_text;

  var helpElement = document.getElementById(id);
  helpElement.onmousemove = createHelpWindow;
}

function closeHelpWindow(close_id) 
{
  var helpBlock = document.getElementById("help_block");
  helpBlock.style.display = 'none';
}
//================================================================================================
function budgetPlannerOpen(url)
{
// alert("test");
  window.open(url);

}

function budgetPlannerClose()
{
  var total_income = document.getElementById("total_income");
  var total_expense = document.getElementById("total_expense");

  window.opener.document.getElementById("household_income").value = total_income.innerHTML;
  window.opener.document.getElementById("household_expenses").value = total_expense.innerHTML;

  window.close();
}

//================================================================================================
function displayRepaymentType()
{
  //var repayment_type = document.getElementById("repayment_type");
  //var line_type = document.getElementById("line_type");

  var fixed_rate = document.getElementById("fixed_rate");
  var split_question = document.getElementById("split_question");

  //if (line_type.checked) repayment_type.style.display = "none";
  //else repayment_type.style.display = "block";

  if (fixed_rate.checked) split_question.style.display = "block";
  else split_question.style.display = "none";
}
//================================================================================================
function displayRefDetails()
{

	var int_only = document.getElementById("int_only");
  var refinance_details = document.getElementById("refinance_details");
	  //alert(refinance_details.style.display)
  if (int_only.checked && refinance_details)
  	refinance_details.style.display = "none";
	else  	
		refinance_details.style.display = "block";
}

//================================================================================================
function displayRateSplit()
{
  var split_yes = document.getElementById("split_yes");
  var sl = document.getElementById("sl");

  if (split_yes.checked)
  {
    sl.style.display = "block";
  }
  else sl.style.display = "none";
}
//================================================================================================

function overFeuture(elem)
{
  elem.style.cursor="pointer";
  elem.style.backgroundColor="#cdcad9";
}

function outFeuture(elem)
{
  elem.style.cursor="default";
  if (elem.value != 1) elem.style.backgroundColor="white";
}

function clickFeuture(elem)
{
//   elem.style.backgroundColor = "red";//"#cdcad9";
  if (elem.value == 1)
  {
    elem.style.backgroundColor="white";
    elem.value = 0;
  }
  else 
  {
    elem.style.backgroundColor="#cdcad9";
    elem.value = 1;
  }
}

var feature_count = 0;
function addFeature()
{
//   var container = document.getElementById("important_container");
//           var container = document.createTextNode(document.getElementById("important_container"));alert(container);
  for (i=1;i<=9;i++)
  {
    var elem = document.getElementById("potential_" + i);
    if (elem.value == 1)
    {
      feature_count++;
      elem.style.display = "none";
      elem.value = 0;
      var new_elem = document.getElementById("important_" + feature_count);
      if (new_elem.style.display == "none") new_elem.style.display = "block";
      new_elem.style.backgroundColor="white";
      new_elem.value = 0;

//       new_elem.innerHTML = document.getElementById("p_" + i).innerHTML;
      document.getElementById("number_" + feature_count).innerHTML = feature_count + ". ";
      document.getElementById("number_" + feature_count).value = i;      
      document.getElementById("i_" + feature_count).innerHTML = document.getElementById("p_" + i).innerHTML;

      var form_elem = document.getElementById("f_" + i);
      form_elem.value = i;
      
      document.getElementById("fl_" + i).value = feature_count;
    }
  }

  var potential_none = document.getElementById("potential_none");
//   alert("potential_5 display= " +  document.getElementById("potential_5").style.display);
  var important_none = document.getElementById("important_none");
  var potential_count = getFeaturesCount("potential_");
  var important_count = getFeaturesCount("important_");

  if (potential_count > 0) potential_none.style.display = "none";
  else potential_none.style.display = "block";
  if (important_count > 0) important_none.style.display = "none";
  else important_none.style.display = "block";

}

function removeFeature()
{
  for (i=1;i<=9;i++)
  {
    var elem = document.getElementById("important_" + i);
    if (elem.value == 1)
    {
      feature_count--;
//       alert("Element #" + i);

      elem.style.display = "none";
      elem.value = 0;
      var new_elem = document.getElementById("potential_" + document.getElementById("number_" + i).value);
      if (new_elem.style.display == "none") new_elem.style.display = "block";
      new_elem.style.backgroundColor="white";
      new_elem.value = 0;

      var form_elem = document.getElementById("f_" + document.getElementById("number_" + i).value);
      form_elem.value = "";

      document.getElementById("i_" + i).innerHTML = "";
      document.getElementById("number_" + i).innerHTML = "";
      document.getElementById("number_" + i).value = 0;



//       feature_count--;
// //       alert("Element #" + i);
//       elem.style.display = "none";
//       elem.value = 0;
//       var new_elem = document.getElementById("potential_" + i);
//       if (new_elem.style.display == "none") new_elem.style.display = "block";
//       new_elem.style.backgroundColor="white";
//       new_elem.value = 0;
// 
//       var form_elem = document.getElementById("f_" + i);
//       form_elem.value = "";
    }
  }

  var last_number = feature_count;
      for (j=1;j<=8;j++)
      {
        current_elem = document.getElementById("i_" + j);
        current_number = document.getElementById("number_" + j);
// alert("next - "+next_elem.innerHTML);
        for (k=j;k<=8;k++)
        {
          next_n = k + 1;
          next_number = document.getElementById("number_" + next_n);
          next_elem = document.getElementById("i_" + next_n);
          if ((current_elem.innerHTML == "") && (next_elem.innerHTML != ""))
          {
            current_elem.innerHTML = next_elem.innerHTML;
            next_elem.innerHTML = "";
            current_number.innerHTML = j + ". ";
            next_number.innerHTML = "";
            current_number.value = next_number.value;
            next_number.value = 0;
            document.getElementById("important_" + j).style.display = "block";
            document.getElementById("important_" + j).style.backgroundColor="white";
            document.getElementById("important_" + next_n).style.display = "none";
          }
        }
      }

  var potential_none = document.getElementById("potential_none");
  var important_none = document.getElementById("important_none");
  var potential_count = getFeaturesCount("potential_");
  var important_count = getFeaturesCount("important_");

  if (potential_count > 0) potential_none.style.display = "none";
  else potential_none.style.display = "block";
  if (important_count > 0) important_none.style.display = "none";
  else important_none.style.display = "block";
}

function getFeaturesCount(f_name)
{
  var f_count = 0;
  for (i=1;i<=9;i++)
  {
    var elem = document.getElementById(f_name + i);
    if (elem.style.display == "block") f_count++;
  }

  return f_count;
}

function overMenu(elem1, id)
{
  elem1.style.backgroundColor='#b4124c';

  var elem2 = document.getElementById(id);
// alert("id=" + id);
  elem2.style.color='#ffffff';
//   elem2.style.fontSize="14px";
}

function outMenu(elem1, id)
{
  elem1.style.backgroundColor='#ffffff';
  var elem2 = document.getElementById(id);
  elem2.style.color='#000000';
//   elem2.style.fontSize="10px";
}

//====================================================================================================
function testKey(e)
{
  var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

  if (e.ctrlKey || e.altKey || key < 32 || key == 46)
    return true;

  key = String.fromCharCode(key);
  return /[\d]/.test(key);
}

//====================================================================================================
function getLoanTerm(repayment, principal, rate)
{
  rate = rate/100;
  t = (12*repayment)- principal*rate;
	
	if (t > 0)
  {
    a = Math.log((12*repayment)/t);    
    b = 12*Math.log(1+rate/12);
    
    if (!a) return 0;
    else if (!b) return 0;
    
    return a/b;
  }
  else return 0;
}

//====================================================================================================

function verifyForm1()
{

  if (document.step1.state.value == "")
  {
    alert("Please select State.");
    return false;
  }

  if (document.step1.employment.value == "")
  {
    alert("Please select Employment of primary borrower.");
    return false;
  }

  if (document.step1.employment_years.value == "" && document.step1.employment_months.value == "")
  {
    alert("Please enter Years/Months with current employer.");
    return false;
  }

  var flag = 0;
  for (i=0; i<4; i++)
  {
    if (document.step1.borrower_type[i].checked == true)
      flag = 1;
  }

  if (flag == 0)
  {
    alert("Please select Borrower Type.");
    return false;
  }

  if (document.step1.loan_term.value == "")
  {
    alert("Please enter Loan Term.");
    return false;
  }
}

function verifyForm2(refinancing)
{
  if (document.step2.household_income.value == "")
  {
    alert("Please enter Household Monthly Income.");
    return false;
  }

  if (document.step2.household_expenses.value == "")
  {
    alert("Please enter Household Monthly Expenses.");
    return false;
  }
  
  if (parseFloat(document.step2.household_income.value) <= parseFloat(document.step2.household_expenses.value))
  {
    alert("Your monthly expenses currently exceed your income, indicating that you would not qualify for a home loan.");
    return false;
  }
/*  
  if (!refinancing && document.step2.deposit.value == "")
  {
    alert("Please enter Deposit Available.");
    return false;
  }
*/



  flag = 0;
  for (i=0; i<2; i++)
  {
    if (document.step2.repayment_type[i].checked == true)
      flag = 1;
  }
  if (flag == 0)
  {
    alert("Please select Preferred Repayment Type.");
    return false;
  }


	
  if (!refinancing)
  {
    flag = 0;
    for (i=0; i<2; i++)
    {
      if (document.step2.desired[i].checked == true)
        flag = 1;
    }
    if (flag == 0)
    {
      alert("Please select Desired Property.");
      return false;
    }
  }
  else 
  {  	
    if (document.step2.interest_rate.value == "")
    {
      alert("Please enter your current interest rate.");
      return false;
    }

    if (document.step2.current_loan.value == "")
    {
      alert("Please enter Current Loan Balance.");
      return false;
    }

    if (document.getElementById("int_only").checked == false && document.step2.current_repayments.value == "")
    {
      alert("Please enter Current Repayments.");
      return false;
    }
  }

  if (document.step2.lower_range.value == "")
  {
    alert("Please enter Lower Price Range.");
    return false;
  }

  if (document.step2.upper_range.value == "")
  {
    alert("Please enter Upper Price Range.");
    return false;
  }

  if (refinancing)
  {
  	if (document.getElementById("int_only").checked == false)
  	{
	  	var amount = 0;
	  	if (document.step2.extra_funds.value)
	  		amount = parseFloat(document.step2.current_loan.value) +  parseFloat(document.step2.extra_funds.value);
	  	else amount = parseFloat(document.step2.current_loan.value);
	
		  if (amount >= parseFloat(document.step2.upper_range.value))
		  {
		    alert("The loan amount required exceeds the value of your security.");
		    return false;
		  }
		}
	}
	else
	{
		var amount = 0;
  	if (document.step2.deposit.value)
  		amount = parseFloat(document.step2.upper_range.value) -  parseFloat(document.step2.deposit.value);
  	else amount = parseFloat(document.step2.upper_range.value);

	  if (amount <= 0)
	  {
	    alert("The deposit available exceeds the value of the property, indicating that no loan is required. Please revise the deposit amount if you would like to mortgage the property.");
	    return false;
	  }
	}

	if (refinancing && document.getElementById("int_only").checked == false)
  {
  	var principal = parseFloat(document.step2.current_loan.value);
  	var repayment = parseFloat(document.step2.current_repayments.value);
  	var current_rate = parseFloat(document.step2.interest_rate.value);

		//getLoanTerm_1
		/*
		if (!getLoanTerm(repayment, principal, 8.53))
		{
			alert("The specified monthly repayment is insufficient to pay down your current loan balance");
			return false;
		}
		*/
		
		//getLoanTerm_1

		if (!getLoanTerm(repayment, principal, current_rate))
		{
			alert("The specified monthly repayment is insufficient to pay down your current loan balance");
			return false;
		}
				    
	}
	
	
}

function verifyForm3()
{
  flag = 0;
  for (i=0; i<3; i++)
  {
    if (document.step3.credit_situation[i].checked == true)
      flag = 1;
  }
  if (flag == 0)
  {
    alert("Please select Credit History.");
    return false;
  }

  if (document.step3.loan_type)
  {
    flag = 0;
    for (i=0; i<4; i++)
    {
      if (document.step3.loan_type[i].checked == true)
        flag = 1;
    }
    if (flag == 0)
    {
      alert("Please select Preferred Loan Type.");
      return false;
    }
  }
}

function verifyForm4()
{

  if (document.step4.name.value == "")
  {
    alert("Please enter Name.");
    return false;
  }

  if (document.step4.email.value != "")
  {
    var reg = "^[a-zA-Z0-9\-\._]+@[-a-zA-Z0-9\-]+\.[-a-zA-Z0-9\-\.]{2,5}(\.[-a-zA-Z0-9\-\.]{2,5})?$";
    var email = document.step4.email.value;
    email = email.replace(/^\s+/, '').replace(/\s+$/, '');
    if (!email.match(reg))
    {
      alert("Email is invalid.");
      return false;
    }
  }
}