
// Brad, if you are integrating this code into existing hvm JavaScript, then some of these functions may already be in place. The only exception is function setPartnersLink() which is brand new and at the bottom of this file.



theHost = document.location.hostname;
local = ( /^\d+\.\d+\.\d+\.\d+$/.test( theHost ) || theHost == "" || theHost == "localhost" );
live = !local;

//----------------------------------------------------------------------------------------
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//----------------------------------------------------------------------------------------

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

//----------------------------------- String Prototype -----------------------------------
// If key is found in strToSearch then returns its value, otherwise returns null
function getVal( key, eqDelim, fieldDelim ) {

	if( !eqDelim ) eqDelim = "=";
	if( !fieldDelim ) fieldDelim = "&";

	var value = null;
	var strToSearch = this.toString();

	var KeyVals = strToSearch.split( fieldDelim );

	for( member in KeyVals ) {
		var ThisPair = KeyVals[ member ].split( eqDelim );
		if( key == ThisPair[ 0 ] ) { value = ThisPair[ 1 ]; break; }
	}
	if( typeof value == "undefined" ) value = "";
	return value;
}
String.prototype.getVal = getVal;

//----------------------------------------------------------------------------------------
/*
	This function checks cookie for access level and: (1) If found and (2) It's value is "full" then (3) It exposes the partners link
	1. For fastest response it is called at the end of the page and before page has fully loaded.
	2. For this reason it is placed in a try/catch in case the browser DOM is not ready
	3. It makes a recurrsive call to itself if an exception is thrown (not expected but possible)
*/
function setPartnersLink() {

	try {

		var hvmCookie = GetCookie( "hvmCook" );

		var accessVal = ( hvmCookie ) ? hvmCookie.getVal( "access" ) : "";

		if( accessVal == "full" )
			document.getElementById( "Partners" ).style.visibility = "visible";

		self.status = "";

	}
	catch( Err ) {

		if( local ) {
			var eMsg = "Exception caught at setPartnersLink function:\n\n";
			for( var i in Err ) eMsg += i + ": " + Err[ i ] + "\n";
			alert( eMsg );
		}

		self.status = "Exception caught at setPartnersLink function...";
		setTimeout( "setPartnersLink();", 250 );
	}
}
