// pop-up window
function newWindow( url, w, h, centered )
{
    var width = w;
    var height = h;
    var winl = 0;
    var wint = 0;
    if ( centered )
    {
	winl = (screen.width - width) / 2;
	wint = (screen.height - height) / 2;
    }

    var myWindow = window.open( url, 'NewWindow', 'width='+width+',height='+height+',top='+wint+',left='+winl+',menubar=no,status=yes,directories=no,location=no,resizable=yes,modal=yes,scrollbars=no' );

    if ( myWindow ==null || typeof( myWindow ) == "undefined" )
    {
	alert( 'You have popup blocker enabled.\nPlease disable and try again!' );
    }
    else
    {
	myWindow.opener = self;
	myWindow.focus();
    }
}


// trim string of beginning and ending spaces
function trim( sInString )
{
    if ( isNullOrBlank( sInString ) )
	return '';

    sInString = sInString.replace( /^\s+/g, "" );   // strip leading
    return sInString.replace( /\s+$/g, "" );	    // strip trailing
}


// checks if string is null or blank
function isNullOrBlank( str )
{
    return ( str == null || str.length == 0 );
}


function Reorder(eSelect, iCurrentField, numSelects)
{
    var eForm = eSelect.form;
    var iNewOrder = eSelect.selectedIndex + 1;
    var iPrevOrder;
    var positions = new Array(numSelects);
    var ix;

    for (ix = 0; ix < numSelects; ix++)
    {
	positions[ix] = 0;
    }
    
    for (ix = 0; ix < numSelects; ix++)
    {
	positions[eSelect.form["FormPosition" + ix].selectedIndex] = 1;
    }
    
    for (ix = 0; ix < numSelects; ix++)
    {
	if (positions[ix] == 0)
	{
	    iPrevOrder = ix + 1;
	    break;
	}
    }

    if (iNewOrder != iPrevOrder)
    {
	var iInc = iNewOrder > iPrevOrder? -1:1
	var iMin = Math.min(iNewOrder, iPrevOrder);
	var iMax = Math.max(iNewOrder, iPrevOrder);
	for (var iField = 0; iField < numSelects; iField++)
	{
	    if (iField != iCurrentField)
	    {
		if (eSelect.form["FormPosition" + iField].selectedIndex + 1 >= iMin && eSelect.form["FormPosition" + iField].selectedIndex + 1<= iMax)
		{
		    eSelect.form["FormPosition" + iField].selectedIndex += iInc;
		}
	    }
	}
    }
}

function submitCheck()
{
    if ( document.frmLayoutSubmit["sort"].value == "no" )
	return true;
    
    if ( !checkOrder() )
	return false;

    DoBuild();
    return true;
}

function checkOrder()
{
    var x, numFound, currentSelect; 
    var form = document.frmLayoutEdit;
    var numFields = form["numSelects"].value;
    numFound = 0;

    while(numFound < numFields)
    {
	for(x = 0; x < numFields; x++)
	{
	    currentSelect = form["FormPosition" + x];
	    if(currentSelect.selectedIndex == numFound)
	    {
		numFound++;
		break;
	    }
	}
    
	if ( x == numFields )
	{
	    alert('Sequence is missing number: ' + (numFound+1));
	    return false;
	}
    }

    return true;
}

function DoBuild()
{
    var numFound, currentSelect, selectValue, selectedIndexValue;
    var form = document.frmLayoutEdit;
    var numFields = form["numSelects"].value;
    var xml = "";
    numFound = 0;

    while(numFound < numFields)
    {
	for(x = 0; x < numFields; x++)
	{
	    currentSelect = form["FormPosition" + x];
	    if(currentSelect.selectedIndex == numFound)
	    {
		selectedIndexValue = currentSelect.selectedIndex + 1;
		selectValue = currentSelect.options[numFound].value;
		if ( xml == "" )
		    xml = selectValue + "=" + selectedIndexValue;
		else
		    xml = xml + "," + selectValue + "=" + selectedIndexValue;
		numFound++;
	    }
	}
    }
    document.frmLayoutSubmit["ReorderedFields"].value=xml;
}
