// *********************************************************
// VIN.js is copyrighted Veterinary Information Network 2004
// *********************************************************

var agt = navigator.userAgent.toUpperCase();
var BrowserVersion = parseInt(navigator.appVersion);
if (agt.indexOf('MSIE 5')) BrowserVersion = 5;
var IsMac = ((agt.indexOf('MAC_') != -1) || (agt.indexOf('MACINTOSH') != -1));
var IsAOL = (agt.indexOf('AOL') != -1);
var IsIE  = (navigator.appName.indexOf('Microsoft') != -1);
var IsNS  = (navigator.appName.indexOf('Netscape') != -1);
var IsNS4 = (IsNS && (BrowserVersion >= 4));
var IsIE4 = (IsIE && (BrowserVersion >= 4));
var IsIE5  = (IsIE && (BrowserVersion >= 5));
var IsDOM = (BrowserVersion >= 5);

var CtrlTxt = (IsMac) ? 'Command' : 'CTRL';
var FavoriteTxt = (IsIE) ? 'Favorite' : 'Bookmark';

var Months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var Days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var CurDate = new Date();
var WeekDay = CurDate.getDay();
var Day = CurDate.getDate();
var Month = CurDate.getMonth();
var Year = CurDate.getYear();
Year = (Year < 1900) ? Year + 1900 : Year;

var VINDocumentID = 0;

if (document.getElementsByTagName("head")) {
    var insertPoint = document.getElementsByTagName("head")[0];
    var prepend = "";
    if (document.domain.toLowerCase().indexOf('localhost')!=-1) 
    {
        if (document.location.href.toLowerCase().indexOf("/cms/")!=-1) prepend='/cms';
        else if (document.location.href.toLowerCase().indexOf("/boards/")!=-1) prepend='/boards';
        else if (document.location.href.toLowerCase().indexOf("/apputil/")!=-1) prepend='/apputil';
    }       
    //if (document.domain=='localhost') prepend = "";
    insertPoint.appendChild(domScriptInsert(prepend + '/global/js/contextmenu.js'));
}

function domScriptInsert(filename) {
    var scriptInsert = document.createElement('script');
    scriptInsert.setAttribute('language', 'javascript');
    scriptInsert.setAttribute('type', 'text/javascript');
    scriptInsert.setAttribute('src', filename);
    return scriptInsert;
}

// Added 7/17/07 PM... trims a string of blank spaces.
function trim(s){
	return s.replace(/^\s*/, "").replace(/\s*$/, "");
}

function isArray(variable) {
    return (variable.constructor == Array);
}    

function GetWindowSize() {
	var windowWidth = 0, windowHeight = 0;
  if(typeof(window.innerWidth) == 'number') {
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  return [windowWidth, windowHeight];
}

function CleanDocTitle(Title) {
  if (window.RegExp) {
    var rExp = /[\\\/\:\*\?\"\|\<\>]/g;
    Title = Title.replace(rExp, " ")
    rExp = /  +/g;
    Title = Title.replace(rExp, " ")
  }
  return (Title);
}

function PrintPage() {
  if (((IsNS4 || IsIE5)) && (window.print)) window.print();
  else alert('You may print this page using '+CtrlTxt+'-P.');
}

function SavePage() {
  if (IsIE4 && !IsMac) document.execCommand('SaveAs', true, CleanDocTitle(document.title));
  else alert('You may save this page by selecting the Save As option from the File menu.');
}

function BackPage() {
  history.back();
}

function BookmarkPage(ForceURL, NameIn) {
	var URL = (ForceURL) ? ForceURL : window.location.href;
	if (URL.toUpperCase().indexOf('/MEMBERS/') != -1 && URL.toUpperCase().indexOf('/MEMBERS/VSPN/') == -1) {
		var DocTitle = (NameIn) ? NameIn : document.title;
		var DocumentID = (VINDocumentID) ? '&DID='+VINDocumentID : '';		
		ShowMyVIN('BookMarks&p=AddLinkFromURL&URL='+escape(URL)+DocumentID+'&NameIn='+escape(CleanDocTitle(DocTitle)));
	} else {
		if (IsAOL) alert('Please click the red heart to favorite this page.');
		else if (IsIE4 && !IsMac) window.external.AddFavorite(URL, CleanDocTitle(document.title));
		else alert('You may '+FavoriteTxt+' this page using '+CtrlTxt+'-D.');
	}
}

function GetCenterPos(BWidth, BHeight) {
  var PosX = (screen.availWidth - 10)/2 - BWidth/2;
  var PosY = (screen.availHeight - 30)/2 - BHeight/2;
  return ('width='+BWidth+',height='+BHeight+',left='+PosX+',top='+PosY);
}

function ShowSizedWindow(URL, Width, Height, WndName) {
  if (!WndName) WndName = '_blank'
  var args = 'width='+Width+',height='+Height+',menubar=no,status=no,location=no,directories=no,toolbar=no';
  window.open(URL, WndName, args); 
}

function OpenNewWindow(URL, Width, Height) {
  var args = 'menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes';
  if (Width && Height) args = 'width='+Width+',height='+Height + ',' + args;
  window.open(URL, '_blank', args);
}

function OpenNewBrowser(URL, Width, Height) {
  var args = '';
  if (Width && Height) args = 'scrollbars=yes,resizable=yes,width='+Width+',height='+Height;
  window.open(URL, '_blank', args);
}

function ShowPicture(ImgPath, ImgWidth, ImgHeight, Branding) {
  var ScrollBar = "no";
  ImgWidth += 20;
  ImgHeight += 50;
  if (Branding==1 || Branding==9) ImgHeight += 10;
  if ((ImgWidth - 20) > screen.availWidth) {ImgWidth = screen.availWidth - 20; ScrollBar = "yes";}
  if ((ImgHeight - 40) > screen.availHeight) {ImgHeight = screen.availHeight - 40; ScrollBar = "yes";}
  var URL = "/Tools/ShowPicture.plx?URL="+ImgPath;
  if (Branding) URL += '&B='+Branding;
  if (!Branding && ImgWidth<335) ImgWidth = 335;
  if (Branding==1 && ImgWidth<410) ImgWidth = 410;
  if (Branding==7 && ImgWidth<385) ImgWidth = 385;
  var args = GetCenterPos(ImgWidth, ImgHeight)+",menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars="+ScrollBar+",resizable="+ScrollBar;
  window.open(URL, "", args); 
}

function OpenViewer(XML, OptionArray) {
  var args = GetCenterPos(445, 545)+',menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=no,resizable=yes';
  var URL = '/Presenter/Shell.htm?viaScript=1&xmlPath='+escape(XML);
  if (window.location.href.toUpperCase().indexOf('/MEMBERS/') != -1) URL = "/Members" + URL;
  if(OptionArray) {
  	if (OptionArray['Branding']) URL += '&branding='+OptionArray['Branding'];
  	if (OptionArray['StartSlide']) URL += '&initSlide='+OptionArray['StartSlide'];
  	if (OptionArray['Subset']) URL += '&subset='+OptionArray['Subset'];
  	if (OptionArray['StartedFrom']) URL += '&startedFrom='+OptionArray['StartedFrom'];
  }
  window.open(URL, 'MMViewer', args);
}

function ShowRemote() {
  ShowMyVIN()
}

function ShowMyVIN(Page) {
  var URLValue;
  URLValue = "/Members/Tools/MyVIN.plx?fnc=ShowMyVIN";
  if (Page) URLValue += '&Page='+Page;
  if (SiteAreaId) 
  {
    var saidtouse = 1;      //vin is efault
    switch (SiteAreaId)
    {
        case 2:
        case 14:
            saidtouse = 2;  //vspn
            break;
        case 3: 
            saidtouse = 3;  //local
            break;            
        case 5: 
            saidtouse = 5;  //sp
            break; 
        case 4: 
        case 13: 
            saidtouse = 4;  //ce
            break;            
    }
    URLValue += '&SAId=' + saidtouse;
  }
  var args = "top=1,width=600,height=450,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes";
  newWin = window.open(URLValue, "MyVIN", args);
}

function ShowFeedback(EmailTo, Subject) {
  var URLValue = "/Members/Help/FeedbackPopup.asp";
  var args = "width=370,height=400,menubar=no,status=no,location=no,directories=no,toolbar=no, scrollbars=yes";
  if (EmailTo || Subject) {
    URLValue += "?";
    if (EmailTo) URLValue += "EmailTo="+EmailTo+"&";
    if (Subject) URLValue += "Subject="+escape(Subject)+"&";
  }
  window.open(URLValue, "_blank", args); 
}

function ShowUnityInstall(Room) {
  var URLValue = "/VINChat/Steps.asp";
  if (Room) URLValue += '?Room='+Room;
  var args = GetCenterPos(450, 300)+",width=450,height=300,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes";
  window.open(URLValue, "_blank", args); 
}

function LaunchUnity(Room) {
  var PosX = screen.availWidth - 810;
  var PosY = 0;
  RoomName = Room.replace('-','_'); //Steve added this, hyphens were breaking the open script.
  window.open('http://www.vin.com/Members/Unity3/Unity.plx?R='+Room, 'VINUnity_'+RoomName, 'left='+PosX+',top='+PosY+',width=800,height=550,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=no,resizable=yes');
}

function LaunchUnityOld(Room) {
  var PosX = screen.availWidth - 810;
  var PosY = 0;
  RoomName = Room.replace('-','_');
  window.open('http://www.vin.com/Members/Unity/Unity.plx?R='+Room, 'VINUnity_'+RoomName, 'left='+PosX+',top='+PosY+',width=800,height=550,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=no,resizable=yes');
}

function SwapImg(Tab, ImgNo) {
  if (Tab == '') return;
  if (Tab == CurrentTab && ImgNo == 1) ImgNo = 3;
  var TabSrc = eval('document.Tab'+Tab+'.src');
  var NewSrc = TabSrc.substring(0, TabSrc.indexOf('.gif') - 1)+ImgNo+'.gif';
  eval('document.Tab'+Tab+'.src = NewSrc');
}

function AsynchRequest() {
	var _AjaxState = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
	
	var _ReqObj = null;
	var _inProgress = false;
	var _isComplete = false;
	var _oThis = this;
	var _isSynchronus = false;
	
	this.METHOD = 'GET';
	
	var _CreateObj = function() {
		if (window.XMLHttpRequest) {
			_ReqObj = new XMLHttpRequest();
		} else if (window.ActiveXObject) {		
			try {
				_ReqObj = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					_ReqObj = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {}
			}
		}
		if (!_ReqObj) _oThis.OnError('Unable to create request.');
	}

	var _CallBack = function() {
		var AjaxState = _AjaxState[_ReqObj.readyState];
		if (AjaxState == 'Complete') {
			try
			{
				var HTTPStatus = _ReqObj.status;
				if (HTTPStatus == 200)
				{
					_inProgress = false;
					_isComplete = true;
					_oThis.OnComplete(_oThis);
				}
				else
				{
					if(HTTPStatus == "500")
					{
						_oThis.OnError('Status: 500 - Your request has timed out. Please contact the system admin.');
					}
					else
						_oThis.OnError('Status: '+HTTPStatus);
				}
			}
			catch(e)
			{
				//alert(e);
			}
		}
	}
	
	this.SetSynchFlag = function(boolFlag) {
	    _isSynchronus = boolFlag;
	}
	
	this.GetSynchFlag = function() {
	    return _isSynchronus;
	}

	this.Send = function(URL, PostData) {
		_CreateObj();
		if (_inProgress && !_isComplete) {
			// Error
			alert('Already in progress');
		} else if (_ReqObj) {
			_inProgress = true;
			_isComplete = false;
			if(_isSynchronus)
			    _ReqObj.open((PostData) ? 'POST' : 'GET', URL, false); 
			else
			    _ReqObj.open((PostData) ? 'POST' : 'GET', URL, true);
			
			_ReqObj.onreadystatechange = _CallBack;
			if (PostData) _ReqObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
			try{					
				_ReqObj.send(PostData);
			}
			catch(e){ } //alert(e); 
		}
	}
	
	this.GetResponseText = function() {
		if(_ReqObj.responseText.indexOf('AuthDB.dll') == -1){
			return _ReqObj.responseText;
		}		
		else
		{
			var win = window.open("SecurityRedirect.aspx");						
			return "AuthDB.dll";
		}
	}

	this.OnComplete = function(AsynchRequestObject) {
		//Needs to override
	}

	this.OnError = function(ErrorStr) {
		//Needs to override
		alert(ErrorStr);
	}
}

function moveCursorAtEnd(TB) 
{   if (TB.createTextRange) 
    {   var FieldRange = TB.createTextRange();
        FieldRange.moveStart('character', TB.value.length);
        FieldRange.collapse();
        FieldRange.select();
    }
}

function GetImageIndex(el)
{
    var retval = -1;
    for (var i=0; i<document.images.length; i++)
    {
        var img = document.images[i];
        if (img.src.toLowerCase().indexOf("/image/handler.ashx") != -1)
        {
            retval++;
            if ((img.src == el.src) && (img.width == el.width)) break;
        }
    }
    return retval;
}

function GetImageCount()
{
    var retval = 0;
    for (var i=0; i<document.images.length; i++)
        if (document.images[i].src.toLowerCase().indexOf("/image/handler.ashx") != -1)
            retval++;
    return retval;
}

{
    var h = document.getElementsByTagName('head')[0], s1 = document.createElement('script'), s2 = document.createElement('script'), pre='';
    s1.setAttribute('type', 'text/javascript'); s2.setAttribute('type', 'text/javascript');
    if (document.domain.toLowerCase().indexOf('localhost')!=-1) 
    {
        if (document.location.href.toLowerCase().indexOf("/cms/")!=-1) pre='/cms';
        else if (document.location.href.toLowerCase().indexOf("/boards/")!=-1) pre='/boards';
        else if (document.location.href.toLowerCase().indexOf("/apputil/")!=-1) pre='/apputil';
    }        
    s1.setAttribute('src', pre+'/Global/JS/yuiloader/yuiloader-min.js');
    h.appendChild(s1);
}

function getTextSelection()
{
    var sel;
    if(window.getSelection) {
    	sel = window.getSelection();
    }
    else if(document.getSelection) {
    	sel = document.getSelection();
    }
    else if(document.selection) {
    	//sel = document.selection;
    	sel = document.selection.createRange().text;
    }
    return sel;
}

function DL(f) //download
{   
    var usewindowopen = false; usewindowopen = (f.indexOf(".pdf")!=-1);
    if (usewindowopen) window.open(f,"_blank");
    else location.href=f;
}

if (0) {
  if (document.getElementsByTagName("head")) {
    var h = document.getElementsByTagName("head")[0];
    var lnk = document.createElement("link");
    lnk.setAttribute("rel", "stylesheet");
    lnk.setAttribute("media", "print");
    lnk.setAttribute("type", "text/css");
    if (screen.width<800) lnk.setAttribute("href", "/Global/css/print800.css");
    else if (screen.width<1024) lnk.setAttribute("href", "/Global/css/print1024.css");
    else if (screen.width<1152) lnk.setAttribute("href", "/Global/css/print1152.css");
    else /* if (screen.width<1280) */ lnk.setAttribute("href", "/Global/css/print1280.css");
    h.appendChild(lnk);
  }
}

function ShowPolicies() {
  var URLValue = "/Members/CE/Templates/Policies.htm";
  var args = "width=440,height=300,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes";
  window.open(URLValue, "_blank", args); 
}

var __STANDARD_POPUP_FEATURES__ = GetCenterPos(800,650)+",menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes";

function DoPermissionAdmin(pid)
{   // VINMainDomain is dropped automatically by VIN.Base.VINBasePage
    var url = 'https://' + VINMainDomain + '/admin/permission/asset.aspx?ItemID=' + pid;
    var args = GetCenterPos(800,590)+',menubar=yes,status=yes,toolbar=no,resizable=yes';
    window.open(url, '_blank', args);
}

function DoIndexing(documentid)
{
    var windowname = "window" + documentid;
    var url = "http://tools.vin.com/IndexingTool/Default.aspx?documentid=" + documentid;
    var x = window.open(url, windowname, __STANDARD_POPUP_FEATURES__);
    x.focus();
	//window.status="Indexing tool is not available";
}

function EditDocument(url)
{
    var x = window.open(url, '_blank', __STANDARD_POPUP_FEATURES__);
    x.focus();    
}