function nudge(o) {
  var w=getWinW(), h=getWinH();
  for(i=0;i<o.childNodes.length;i++)
    if(o.childNodes[i].nodeName == 'SPAN') s = o.childNodes[i];
  if(s)
    if(gW(s))  {
      if(getAbsX(s)+gW(s)+20>w) sX(s,gX(s)-(getAbsX(s)+gW(s)-w)-20)
      if(getAbsY(s)+gH(s)>h) sY(s,gY(s)-(getAbsY(s)+gH(s)-h)-(gY(s)*2))
    } else  {
      // Adhoc hack for IE since it can't get width/height of span
      // If you change the width in the stylesheet you'll need to
      // change this too. Height is just a guess
      if(getAbsX(s)+300+25>w) sX(s,gX(s)-(getAbsX(s)+300-w)-20)
      if(getAbsY(s)+200>h) sY(s,gY(s)-(getAbsY(s)+200-h))
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0A
  // Butchered for WPS which needs to rewrite URL's
  window.open(resolvePath(theURL),winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0 - Modified by Smitty
  var lw,lh,ww,wh,i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  	if ((obj=MM_findObj(args[i]))!=null) { 
  		v=args[i+2];
    	if (obj.style) { 
			  obj=obj.style; 
			  v=(v=='show')?'visible':(v='hide')?'hidden':v; 
		  }
      //Get Heights & Widths
      if(IE)  {
        lw = new Number(obj.width.substring(0,obj.width.length-2));
        lh = new Number(obj.height.substring(0,obj.height.length-2));
        ww = document.body.scrollWidth;
        wh = document.body.scrollHeight;
      } else  {
        lw = obj.clip.width;
        lh = obj.clip.height;
        ww = window.innerWidth;
        wh = window.innerHeight;
      }

      if((mouseX+40+lw) > ww)
        if(mouseX-20-lw>0)
          obj.left = mouseX-20-lw;
        else
          obj.left = 0;
      else
        obj.left = (mouseX + 10);

      if((mouseY+40+lh) > wh)
        if(mouseY-20-lh>0)
          obj.top = mouseY-20-lh;
        else
          obj.top = 0;
      else
        obj.top = (mouseY + 15);
      
      //set visibility
    	obj.visibility=v;
  	}
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

var mouseX = 0
var mouseY = 0

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){tempX = 0}
  if (mouseY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  // document.Show.MouseX.value = tempX
  // document.Show.MouseY.value = tempY
  return true;
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function choosepage(dref,rref)  {
  if(getCookie('party') == 'd')
    document.location = resolvePath(dref);
  if(getCookie('party') == 'r')
    document.location = resolvePath(rref);
}

function resolvePath(myLink) {
        var myURL = '';
        //trim off last seven character beyond the .html
        var trimEnd = myLink.length - 7;
        //start 10 characters into the value of myLink
        //I know 10 character will get us beyond the a, href, and = sign
        for ( var i=10; i<trimEnd; i++ ) {
                myURL = myURL + myLink.charAt(i);
        }
        //use a RegExp() to get the pattern of a CURL
        var myPattern = /[01],\d*,\d+(-(\w*))?,\d*(\.utf8)?\.html/g;
        // Fix for new URL syntax:
        var urlStartIdx = myLink.indexOf('"');
        var urlEndIdx = myLink.indexOf('"', urlStartIdx + 1);
        var myNewURL = myLink.substring(urlStartIdx + 1, urlEndIdx);
        var myCURL = myPattern.exec(myURL);
        return(myNewURL);
}

function getSurveyVariable(variable, str) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      if(pair[1] > 0)
        return document.write("<span class=\"body1\">"+str+" ("+pair[1]+")</span>");
      else
        if(variable.indexOf("TextBox") == 0)
          return document.write("<span class=\"body1\"><b>"+str+" ("+Math.abs(pair[1])+")</b></span>");
        else
          return document.write("<span class=\"body_green\">"+str+" ("+Math.abs(pair[1])+")</span>");
    }
  } 
  return document.write("<span class=\"body1\">"+str+" (0)</span>");
}

function submitSurvey(survey)  {
  if(!survey) var survey = document.forms[0];var j=0;var QStr;var err = new Array();
  var QPat = /(RadioGroup|CheckGroup|TextBox)(\d*)/;
  var QPat2 = /[a-zA-Z]*\d*_(\d*)/;
  var QPat3 = /([a-zA-Z]*\d*)[a-zA-Z]*_(\d*)/;
  var NPat = /^\d*$/;
  var NPat2 = /^(\d*).(\d*)$/;
  var CheckGroups = new Array()

  for (var i in survey) {
    var QArr = QPat.exec(i);
    if(QArr)  {
      if(QArr[0] != QStr)  {
        QStr = QArr[0];
        j++;
      }
      // Text Boxes can be empty but if they contain anything it must be a number
      // and it needs 0<=Number && Number<=Max
      if(QArr[1] == "TextBox")  {
        QArr = QPat2.exec(i);
        NArr = NPat.exec(survey[i].value);
        if(survey[i].value && !(NArr && QArr && Number(survey[i].value) >= 0 && Number(survey[i].value) <= Number(QArr[1])))
          err[err.length] = "    Q"+j+") Please enter an integer between 0 and "+QArr[1];
      } 
      // Some CheckGroups have a limit on the number of options they can choose
      if(QArr[1] == "CheckGroup")  {
        QArr = QPat3.exec(i);
        if(QArr && survey[i].checked)  {
          //alert("PreCheck: "+QArr[1]);
          if(!CheckGroups[QArr[1]])  {
            CheckGroups[QArr[1]] = new Array();
            CheckGroups[QArr[1]].req   = QArr[2];            
            CheckGroups[QArr[1]].qnum  = j;            
            CheckGroups[QArr[1]].count = 1;
          } else
            CheckGroups[QArr[1]].count += 1;
            
          if((CheckGroups[QArr[1]].count > QArr[2]) && !CheckGroups[QArr[1]].done)  {
            err[err.length] = "    Q"+j+") Please choose only "+QArr[2]+" options";
            CheckGroups[QArr[1]].done = 1;
          }
        }
      }
    }
  }
  
  // For those checkgroups that were answered see if they have picked the right amount
  for(var i in CheckGroups)  {
    //alert("Post check: "+i);
    QArr = QPat3.exec(i);
    NArr = NPat2.exec(CheckGroups[i]);
    if((CheckGroups[i].count < CheckGroups[i].req) && !CheckGroups[i].done)
      err[err.length] = "    Q"+CheckGroups[i].qnum+") Please choose "+(CheckGroups[i].req - CheckGroups[i].count)+" more options";
  }
  
  if(err.length)  {
    err.sort();
    alert("Please Make the following corrections to your submission:\n"+err.join("\n"));
  } else
    survey.submit();
}
