var MAX_OPTIONS = 50;
var OkToSubmit = false;
var BannersHash = new Object();

function onLoadInit()
{
    // for now list of available banners
    // is the same for both top and bottom banners
    // so create BannersHash from availableTopBanners
    // (contains list of available banners) and use it for
    // initiatizing both top and bottom banners
    createBannersHash(document.forms[0].availableTopBanners);

    initBannerList(document.forms[0].tBanners_tab);
    initBannerList(document.forms[0].bBanners_tab);
}

function addOption(selList, text, val)
{
    if(optionExists(selList, val))
    { 
        alert("This banner has already been selected.");
        return; 
    }
    else if(selList.length >= MAX_OPTIONS)
    {
        alert("Limit of " + MAX_OPTIONS + " banners has been reached.");
        return; 
    }
    selList.options[selList.length] = new Option(text, val);
}

function removeSelectedOptions(selList)
{
    while(selList.selectedIndex > -1)
    {
        selList.options[selList.selectedIndex] = null;
    }
}
function modifySelectedOptions(selList, textInput)
{
    if(selList.selectedIndex < 0) { return; }

    textInput.value = selList.options[selList.selectedIndex].value;
    selList.options[selList.selectedIndex] = null;
}

function createBannersHash(availableBanners)
{
    for (i = 0; i < availableBanners.length; i++)
    { 
         //Add a property to the hash table for every available banner (named "v" + the banner's key)
         //And assign to it the banner's name.
         //We need this so we can quickly index on a banner key in order to get its name.
         eval("BannersHash.v" + availableBanners.options[i].value + " = availableBanners.options[i].text;");
    }

}

function initBannerList(selList)
{
    var i;

    if(selList.options[0].value == "")
    {
        // If the first option has the empty string as its value, remove the option as it
        // is just a place holder to give the selection list some width (due to the value
        // of the option's text property).
        selList.options[0] = null;
    }

    for (i = 0; i < selList.length; i++)
    {
        // use BannersHash to set the text property of each banner currently associated with this record to
        // the banner's name.
        eval("selList.options[i].text = BannersHash.v" + selList.options[i].value + ";");
    }
}

function selectAll(selList)
{
    var i;

    for (i = 0; i < selList.length; i++)
    {
        selList.options[i].selected = true;
    }
}

function optionExists(selList, val)
{
    var i;

    for (i = 0; i < selList.length; i++)
    {
        if(selList.options[i].value == val)
        {
            return true;
        }
    }
    
    return false;
}

function prepareForSubmition(frm)
{
    selectAll(frm.tBanners_tab);
    selectAll(frm.bBanners_tab);

    OkToSubmit = true;
    frm.submit();
}

function okToSubmit()
{
    return OkToSubmit;
}

function popupUrl(sUrl) {
var biggraphic=sUrl;
thatwindow = window.open(biggraphic,"namehere","toolbar=yes, location=yes, status=yes,menubar=yes,scrollbars=yes,width=620,height=370,resizable=yes");

}

// This function is obsolete, it has been replaced by uploadFile (also found in this file)
function uploadImageOld(path, imageField)
{
	uploadwindow = window.open("", "uploadwindow", "width=400,height=200,resizeable,scrollbars");
	uploadwindow.document.open();
	uploadwindow.document.writeln('<html><head><title>Upload an image</title><head><script language=\"javascript1.2\">');
	uploadwindow.document.writeln('function setSaveAs(frm)');
	uploadwindow.document.writeln('{');
	uploadwindow.document.writeln('	re = /^.*[\\\\\\/](.*)$/;');
	uploadwindow.document.writeln('	opener.' + imageField + '.value = frm.image1local.value.replace(re, \'$1\');');
	uploadwindow.document.writeln('	frm.fileName.value = frm.image1local.value.replace(re, \'$1\');');
	uploadwindow.document.writeln('}');
	uploadwindow.document.writeln("</script></head>");
	uploadwindow.document.writeln("<body><h2>Upload an image</h2>");
	uploadwindow.document.write('<form action="/cgi-bin/upload.cgi" '); 
	uploadwindow.document.writeln('ENCTYPE="multipart/form-data" method=post>');
	uploadwindow.document.writeln('<b>File on your hard disk:</b><br><INPUT TYPE=FILE VALUE="*.*" NAME="image1local" onChange="setSaveAs(this.form);"><p>');
	uploadwindow.document.writeln('<b>Save as:</b><br>(must end with .jpe .jpg .jpeg .tif .tiff .eps .bmp or .gif):<br><INPUT TYPE=TEXT VALUE="" NAME="fileName" onFocus="if(this.value.match(/^\\s*$/)) { setSaveAs(this.form); }" onChange="opener.' + imageField + '.value = this.value">');
	uploadwindow.document.writeln('<INPUT TYPE=hidden VALUE="" NAME="target">');
	uploadwindow.document.writeln('<INPUT TYPE=SUBMIT value="Upload File" onClick="if(this.form.fileName.value.match(/^\\s*$/)) { alert(\'Please supply a file name\'); this.form.fileName.focus(); return false; } else { opener.' + imageField + '.value = this.form.fileName.value; this.form.target.value = \'' + path + '/\' + this.form.fileName.value; return true; }">');
	uploadwindow.document.writeln('</form></body></html>');
	uploadwindow.document.close();
}

function uploadFile(path, textField, fileType)
{
	if(fileType == "csv")
	{
		fileTypeDesc = "csv file";
		acceptedFileExt = ".csv";
	}
	else if(fileType == "archive")
	{
		fileTypeDesc = "a file archive";
		acceptedFileExt = ".gz .tar";
	}
	else //assume image
	{
		fileTypeDesc = "an image";
		acceptedFileExt = ".jpe .jpg .jpeg .tif .tiff .eps .bmp or .gif";
	}
	uploadwindow = window.open("", "uploadwindow", "width=400,height=300,resizeable,scrollbars");
	uploadwindow.document.open();
	uploadwindow.document.writeln('<html><head><title>Upload an image</title><head><script language=\"javascript1.2\">');
	uploadwindow.document.writeln('function setSaveAs(frm)');
	uploadwindow.document.writeln('{');
	uploadwindow.document.writeln('	re = /^.*[\\\\\\/](.*)$/;');
	uploadwindow.document.writeln('	opener.' + textField + '.value = frm.localFileName.value.replace(re, \'$1\');');
	uploadwindow.document.writeln('	frm.fileName.value = frm.localFileName.value.replace(re, \'$1\');');
	uploadwindow.document.writeln('}');
	uploadwindow.document.writeln("</script></head>");
	uploadwindow.document.writeln("<body><h2>Upload " + fileTypeDesc + "</h2>");
	uploadwindow.document.write('<form action="/cgi-bin/upload.cgi" '); 
	uploadwindow.document.writeln('ENCTYPE="multipart/form-data" method=post>');
	uploadwindow.document.writeln('<b>File on your hard disk:</b><br><INPUT TYPE=FILE VALUE="*.*" NAME="localFileName" onChange="setSaveAs(this.form);"><p>');
	uploadwindow.document.writeln('<b>Save as:</b><br>(must end with ' + acceptedFileExt + '):<br><INPUT TYPE=TEXT VALUE="" NAME="fileName" onFocus="if(this.value.match(/^\\s*$/)) { setSaveAs(this.form); }" onChange="opener.' + textField + '.value = this.value">');
	uploadwindow.document.writeln('<INPUT TYPE=hidden VALUE="' + fileType + '" NAME="fileType">');
	uploadwindow.document.writeln('<INPUT TYPE=hidden VALUE="" NAME="target">');
	uploadwindow.document.writeln('<INPUT TYPE=SUBMIT value="Upload File" onClick="if(this.form.fileName.value.match(/^\\s*$/)) { alert(\'Please supply a file name\'); this.form.fileName.focus(); return false; } else { opener.' + textField + '.value = this.form.fileName.value; this.form.target.value = \'' + path + '/\' + this.form.fileName.value; return true; }">');
	uploadwindow.document.writeln('<br><font color="red"><b>NOTE: </b>The file upload will not succeed if this window is closed or a new file upload is started before this upload completes. Please wait for a confirmation before starting a new file upload or closing this window. There is a 5MB limit on each file uploaded.</font>');
	uploadwindow.document.writeln('</form></body></html>');
	uploadwindow.document.close();
}

