var dataFrameLoaded = false;

var errorMessageArray = new Array();
errorMessageArray['BusinessIndustry'] = "Please indicate the type of industry your business is in.";
errorMessageArray['TimeOfOperation'] = "Please indicate how long your business has been in operation.";	

errorMessageArray['Transactions'] = "Please indicate how many business-banking transactions you do in a month.";
errorMessageArray['NumberOfEmployees'] = "Please indicate how many employees your business has (including yourself).";	


errorMessageArray['AboutRepaymentSchedule'] = "Please select the repayment schedule that best suits you.";

errorMessageArray['OptionsEquity']="Please indicate if you would consider using your home equity to borrow at a lower interest rate.";
errorMessageArray['EmptyHouseValue'] = "Please enter an amount.";
errorMessageArray['InvalidHouseValue'] = "You have entered an invalid currency amount. Please re-enter the amount.";
errorMessageArray['EmptyMortgageBalance'] = "Please enter an amount.";
errorMessageArray['InvalidMortgageBalance'] = "You have entered an invalid currency amount. Please re-enter the amount.";
errorMessageArray['WrongMortgageBalance'] = "Your mortgage balance can not be more than or equal to your home value. Please re-enter the amount.";
errorMessageArray['OptionsFlexibility']="Please indicate if you want the flexibility to borrow again in the future without having to re-apply.";
errorMessageArray['foreignCurrency']="Please indicate if you require U.S. dollars or other foreign currency to run your business.";
errorMessageArray['multipleLocations']="Please indicate if you have multiple business locations.";
errorMessageArray['businessOutsideCanada']="Please indicate if you conduct business outside of Canada.";
errorMessageArray['financingRequired']="Please indicate if you own 20% or more of your business and if so what type of financing your business requires.";
errorMessageArray['revenue']="Please indicate how much revenue your business generated last year.";
errorMessageArray['workingCapital']=" Please indicate if you have sufficient working capital to run your business in case of an emergency.";


function fillCheckValue(formFieldName) {
	eval("parent.frames['formFrame'].document.solutionStepOneForm."+formFieldName+".checked = (parent.frames[\'submitFrame\'].document.dataStore."+formFieldName+".value != 0) ? true : false");
}
function fillRadioValue(formFieldName) {
	var revenue = eval("parent.frames['submitFrame'].document.dataStore."+formFieldName+".value;");
	var formRevenue = eval("parent.frames['formFrame'].document.solutionStepOneForm."+formFieldName+";");
	for (var i = 0; i < formRevenue.length; i++) {
		if (formRevenue[i] && formRevenue[i].value == revenue) {
			formRevenue[i].checked = true;
		}
	}					
}



function startOver(){
  frames['submitFrame'].document.forms[0].requestPopulatePageNumber.value=0;		  
  parent.document.location = "solution-finder.html";
}				


function moveNextPage(){
	var currentStep=parent.frames['formFrame'].document.forms[0].stepNum.value;
	//frames['formFrame'].document.forms[0].stepNum.value=frames['formFrame'].document.forms[0].stepNum.value;
		
	switch (currentStep) {
	
		case '1':
			if(validateStepOne()){ 
				saveStepOne(); 
				parent.frames['formFrame'].location.href="solution-step2.html";
			}
		break;
		case '2':
			if(validateStepTwo()){ 
				saveStepTwo(); 
				parent.frames['formFrame'].location.href="solution-step3.html";
			}
		break;
		case '3':
			if(validateStepThree()) {			
				saveStepThree();
				parent.frames['formFrame'].location.href="solution-step4.html";
			}
		break;

		case '4':
			if(validateStepFour()) {			
				saveStepFour();
				parent.frames['formFrame'].location.href="solution-results.html";
			}
		break;
		
		default:
			//startOver();
		break;
	} // end switch	
}		

function movePrevPage(){
  //parent.frames['formFrame'].history.go(-1);
	parent.frames['submitFrame'].document.dataStore.requestPopulatePageNumber.value	= 1;
	if (parent.frames['formFrame'].location.href.indexOf("solution-results.html") != -1) {
		parent.frames['formFrame'].location.href="solution-step4.html";
	}
	if (parent.frames['formFrame'].location.href.indexOf("solution-step4.html") != -1) {
		parent.frames['formFrame'].location.href="solution-step3.html";
	}
	if (parent.frames['formFrame'].location.href.indexOf("solution-step3.html") != -1) {
		parent.frames['formFrame'].location.href="solution-step2.html";
	}
	if (parent.frames['formFrame'].location.href.indexOf("solution-step2.html") != -1) {
		parent.frames['formFrame'].location.href="solution-step1.html";
	}
}

function validateStepOne() {
	var validationStatus=true;


	if(! radioButtonChecking('businessIndustry')) {
		frames['formFrame'].document.getElementById('errBusinessIndustrySection').style.display = "block";			
		validationStatus=false;
	}
	else {
		frames['formFrame'].document.getElementById('errBusinessIndustrySection').style.display = "none";					  
	}

	if(! radioButtonChecking('timeOfOperation')){
		frames['formFrame'].document.getElementById('errTimeOfOperationSection').style.display = "block";					  	  
		validationStatus=false;
	}
	else {
		frames['formFrame'].document.getElementById('errTimeOfOperationSection').style.display = "none";			  	  	  
	}	 
	return validationStatus;
}

function saveStepOne(){

	radioButtonSaving('businessIndustry');		  
	radioButtonSaving('timeOfOperation');		
	checkBoxSavingGroup('preferredBanking');			   	   
}

function validateStepTwo(){

	var validationStatus=true;
	
	if(! radioButtonChecking('transactions')){
		frames['formFrame'].document.getElementById('errTransactionsSection').style.display = "block";
		validationStatus=false;
	}
	else{
		frames['formFrame'].document.getElementById('errTransactionsSection').style.display = "none";
	}
	
	if(! radioButtonChecking('numberOfEmployees')){
		frames['formFrame'].document.getElementById('errNumberOfEmployeesSection').style.display = "block";
		validationStatus=false;
	}
	else{
		frames['formFrame'].document.getElementById('errNumberOfEmployeesSection').style.display = "none";
	}	 
	if(! radioButtonChecking('foreignCurrency')){
		frames['formFrame'].document.getElementById('errForeignCurrencySection').style.display = "block";
		validationStatus=false;
	}
	else{
		frames['formFrame'].document.getElementById('errForeignCurrencySection').style.display = "none";
	}	 
	
	return validationStatus;	
}

function saveStepTwo(){
	radioButtonSaving('transactions');		  
	radioButtonSaving('numberOfEmployees');		
	checkBoxSavingGroup('howToPay');
	radioButtonSaving('foreignCurrency');
	checkBoxSavingGroup('paySuppliers');
}	

function validateStepThree() {
	var isValid = true;
	
	if (! radioButtonChecking('multipleLocations')) {
		isValid = false;
		frames['formFrame'].document.getElementById('errMultipleLocationsSection').style.display = "block";
	}
	else {
		frames['formFrame'].document.getElementById('errMultipleLocationsSection').style.display = "none";
	}
	
	if (! radioButtonChecking('businessOutsideCanada')) {
		isValid = false;
		frames['formFrame'].document.getElementById('errBusinessOutsideCanadaSection').style.display = "block";
	}
	else {
		frames['formFrame'].document.getElementById('errBusinessOutsideCanadaSection').style.display = "none";
	}
	
	if (! radioButtonChecking('financingRequired')) {	
		isValid = false;
		frames['formFrame'].document.getElementById('errFinancingRequiredSection').style.display = "block";
	}
	else {
		frames['formFrame'].document.getElementById('errFinancingRequiredSection').style.display = "none";
	}
	return isValid;
}

function saveStepThree() {
	checkBoxSavingGroup('payments');
	radioButtonSaving('multipleLocations');
	radioButtonSaving('businessOutsideCanada');
	radioButtonSaving('financingRequired');
}

function validateStepFour() {

	var isValid = true;
	
	if (! radioButtonChecking('revenue')) {
		isValid = false;
		frames['formFrame'].document.getElementById('errRevenueSection').style.display = "block";
	}
	else {
		frames['formFrame'].document.getElementById('errRevenueSection').style.display = "none";
	}

	if (! radioButtonChecking('workingCapital')) {
		isValid = false;
		frames['formFrame'].document.getElementById('errWorkingCapitalSection').style.display = "block";
	}
	else {
		frames['formFrame'].document.getElementById('errWorkingCapitalSection').style.display = "none";
	}
	
	return isValid;

}

function saveStepFour() {
	checkBoxSavingGroup('willYou');
	radioButtonSaving('revenue');
	radioButtonSaving('workingCapital');
}

	
	function checkBoxChecking(elementName)
	{
	  var checkBoxChecked=false;
	  if (frames['formFrame'].document.forms[0].elements[elementName].checked == true)
	  {
	    checkBoxChecked=true;
	  }
	  else
	  {
	    checkBoxChecked=false;
	  }
	  return checkBoxChecked;
	}
			
	function radioButtonChecking(elementName)
	{
	  var radioSelect=false;

	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;

	  
	  for(var i=0; i<totalNumber &&( ! radioSelect); i++)
	  {
	      if(parent.frames['formFrame'].document.forms[0].elements[elementName][i].checked)
		  {
		    radioSelect=true;
		  }
	  }
	  return radioSelect;
	}
		
function dropDownListChecking(elementName)
{
  var index=parent.frames['formFrame'].document.forms[0].elements[elementName].selectedIndex;
  if(index!=0)
  {
    return true;
  }
  else
  {
    return false;
  }
}	
			
   function checkboxSaving(elementName)	
  {			
	if (frames['formFrame'].document.forms[0].elements[elementName].checked == true) {
		frames['submitFrame'].document.forms[0].elements[elementName].value = frames['formFrame'].document.forms[0].elements[elementName].value;
	} else {
	frames['submitFrame'].document.forms[0].elements[elementName].value = 0;
	}
  }
 						
	function radioButtonSaving(elementName)
	{
	  var radioSelect=false;

	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;


	  for(var i=0; i<totalNumber && ( ! radioSelect); i++)
	  {
	      if(frames['formFrame'].document.forms[0].elements[elementName][i].checked)
		  {			    
		     frames['submitFrame'].document.forms[0].elements[elementName].value=frames['formFrame'].document.forms[0].elements[elementName][i].value;
		     radioSelect=true;
		  }		  
	  }		   		  		
	}
	
function dropDownListSaving(elementName)
	{
      var index=parent.frames['formFrame'].document.forms[0].elements[elementName].selectedIndex;
      var dropDownListValue=parent.frames['formFrame'].document.forms[0].elements[elementName][index].value;
        frames['submitFrame'].document.forms[0].elements[elementName].value=dropDownListValue;
	}		
	
function textBoxSaving(elementName)
{
        frames['submitFrame'].document.forms[0].elements[elementName].value=frames['formFrame'].document.forms[0].elements[elementName].value;
}				
	
	
function returnRadioButtonValue(elementName)		
{
	 var radioSelect=false;
	 var selectedRadioButtonValue;

	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;


	  for(i=0; i<totalNumber && ( ! radioSelect); i++)
	  {
	      if(frames['formFrame'].document.forms[0].elements[elementName][i].checked)
		  {			    
		     selectedRadioButtonValue=frames['formFrame'].document.forms[0].elements[elementName][i].value;				 
		     radioSelect=true;
		  }		  
	  }		   		  	

	 return selectedRadioButtonValue; 	
}		

   function defaultTextBox(elementName, defaultValue)
{
  parent.frames['formFrame'].document.forms[0].elements[elementName].value=defaultValue;
}
	
function defaultRadioButton(elementName, defaultValue)
{   
	  var radioSelect=false;
	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;
     
	  for(i=0; i<totalNumber && ( ! radioSelect); i++)
	  {  
	      if(frames['formFrame'].document.forms[0].elements[elementName][i].value==defaultValue)
		  {			    
		    frames['formFrame'].document.forms[0].elements[elementName][i].checked=true;
		     radioSelect=true;
		  }		  
	  }		   		  			   
}

function defaultRadioButtonUpdate(elementName, defaultValue)
{   
	  var radioSelect=false;
	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;
     
	  for(i=0; i<totalNumber; i++)
	  {  
	      if(frames['formFrame'].document.forms[0].elements[elementName][i].value==defaultValue)
		  {			    
		    frames['formFrame'].document.forms[0].elements[elementName][i].checked=true;
		     radioSelect=true;
		  }		  
	  }		   		  			   
}	

  function defaultDropDown(elementName, defaultValue)
  {  
	  var dropDownSelect=false;
	  var totalNumber=parent.frames['formFrame'].document.forms[0].elements[elementName].length;   
	  for(i=0; i<totalNumber && ( ! dropDownSelect); i++)
	  {  		  				  
	      if(frames['formFrame'].document.forms[0].elements[elementName].options[i].value==defaultValue)
		  {			    
		    frames['formFrame'].document.forms[0].elements[elementName].options[i].selected=true;
		    dropDownSelect=true;
		  }		  
	  }				  
  }	
  
// This function is for checkbox with different name
  
  function defaultCheckBox(elementName, defaultValue, benchmarkValue)
  {   
       if(defaultValue==benchmarkValue)
	{
	  frames['formFrame'].document.forms[0].elements[elementName].checked=true;		 
	}		  
  }	   

function returnDropDownListChecking(elementName)
{
  var index=parent.frames['formFrame'].document.forms[0].elements[elementName].selectedIndex;
  var dropDownListValue=parent.frames['formFrame'].document.forms[0].elements[elementName][index].value;
     return dropDownListValue;
}				
	

function checkBoxSavingGroup(elementName)
{
   var totalNumber = frames['formFrame'].document.forms[0].elements[elementName].length;
   
   for(var i=0; i<totalNumber; i++)
   {
	  var newName = elementName + frames['formFrame'].document.forms[0].elements[elementName][i].value;
	  if(frames['formFrame'].document.forms[0].elements[elementName][i].checked == true)
	  {
	    
		frames['submitFrame'].document.forms[0].elements[newName].value = frames['formFrame'].document.forms[0].elements[elementName][i].value;
		
	  }
	  else
	  {
	  
		frames['submitFrame'].document.forms[0].elements[newName].value = 0;
		
	  }
   }
}
	