﻿
var IE7 = ( document.all && !window.opera && window.XMLHttpRequest );
var FF2 = ( !document.layers && (
  navigator.userAgent.indexOf("Firefox/2")!=-1 || navigator.userAgent.indexOf("Firefox/3")!=-1 ||
  navigator.userAgent.indexOf("Firefox/4")!=-1 || navigator.userAgent.indexOf("Firefox/5")!=-1 ||
  navigator.userAgent.indexOf("Firefox/6")!=-1 || navigator.userAgent.indexOf("Firefox/7")!=-1 ||
  navigator.userAgent.indexOf("Firefox/8")!=-1 || navigator.userAgent.indexOf("Firefox/9")!=-1 
) );

//alert(navigator.vendor);
//alert(navigator.appVersion);
//alert(navigator.userAgent);

// for windows where the only way to make them go away is the close button
function NewShowPopup(title,url,width,height)
{
	if ( !IE7 && !FF2 )
	{
		var w = window.open(
			url,
			"_blank",
			"width="+width+",height="+height+",modal=yes,dialog=yes,"+
			"directories=no,menubar=no,toolbar=no,location=no,scrollbars=no,"+
			"status:no,resizable=yes");
	}
	else
	{
		Window.keepMultiModalWindow = true;
		var windowOptions = function() {};
		windowOptions.title = title;
		windowOptions.className="mac_os_x";//"alphacube"; 
		if ( width>0 && height>0 )
		{
			windowOptions.width = width; 
			windowOptions.height = height;
		}
		windowOptions.recenterAuto = true;
		windowOptions.url = url; 
		windowOptions.resizable=true;
		windowOptions.minimizable=false;
		windowOptions.maximizable=false;
		windowOptions.closable=true;
		windowOptions.destroyOnClose=true;
		windowOptions.showEffect = Element.show;
		windowOptions.hideEffect = Element.hide;

		var popupWindow = new Window( windowOptions );
		if ( width<1 && height<1 )
		{
			popupWindow.updateWidth();
			popupWindow.updateHeight();
		}
		popupWindow.showCenter(true);// must be this to make it modal + centered!!!
	}
}

// for windows which might have a callback, and which can be closed programmatically
function NewShowDialog(title,url,width,height,handler)
{
	if ( !IE7 && !FF2 )
	{
		windowStackTop++;
		dialogHandler[windowStackTop]=handler;
		var w = window.open(
			url,
			"_blank",
			"width="+width+",height="+height+",modal=yes,dialog=yes,"+
			"directories=no,menubar=no,toolbar=no,location=no,scrollbars=no,"+
			"status:no,resizable=yes");
	}
	else
	{
		Window.keepMultiModalWindow = true;
		top.windowStackTop++;
		top.dialogHandler[top.windowStackTop]=handler;
		var windowOptions = function() {};
		windowOptions.title = title;
		windowOptions.className="mac_os_x";//"alphacube"; 
		if ( width>0 && height>0 )
		{
			windowOptions.width = width; 
			windowOptions.height = height;
		}
		windowOptions.recenterAuto = true;
		windowOptions.url = url; 
		windowOptions.resizable=true;
		windowOptions.minimizable=false;
		windowOptions.maximizable=false;
		windowOptions.closable=true;
		windowOptions.destroyOnClose=true;
		//      windowOptions.showEffect=Effect.BlindDown;
		//      windowOptions.hideEffect=Effect.SwitchOff;
		//      windowOptions.wiredDrag=true;
		windowOptions.showEffect = Element.show;
		windowOptions.hideEffect = Element.hide;
		//      windowOptions.showEffectOptions = {duration:0.1}; 
		//      windowOptions.hideEffectOptions = {duration:0.1}; 
		//      windowOptions.okLabel = "Ok";
	  
		//      Dialog.alert( {url:url, options:{method:"get"}}, windowOptions ); // no - no javascript

		top.dialogWindow[top.windowStackTop] = new Window( windowOptions );
		if ( width<1 && height<1 )
		{
			top.dialogWindow[top.windowStackTop].updateWidth();
			top.dialogWindow[top.windowStackTop].updateHeight();
		}
		//  if ( handler!=null ) top.dialogWindow[top.windowStackTop].setCloseCallback(handler);
		top.dialogWindow[top.windowStackTop].showCenter(true);// must be this to make it modal + centered!!!
		//      dialogWindow.setDestroyOnClose();
	}
}

// same as NewShowDialog, but take a DOM ID instead of a URL
function NewShowDialogDOM(title,id,right,top,onClose)
{
	if ( !IE7 && !FF2 )
	{
		windowStackTop++;
		dialogHandler[windowStackTop]=onClose;
		var el = document.getElementById(id);
		var html = el.outerHTML;
		html = html.replace(/none/,"block");
		var w = window.open(
			"",
			"_blank",
			"width=400,height=300,modal=yes,dialog=yes,"+
			"directories=no,menubar=no,toolbar=no,location=no,scrollbars=no,"+
			"status:no,resizable=yes");
		dialogWindow[windowStackTop] = w;
		w.document.write(
			"<html>\n<head>\n<title>"+title+"</title>\n"+
			"<script src='utils.js' type='text/javascript' language='javascript'></script>\n"+
			"</head>\n<body>\n<b>"+title+"</b><br />\n"+html+"\n</body>\n</html>\n") ;  
    w.document.close();			
	}
	else
	{
		Window.keepMultiModalWindow = true;
		windowStackTop++;
		dialogHandler[windowStackTop]=null;
		var windowOptions = function() {};
		windowOptions.title = title;
		windowOptions.className="mac_os_x";//"alphacube"; 
		windowOptions.right = right; 
		windowOptions.top = top;
		windowOptions.recenterAuto = false;
		windowOptions.resizable=true;
		windowOptions.minimizable=false;
		windowOptions.maximizable=false;
		windowOptions.closable=true;
		windowOptions.destroyOnClose=true;
		windowOptions.showEffect = Element.show;
		windowOptions.hideEffect = Element.hide;
		dialogWindow[windowStackTop] = new Window( windowOptions );
		dialogWindow[windowStackTop].setContent(id, true, false);
		if ( onClose!=null ) dialogWindow[windowStackTop].setCloseCallback(onClose);
		dialogWindow[windowStackTop].show(true);// must be this to make it modal 
	}
}

// for use in conjunction with NewShowDialogDOM only
function NewCloseDialogDOM()
{
	if ( !IE7 && !FF2 )
	{
		window.close();	
		window.opener.dialogWindow[window.opener.windowStackTop]=null;
	  window.opener.dialogHandler[window.opener.windowStackTop]=null;
		window.opener.windowStackTop--;
	}
	else
	{
	  dialogWindow[windowStackTop].close(dialogWindow[windowStackTop].getId(), null);
		dialogWindow[windowStackTop]=null;
	  dialogHandler[windowStackTop]=null;
		windowStackTop--;
	}
}

function FindNewParentWindow(w)
{
  if ( w==null )
    return null;
  else if ( typeof(w.dialogWindow)!="undefined" )
    return w;
  else 
    return FindNewParentWindow(w.parent);
}

// for use in conjunction with NewShowDialog only
function NewCloseDialog(result)
{
	if ( !IE7 && !FF2 )
	{
		window.close();	
		window.opener.dialogWindow[window.opener.windowStackTop]=null;
	  window.opener.dialogHandler[window.opener.windowStackTop]=null;
		window.opener.windowStackTop--;
	}
	else
	{
		if ( typeof(result)=="undefined" )
		{
			var w=FindNewParentWindow(window);
	//    var ww=Windows.getFocusedWindow();
	  
			if ( w!=null )
			{
				w.dialogWindow[w.windowStackTop].close(w.dialogWindow[w.windowStackTop].getId(), null);
	//  window.close();
				w.dialogWindow[w.windowStackTop]=null;
				w.dialogHandler[w.windowStackTop]=null;
				w.windowStackTop--;
			}
		}
		else
		{
			var w=FindNewParentWindow(window);
			if ( w!=null ) 
			{		
				w.dialogHandler[w.windowStackTop](result);
				w.dialogWindow[w.windowStackTop].close(w.dialogWindow[w.windowStackTop].getId(), null);
				w.dialogWindow[w.windowStackTop]=null;
				w.dialogHandler[w.windowStackTop]=null;
				w.windowStackTop--;
			}
		}
	}
}

function NewSetTitle(title)        
{
  top.dialogWindow[top.windowStackTop].setTitle(title);   
//  examine(top.dialogWindow);
}

/*
// beware when accessing opener window:  
// showModalDialog does not have an opener, so passing window in as dialogArguments
// window.open has no dialogArguments, so use window.opener
//
// beware when navigating/posting back in dialogs - showModalDialog does not allow this!
function Show_Dialog(url,width,height,handler)
{
//	if ( window.document.all )
//	{
//		var result = showModalDialog( 
//			url, 
//			window, 
//			"dialogWidth:"+width+"px;dialogHeight:"+height+"px;status:no;");
//		if ( typeof(handler)!='undefined' && handler!=null ) 
//			handler(result);
//	}
//	else
//	{
		dialogHandler=handler;
		var w = window.op en(
			url,
			"_blank",
			"width="+width+",height="+height+",modal=yes,dialog=yes,"+
			"directories=no,menubar=no,toolbar=no,location=no,scrollbars=no,"+
			"status:no,resizable=yes");
//	}
}

function DialogDone(result)
{
//	if ( window.document.all )
//	{
//		window.returnValue = result;
//		window.close();
//	}
//	else
//	{
		var topFrame = window.top;
		var opener = topFrame.opener;
		if ( typeof(opener.dialogHandler)!='undefined' && opener.dialogHandler!=null ) 
		{		
			opener.dialogHandler(result);
			opener.dialogHandler=null;
		}
		topFrame.close();
//	}
}
*/

function examine(obj)
{      
  var simple = true;
    if ( obj )
    {
        var rpt = "";
        for (var item in obj)
        {
        if ( item )
        {
            try
            {
            if ( obj[item] )
            {
                if ( simple )
                rpt += item+" = "+obj[item]+"\n";
                else
                rpt += "<span style='font-weight:bold;'>"+item+" = "+obj[item]+"\n";
            }
            else
            {
                if ( simple )
                rpt += item+" = null\n";
                else
                rpt += "<span style='font-weight:bold;'>"+item+" = <span style='color:blue;'>null</span>\n";
            }
            }
            catch( e )
            {
            if ( simple )
                rpt += item+" = error\n";
            else
                rpt += "<span style='font-weight:bold;'>"+item+" = <span style='color:red;'>error</span>\n";
            }
        }
        else
        {
            if ( simple )
            rpt += "invalid member\n";
            else
            rpt += "<span style='color:red;'>invalid member</span>\n";
        }
        }
    }
    else
    {
        if ( simple )
        rpt = "null";
        else
        rpt = "<span style='color:red;'>null</span>\n";
    }
    if ( false )
        alert(rpt);
    else
    {
        var w = window.open("","_blank","status=yes,resizable=yes,scrollbars=yes,width=400,height=300");
        w.document.write("<html><body><pre>"+rpt+"</pre></body></html>\n");
        w.document.close();
    }
}

// convert date from UTC date to format required by cookies
function convertDate(dateIn)
{
	var weekdaysShort = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
  var weekdaysLong  = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
  var monthsShort = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
  var monthsLong  = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

  var day = dateIn.getUTCDay();
  var date = dateIn.getUTCDate();
  var month = dateIn.getUTCMonth();
  var year = ""+dateIn.getUTCFullYear();
  year = year.substr(2,year.length);

  var hours = dateIn.getUTCHours();
  var minutes = dateIn.getUTCMinutes();
  var seconds = dateIn.getUTCSeconds();

  //  Wdy, DD-Mon-YYYY HH:MM:SS GMT

  // Monday, 16-Apr-2001 21:53:41 GMT

  var dateOut = weekdaysLong[day]+", "+date+"-"+monthsShort[month]+"-"+year+" "+hours+":"+minutes+":"+seconds+" GMT";

  return dateOut;
}

function getCookie(theName)
{
  var search = theName + "=";

  if (window.document.cookie.length > 0)   // if there are any cookies
  {
    var offset = window.document.cookie.indexOf(search);
    if (offset != -1)               // if cookie exists
    {
      offset += search.length;
      var end = window.document.cookie.indexOf(";", offset);  // set index of beginning of value
      if (end == -1) end = window.document.cookie.length; // set index of end of cookie value
      return decodeURIComponent( document.cookie.substring(offset,end) );
    }
  }
}

function setCookie(name, value, path, domain, secure)
{
  var expireTime = new Date();
  expireTime.setTime(expireTime.getTime() + 3E11); // looooong time interval
  var dateString = convertDate(expireTime);
  document.cookie = name + "=" + encodeURIComponent(value) +
    "; expires=" + dateString +
    ((path)? "; path=" + path : "") +
    ((domain)? "; domain=" + domain : "") +
    ((secure)? "; secure" : "");
}

function setSessionCookie(name, value, path, domain, secure)
{
  document.cookie = name + "=" + encodeURIComponent(value) +
    "; " +
    ((path)? "; path=" + path : "") +
    ((domain)? "; domain=" + domain : "") +
    ((secure)? "; secure" : "");
}

// Return the x coordinate of an element relative to the page.
function getElementLeft(el)
{
  if ( el==null) return 0;
  var x = parseInt(el.offsetLeft);
  if (el.offsetParent != null)
    x += getElementLeft(el.offsetParent);

  return x;
}

// Return the y coordinate of an element relative to the page.
function getElementTop(el)
{
  if ( el==null) return 0;
  var y = parseInt(el.offsetTop);
  if (el.offsetParent != null)
    y += getElementTop(el.offsetParent);

  return y;
}
      
function WindowWidth()
{
    //Non-IE
    if( window.document.body!=null && typeof( window.document.body.clientWidth ) == 'number' )
      return window.document.body.clientWidth; 

    //IE 6+ in 'standards compliant mode'
    else if ( window.document.documentElement && window.document.documentElement.clientWidth ) 
      return window.document.documentElement.clientWidth;

    //IE 4 compatible
    else if ( window.document.body && window.document.body.clientWidth ) 
      return window.document.body.clientWidth;
}

function WindowHeight()
{
    //Non-IE
    if( typeof( window.innerHeight ) == 'number' )
      return window.innerHeight;

    //IE 6+ in 'standards compliant mode'
    else if ( window.document.documentElement && window.document.documentElement.clientHeight ) 
      return window.document.documentElement.clientHeight;

    //IE 4 compatible
    else if ( window.document.body && window.document.body.clientHeight ) 
      return window.document.body.clientHeight;
}

function diag(msg)
{
  var el = document.getElementById("diag");
  if ( el!=null ) el.innerHTML += msg+"<br>\n";  
}

function addLoadEvent(fn)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = fn;
  }
  else
  {
    window.onload = function() { oldonload(); fn(); }
  }
} 

function preview(filespec,width,height)
{
  top.NewShowPopup( "Preview "+filespec, filespec, 30+width,30+height);
  return false;
}

function help(title,name,width,height)    
{
	NewShowPopup("BC Playthings Help: "+title,"help/"+name+".aspx",width,height);
}


function ToggleAndOrderProduct(productID,caveatValue)
{
  NewCloseDialogDOM();
  if ( !IE7 && !FF2 )
	  window.opener.OrderProduct(productID,caveatValue);
	else
	  OrderProduct(productID,caveatValue);
}


