﻿var OfakimWindowName = "OfekJuniorWin";
var s_CHILD_FRAME_ID = "AspSupport_CHILDFRAME_Ofakim";

function CreateTaskFromUnit(sID, bNewWindow, sBridgUrl)
//       ~~~~~~~~
{
  var sOfekUrl =  '/Teacher/TaskForm.aspx?gItemID=' + sID;
  OpenOfekUrl(bNewWindow, sBridgUrl, sOfekUrl);

}
function OpenOfekUrl(bNewWindow, sBridgUrl, sOfekUrl)
//       ~~~~~~~~
{
  try 
  {
    //check if to open in the same window
    if (bNewWindow == false || 
        window.parent == null ||
        window.parent.opener == null ||
        window.parent.opener.closed)//if the window was closed, redirect in the same page
    {
      location.href = sOfekUrl;
    }
    //try to redirect the opener window to ofek page
    else if (sBridgUrl == null || window.name != OfakimWindowName) 
    {
      window.parent.opener.location.href = sOfekUrl;
      window.close();
    }
    else
    {
      //if we are not in the same domain, try to create iframe to ofakim and redirect threw the bridge
      AspSupport_OfakimBridge(sBridgUrl, 'redirectOfek', sOfekUrl, '1');
    }
    
  }
  catch (e) 
  {
    //if we are not in the same domain, try to create iframe to ofakim
    AspSupport_OfakimBridge(sBridgUrl, 'redirectOfek', sOfekUrl, '1');
  }
  //javascript: window.close();
}


// child frame function:
function AspSupport_OfakimBridge(sBridgUrl,sAction, sParams, sRedirect)     //method similar to AspSupport_ResizeParentFrame
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
  //debugger;
  if (sBridgUrl == null)
    return;
  var s_CHILD_FRAME_SRC = sBridgUrl + "?sAction=" + sAction + "&sParams=" + sParams;
  //var s_CHILD_FRAME_SRC = "http://JuniorHigh/Bridge.aspx?sAction=" + sAction + "&sParams=" + sParams;

  if (typeof (AspSupport_aAltDomains) != "undefined" && AspSupport_aAltDomains.length > 0)
    s_CHILD_FRAME_SRC += "&alt=" + AspSupport_aAltDomains.join(",");


  // load an intermediate page in parent domain
  var oChildFrame = document.getElementById(s_CHILD_FRAME_ID);
  if (!oChildFrame) 
  {
    oChildFrame = document.createElement("iframe");
    oChildFrame.id = s_CHILD_FRAME_ID;
    oChildFrame.height = 0;
    oChildFrame.onreadystatechange = iframeStateChanged;
    document.body.appendChild(oChildFrame);
  }
  oChildFrame.src = s_CHILD_FRAME_SRC;

}
function iframeStateChanged() 
{
  var oChildFrame = document.getElementById(s_CHILD_FRAME_ID);
  if (oChildFrame && oChildFrame.readyState == 'complete') 
  {
    //after the iframe is loaded, close the child window
    window.close();
  }
} 

var ofekJuniorHs =
{
  ResizeWindow: function(isInModalWin) 
  {

    if (isInModalWin){
      var maxX = screen.width;
      var maxY = screen.height - 25;
      var modalWin = window;
      modalWin.moveTo(0, 0);
      modalWin.resizeTo(maxX, maxY);
    }

  }
}
function OpenerIsOfek() 
{
  if (window.parent.opener.location.href)
    return true;
  return false;
}
//===========================================================
/// the function copy given text to clipboard
function CopyToClipbord(text2copy) 
{
  //~~~~~~~~~~~~~~~~~~~~~~
  if (window.clipboardData) 
  {
    window.clipboardData.setData("Text", text2copy);
  }
  else 
  {
    var flashcopier = 'flashcopier';
    if (!document.getElementById(flashcopier)) 
    {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard=' + escape(text2copy) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}
