﻿
/// S H A R E D   C L A S S   -   B E   C A R E F U L L   I F   Y O U   C H A N G E   A N Y T H I N G ///
// Constants
var __gateWayApplicationPath = '';
var __weblinkApplicationPath = '/SGS/Weblink';
var __webstockApplicationPath = '/WebStock';
var __webcaseApplicationPath = '/WebCase';
var __lmsApplicationPath = '/LMS';

var ajaxObj = new Array();
var keeyAjaxObj = new Array();

var urls = new Array();
urls[0] = __lmsApplicationPath + '/Services/SignOff.aspx'; // School (LMS)
urls[1] = __webcaseApplicationPath + '/Services/SignOff.aspx'; // Job Management (WebCase)
urls[2] = __webstockApplicationPath + '/Services/SignOff.aspx'; // WebStock
urls[3] = __weblinkApplicationPath + '/Services/SignOff.aspx'; // Weblink
urls[4] = __gateWayApplicationPath + '/Services/SignOff.aspx'; // Gateway

var keepAliveUrls = new Array();
//keepAliveUrls[0] = __lmsApplicationPath + '/KeepAlive.aspx'; // School (LMS)
//keepAliveUrls[1] = __webcaseApplicationPath + '/KeepAlive.aspx'; // Job Management (WebCase)
keepAliveUrls[0] = __webstockApplicationPath + '/KeepAlive.aspx'; // WebStock
keepAliveUrls[1] = __weblinkApplicationPath + '/KeepAlive.aspx'; // Weblink
keepAliveUrls[2] = __gateWayApplicationPath + '/KeepAlive.aspx'; // Gateway



var cntri = 0;
var loadingImage = '/SGS/Weblink/images/ajax-loader.gif';


function CheckMaxLengthOnTextBox(e, obj, maxLength){
 if((obj.value.length >= maxLength) && (e.keyCode != 8) && (e.keyCode != 46)){
  return false;
 }
}

function getAjaxObject(){
	if (window.XMLHttpRequest) {
	// If IE7, Mozilla, Safari, and so on: Use native object
		xmlHttp = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject) {
		// ...otherwise, use the ActiveX control for IE5.x and IE6
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function SignOff(idle) {
  
  try {
    var signoffHTML = '' // '<iframe style="width:0px; height:0px; position: absolute; left: -1000px; top: -1000" src="' + urls[0] + '"></iframe><iframe style="width:0px; height:0px; position: absolute; left: -1000px; top: -1000" src="' + urls[1] + '"></iframe><iframe style="width:0px; height:0px; position: absolute; left: -1000px; top: -1000" src="' + urls[2] + '"></iframe><iframe style="width:0px; height:0px; position: absolute; left: -1000px; top: -1000" src="' + urls[3] + '"></iframe><iframe style="width:0px; height:0px; position: absolute; left: -1000px; top: -1000" src="' + urls[3] + '"></iframe>';
    jQuery.facebox('<br /><img src="/sgs/js/facebox/loading.gif" title=""> ' + SignOffInnerHTML + signoffHTML);
  } catch(ex) {
  
  }

  if (cntri < urls.length) {
     return signOffSite(idle);
  }
}

function signOffSite(idle) {

    var idleSignOff = '';
    if (idle != null) {
      if (idle) {
        idleSignOff = '&IdleSignOff=true';
      }
    }

  ajaxObj[cntri] = getAjaxObject();
  if (cntri == urls.length -1) {
    ajaxObj[cntri].onreadystatechange = function(){ 
      if (ajaxObj[urls.length -1].readyState == 4){
        jQuery(document).trigger('close.facebox');
        if (parent.window) {
          parent.window.location.href = urls[urls.length - 1] + '?signoff=true' + idleSignOff;
        } else {
          window.location.href = urls[urls.length - 1] + '?signoff=true' + idleSignOff; 
        }
      }  
    };
  } else {
    ajaxObj[cntri].onreadystatechange = function(){
      if (ajaxObj[cntri].readyState == 4){
        cntri++;
        setTimeout('signOffSite()', 10);
      }    
    };
  }

  try {
      ajaxObj[cntri].open('GET', urls[cntri]);
      ajaxObj[cntri].send(null);
  } catch (ex) {
  }  
}

function changeLanguage(Mode) {
  var xmlHttp = getAjaxObject();
  try {
    var url = '';
    switch(RootApplicationPath.toUpperCase()) 
    {
    case '/SGS':
      url = '/sgs/weblink/Services/QuickMenuService.aspx?Language.Mode='+Mode;
      break;
    case '/WEBCASE':
      url = '/webcase/Services/QuickMenuService.aspx?Language.Mode='+Mode;
      break;
    case '/WEBSTOCK':
      url = '/webstock/Services/QuickMenuService.aspx?Language.Mode='+Mode;
      break;
    case '/LMS':
      url = '/LMS/Services/QuickMenuService.aspx?Language.Mode='+Mode;
      break;
    default:
      url = '/Services/QuickMenuService.aspx?Language.Mode='+Mode;
      break;
    }
  
    xmlHttp.open('GET', url);
    xmlHttp.onreadystatechange = function(){ 
      if (xmlHttp.readyState == 4){
        if (window.parent != null){
           refreshParentLocation();
        } else {
            refreshLocation();
        }  
        
      }    
    }
    xmlHttp.send();
  
  } catch (e) {
    jQuery.facebox(e.message);
  }
}

function refreshLocation() {
  if (window.location.href.indexOf("?") == -1) {
    window.location.href = window.location.href + '?' + guid();
  } else {
    window.location.href = window.location.href + '&' + guid();
  }  
}

function refreshParentLocation() {
  if (window.parent.location.href.indexOf("?") == -1) {
    window.parent.location.href = window.parent.location.href + '?' + guid();
  } else {
    window.parent.location.href = window.parent.location.href + '&' + guid();
  }  
}
  
function S4() {
  return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
  return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function AttachEvent(obj,evt,fnc,useCapture){
 if (!useCapture) useCapture=false;
 if (obj.addEventListener){
   obj.addEventListener(evt,fnc,useCapture);
   return true;
 } else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
 else{
   MyAttachEvent(obj,evt,fnc);
   obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
 }
} // AttachEvent


function getElementsByClass(searchClass, node, tag) {
  var classElements = new Array();
  if ( node == null )
	  node = document;
  if ( tag == null )
	  tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++) {
	  if ( pattern.test(els[i].className) ) {
		  classElements[j] = els[i];
		  j++;
	  }
  }
  return classElements;
}

function removeElementsByClass(searchClass, node, tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
        classElements[j] = els[i];
			  j++;
    }
  }
  
  var length = classElements.length
  for (i = length-1; i > -1; i--) {
    if (classElements[i].parentNode) {
      classElements[i].parentNode.removeChild(classElements[i]);
    }  
  }
  
  return classElements;
}


function quickSearch(value) {
  try {
    if (parent.window != null) {
      parent.window.location.href = '/SGS/Weblink/Search/QuickSearch.aspx?Q=' + escape(value);
    } else {  
      window.location.href = '/SGS/Weblink/Search/QuickSearch.aspx?Q=' + escape(value);
    }
  } catch(e) {  
    jQuery.facebox(e.message);
  }
}

var rmcnt = 0;

function fnFixArticleNo(obj){
  try
  { 

    if ((event.shiftKey != 1) && (event.ctrlKey != 1)) {
  		var keyCode = event.keyCode;
		  if (keyCode > 20){ 
	  	  var pos = getCursorPos(obj);
		    rmcnt = 0;

		    if ((keyCode < 37) || (keyCode > 40)){ 
			    if (obj != null) {
			      obj.value = removeChars(obj.value, ' ');
			      obj.value = removeChars(obj.value, '-');
			      obj.value = removeChars(obj.value, '.');
			    }
			  }  
  		  setCursorPos(obj, (pos-rmcnt));
  		}  
		
		} else {
      obj.value = removeChars(obj.value, ' ');
      obj.value = removeChars(obj.value, '-');
      obj.value = removeChars(obj.value, '.');
		}
	
	}catch(e){
	
	}
}
	
function removeElement(obj) {
  obj.parentNode.removeChild(obj);
}
	
function removeChars(entry, rmchr) {
	temp = '' + entry; // temporary holder

	while (temp.indexOf(rmchr)>-1) {
		rmcnt = rmcnt+1;
		pos=temp.indexOf(rmchr);
		temp = "" + (temp.substring(0, pos) + temp.substring((pos + rmchr.length), temp.length));
	}
	return temp;
}
	
function getCursorPos(el){ 
	var sel, rng, r2, i=-1; 
	
	if(document.selection && el.createTextRange) { 
		sel=document.selection; 

		if(sel){ 
			r2=sel.createRange(); 
			rng=el.createTextRange(); 
			rng.setEndPoint("EndToStart", r2); 
			i=rng.text.length; 
		} 

	} 

	return i; 
} 

function setCursorPos(field,pos) { 
	if (field.createTextRange) { 
		var r = field.createTextRange(); 

		r.moveStart('character', pos); 
		r.collapse(); 
		r.select(); 
	} 

} 	

function replace(string,text,by) {
// Replaces text with by in string
		var strLength = string.length, txtLength = text.length;
		if ((strLength == 0) || (txtLength == 0)) return string;

		var i = string.indexOf(text);
		if ((!i) && (text != string.substring(0,txtLength))) return string;
		if (i == -1) return string;

		var newstr = string.substring(0,i) + by;

		if (i+txtLength < strLength)
				newstr += replace(string.substring(i+txtLength,strLength),text,by);

		return newstr;
}		

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

this.imagePreview = function() {
  /* CONFIG */

  xOffset = 30;
  yOffset = 30;

  // these 2 variable determine popup's distance from the cursor
  // you might want to adjust to get the right result

  /* END CONFIG */
  $("a.preview").hover(function(e) {
    this.t = this.title;
    this.title = "";
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='preview' style='z-index: 999;'><img src='" + this.rel + "' alt='' />" + c + "</p>");
    $("#preview")
			.css("top", (e.pageY - xOffset) + "px")//- xOffset
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
  },
	function() {
	  this.title = this.t;
	  $("#preview").remove();
	});
  $("a.preview").mousemove(function(e) {
    $("#preview")
			.css("top", (e.pageY - xOffset) + "px") // - xOffset
			.css("left", (e.pageX + yOffset) + "px")
			.css("z-index", 1006);
  });
};


// starting the script on page load
$(document).ready(function() {
    try {
        imagePreview();
    } catch (ex) { }
});

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
  }

  var keepAliveCount = 0;
  
  function keepAlive() {
    keepAliveCount = 0;
		window.setTimeout('keepAliveLoop()', 10);
  }
  
  
//  function keepAliveLoop() {

//    keeyAjaxObj[keepAliveCount] = getAjaxObject();

//    keeyAjaxObj[keepAliveCount].onreadystatechange = function() {
//      if (keeyAjaxObj[keepAliveCount].readyState == 4) {
//	      if (keepAliveCount < keepAliveUrls.length - 1) {
//          keepAliveCount++;
//					window.setTimeout('keepAliveLoop()', 10);
//        }
//      }
//    };

//    try {
//      keeyAjaxObj[keepAliveCount].open('GET', keepAliveUrls[keepAliveCount]);
//      keeyAjaxObj[keepAliveCount].send(null);
//    } catch (ex) {
//    	
//    }
//		
//		
//		
//    if (keepAliveCount == (keepAliveUrls.length - 1)) {
//      window.setTimeout('keepAlive()', 180000); // 240000 -> 3 minits.
//    }

//  }

  //setTimeout('keepAlive()', 10);
