function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function() 
		{
			oldonload();
			func();
		}
	}
}
function toggle(obj)
{
	if ( obj.style.display != 'none' ) 
	{
		obj.style.display = 'none';
	}
	else 
	{
		obj.style.display = '';
	}
	return obj;
}

var viewTestimonialCurrentItem;
function viewTestimonial( aId )
{
	if( viewTestimonialCurrentItem )
	{
		toggle(viewTestimonialCurrentItem);
	}
	var el = document.getElementById("Testimonial_" + aId);
	toggle(el);
	viewTestimonialCurrentItem = el;
}



/*
 * Scan Form
 */
function onTabShowed(TabNumber)
{
	$('CurrentStep').innerHTML = TabNumber;
}
function goToTab(TabNumber, aRequired)
{
	var lTotal;
	var lElement;
	var lValid = true;
	if( aRequired != null )
	{
		lTotal = aRequired.length;
		for(i=0; i<lTotal; i++)
		{
			eval("lValid &= ValidateGroup_" + aRequired[i] +"();");
		}
	}
	if( lValid )
	{
		toggleTab(TabNumber, 'TabNavBtn' + TabNumber, onTabShowed);
	}
	else
	{
		alert("Please complete all the questions before advancing to the next stage.");
	}
}
function ValidateGroup(GroupName)
{
	if( isDisabledRadioButtonList(GroupName) )
	{
		return true;
	}
	return validateRadioButtonList(GroupName);
}
function ValidateTextBox(aId, aMinLength, aMaxLength)
{
	var lElement = $(aId);
	if( lElement.disabled )
	{
		return true;
	}
	return validateTextInput(lElement, aMinLength, aMaxLength);
}
function DynTableScan_Answer(Row, ColumnName, RowIndex, TargetDiv)
{
	newElem = document.createElement("input");
	newElem.name = TargetDiv.id + "_" + RowIndex;
	newElem.id = TargetDiv.id + "_" + RowIndex;
	newElem.type = "text";
	newElem.value = Row[ColumnName];
	return newElem;
}
function ObserveScanQuestions()
{
	//$('Question_17_4_RbList_0').observe('click', Question5Click);
	//$('Question_17_4_RbList_1').observe('click', Question5Click);
}
function ObserveScanFemaleQuestions()
{
	/*
	$('Question_2_0_RbList_0').observe('click', QuestionFemale1Click);
	$('Question_2_0_RbList_1').observe('click', QuestionFemale1Click);
	$('Question_2_1_RbList_0').observe('click', QuestionFemale2Click);
	$('Question_2_1_RbList_1').observe('click', QuestionFemale2Click);
	*/
}

function Question5Click(event) 
{
	checkValues(ffw_GetRadioButtonValue('Question_17_4_RbList'), 1, 17, 5, 12);
}
function QuestionFemale1Click(event) 
{
	checkValues(ffw_GetRadioButtonValue('Question_2_0_RbList'), 1, 2, 1, 3);
	if( element.value == 2 )
	{
		$('Question_2_1_RbList_1').checked = true;
		checkValues(2, 1, 2, 3, 5);
	}
}
function QuestionFemale2Click(event) 
{
	checkValues(ffw_GetRadioButtonValue('Question_2_1_RbList'), 1, 2, 3, 5);
}
function checkValues(Value, RightValue, Group, StartQ, EndQ)
{
	if(Value == RightValue)
	{
		for(var i=StartQ; i<EndQ; i++)
		{
			Form.enable($('Question_'+Group+'_'+i+'_RbList_Label'));
		}
	}
	else
	{
		for(var i=StartQ; i<EndQ; i++)
		{
			Form.disable($('Question_'+Group+'_'+i+'_RbList_Label'));
		}
	}
}





/*
 * Questions CRUD 
 */
function DynTableQuestion_Answers(Row, ColumnName, RowIndex, TargetDiv)
{
	newElem = document.createElement("input");
	newElem.name = TargetDiv.id + "_" + RowIndex;
	newElem.id = TargetDiv.id + "_" + RowIndex;
	newElem.type = "text";
	newElem.style.width = "600px";
	newElem.value = Row[ColumnName];
	return newElem;
}
function DynTableQuestion_AnswersKey(Row, ColumnName, RowIndex, TargetDiv)
{
	newElem = document.createElement("input");
	newElem.name = TargetDiv.id + "_Key" + RowIndex;
	newElem.id = TargetDiv.id + "_Key" + RowIndex;
	newElem.type = "hidden";
	newElem.value = Row[ColumnName];
	return newElem;
}
function ShowTableAnswers()
{
	QValue = $("question_idQuestionType").value; 
	if( QValue != 2 && QValue != 3)
	{
		$("DivAnswersList").style.display = "none";
		$("AnswerNotA").style.display = "";
	}
	else
	{
		$("DivAnswersList").style.display = "";
		$("AnswerNotA").style.display = "none";
	}
}






/*
	Scan Reports
*/
function ShopList_GetFoodList(idFoodCategory, FoodCategory)
{
	var url="Application.php?Page=ShopList&View=GetShopListAJAX&ContentType=TYPEHTML"
	url+="&idFoodCategory=" + idFoodCategory;
	url+="&IdScan=" + $('ShopList_idScan').value;	
	xmlHttp = ffwGetXmlHttpObject(ShopList_GetFoodList_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('ShopList_idFoodCategory').value = idFoodCategory; 
	$('ShopList_FoodTitle').innerHTML = FoodCategory;
	$('ShopList_FoodList').innerHTML = "Updating List...";
}
function ShopList_GetFoodList_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		$('ShopList_FoodList').innerHTML = xmlHttp.responseText;
	}
}
function ShopList_SaveYourFoodList()
{
	var url="Application.php?Page=ShopList&View=SaveYourFoodList&ContentType=TYPEHTML"
	url+="&IdScan=" + $('ShopList_idScan').value;	
	xmlHttp = ffwGetXmlHttpObject(ShopList_SaveYourFoodList_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('ShopList_BtnSave').disabled = true;
	//$('ShopList_BtnSave').value = "Saving...";
}
function ShopList_SaveYourFoodList_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result == "1")
		{
			alert('Grocery List saved successfully');
			if( $('ShopList_isNew').value == "true")
			{
				window.location.href = "Application.php?Page=ShopList&View=Default&IdScan=" + $('ShopList_idScan').value;
			} 
		}
		else
		{
			alert("Error, we could not save your Grocery List. \n" + "Message:" + Answer.Msg);
		}
		$('ShopList_BtnSave').disabled = false;
		//$('ShopList_BtnSave').value = "Save";
	}
}
function ShopList_AddFoodToYou(TheElement)
{
	var lIdFoodCategory = $('ShopList_idFoodCategory').value;
	var EffectFinished;
	var idFood;
	var ReturnToList = true;
	if( TheElement.EffectInProgress == true)
	{
		return ;
	}
	TheElement.EffectInProgress = true;
	TheElement.disabled = true;
	idFood = TheElement.value;
	TheElement.checked = TheElement.parentNode.parentNode.id == "ShopList_Label";
	if( TheElement.checked )
	{
		EffectFinished = ShopList_AddFoodToYou_EffectFinished;
		var url="Application.php?Page=ShopList&View=AddFoodToCart&ContentType=TYPEHTML";
	}
	else
	{
		EffectFinished = ShopList_ReturnToTheList_EffectFinished;
		var url="Application.php?Page=ShopList&View=RemoveFoodFromCart&ContentType=TYPEHTML";
		ReturnToList = TheElement.parentNode.parentNode.id == "YourShopList_" + lIdFoodCategory + "_Label";
	}
	url+="&idFood=" + idFood;
	url+="&IdScan=" + $('ShopList_idScan').value;
	xmlHttp = ffwGetXmlHttpObject(ShopList_ListUpdated_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	if( $('CategoryList_' + lIdFoodCategory).style.display == "none" )
	{
		$('CategoryList_' + lIdFoodCategory).style.display = "block";
	}
	new Effect.Highlight(TheElement.parentNode, {duration:0.4, startcolor:"#B7ECB7", afterFinish:EffectFinished, ReturnToList:ReturnToList });
}
function ShopList_ListUpdated_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//
	}
}
function ShopList_AddFoodToYou_EffectFinished(obj)
{
	var lIdFoodCategory = $('ShopList_idFoodCategory').value;
	obj.element.childNodes[0].EffectInProgress = false;
	obj.element.childNodes[0].disabled = false;
	TargetElem = 'YourShopList_' + lIdFoodCategory + '_Label'; 
	$(TargetElem).appendChild(obj.element);
}
function ShopList_ReturnToTheList_EffectFinished(obj)
{
	obj.element.childNodes[0].EffectInProgress = false;
	obj.element.childNodes[0].disabled = false;
	if( obj.options.ReturnToList )
	{
		$('ShopList_Label').appendChild(obj.element);
	}
	else
	{
		obj.element.parentNode.removeChild(obj.element);
	}
}
function PrintReportWindow(aIdScan, aIdReport)
{
	var lUrl = "Application.php?Page=PrintableReport&View=PrintReport&IdScan=" + aIdScan;
	lUrl += "&IdReport=" + aIdReport;
	var newWin = window.open(lUrl,"PrintableReport","width=760,height=450,scrollbars=yes");
	newWin.focus();
}
function PrintReport(aTarget)
{
	aTarget.style.display = "none";
	if( window.print )
	{
		window.print();
	}
	else
	{
		alert("Sorry, your browser do not allow you to print.");
	}
	aTarget.style.display = "";
}


/*
* User 
*/
function User_CountryChange(aDropCountry, aIdDropStates)
{
	if( aDropCountry.value != 184 && aDropCountry.value != "US" )
	{
		$(aIdDropStates).selectedIndex = $(aIdDropStates).length - 1;
	}
}
function UserAccount_Validate()
{
	var lDateField = $('user_BirthDate');
	if( lDateField == null )
	{
		alert("Bad access");
		return false;
	}
	if( lDateField.value.length == 0 )
	{
		alert("Please type your Birth Date");
		return false;
	}
	return SingleDateField_Validate( lDateField );
}
function UserAccount_OpenConfirmPopUp()
{
	var lValue = ffw_GetRadioButtonValue('user_AccountType');
	var lPayPeriod = ffw_GetRadioButtonValue('user_PayPeriod');
	if(lValue == "Premium" && lPayPeriod == "Monthly")
	{
		Modalbox.show(	$('TermsContent'), {title: "Important notice!", width: 800, height: 460 });
	}
	else
	{
		DoAction('Register');
	}
}
function UserAccount_AcceptTerms()
{
	DoAction('Register');
}
function UserAccount_ValidateForm()
{
	if( !UserAccount_Validate() )
	{
		return false;
	}
	var lValue = ffw_GetRadioButtonValue('user_AccountType');
	var lPayType = ffw_GetRadioButtonValue('user_PaymentMethod');
	var lEbook = ffw_GetRadioButtonValue('user_Ebook');
	var lPayPeriod = ffw_GetRadioButtonValue('user_PayPeriod');
	if( lValue == "Premium" )
	{
		if(lPayPeriod == "Monthly" && lEbook == null)
		{
			alert("Please select an Ebook");
			return false;
		}
		if(lPayType == null)
		{
			alert("Please select the payment method");
			return false;
		}
		if(lPayType == "NewCard")
		{
			return validatePaymentForm();
		}
		if(lPayType == "Coupon")
		{
			if($('user_CouponCode').value.length < 3)
			{
				alert('Please type the coupon code');
				return false;
			}
			return true;
		}
	}
	else
	{
		return true;
	}
}
function AcceptDisclaimer(aTarget)
{
	aTarget.disabled = true;
	new Effect.Grow('LinkToPerform');
}



/*
* Reports
*/
function ShowLearnMore(Target)
{
	Modalbox.show(	$(Target).value, {title: "Learn More", width: 600, height: 460 });
}
function ShowDetailInformation(aFile)
{
	lIdScan = $('PrecisionMix_idScan').value;
	lTarget = "Application.php?Page=EatingPlan&ContentType=TYPEHTML&View=Showfile&File="+aFile+"&IdScan=" + lIdScan;
	aTitle = '';
	Modalbox.show(lTarget, {title: aTitle, width: 600, height: 460 });
}

/*
* Exercise
*/
function Exercise_ViewExercise(aIdExercise)
{
	lIdScan = $('Exercise_idScan').value;
	lTarget = "Application.php?Page=MainExercise&ContentType=TYPEHTML&View=ViewExercise&idExercise="+aIdExercise+"&IdScan=" + lIdScan;
	aTitle = '';
	Modalbox.show(lTarget, {title: aTitle, width: 750, height: 460 });	
}

/*
* ScanWindow
*/
function Scan_ViewScan3D(aIdScan)
{
	//myLightWindow.createWindow('ScanLink');
	myLightWindow.activate(null, $('ScanLink'));
	
	/*lTarget = "Application.php?Page=ScanPreview&View=Default&IdScan="+aIdScan+"&ContentType=TYPEHTML";
	aTitle = 'Scan';
	Modalbox.show(lTarget, {title: aTitle, width: 700, height: 800});	
	lIdScan = $('Exercise_idScan').value;
	Element.hide(Modalbox.MBclose); 
	Event.stopObserving(Modalbox.MBclose, "click", Modalbox.hide );*/
}


/*
* Supplement List
*/
function Scan_ShowAdditionalSupplements()
{
	Effect.toggle('NutritionAdditionalContainer', 'blind',{duration: 1});
}


/*
* Printable Report. Send report to Doctor
*/
function DoctorNote_ShowSendToForm()
{
	new Effect.toggle('PrintableOptions_SendToDoctorDiv','blind', {duration:0.4});
}
function Printable_SendReport(OnlyDoctorNote)
{

	if( OnlyDoctorNote === true )
	{
		OnlyDoctorNote = "1";
	}
	else
	{
		OnlyDoctorNote = "0";
	}
	if( $('PrintableOptions_DoctorEmail').value.length < 3 )
	{
		alert('Please type a valid Email');
		return null;
	}

	var url="Application.php?Page=ScanReport&View=SendReport&ContentType=TYPEHTML";
	url+="&IdScan=" + $('PrintReport_idScan').value;
	url+="&DoctorName=" + $('PrintableOptions_DoctorName').value;
	url+="&DoctorEmail=" + $('PrintableOptions_DoctorEmail').value;
	url+="&OnlyDoctorNote=" + OnlyDoctorNote;
	xmlHttp = ffwGetXmlHttpObject(Printable_SendReport_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('Printable_Save').disabled = true;
	$('Printable_Save').value = 'sending...';
}
function Printable_SendReport_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when tried to sent the email");
		}
		$('PrintableOptions_SendToDoctorDiv').innerHTML = Answer.Msg;
	}
}
function CheckAvailability()
{
	var lEmail = $('user_Email').value;
	if( EmailCheck(lEmail) )
	{
		var url="Application.php?Page=UserAccount&View=CheckEmail&ContentType=TYPEHTML";
		url+="&Email=" + lEmail;
		xmlHttp = ffwGetXmlHttpObject(CheckAvailability_Listener);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
		$('AvaliabilityAnswer').innerHTML = "checking...";
	}
	else
	{
		alert('Please type a valid Email');
	}
}
function CheckAvailability_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		$('AvaliabilityAnswer').innerHTML = xmlHttp.responseText;
	}
}
function Doctor_RemoveSupplement(aIdSupplement, aIdScan)
{
	$('SupContainer_'+aIdSupplement).innerHTML = "removing";
	var url="Application.php?Page=DoctorReport&View=ToggleSupplement&ContentType=TYPEHTML";
	url+="&IdSupplement=" + aIdSupplement;
	url+="&IdScan=" + aIdScan;
	url+="&Enable=true";
	xmlHttp = ffwGetXmlHttpObject(Doctor_RemoveSupplement_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	CurrentRemovingSupplement = aIdSupplement;
}
function Doctor_RemoveSupplement_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when tried to save the supplement");
		}
		$('Supplement_'+CurrentRemovingSupplement).style.display = 'none';
	}
}
function Doctor_RemoveStudy(aIdDoctorNoteAction, aIdScan)
{
	$('Study_'+aIdDoctorNoteAction).innerHTML = "removing";
	var url="Application.php?Page=DoctorReport&View=ToggleStudy&ContentType=TYPEHTML";
	url+="&IdDoctorNoteAction=" + aIdDoctorNoteAction;
	url+="&IdScan=" + aIdScan;
	url+="&Enable=true";
	xmlHttp = ffwGetXmlHttpObject(Doctor_RemoveStudy_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	CurrentDoctorStudy = aIdDoctorNoteAction;
}
function Doctor_RemoveStudy_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when tried to save the study " + Answer.Msg);
		}
		$('StudyContainer_'+CurrentDoctorStudy).style.display = 'none';
	}
}
function resetSupAndStudies(aIdScan)
{
	var url="Application.php?Page=DoctorReport&View=ResetSupAndStudies&ContentType=TYPEHTML";
	url+="&IdScan=" + aIdScan;
	xmlHttp = ffwGetXmlHttpObject(resetSupAndStudies_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	CurrentIdScan = aIdScan;
}
function resetSupAndStudies_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when tried to save the study " + Answer.Msg);
		}
		else
		{
			getURI('Application.php?Page=PrintableReport&View=PrintReport&IdScan='+CurrentIdScan+'&IdReport=20');
		}
	}
}



