// JavaScript

function UnunderlineDescenders (letter, string, re) {
  if (BrowserDetect.browser != 'Explorer') return false;  // Css doesn't work in Firefox & Safari in quirks mode
  var Anchors = document.links;
  if (Anchors!=null) 
  {
    for (i=0; i<Anchors.length; i++) 
    {
      var anchorContent = Anchors.item(i).innerHTML;
      if (anchorContent.indexOf(letter) != -1) Anchors.item(i).innerHTML = anchorContent.replace(re, '$1' + string);
    }
  }
  return true;
}

function AnchorsDescendersFix () {
  var re_j = /(>?[^<>]*?)j(?![^<>]*?>)/g;
  var re_g = /(>?[^<>]*?)g(?![^<>]*?>)/g;
  UnunderlineDescenders('j', '\<u\>j\<\/u\>', re_j);
  UnunderlineDescenders('g', '\<u\>g\<\/u\>', re_g);
  var re_Q = /(>?[^<>]*?)Q(?![^<>]*?>)/g;
  var re_at = /(>?[^<>]*?)@(?![^<>]*?>)/g;
  UnunderlineDescenders('@', '\<u\>@\<\/u\>', re_at);
}


function FixRecurringLinks () {
  var index = {};
  var Anchors = document.links;
  for (var i=0; i<Anchors.length-4; i++) {
    var link = Anchors[i];
    if (index[link.href]==true) 
      link.className += link.className ? ' RecurringLink' : 'RecurringLink';
    index[link.href] = true;
  }
}


// from www.design.ru

function InputPlaceholder (input, value, cssFilled, cssEmpty, isEditable)
{
  var thisCopy = this;
  
  this.Input = input;
  this.Value = value;
  this.SaveOriginal = (input.value == value);
  this.CssBase = input.className;
  this.CssFilled = cssFilled;
  this.CssEmpty = cssEmpty;
  this.isEditable = isEditable;
  this.isFirstBlurPassed = false;

  this.setupEvent (this.Input, 'focus', function() {return thisCopy.onFocus()});
  this.setupEvent (this.Input, 'blur',  function() {return thisCopy.onBlur()});
  this.setupEvent (this.Input, 'keydown', function() {return thisCopy.onKeyDown()});

  if (input.value == '') this.onBlur();

  return this;
}

InputPlaceholder.prototype.setupEvent = function (elem, eventType, handler)
{
  if (elem.attachEvent)
  {
    elem.attachEvent ('on' + eventType, handler);
  }

  if (elem.addEventListener)
  {
    elem.addEventListener (eventType, handler, false);
  }
}

InputPlaceholder.prototype.onFocus = function()
{
  this.isFirstBlurPassed = true;
  if (!this.SaveOriginal &&  this.Input.value == this.Value)
  {
    if (!this.isEditable) this.Input.value = '';
    this.Input.className = this.CssBase + ' ' + this.CssFilled;
  }
  else
  {
    this.Input.className = this.CssBase;
  }
}

InputPlaceholder.prototype.onKeyDown = function()
{
  this.Input.className = this.CssBase + ' ' + this.CssFilled;
}

InputPlaceholder.prototype.onBlur = function()
{
  if (!this.isFirstBlurPassed || !this.isEditable)
  {
    if (this.Input.value == '' || this.Input.value == this.Value)
    {
      this.Input.value = this.Value;
      this.Input.className = this.CssBase + ' ' + this.CssEmpty;
    }
    else
    {
      this.Input.className = this.CssBase + ' ' + this.CssFilled;
    }
  }
  else
  {
    if (this.Input.value == this.Value)
    {
      this.Input.value = this.Value;
      this.Input.className = this.CssBase + ' ' + this.CssEmpty;
    }
    else
    {
      this.Input.className = this.CssBase + ' ' + this.CssFilled;
    }
  }
}


// Home page elements randomizer


function SwapJobs()
{
  var a = Math.random();
  document.getElementById('Job1').style.display = 'none';
  document.getElementById('Job2').style.display = 'none';
  if (a <= .5) 
  {
    document.getElementById('Job1').style.display = 'inline';
    return;
  } 
  if (a <= 1) 
  {
    document.getElementById('Job2').style.display = 'inline';
    return;
  } 
  
}


// Browser detection

var BrowserDetect = {
	init: function()
	{
		this.browser = this.searchString(this.dataBrowser) || 'An unknown browser';
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || 'an unknown version';
		this.OS = this.searchString(this.dataOS) || 'an unknown OS';
	},
	searchString: function (data)
	{
		for (var i=0; i<data.length; i++)
		{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString)
			{
				if (dataString.indexOf(data[i].subString) != -1) return data[i].identity;
			}
			else if (dataProp) return data[i].identity;
		}
	},
	searchVersion: function (dataString)
	{
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{string: navigator.userAgent, subString: 'OmniWeb', versionSearch: 'OmniWeb/', identity: 'OmniWeb'},
		{string: navigator.vendor, subString: 'Apple', identity: 'Safari'},
		{prop: window.opera, identity: 'Opera'},
		{string: navigator.vendor, subString: 'iCab', identity: 'iCab'},
		{string: navigator.vendor, subString: 'KDE', identity: 'Konqueror'},
		{string: navigator.userAgent, subString: 'Firefox', identity: 'Firefox'},
		{string: navigator.vendor, subString: 'Camino', identity: 'Camino'},
		{string: navigator.userAgent, subString: 'Netscape', identity: 'Netscape'},  // for newer Netscapes (6+)
		{string: navigator.userAgent, subString: 'MSIE', identity: 'Explorer', versionSearch: 'MSIE'},
		{string: navigator.userAgent, subString: 'Chrome', identity: 'Chrome'},
		{string: navigator.userAgent, subString: 'Gecko', identity: 'Mozilla', versionSearch: 'rv'},
		{string: navigator.userAgent, subString: 'Mozilla', identity: 'Netscape', versionSearch: 'Mozilla'}  // for older Netscapes (4-)
	],
	dataOS : [
		{string: navigator.platform, subString: 'Win', identity: 'Windows'},
		{string: navigator.platform, subString: 'Mac', identity: 'Mac'},
		{string: navigator.platform, subString: 'Linux', identity: 'Linux'}
	]
};
BrowserDetect.init();
document.getElementsByTagName('html')[0].className += document.getElementsByTagName('html')[0].className ? ' ' + BrowserDetect.browser : BrowserDetect.browser;
document.getElementsByTagName('html')[0].className += document.getElementsByTagName('html')[0].className ? ' ' + BrowserDetect.browser + BrowserDetect.version : BrowserDetect.browser +  BrowserDetect.version;


// Popup panels

var keepPopupPanel = false;
var currentPopupId = false;
function TogglePopupPanel (PanelId)
{
  if (currentPopupId && currentPopupId != PanelId) TogglePopupPanel(currentPopupId);
	var PopupPanel = document.getElementById(PanelId);
	if (PopupPanel)
	{
		var display = PopupPanel.style.display;
		if (display == 'none' || !display)
		{
			PopupPanel.style.display = 'block';
			if (PanelId == 'SubscriptionPanel') ToggleDisplay('SubscriptionTop');
			keepPopupPanel = true;
      currentPopupId = PanelId;
			
			document.onclick = ClosePopupPanel;
			document.onkeydown = EscapePopupPanel;

			if (PanelId == 'LoginSplashID') var isLoginFocused = LoginFieldFocus();
			PopupPanel.onclick = KeepPopupPanel;
		}
		else
		{
			PopupPanel.style.display = 'none';
			if (PanelId == 'SubscriptionPanel') ToggleDisplay('SubscriptionTop');
    	currentPopupId = false;
		}
		return false;
	}
	else return true;	
}

function KeepPopupPanel()
{
	keepPopupPanel = true;
}

function ClosePopupPanel(event)
{
	if (keepPopupPanel)
	{
		keepPopupPanel = false;
		return;
	}
	var PopupPanel = document.getElementById (currentPopupId);
	if (!PopupPanel) return;
	PopupPanel.style.display = 'none';
	if (currentPopupId == 'SubscriptionPanel') ToggleDisplay('SubscriptionTop');
	currentPopupId = false;

	document.onclick = null;
	document.onkeydown = null;
}

function ToggleDisplay (panelId)
{
  document.getElementById(panelId).style.display = (document.getElementById(panelId).style.display == 'none') ? 'block' : 'none' ;
}

function EscapePopupPanel (event)
{
	if (window.event) event = window.event;
	var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
	if (code == 27)
	{
		var PopupPanel = document.getElementById (currentPopupId);
		if (!PopupPanel) return;
		PopupPanel.style.display = 'none';
  	if (currentPopupId == 'SubscriptionPanel') ToggleDisplay('SubscriptionTop');
		currentPopupId = false;

		document.onclick = null;
		document.onkeydown = null;
	}
}


function HideSelectedOption (ListId, OptionType)
{
  var SelectedOption = document.getElementById(OptionType).value;
  var Options = document.getElementById(ListId).getElementsByTagName('LI');
  for (i=0; i<Options.length; i++) 
  {
    Options[i].style.display='block';
    if (Options[i].getElementsByTagName('A')[0].innerHTML == SelectedOption) Options[i].style.display='none';
  }
}

function SelectOption(Anchor, OptionType, OtherFields)
{
  document.getElementById(OptionType).value = Anchor.innerHTML;
  document.getElementById('Show' + OptionType + 'sAnchor').getElementsByTagName('EM')[0].getElementsByTagName('SPAN')[0].innerHTML = Anchor.innerHTML;
  RefreshOtherFields(OptionType, OtherFields);
  return TogglePopupPanel(Anchor.parentNode.parentNode.id);
}

function EvalDownloadFieldName(id, actionType)
{
  var editionId = eval(actionType + "Edition" + id)
  if (editionId==0)
    editionId = "";

  var versionId = eval(actionType + "Version" + id)
  if (versionId==0)
    versionId = "";
  return "Download" + id + "_" + editionId + "_" + versionId;
}

function RefreshOtherFields (OptionType, OtherFields)
{
    
    var field = document.getElementById(EvalDownloadFieldName(OtherFields, "active"));
    var foundField = field != null
    if (!foundField)
      field = document.getElementById(EvalDownloadFieldName(OtherFields, "default"));
    var Fields = field.parentNode.childNodes;
    
    for (i=0; i<Fields.length; i++)
    {
      if (Fields[i].style != null)
        Fields[i].style.display = 'none';
    }
    if (foundField)
      field.style.display='block';
}


// Grid

function ToggleCheckbox (Checkbox)
{
  var Rows = document.getElementById('GridTable').getElementsByTagName('TR');
  for (i=0; i<Rows.length; i++) 
  {
    var Inputs = Rows[i].getElementsByTagName('INPUT');
    for (k=0; k<Inputs.length; k++) 
    {
      if (Inputs[k].id == Checkbox.id) ColorizeRow(Rows[i], Checkbox);
    }
  }
}

function ToggleAllCheckboxes (Checkbox)
{
  var Rows = document.getElementById('GridTable').getElementsByTagName('TR');
  for (i=0; i<Rows.length; i++) 
  {
    var Inputs = Rows[i].getElementsByTagName('INPUT');
    for (k=0; k<Inputs.length; k++) 
    {
      if (Inputs[k].id.indexOf('Selector') != -1)
      {
        Inputs[k].checked = Checkbox.checked;
        ColorizeRow(Rows[i], Inputs[k]);
      }
    }
  }
}

function ColorizeRow (Row, Checkbox)
{
  Row.className = (Checkbox.checked ? 'SelectedRow' : '');
}

function ResovleRootURL (AbsoluteDocumentPath, RelativeRoot)
{
  var Position = AbsoluteDocumentPath.lastIndexOf('/');
  return ClipUrl(AbsoluteDocumentPath.substring(0, Position), RelativeRoot);
}

function ClipUrl(Url, PathToRoot)
{
  var Position1 = PathToRoot.indexOf('..');
  if (Position1 != -1)
  {
    PathToRoot = PathToRoot.substring(Position1 + 2);
    var Position2 = Url.lastIndexOf('/');
    Url = Url.substring(0, Position2);
    return ClipUrl(Url, PathToRoot);
  }
  return Url + '/';
}

function FeedbackValidate(NameFieldId, EmailFieldId, QuestionFieldId, SubmitButtonId)
{
	var UserName = document.getElementById (NameFieldId);
	var EmailField = document.getElementById (EmailFieldId);
	var QuestionField = document.getElementById (QuestionFieldId);
	var FeedbackSubmit = document.getElementById (SubmitButtonId);

	if (!FeedbackSubmit || !EmailField || !QuestionField || !UserName) return;

	FeedbackSubmit.disabled = (EmailField.value == '') || (QuestionField.value == '') || (UserName.value == ''); 
}


function SwapPaintings()
{
  var a = Math.random();
  document.getElementById('Paint1').style.display = 'none';
  document.getElementById('Paint2').style.display = 'none';
  document.getElementById('Paint3').style.display = 'none';
  if (a <= .333) 
  {
    document.getElementById('Paint1').style.display = 'block';
    return;
  } 
  if (a <= .667) 
	{
		document.getElementById('Paint2').style.display = 'block';
    return;
  } 
  if (a <= 1) 
	{
		document.getElementById('Paint3').style.display = 'block';
    return;
  } 
}

