var contentelementname = "maincontentspot";
var messageelementname = "mainmessagespot";
// Funktion som visar felmeddelanden
var errormessages = new Array();
function showErrors() {
 	if( errormessages.length > 0 ) {
 		mes = "Följande fel inträffade:<BR><BR>";
 		for( i = 0; i < errormessages.length; i++ ) {
 			mes += (i+1) + ": " + errormessages[i] + "<BR>";
 		}
 		showAlertMessage( mes );
		errormessages = new Array();
 	}
}

function printErrors( elementname ) {
 	if( errormessages.length > 0 ) {
 		mes = "<SPAN CLASS=label>F&ouml;ljande fel intr&auml;ffade:</SPAN><BR><BR>";
 		for( i = 0; i < errormessages.length; i++ ) {
 			mes += "<SPAN CLASS=label>" + (i+1) + ":</SPAN> " + errormessages[i] + "<BR>";
 		}
 		mes += "<BR>";
 		updateElement( elementname, mes );
		errormessages = new Array();
 	}
}

// Tömmer elementet med namnet mainmessagespot i aktuellt dokuments parent efter angivet antal millisekunder. 
function emptyParentMainMessageSpot( milliseconds ) { setTimeout( "parent.$( 'mainmessagespot' ).update()", milliseconds ); }
// Tömmer elementet med namnet maincontentspot i aktuellt dokuments parent efter angivet antal millisekunder. 
function emptyParentMainContentSpot( milliseconds ) { setTimeout( "parent.$( 'maincontentspot' ).update()", milliseconds ); }

// Tömmer elementet med namnet contentspot i aktuellt dokument efter angivet antal millisekunder. 
function emptyOwnContentSpot( milliseconds ) { setTimeout( "$( 'contentspot' ).update()", milliseconds ); }
// Tömmer elementet med namnet messagespot i aktuellt dokument efter angivet antal millisekunder. 
function emptyOwnMessageSpot( milliseconds ) { setTimeout( "$( 'messagespot' ).update()", milliseconds ); }


function resetRecipeWorkplace() {
	emptyElement( messageelementname );
	emptyElement( contentelementname );
	recipecatviewframe.location="getrecipecategorie.php";
}

function resetParentWorkplace() {
	emptyElement( parent.$( "mainmessagespot" ) );
	emptyElement( parent.$( "maincontentspot" ) );
	parent.recipecatviewframe.location = "getrecipecategorie.php";
}

function foo( milliseconds ) {
	alert( "foo" );
}

// Uppdatera gränssnittet efter uppladdning av ett nytt recept
function updateViewAfterRecipeUpload( userid, catid ) {
	emptyParentMainContentSpot( 2000 );
	parent.userrecipelistframe.location="myrecipelist.php";
	getPage3( createRequest(), "getrecipenames.php", parent.$( "mainmessaspot" ), parent.recipecatviewframe.$( "recipecatnamesspot" ), "userid=" + userid + "&catid=" + catid, '', '' );
}

// Uppdatera gränssnittet efter uppladdning av ett nytt recept
function updateViewAfterRecipeModify( userid, catid ) {
	emptyParentMainContentSpot( 4000 );
	parent.userrecipelistframe.location="myrecipelist.php";
	getPage3( createRequest(), "getrecipenames.php", parent.$( "mainmessaspot" ), parent.recipecatviewframe.$( "recipecatnamesspot" ), "userid=" + userid + "&catid=" + catid, '', '' );
}


function deleteRecipe( recipetablename, recipeid ) {
	var elementname = parent.$( "mainmessagespot" );
	paramurl = "recipetablename=" + recipetablename + "&recipeid=" + recipeid + "&optype=4";
	emptyElement( "recipecatpicuploadformspot" );
	var deleterecipecatrequest = createRequest();
	var url = "userrecipeshandler.php";
	deleterecipecatrequest.open( "POST", url, true );
	deleterecipecatrequest.onreadystatechange = function doDOMChanges() {
		if( deleterecipecatrequest.readyState == 4 ) {
			if( deleterecipecatrequest.status == 200 ) {
				var response = deleterecipecatrequest.responseText;
				if( response == 1 ) {
					elementid =  "recipe_" + recipeid;
//					alert( elementid );
					removeElement( elementid );
					parent.userrecipelistframe.location="myrecipelist.php";
					updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Receptet togs bort." );
					emptyParentMainMessageSpot( 3000 );
				}
				else {
					updateElement( elementname, "Receptet kunde inte tas bort.<BR>Felmeddelande: " + response );
				}
			}
			else {
				updateElement( elementname, "Det uppstod ett fel n&auml;r receptet skulle tas bort!<BR>Felkod: " + deleterecipecatrequest.status );
			}
		}
		else {
			updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Tar bort recept... Var god v&auml;nta..." );
		}
	}
	deleterecipecatrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
//	showAlertMessage( paramurl );
	deleterecipecatrequest.send( paramurl );
}

function confirmDeleteRecipe( recipetablename, recipeid, returnvalue ) {
	parent.$( 'mainmessagespot' ).update();
	if( returnvalue == "" ) {
		paramvarvalue1 = recipetablename; paramvarvalue2 = recipeid; callbackfunctionname = "confirmDeleteRecipe";
		confirmquestion = "&Auml;r du s&auml;ker p&aring; att du vill ta bort receptet?";
		paramurl = "paramvarvalue1=" + paramvarvalue1 + 
						"&paramvarvalue2=" + paramvarvalue2 + 
						"&callbackfunctionname=" + callbackfunctionname + 
						"&confirmquestion=" + escape( confirmquestion );
		getPage( "confirmpage.php", "messagespot", paramurl );
	}
	else {
		if( returnvalue == 1 ) {
			deleteRecipe( recipetablename, recipeid );
		}
		else {
		}
		emptyElement( "messagespot" );
	}
}

function deleteRecipeCategorie( userid, recipecatid ) {
	var elementname = parent.$( "mainmessagespot" );
	paramurl = "userid=" + userid + "&recipecatid=" + recipecatid + "&optype=5";
//	showAlertMessage( paramurl );
	emptyElement( "recipecatpicuploadformspot" );
	var deleterecipecatrequest = createRequest();
	var url = "userrecipeshandler.php";
	deleterecipecatrequest.open( "POST", url, true );
	deleterecipecatrequest.onreadystatechange = function doDOMChanges() {
		if( deleterecipecatrequest.readyState == 4 ) {
			if( deleterecipecatrequest.status == 200 ) {
				var response = deleterecipecatrequest.responseText;
				if( response == 1 ) {
					updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Receptkategorin togs bort." );
					parent.recipecatviewframe.location="getrecipecategorie.php";
					parent.userrecipelistframe.location="myrecipelist.php";
				}
				else {
					updateElement( elementname, "Receptkategorin kunde inte tas bort.<BR>Felmeddelande: " + response );
				}
			}
			else {
				updateElement( elementname, "Det uppstod ett fel n&auml;r receptkategorin skulle tas bort!<BR>Felkod: " + deleterecipecatrequest.status );
			}
		}
		else {
			updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Tar bort receptkategori... Var god v&auml;nta..." );
		}
	}
	deleterecipecatrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	deleterecipecatrequest.send( paramurl );
}

function confirmDeleteRecipeCategorie( userid, recipecatid, returnvalue ) {
	parent.$( 'mainmessagespot' ).update();
	if( returnvalue == "" ) {
		paramvarvalue1 = userid; paramvarvalue2 = recipecatid; callbackfunctionname = "confirmDeleteRecipeCategorie";
		confirmquestion = "&Auml;r du s&auml;ker p&aring; att du vill ta bort receptkategorin och alla recept i den?";
		paramurl = "paramvarvalue1=" + paramvarvalue1 + 
						"&paramvarvalue2=" + paramvarvalue2 + 
						"&callbackfunctionname=" + callbackfunctionname + 
						"&confirmquestion=" + escape( confirmquestion );
		getPage( "confirmpage.php", "messagespot", paramurl );
	}
	else {
		if( returnvalue == 1 ) {
			deleteRecipeCategorie( userid, recipecatid );
		}
		else {
		}
		emptyElement( "messagespot" );
	}
}

function renameRecipecat() {
	userid = $F( "userid" );
	recipecatid = $F( "recipecatid" );
	recipecatname = $F( "recipecatname" );
 	if( recipecatname == "" ) { errormessages.push( "Du gl&ouml;mde att ange det nya namnet p&aring; receptkategorin." ); }
	var paramurl = "userid=" + userid + "&recipecatid=" + recipecatid + "&recipecatname=" + escape( recipecatname ) + "&optype=3";
//	alert( paramurl );

 	if( errormessages.length > 0 ) {
 		printErrors( elementname );
 	}
	else {
		var url = "userrecipeshandler.php";
		var recipecatrenamerequest = createRequest();
		recipecatrenamerequest.open( "POST", url, true );
		recipecatrenamerequest.onreadystatechange = function doDOMChanges() {
			if( recipecatrenamerequest.readyState == 4 ) {
				if( recipecatrenamerequest.status == 200 ) {
					var response = recipecatrenamerequest.responseText;
					if( response == 1 ) {
						updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Receptkategorin d&ouml;ptes om till: " + recipecatname + "." );
						emptyElement( contentelementname );
						recipecatviewframe.location="getrecipecategorie.php?userid=" + userid + "&catid=" + recipecatid + "&pagetype=2&optype=3";
						userrecipelistframe.location="myrecipelist.php";
					}
					else {
						updateElement( messageelementname, "Namnet p&aring; receptkategorin kunde inte &auml;ndras.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( messageelementname, "Det uppstod ett fel n&auml;r namnet p&aring; receptkategorin skulle $auml;ndras.<BR>Felkod: " + recipecatrenamerequest.status );
				}
			}
			else {
				updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Byter namn p&aring; receptkategori... Var god v&auml;nta..." );
			}
		}
		recipecatrenamerequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		recipecatrenamerequest.send( paramurl );
	}
}

function cancelRenameRecipecat() { emptyElement( messageelementname ); emptyElement( contentelementname ); }

function showRecipeCategorieRenameForm( userid, recipecatid ) {
	getPage3( createRequest(), "recipecatrenameform.php", parent.$( "mainmessagespot" ), parent.$( "maincontentspot" ), "userid=" + userid + "&recipecatid=" + recipecatid, '', '' );
}

// Visar den aktuella receptkategorin
function showRecipecategorie( userid, catid ) {
	resetParentWorkplace();
	parent.recipecatviewframe.location="getrecipecategorie.php?userid=" + userid + "&catid=" + catid;
}

// Funktion som visar formuläret som används för att redigera ett recept.
function showRecipe( recipetablename, recipeid ) {
	getPage3( createRequest(), "showrecipedetails.php", parent.$( "mainmessagespot" ), parent.$( "maincontentspot" ), "recipetablename=" + recipetablename + "&recipeid=" + recipeid, '', '' );
}

// Funktion som visar formuläret som används för att redigera ett recept.
function showModifyRecipeForm( recipetablename, recipeid, recipecatid ) {
	updateElement( parent.$( "maincontentspot" ), "<IFRAME FRAMEBORDER=0 SRC=\"editrecipeform.php?recipetablename=" + recipetablename + "&recipeid=" + recipeid +"&recipecatid=" + recipecatid + 
															"\"  WIDTH=450 HEIGHT=300 NAME=editrecipeframe ID=editrecipeframe></IFRAME>" );
}

// Funktion som visar formuläret som används för att lägga till nytt recept.
function showNewRecipeForm( recipecatid ) {
	updateElement( parent.$( "maincontentspot" ), "<IFRAME FRAMEBORDER=0 SRC=\"newrecipeform.php?recipecatid=" + recipecatid + 
															"\"  WIDTH=460 HEIGHT=300 NAME=newrecipeframe ID=newrecipeframe></IFRAME>" );
}

// Skapar ny receptkategori åt den inloggade användaren
function createNewRecipecategorie() {
	var elementname = "mainmessagespot";
	var newreciperequest = createRequest();
	headline = $F( "headline" ).strip();
	paramurl = "headline=" + escape( headline ) + "&pagetype=2&optype=1";
 	if( headline == "" ) { errormessages.push( "Du gl&ouml;mde att ange namnet p&aring; den nya receptkategorin." ); }

 	if( errormessages.length > 0 ) {
		printErrors( elementname );
	}
	else {
		var url = "userrecipeshandler.php";
		newreciperequest.open( "POST", url, true );
		newreciperequest.onreadystatechange = function doDOMChanges() {
			if( newreciperequest.readyState == 4 ) {
				if( newreciperequest.status == 200 ) {
					emptyElement( elementname );
					response = newreciperequest.responseText;
					if( response == 1 ) {
						updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Den nya receptkategorin skapades." );
						setTimeout( "emptyElement( '" + elementname + "')", 2000);
						emptyElement( "maincontentspot" );
						userrecipelistframe.location="myrecipelist.php";
					}
					else {
						updateElement( elementname, "Den nya receptkategorin kunde inte skapas.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( elementname, "Det uppstod ett fel n&auml;r den nya receptkategorin skulle skapas!<BR>Felkod: " + newreciperequest.status );
				}
			}
			else {
				updateElement( elementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Skapar ny receptkategori... Var god v&auml;nta..." );
			}
		}
		newreciperequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		newreciperequest.send( paramurl );
	}
}

function deleteRecipePic( recipetablename, recipeid ) {
	if( confirm( "Är du säker på att du vill ta bortreceptbilden?" ) ) {
		var path = "uploadedpics/useralbumpics/";
		paramurl = "recipetablename=" + recipetablename + "&recipeid=" + recipeid + "&optype=6";
		
		var dumprecipepicrequest = createRequest();
		var url = "userrecipeshandler.php";
		dumprecipepicrequest.open( "POST", url, true );
		dumprecipepicrequest.onreadystatechange = function doDOMChanges() {
			if( dumprecipepicrequest.readyState == 4 ) {
				if( dumprecipepicrequest.status == 200 ) {
					var response = dumprecipepicrequest.responseText;
					if( response == 1 ) {
						emptyElement( "recipepicspot" );
						emptyElement( messageelementname );
					}
					else {
						updateElement( messageelementname, "Bilden kunde inte tas bort.<BR>Felmeddelande: " + response );
					}
				}
				else {
					updateElement( messageelementname, "Det uppstod ett fel när bilden skulle tas bort!<BR>Felkod: " + dumprecipepicrequest.status );
				}
			}
			else {
				updateElement( messageelementname, "<IMG SRC=pics/loading.gif BORDER=0 ALIGN=BOTTOM STYLE=\"text-align:bottom;\"> Tar bort receptbild... Var god v&auml;nta..." );
			}
		}
		dumprecipepicrequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		dumprecipepicrequest.send( paramurl );
	}
}

