var aWindow = null;

function gotoURL(url,type)
{
    var url;
    var type;
    var eventyear;
    var thisdate = new Date();
    var thisyear = thisdate.getFullYear();
    eventyear = prompt('Please enter the correct 4-digit year for the event:',thisyear);
    if ((eventyear=='') || (eventyear==null)) eventyear = thisyear;
    location.href=url + '?Type=' + type + '&EventYear=' + eventyear;
}

function newWin(url,w,h,extra)
{
    if (extra==null) extra = "resizable=yes,scrollbars=yes";
    if (aWindow && aWindow.open && !aWindow.closed)
    {
        w = parseInt(w)+10;
        h = parseInt(h)+15;
        //alert("w is " + w + "\nh is " + h);  // this may cause troubles with pre-4.x browsers
        aWindow.resizeTo(w,h);
    }
    aWindow = open(url,"newWin","width=" + w + ",height=" + h + "," + extra);
    aWindow.focus();
}

function closeChild()
{
    if (aWindow && aWindow.open && !aWindow.closed) aWindow.close();
}

function newpage(url,replace)
{
    if (replace==null) replace=1;
    if (replace > 0)
    {
        location.replace(url);
    } else {
        location.href=url;
    }
}

/*
var checkflag = "false";
function checkAll(theForm,fieldname,destination)
{
    if (destination==null)
    {
        destination='';
    } else {
        destination.value='';
    }
    if (checkflag == "false")
    {
        for (i=0;i<theForm.elements.length;i++)
        {
            if (theForm.elements[i].name.indexOf(fieldname) != -1)
            {
                theForm.elements[i].checked = true;
                if (destination != '') checkit(destination,theForm.elements[i].value);
            }
        }
        checkflag = "true";
    } else {
        for (i=0;i<theForm.elements.length;i++)
        {
            if (theForm.elements[i].name.indexOf(fieldname) != -1)
            {
                theForm.elements[i].checked = false;
            }
        }
        checkflag = "false";
    }
}
*/

function checkAll(someForm,checkboxObj,textObj,update)
{
    // update added to keep update_field from choking on strings
    if (isUndefined(update)) update = true;

    // make array of checkbox objects on this page
    var thisArr = new Array();

     // make array of checked objects on all pages
    var totalArr = build_array(textObj.value);
    var notChecked = false;

    if (!isUndefined(checkboxObj.length))
    {
        for (var i=0; i < checkboxObj.length; i++)
        {
            thisArr[i] = checkboxObj[i].value;
        }
        for (var i=0; i < checkboxObj.length; i++)
        {
            if (checkboxObj[i].checked==false) notChecked = true;
            if (!in_array(checkboxObj[i].value,totalArr))
            {
                checkboxObj[i].checked=true;
                if (textObj.value == '')
                {
                    textObj.value = checkboxObj[i].value;
                } else {
                    textObj.value += ',' + checkboxObj[i].value;
                }
                if (update) update_field(textObj);
            }
        }
    } else {
        thisArr[0] = checkboxObj.value;
        if (checkboxObj.checked==false) notChecked = true;
        if (!in_array(checkboxObj.value,totalArr))
        {
            checkboxObj.checked=true;
            if (textObj.value == '')
            {
                textObj.value = checkboxObj.value;
            } else {
                textObj.value += ',' + checkboxObj.value;
            }
            update_field(textObj);
        }
    }

    if (notChecked==false) // DESELECT everything
    {
        textObj.value = "";

        for (var i=0; i < totalArr.length; i++)
        {
            if (!in_array(totalArr[i],thisArr))
            {
                if (textObj.value == '')
                {
                    textObj.value = totalArr[i];
                } else {
                    textObj.value += ',' + totalArr[i];
                }
            }
        }
        if (!isUndefined(checkboxObj.length))
        {
            for (var i=0; i < checkboxObj.length; i++)
            {
                checkboxObj[i].checked=false;
            }
        } else {
            checkboxObj.checked=false;
        }
    }
}


function confirmAction(message)
{
    if (message=='' || !message || message==null)
    {
        message = "Are you sure?";
    }
    var agree=confirm(message);
    if (agree)
    {
        return true ;
    } else {
        return false ;
    }
}

function preloadImages()
{
    var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function findObj(n,d)
{
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function swapImage()
{
    var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function swapImgRestore()
{
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function in_array(needle,haystack)
{
    for (j=0;j<haystack.length;j++)
    {
        if (haystack[j] == needle)  return true;
    }
    return false;
}

function inArrayPos(needle,haystack)
{
    for (j=0;j<haystack.length;j++)
    {
        if (haystack[j] == needle) return j;
    }
    return "crap";
}

function build_array(fieldvalue)
{
    var thisArray = fieldvalue.split(',');
    return thisArray;
}

function sortNumbers(a, b)
{
    return a - b;
}

function update_field(field)
{
    var update_array = build_array(field.value);
    field.value = update_array.sort(sortNumbers)
}

function checkit(field,id,update,toggle)
{
    // toggle=false prevents id from being deleted from field
    if (isUndefined(update)) update = true;
    if (isUndefined(toggle)) toggle = true;
    if (field.value != '')
    {
        var myArray = build_array(field.value);

        if (!in_array(id,myArray))
        {
            field.value += ',' + id;
            if (update) update_field(field);
        } else {
            field.value = '';
            for (n=0; n < myArray.length; n++)
            {
                if (toggle)
                {
                    if ((myArray[n] != id) && (myArray[n] != ''))
                    {
                        if (field.value == '')
                        {
                            field.value = myArray[n];
                        } else {
                            field.value += ',' + myArray[n];
                        }
                    }
                } else {
                    if (myArray[n] != '')
                    {
                        if (field.value == '')
                        {
                            field.value = myArray[n];
                        } else {
                            field.value += ',' + myArray[n];
                        }
                    }
                }
            }
        }
    } else {
        field.value = id;
    }
}

function formSubmit(someform,someaction,sometarget,confirmation,msg)
{
    if (someaction==null) someaction = "";
    if (sometarget==null) sometarget = "_self";
    if (confirmation==null) confirmation=0;
    if (msg==null) msg='';

    if (confirmation==1)
    {
        if (confirmAction(msg))
        {
            someform.action=someaction;
            someform.target=sometarget;
            someform.submit();
        }
    } else {
        someform.action=someaction;
        someform.target=sometarget;
        someform.submit();
    }
}

//global arr
clssNodeArr = new Array();
// parameters
// e = element
// v = class value
function createClassNodeArr(e,v)
{
    if(document.getElementsByTagName)//check for obj
    {
        var nodes = document.getElementsByTagName(e);
        var max = nodes.length;
        for(var i = 0;i < max;i++)
        {
            var nodeObj = nodes.item(i);
            var attrMax = nodeObj.attributes.length;
            for(var j = 0; j < attrMax; j++)
            {
                if(nodeObj.attributes.item(j).nodeName == 'class')
                {
                    if(nodeObj.attributes.item(j).nodeValue == v)
                    {
                        clssNodeArr[clssNodeArr.length] = nodeObj;
                    }
                }
            }
        }
    }
}

function styleByClass(clr)
{
    if(document.getElementsByTagName)//check for obj
    {
    var max = clssNodeArr.length;
    for(var i = 0;i < max;i++)
      {
      var nodeObj = clssNodeArr[i];
      nodeObj.style.color = clr;
      }
    }
}

function move(fbox,tbox) {
for(var i=0; i<fbox.options.length; i++) {
if(fbox.options[i].selected && fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
   }
}
BumpUp(fbox);
if (sortitems) SortD(tbox);
}
function BumpUp(box)  {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "")  {
for(var j=i; j<box.options.length-1; j++)  {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
   }
}
if(ln < box.options.length)  {
box.options.length -= 1;
BumpUp(box);
   }
}

function SortD(box)  {
var temp_opts = new Array();
var temp = new Object();
for(var i=0; i<box.options.length; i++)  {
temp_opts[i] = box.options[i];
}
for(var x=0; x<temp_opts.length-1; x++)  {
for(var y=(x+1); y<temp_opts.length; y++)  {
if(temp_opts[x].text > temp_opts[y].text)  {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
      }
   }
}
for(var i=0; i<box.options.length; i++)  {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
   }
}

function verifyReps(f,t)
{
    // f = form
    // t = fair type

    if (t=='Expo' || t=='Intern')
    {
        // debug
        // alert("Day1 value is " + f.Day1.value + "\n" + "Day2 value is " + f.Day2.value);
        if ((f.Day1.value < 1 || f.Day1.value > 6) && (f.Day2.value < 1 || f.Day2.value > 6))
        {
            f.Day2.pattern='validnum';
        } else {
            f.Day2.pattern=null;
        }
    }

    //alert(f.Day1.value);
    //alert(f.Day2.value);
}

function fieldCompare(f1,f2,field)
{
    if (field==null) field = f1.name.toUpperCase();
    if (f1.value != f2.value)
    {
        alert("The " + field + " fields do not match!");
        f1.focus();
        return false;
    }
}

function force_cookies(someform)
{
    var force_msg;

    if (document.cookie == "")
    {
        someform.cookieExists.value="false";
    } else {
        someform.cookieExists.value="true";
    }
    if (someform.cookieExists.value=="false")
    {
        force_msg  = "ERROR: This site requires cookies to function properly.\n";
        force_msg += "PLEASE ENABLE YOUR COOKIES\n";
        force_msg += "================================\n\n";
    }

    if (force_msg) alert(force_msg);
}

    function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function closeChild(newurl)
{
    if (newurl && window.opener)
    {
        window.opener.location.replace(newurl);
        window.opener.focus();
        window.setTimeout('window.close()', 1000);
    }
}
function targetURL(newurl)
{
    if (newurl && window.opener)
    {
        window.opener.location.replace(newurl);
        window.opener.focus();
        window.setTimeout('window.close()', 1000);
    } else {
        window.location.href=newurl;
    }
}

function emailUser(someform)
{
    if (someform.check_list.value == '')
    {
        alert('You must select a record first.');
        return;
    } else {
        someform.action='/myUCS/mail/index.php';
        someform.submit();
    }
}
function buildStr(somefield, somevalue, dupe)
{
    // dupe = 0 won't allow duplicate values
    if (somefield.value)
    {
        if (dupe == 0)
        {
            var tempArr = build_array(somefield.value);
            if (!in_array(somevalue,tempArr))
            {
                somefield.value += "," + somevalue;
            }
        } else {
            somefield.value += "," + somevalue;
        }
    } else {
        somefield.value = somevalue;
    }
}

function interrogate(what) /* Use this to find obj properties */
{
    var output = '';
    for (var i in what)
        output += i+ '\n';
    alert(output);
}
/*
How can I send data to the server without using a form?
You can send data back to the server by utilising any request sent back to the server.
Typically this is an image request. For example, on the client side you can do this:
*/
function senddata(variablename){
  var x = new Image();
  x.src="myscript.asp?var="+variablename;
}



function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
    // use something like this instead
    /*
    if (isUndefined(checkboxObj.length))
    {
        alert("This IS NOT an array");
    } else {
        alert("This IS an array");
    }
    */
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function copy_clip(meintext,msg)
{
    // Mozilla will squeal about security
    // change users prefs.js file: user_pref("signed.applets.codebase_principal_support", true);
    // or about:config - filter on 'applets' and change above to true

    // if msg==1, show alert

    if (window.clipboardData)
    {
        // for IE
        window.clipboardData.setData("Text", meintext);
    } else if (window.netscape) {
        // you have to sign the code to enable this, or see notes below
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext=meintext;
        str.data=copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid=Components.interfaces.nsIClipboard;
        if (!clip) return false;

        clip.setData(trans,null,clipid.kGlobalClipboard);
    }
    if (msg==1) alert("Following info was copied to your clipboard:\n\n" + meintext);
    return false;
}

function popIt()
{
    var n = window.open('about:blank', 'formwin', 'scrollbars=yes,height=250,width=350,status=no');
    return true;
}