//// Generic flags
var gbDebug=false;     // by default, can be changed by param
var gbCookiesOK=true;  // by default, will be tested later


function PopupPrivacy()
{
	var s="Privacy Statement: \n\nThis web site does not collect personally identifiable information about you unless you provide the information in the contact form or in a form required to obtain the license to use some content available through the web site.\n\nOstyn Consulting will not disclose a user's personally identifiable information to any third party without the user's express permission. We do not sell, rent or lease our customer lists to third parties. Ostyn Consulting may, from time to time, contact you about a particular offering that may be of interest to you. Ostyn Consulting will only disclose your personal information, without notice, if required to do so by law or in the good faith belief that such action is necessary to: (a) conform to the edicts of the law or comply with legal process served on Ostyn Consulting or the site; (b) protect and defend the rights or property of Ostyn Consulting; and, (c) act under exigent circumstances to protect the personal safety of users of Ostyn Consulting, its web sites, or the public."

alert(s)
}

///// Generic Utility functions ////
/// Utility functions ///
function trim(str)
{
  if (str == null) return "";
  return str.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

function compareStr(sA,sB)
{
  if (sA > sB) return 1;
  if (sA < sB) return -1;
  return 0;
}

function ReplaceLineBreaksForDisplay(s)
{
	return s.replace(/^\n*/, "\n");
}

function IsUnsafeString(s)
{
  // True if any char < "!" that is not tab, CR or LF
  var c;
  for (var i=0;i<s.length;i++)
  {
    c = s.charCodeAt(i);
    if ((c < 33)&&((c!=9)&&(c!=10)&&(c!=13))) return true;
  }
  return false;
}

function pseudoPrintf(s)
{
  // Replace placeholders in the form #1 #2 etc. in s with
  // corresponding strings passed as extra arguments
  if (arguments.length < 2) return s;
  var re = new RegExp("x1", "g");
  for (var i=arguments.length-1;i>0;i--)
  {
    re.compile("#" + i,"g");
    s = s.replace(re, arguments[i] + "");
  }
  return s;
}

//// Generic URL related functions

//// URL path parsing and massaging ////

function FileNameToUrl(url)
{
  // Stomp those backslashes
  p = url.indexOf(":\\");
  if (p < 0) return url;
  if (p == 2) url = url.substr(1); // clear leading backslash
  rExp = /\\/gi;
  url = url.replace(rExp,"/");
  return url;
}

function GetUrlDomain(url)
{
	if (!url) url = window.location.href;
	url = FileNameToUrl(url);
	var p = url.indexOf("://");
	if (p < 0) return url; // oops!
	url = url.substr(p+2);
	if (url.indexOf("/") == 0) url = url.substr(1);
	p = url.indexOf("/");
	if (p < 0) return url; // should never happen
	return "domain is:" + url.substr(0,p);
}

function GetLocalPath(url)
{
  if (url) s = url;
  else s = FileNameToUrl(window.location.href);
  p = s.indexOf("?");
  if (p > -1) s = s.substr(0,p);
  p = s.lastIndexOf("/");
  s = s.substr(0,p+1);
  return s;
}

function GetFileName(url)
{
	if (!url) url = window.location.href;
	var p = url.indexOf("?");
	if (p >= 0) url = url.substr(0,p);
	p = url.indexOf("#");
	if (p >= 0) url = url.substr(0,p);
	p = url.lastIndexOf("/");
	if (p >= 0) url = url.substr(p+1);
	return url;
}

function RelativeUrl(refUrl,url)
{
  // Returns the relative, shortened version of the url param
  // Works only if refUrl is in a parent path of the url path
  var s1 = LocalPath(refUrl);
  if (url.indexOf(s1) == 0)
  {
    url = url.substr(s1.length);
  }
  return url;
}

function FixUrlForSanity(url)
{
  // Changes a relative URL into a fully qualified URL
  // that is suitable for browser launch in a popup.
  url = FileNameToUrl(url);
  if ((url.indexOf(":/")!= 1) && (url.indexOf("http:") != 0) && (url.indexOf("file:") != 0))
  {
    url = LocalPath() + url;
  }
  // Sanity fix if launching in file system, e.g. launching in test suite
  if ((url.indexOf("http:") != 0) && (url.indexOf("file://") != 0))
  {
    url = "file:///" + url;
  }
  return url
}

function urlpath(url){
	var pth = url;
	if (pth == null) {
		pth = window.location.href;
	}
	var p = pth.lastIndexOf("/");
	if (p > 0){
		pth = pth.substr(0,++p);
	} else {
		p = pth.lastIndexOf("\\");
		if (p > 0){
			pth = pth.substr(0,++p);
		}
	}
	//alert(pth);
	return pth
}

var gTargetUrl = null;
var ggWndExtPop = null;

function PopExternalLink(url)
{
	//alert(url);
	gsExtTargetUrl = url;
	gTargetUrl = url;
	if ((ggWndExtPop) && (!(ggWndExtPop.closed))) {
		try
		{
			ggWndExtPop.location.href = "about:blank";
			ggWndExtPop.focus();
		}
		catch (e) {};
		try
		{
			ggWndExtPop.location.href = url;
		}
		catch (e)
		{
			window.open(url);
		}
	}
	else
	{
		ggWndExtPop= window.open(url);
	}
}




//// Parameter extraction /////

function URLParams2Array()
{
  var s  = location.search;    // Strips query string from URL.

  if (s.length > 0)
  {
    if (s.charAt(0) == "?") s = s.substr(1);
    if (s.length > 0)
    {
      var aNamVals = s.split("&");
      var aNamVal = null;
      var v = "";
      var Pattern1 = new RegExp(/\+/g);
      for (var i=0;i<aNamVals.length;i++)
      {
        aNamVal = aNamVals[i].split("=");
        v = aNamVal[1];
        if ((v)&&(aNamVal[0].length>0))
        {
          v = v.replace(Pattern1, " ");
          gaURLParams[gaURLParams.length] =
            new Array(aNamVal[0].toUpperCase(),unescape(v));
        }
      }
    }
  }
}

function GetURLParam(nam)
{
  nam=nam.toUpperCase();
  for (i=0;i<gaURLParams.length;i++)
  {
    if (gaURLParams[i][0]==nam)
    {
      return gaURLParams[i][1];
    }
  }
  return ""
}

function GetBoolParam(nam, defVal)
{
  var v = GetURLParam(nam).toUpperCase();
  if ((v.indexOf("T")==0)||(v.indexOf("Y")==0)||(v=="1")) return true;
  if ((v.indexOf("F")==0)||(v.indexOf("N")==0)||(v=="0")) return false;
  return (defVal == true);
}

function GetIntParam(nam, defVal)
{
  var v = parseInt(GetURLParam(nam));
  return ((!isNaN(v))? v : defVal);
}

function GetStringParam(nam, defVal)
{
  var v = GetURLParam(nam)
  return ((v != "")? v : defVal+"");
}

/// Read the URL parameters now ////

URLParams2Array();


//// Generic cookie functions ////

var gCookieExpDate = new Date ();

// Mac date bug fix
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function SetCookie (name,value,expires,path,domain,secure) {
// first 2 params are required, others optional
  //if (!(domain)) { domain = "Saranjan"; }
  if (!(expires)) { expires = gCookieExpDate ;}
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

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 GetCookie(nam) {
// Parses document's cookie for a named value
// Weird coding supports both IE6 and Mozilla
  var a = document.cookie.split(";");
  var aCk = null;
  for (i=0; i<a.length;i++)
  {
    if (a[i].indexOf("=") < 0)
    {
      continue;
    }
    aCk = a[i].split("=");
    if (trim(aCk[0]) == nam)
    {
      if (aCk.length > 0)
      {
        return unescape(aCk[1]);
      }
      else
      {
        break;
      }
    }
  }
  return "";
}

function GetCookieList()
{
  // Returns an array of cookie names
  // IE uses the form 'name;' for empty cookies,
  // while moz uses the form 'name=;'
  var a = document.cookie.split(";");
  var aNams = new Array();
  for (i=0; i<a.length;i++)
  {
    if (a[i].indexOf("=") > -1)
    {
      aNams[aNams.length] = trim((a[i].split("="))[0]);
    }
    else
    {
      aNams[aNams.length] = trim(a[i]);
    }
  }
  return aNams.sort(compareStr);
}

function ClearAllCookies(path, domain)
{
  var aNams = GetCookieList();
  for (i=0;i<aNams.length;i++)
  {
    document.cookie = aNams[i] + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

FixCookieDate (gCookieExpDate); // Correct for Mac date bug - call only once for given Date object!

// Arbitrary expiration date set to 1 year for this app.
gCookieExpDate.setTime (gCookieExpDate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 * 24 hrs from now

function GetWeirdCookie(nam){
  var s1 = GetCookie(nam);
  var s = '';
  if (s1 != null) {
    for (i=0;i<s1.length;i++) {
      n = s1.charCodeAt(i) - 1;
      s += String.fromCharCode(n);
    }
  }
  return s;
}

function SetWeirdCookie(nam,val) {
  var s = '';
  if (val != null) {
    var s1 = val + '';
    for (i=0;i<s1.length;i++) {
      n = s1.charCodeAt(i) + 1;
      s += String.fromCharCode(n);
    }
  }
  SetCookie(nam,s);
}

function clearCookies(aNames) {
  if (aNames) {
    for (i=0;i<aNames.length;i++){
      DeleteCookie(aNames[i])
    }
  }
}

// End of generic cookie functions

///////// Initializations from cookie

var gbUseCookie = (GetCookie("CookieOK") == "true");
function SetAllowCookie(bAllow)
{
	gbUseCookie = (bAllow == true);
	if (gbUseCookie) SetCookie("CookieOK", gbUseCookie);
	else ClearAllCookies();
}



//alert("script ok")

function CheckFrame()
{
	var topDomain = null;
	if (window != window.top)
	{
		try
		{
			var topDomain = GetUrlDomain(window.top.location.href)
		}
		catch (e) { } // ignore any error here
		if (topDomain != GetUrlDomain(window.location.href))
		{
			window.top.location.href = window.location.href;
			// No mais des fois. Sont pas vite gênés!
			return false; // No point in continuing our load stuff here.
		}
	}
	return true;
}

// See style sheet for the meaning of this
sfHover = function()
{
  var sfEls = document.getElementById("nav");
  if (!sfEls) return;
  sfEls = sfEls.getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++)
  {
    sfEls[i].onmouseover=function()
    {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function()
    {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}

/***  Load and unload event management functions ***
 These functions allow the generic script to be invoked
 to initialize and terminate the session without having to add
 onunload and onunload handlers to the body or frameset element
 of the actual SCO. In other words, by just including this
 script these functions can turn just about any web page into a SCO.
 The existing onload, onbeforeunload and onunload handlers
 that may be specified in the body or frameset tag for the web
 page are preserved. See the note about precedence below.
*/


// Relay function for onload event
function _InitPage()
{
  if (CheckFrame())
  {
  	sfHover();
  	HighlightCurrentSection();
  }
  return;
}

// Relay function for onunload event
function _UnloadPage()
{
  //alert("unload detected");
  return;
}

// Important difference in behavior between IE and Firefox
// In IE, the event handler added by this script will execute
// after the event handler defined in the body tag, if any.
// In FF, the event handler added by this script will execute
// before the event handler defined in the body tag, if any.

// Inspired by http://www.tek-tips.com/faqs.cfm?fid=4862
function AddLoadAndUnloadEvents()
{
  var sfL = "_InitPage";
  var sfU = "_UnloadPage";
  var sfB = "_Scorm_TerminateSessionBeforeUnload";
  var fL = window._InitPage;
  var fU = window._UnloadPage;
  var fB = window._Scorm_TerminateSessionBeforeUnload;
  if (typeof(window.addEventListener) != "undefined")
  {
    // alert("addEventListener") // this fires off in FireFox
    window.addEventListener("load", fL, false );
    window.addEventListener("unload", fU, false );
  }
  else if (typeof(window.attachEvent) != "undefined" )
  {
    // alert("attachEvent") // this fires off in IE 6
    window.attachEvent("onload", fL);
    window.attachEvent("onunload", fU);
  }
  {
    var oldFunc;
    if (window.onload != null)
    {
      oldFunc = window.onload;
      window.onload = function ( e ) {
        oldFunc( e );
        fL();
      };
    }
    else
    {
      window.onload = fL;
    }
    if (window.onunload != null)
    {
      oldFunc = window.onunload;
      window.onunload = function ( e ) {
        oldFunc( e );
        fU();
      };
    }
    else
    {
      window.onunload = fU;
    }
  }
}

AddLoadAndUnloadEvents();

/*** End load and unload event management functions ***/


// Highlight current section of the web site.
// This is done every time a page is entered.
// The name of the page is looked up in a table
// to determine the ID of the top navigation link
// that must be highlighted.
// Then the link is sought and the style classname
// of "current" is added to its style.
// The table is generated automatically by a XSL
// and is stored in the variable gaNavMap in
// in the include file "ostynSiteMap.js"

function HighlightCurrentSection()
{
	if (typeof(gaNavMap) == "undefined") return;
	var fNam = GetFileName();
	var idNav = null;

	//alert("Trying to highlight for '" + fNam + "'");
	for (var i=0; i<gaNavMap.length;i++)
	{
		if (gaNavMap[i][0] == fNam)
		{
			idNav = gaNavMap[i][1];
			break;
		}
	}
	if (idNav)
	{
		var o = document.getElementById(idNav+"")
		if (o)
		{
			o.className = "current";

			// debug
			var o2 = document.getElementById("resourcesCompetency");
		}
	}
}


//alert("OK");

var gaNavMap = new Array( ['index.html','home'],['services.htm','services'], ['experience.htm','services'], ['ratecard.htm','services'], ['resources.htm','resources'], ['resbigpicture.htm','resources'], ['standards.htm','resources'], ['rescompetency.htm','resources'], ['resscorm.htm','resources'], ['resscormtech.htm','resources'], ['resdownloads.htm','resources'], ['resbiblio.htm','resources'], ['contact.htm','contact'],['blog.htm','blog'],['','']);