// GLOBAL JAVASCRIPT
var xmlHttp;
var myDate = new Date();
var rand = myDate.getTime();


//SERVER TIME
function updateTime()
{
var date = new Date();

var localTime = date.getTime();
var localOffset = (date.getTimezoneOffset()  * 60000);

var ServerTime = (localTime + localOffset);

date.setTime(ServerTime);

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

var output = "" + ((hours > 12) ? (hours - 12) : hours);

output += ((minutes < 10) ? ":0" : ":") + minutes;
output += ((seconds < 10) ? ":0" : ":") + seconds;

output += ((hours >= 12) ? " PM" : " AM");

fulloutput = "Demon War Time: " + output;

document.getElementById('header_time').innerHTML = fulloutput;

setTimeout("updateTime()", 1000);
}



//ALERTS
function msgalert (Msg, Sec, Red)
{
var Seconds = Sec * 1000;
var Message = Msg;
var Redirect = Red;
WrapperDivName = document.getElementById('alert_wrapper');
WrapperDivName.style.display = 'block';

ContentDivName = document.getElementById('alert_text');
ContentDivName.innerHTML = Message;

if (Redirect == 1) 
{setTimeout("window.location='/Dashboard.php'", Seconds);}
else
{setTimeout("WrapperDivName = document.getElementById('alert_wrapper'); WrapperDivName.style.display = 'none';", Seconds);
}
}




/////////////////BUTTONS//////////////////////
function change(frameid){
document.getElementsByName(frameid)[0].style.backgroundImage = "url(/Images/Backgrounds/Background_InnerTile.jpg)";
}

function change_back(frameid){
document.getElementsByName(frameid)[0].style.backgroundImage = "url(/Images/Backgrounds/Background_LightInnerTile.jpg)";
}

function changeloginbutton(){
document.emailForm.submitemail.value = "Please wait...";
}

function changebutton(frameid){
document.getElementsByName(frameid)[0].style.backgroundImage = "url(/Images/Backgrounds/Button_Glow.jpg)";
document.getElementsByName(frameid)[0].style.color = "#000";
}

function changebutton_back(frameid){
document.getElementsByName(frameid)[0].style.backgroundImage = "url(/Images/Backgrounds/Button_Normal.jpg)";
document.getElementsByName(frameid)[0].style.color = "#FFFFCC";
}

function changemenu(frameid, framenum){
document.getElementsByName(frameid)[framenum].style.backgroundImage = "url(/Images/Dashboard/MenuButton_Background.png)";
}

function changemenu_back(frameid, framenum){
document.getElementsByName(frameid)[framenum].style.backgroundImage = "url(/Images/Backgrounds/Background_LightInnerTile.jpg)";
}

function changemenu_perm(frameid, framenum)
{
var x = document.getElementsByName(frameid);
	for (i=0;i<x.length;i++)
	{
	x[i].style.backgroundImage = "none";
	}
document.getElementsByName(frameid)[framenum].style.backgroundImage = "url(/Images/Generic/BlackOverlay.png)";
}

/////////////////MENU//////////////////////
function OpenSection(Section)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
ajaxpage('/Account/Menu.php?Section='+Section, 'menu_wrapper');
}

/////////////////MESSAGES//////////////////////
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
countfield.value = maxlimit - field.value.length;
}


/////////////////SIGNUP & MAIN PAGE//////////////////////

function SignupAgreement (){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

 ajaxpage('/Main/Agreement.php', 'generic_outerwrapper_medium');
}

function ForgotPassword (){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

var username = document.nopass.name.value;

 ajaxpage('/Account/SendPassword.php?name='+username, 'blankwrapper_login');
 setTimeout("ajaxpage('/Main/Login.php', 'blankwrapper_login')",4000);
}


/////////////////REFRESH STATS//////////////////////

function refreshdungeonstats (){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

 ajaxpage('/Map/GetDungeon.php', 'blank_wrapper');
}

function refreshdashboardstats (){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small');
}


/////////////////INVENTORY//////////////////////

function UnEquip(ItemID){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
var url="/Equip/UnEquip.php?ItemID="+ItemID

xmlHttp.readystate;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);

ajaxpage('/Equip/GetEquip.php', 'blank_wrapper');
}


function EquipItem(EquipmentTypeID, EquipmentID, InventoryID){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
}

var url="/Equip/EquipItem.php?EquipmentTypeID="+EquipmentTypeID+"&EquipmentID="+EquipmentID+"&InventoryID="+InventoryID

xmlHttp.readystate;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
ResponseID = xmlHttp.responseText;
if (ResponseID == 1){msgalert('You are already under the influence of this Elixir',3, 0);}
ajaxpage('/Equip/GetEquip.php', 'blank_wrapper');
}


function SellItem(ItemID){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
return;
}
var choice = confirm("Do you really want to sell this item?");

if (choice== true){

   var url="/Equip/SellItem.php?ItemID="+ItemID;
	xmlHttp.readystate;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	ajaxpage('/Equip/GetEquip.php', 'blank_wrapper');
   }else{
  window.location='/Equipment.php';}
}




////////////////TRAINING///////////////////////

function Train(Stat){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}

ajaxpage('/Train/GetTrain.php?Train='+Stat, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);}


function Whisperer(Hours)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}

ajaxpage('/Train/GetTrain.php?Whisperer=1&Hours='+Hours, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);
}


/////////////////////////GAMBLE/////////////////////////////////


function GameReb (Play){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

 ajaxpage('/GamblersDen/GetGame_Rebellion.php?Play='+Play, 'generic_outerwrapper_medium');
 setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",1000);
}



/////////////////////////MAP/////////////////////////////////

function ProcessTrav(MapDirection, MapPositionX, MapPositionY)
{
var url="/Map/ProcessTravel.php";
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET", url, false);                             
     xmlHttp.send(null);
     ResponseID = xmlHttp.responseText;
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

if (ResponseID == 1) {msgalert('Sorry, you are too badly injured to travel!',3, 0);}
else
if (ResponseID == 2) {msgalert('Sorry, you are too tired to travel!',3, 0);}
else
{Map(MapDirection, MapPositionX, MapPositionY)}
}



function Map(MapDirection, MapPositionX, MapPositionY)
{
var url="/Map/ProcessMovement.php?Type=0&Move=1";
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET", url, false);                             
     xmlHttp.send(null);
     ResponseID = xmlHttp.responseText;

if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

     if (ResponseID == 1) {msgalert('Sorry, you are too badly injured to move!',3, 0);}
     else
     if (ResponseID == 2) {msgalert('Sorry, you are too tired to move!',3, 0);}
     else
     {
	 ajaxpage('/Map/GetMap.php?MapDirection='+MapDirection+'&MapPositionX='+MapPositionX+'&MapPositionY='+MapPositionY, 'generic_outerwrapper_large');
	setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);
	 }
} 


function FreeMap(MapDirection, MapPositionX, MapPositionY)
{
ajaxpage('/Map/GetMap.php?MapDirection='+MapDirection+'&MapPositionX='+MapPositionX+'&MapPositionY='+MapPositionY, 'generic_outerwrapper_large');
} 


function FilterWorldMap (LocationID)
{
ajaxpage('/Map/GetWorldMap.php?LocationID='+LocationID, 'generic_outerwrapper_large');
}

function TerScore(MapID)
{
ajaxpage('/Map/GetTerritoryScore.php?MapID='+MapID, 'map_pingridtoc');
}


/*////////////////HOSPITAL//////////////*/

function PurchaseHospitalItem (ItemID, UserID, CharacterID, Encumbered)
{
if (Encumbered == 1)
{
msgalert('You have too many items in your Inventory',3, 0);
}
else
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}
var url="/Hospital/PurchaseHospitalItem.php"
+"?ItemID="+ItemID
+"&UserID="+UserID
+"&CharacterID="+CharacterID


xmlHttp.readystate;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);

urlpassback = "?HospitalItemType="+ItemID;
ajaxpage('/Hospital/GetHospital.php'+urlpassback, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);
}
}


function Heal(Stat, Cost, Type){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}

ajaxpage('/Hospital/GetHospital.php?Stat='+Stat+'&Cost='+Cost+'&Type='+Type, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);}


/*////////////////STORE//////////////*/

function PurchaseStoreItem (ItemID, UserID, CharacterID, ItemType, Encumbered)
{

if (Encumbered == 1)
{
msgalert('You have too many items in your Inventory',3, 0);
}
else
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}
var url="/Store/PurchaseItem.php"
+"?ItemID="+ItemID
+"&UserID="+UserID
+"&CharacterID="+CharacterID

xmlHttp.readystate;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);

urlpassback = "?ItemType="+ItemType;
setTimeout("ajaxpage('/Store/FilterStore.php'+urlpassback, 'generic_outerwrapper_large')",200);
}
}


/*////////////////BANK//////////////*/

function BankWithdraw ()
{
var denzon = document.BankOut.outnum.value
ajaxpage('/Stash/GetStash.php?withdrawnum='+denzon, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);
}

function BankDeposit ()
{
var denzon = document.BankIn.innum.value
ajaxpage('/Stash/GetStash.php?stashnum='+denzon, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);
}



/*////////////////FAMILIARS////////////////*/
function GetFamiliar (FamiliarPos)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }
ajaxpage('/Familiars/GetFamiliars.php?LocationID=32&FamiliarPos='+FamiliarPos, 'generic_outerwrapper_medium');


}

function PurchaseFamiliar (FamiliarPos, Purchase)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

url = "?LocationID=32&FamiliarPos="+FamiliarPos+"&Purchase="+Purchase;

ajaxpage('/Familiars/GetFamiliars.php'+url, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);

}


function SellFamiliar ()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

url = "?LocationID=32&Sell=1";

ajaxpage('/Familiars/GetFamiliars.php'+url, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);

}






/*////////////////SCAVENGE AND STEAL////////////////*/

function Scav(ScavengeNum){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}
ajaxpage('/Scavenge/GetScavenge.php?Scav='+ScavengeNum, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);}


function Steal(StealNum){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;}
ajaxpage('/Steal/GetSteal.php?Steal='+StealNum, 'generic_outerwrapper_medium');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'generic_outerwrapper_small')",200);}





/*////////////////DUNGEON////////////////*/


function ProcessMov(DungeonDirection, MsgID, DungeonMonster, DungeonLocationID, DungeonTileID)
{
var url="/Map/ProcessMovement.php?Type="+DungeonMonster;
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET", url, false);                             
     xmlHttp.send(null);
     ResponseID = xmlHttp.responseText;
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }
//alert (ResponseID)
	//Moved to Hospital
	if (ResponseID == 10) 
	{
	window.location='/Dashboard.php';
	}
	else
	if (ResponseID == 1) 
	{
	msgalert('You have been moved to a Hospital',3, 0); 
	setTimeout("window.location='/Dashboard.php'",3000);
	}
	else
	{
		//Continue Map Movement
		if (DungeonMonster == 0)
		{
		     if (ResponseID == 1) 
		     {
		     msgalert('You have been moved to a Hospital',3, 0); 
			setTimeout("window.location='/Dashboard.php'",3000);
			}
		     else
		     if (ResponseID == 2) {msgalert('Sorry, you are too tired to move!',3, 0);}
		     else
		     
		     {Dungeon(DungeonDirection, MsgID, DungeonMonster, DungeonLocationID, DungeonTileID, 0)}
		     //return false;                                        
		} 
		else
		//Check for task monster
		if (DungeonMonster != 0)
		{
		
			if(MsgID != 4) //Dont interfere with Boss
			{
			 if (ResponseID == 1) {msgalert('Sorry, you are too badly injured to move!',3, 0);}
		     else
		     if (ResponseID == 2) {msgalert('Sorry, you are too tired to move!',3, 0);}
		     else
		     if (ResponseID == 3) {msgalert('Defeat this creature to make your delivery!',3, 0);} //Delivery
		     else
		     if (ResponseID == 4) {msgalert('You have found your target',3, 0);} // Wanted
		     else
		     if (ResponseID == 5) {msgalert('You have found a local Threat',3, 0);} // Local Threat
		     else
		     if (ResponseID == 6) {msgalert('You have found your target!',3, 0);} // Help Us
		     else
		     if (ResponseID == 7) {msgalert('Defeat this creature to make your delivery!',3, 0);} // Smuggle
		     else
		     if (ResponseID == 8) {msgalert('You have found your target!',3, 0);} // Assassinate
		   }
		     
		     
			if (ResponseID != 1 && ResponseID != 2) {Dungeon(DungeonDirection, MsgID, DungeonMonster, DungeonLocationID, DungeonTileID, ResponseID);}
		}
	}      
}



function Dungeon(DungeonDirection, MsgID, DungeonMonster, DungeonLocationID, DungeonTileID, ResponseID)
{ 

if (MsgID == 3) // Exit Tile
{
var url="/Map/LeaveDungeon.php?";
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET", url, false);                             
xmlHttp.send(null);
msgalert('You are leaving the dungeon area',3, 1);
//window.location="/Dashboard.php";
}
else
if (MsgID == 0)
{
	if (DungeonMonster != 0)
	{
		ajaxpage('/Map/GetDungeon.php?DungeonDirection='+DungeonDirection+'&ResponseID='+ResponseID, 'blank_wrapper');
	}
	else
	{
		randchance = (Math.round((Math.random()*9)+1));  //dont like them seeing the roll :/  (BM)
		var LocationID = DungeonLocationID;
		//1 in 10 chance of encountering a wandering monster
		//randchance = 1;
		if (randchance == 1)
		{
			msgalert('You have been Ambushed!',3, 0);
			ajaxpage('/Map/GetDungeon.php?DungeonDirection='+DungeonDirection+'&Ambush=1&ResponseID='+ResponseID, 'blank_wrapper');
		}
		else
		{
			ajaxpage('/Map/GetDungeon.php?DungeonDirection='+DungeonDirection+'&ResponseID='+ResponseID, 'blank_wrapper');
		}
	}
}
else
if (MsgID == 4)
{
		msgalert('You have found the Dungeon Boss!',3, 0);
		ajaxpage('/Map/GetDungeon.php?DungeonDirection='+DungeonDirection+'&ResponseID='+ResponseID, 'blank_wrapper');
}

}



/*////////////////TRADE//////////////*/

function ProcessTrade (TradeItem, TradeType)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;}

switch (TradeItem)
{
	case 0: TradeNum = document.tradeform0.num0.value; break;
	case 1: TradeNum = document.tradeform1.num1.value; break;
	case 2: TradeNum = document.tradeform2.num2.value; break;
	case 3: TradeNum = document.tradeform3.num3.value; break;
	case 4: TradeNum = document.tradeform4.num4.value; break;
	case 5: TradeNum = document.tradeform5.num5.value; break;
	case 6: TradeNum = document.tradeform6.num6.value; break;
	case 7: TradeNum = document.tradeform7.num7.value; break;
	case 8: TradeNum = document.tradeform8.num8.value; break;
	case 9: TradeNum = document.tradeform9.num9.value; break;
}


var url="?TradeItem="+TradeItem
+"&TradeType="+TradeType
+"&TradeNum="+TradeNum


ajaxpage('/Trading/GetTrade.php'+url, 'trade_stock');
ajaxpage('/Trading/GetVehicle.php'+url, 'trade_vehicle');
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'dash_charactersheet')",1000);

}

/*/////Trade Vehicles///////////*/

function GetVehicle (VehiclePos)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }
ajaxpage('/Trading/GetVehicle.php?VehiclePos='+VehiclePos, 'trade_vehicle');


}

function PurchaseVehicle (VehiclePos, Purchase)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

url = "?VehiclePos="+VehiclePos+"&Purchase="+Purchase;

setTimeout("ajaxpage('/Trading/GetVehicle.php'+url, 'trade_vehicle')",1000);
setTimeout("ajaxpage('/Dashboard/CharacterSheet.php', 'dash_charactersheet')",1200);

}



/*////////////////CHARACTER FUNCTIONS//////////////*/

function CharGen_Stats (RaceID){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
 alert ("Browser does not support HTTP Request");
 return;
 }

ajaxpage('/CharGen/CharGen_GetStats.php?RaceID='+RaceID, 'Step1');
ajaxpage('/CharGen/CharGen_GetPaths.php?RaceID='+RaceID, 'Step2');
}


function CharGen_Paths (PathID, RaceID){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){alert ("Browser does not support HTTP Request");
 return;
 }

ajaxpage('/CharGen/CharGen_GetPaths.php?PathID='+PathID+'&RaceID='+RaceID, 'Step2');
ajaxpage('/CharGen/CharGen_GetSkills.php?PathID='+PathID+'&RaceID='+RaceID, 'Step3');

}


function CreateCharacter (){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
 return;}
var CharacterName = document.charsubmit.charname.value;
var RaceID = document.charsubmit.RaceID.value;
var PathID = document.charsubmit.PathID.value;
if (CharacterName == '' || CharacterName == null){
	window.location = '/CharGen.php?err=1';
}else{var url="/CharGen/CharGen_Create.php";
	url=url+"?RaceID="+RaceID;
	url=url+"&PathID="+PathID;
	url=url+"&CharacterName="+CharacterName;
	var myDate = new Date();
	var rand = myDate.getTime();
	url=url+"&rand="+rand;
	
	xmlHttp.readystate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	setTimeout("window.location = '/CharGen/CharGen_SetCookies.php'",200); 
}
}

function CheckName ()
{
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
 return;}
var CharacterName = document.charsubmit.charname.value;
var url="/CharGen/Chargen_CheckName.php?CharName="+CharacterName;
xmlHttp=GetXmlHttpObject();
xmlHttp.open("GET", url, false);                             
     xmlHttp.send(null);
     ResponseID = xmlHttp.responseText;
if(ResponseID == 1){msgalert('Name already in use',3, 0);}
else
if(ResponseID == 2){msgalert('Invalid name (minimum of 4 characters required)',3, 0);}
else
{CreateCharacter();}
}


function KillChar (CharacterID){

var choice= confirm("Do you really want to remove this character?");

if (choice== true){
   window.location='/CharacterSelect/KillChar.php?CharacterID='+CharacterID;
 }else{
  window.location='/CharSelect.php';
 }
}


