// global variables
var _browserType = navigator.appName;
var _isIEBrowser = _browserType == 'Microsoft Internet Explorer';
var _vUID = '';
var _ip = false;
var _ips = false;

// global constants
var _cXmlNodeTypeElement = 1; //xml node type is Element

// nuconomy safe api
window.NUCONOMY = window.NUCONOMY || {};
NUCONOMY.Logger = function() {};
NUCONOMY.Logger.LogActivity = function(a,b,c,d,e){};
NUCONOMY.Logger.SetContentMetaData= function(a,b,c,d){};

function addBookmark() {
  if (window.sidebar) // Mozilla Firefox Bookmark	
    window.sidebar.addPanel(_siteName, _siteLink, "");
  else if (window.external) // IE Favorite
    window.external.AddFavorite(_siteLink, _siteName);
}

function doRedirect() {
  window.location.href = _siteLink;
}

function openWindow(url, windowData, width, height, top, left, resizable, scrollbars) {
  var settings = buildWindowOpenArguments(width, height, top, left, resizable, scrollbars);
  window.open(url, windowData, settings);
}

function buildWindowOpenArguments(width, height, top, left, resizable, scrollbars) {
  var resizable = (resizable ? 'yes' : 'no');
  var scrollbars = (scrollbars ? 'yes' : 'no');
  var settings = 'width=' + width + ',height=' + height + ',resizable=' + resizable + ',scrollbars=' + scrollbars;
  if (top)
    settings += ',top=' + top;
  if (left)
    settings += ',left=' + left;
  return settings
}

function openPopupWindow(url, windowData, width, height, top, left, resizable, scrollbars) {
  url += (url.indexOf('?') >= 0 ? '&' : '?') + 'popupMode=true';
  openWindow(url, windowData, width, height, top, left, resizable, scrollbars);
}

function showHelpPage(url) {
  openPopupWindow(url, null, 520, 300, 100, 100, 'yes', 'yes');
}

/*function getApplicationName() {
  var pagePath = window.location.pathname;
  var pos = pagePath.indexOf('/', 1);
  if (pos > 0)
    return pagePath.substring(0, pos + 1);
  else
    return '/';
}*/

function getObjectPath(objectName) {
  return getApplicationName() + objectName;
}

/*function getPageName() {
  var pagePath = window.location.pathname;
  var pos = pagePath.indexOf('/', 1);
  return pagePath.substring(pos + 1, pagePath.length).toLowerCase();
}*/

function getPageMainForm(formName) {
  formName = (formName ? formName : 'form1');
  return document.forms[formName];
}

function submitPage(sNewFormAction, formName) {
  var frm = getPageMainForm(formName);
  if (frm) {
    frm.action = sNewFormAction;
    if (frm.__VIEWSTATE) frm.__VIEWSTATE.name = 'NOVIEWSTATE';
    frm.submit();
  }
}

function submitPageWithDelay(sNewFormAction, delay) {
  setTimeout('submitPage(\'' + sNewFormAction + '\')', delay);
}

function redirectPage(url) {
  document.location.href = url;
}

function redirectPageWithDelay(url, delay) {
  setTimeout('redirectPage(\'' + url + '\')', delay);
}

function browserDetect(text) {
   str = (navigator.userAgent.toLowerCase()).indexOf(text) + 1;
   data = text;
   return str;
}

function getInternetExplorerVersion() {
	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;
}

function getWindowWidth() {
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined') {
    return window.innerWidth;
  }
  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
    return document.documentElement.clientWidth;
  }
  // older versions of IE 
  else {
    return document.getElementsByTagName('body')[0].clientWidth;
  }
}

function getWindowHeight() {
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerHeight != 'undefined') {
    return window.innerHeight;
  }
  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0) {
    return document.documentElement.clientHeight;
  }
  // older versions of IE 
  else {
    return document.getElementsByTagName('body')[0].clientHeight;
  }
}

function doNothing() {
  // not supposed to do anything
}

function logNuconomyActivity(contentOwnerID, contentType, contentID, contentTitle, activityID, activityValue, categories, location, brand, tags) {
  if (_ip) {
    if (activityValue == '0') activityValue = '';
    NUCONOMY.ContentType = contentType;
    NUCONOMY.ContentTitle = contentTitle;
    NUCONOMY.Tags = (tags ? tags : '');
    NUCONOMY.ActivityParameter1 = (categories ? categories : '');
    NUCONOMY.ActivityParameter2 = (location ? location : '');
    NUCONOMY.ActivityParameter3 = getCookie("Aff", "Sys");
    NUCONOMY.ActivityParameter4 = (brand ? brand : '');
    NUCONOMY.Logger.LogActivity(_vUID, contentOwnerID, contentID, activityID, activityValue);
  }
  //else alert("visitorUserID=" + _vUID + "\ncontentOwnerID=" + contentOwnerID + "\ncontentType=" + contentType + "\ncontentID=" + contentID + "\ncontentTitle=" + contentTitle + "\nactivityID=" + activityID + "\nactivityValue=" + activityValue + "\ncategories=" + categories+ "\nlocation=" + location + "\nbrand=" + brand + "\ntags=" + tags);
}

function checkSearchText(e) {
  var ev =(window.event ? window.event : e); //IE:Moz
  var keyCode = (ev.keyCode ? ev.keyCode : ev.charCode);
  if (keyCode == 47 || keyCode == 92) { // '/' '\'
    if (ev.preventDefault)
      ev.preventDefault();
    else
      ev.returnValue = false;
    return false;
  }
}

function getCookie(c_name, c_item_name) {
  var rslt = '';
  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;
      rslt = unescape(document.cookie.substring(c_start,c_end));
    }
  }
  if (rslt != '' && c_item_name) {
    i_start = rslt.indexOf(c_item_name + "=");
    if (i_start != -1) {
      i_start = i_start + c_item_name.length+1; 
      i_end = rslt.indexOf("&",i_start);
      if (i_end == -1)
        i_end = rslt.length;
      rslt = unescape(rslt.substring(i_start,i_end));
      return rslt;
    }
    else
      return '';
  }
  return rslt;
}

function set_onFocus() {
	var links = document.getElementsByTagName('A');
	for(i=0;i<links.length; i++)
		links[i].setAttribute("onfocus", "blur()");
}

function showBanner(bannerID, bannerSrc, bannerWidth, bannerHeight) {
  var banner = document.getElementById(bannerID); 
  var windowWidth = getWindowWidth();
  if (banner && (bannerID != 'megaBanner' || (bannerID == 'megaBanner' && windowWidth >= 1000)))
    banner.innerHTML = '<IFRAME style="BACKGROUND-COLOR: transparent" marginWidth="0" marginHeight="0" src="' + bannerSrc + '" height="' + bannerHeight + '" width="' + bannerWidth + '" scrolling="no" frameBorder="no" allowTransparency></IFRAME>';
  else if (banner && bannerID == 'megaBanner' && windowWidth < 1000)
    banner.style.display = 'none';
}

function showFlashBanner(bannerID, bannerSrc, bannerWidth, bannerHeight, wmode) {
  var banner = document.getElementById(bannerID);
  if (banner) banner.innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" WIDTH="' + bannerWidth + '" HEIGHT="' + bannerHeight + '"><PARAM NAME="movie" VALUE="' + bannerSrc + '"><PARAM NAME="quality" VALUE="high"><PARAM NAME="allowScriptAccess" VALUE="always">' + (wmode ? '<param name="wmode" value="' + wmode + '" />' : '') + '<EMBED src="' + bannerSrc + '" quality="high" WIDTH="' + bannerWidth + '" HEIGHT="' + bannerHeight + '" ' + (wmode ? 'wmode="' + wmode + '"' : '') + ' allowScriptAccess="always" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED></OBJECT>';
}

function handleBannerAction(bannerID, actionName) {
  switch (actionName) {
    case 'close': hideBanner(bannerID); break;
    default:
      if (typeof(window['handleBannerActionEx']) == 'function')
        handleBannerActionEx(bannerID, actionName);
  }
}

function split() {
  var birthdayBanner = document.getElementById('birthdayBanner');
	if (birthdayBanner) birthdayBanner.style.display = 'none';
}

function hideBanner(bannerID) {
  var banner = document.getElementById(bannerID);
  if (banner) banner.style.display = 'none';
}

function show_registerTooltip(e, linx, objId, objHeight, title, text) {
  var ev =(window.event)?window.event:e; //IE:Moz
	//leftx = (findPosX(linx))-322;
	//topx = (findPosY(linx))+14;

	leftx = ev.clientX + document.body.scrollLeft - 335;
	topx = ev.clientY + document.body.scrollTop + 16;
	
	if (leftx < 15) {
	  leftx += 320;
	  objId = objId + '_ltr';
	}

	var frameHeight = window.screen.availHeight;
	 if (frameHeight < topx + objHeight + window.screenTop) {
	  topx -= (objHeight + 10)
	  objId = objId + '_up';
	}
	
	var toolObj = document.getElementById(objId)
	if (toolObj) {
	  if (title) toolObj.getElementsByTagName('H4')[0].innerHTML = title;
	  if (text) toolObj.getElementsByTagName('DIV')[0].innerHTML = text;
    toolObj.style.left = leftx+'px';
    toolObj.style.top = topx+'px';
	  document.getElementById(objId).style.visibility = 'visible';
	  if(getInternetExplorerVersion()==6) {
		  selectArr = document.getElementsByTagName('select');
		  for(i=0; i<selectArr.length; i++)
			  selectArr[i].style.visibility = 'hidden';
	  }
	}
}

function hide_registerTooltip(objId) {
	document.getElementById(objId).style.visibility = 'hidden';
	document.getElementById(objId + '_ltr').style.visibility = 'hidden';
	document.getElementById(objId + '_up').style.visibility = 'hidden';
	document.getElementById(objId + '_ltr_up').style.visibility = 'hidden';
	selectArr = document.getElementsByTagName('select');
	for(i=0; i<selectArr.length; i++)
		selectArr[i].style.visibility = 'visible';
}

function fbs_click(url, title) {
  window.open('http://www.facebook.com/share.php?u=' + url + '&t=' + title, "sharer", "toolbar=0,status=0,width=626,height=436");
	return false;
}

function doLoad() {
  set_onFocus();
  if (typeof(window['showBanners']) == 'function')
    showBanners();
}
window.onload = doLoad;