function deleteBlock(A,message)
{
	var agree=confirm("Are you sure you wish to delete HTML block '"+message+"'?");
	if (agree==true){
		window.location.href="html-manager.php?id="+A+"&delete=true";
	}
}

function deleteTemplate(A,message)
{
	var agree=confirm("Are you sure you wish to delete template '"+message+"'?");
	if (agree==true){
		window.location.href="console-template-manager.php?id="+A+"&delete=true";
	}
}

function deleteConsole()
{
	var agree=confirm("Are you sure you wish to delete this console? WARNING: This action cannot be undone!");
	if (agree==true){
		return true;
	}else{
		return false;	
	}
}

function deleteMember()
{
	var agree=confirm("Are you sure you wish to delete this member? This action cannot be undone!");
	if (agree==true){
		return true;
	}else{
		return false;	
	}
}

function resetMember(username,casino,casinoId,credits)
{
	var agree=confirm("Are you sure you wish to reset the credits for member '"+username+"'");
	if (agree==true){
		location = "reset-credits.php?username="+username+"&casino="+casino+"&casino_id="+casinoId+"&credits="+credits;
	}
}

function resetCasino(casino,casinoId,credits)
{
	var agree=confirm("Are you sure you wish to reset all members' credits for this casino?");
	if (agree==true){
		location = "reset-credits.php?type=resetAll&casino="+casino+"&credits="+credits+"&casino_id="+casinoId;
	}
}


function emailCustomCode(first,last,email)
{
	html_block = document.myForm.html_block.value;
	html_block = escape(html_block);
	window.open('email-custom-code.php?html_block='+html_block+'&first_name='+first+'&last_name='+last+'&email='+email);
	
}

function displayConfirm(message)
{
	var agree=confirm(message);
	if (agree==true){
		return true;
	}
	else{
		return false;	
	}
}



function setRedirect(page,seconds){
	seconds = seconds * 1000;
	setTimeout('redirect("'+page+'")', seconds);
}

function redirect(page){
	document.location = page;	
}


function previewImage(file,preview)
{
    var Img = new Image();
    if (navigator.appName == "Netscape")
    {
      alert("Previews do not work in Netscape.");
    }
    else
    {
      Img.src = filename;
      file.src = Img.src;
	}
}


function checkEmailContents(){
	myReturn = true;
	
	if(document.myForm.subject.value==""){
		alert("Please enter a subject for your email");
		myReturn = false;
	}
	else if(document.myForm.message.value==""){
		alert("Please enter a message for your email");	
		myReturn = false;
	}
	else if(document.myForm.sendTo[2].checked){
		if(document.myForm.email.value==""){
			alert("Please enter an e-mail address!");	
			myReturn = false;
		}
	}
	
	return myReturn;
}

//CODE FOR ADDING/REMOVING IN-GAME ADVERTS
//************************************************************************************
//Which browser is the client using?
var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

var arrayNum = 0;
var advertInfo = new Array();

//What are the current casino games?
//--------------------------------------------------------------------//

//"Real" names of the casino games
var casinoGame = new Array();
casinoGame[0] = "Blackjack";
casinoGame[1] = "American Roulette";
casinoGame[2] = "European Roulette";
casinoGame[3] = "5 Reel Slot - Treasure Chest";
casinoGame[4] = "5 Reel Slot - Fortune Wheel";
casinoGame[5] = "3 Reel Slot - Money Matters";
casinoGame[6] = "3 Reel Slot - Space Travel";
casinoGame[7] = "Jacks or Better Video Poker";
casinoGame[8] = "Deuces Wild Video Poker";
casinoGame[9] = "Craps";
casinoGame[10] = "Baccarat";
casinoGame[11] = "Keno";
casinoGame[12] = "Scratch Cards";
casinoGame[13] = "Wilderness Wealth";
casinoGame[14] = "New Yorker";
casinoGame[15] = "Genie's Jewels";
casinoGame[16] = "Lucky Clover";
casinoGame[17] = "Polly Wolly";
casinoGame[18] = "Enchanted Forest";
casinoGame[19] = "Grocery Game Show";
casinoGame[20] = "Cleopatras Gold";

//"System" names of the casino games
var casinoGameShort = new Array();
casinoGameShort[0] = "Blackjack";
casinoGameShort[1] = "AmericanRoulette";
casinoGameShort[2] = "EuropeanRoulette";
casinoGameShort[3] = "TreasureChestSlot";
casinoGameShort[4] = "FortuneWheelSlot";
casinoGameShort[5] = "MoneyMattersSlot";
casinoGameShort[6] = "SpaceTravelSlot";
casinoGameShort[7] = "JacksOrBetter";
casinoGameShort[8] = "DeucesWild";
casinoGameShort[9] = "Craps";
casinoGameShort[10] = "Baccarat";
casinoGameShort[11] = "Keno";
casinoGameShort[12] = "ScratchCard";
casinoGameShort[13] = "wilderness_wealth";
casinoGameShort[14] = "new_yorker";
casinoGameShort[15] = "genies_jewels";
casinoGameShort[16] = "lucky_clover";
casinoGameShort[17] = "polly_wolly";
casinoGameShort[18] = "enchanted_forest";
casinoGameShort[19] = "grocery_game_show";
casinoGameShort[20] = "cleopatras_gold";
//--------------------------------------------------------------------//

//Create the drop down menu
function createSelection(selectedGame,advertId)
{
	var myText = "<select name='game"+advertId+"'>";
	
	for(var i = 0; i<casinoGame.length; i++)
	{
		if(casinoGameShort[i]==selectedGame)
		{
			myText += "<option value='"+casinoGameShort[i]+"' selected='selected'>"+casinoGame[i]+"</option>";	
		}
		else
		{
			myText += "<option value='"+casinoGameShort[i]+"'>"+casinoGame[i]+"</option>";	
		}
	}
	myText += "</select>";
	return myText;
}

//Update the advert values
function modifyAdvert(advertId){
	
	if(document.myForm["bonus"+advertId].value=="" || document.myForm["ad_text"+advertId].value=="" || document.myForm["ad_url"+advertId].value=="")
	{
		alert("You must fill in all advert fields!");
	}
	else
	{
		advertInfo[advertId][0] = document.myForm["game"+advertId].value;
		advertInfo[advertId][1] = encodeHTMLCharacters(document.myForm["bonus"+advertId].value);
		advertInfo[advertId][2] = encodeHTMLCharacters(document.myForm["ad_text"+advertId].value);
		advertInfo[advertId][3] = encodeHTMLCharacters(document.myForm["ad_url"+advertId].value);
		displayAdverts();
	}
}

function addNewAdvert(gameType,bonus,adtext,adurl){
	
	if(bonus=="" || adtext=="" || adurl=="")
	{
		alert("You must fill in all advert fields!");
	}
	else
	{
	
		advertInfo[arrayNum] = new Array(4);
		advertInfo[arrayNum][0] = gameType;
		advertInfo[arrayNum][1] = encodeHTMLCharacters(bonus);
		advertInfo[arrayNum][2] = encodeHTMLCharacters(adtext);
		advertInfo[arrayNum][3] = encodeHTMLCharacters(adurl);

		//Clear the form
		document.myForm.bonus.value = "";
		document.myForm.adtext.value = "";
		document.myForm.adurl.value = "http://";

		arrayNum++;

		//Display the advert
		displayAdverts();
	}
}

//Adds an advert but doesn't update the page
//Script must manually call the displayAdverts() method in order to
//correctly display all the adverts on the page
function addNewAdvertNoUpdate(gameType,bonus,adtext,adurl){
	
	if(bonus!="" && adtext!="" && adurl!="")
	{
		advertInfo[arrayNum] = new Array(4);
		advertInfo[arrayNum][0] = gameType;
		advertInfo[arrayNum][1] = encodeHTMLCharacters(bonus);
		advertInfo[arrayNum][2] = encodeHTMLCharacters(adtext);
		advertInfo[arrayNum][3] = encodeHTMLCharacters(adurl);

		arrayNum++;
	}
}

//Switch around the array in order to sort it
function switchAround(moveFrom)
{
	if(moveFrom!=0)
	{
		for(var i=0; i<advertInfo.length; i++){
			var tmp = advertInfo[i][0];
			advertInfo[i][0] = advertInfo[i][moveFrom];
			advertInfo[i][moveFrom] = tmp;
		}
	}
}

//Display the advert
function displayAdverts(){

var myText = "";
myText = "<table border='0' cellpadding='3' cellspacing='3'><tbody></tbody>";
myText += "<tr><td><a href='JavaScript: sortAdvert(0);'>Game</a></td><td><a href='JavaScript: sortAdvert(1);'>Ad Bonus</a></td><td><a href='JavaScript: sortAdvert(2);'>Ad Text</a></td><td><a href='JavaScript: sortAdvert(3);'>Ad Url</a></td><td>Management</td></tr>";

for(var i=0; i<arrayNum; i++){

if(advertInfo[i][0]!="delete"){
myText += "<tr><td>"+createSelection(advertInfo[i][0],i)+"</td><td><input value='"+advertInfo[i][1]+"' name='bonus"+i+"' onkeyup=\"textLimit(this.form.bonus"+i+", 100, false);\" />	</td><td><input value='"+advertInfo[i][2]+"' name='ad_text"+i+"' onkeyup=\"textLimit(this.form.ad_text"+i+", 200, false);\"/></td><td><input value='"+advertInfo[i][3]+"' name='ad_url"+i+"' onkeyup=\"textLimit(this.form.ad_url"+i+", 300);\"/></td><td><a href='JavaScript: modifyAdvert("+i+");'>Modify</a> | <a href='JavaScript: deleteAdvert("+i+");'>Delete </a></td></tr>";
}
}
myText += "</table>";
myText += "<input type='hidden' name='game_advert_xml' value='"+createXML()+"'>";

writetolayer('game_adverts',myText);
}

function encodeHTMLCharacters(myText)
{
	myText = str_replace("\"", "&quot;", myText);
	myText = str_replace("'", "&#39;", myText);
	myText = str_replace("<","&lt;",myText);
	myText = str_replace(">","&gt;",myText);
	return myText;
}


//Sort the array
function sortAdvert(type)
{
	switchAround(type);
	advertInfo = advertInfo.sort();
	switchAround(type);
	displayAdverts();
}

//Create the xml for the adverts
function createXML(){

var interval_type;
var display_interval;
var display_type = getCheckedValue(document.myForm.display_type);

interval_type = getCheckedValue(document.myForm.interval_type);

if(interval_type=="time"){
	display_interval = document.myForm.time_interval.value;
}
else if(interval_type=="wins"){
	display_interval = document.myForm.wins_interval.value;
}
else if(interval_type=="plays"){
	display_interval = document.myForm.plays_interval.value;
}


var myText = "";
//var myText = "<game_advert display_type=\""+display_type+"\" interval_type=\""+interval_type+"\" display_interval=\""+display_interval+"\">\n";
for(var i=0; i<advertInfo.length; i++){
if(advertInfo[i][0]!="delete"){	
myText += "<advert>\n<game>"+encodeXMLText(advertInfo[i][0])+"</game>\n<bonus>"+encodeXMLText(advertInfo[i][1])+"</bonus>\n<text>"+encodeXMLText(advertInfo[i][2])+"</text>\n<url>"+encodeXMLText(advertInfo[i][3])+"</url>\n</advert>\n";
}
}
//myText+="</game_advert>";;
return myText;
}

//Encode text to be placed inside xml
function encodeXMLText(myText)
{
	myText = str_replace("&", '**!and!**', myText);
	myText = str_replace("\"", '**!quote!**', myText);	
	myText = str_replace("<", '**!leftarrow!**', myText);
	myText = str_replace(">", '**!rightarrow!**', myText);
	return myText;
}

function str_replace(SearchFor, ReplaceWith, Content) {

   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;

   if (SearchFor.length == 0)
      return;


   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
   
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;


      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);

   }

   return tmpOutput + tmpContent;

}


//Delete the advert
function deleteAdvert(A){
	advertInfo[A][0] = "delete";
	displayAdverts();
}

//Delete all adverts displayed on the page
function deleteAllAdverts()
{
	for(var i=0; i<advertInfo.length; i++)
	{
		advertInfo[i][0] = "delete";	
	}
	displayAdverts();
}

function writetolayer(lay,txt)
{
	if (ie4) {
		document.all[lay].innerHTML = txt;
	}
	if (ns4) {
		document[lay].document.write(txt);
		document[lay].document.close();
	}
	if (ns6) {
		over = document.getElementById([lay]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()) {
			over.removeChild(over.lastChild);
		}
		over.appendChild(domfrag);
	}
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function textLimit(field, maxlen, shouldAlert) {
	if (field.value.length > maxlen && shouldAlert!=false)
		alert('You may only enter '+maxlen+' characters!');
	if (field.value.length > maxlen)
		field.value = field.value.substring(0, maxlen);
}