

var prom = null;

function promptOpen(str) {
	if (!prom) {
		prom = document.createElement('DIV');
		prom.id = 'sprompt';
		prom.innerHTML = '<div id="p_head"><a href="#" onclick="promptClose(); return false;"><img src="/skin/img/cancel.png" alt="*" style="border:none;vertical-align:middle;" /> CLOSE EDIT</a></div><div id="p_body"></div>';
		document.body.appendChild(prom);
	}
	document.getElementById('p_body').innerHTML = str;
	prom.style.visibility = 'visible'; 
}

function promptWidth(wid) {
	prom.style.width = wid + 'px';
}

function promptClose() {
	prom.style.visibility = 'hidden';
	prom.style.width = '300px';
}

function confirmSide(sid,os,ns) {
	if (ns.length<2) {
		return;
	}
	AjaxGetAddress('/Edit.php?Type=scenario&Id='+sid+'&Command=Newside&S1='+escape(os)+'&S2='+escape(ns));
}

function changeSide(sid,side) {
	var str = '<h1>Change scenario nationality</h1>';
	str+= '<p>Change <strong>'+side+'</strong></p>';
	str+= '<input type="text" value="'+side+'" id="newside" />';
	str+= '<input type="button" value="change" onclick="confirmSide('+sid+',\''+side+'\',document.getElementById(\'newside\').value);" />';
	promptOpen(str);
}

function confirmTrash(sid) {
	AjaxGetAddress('/Edit.php?Type=scenario&Id='+sid+'&Command=Trash');
}

function trashScenario(sid) {
	var str = '<h1>Delete Scenario</h1><p>Do you really want to remove this scenario? This operation will be permanent!</p>' +
			'<input type="button" value="yes, delete" onclick="confirmTrash('+sid+');" />';
	promptOpen(str);
}

function savePubTitle(pid) {
	var newval = document.getElementById('newtitle').value;
	AjaxGetAddress('/Edit.php?Type=publication&Id='+pid+'&Command=Title&Newval='+escape(newval));
}

function changePubTitle(curr,pid) {
	var str = '<h1>Title</h1><p>Enter the name of the publication:</p><input type="text" id="newtitle" /><input type="button" onclick="savePubTitle('+pid+');" value="Save" />';
	promptOpen(str);
	document.getElementById('newtitle').value = curr;
	document.getElementById('newtitle').focus();
}

function savePubYear(pid) {
	var newval = document.getElementById('newyear').value;
	if (newval > 1980) {
		Ajax('/Edit.php?Type=publication&Id='+pid+'&Command=Year&Newval='+escape(newval),'pubyear');
	} else {
		alert('Year must be something higher than 1980!');
	}
	promptClose();
}

function changePubYear(curr,pid) {
	var str = '<h1>Year of Publication</h1><p>Enter the year of the publication:</p><input type="text" id="newyear" /><input type="button" onclick="savePubYear('+pid+');" value="Save" />';
	promptOpen(str);
	document.getElementById('newyear').value = curr;
	document.getElementById('newyear').focus();
}


function createPublication(pid) {
	uri = '/Edit.php?Type=publisher&Id='+pid+'&Command=newpublication&newname=' + escape(document.getElementById('newname').value);
	AjaxGetAddress(uri);
}

function PublicationForm(pid) {
	str = '<h1>Create New Publication</h1>';
	str+= '<p>Enter the name of the new publication:</p>';
	str+= '<input type="text" id="newname" /><input type="button" onclick="createPublication('+pid+');" value="Create" />';
	promptOpen(str);
}

function confirmScenario(uri) {
	AjaxGetAddress(uri);
}

function createScenario(pid) {
	var newname = document.getElementById('newname').value;
	var newcode = document.getElementById('newcode').value;
	var side1 = document.getElementById('side1').options[document.getElementById('side1').selectedIndex].value;
	var side2 = document.getElementById('side2').options[document.getElementById('side2').selectedIndex].value;
	if (!newname) {
		alert('Scenario must have a name');
		return;
	}	
	if (!newcode) {
		alert('Scenario must have a code');
		return;
	}	
	if (!side1 || !side2 || side1 == side2) {
		alert('You have to enter two different sides as the nationalities');
		return;
	}
	Ajax('/Edit.php?Type=publication&Id='+pid+'&Command=Createscen&newname='+escape(newname)+'&newcode='+escape(newcode)+'&side1='+escape(side1)+'&side2='+escape(side2),'p_body');
}

function ScenarioForm(pid) {
	promptOpen('<p>Loading form..</p>');
	Ajax('/Edit.php?Type=publication&Id='+pid+'&Command=Createscenform','p_body');
}

function saveDate(scen_id) {
	promptClose();
	var newval = document.getElementById('newdate').value;
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Date&Newval='+escape(newval),'sdt');
}

function pickDate(curr,sid) {
	var str = '<h1>Date</h1><p>Enter the date of scenario in format YYYY-MM-DD. Any date between 1920 and 1960 will be accepted.</p><input type="text" value="'+curr+'" id="newdate" /><input type="button" onclick="saveDate('+sid+');" value="Save" />';
	promptOpen(str);
	document.getElementById('newdate').focus();
}

function pickCounters(scen_id,side,nr) {
	promptOpen('<p>Getting Counter Sets ...</p>');
	promptWidth(600);
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Getcounters&nr='+nr+'&side='+escape(side),'p_body');
}

function updateCounters(nr,scen_id,side,counterid,onoff) {
	if (onoff) {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Setcounters&side='+escape(side)+'&csetid='+counterid+'&set=1', 'counters_'+nr);
	} else {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Setcounters&side='+escape(side)+'&csetid='+counterid+'&set=0', 'counters_'+nr);
	}
}


function setLocation(locid,scen_id) {
	promptClose();
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Location&Newval='+locid,'slo');
}

function pickLocation(sid) {
	promptOpen('<p>Getting Locations ...</p>');
	promptWidth(600);
	Ajax('/Edit.php?Type=scenario&Id='+sid+'&Command=Getlocations','p_body');
}


function setInitiative(side,scen_id) {
	promptClose();
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Initiative&Newval='+escape(side),'sin');
}

function pickAttacker(side1,side2,sid) {
	str = '<h1>Initiative</h1><p>Choose the side with the initiative:</p><ul>';
	str+= '<li><a href="#" onclick="setInitiative(\''+side1+'\','+sid+'); return false;">' + escape(side1) + '</a></li>';
	str+= '<li><a href="#" onclick="setInitiative(\''+side2+'\','+sid+'); return false;">' + escape(side2) + '</a></li>';
	str+= '<li><a href="#" onclick="setInitiative(2,'+sid+'); return false;">DUAL ATTACK</a></li>';
	str+= '<li><a href="#" onclick="setInitiative(0,'+sid+'); return false;">no information</a></li>';
	str+= '</ul>';
	promptOpen(str);
}

function saveTurns(scen_id) {
	promptClose();
	var newval = document.getElementById('newturns').value;
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Turns&Newval='+escape(newval),'sgt');
}

function pickTurns(curr,sid) {
	var str = '<h1>Game Turns</h1><p>Enter the number of game turns in the scenario. If the number is variable, enter the <strong>minimum</strong> number of turns.</p><input type="text" value="'+curr+'" id="newturns" /><input type="button" onclick="saveTurns('+sid+');" value="Save" />';
	promptOpen(str);
	document.getElementById('newturns').focus();
}

function saveTitle(scen_id) {
	var newval = document.getElementById('newtitle').value;
	AjaxGetAddress('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Title&Newval='+escape(newval));
}

function changeTitle(curr,sid) {
	var str = '<h1>Title</h1><p>Enter the name of the scenario:</p><input type="text" id="newtitle" /><input type="button" onclick="saveTitle('+sid+');" value="Save" />';
	promptOpen(str);
	document.getElementById('newtitle').value = curr;
	document.getElementById('newtitle').focus();
}

function appendPublication(scen_id,pub_id) {
	var Code = document.getElementById('Code').value;
	if (Code.length>0) {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Addpublication&Pid='+pub_id+'&Code='+escape(Code),'publist');
		promptClose();
	} else {
		alert('You have to specify a code that was used for the scenario in the publication');
	}
}

function dropPublication(scen_id,pub_id) {
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Droppublication&Pid='+pub_id,'publist');
	promptClose();
}

function getPublications(scen_id,publisher) {
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Appendpublication&publisher='+publisher,'p_body');
}

function modPublications(sid) {
	promptOpen('<p>Getting Publications ...</p>');
	Ajax('/Edit.php?Type=scenario&Id='+sid+'&Command=Getpublications','p_body');
}


function modRules(sid) {
	promptOpen('<p>Getting Rules ...</p>');
	Ajax('/Edit.php?Type=scenario&Id='+sid+'&Command=Getrulelist','p_body');
}

function updateFeature(feat,scen_id,onoff) {
	if (onoff) {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Feature&Fid='+feat+'&set=1','featlist');
	} else {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Feature&Fid='+feat+'&set=0','featlist');
	}
}

function modMaps(sid) {
	promptOpen('<p>Getting Maps ...</p>');
	promptWidth(600);
	Ajax('/Edit.php?Type=scenario&Id='+sid+'&Command=Getmaplist','p_body');
}

function updateMap(mapid,scen_id,onoff) {
	if (onoff) {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Map&Mid='+mapid+'&set=1','maplist');
	} else {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Map&Mid='+mapid+'&set=0','maplist');
	}
}

function modOverlays(sid) {
	promptOpen('<p>Getting Overlays ...</p>');
	promptWidth(600);
	Ajax('/Edit.php?Type=scenario&Id='+sid+'&Command=Getoverlaylist','p_body');
}

function updateOverlay(oid,scen_id,onoff) {
	if (onoff) {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Overlay&Oid='+oid+'&set=1','overlaylist');
	} else {
		Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Overlay&Oid='+oid+'&set=0','overlaylist');
	}
}
function addOverlay(scen_id) {
	var str = '<h1>Create New Overlay</h1>';
	str+= '<p>Enter the code of the new overlay</p>';
	str+= '<input type="text" id="newoverlay" /><input type="button" onclick="createOverlay('+scen_id+');" value="Create" />';
	promptOpen(str);
}
function createOverlay(scen_id) {
	var newcode  = document.getElementById('newoverlay').value;
	Ajax('/Edit.php?Type=scenario&Id='+scen_id+'&Command=Getoverlaylist&newcode='+escape(newcode),'p_body');
}

