function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function getSelectedValue(fld) {
	return fld[fld.selectedIndex].value
}

function popupDiv(divid,action,menuActive) {
	if (document.body.clientHeight < 600) {
	  var clientHeight = window.innerHeight;
	 } else {
	  var clientHeight = document.body.clientHeight;
	 }
	 if (clientHeight == undefined) { var clientHeight = document.body.clientHeight } 
	var clientWidth = document.body.clientWidth;
 	var div = document.getElementById(divid);
	if (action == 'show') {
		var x = document.getElementsByTagName('div');
		if (menuActive == 1) { var nz = 0 } else { var nz = 10000 };
		for (var i=0; i<x.length; i++) {
			if (x[i].style.zindex > nz) { nz = x[i].style.zindex }
		}
		nz = nz + 2;
		div.style.display = 'inline';
    	div.style.top = ((clientHeight / 2) - (div.clientHeight / 2)) + "px";
    	div.style.left = ((clientWidth / 2) - (div.clientWidth / 2)) + "px";
		div.style.zIndex = nz;
		nz = nz - 1;
		
	var blackout = document.getElementById('blackout');
	if (!blackout) {
		var blackout = document.createElement('div');
	}
    document.body.appendChild(blackout); 
	blackout.id = 'blackout';
	blackout.style.width = clientWidth;
	blackout.style.height = clientHeight;
	blackout.style.position = 'fixed';
	blackout.style.top = 0;
	blackout.style.left = 0;
	blackout.style.display = 'inline';
	blackout.style.zIndex = nz;
	blackout.style.opacity = .50;
	blackout.innerHTML = '&nbsp';
	} else {
	  div.style.display = 'none';
	  document.getElementById('blackout').style.display = 'none';
	}
}

function selectField(fld) {
  var def = GetCookie(fld + ";default");
  var fldObj = document.getElementById(fld);
  
  if (fldObj.value == def) { fldObj.value="" };
  fldObj.className="field-input-selected";
}

function deselectField(fld) {
  var def = GetCookie(fld + ";default");
  var fldObj = document.getElementById(fld);
  if ((fldObj.value !== def) && (fldObj.value !== null) && (fldObj.value !== "")) {
    SetCookie(fld + ";value",fldObj.value,"",365);
  } else {
    fldObj.value = def;
  }
  fldObj.className="field-input";
}
 
function GetCookie(name) {
            var cookies = document.cookie;            // cookies for site
            var ix1;                                  // loop index
            var n1 = name;                            // main cookie name
            var n2;                                   // crumb name
            var out;                                  // returned value
            ix1 = n1.indexOf(";");                    // cookie/crumb break
            if (ix1 != -1) {
              n2 = n1.substring(ix1 + 1);             // crumb name
              n1 = n1.substring(0, ix1);              // main cookie name
            }
            out = GetCookieCrumb(cookies, n1);
            if (n2 && out) { out = GetCookieCrumb(out, n2); }
            cookie = rest;
            return out;
          }
          
          
          function SetCookie(name, value, expires, days) {
            var cookies = document.cookie;            // cookies for site
            var ix1;                                  // index
            var n1 = name;                            // main cookie name
            var n2 = "";                              // crumb name
            var out;                                  // returned value
            var p2 = value;                           // new value
            var p3 = expires;                         // param work area
            var p4 = days;
            var msd = 1000 * 60 * 60 * 24;            // millisecs per day
            if (!p3) {                                // no expiration
              if (!p4) { p4 = 1; }                    // no days: use 1
              p3 = new Date();
              p3.setTime(p3.getTime() + msd * days);
            }
            ix1 = n1.indexOf(";");                    // cookie/crumb break
            if (ix1 != -1) {
              n2 = n1.substring(ix1 + 1);             // crumb name
              n1 = n1.substring(0, ix1);              // main cookie name
            }
            out = GetCookieCrumb(cookies, n1);
            if (n2) {
              if (out) { out = GetCookieCrumb(out, n2); }
              else { rest = ""; }
              if (rest) { p2 = rest + ";" + n2 + "=" + p2; }
              else      { p2 =              n2 + "=" + p2; }
            }
            document.cookie=n1+"="+escape(p2)+"; expires="+p3.toGMTString();
            return true;
          }
          function GetCookieCrumb(instring, seeking) {
            var c1;                                   // cookies work area
            var c2;                                   // cookie work area
            var c3;                                   // cookie name
            var c4;                                   // cookie value
            var ix1; var ix2; var ix3; var ix4;       // indexes
            var out;                                  // return value
            rest = "";          
            c1 = instring;
            c1 = c1.replace(/\n/g, "");               // drop nl chars
            c1 = c1.replace(/\r/g, "");               // drop cr chars
            c1 = c1 + ";";                            // for final cookie
            ix1 = 0;                                  // from beginning
            if (seeking.substring(0, 1) == "#") {
              if (seeking == "#0" && !instring) { return 0; }
              ix4 = seeking.substring(1);             // which item wanten
              ix3 = 0;                                // none so far
              while (c1.indexOf(";", ix1) != -1) {
                ix2 = c1.indexOf(";", ix1);
                cookie = c1.substring(ix1, ix2);
                ix3 += 1;
                if (ix3 == ix4) { return cookie; }
                ix1 = ix2 + 1;
              }
              if (ix4 == 0) { return ix3; }           // count of items
              return "";
            }
            while (c1.indexOf(";", ix1) != -1) {
              ix2 = c1.indexOf(";", ix1);             // next cookie break
              if (ix2 == -1) { ix2 = c1.length; }
              cookie = c1.substring(ix1, ix2);        // a single cookie
              c2 = cookie;                            // cookie work area
              ix1 = ix2 + 1;
              ix3 = c2.indexOf("=", 0);               // name=value break
              if (ix3 == -1) { ix3 = c2.length; }
              c3 = c2.substring(0, ix3);              // get the name
              c4 = c2.substring(ix3 + 1);             // get the value
              c3 = c3.replace(/ /g, "");              // drop all space
              if (c3 == seeking) {
                out = unescape(c4);
              } else {
                if (rest) { rest = rest + ";" + cookie; }
                else      { rest = cookie; }
              }
            }
            return out;
          }


function dialogSave(dlgDivName,fldname) {
	var fld1 = document.getElementById('fakeinput1')
	var fld2 = document.getElementById(fldname)
	
	if (dlgDivName == 'uploadDialog') { 
		setDisplayFileName(fld1.value, "file")
		//fld2.value = ''
	}
	if (dlgDivName == 'copyPasteDialog') {
		if (fld2.value.length < 40) { var maxL = fld2.value.length - 1 } else { maxL = 40 }
		var dspval = Mid(fld2.value,0,maxL) + " ..."
		setDisplayFileName(dspval, "text")
		//fld1.value = ''
	}
  	popupDiv(dlgDivName,'hide')
}

function initInputFields(fldarray) {
  for (var i=0;i<fldarray.length;i++) {
    var obj = document.getElementById(fldarray[i]);
	var def = GetCookie(fldarray[i] + ";default");
	var val = GetCookie(fldarray[i] + ";value");
	if ((def=="") || (def==null) || (def==undefined)) {
	  SetCookie(fldarray[i] + ";default",obj.value,"",365);
	} else {
	  if (val == undefined) {
	    obj.value = def;
	  } else {
	    obj.value = val;
	  }
	}
  }
}

function playMovie(action) {
  if (action) {
    popupDiv("thinkMovie","show");
    var html = "<div style='z-index:11000;'>";
    html += "<embed src='http://www.impactmovie.com/thinkresources/thinkresources_wb.swf' quality='high' ";
    html += "wmode='transparent' pluginspage='http://www.macromedia.com/go/getflashplayer' ";
    html += "type='application/x-shockwave-flash' width='720' height='426' style='z-index:12000;'></embed></div>";
  } else {
    popupDiv("thinkMovie","hide");
    var html = "";
  }
  document.getElementById("moviescreen").innerHTML = html;
}

function setDisplayFileName(val, type) {
	var obj = document.getElementById('dspFileName')
	var hrefEdit
	
	if (type == 'file') {
		hrefEdit = "<a href=\"javascript: popupDiv('uploadDialog','show');\" style=\"text-decoration: underline;\">(edit)</a>"
	} else {
		 hrefEdit = "<a href=\"javascript: popupDiv('copypasteDialog','show');\" style=\"text-decoration: underline;\">(edit)</a>"
	}
	obj.innerHTML = val + "&nbsp;&nbsp;" + hrefEdit
}

function Mid(str, start, len) {
  if (start < 0 || len < 0) return "";
  var iEnd, iLen = String(str).length;
  if (start + len > iLen) {
    iEnd = iLen;
  } else {
    iEnd = start + len;
  return String(str).substring(start,iEnd);
	}
}
