//----------------------------------save the parent Portal
function addParentWindow(parentName)
{
	registerParentPortal(parentName, "InternalWindows");
}

//------------------metods used by the outside applications
function open_no_buttons_generic(URL,name,winWidth,winHeight)
{
    open_no_buttons(URL,name,"InternalWindows",winWidth,winHeight);
}
function open_default_generic(URL,name) 
{
 	open_default(URL,name,"InternalWindows","");
}

function closePopup_generic(refreshParent, url)
{
	closePopup(refreshParent,url,"InternalWindows");
}

function open_specific_generic(URL,name,parameters)
{
	open_specific(URL,name,"InternalWindows",parameters);
}
function removeWindow_generic(windowName)
{
	removeWindow(windowName, "InternalWindows");
}

function open_no_buttons_generic_max_size(URL,name)
{
    open_no_buttons(URL,name,"InternalWindows", screen.availWidth-10,screen.availHeight-50);
}
function open_no_buttons_generic_append_param_max_size(URL,control,name)
{
    open_no_buttons_generic_append_param(URL,control,name,screen.availWidth-10,screen.availHeight-50);
}
function open_no_buttons_generic_append_param(URL,control,name,winWidth,winHeight)
{
	var elem = document.getElementById(control);
    open_no_buttons(URL+elem.value,name,"InternalWindows",winWidth,winHeight);
}

function process_enter_key(event, control)
{
	// Capture the enter key and invoke the corresponding anchor tag
	if (event && event.keyCode == 13)
	{
		var anchorName = control + ".anchor";
		var anchorElement = document.getElementById(anchorName);
		anchorElement.click();
		return true;
	}
	else 
	{
		return true;
	}
}

//----------------------------open and close window methods

function closePopup(refreshParent, url, groupName)
{
	//if refresh requested  on the parent, do the refresh
	if (refreshParent && url !=null && url != "") 
	{
		//changed to window.parent for timeouts inside HRLeave iframe
		window.parent.location.href=url;		
		window.focus();
	}

	//retrieve the cookie with the groupName
	var winNames = GetCookie(groupName);

	if (winNames != null)
	{
		//convert into array
		var bites = winNames.split(";");
	    for (i=0; i < bites.length; i++) 
	    {
	      nextbite = bites[i].split("="); // break into name and value
          winName = unescape(nextbite[1]); // return value
      	  var winRef = window.open("/sharedcontent/close.html",winName);
	    }	
    }
    //reset the cookie
	SetCookie(groupName, '', null, '/'); 
	      		
}

function open_no_buttons(URL,name,groupName, winWidth,winHeight)
{
  	var options = "width=" + winWidth + ",height=" + winHeight +            
   	 ",scrollbars=yes,resizable=yes,left=0,top=0,menubar=yes,status=yes,toolbar=no,directories=no,history=no";
    open_specific(URL,name,groupName,options);
}


function open_default(URL,name,groupName) 
{
 	open_specific(URL,name,groupName,"");
}

function open_specific(URL,name,groupName, parameters)
{
	var added = false;
	var newWin;
	if(URL)
	{
		if(registered(name, groupName))
		{
			newWin = window.open(URL,name,parameters);
  			newWin.focus();	
  		}
  		else
  		{
			newWin = window.open(URL,name,parameters);						
  			registerWindow(name, groupName);
  			newWin.focus();  
  		}
	}
}



function open_link(URL,winWidth,winHeight) {
  var options = "width=" + winWidth + ",height=" + winHeight +            
",scrollbars=yes,resizable=yes,left=0,top=0,menubar=no,status=no,toolbar=no,location=no,directories=no,history=no";
  
  if (URL) {
    // Open a new window and show the specified page
    window.open(URL,'newWin',options);
  }
}

function open_help_system(URL) {
  var options = "width=600,height=500,scrollbars=yes,resizable=yes,left=0,top=0,menubar=no,status=no,toolbar=no,location=no,directories=no,history=no";
  
  if (URL) {
    // Open a new window and show the specified page
    window.open(URL,'helpSystem',options);
  }
}

function open_help_topic(URL) {
  var options = "width=300,height=400,scrollbars=yes,resizable=yes,left=0,top=0,menubar=no,status=no,toolbar=no,location=no,directories=no,history=no";
  
  if (URL) {
    // Open a new window and show the specified page
    window.open(URL,'helpTopic',options);
  }
}

//----------------------------------------cookie methods

function SetCookie (name, value) 
{
	 var argv = SetCookie.arguments;
	 var argc = SetCookie.arguments.length;
	 var expires = (argc > 2) ? argv[2] : null;
	 var path = (argc > 3) ? argv[3] : null;
	 var domain = (argc > 4) ? argv[4] : null;
	 var secure = (argc > 5) ? argv[5] : false;
	 document.cookie = name + "=" + escape (value) +
	 ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	 ((path == null) ? "" : ("; path=" + path)) +
	 ((domain == null) ? "" : ("; domain=" + domain)) +
	 ((secure == true) ? "; secure" : "");
}

function setC(name, value) 
{
   var expdate = new Date ();
   expdate.setTime (expdate.getTime() + (60 * 60 * 1000));
   SetCookie (name, value);
}

// Get Cookie Value function
function getCookieVal(offset) 
{
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape (document.cookie.substring(offset, endstr));
}

// Get Cookie function
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;
}


function getCookie(name) 
{
  	var bites = document.cookie.split("; "); // break cookie into array of bites
    for (i=0; i < bites.length; i++) 
    {
      nextbite = bites[i].split("="); // break into name and value
      if (nextbite[0] == name) // if name matches
        return unescape(nextbite[1]); // return value
    }
    return null; // if no match return null
  }

function registered(lookingFor, groupName)
{
	var reg = false;
	//retrieve the cookie with the groupName
	var winNames = GetCookie(groupName);

	if (winNames != null)
	{
		//convert into array
		var bites = winNames.split(";");
	    for (i=0; i < bites.length; i++) 
	    {
	      nextbite = bites[i].split("="); // break into name and value
	      if (nextbite[0] == lookingFor) // if name matches
	      {
			reg = true;
			break;
	      }
	    }	
    }
	return reg;
}

function valueOf(lookingFor, groupName)
{
	var reg = "";
	//retrieve the cookie with the groupName
	var winNames = GetCookie(groupName);

	if (winNames != null)
	{
		//convert into array
		var bites = winNames.split(";");
	    for (i=0; i < bites.length; i++) 
	    {
	      nextbite = bites[i].split("="); // break into name and value
	      if (nextbite[0] == lookingFor) // if name matches
	      {
			reg = nextbite[1];
			break;
	      }
	    }	
    }
	return reg;
}

function registerWindow(name, groupName)
{
	//retrieve the cookie with the groupName
	var winNames = GetCookie(groupName);
	if (winNames == null)
	{
		winNames = name+"="+name;
	}
	else
	{
		winNames = winNames + ";" + name+"="+name;
	}
	SetCookie(groupName, winNames, null, '/');
}

function registerParentPortal(name, groupName)
{
	//retrieve the cookie with the groupName
	var winNames = GetCookie(groupName);
	if (winNames == null)
	{
		winNames = "parentPortal="+name;
	}
	else
	{
		winNames = winNames + ";" + "parentPortal="+name;
	}
	SetCookie(groupName, winNames, null, '/');
}

function removeWindow(name,groupName)
{
	var winNames = GetCookie(groupName);
	var lookingfor = name + "=" + name;
	var newCookie;
	if(winNames != null)
	{
		var nameArray = winNames.split(";");
		
		for(i=0; i<nameArray.length; i++)
		{
			if(nameArray[i] != lookingfor)  //found name
			{
				if(newCookie == null)
				{
					newCookie = nameArray[i];
				}
				else
				{
					newCookie = newCookie + ";" + nameArray[i];
				}
			}
		}
		SetCookie(groupName, newCookie, null, '/');
	}
}

function getRootOpener(myWindow)
{
	if (myWindow.opener == null)
	{
		return myWindow;
	}
	else
	{
		return getRootOpener(myWindow.opener);
	}
}

// openNewWindow used to open Products and Services Flash piece...
function openNewWindow(URLtoOpen, windowName, windowFeatures) { 
  newWindow=window.open(URLtoOpen, windowName, windowFeatures); 
}
