// Remote Window
function openWin(url,width,height,scroll,target)
{
	featureList = ",resizable=yes,status=no,toolbar=no,menubar=no,addressbar=no";

	if(arguments[4]) { windowName=arguments[4] };
	if(arguments[5]) { featureList=","+arguments[5] };

	if(width > screen.width) { screen.width - 50; }
	if(height > screen.height) { height = screen.height - 50; }

	leftWin = (screen.width - width)/2;
	topWin = (screen.height - height)/2;

	win = open(url, target, "width="+width+",height="+height+",left="+leftWin+",top="+topWin+",scrollbars="+scroll+featureList);
	win.focus();
	return win;

}
/*
function openPopupEditor(type,form)
{
	//win=openWin('edit.php?mini=1','500','350','No','popup');
	win=openWin('','500','350','No','popup');
	form.type.value=type;
	form.submit();
}
*/

function openPopupEditor(type,form)
{
	openPopupEditorNamed(type,form,'popup');
}

function openPopupEditorNamed(type,form,name)
{
	win=openWin('','500','350','No',name);
	form.type.value=type;
	form.submit();
}

function closeAndRefresh() {
	//window.opener.location.reload();
	window.close();
}


function submitenter(myfield,e)
{
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		myfield.form.submit();
		return false;
	}
	else
		return true;
}

function setfocus(myfield)
{
	myfield.focus();
}



function getSeason(issue) {
	var seasons = new Array();
	seasons[0]="Spring";
	seasons[1]="Summer";
	seasons[2]="Autumn";
	seasons[3]="Winter";
	seasonNum = (issue-1) % 4;
	//alert('for issue '+issue+', got season: '+seasons[seasonNum]);
	return seasons[seasonNum];
}

function getYear(issue) {
	var year1 = 1995;
	offset = Math.round((issue-1)/4);
	year = year1 + offset;
	//alert('got year: '+year);
	return Math.round(year);
}

function getDescription(issue, target) {
	if(issue != "") {
		season = getSeason(issue);
		year = getYear(issue);
		if(season=="Summer")
		{
			year2 = year%100 + 1;
			if(year2 < 10) { year2 = "0" + year2; }
			year = year + "/" + year2;
		}
		description = season + " " + year;
		target.value = description;
	}
}


// Function to return the form element from a given form field name
function getForm(field)
{
	var objElement = field;

	// Find the appropriate form
	do
	{
		objElement = objElement.parentNode;
	} while (!objElement.tagName.match(/form/i) && objElement.parentNode);

	return objElement;
}

function confirmSubmit(field,msg)
{
	var proceed = confirm(msg);
	if(proceed)
	{
		getForm(field).submit();
	}
}

function getCboText(cbo)
{
	return cbo.options[cbo.selectedIndex].text;
}

function getCboValue(cbo)
{
	return cbo.options[cbo.selectedIndex].value;
}


function insertIntoCbo(cbo,text,value)
{
	var l = cbo.options.length;
	cbo.options.length = l + 1;
	cbo.options[l].text = text;
	cbo.options[l].value = value;
}

function insertIntoCboOrderedInt(cbo, text, value)
{
	var l = cbo.options.length;
	cbo.options.length = l + 1;
	for(i=l; i>=1; i--)
	{
		if(parseInt(cbo.options[i-1].text) < parseInt(text))
		{
			cbo.options[i].value = value;
			cbo.options[i].text  = text;
			break;
		} else {
			cbo.options[i].value = cbo.options[i-1].value;
			cbo.options[i].text  = cbo.options[i-1].text;
		}
	}
}

function insertIntoCboOrderedStr(cbo, text, value)
{
	var l = cbo.options.length;
	cbo.options.length = l + 1;
	for(i=l; i>=1; i--)
	{
		if(cbo.options[i-1].text.toLowerCase() < text.toLowerCase())
		{
			cbo.options[i].value = value;
			cbo.options[i].text  = text;
			break;
		} else {
			cbo.options[i].value = cbo.options[i-1].value;
			cbo.options[i].text  = cbo.options[i-1].text;
		}
	}
}

function removeFromCbo(cbo,value)
{
	i=0;
	found=false;
	length = cbo.options.length;

	while(i < length)
	{
		if(!found)
		{
			if(cbo.options[i].value == value)
			{
				proceed = confirm('Really remove '+cbo.options[i].text+' from the author list?');
				if(!proceed)
				{
					return;
				}
				found=true;
			}
		}

		if(found && i < length-1)
		{
			cbo.options[i].text = cbo.options[i+1].text;
			cbo.options[i].value = cbo.options[i+1].value;
		}
		i++;
	}

	if(found)
	{
		cbo.length = cbo.length - 1;
	}
}
