


/*
Mental Health Connections Inc. 2007
Lexington, MA mhc@mhc.com
*/


function findDoNextTask()
{
//alert('start rules');
/*------------- Keep for Debugging ----------------------------------------------------------------------------------------------------------------*/

//	debugging = true;

/*-------------------------------------------------------------------------------------------------------------------*/

	showFacts();
	showRule('start on rules');
	document.getElementById('d_0.1').className = 'hideIt';

	if(subscr.dnaLogIn)
	{
		showRule('d_24 DNA User log in done');
		show('d_24');
	//	document.getElementById('buttonDiv').className = 'hideIt';
		return;			
	}
	
	if(subscr.updateDone)
	{
//	alert('in updatedone');
		showRule('d_23 Update done');
		show('d_23');
		document.getElementById('buttonDiv').className = 'hideIt';
		return;
	}
	if(subscr.demoEstablished)
	{
		showRule('subscr.demoEstablished. d_15');
		show('d_15');  // callback has put text in the window.
		document.getElementById('buttonDiv').className = 'hideIt';
		return;
	}
	if(subscr.finalizeOrder)
	{
		show('d_19');
		showRule('finalizeOrder. d_19');
		logInSetup();
		if(document.getElementById('p10').checked || document.getElementById('p20').checked || document.getElementById('p49').checked)
		{
			document.getElementById('multCopies').className = 'showIt';
		}
		if(parent.document.title == 'GeneMedRx – A Tool to Improve Drug Safety')
			document.getElementById('hitEnter').className = 'showIt';
		return;
	}
	
	if(subscr.studentRegistered)
	{
		showRule('d_20:studentRegistered true' );
		show('d_20');
		document.getElementById('buttonDiv').className = 'hideIt';
		return;
	}
	
	if(subscr.goal == 'demo' && subscr.noDemoText)
	{
		showRule('subscr.goal == demo && subscr.noDemoText. d_10');
		document.getElementById('noDemoText'). innerHTML = subscr.noDemoText;
		show('d_10');
		document.getElementById('buttonDiv').className = 'hideIt';
		return;
	}
	
	if(subscr.passwordSent)
	{
		showRule('d_21 Password was snet');
	//	document.getElementById('noDemoText'). innerHTML = subscr.noDemoText;
		show('d_21');
		document.getElementById('buttonDiv').className = 'hideIt';
		return;
	}	

//alert(subscr.goal);
	if(subscr.goal == 'LMS')
	{
		showRule('main menu');
		showGoal(subscr.goal);
		subscr.catagory = 'labelMe';
		show('d_1');
		return '.5';
	}

//-------------------------------- USUAL START ----------------------------------------------------------------

	if(!subscr.goal) // || subscr.goal == 'LMS')
	{
		showRule('main menu');
		show('d_1');
		return '.5';
	}
//	alert('rules: prices= '  + subscr.nmPrices);
	if(subscr.goal == 'buyExtNm' && !subscr.nmPrices && subscr.email && (subscr.registDataIn || subscr.okForDemo)) // If eamil unrecognized don't require regist data in
	{
		showGoal(subscr.goal);
		showRule('Non Med Prices d_25');
		if(parent.logInDone)
			document.getElementById('d_25_3Span').innerHTML = 'Since you are logged in we will use the "Tell a friend: extend two subscriptions" function (lower left side of screen). To let you send an email entitling another person to a discounted price. When they purchase, your subscription will be extended by three months and they will get a 15, instead of 12, month subscription which is equivalent to the $17.95 family plan price.';
		show('d_25');
		return;
	}
	
	if(subscr.nmPrices && !subscr.warningNM)
	{
		if(!subscr.okForDemo) //catagory == 'IndNM' || catagory == 'fxGrp') // If not okfordemo his email was recognized and so no need for warning.
		{
			subscr.warningNM = true;   /// 
		}
		else
		{
			showRule('Warning for nonmedical subscribers d_27');
			show('d_27');
			return;
		}
	}
	if(subscr.warningNM && (subscr.registDataIn || subscr.okForDemo)) // If eamil unrecognized don't require regist data in
	{
		showRule('subscr.nmRegist d_7');
		show('d_7');
//		document.getElementById('subscriptionInstructions').className = 'hideIt';
		document.getElementById('firstName').focus();
		if(subscr.email) // Tour users have given this info
		{
			document.getElementById('email1').value = subscr.email;
			document.getElementById('email2').value = subscr.email;
		}
		document.getElementById('pwInstruct').className = 'hideIt';
		document.getElementById('registHeadline').innerHTML = '<h1>Registration and Billing Information</h1>';
		document.getElementById('cardBillInfo').className = 'showIt';
		if(!subscr.okForDemo)
			document.getElementById('pwInstruct').className = 'showIt';		
		return 7;
	}

	if(subscr.goal == 'demo' && !subscr.demoQualificationAsserted && !subscr.studentQualificationAsserted && subscr.tentativeEmail)
	{
		showRule('Medical Trial subscr. d_16');
		show('d_16');
		return;
	}
	if((subscr.goal == 'activateMed' | subscr.goal == 'activateNonMed') && !subscr.labID)
	{
		show('d_17');
		showGoal(subscr.goal);
		showRule('Activate dnaSubscr. d_17');
		if(subscr.goal == 'activateMed')
		{
			if(subscr.email && subscr.password)
			{
				document.getElementById('userName_q17').className = 'hideIt';	
				document.getElementById('email_q17').className = 'hideIt';
				document.getElementById('pswd_q17').className = 'hideIt';
				document.getElementById('myid').className = 'hideIt';
				document.getElementById('2_q17').className = 'hideIt';
			}	
				
			document.getElementById('3_q17').className = 'showIt';	
			document.getElementById('4_q17').className = 'hideIt';	
		}
		else
		{
			document.getElementById('myid').className = 'hideIt';	
			document.getElementById('2_q17').className = 'hideIt';	
			document.getElementById('3_q17').className = 'hideIt';	
			document.getElementById('4_q17').className = 'showIt';	
		}
//		parent.directRegistr(n) //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
		return;
	}
	
//	alert(subscr.goal);
	if((subscr.goal == 'studentMedical' || 
		subscr.goal == 'studentResident' ||
		subscr.goal == 'studentPharmacy' ||
		subscr.goal == 'studentNursing') && !subscr.studentQualificationAsserted)
	{
		showRule('d_18 Student subscription');
		show('d_18');
		var type = 'Nursing Student';
		if(subscr.goal == 'studentMedical')
			type = 'Medical Student';
		if(subscr.goal == 'studentPharmacy')
			type = 'Pharmacy Student';
		if(subscr.goal == 'studentResident')
			type = 'Medical Resident';		 
		document.getElementById('studentType').innerHTML = 'Free Subscription for ' + type;
		return;
	}
	
	if(subscr.goal == 'tour')
	{
		showRule('d_26: tour start');
		show('d_26');
		showGoal(subscr.goal);
		return;
	}	

	if(subscr.email && subscr.password && !subscr.pswdMatched)  // If data sent in we can conclude they match
	{
		showRule('d_5: Data in params - process like check password');
		showGoal(subscr.goal);
		var param = 'email=' + subscr.email + '&password=' + subscr.password;
		sendAjax('checkPswd',param);
		return;
	}
//		alert('Before d_7 rule pswdM = ' + subscr.pswdMatched);
	if(subscr.goal == 'update' && subscr.pswdMatched == 'yes' && subscr.registDataIn)  // Wants update - only done with data sent in
	{
//alert('in d_7 s.pasword= ' + subscr.password);
		showRule('d_7 Update data');
		showGoal(subscr.goal);
		show('d_7');  //
		document.getElementById('subscriptionInstructions').className = 'showIt';  // "please make any needed changes"
		document.getElementById('firstName').focus();		
		document.getElementById('firstName').value = subscr.firstName; 
		document.getElementById('lastName').value =subscr.lastName;
		document.getElementById('address1').value = subscr.address1;
		document.getElementById('address2').value = subscr.address2;
		document.getElementById('city').value = subscr.city;
		document.getElementById('state').value = subscr.state;
		document.getElementById('postcode').value = subscr.postcode;
		document.getElementById('country').value = subscr.country;
		document.getElementById('telephone').value = subscr.telephone;
		document.getElementById('email1').value = subscr.email;		
		document.getElementById('email2').value = subscr.email;
		document.getElementById('pwInstruct').className = 'showIt';
		document.getElementById('registHeadline').innerHTML = '<h1>Update Registration Information</h1>';
		return 7;			
	
	}	
	if(!subscr.email)
	{
		showRule('d_2: get email');
		showGoal(subscr.goal);
		show('d_2');  // Enter your email.
		document.getElementById('emailInput').focus();
		return 2;
	}
//	if(subscr.id && !subscr.password && !subscr.pswdMatched)  //subscr.noDemoText)  // Email was found
	if(!subscr.password && !subscr.pswdMatched && subscr.id)  // Email was found so ask for password
	{
		showRule('get password d_4');
		show('d_4');  // Enter your password
		document.getElementById('password').focus();
		return 4;
	}

	if(subscr.pswdMatched == 'no')
	{
		showRule('d_6 Password did not match');
		subscr.pswdMatched = undefined;
		subscr.password = undefined;
		var doc = getIFrameDoc('reqPswdFrame');
//		var iframeDoc = frames['reqPswdFrame'].document;
//		alert(doc);
		doc.getElementById('reqPswd').href = 'http://mhc.com/registAjax.php?task=sendPassword&email=' + subscr.email;
		show('d_6');
//		document.getElementById('buttonDiv').className = 'hideIt';
		return;		
	}

	if(!subscr.id && subscr.studentQualificationAsserted && !subscr.registDataUpdated)  // No id = new person	
	{
		showRule('d_7 for student');
		show('d_7');  //
//		document.getElementById('questions2').className = 'hideIt';
		document.getElementById('subscriptionInstructions').classname = 'hideIt';
		document.getElementById('firstName').focus();
		document.getElementById('email1').value = subscr.tentativeEmail;
		document.getElementById('email2').value = subscr.tentativeEmail;
		document.getElementById('pwInstruct').className = 'hideIt';
		document.getElementById('registHeadline').innerHTML = '<h1>Registration for Student Subscription</h1>';
		return 7;
	}

	if(!subscr.id && subscr.goal == 'demo' && !subscr.registDataUpdated)  // No id = new person	
	{
		showRule('d_7 registration screen with survey questions if new person, goal= demo');
		show('d_7');  //
//		document.getElementById('questions2').className = 'hideIt';
		document.getElementById('subscriptionInstructions').className = 'hideIt';
		document.getElementById('firstName').focus();
		document.getElementById('email1').value = subscr.tentativeEmail;
		document.getElementById('email2').value = subscr.tentativeEmail;
		document.getElementById('pwInstruct').className = 'hideIt';
		document.getElementById('registHeadline').innerHTML = '<h1>Registration for 30 Day Trial Subscription</h1>';
		return 7;
	}	
	
	if(subscr.studentQualificationAsserted && !subscr.id && !subscr.registDataUpdated)  // No id = new student	
	{
		showRule('d_7 registration screen; student');
		show('d_7');
		document.getElementById('questions').className = 'showIt';
		document.getElementById('pwInstruct').className = 'hideIt';
		document.getElementById('subscriptionInstructions').className = 'hideIt';
		document.getElementById('questions').className = 'showIt';
		document.getElementById('firstName').focus();
		document.getElementById('email1').value = subscr.tentativeEmail;
		document.getElementById('email2').value = subscr.tentativeEmail;
		return 7;
	}

	if(!subscr.id && subscr.goal == 'buyExt' && !subscr.registDataUpdated && subscr.purchaseSelected)  // No id = new person	
	{
		showRule('d_7 registration screen; new person. with survey questions and card info');
		show('d_7');
		document.getElementById('questions').className = 'showIt';
		document.getElementById('cardBillInfo').className = 'showIt';
//		isPageEmbedded();
		document.getElementById('subscriptionInstructions').className = 'hideIt';
		document.getElementById('pwInstruct').className = 'hideIt';
		document.getElementById('firstName').focus();
		document.getElementById('email1').value = subscr.tentativeEmail;
		document.getElementById('email2').value = subscr.tentativeEmail;
		return 7;
	}
	if(subscr.id && !subscr.registDataUpdated && subscr.studentQualificationAsserted && subscr.registDataIn)  // Known student	
	{
		showRule('registration screen; Known student. with survey questions and card info');
		show('d_7');
		document.getElementById('firstName').focus();
		document.getElementById('subscriptionInstructions').className = 'hideIt';
		document.getElementById('firstName').value = subscr.firstName; 
		document.getElementById('lastName').value =subscr.lastName;
		document.getElementById('address1').value = subscr.address1;
		document.getElementById('address2').value = subscr.address2;
		document.getElementById('city').value = subscr.city;
		document.getElementById('state').value = subscr.state;
		document.getElementById('postcode').value = subscr.postcode;
		document.getElementById('country').value = subscr.country;
		document.getElementById('telephone').value = subscr.telephone;
		document.getElementById('email1').value = subscr.email;
		return 7;
	}

// UNUSED?????	
	if(subscr.id && subscr.registDataUpdated)  // Person is known	
//	if(subscr.registDataUpdated )  // Person  known or not known	
	{
		showRule('registration screen only: registDataUpdated=true d_7');
		show('d_7');  // Form filled when password was found
		document.getElementById('firstName').focus();
		document.getElementById('email1').value = subscr.email;
		document.getElementById('email2').value = subscr.email;
		return 7;
	}
	if(subscr.registDataIn && interpretCatagory() && !subscr.catagoryRechecked && subscr.goal == 'buyExt')
	{
		showRule('d_22  Person said he\'s in a group in past.');
		document.getElementById('affilMemberStill').innerHTML = interpretCatagory();
		show('d_22');
		return;
	}		
	
	if(!subscr.id && subscr.purchaseSelected)	
	{
		showRule('credit card');
		show('cardBillInfo');
//		isPageEmbedded();
//		alert(parent.title);
//		if(parent.title == 'GeneMedRx')   // Relay needs to know if we ran from inside GeneMedRx
//			document.getElementById('insideGmrx').value = 'yes';
//		alert(document.getElementById('insideGmrx').value);
		return;	
	}
	if(subscr.registDataIn && subscr.goal == 'buyExt' && !subscr.amount && !subscr.showAffilPage)  // Registration data was 	
	{
//	alert('price page 1');
		showRule('price page 1 runs after regist data.');
		setupPricePage();
		return 11 
	}
//alert('ready for pricepage try')
	if(!subscr.id && subscr.goal == 'buyExt' && !subscr.amount && !subscr.showAffilPage)  //
	{
//	alert('in price lage 2');
		showRule('price page 2 Runs before resist data');
		setupPricePage();
		return 11 
	}

	if(subscr.pswdMatched == 'yes' && subscr.registDataIn == undefined)  // Gets Registration data from server
	{

		showRule('get data from server');
		show('d_0.5');   //Processing information....
		document.getElementById('buttonDiv').className = 'hideIt';
		var param = 'email=' + subscr.email + '&password=' + subscr.password;
		sendAjax('registration',param);
		return 7;		
	}
	if(subscr.pswdMatched == 'no' && passwordFailedReported == undefined)
	{
		showRule('password did not match');
		show('d_8');
		return 8;
	}

	if(subscr.email == undefined && !subscr.membershipChecked)	
	{
		showRule('email did not match');
		show('d_9');  // email did not match
		return 9;
	}
	if(subscr.purchaseSelected)
	{
		showRule('registration page 2 purchaseSelected= true');
		document.getElementById('firstName').value = subscr.firstName; 
		document.getElementById('lastName').value =subscr.lastName;
		document.getElementById('address1').value = subscr.address1;
		document.getElementById('address2').value = subscr.address2;
		document.getElementById('city').value = subscr.city;
		document.getElementById('state').value = subscr.state;
		document.getElementById('postcode').value = subscr.postcode;
		document.getElementById('country').value = subscr.country;
		document.getElementById('telephone').value = subscr.telephone;
		document.getElementById('email1').value = subscr.email;
		show('d_7');
		document.getElementById('cardBillInfo').className= 'showIt';
//		isPageEmbedded();
		return ;		
	}
	if(subscr.showAffilPage)
	{
		showRule('d_13 Confirm organization membership');
		show('d_13');
		document.getElementById('affiliateOrgName').innerHTML = document.getElementById('affiliateOrgName2').innerHTML = subscr.affiliateOrgName;
		return; 	
	}
	
	alert('No rule to run');		
}

