﻿function checkQuestionAnswers(thePage){
	//create variables for questions
	var theQuestionsWrapper = document.getElementById('theQuestions');
	var theQuestionsError = document.getElementById('theQuestionsError');
	var theQuestionsArray = theQuestionsWrapper.getElementsByTagName('select');
	
	//clear error text, if there is any
	theQuestionsError.innerHTML = '';
	
	//loops through questions, and checks if any are answered
	var anyAnswered=0;
	for (var i=0; i<theQuestionsArray.length; i++){
		//note: arbitrarily, yes = 1 and no = 2 because this is a Tangora category
		if (theQuestionsArray[i].value == 2){ 
			anyAnswered+=1;
		}
	}
	if (anyAnswered>0){ //answered yes to one question
		var theForm = document.forms['rediger'+thePage];
		theForm.submit();
	} else { //otherwise they answered no, we need to make an error message
		theQuestionsError.innerHTML = '<p class="errorMessage">You need to be able to answer yes to at least one question to proceed.</p>';
		return;
	}

}

function checkDisclaimerCheckbox(thePage){
var disclaimerCheckbox = document.getElementById('extrafield1065');
var theQuestionsError = document.getElementById('theQuestionsError');

  if (!disclaimerCheckbox.checked ){
    theQuestionsError.innerHTML = '<p class="errorMessage">You need to accept the disclaimer in order to proceed.</p>';
  } else if (disclaimerCheckbox.checked){
    var theForm = document.forms['rediger'+thePage];
    theForm.submit();
  }
}

//Function to check if user checked disclaimer on RegisterNonUSIndividual page
function checkDisclaimerCheckbox(thePage){
var disclaimerCheckbox = document.getElementById('extrafield1065');
var theQuestionsError = document.getElementById('theQuestionsError');

  if (!disclaimerCheckbox.checked ){
    theQuestionsError.innerHTML = '<p class="errorMessage">You need to accept the disclaimer in order to proceed.</p>';
  } else if (disclaimerCheckbox.checked){
    var theForm = document.forms['rediger'+thePage];
    theForm.submit();
  }
}

//Function to check if user checked disclaimer on RegisterNonUSInstitutional page
function checkDisclaimerCheckbox2(thePage){
var disclaimerCheckbox2 = document.getElementById('extrafield1066');
var theQuestionsError = document.getElementById('theQuestionsError');

  if (!disclaimerCheckbox2.checked ){
    theQuestionsError.innerHTML = '<p class="errorMessage">You need to accept the disclaimer in order to proceed.</p>';
  } else if (disclaimerCheckbox2.checked){
    var theForm = document.forms['rediger'+thePage];
    theForm.submit();
  }
}

//portal front redirect script

function directUser(){
  var selectedDomicile = document.getElementById('domicile').value;
  var selectedInvestorStatus = document.getElementById('investorstatus').value;
  
  var errorMessage = document.getElementById('portalError');
  
  var usInstitutionPage = '/RegisterUSinstitutional'; //792
  var usIndividualPage = '/RegisterUSindividual'; //777
  var worldInstitutionPage = '/RegisterNonUSinstitutional';  //793
  var worldIndividualPage = '/RegisterNonUSindividual'; //794
  
  //deletes error message at outset
  errorMessage.innerHTML='';
  
  //case: either domicile or investor status not chosen
  if (selectedDomicile == 'nodomicile' || selectedInvestorStatus == 'noinvestorstatus'){
    errorMessage.innerHTML='Please choose both your domicile and your investor status.';
    return;
  }
  
  //case US institution
  if (selectedDomicile == 'us' && selectedInvestorStatus == 'institution'){
    document.location = usInstitutionPage;
  }
  
  //case US individual
  if (selectedDomicile == 'us' && selectedInvestorStatus == 'individual'){
    document.location = usIndividualPage;
  }
  
  //case world institution
  if (selectedDomicile == 'world' && selectedInvestorStatus == 'institution'){
    document.location = worldInstitutionPage;
  }
  
  //case world individual
  if (selectedDomicile == 'world' && selectedInvestorStatus == 'individual'){
    document.location = worldIndividualPage;
  }
  
}
