﻿/* Contact Us and Help popup script */
var gmyWin=null;
var pW = ',left ='+ (screen.availWidth - 710); // sets popup to right of screen less integer
function myOpenWindow(winURL, winName, winFeatures, winObj) {
	//alert(winURL);
	var theWin = window.open(winURL, winName, winFeatures);
	//for writing title text specific to each document
	//document.form1.helpText.value = winText; 
	// first check to see if the window already exists
	if (winObj != null) {
		// the window has already been created, but did the user close it?
		// if so, then reopen it. Otherwise make it the active window.
		if (!winObj.closed) {
			winObj.focus();
			return winObj;
		}
	}
	// otherwise fall through to the code below to re-open the window  
	// if we get here, then the window hasn't been created yet, or it  
	// was closed by the user.  

	//window.new.getElementById('helpText').innerHTML = 'sample static help text';
	return theWin; 
}

var win;
// constants for window names
var WIN_DOC_ACTION = 'winDocAction';
var WIN_LNB = 'rosetta';
var WIN_INFO = 'winInfo';
var WIN_ATTACHMENT = 'winAttachment';


function openWin( url )
{
    openWinImpl(url, 800, 600, "resizable=yes,scrollbars=yes");
}

function more( ctrl )
{
    openWin( ctrl.href );
}
function quickSearch( baseUrl, ctrl )
{
    // Assumptions
    // If the baseUrl includes a sr= term it MUST be the last query parameter
    
    var terms = ctrl.value;
    
    if( terms == null || terms.length == 0 )
    {
        alert( 'Please specify a search term' );
        return;
    }
    
    url = baseUrl.replace( /%7b0%7d/, escape(terms) );
    
    openWin(url);
}

function signOut(url)
{
    closeChildWindow(); 
    window.open(url, "_self");
}

function closeChildWindow()
{
    if( win )
    {
        if( !win.closed )
        {
            win.close();
        }
    }
    else
    {
        // if win is null, the child window may still be open if the home page 
        // has done a post back since the window was opened - to be safe use the 
        // following hack
        
        win = openWinImpl("Utils/PleaseWait.htm", 200, 200, 
            "location=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no" );
    }
}

function openWinImpl(url, sW, sH, features)
{
	var w = sW
	var h = sH

	if(screen.width)
	{
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	} 
	else 
	{
		winl = 0;
		wint = 0;
	}
	
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;

	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;

	win = window.open(url, "LNB", settings);
	win.focus();
}

function getObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId);
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId);
  } else {
    return false;
  }
}

function getStyleObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId).style;
  } else {
    return false;
  }
}

function changeDiv(the_div,the_change)
{
  var the_style = getStyleObject(the_div);
  if (the_style != false)
  {
    the_style.display = the_change;
  }
}

function GetHttpRequestObject()
{
    var xmlhttp=false;
    try 
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	    try {
		    xmlhttp = new XMLHttpRequest();
	    } catch (e) {
		    xmlhttp=false;
	    }
    }
    if (!xmlhttp && window.createRequest) {
	    try {
		    xmlhttp = window.createRequest();
	    } catch (e) {
		    xmlhttp=false;
	    }
    }
    return xmlhttp;
}

// Common js functions
//----------------------------------
// Window
function openPlainWindow(winURL, winName, winFeatures)
{
	var win = window.open(winURL, winName, 'toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=1,' + winFeatures)
	if (win)
	{
        win.focus();
    }
    return win;
}

function openWindow(winURL, winName, winFeatures, noFocus)
{
	var win = window.open(winURL, winName, winFeatures);
	if (win && !noFocus)
	{
        win.focus();
    }
    return win;
}

function closeWindow(winRef, winName)
{
    // close by ref then by win name since closing by win name (which opens a window to get the reference) will be the slowest method.
    if (winRef)
    {
        closeWindowByRef(winRef);
    }
    else
    {
        closeWindowByName(winName);
    }
}

function closeWindowByName(winName)
{
    if (winName)
    {
        winRef = openWindow('', winName, createWindowFeatures(1, 1, 0, 0, 0, 0, 1, 0))
        closeWindowByRef(winRef);
    }
}

function closeWindowByRef(winRef)
{
    if (winRef && !winRef.closed)
    {
        winRef.close();
    }
}

function createWindowFeatures(width, height, pos, menubar, toolbar, status, resizable, scrollbars) 
{
	var xpos = 0;
	var ypos = 0;
	var xfudge = 10;
	var yfudge = 40;
	xpos = (screen.width - width - xfudge) / 2;
	ypos = (screen.height - height - yfudge) / 2;
	//xpos = screen.availWidth - 710
	//ypos = pos;
	var args = '';

	if (width!=0 && height!=0)
	{
	    args = 	"width="      + width +
			    ",height="    + height +
			    ",screenx="   + xpos +
			    ",left="      + xpos +
			    ",screeny="   + ypos +
			    ",top="       + ypos
	}
			
    args += ",menubar="   + menubar +
			",toolbar="   + toolbar +
			",status="    + status +
			",resizable=" + resizable +
			",scrollbars=" + scrollbars;

	return args;
}

//----------------------------------
// Browser
function isIE()
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//----------------------------------
// Email
function isEmailAddress(str) {
  // validates email address  

  // check if regular expressions are supported
  if (!RegExpSupported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

  // validate
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

//----------------------------------
// Navigation
function redirect(url)
{
    // determine if in a popup window, if so load in the opener and close
    if (self.opener)
    {
        self.opener.location = url;
        self.close();
    }
    else
    {
        self.location = url;
    }
}

//----------------------------------
// Misc
function copyToClipboard(elementId)
{
    var range = document.body.createTextRange();
    range.moveToElementText(document.getElementById(elementId));
    range.execCommand("copy");
    
    return false;
}

function RegExpSupported() 
{
  // determines if regular expressions are supported
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) return 1;
  }
}

//----------------------------------
// Open document actions, print/email/copyurl
function openLNB(url)
{
    window.open(url, WIN_LNB, '');
}
function openDocumentUrlAction(url)
{
    openWindow(url, WIN_DOC_ACTION, createWindowFeatures(600, 200, 0, 0, 0, 0, 0, 0));
    return false;
}
function openDocumentEmailAction(url)
{
    openWindow(url, WIN_DOC_ACTION, createWindowFeatures(700, 500, 0, 0, 0, 0, 0, 0));
    return false;
}
function openDocumentPrint(url)
{
    openWindow(url, WIN_DOC_ACTION, createWindowFeatures(700, 500, 0, 1, 1, 0, 1, 1));
    return false;
}

//----------------------------------
// Open rosetta citation link widow
function getDocRosetta(type, id)
{
    window.open('../DocumentView/DocumentLink.aspx?type=' + type + '&id=' + id, WIN_LNB, '');
}

function getLinkRosetta(url)
{
    window.open(url, WIN_LNB, '');
}

//----------------------------------
// Random qs arg
function rand()
{
    return '&rnd=' + Math.floor(Math.random()*89999+10000);
}

//----------------------------------
// Open Help landing page
function openHelpWindow(url, noFocus)
{
    openWindow(url, 'help', createWindowFeatures(705, 550, 0, 1, 1, 0, 1, 1), true);
    return false;
}

//----------------------------------
// Set cookie (session or permanent)
function setCookie(name, value, expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = name+"="+escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}