IE = navigator.appName=="Microsoft Internet Explorer";
NS = navigator.appName=="Netscape";
bVer = parseInt(navigator.appVersion);

var isMac = navigator.userAgent.toString().toUpperCase().indexOf("MAC") != -1;

var newWin=null;

function spawnWin (loc, name, width, height, tag_resize, tag_status, tag_scroll) {
    var _params = "width=" + width + ",height=" + height +
      ",resizable=" + tag_resize + ",status=" + tag_status + ",scrollbars=" + tag_scroll;
    // CENTER ON BROWSERS WHICH SUPPORT JSCRIPT 1.2
    if (bVer >= 4) {
         _left = ( (screen.width-width) >>1 );
         _top = ( (screen.height-height) >>1 );
    } else {
         _left = ( (800-width) >>1 );
         _top = ( (600-height) >>1 );
    }

    if (IE) _params += ",top=" + _top + ",left=" + _left;
    else if (NS) _params += ",screenX=" + _left + ",screenY=" + _top;

    newWin = window.open(loc, name, _params);
    if ( newWin!=null && !(IE && bVer<5) )
        newWin.focus(); // MSIE4 DOESN'T FOCUS WINDOWS
}


function popup(loc, name, width, height) {
    spawnWin(loc, name, width, height, 'no', 'no', 'no');
}

function popup_home () {
    spawnWin('home.php', 'EPYC', 788, 489, 'yes', 'no', 'no');
}

function popup_scroll (loc, name, width, height) {
    spawnWin(loc, name, width, height, 'no', 'no', 'yes');
}

function popPhoto(loc, caption) {
    prependPath = "";
    if (loc.indexOf("/") == -1)
        prependPath = "photos/";
    final_path = "getImage.php?imagePath=" + prependPath + loc + "&caption=" + caption;
    popup_scroll(final_path, "photo", 460, 490);
}

function popAudio(audio_xml) {
    final_path = "extra/audio_player.php?audioXML=" + audio_xml;
    popup(final_path, "audio", 248, 119);
}

// provocative questions
function popPQ(loc, id) {
    var final_path = loc;
    popup(final_path, "pq", 398, 300);
    var e = document.getElementById(id);
    e.style.visibility = 'hidden';
    e.style.display = 'none';
}

function antispam(name,domain) {
    document.location = "mailto:" + name + "@" + domain;
}

//version 6
function writeFlash(name, width, height, bgcolor, flashvars) {
    flashvars = flashvars!=null ? flashvars : '';
    var swf = '';
    swf += '<object';
    swf += ' width="'+width+'"';
    swf += ' height="'+height+'"';
    swf += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
    swf += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">';
    swf += '<param name="movie" value="'+name+'?'+flashvars+'">';
    swf += '<param name="FlashVars" value="'+flashvars+'">';
    swf += '<param name="bgcolor" value="'+bgcolor+'">';
    swf += '<param name="quality" value="high">';
    swf += '<param name="menu" value="false">';
    swf += '<embed src="'+name+'?'+flashvars+'"';
    swf += ' Flashvars="'+flashvars+'"';
    swf += ' width="'+width+'"';
    swf += ' height="'+height+'"';
    swf += ' bgcolor="'+bgcolor+'"';
    swf += ' quality="high"';
    swf += ' menu="false"';
    swf += ' type="application/x-shockwave-flash"';
    swf += ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
    swf += '<\/embed>';
    swf += '<\/object>';
    document.write(swf);
}

/* for Provocative Questions */

var floatX = 615;
var offsetY = 150; // 150: the magic number that works.  why?
var floatE; // the floating element
var docY;  // document height
// floating element's y coordinate.
// unfortunately there is no good way of getting that number
// element.style.top gives you a string with the measurement ('120px')
// instead of processing the string, i decided to use a global variable.
var floatY;
var totalY;

//initialize the floating element
//credit: http://www.quirksmode.org/viewport/compatibility.html
//for document height...  all the quirks.
function epyc_float(id) {
    //get document height
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight;
    if (test1 > test2) { // all but Explorer Mac
        totalY = document.body.scrollHeight;
    } else { // Explorer Mac;
        //would also work in Explorer 6 Strict, Mozilla and Safari
        totalY = document.body.offsetHeight;
    }
	if (isMac) {
		totalY += offsetY;
	} else {
		totalY -= offsetY;
	}
    //alert(totalY);
    //initialize docY, document height
    if (self.innerHeight) { // all except Explorer
        docY = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        docY = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        docY = document.body.clientHeight;
    }
    floatY = docY - offsetY;
    floatE = document.getElementById(id);
    //initial placement
    floatE.style.left = floatX + 'px';
    floatE.style.top = floatY + 'px';
    //this will keep it floating
    floatbottom();
}

function floatbottom() {
    var scrollY;
    if (self.pageYOffset) { // all except Explorer
        scrollY = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        // Explorer 6 Strict
        scrollY = document.documentElement.scrollTop;
    } else if (document.body) { // all other Explorers
        scrollY = document.body.scrollTop;
    }
    var newY = floatY + (scrollY + docY - offsetY - floatY)/8;
    if (newY <= totalY) {
        floatE.style.top = newY + 'px';
        floatY = newY;
    }
    setTimeout("floatbottom()", 10);
}

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        }
    }
    
    // test radio groups
    radio1 = valButton(f.how_hear);
    radio2 = valButton(f.how_plan);    
    
    if (radio1 == null) errors += "\"How did you hear about EPYC?\" is not answered."
    if (radio2 == null) errors += "\n\n\"How do you plan on using the EPYC curriculum?\" is not answered."
    
    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "The form was not submitted because of the following error(s).\n"
    
    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

function valButton(btn) {
    var cnt = -1;
    for (var i=0; i < btn.length; i++) {
       if (btn[i].checked) {cnt = i; i = btn.length;}
       }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}