 
/*  
   Name:  AllJavaScripts.js
   JR 01/24/2007

  Called from  AllIncludesLoadFromHere . asp
  Called from  AllIncludesForTestPages . asp   (which is called Only from TestPagePresentGeneric and TestProcessPage)
  Called from  PickBallot4SensoryTest . asp    (which does not include either of the big All Includers.... nice n simple) 
  Called from  \Default . asp      (which does not include either of the big All Includers....) 
*/


// ***************************************
//  BEGIN:  Javascript  Immediate Code 
//     06/09/2010  Moved to BELOW Functions,   error when I tried to call addToCookieArray ..
//  END:  Javascript  Immediate Code 
// ***************************************


//  BEGIN:  Just FUNCTIONS Here Down ,  Immediate Code is not VALID in here, ie Client-side scripts

/* 
    MeMaximizeWindowSize()
    PageOnloadEnvironTestingPagesOnly()
    String.prototype.trim    ' A special way to go Trim ()s... 
    Set_Cookie()
    Get_Cookie()
    getCookieArray() 
    addToCookieArray()
    parseCookieArray()
    HideElement()
    startTimeCountDown()   // 09/09/2009  Thinking about  a) Time Delay  &  b) Time Intensity  support for clients 
*/

/* 
' MeMaximize called from <BODY ONLOAD="MeMaximize()"...> , from HomePage, SensoryTestLogin.asp, UsingLineScales.asp 
'  JR 06/12/2006 This function is MIS-NAMED, more accurately would be something like MeMaximizeWindowSize().  
'   Because 'Maximize' in Windows means just that, there's no Window per se, not Window'ed.  
'   I've removed this call from HomePage, it was messing with the Window, where we would be in Maximize mode, then
'   surf to SensoryTest.com and screen would shift to Window'ed, and shifting that didn't feel right.  
'
' One call in our source was pre 07/29/2006 in SensoryTestLogin.asp <BODY ONLOAD="MeMaximize()"  
'   We retired popups and related ...  
'
'  07/31/2006  RETIRED
'    There is NO way to really maximize a Window. 
'    There is NO way to detect current window size, and then be able to decide if a 'Normal' Windows resize it needed.
'  So, I quess we'll just stop trying to mess with window sizes entirely. 

'  Despite what PL writes, the use of  availwidth & availheight  vs Width & Height  seems to me 99.9% recommended.
'  self.resizeTo(screen.availwidth,screen.availheight); //Sometimes this resize does not use whole screen.
'  self.resizeTo(screen.width,screen.height); //This line resizes the window based on current screen dimensions.
'  self.moveTo(0,0);                          //This line moves the window to x,y coordinates on the screen.
'
'
'    window.outerWidth is defined for Firefox and Netscape
'    document.body.clientWidth is defined for Internet Explorer
'    document.body.clientHeight somehow did not change when the height changed, so we rely on width only
'    When screen resolution is 800x600, document.body.clientWidth value often did not decrease below 761--strange.
' // window.moveTo(0, 0); 
' // if(navigator.appName=="Netscape") {window.outerWidth = screen.availWidth; window.outerHeight = screen.availHeight; }
' // else {window.resizeTo(screen.availWidth, screen.availHeight);}
*/


function MeMaximizeWindowSize() {
var scr_w = screen.availWidth;
var scr_h = screen.availHeight;
var browseWidth = 0;
var DoScreenResize = 0;

browseWidth = document.body.clientWidth;

//alert("\r\n screen.width=" + screen.width + "\r\n screen.height=" + screen.height + "\r\n scr_Aw=" + scr_w + "\r\n scr_Ah=" + scr_h + "\r\n browseWidth=" + browseWidth + "\r\n browseWidth/scr_w)=" + browseWidth/scr_w ); 
/*
'   JRs PC 1024x768 returns:
'    Max window normal...........:   scr_w=1024 ,  scr_h=738 ,  browseWidth=1004
'    With Fav or Search Window...:   scr_w=1024 ,  scr_h=738 ,  browseWidth=800  (this one can vary)
' 
'// browseWidth = window.outerWidth;
'// browseWidth will be "undefined" for IE, since window.outerWidth; unsupported
'// if (typeof browseWidth == "undefined") { browseWidth=document.body.clientWidth; }
*/

// JR 01/24/2007  Might need this when users browser window includes a Favorites or Search Window, 
//  thus the screen Width is not as accurate for figuring LineScale Width Adjustments.
//  11/25/2008 Only spot used is in TestCreatepages . asp,  LineScaleWidthConvert2HTML 
//   document.cookie = "DocumentBodyClientWidth="+escape(browseWidth);   06/09/2010  Free up Root Level Cookies.... 

addToCookieArray("Environment","DocumentBodyClientWidth",escape(browseWidth));

//                                                      03/15/2010 Set to .95, .98 was a tad too high, needless DoScreenResize = 1
if (browseWidth > 0 && scr_w > 500 && scr_h > 500) { if (browseWidth/scr_w < .95) {  DoScreenResize = 1; } }
if (browseWidth > 0 && scr_w > 500 && scr_h > 500) { if (scr_w == 800 && browseWidth < 770) { DoScreenResize = 1; }  }
if (DoScreenResize == 1) { window.moveTo(0, 0); window.resizeTo(scr_w, scr_h); }
}

/* 'window.history.go(1) should run on every page that you want to prevent user from going BACK to 
   'and should NOT appear on any page that you want to allow user to go BACK to
   'Appears only one function can be called from <body onload, so will make it the one below.
*/


//  Just FUNCTIONS Here Down

//   PageOnloadEnvironTestingPagesOnly   ( In AllJavaScripts . js, only does meMaxStuff & History forward attempt ) 
function PageOnloadEnvironTestingPagesOnly() {
MeMaximizeWindowSize();
history.go(1);
window.history.go(1);
}


//  This creates a trim type function.... google:  String.prototype.trim =  
//   used at least by parseCookieArray below 
// Example ms = ms.trim() 
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "")}




// COOKIES Javascript
// ******************

// Fun Testing, all four possibilities,   Cookies  Set & Read,  Simple & Array,  
//addToCookieArray("PanelistLogin","JRTestCookieArray","Value In JRTestCookieArray");
//Set_Cookie("JRTestCookieSimple","Value In JRTestCookieSimple","");
//window.alert ("This is cookie array named (PanelistLogin)(JRTestCookieArray)=" + getCookieArray("PanelistLogin","JRTestCookieArray") + "\r\r" + "This is cookie simple named (JRTestCookieSimple)=" + Get_Cookie("JRTestCookieSimple")  );

// This one WOW, shows the entire document cookie collection, everything.  Cookie Arrays too.
//   And is then Basis for reading cookies in Javascript 
//window.alert (document.cookie); 



//  Simple Cookie Usage
function Set_Cookie( name, value, expires, path, domain, secure ) 
  { 
  /* 11/25/2008   'SetCookie creates a cookie in JavaScript  
   Example:   setCookie("Test","Yes");     setCookie("FromLoginAsp","NO");

     Source:  http://techpatterns.com/downloads/javascript_cookies.php
       Current Version: 1.1.1   File Last Modified: October 04, 2007. 12:06:16 pm
 
     expires:  If no value is set for expires, it will only last as long as the current session of the visitor, 
                 and will be automatically deleted when they close their browser. 
               If the expires variable is set, make the correct expires time, the current script below will set 
                 it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24

     path:  As a good general rule, set the path to '/', the root of your website. 
             Generally 'domain' and 'secure' are not something you will be needing to use unless you set the cookie on a subdomain
 
     domain and secure:  Are not utilized. Use 'domain' on the Javascript cookie if you are using it on a subdomain, like widgets.yoursite.com
  */
  if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }

  // set current time, then add expires , everything is in milliseconds
  var today = new Date();  today.setTime( today.getTime() );
  var expires_date = new Date( today.getTime() + (expires) );
  
  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
  ( ( path ) ? ";path=" + path : "" ) + 
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
  }


// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
//     Source:  http://techpatterns.com/downloads/javascript_cookies.php
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
  }	



//  Cookie Array Usage , more complex then simple cookies.
//  11/25/2008 these THREE were originally in Panelist Register . asp


//  Cookie Array Usage
function getCookieArray(name,key) { 
  var cookies = parseCookieArray(name) 
  return cookies[key]
  }

//  Cookie Array Usage
//   Example:  addToCookieArray("Test","Value","Yes");
function addToCookieArray(cookieName,key,value) { 
  var cookies = parseCookieArray(cookieName) 
  cookies[key] = value 
  var cookieStr = cookieName + "=" 
  for (var item in cookies) 
    cookieStr += item + "=" + cookies[item] + "&" 
  cookieStr = cookieStr.substring(0,cookieStr.length-1) 
  cookieStr = cookieStr + "; path=/" 
  document.cookie = cookieStr 
  } 


// Called by above two, for  Cookie Array Usage
function parseCookieArray(name) { 
  var str = "" 
  // Then split document.cookie on semicolons. a becomes an array containing all cookies that are set for this domain and path.
  var a = document.cookie.split(";") 
  for (var i=0; i<a.length; i++) { 
    a[i] = a[i].trim() 
    if (a[i].substring(0,name.length) == name) 
      str = a[i].substring(name.length+1) 
  } 
     //    window.alert (str);  // Wow.... 
  var a = str.split("&")   // Array ... of everything past above point point... 
  var obj = new Object() 
  if (str.length) 
    for (i=0; i<a.length; i++) { 
      temp = a[i].split("=")   // 2 dimension, 0 Key  1 Value
      obj[temp[0]] = temp[1]   // builds array  
    } return obj 
  } 



// 05/21/2009 Added
function hideElement(ElementName) { document.getElementById(ElementName).style.display="none"; }


// 09/09/2009  Thinking about  a) Time Delay  &  b) Time Intensity  support for clients 
//              http://forums.digitalpoint.com/showthread.php?t=189943
function startTimeCountDown() {
 if (countdownfrom > 0)  
 { document.getElementById("countDownDiv").innerHTML = "&nbsp; " + countdownfrom + " seconds...";
   countdownfrom = countdownfrom - 1;

   if (typeof fRef.submit1 != 'undefined') { 
     fRef.submit1.disabled = true;
     fRef.submit1.style.background = 'lightgrey';
     fRef.submit1.style.background = 'ivory';
     fRef.submit1.style.background = 'whitesmoke';
     //fRef.submit1.style.tooltip = 'ALT';   // Cant get to the tip, it's part of the <img src= attributes
     //fRef.submit1.style.visibility = "hidden";
   };
   setTimeout("startTimeCountDown()", 1000);

 } else { 
   document.getElementById("countDownDiv").innerHTML = "&nbsp;  Done.    Please Press the Next Page button.";
   if (typeof fRef.submit1 != 'undefined') { 
     fRef.submit1.style.background = 'deepskyblue';
     fRef.submit1.disabled = false;   // has to be enabled for click to fire
     fRef.submit1.click();
     //fRef.submit1.style.visibility = "";

   };
 }
}
//  END:  



// ***************************************
//  BEGIN:  Javascript  Immediate Code 

// 06/09/2010  Reduce Cookies Root Level
//Set_Cookie( 'ScreenRes', "Screen="+screen.width+"x"+screen.height, '', '/', '', '' );
var msg="Screen="+screen.width+"x"+screen.height;
addToCookieArray("Environment","ScreenRes",escape(msg));

// Pre 11/25/2008
//  document.cookie = "ScreenRes="+escape("Screen="+screen.width+"x"+screen.height);
//  The escape() function encodes a string, so it can be read on all computers.

//  END:  Javascript  Immediate Code 
// ***************************************
