#define NONEISLAND_TEXTURE_NAME	"interfaces\empty_sea.tga"
#define MAX_SAVE_GAME_LIMIT 40 //Draksen; was 20

int nCurScroll;
bool bThisSave;

aref scrshot;

bool bSaveConfirm;
bool bLoadConfirm;
bool bEnableNewSave;

int FreeSpace;

void InitInterface_B(string iniName, bool isSave)
{
	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	bThisSave = isSave;
	if(bThisSave)	GameInterface.title = "titleSave";
	else	GameInterface.title = "titleLoad";

	FindScrshotClass();
	SetEventHandler("GetInterfaceTexture","NewInterfaceTexture",0);
	SetEventHandler("DelInterfaceTexture","ReleaseInterfaceTexture",0);

	if(GetTargetPlatform()=="xbox")
		FreeSpace = SendMessage(&GameInterface, "l", MSG_INTERFACE_GET_FREE_SPACE);
	else
		FreeSpace = 50000;

	bEnableNewSave = FreeSpace>=MAX_SAVE_STORE_BLOCKS;

	FillScroll();
	SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);
	CreateExitString();//MAXIMUS: standard exit-string for exit-button

	CreateString(true,"SaveName","",FONT_NORMAL,COLOR_NORMAL,50,273,SCRIPT_ALIGN_LEFT,1.0);
	CreateString(true,"NoSave","",FONT_NORMAL,COLOR_NORMAL,320,475,SCRIPT_ALIGN_CENTER,0.8); // NK for nosave mod

	if (bThisSave)
		SetNodeUsing("LOAD_BUTTON",false);
	else
		SetNodeUsing("SAVE_BUTTON",false);

	CreateImage("face1","EMPTYFACE","emptyface",34,323,162,451);
	CreateImage("face2","EMPTYFACE","emptyface",183,323,311,451);
	CreateImage("face3","EMPTYFACE","emptyface",332,323,460,451);
	CreateImage("face4","EMPTYFACE","emptyface",481,323,609,451);

	if(GetTargetPlatform()=="xbox")
	{
		string sFreeSpace = "Free Space: "+FreeSpace+" blocks";
		if(FreeSpace>50000) sFreeSpace = "Free Space: 50000+ blocks";
		CreateString(true,"FreeSpace",sFreeSpace,FONT_NORMAL,COLOR_NORMAL,320,178,SCRIPT_ALIGN_CENTER,1.0);
		SetNodeUsing("FREESPACE_IMG",true);
	}

	SetNodeUsing("VERSIONMISMATCHBOX",false); // TIH Aug24'06
	SetNodeUsing("VERSIONMISMATCH",false); // TIH Aug24'06
	SetFormatedText("VERSIONMISMATCH",LanguageConvertString(tmpLangFileID,"Cannot Load")); // TIH Aug24'06
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("TEXTWINDOW1",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("DISKETS_PIC",false);
	SetSelectable("EXIT_BUTTON",true);
	infoWindowHide();
	SetNodeUsing("B_PROFILE",true);
	SetSelectable("B_PROFILE",true);
	SetCurrentNode("B_PROFILE");

	SetFormatedText("LOWSTORAGE_WINDOW",LanguageConvertString(tmpLangFileID,"save game limit"));
	LanguageCloseFile(tmpLangFileID);

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("frame","FrameProcess",1);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("SavePress","ProcessSaveConfirm",0);
	SetEventHandler("LoadPress","ProcessLoadConfirm",0);
	SetEventHandler("DelPress","ProcessDelete",0);
	SetEventHandler("DelNo","ProcessDelete_no",0);
	SetEventHandler("DelYes","ProcessDelete_yes",0);
	SetEventHandler("bleft","ButtonToLeft",0);
	SetEventHandler("bright","ButtonToRight",0);
	SetEventHandler("slistdown","SetFirstButton",0);
	SetEventHandler("ShowInfo","infoWindowShow",0);
	SetEventHandler("HideInfo","infoWindowHide",0);
	SetEventHandler("ievnt_command","ProcessCommand",0);
}

object LocDirectory;

void ProcessCommand()
{
	string comName = GetEventData();
	string nodName = GetEventData();
	DeleteAttribute(&LocDirectory, "");

	LocDirectory.dir = DEFAULT_PATH;
	LocDirectory.mask = "*";
	CreateEntity(&LocDirectory,"FINDFILESINTODIRECTORY");
	DeleteClass(&LocDirectory);
	aref arList;
	makearef(arList, LocDirectory.filelist);
	for(int i=0; i<GetAttributesNum(arList); i++)
	{
		if(i>16) continue;
		aref arCur = GetAttributeN(arList, i);
		string fname = GetAttributeValue(arCur);
		if(HasSubStr(fname,".")) continue;
		string prfInfo = fname;
		string curNodNum = "PROFPIC"+sti(i-1);
		if(nodName==curNodNum && comName=="click")
		{
			PlayerProfile.name = prfInfo;
			PlayerProfile.Path = "SAVE\"+fname;
			&GameInterface.SavePath = "SAVE\"+fname;
			LaunchLoadGame();
			IDoExit(RC_INTERFACE_SAVELOAD_EXIT);
			EndCancelinterface(true);
//			PostEvent("exitCancel",100);
//			infoWindowHide();
//			FillScroll();
//			GetCurSaveName();
			return;
		}
	}
}

void infoWindowShow()
{
	SetNodeUsing("PROFRECT",true);
	SetSelectable("EXIT_BUTTON",false);
	SetSelectable("SAVESLIST",false);
	SetSelectable("LEFTSCROLLBUTTON",false);
	SetSelectable("RIGHTSCROLLBUTTON",false);
	SetSelectable("SAVE_BUTTON",false);
	SetSelectable("LOAD_BUTTON",false);
	SetSelectable("DEL_BUTTON",false);
	SetSelectable("CANCEL_BUTTON",false);
	SetSelectable("B_PROFILE",false);
	SetNodeUsing("PROFTEXT1",true);
	SetNodeUsing("PROFTEXT2",true);
	SetNodeUsing("PROFTEXT3",true);
	SetCurrentNode("PROFTEXT1");
	string prfInfo1 = "";
	string prfInfo2 = "";
	string prfInfo3 = "";
	int i, j;
	string empty = "";

	DeleteAttribute(&LocDirectory, "");

	LocDirectory.dir = DEFAULT_PATH;
	LocDirectory.mask = "*";
	CreateEntity(&LocDirectory,"FINDFILESINTODIRECTORY");
	DeleteClass(&LocDirectory);
	aref arList;
	makearef(arList, LocDirectory.filelist);
	for(i=0; i<GetAttributesNum(arList); i++)
	{
		if(i>16) continue;
		aref arCur = GetAttributeN(arList, i);
		string fname = GetAttributeValue(arCur);
		if(HasSubStr(fname,".")) continue;
		if(i<7)
		{
			empty = "";
			for(j=0; j<sti(23-strlen(fname)); j++) { empty += " "; }
			prfInfo1 += fname+empty+"     ";
		}
		SetFormatedText("PROFTEXT1",prfInfo1);
		if(i>=7 && i<=11)
		{
			empty = "";
			for(j=0; j<sti(23-strlen(fname)); j++) { empty += " "; }
			prfInfo2 += fname+empty+"     ";
		}
		SetFormatedText("PROFTEXT2",prfInfo2);
		if(i>11 && i<=16)
		{
			empty = "";
			for(j=0; j<sti(23-strlen(fname)); j++) { empty += " "; }
			prfInfo3 += fname+empty+"     ";
		}
		SetFormatedText("PROFTEXT3",prfInfo3);
		SetNewPicture("PROFPIC_"+sti(i-1),"interfaces\installedflag.tga");
	}
}

void infoWindowHide()
{
	SetSelectable("EXIT_BUTTON",true);
	SetSelectable("SAVESLIST",true);
	SetSelectable("LEFTSCROLLBUTTON",true);
	SetSelectable("RIGHTSCROLLBUTTON",true);
	SetSelectable("SAVE_BUTTON",true);
	SetSelectable("LOAD_BUTTON",true);
	SetSelectable("DEL_BUTTON",true);
	SetSelectable("CANCEL_BUTTON",true);
	SetSelectable("B_PROFILE",true);
	SetNodeUsing("PROFRECT",false);
	SetNodeUsing("PROFTEXT1",false);
	SetNodeUsing("PROFTEXT2",false);
	SetNodeUsing("PROFTEXT3",false);

	DeleteAttribute(&LocDirectory, "");

	LocDirectory.dir = DEFAULT_PATH;
	LocDirectory.mask = "*";
	CreateEntity(&LocDirectory,"FINDFILESINTODIRECTORY");
	DeleteClass(&LocDirectory);
	aref arList;
	makearef(arList, LocDirectory.filelist);
	for(int i=0; i<GetAttributesNum(arList); i++)
	{
		if(i>16) continue;
		aref arCur = GetAttributeN(arList, i);
		string fname = GetAttributeValue(arCur);
		if(HasSubStr(fname,".")) continue;
		SetNewPicture("PROFPIC_"+sti(i-1),"");
	}
	SetCurrentNode("B_PROFILE");
}

void FrameProcess()
{
	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	// scheffnow -->
	bool bNightmareMode = false; // NK defaults to off, needed due to CA below
	ref PMainCharacter = GetMainCharacter();
	if(CheckAttribute(PMainCharacter,"NoSave.CarryRelic") && PMainCharacter.NoSave.CarryRelic != "none") { 
		bNightmareMode = true;
		GameInterface.strings.NoSave = LanguageConvertString(tmpLangFileID,"You can't save while carrying a holy relic! Give it to a monk first."); // NK
	} 
	// scheffnow <--
// KK -->
	if (CheckAttribute(PMainCharacter, "NoSave.Resurrection")) {
		bNightmareMode = true;
		GameInterface.strings.NoSave = LanguageConvertString(tmpLangFileID, "You can't save until") + " " + ResurrectingOfficiant.Name + " " + ResurrectingOfficiant.Lastname + " " + LanguageConvertString(tmpLangFileID, "leaves.");
	}
// <-- KK

	if( nCurScroll != sti(GameInterface.saveslist.current) )
	{
		nCurScroll = sti(GameInterface.saveslist.current);
		string attrName = "pic" + (nCurScroll+1);
		bool bFilePresent = false;
		bool bCorrupted = false;
		
		if (CheckAttribute(&GameInterface,"saveslist."+attrName+".descr") && GameInterface.saveslist.(attrName).descr != "")
		{
			bFilePresent = true;
			if (GameInterface.saveslist.(attrName).savestr == "" && GameInterface.saveslist.(attrName).name1 != "quicksave") // allow loading quicksave. NK 05-04-14
				{ bCorrupted = true; /*aref artmp; makearef(artmp, GameInterface.saveslist.(attrName)); dumpattributes(artmp);*/ }
		}

		SetSelectable("SAVE_BUTTON",true);
		SetSelectable("DEL_BUTTON",bFilePresent);
		SetNodeUsing("VERSIONMISMATCHBOX",false); // TIH Aug24'06
		SetNodeUsing("VERSIONMISMATCH",false); // TIH Aug24'06
		
		if(bFilePresent)
		{
			if( !bThisSave )
				SetSelectable("LOAD_BUTTON",!bCorrupted);
		}
		else {
			SetSelectable("SAVE_BUTTON",bEnableNewSave);
		}

		if( bFilePresent )
		{
			if ( bCorrupted )
				SettingDataForSave("");
			else
				SettingDataForSave( GameInterface.saveslist.(attrName).descr );
		}
		else
		{
			if(bThisSave)
				SettingDataForSave("NewSave");
			else
			{
				SetSelectable("LOAD_BUTTON",false);
				SettingDataForSave("");
			}
		}

		ShowLowStorage( !CheckAvailablePlace() );

		if( CheckLimited() ) {
			SetNodeUsing("LOWSTORAGE_WINDOW",false);
		} else {
			SetNodeUsing("LOWSTORAGE_WINDOW",true);
			DisableString("SaveName");
			SetSelectable("SAVE_BUTTON",false);
		}
		// scheffnow -->
		if(bNightmareMode == 1) 
		{
			SetSelectable("SAVE_BUTTON",false);
		}
		// scheffnow <--
		if( !GetSelectable(GetCurrentNode()) ) SetCurrentNode("SAVESLIST");
	}
}

void FillScroll()
{
    int i=0;
    string attributeName;

	nCurScroll = -1;
	GameInterface.SavePath = DEFAULT_PATH;
	GameInterface.saveslist.current = 0;
	GameInterface.saveslist.BadPicture1 = "interfaces\Disketa_Empty.tga";
	GameInterface.saveslist.ImagesGroup.t0 = "CORRUPTSAVE";
	GameInterface.saveslist.NotUsed = 5;

	int nSaveNum=0;
	
	if(bThisSave)
	{
		GameInterface.saveslist.pic1.name1 = "newsave";
		
		if( !bEnableNewSave ) {
			GameInterface.saveslist.pic1.str1 = "Low Storage";
		}

		GameInterface.saveslist.pic1.saveSize = 0;
		i++;
	}
	
	string saveName;
	string saveString;
	int nSaveSize;
//	if(GetCurSaveName()!="quicksave"){
    while( SendMessage(&GameInterface,"llee",MSG_INTERFACE_SAVE_FILE_FIND,nSaveNum,&saveName,&nSaveSize)!=0)
    {
		nSaveNum++;
		attributeName = "pic" + (i+1);
		GameInterface.saveslist.(attributeName).img1 = "corruptsave";
		GameInterface.saveslist.(attributeName).tex1 = 0;
		GameInterface.saveslist.(attributeName).name1 = saveName;
		GameInterface.saveslist.(attributeName).descr = saveName;
		GameInterface.saveslist.(attributeName).savestr = "";
		GameInterface.saveslist.(attributeName).saveSize = nSaveSize;
		i++;
    }//}
	GameInterface.saveslist.ListSize = i;
}

void ProcessCancelExit()
{
	IDoExit(RC_INTERFACE_SAVELOAD_EXIT);
	if( !CheckAttribute(&InterfaceStates,"InstantExit") || sti(InterfaceStates.InstantExit)==false )
	{
		ReturnToMainMenu();
	}
}

void IDoExit(int exitCode)
{
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("frame","FrameProcess");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("SavePress","ProcessSaveConfirm");
	DelEventHandler("LoadPress","ProcessLoadConfirm");
	DelEventHandler("DelPress","ProcessDelete");
	DelEventHandler("DelNo","ProcessDelete_no");
	DelEventHandler("DelYes","ProcessDelete_yes");
	DelEventHandler("bleft","ButtonToLeft");
	DelEventHandler("bright","ButtonToRight");
	DelEventHandler("slistdown","SetFirstButton");
	DelEventHandler("GetInterfaceTexture","NewInterfaceTexture");
	DelEventHandler("DelInterfaceTexture","ReleaseInterfaceTexture");
	DelEventHandler("ShowInfo","infoWindowShow");
	DelEventHandler("HideInfo","infoWindowHide");
	DelEventHandler("ievnt_command","ProcessCommand");

	SetNodeUsing("SAVESLIST",false);

    interfaceResultCommand = exitCode;
	if( CheckAttribute(&InterfaceStates,"InstantExit") && sti(InterfaceStates.InstantExit)==true ) {
		EndCancelInterface(false);
	} else {
		EndCancelInterface(true);
	}
}

void ProcessSaveConfirm()
{
	string attributeName = "saveslist.pic" + (nCurScroll+1);
	if( CheckAttribute(&GameInterface,attributeName+".descr") )
	{
		bSaveConfirm = true;
		bLoadConfirm = false;

		SetNodeUsing("CONFIRM_RECTANGLE",true);
		SetNodeUsing("TEXTWINDOW",true);
	    SetNodeUsing("TEXTWINDOW1",true); //added by MAXIMUS
		SetNodeUsing("CONFIRM_YES_BUTTON",true);
		SetNodeUsing("CONFIRM_NO_BUTTON",true);
		SetNodeUsing("DISKETS_PIC",true); //added by MAXIMUS
		SetNewPicture("DISKETS_PIC", "interfaces\Disketa_Save.tga"); //added by MAXIMUS
		SetCurrentNode("CONFIRM_YES_BUTTON"); // NK
		SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "CONFIRM_NO_BUTTON");
		SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "CONFIRM_YES_BUTTON");

		int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
		SetFormatedText("TEXTWINDOW",LanguageConvertString(tmpLangFileID,"Overwrite savefile confirm"));
		SetFormatedText("TEXTWINDOW1",GameInterface.strings.SaveNameConfirm); //added by MAXIMUS
		LanguageCloseFile(tmpLangFileID);
	}
	else
	{
		ProcessSave();
	}
}

void ProcessSave()
{
	string curSave = GetCurSaveName();
	if(nCurScroll>0)
	{
		SendMessage(&GameInterface,"ls",MSG_INTERFACE_DELETE_SAVE_FILE,curSave);
		nCurScroll = 0;
		curSave = GetCurSaveName();
	}

	ref PChar = GetMainCharacter();
	if(CheckAttribute(PChar,"systemStatus")) DeleteAttribute(PChar,"systemStatus");//MAXIMUS
	string locLabel = PChar.location;
	int locidx = FindLocation(PChar.location);
	
// KK -->
	if( locidx>=0 && CheckAttribute(&Locations[locidx],"id.label") ) { 
		if (CheckAttribute(Locations[locidx], "townsack")) {
			Preprocessor_Add("town_name", GetTownName(Locations[locidx].townsack));
			Preprocessor_Add("island_name", FindIslandName(GetIslandIDFromTown(Locations[locidx].townsack)));
		}
		locLabel = PreprocessText(Locations[locidx].id.label);
		Preprocessor_Remove("town_name");
		Preprocessor_Remove("island_name");
	} else {
		locLabel = "Open Sea";
	}
// <-- KK

	string sSaveDescriber = locLabel +"@"+ 
		GetFacePicture(GetOfficersIndex(PChar,0)) +"@"+ 
		GetFacePicture(GetOfficersIndex(PChar,1)) +"@"+ 
		GetFacePicture(GetOfficersIndex(PChar,2)) +"@"+ 
		GetFacePicture(GetOfficersIndex(PChar,3)) +"@"+ 
		GetStringTime(GetTime()) +"  "+ GetStringDate(GetDataYear(),GetDataMonth(),GetDataDay())
		+"@"+ IS_SGV
		; // TIH Aug24'06

	SetEventHandler("evntSave","SaveGame",1);

	if(!CheckAttribute(&GameInterface, "SavePath")) GameInterface.SavePath = DEFAULT_PATH;

	if(GetTargetPlatform()=="pc") {
		PostEvent("evntSave",0,"ss", GameInterface.SavePath+"\\"+curSave, sSaveDescriber);
	} else {
		PostEvent("evntSave",0,"ss", curSave, sSaveDescriber);
		Event("DoInfoShower","sl","save game",true);
	}

	IDoExit(RC_INTERFACE_SAVELOAD_EXIT);
}

void ProcessLoadConfirm()
{
	bLoadConfirm = true;
	bSaveConfirm = false;

	SetNodeUsing("CONFIRM_RECTANGLE",true);
	SetNodeUsing("TEXTWINDOW",true);
	SetNodeUsing("TEXTWINDOW1",true); //added by MAXIMUS
	SetNodeUsing("CONFIRM_YES_BUTTON",true);
	SetNodeUsing("CONFIRM_NO_BUTTON",true);
	SetNodeUsing("DISKETS_PIC",true); //added by MAXIMUS
	SetNewPicture("DISKETS_PIC", "interfaces\Disketa_Save.tga"); //added by MAXIMUS
	SetCurrentNode("CONFIRM_YES_BUTTON"); // NK
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "CONFIRM_NO_BUTTON");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "CONFIRM_YES_BUTTON");

	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	SetFormatedText("TEXTWINDOW",LanguageConvertString(tmpLangFileID,"Load game confirm"));
	SetFormatedText("TEXTWINDOW1",GameInterface.strings.SaveNameConfirm); //added by MAXIMUS
	LanguageCloseFile(tmpLangFileID);
}

void ProcessLoad()
{
	string sCurSave = GetCurSaveName();

	IDoExit(RC_INTERFACE_SAVELOAD_EXIT);
	ResetSound();

	SetEventHandler("evntLoad","LoadGame",1);

	if(!CheckAttribute(&GameInterface, "SavePath")) GameInterface.SavePath = DEFAULT_PATH;

	if(GetTargetPlatform()=="pc") {
		PostEvent("evntLoad",0,"s",GameInterface.SavePath+"\\"+sCurSave);
	} else {
		PostEvent("evntLoad",0,"s",sCurSave);
	}
	Event("evntPreLoad");
}

void ProcessDelete()
{
	bSaveConfirm = false;
	bLoadConfirm = false;

	SetNodeUsing("CONFIRM_RECTANGLE",true);
	SetNodeUsing("TEXTWINDOW",true);
	SetNodeUsing("TEXTWINDOW1", true); // KK
	SetNodeUsing("CONFIRM_YES_BUTTON",true);
	SetNodeUsing("CONFIRM_NO_BUTTON",true);
	SetNodeUsing("DISKETS_PIC",true); //added by MAXIMUS
	SetNewPicture("DISKETS_PIC", "interfaces\Disketa_Save.tga"); //added by MAXIMUS
	SetCurrentNode("CONFIRM_NO_BUTTON");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "CONFIRM_NO_BUTTON");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "CONFIRM_YES_BUTTON");

	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	SetFormatedText("TEXTWINDOW",LanguageConvertString(tmpLangFileID,"Delete savefile confirm"));
	SetFormatedText("TEXTWINDOW1",GameInterface.strings.SaveNameConfirm); //added by MAXIMUS
	LanguageCloseFile(tmpLangFileID);
}

void ProcessDelete_no()
{
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("TEXTWINDOW1",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("DISKETS_PIC",false);
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	if(	bSaveConfirm )	{SetCurrentNode("SAVE_BUTTON");}
	else {
		if(bLoadConfirm) {SetCurrentNode("LOAD_BUTTON");}
		else {SetCurrentNode("DEL_BUTTON");}
	}
}

void ProcessDelete_yes()
{
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("TEXTWINDOW1",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("DISKETS_PIC",false);
	SetCurrentNode("DEL_BUTTON");
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	if(	bSaveConfirm )
	{
		ProcessSave();
		return;
	}
	if( bLoadConfirm )
	{
		ProcessLoad();
		return;
	}

	string curSave = GetCurSaveName();
	SendMessage(&GameInterface,"ls",MSG_INTERFACE_DELETE_SAVE_FILE,curSave);
	DelFromScroll("SAVESLIST",nCurScroll);

	if( GetTargetPlatform()!="pc" )
	{
		FreeSpace = SendMessage(&GameInterface, "l", MSG_INTERFACE_GET_FREE_SPACE);
		string sFreeSpace = "Free Space: "+FreeSpace+" blocks";
		if(FreeSpace>50000) sFreeSpace = "Free Space: 50000+ blocks";
		GameInterface.strings.FreeSpace = sFreeSpace;
	}
	if( bThisSave && !bEnableNewSave && FreeSpace>=MAX_SAVE_STORE_BLOCKS )
	{
		bEnableNewSave = true;
		DeleteAttribute( &GameInterface,"saveslist.pic1.str1" );
		SendMessage(&GameInterface,"lsl", MSG_INTERFACE_SCROLL_CHANGE, "SAVESLIST", 0);
	}
	nCurScroll = -1;
}

string GetCurSaveName()
{
	string attrName = "pic"+(nCurScroll+1);
	if( CheckAttribute(&GameInterface,"saveslist."+attrName+".descr") &&
		GameInterface.saveslist.(attrName).descr != "" )	return GameInterface.saveslist.(attrName).descr;

	string curLocName = GetCurLocationName();
	string saveName;
	saveName = GetClampedSaveName(curLocName,0);

	int idx = 0;
	while( SendMessage(&GameInterface,"ls", MSG_INTERFACE_NEW_SAVE_FILE_NAME, saveName)==1 )
	{
		saveName = GetClampedSaveName(curLocName,idx+1);
		idx++;
	}

	return saveName;
}

void ButtonToLeft()
{
	if( GetCurrentNode() == "DEL_BUTTON" )
	{
		if(bThisSave) {
			if( GetSelectable("SAVE_BUTTON") ) SetCurrentNode("SAVE_BUTTON");
		} else {
			if( GetSelectable("LOAD_BUTTON") )	SetCurrentNode("LOAD_BUTTON");
		}
	}
	else
	{
		if( GetSelectable("DEL_BUTTON") ) SetCurrentNode("DEL_BUTTON");
		else
		{
			if(bThisSave) {
				if( GetSelectable("SAVE_BUTTON") )	SetCurrentNode("SAVE_BUTTON");
			} else {
				if( GetSelectable("LOAD_BUTTON") )	SetCurrentNode("LOAD_BUTTON");
			}
		}
	}
}

void ButtonToRight()
{
	if( GetCurrentNode() == "DEL_BUTTON" ) SetCurrentNode("CANCEL_BUTTON");
	else
	{
		if( GetSelectable("DEL_BUTTON") ) SetCurrentNode("DEL_BUTTON");
		else SetCurrentNode("CANCEL_BUTTON");
	}
}

void SetFirstButton()
{
	if( bThisSave )
	{
		if( GetSelectable("SAVE_BUTTON") ) {
			SetCurrentNode("SAVE_BUTTON");
			return;
		}
	}
	else
	{
		if( GetSelectable("LOAD_BUTTON") ) {
			SetCurrentNode("LOAD_BUTTON");
			return;
		}
	}
	if( GetSelectable("DELETE_BUTTON") ) SetCurrentNode("DELETE_BUTTON");
	else SetCurrentNode("CANCEL_BUTTON");
}

void SettingDataForSave(string saveFileName)
{
	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	string pic1,pic2,pic3,pic4;
	string locationStr, timeStr, tmpStr;
	string sgvStr = -1; // TIH Aug24'06
	int locidx;

	if( saveFileName=="" )
	{
		EnableString("SaveName");
		timeStr = GetCurSaveName();
		SendMessage(&GameInterface, "lse", MSG_INTERFACE_FILENAME2DATASTR, GetCurSaveName(), &timeStr);
		GameInterface.strings.SaveName = XI_ConvertString("Unable to load") + " " + timeStr;
		pic1 = -1;
		pic2 = -1;
		pic3 = -1;
		pic4 = -1;
		return;
	}

	EnableString("SaveName");
	tmpStr = "";
	if( saveFileName=="NewSave" )
	{
		if(GetMainCharacterIndex()>=0)
		{
			locidx = FindLocation(Characters[GetMainCharacterIndex()].location);
// KK -->
			if (locidx>=0 && CheckAttribute(&Locations[locidx],"id.label")) {
				if (CheckAttribute(&Locations[locidx],"townsack")) {
					Preprocessor_Add("town_name", GetTownName(Locations[locidx].townsack));
					Preprocessor_Add("island_name", GetIslandNameByLocationID(Locations[locidx].id));
					locationStr = GetIslandNameByLocationID(Locations[locidx].id) + ". " + Locations[locidx].id.label;
					Preprocessor_Remove("town_name");
					Preprocessor_Remove("island_name");
				} else {
					locationStr = "Open Sea";
				}
			}
// <-- KK
			pic1 = GetFacePicture(GetOfficersIndex(GetMainCharacter(),0)); // NK
			pic2 = GetFacePicture(GetOfficersIndex(GetMainCharacter(),1)); // NK
			pic3 = GetFacePicture(GetOfficersIndex(GetMainCharacter(),2)); // NK
			pic4 = GetFacePicture(GetOfficersIndex(GetMainCharacter(),3)); // NK
		}
		else
		{
			locationStr = "none";
			pic1 = -1;
			pic2 = -1;
			pic3 = -1;
			pic4 = -1;
		}
		timeStr = GetCurSaveName();
		SendMessage(&GameInterface, "lse", MSG_INTERFACE_FILENAME2DATASTR, LanguageConvertString(tmpLangFileID,"NewSave") + " - " + GetCurSaveName(), &timeStr);
		SendMessage(&GameInterface, "lse", MSG_INTERFACE_GETTIME, "", &tmpStr);
	}
	else
	{
		if(saveFileName!="quicksave")
		{
		  if( !ParseSaveData(IGetSaveStringFromScroll(), &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr) ) // TIH Aug24'06
			ParseSaveData("none@-1@-1@-1@-1@not implemented@-1", &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr); // TIH Aug24'06
		  timeStr = saveFileName;
		  SendMessage(&GameInterface, "lse", MSG_INTERFACE_FILENAME2DATASTR, saveFileName, &timeStr);
		  if( GetTargetPlatform()=="xbox" ) {
			SendMessage(&GameInterface, "lse", MSG_INTERFACE_GETTIME, saveFileName, &tmpStr);
		  } else {
			SendMessage(&GameInterface, "lse", MSG_INTERFACE_GETTIME, GameInterface.SavePath+"\\"+saveFileName, &tmpStr);
		  }
		}
		else
		{
		  locationStr = qsLocationStr;//"quicksave";
		  pic1 = qsPic1;//GetFacePicture(GetOfficersIndex(GetMainCharacter(),0)); // NK
		  pic2 = qsPic2;//GetFacePicture(GetOfficersIndex(GetMainCharacter(),1)); // NK
		  pic3 = qsPic3;//GetFacePicture(GetOfficersIndex(GetMainCharacter(),2)); // NK
		  pic4 = qsPic4;//GetFacePicture(GetOfficersIndex(GetMainCharacter(),3)); // NK
		  timeStr = qsTimeStr;
		  sgvStr = qsSgvStr;
		  
		  string sSaveDescriber = locationStr + "@" + pic1 + "@" + pic2 + "@" + pic3 + "@" + pic4 + "@" + timeStr + "@" + sgvStr;

		  if( !ParseSaveData(IGetSaveStringFromScroll(), &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr) ) // TIH Aug24'06
			ParseSaveData(sSaveDescriber, &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr);
		  //bool bParsed = ParseSaveData(IGetSaveStringFromScroll(), &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr); // TIH Aug24'06
		  //ParseSaveData(IGetSaveString(GameInterface.SavePath+"\\"+saveFileName), &pic1,&pic2,&pic3,&pic4, &locationStr, &timeStr, &sgvStr); // TIH Aug24'06
		  //if (!bParsed) trace("SAVEGAME - quicksave did not parse! "+ IGetSaveStringFromScroll());
//		  timeStr = saveFileName;//"Quicksave";
		  SendMessage(&GameInterface, "lse", MSG_INTERFACE_FILENAME2DATASTR, saveFileName, &timeStr);
		  if( GetTargetPlatform()=="xbox" ) SendMessage(&GameInterface, "lse", MSG_INTERFACE_GETTIME, saveFileName, &tmpStr);
          else SendMessage(&GameInterface, "lse", MSG_INTERFACE_GETTIME, GameInterface.SavePath+"\\"+saveFileName, &tmpStr);
		}
	}

	// TIH --> Aug24'06
	if ( saveFileName != "" && stf(sgvStr) < stf(IS_SGV) && !bThisSave)// saveFileName!="quicksave") //
	{
		SetSelectable("LOAD_BUTTON",false);
		SetNodeUsing("VERSIONMISMATCHBOX",true);
		SetNodeUsing("VERSIONMISMATCH",true);
	}
	// TIH <--

	GameInterface.strings.SaveName = timeStr + " " + tmpStr;
//	if(saveFileName=="quicksave") GameInterface.strings.SaveName = "Corrupted Quicksave - Delete This!";// TIH quicksaves are off until build14

	GameInterface.strings.SaveNameConfirm = timeStr + " " + tmpStr; // added by MAXIMUS

	GameInterface.pictures.face1.pic = GetFacePictureName(sti(pic1));
	GameInterface.pictures.face1.tex = GetFaceGroupNameFromFace(sti(pic1)); // NK

	GameInterface.pictures.face2.pic = GetFacePictureName(sti(pic2));
	GameInterface.pictures.face2.tex = GetFaceGroupNameFromFace(sti(pic2)); // NK

	GameInterface.pictures.face3.pic = GetFacePictureName(sti(pic3));
	GameInterface.pictures.face3.tex = GetFaceGroupNameFromFace(sti(pic3)); // NK

	GameInterface.pictures.face4.pic = GetFacePictureName(sti(pic4));
	GameInterface.pictures.face4.tex = GetFaceGroupNameFromFace(sti(pic4)); // NK
}

bool ParseSaveData(string fullSaveData, ref pic1,ref pic2,ref pic3,ref pic4, ref locationStr, ref timeStr, ref sgvStr) // TIH Aug24'06
{
	Trace("SAVEGAME - parse: "+ fullSaveData);
	string lastStr;
	if( !GetNextSubStr(fullSaveData, locationStr, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, pic1, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, pic2, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, pic3, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, pic4, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, timeStr, &lastStr) ) return false;
	if( !GetNextSubStr(lastStr, sgvStr, &lastStr) ) return false; // TIH Aug24'06
	return true;
}

bool GetNextSubStr(string inStr, ref outStr, ref lastStr)
{
	if(inStr=="")
	{
		outStr="";
		lastStr="";
		return false;
	}
	int strSize = strlen(inStr)-1;
	if(strSize<=0)
	{
		outStr="";
		lastStr="";
		return false;
	}
	int sympos = findsubstr(inStr,"@",0);
	if(sympos==-1)
	{
		outStr = inStr;
		lastStr = "";
		return true;
	}
	if(sympos>0)	outStr = strcut(inStr,0,sympos-1);
	else	outStr = "";
	if(strSize>sympos+1) lastStr = strcut(inStr,sympos+1,strSize);
	else lastStr = "";
	return true;
}

int sl_tmp_var;
ref NewInterfaceTexture()
{
	string str = GetEventData();
	string strSaveData = ""; // TIH initialize it Aug24'06
	if(str != "quicksave") // NK 05-04-14 fix quicksave "out of memory" error
	{
		if(GetTargetPlatform()=="pc") {
			sl_tmp_var = SendMessage(&scrshot,"lsse", MSG_SCRSHOT_READ, GameInterface.SavePath, str, &strSaveData);
		} else {
			sl_tmp_var = SendMessage(&scrshot,"lsse", MSG_SCRSHOT_READ,"", str, &strSaveData);
		}
	}
	else { sl_tmp_var = 0; }
	SetDataIntoList(str,strSaveData);
	return &sl_tmp_var;
}

void ReleaseInterfaceTexture()
{
	string str = GetEventData();
	sl_tmp_var = SendMessage(&scrshot,"ls",MSG_SCRSHOT_RELEASE,str);
}

void FindScrshotClass()
{
	string layerName;
	if(bSeaActive && !bAbordageStarted) layerName = SEA_REALIZE;
	else layerName = "realize";
	aref refObj;
	if( GetEntity(layerName,&refObj) )
	{
		while(true)
		{
			if(GetEntityName(&refObj)=="scrshoter")
			{
				scrshot = refObj;
				break;
			}
			if( !GetEntityNext(&refObj) ) break;
		}
	}
}

string _IGetSaveString(string saveName)
{
	if(GetTargetPlatform()=="pc") {
		return IGetSaveString(GameInterface.SavePath+"\\"+saveName);
	} else {
		return IGetSaveString(saveName);
	}
}

string IGetSaveStringFromScroll()
{
	string attributeName = "pic" + (nCurScroll+1);
	if( CheckAttribute(&GameInterface,"saveslist."+attributeName) )
		return GameInterface.saveslist.(attributeName).savestr;
	return "";
}

bool CheckAvailablePlace()
{
	if( GetTargetPlatform() == "pc" ) return true;
	string attributeName = "pic" + (nCurScroll+1);
	int nOldBlocks = 0;
	if( CheckAttribute(&GameInterface,"saveslist."+attributeName+".saveSize") ) {
		nOldBlocks = sti( GameInterface.saveslist.(attributeName).saveSize );
	}
	int availableBlocks = FreeSpace + nOldBlocks;
	if( availableBlocks >= MAX_SAVE_STORE_BLOCKS ) return true;
	return false;
}

void ShowLowStorage(bool bStartShow)
{
	if(bStartShow)	DisableString("SaveName");
}

void SetDataIntoList(string saveName, string strSaveData)
{
	aref aRoot;
	makearef(aRoot,GameInterface.saveslist);

	int n=1;
	string attributeName = "pic1";
	while( CheckAttribute(aRoot,attributeName) )
	{
		if( aRoot.(attributeName).name1 == saveName )
		{
			aRoot.(attributeName).savestr = strSaveData;
			return;
		}
		n++;
		attributeName = "pic"+n;
	}
}

string GetCurLocationName()
{
	if( GetMainCharacterIndex()<0 ) return "";
	ref PChar = GetMainCharacter();
	if( !CheckAttribute(PChar,"location") ) return "";

// rewritten by MAXIMUS 09.10.2006 [for correct location name showing]-->
	string locLabel, curIsland, curComma;
	locLabel = PChar.location;
// rewritten by MAXIMUS 09.10.2006 [for correct location name showing]<--

	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");

	int locidx = FindLocation(PChar.location);
	if( locidx>=0 )
	{
		if( CheckAttribute(&Locations[locidx],"id.label") ) {
// rewritten by MAXIMUS 09.10.2006 [for correct location name showing]--> // KK -->
			if (CheckAttribute(&Locations[locidx], "townsack")) Preprocessor_Add("town_name", GetTownName(Locations[locidx].townsack));
			curIsland = XI_ConvertString(FindIslandName(Islands[FindIslandBySeaLocation(GetCharacterShipLocation(PChar))].id));
			Preprocessor_Add("island_name", curIsland);
			curComma = GetSymbol(locLabel, strlen(locLabel)-1);
			if(curComma==".")
				locLabel = LanguageConvertString(tmpLangFileID,strcut(locLabel,0,strlen(locLabel)-2));
			else
				locLabel = LanguageConvertString(tmpLangFileID,locLabel);
			locLabel = curIsland + ". " + PreprocessText(Locations[FindLocation(PChar.location)].id.label);
			Preprocessor_Remove("town_name");
			Preprocessor_Remove("island_name");
// rewritten by MAXIMUS 09.10.2006 [for correct location name showing]<-- // <-- KK
		}
	}
	else
	{
		locLabel = LanguageConvertString(tmpLangFileID,"Open Sea");
	}
	// boal -->
	//for(int i = 0; i < 4; i++) { if(GetOfficersIndex(PChar,i) != -1) locLabel += " " + Characters[GetOfficersIndex(PChar,i)].faceID; }
	LanguageCloseFile(tmpLangFileID);
	return locLabel +" "+ GetStringDate(GetDataYear(),GetDataMonth(),GetDataDay());
	// boal <--
}

string GetClampedSaveName(string sInName, int nNumber)
{
	if( GetTargetPlatform()!="xbox" ) {
		if(nNumber>0) {return sInName + " " + nNumber;}
		else {return sInName;}
	}

	string retStr = sInName;
	int nLen = strlen(sInName)+1;
	int n = nNumber;
	int nAdd = 0;
	if(n>0) nAdd=1;
	while(n>0)
	{
		nAdd++;
		n = n/10;
	}
	if(nLen+nAdd>30)
	{
		int lastNum = nLen - sInName
		retStr = strcut(sInName,0,30-nAdd);
	}

	if(nNumber>0) retStr += " " + nNumber;
	return retStr;
}

bool CheckLimited()
{
	if( nCurScroll>0 ) return true;
	if( !CheckAttribute(&GameInterface,"saveslist.pic1.descr") ||
		GameInterface.saveslist.pic1.descr!="" )
	{
		if( sti(GameInterface.saveslist.ListSize) > MAX_SAVE_GAME_LIMIT ) {
			SetNodeUsing("LOWSTORAGE_WINDOW",true);
			return false;
		} else {return true;}
	}

	return true;
}
