var contentelementname = "maincontentspot"; var messageelementname = "mainmessagespot";

function motionDiarySearch() {
	fromyear = $F( "fromyear" ); frommonth = $F( "frommonth" ); fromday = $F( "fromday" );
	thismonth = ( frommonth.length < 2 && frommonth < 10 ) ? "0" + frommonth : frommonth;
	thisday = ( fromday.length < 2 && fromday < 10 ) ? "0" + fromday : fromday;
	fromdate = fromyear + "-" + thismonth + "-" + thisday;

	toyear = $F( "toyear" ); tomonth = $F( "tomonth" ); tilday = $F( "tilday" );
	thismonth = ( tomonth.length < 2 && tomonth < 10 ) ? "0" + tomonth : tomonth;
	thisday = ( tilday.length < 2 && tilday < 10 ) ? "0" + tilday : tilday;
	todate = toyear + "-" + thismonth + "-" + thisday;
	motiondiarytablename = $F( "motiondiarytablename" );
	motiondiarysearchword = $F( "motiondiarysearchword" ).strip();
	if( motiondiarytablename.length > 20 ) {
		emptyElement( submessageelementname );
		paramurl = "searchpageoption=1&fromdate=" + fromdate + "&todate=" + todate + "&motiondiarytablename=" + motiondiarytablename + "&motiondiarysearchword=" + escape( motiondiarysearchword );
		getPage3( createRequest(), "motiondiarysearchresults.php", submessageelementname, contentelementname, paramurl, '', '' );
	}
}

function callMotionDiarySearch() {
	motiondiarytablename = $F( "motiondiarytablename" );
	motiondiarysearchword = $F( "motiondiarysearchword" ).strip();
	if( motiondiarytablename.length > 20 ) { motionDiarySearch(); }
	else { updateElement( submessageelementname, "<BR>Du har inte valt en motionsdagbok att s&ouml;ka i &auml;n." ); resetJustWorkspace(); }
}

function foo( milliseconds ) { alert( "foo" ); }

// Funktion som visar en motionsdagbokspost.
function showSearchMotiondiarySearchpage() {
	resetWorkspace(); resetSubWorkspace();
	getPage3( createRequest(), "motiondiarysearchpage.php", "searchresultmessagespot", "searchresultcontentspot", '', '', '' );
}

// Funktion som visar en motionsdagboksposter efter angivet datum.
function reloadMotiondiaryRecordlist( motiondiarytablename, searchpageoption ) {
	if( searchpageoption == 1 ) {
		motionDiarySearch();
	}
	else {
//		resetWorkspace(); resetSubWorkspace();
		var showmotiondiarypostsperiodrequest = createRequest();
		year = $F( "year" ); month = $F( "month" ); day = $F( "day" );
		thismonth = ( month.length < 2 && month < 10 ) ? "0" + month : month;
		thisday = ( day.length < 2 && day < 10 ) ? "0" + day : day;
		choosendate = year + "-" + thismonth + "-" + thisday;
		
		paramurl = "searchpageoption=" + searchpageoption + "&choosendate=" + choosendate + "&motiondiarytablename=" + motiondiarytablename;
//		alert( paramurl );
//		alert( searchpageoption );
		getPage3( createRequest(), "getmotiondiarynames.php", messageelementname, "motiondiarynamesspot", paramurl, '', '' );
	}
}

// Uppdatera gränssnittet efter uppladdning av en ny motionsdagbokspost
function doAddNewMotiondiaryAfterwork( motiondiarytablename, searchpageoption ) {
	resetJustSubWorkspace();
	emptySubWorkspot( 3000 );
	getPage3( createRequest(), "mymotiondiarylist.php", messageelementname, "usermotiondiarylist", '', '', '' );
	reloadMotiondiaryRecordlist( motiondiarytablename, searchpageoption );
}

// Spara ändringar i motionsdagbokspost
function saveMotiondiaryRecord( motiondiaryrecordid, userid, motiondiaryid, tablename, searchpageoption ) {
	var savemotiondiaryrecordrequest = createRequest();
	thedate = $F( "year" ) + "-" + $F( "month" ) + "-" + $F( "day" ); thetime = $F( "hour" ) + ":" + $F( "minute" );
	activity = $F( "activity" ).strip(); quantity = convertCommasToDots( $F( "quantity" ).strip() ); lengthorweight = convertCommasToDots( $F( "lengthorweight" ).strip() );
	text = $F( "text" ).strip();

	paramurl = "date=" + thedate + "&time=" + thetime + "&activity=" + escape( activity ) + "&quantity=" + escape( quantity ) + "&lengthorweight=" + escape( lengthorweight ) + 
	"&text=" + escape( text ) + "&tablename=" + tablename + "&motiondiaryrecordid=" + motiondiaryrecordid + "&pagetype=2&optype=7";

 	if( activity == "" ) { errormessages.push( "Du gl&ouml;mde att fylla i f&auml;ltet \"Namn p&aring; aktivitet\" f&ouml;r motionsdagboksposten." ); }
	if( lengthorweight != "" ) { if( !validateNumber( lengthorweight ) ) { errormessages.push( "V&auml;rdet du skrev i f&auml;ltet \" L&auml;ngd/kg:\" &auml;r inte ett giltigt tal." ); } }
	if( quantity != "" ) { if( !validateNumber( quantity ) ) { errormessages.push( "V&auml;rdet du skrev i f&auml;ltet \" Tid/antal reps:\" &auml;r inte ett giltigt tal." ); } }
//	alert( paramurl );
 	if( errormessages.length == 0 ) {
		var url = "usermotiondiarieshandler.php";
		savemotiondiaryrecordrequest.open( "POST", url, true );
		savemotiondiaryrecordrequest.onreadystatechange = function doDOMChanges() {
			if( savemotiondiaryrecordrequest.readyState == 4 ) {
				if( savemotiondiaryrecordrequest.status == 200 ) {
					response = savemotiondiaryrecordrequest.responseText;
					if( response == 1 ) {
						updateElement( submessageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Motionsdagboksposten uppdaterades." );
						emptyElement( subcontentelementname );
						doAddNewMotiondiaryAfterwork( tablename, searchpageoption );
					}
					else {
						updateElement( submessageelementname, "Motionsdagboksposten kunde inte uppdateras.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( submessageelementname, "Det uppstod ett fel n&auml;r motionsdagboksposten skulle uppdateras in i  i motionsdagboken.<BR>Felkod: " + savemotiondiaryrecordrequest.status );
				}
			}
			else {
				updateElement( submessageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Uppdaterar motionsdagbokspost... Var god v&auml;nta..." );
			}
		}
		savemotiondiaryrecordrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		savemotiondiaryrecordrequest.send( paramurl );
	}
	else {
		printErrors( submessageelementname );
	}
}

// Lägger till ny motionsdagbokspost
function addNewMotiondiaryRecord( userid, motiondiaryid, tablename ) {
	var newmotiondiaryrecordrequest = createRequest();
	thedate = $F( "year" ) + "-" + $F( "month" ) + "-" + $F( "day" ); thetime = $F( "hour" ) + ":" + $F( "minute" );
	activity = $F( "activity" ).strip(); quantity = convertCommasToDots( $F( "quantity" ).strip() ); lengthorweight = convertCommasToDots( $F( "lengthorweight" ).strip() );
	text = $F( "text" ).strip();

	paramurl = "date=" + thedate + "&time=" + thetime + "&activity=" + escape( activity ) + "&quantity=" + escape( quantity ) + "&lengthorweight=" + escape( lengthorweight ) + 
	"&text=" + escape( text ) + "&tablename=" + tablename + "&pagetype=2&optype=6";
//	alert( paramurl );
 	if( activity == "" ) { errormessages.push( "Du gl&ouml;mde att fylla i f&auml;ltet \"Namn p&aring; aktivitet\" f&ouml;r motionsdagboksposten." ); }
	if( lengthorweight != "" ) { if( !validateNumber( lengthorweight ) ) { errormessages.push( "V&auml;rdet du skrev i f&auml;ltet \" L&auml;ngd/kg:\" &auml;r inte ett giltigt tal." ); } }
	if( quantity != "" ) { if( !validateNumber( quantity ) ) { errormessages.push( "V&auml;rdet du skrev i f&auml;ltet \" Tid/antal reps:\" &auml;r inte ett giltigt tal." ); } }

 	if( errormessages.length == 0 ) {
		var url = "usermotiondiarieshandler.php";
		newmotiondiaryrecordrequest.open( "POST", url, true );
		newmotiondiaryrecordrequest.onreadystatechange = function doDOMChanges() {
			if( newmotiondiaryrecordrequest.readyState == 4 ) {
				if( newmotiondiaryrecordrequest.status == 200 ) {
					response = newmotiondiaryrecordrequest.responseText;
					if( response == 1 ) {
						updateElement( submessageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Den nya motionsdagboksposten lades in i motionsdagboken." );
						doAddNewMotiondiaryAfterwork( tablename, 0 );
					}
					else {
						updateElement( submessageelementname, "Den nya motionsdagboksposten kunde inte l&auml;ggas in i motionsdagboken.<BR>Felmeddelande: " + response + "" );
					}
				}
				else {
					updateElement( submessageelementname, "Det uppstod ett fel n&auml;r den nya motionsdagboksposten skulle l&auml;ggas in i  i motionsdagboken.<BR>Felkod: " + newmotiondiaryrecordrequest.status );
				}
			}
			else {
				updateElement( submessageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> L&auml;gger till ny motionsdagbokspost... Var god v&auml;nta..." );
			}
		}
		newmotiondiaryrecordrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		newmotiondiaryrecordrequest.send( paramurl );
	}
	else {
		printErrors( submessageelementname );
	}
}

// Funktion som visar formuläret som används för att lägga till ny motionsdagbokspost.
function showNewMotiondiaryRecordForm( motiondiaryid ) {
	getPage3( createRequest(), "newmotiondiaryrecordform.php", submessageelementname, subcontentelementname, "motiondiaryid=" + motiondiaryid, '', '' );
}

// Funktion som visar formuläret som används för att redigera ett motionsdagbok.
function showModifyMotiondiaryRecordForm( motiondiaryrecordid, motiondiarytablename, motiondiaryid, userid, searchpageoption ) {
	getPage3( createRequest(), "editmotiondiaryrecordform.php", submessageelementname, subcontentelementname, 
		"searchpageoption=" + searchpageoption + "&motiondiarytablename=" + motiondiarytablename + "&motiondiaryid=" + motiondiaryid + "&motiondiaryrecordid=" + motiondiaryrecordid + "&userid=" + userid, '', '' );
}

function deleteMotiondiaryRecord( motiondiarytablename, motiondiaryrecordid ) {
	resetSubWorkspace();
	paramurl = "motiondiarytablename=" + motiondiarytablename + "&motiondiaryrecordid=" + motiondiaryrecordid + "&optype=4";
	var deletemotiondiarycatrequest = createRequest();
	var url = "usermotiondiarieshandler.php";
	deletemotiondiarycatrequest.open( "POST", url, true );
	deletemotiondiarycatrequest.onreadystatechange = function doDOMChanges() {
		if( deletemotiondiarycatrequest.readyState == 4 ) {
			if( deletemotiondiarycatrequest.status == 200 ) {
				var response = deletemotiondiarycatrequest.responseText;
				if( response == 1 ) {
					elementid =  "motiondiary_" + motiondiaryrecordid;
					removeElement( elementid );
					updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Motionsdagboksposten togs bort." );
					emptyMainMessagespot( 3000 );
					if( $F( "searchpage" ) == 1  ) {
						doAddNewMotiondiaryAfterwork( motiondiarytablename, 1 );
						motionDiarySearch();
					}
					else {
						doAddNewMotiondiaryAfterwork( motiondiarytablename, 0 );
					}
				}
				else {
					updateElement( messageelementname, "Motionsdagboksposten kunde inte tas bort.<BR>Felmeddelande: " + response );
				}
			}
			else {
				updateElement( messageelementname, "Det uppstod ett fel n&auml;r Motionsdagboksposten skulle tas bort!<BR>Felkod: " + deletemotiondiarycatrequest.status );
			}
		}
		else {
			updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Tar bort motionsdagbokspost... Var god v&auml;nta..." );
		}
	}
	deletemotiondiarycatrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
//	showAlertMessage( paramurl );
	deletemotiondiarycatrequest.send( paramurl );
}

function confirmDeleteMotiondiaryRecord( motiondiarytablename, motiondiaryrecordid, returnvalue ) {
	if( returnvalue == "" ) {
		paramvarvalue1 = motiondiarytablename; paramvarvalue2 = motiondiaryrecordid; callbackfunctionname = "confirmDeleteMotiondiaryRecord";
		confirmquestion = "&Auml;r du s&auml;ker p&aring; att du vill ta bort motionsdagboksposten?";
		paramurl = "paramvarvalue1=" + paramvarvalue1 + 
						"&paramvarvalue2=" + paramvarvalue2 + 
						"&callbackfunctionname=" + callbackfunctionname + 
						"&confirmquestion=" + escape( confirmquestion );
		getPage( "confirmpage.php", "messagespot", paramurl );
	}
	else {
		if( returnvalue == 1 ) {
			deleteMotiondiaryRecord( motiondiarytablename, motiondiaryrecordid );
		}
		else {
		}
		emptyElement( "messagespot" );
	}
}

// Funktion som visar en motionsdagbokspost.
function showMotiondiaryRecord( motiondiarytablename, motiondiaryrecordid ) {
	getPage3( createRequest(), "showmotiondiaryrecorddetails.php", submessageelementname, subcontentelementname, "motiondiarytablename=" + motiondiarytablename + "&motiondiaryrecordid=" + motiondiaryrecordid, '', '' );
}

// Funktioner som jobbar direkt med själva motionsdagböckerna //

// Uppdatera gränssnittet vid ändringar för själva motionsdagboken
function doUpdateMotiondiaryAfterwork( motiondiarytablename ) {
	emptySubMessagespot( 3000 );
	emptyElement( subcontentelementname );
	getPage3( createRequest(), "mymotiondiarylist.php", messageelementname, "usermotiondiarylist", '', '', '' );
	getPage3( createRequest(), "getmotiondiary.php", messageelementname, contentelementname, "motiondiarytablename=" + motiondiarytablename, '', '' );
}

function deleteMotiondiary( userid, motiondiaryid ) {
	paramurl = "userid=" + userid + "&motiondiaryid=" + motiondiaryid + "&optype=5";
//	showAlertMessage( paramurl );
	var deletemotiondiarycatrequest = createRequest();
	var url = "usermotiondiarieshandler.php";
	deletemotiondiarycatrequest.open( "POST", url, true );
	deletemotiondiarycatrequest.onreadystatechange = function doDOMChanges() {
		if( deletemotiondiarycatrequest.readyState == 4 ) {
			if( deletemotiondiarycatrequest.status == 200 ) {
				var response = deletemotiondiarycatrequest.responseText;
				if( response == 1 ) {
					updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Motionsdagboken togs bort." );
					emptyMainMessagespot( 3000 );
					resetJustWorkspace();
					getPage3( createRequest(), "mymotiondiarylist.php", submessageelementname, "usermotiondiarylist", '', '', '' );
				}
				else {
					updateElement( messageelementname, "Motionsdagboken kunde inte tas bort.<BR>Felmeddelande: " + response );
				}
			}
			else {
				updateElement( messageelementname, "Det uppstod ett fel n&auml;r motionsdagboken skulle tas bort!<BR>Felkod: " + deletemotiondiarycatrequest.status );
			}
		}
		else {
			updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Tar bort motionsdagbok... Var god v&auml;nta..." );
		}
	}
	deletemotiondiarycatrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	deletemotiondiarycatrequest.send( paramurl );
}

function confirmDeleteMotiondiary( userid, motiondiaryid, returnvalue ) {
	resetSubWorkspace();
	if( returnvalue == "" ) {
		paramvarvalue1 = userid; paramvarvalue2 = motiondiaryid; callbackfunctionname = "confirmDeleteMotiondiary";
		confirmquestion = "&Auml;r du s&auml;ker p&aring; att du vill ta bort motionsdagboken och alla poster i den?";
		paramurl = "paramvarvalue1=" + paramvarvalue1 + 
						"&paramvarvalue2=" + paramvarvalue2 + 
						"&callbackfunctionname=" + callbackfunctionname + 
						"&confirmquestion=" + escape( confirmquestion );
		getPage( "confirmpage.php", messageelementname, paramurl );
	}
	else {
		if( returnvalue == 1 ) {
			deleteMotiondiary( userid, motiondiaryid );
		}
		else { }
		resetJustMessagespace();
	}
}

// Skapar ny motionsdagbok åt den inloggade användaren
function saveMotiondiaryChanges( userid, diaryid, motiondiarytablename ) {
	var savemotiondiaryrequest = createRequest();
	admincommentsoption = $F( "admincommentsoption" );
	firstname = $F( "firstname" ).strip(); lastname = $F( "lastname" ).strip(); birthdate = $F( "birthdate" ).strip();
	paramurl = "firstname=" + escape( firstname ) + "&lastname=" + escape( lastname ) + "&birthdate=" + escape( birthdate ) + 
	"&admincommentsoption=" + admincommentsoption + "&diaryid=" + diaryid + "&pagetype=2&optype=3";
//	alert( paramurl );
 	if( firstname == "" ) { errormessages.push( "Du gl&ouml;mde att ange f&ouml;rnamnet p&aring; personen som den nya motionsdagboken avser." ); }
 	if( lastname == "" ) { errormessages.push( "Du gl&ouml;mde att ange efternamnet p&aring; personen som den nya motionsdagboken avser." ); }
 	if( !validateDate( birthdate ) ) { errormessages.push( "F&ouml;delsedatumet du angav &auml;r inte ett giltigt datum." ); }

 	if( errormessages.length > 0 ) {
		printErrors( messageelementname );
	}
	else {
		var url = "usermotiondiarieshandler.php";
		savemotiondiaryrequest.open( "POST", url, true );
		savemotiondiaryrequest.onreadystatechange = function doDOMChanges() {
			if( savemotiondiaryrequest.readyState == 4 ) {
				if( savemotiondiaryrequest.status == 200 ) {
					emptyElement( messageelementname );
					response = savemotiondiaryrequest.responseText;
					if( response == 1 ) {
						updateElement( submessageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> &Auml;ndringarna i motionsdagboken sparades." );
						doUpdateMotiondiaryAfterwork( motiondiarytablename );
					}
					else {
						updateElement( messageelementname, "Motionsdagbokens &auml;ndringar kunde inte sparas.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( messageelementname, "Det uppstod ett fel n&auml;r &auml;ndringarna i motionsdagboken skulle sparas!<BR>Felkod: " + savemotiondiaryrequest.status );
				}
			}
			else {
				updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Sparar &auml;ndringarna i motionsdagboken... Var god v&auml;nta..." );
			}
		}
		savemotiondiaryrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		savemotiondiaryrequest.send( paramurl );
	}
}

function showMotiondiaryPropertiesEditForm( userid, diaryid ) {
	getPage3( createRequest(), "motiondiaryeditpropertiesform.php", submessageelementname, subcontentelementname, "userid=" + userid + "&diaryid=" + diaryid, '', '' );
}

// Visar den aktuella motionsdagboken
function showMotiondiary( motiondiarytablename ) {
	resetWorkspace(); resetSubWorkspace();
	getPage3( createRequest(), "getmotiondiary.php", messageelementname, contentelementname, "motiondiarytablename=" + motiondiarytablename, '', '' );
}

function setAdmincommentsoption( optvalue ) { $( "admincommentsoption" ).value = optvalue; }

// Skapar ny motionsdagbok åt den inloggade användaren
function createNewMotiondiary() {
	var elementname = "mainmessagespot";
	var newMotiondiaryrequest = createRequest();
	admincommentsoption = $F( "admincommentsoption" );
	firstname = $F( "firstname" ).strip();
	lastname = $F( "lastname" ).strip();
	birthdate = $F( "birthdate" ).strip();
	paramurl = "firstname=" + escape( firstname ) + "&lastname=" + escape( lastname ) + "&birthdate=" + escape( birthdate ) + "&admincommentsoption=" + admincommentsoption + "&pagetype=2&optype=1";
//	alert( paramurl );
 	if( firstname == "" ) { errormessages.push( "Du gl&ouml;mde att ange f&ouml;rnamnet p&aring; personen som den nya motionsdagboken avser." ); }
 	if( lastname == "" ) { errormessages.push( "Du gl&ouml;mde att ange efternamnet p&aring; personen som den nya motionsdagboken avser." ); }
 	if( !validateDate( birthdate ) ) { errormessages.push( "F&ouml;delsedatumet du angav &auml;r inte ett giltigt datum." ); }

 	if( errormessages.length > 0 ) {
		printErrors( elementname );
	}
	else {
		var url = "usermotiondiarieshandler.php";
		newMotiondiaryrequest.open( "POST", url, true );
		newMotiondiaryrequest.onreadystatechange = function doDOMChanges() {
			if( newMotiondiaryrequest.readyState == 4 ) {
				if( newMotiondiaryrequest.status == 200 ) {
					emptyElement( elementname );
					response = newMotiondiaryrequest.responseText;
					if( response == 1 ) {
						updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Den nya motionsdagboken skapades." );
						emptyMainMessagespot( 2000 );
						resetJustWorkspace();
						getPage3( createRequest(), "mymotiondiarylist.php", messageelementname, "usermotiondiarylist", '', '', '' );
					}
					else {
						updateElement( elementname, "Den nya motionsdagboken kunde inte skapas.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( elementname, "Det uppstod ett fel n&auml;r den nya motionsdagboken skulle skapas!<BR>Felkod: " + newMotiondiaryrequest.status );
				}
			}
			else {
				updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Skapar ny motionsdagbok... Var god v&auml;nta..." );
			}
		}
		newMotiondiaryrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		newMotiondiaryrequest.send( paramurl );
	}
}

