﻿
var Base = Class.create();
Base.Login = Class.create();
Base.Search = Class.create();

Base.setCookie = function(name, value, domain, expiresDays) 
{ 
   expiresDays = (expiresDays * 24 * 60 * 60 * 1000);
   var exp = new Date();
   exp.setTime(exp.getTime() + expiresDays);
   document.cookie = name + "=" + escape (value) + ((exp == null) ? "" : ("; expires=" + exp.toGMTString())) +  ((domain == null) ? "" : ("; domain=" + domain));
}

Base.getCookie = function(name) 
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1)
  {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  }
  else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

Base.moveTop = function()
{
   scroll(0,0);
}

Base.removeHtml = function(text, replaceBrackets)
{
   // This line is optional, it replaces escaped brackets with real ones, i.e. < is replaced with < and > is replaced with >
   if(replaceBrackets && replaceBrackets == true) text = text.replace(/&(lt|gt);/g, function (strMatch, p1){ return (p1 == "lt")? "<" : ">"; });
   text = text.replace(/<\/?[^>]+(>|$)/g, '');
   return text;
}

Base.replaceBreaks = function(text)
{
   text = text.replace(/\r\n/gi, '<br/>');
   text = text.replace(/\r\n/gi, '<br/>');
   return text;
}

Base.replaceNewLines = function(text)
{
   text = text.replace(/\r\n/gi, '<br>');
   text = text.replace(/\n/gi, '<br>');
   text = text.replace(/\r/gi, '<br>');
   text = text.replace(/\r\n/gi, '<br/>');
   text = text.replace(/\n/gi, '<br/>');
   text = text.replace(/\r/gi, '<br/>');
   return text;
}

Base.getSelection = function(clear)
{
   var text;
   clear  = clear ? clear : true;
   if (document.getSelection)
   { 
      text = document.getSelection();
      if(text && !(text == '')) window.getSelection().collapseToStart();
   }
   else if (document.selection && document.selection.createRange)
   {
      var range = document.selection.createRange();
      text = range.text;
      range.collapse();
      range = null;
   }
   if(!text) text = '';
   return text;
}


Base.noenter = function()
{
   alert(window.event.keyCode == 13);
  return !(window.event && window.event.keyCode == 13);
}

Base.catchEnter = function(e)
{
   var key;

   if(window.event)
        key = window.event.keyCode;     //IE
   else
        key = e.which;     //firefox

   var result = key != 13;

   return result;
}

// **** Base.Login ****

Base.Login.username = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Username' : item.value);      
   else
      item.value = (item.value == 'Username' ? '' : item.value);
}

Base.Login.password = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Password' : item.value);      
   else
      item.value = (item.value == 'Password' ? '' : item.value);
}

// **** Base.Search ****

Base.Search.focus = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Search' : item.value);      
   else
      item.value = (item.value == 'Search' ? '' : item.value);
}

Base.Search.init = function()
{
   var query = $id('_SearchQuery');
	var search = query.value.trim();
	if(search == '' || search == null)
	{
	   alert('Please enter a search term and try again.');
	   return false;
	}

   window.location = '/search/' + search.replace(/[^\w\s]/gi, '');;
   //window.location = 'http://www.google.com/search?q=' + search + ' site:' + location.host;
}

Base.Search.initKey = function(e)
{
   if(!Base.catchEnter(e))
   {
      Base.Search.init();
      return false;
   }

   return true;
}

Base.timeDiff = function(fromDate, toDate)
{
	var sec = toDate.getTime() - fromDate.getTime();
	var baseSec = sec;
	
	if (isNaN(sec)){ alert(0); return; }
	if (sec < 0){ alert(1); return; }

	var second = 1000, minute = 60 * second, hour = 60 * minute;

	var hours = Math.floor(sec / hour);
	sec -= hours * hour;
	var minutes = Math.floor(sec / minute);
	sec -= minutes * minute;
	var seconds = Math.floor(sec / second);
	sec -= seconds * second;
	var ms = sec;
	var result = hours + ":" + minutes + ":" + seconds + ':' + ms + ' (' + (baseSec) + 'ms)';

	return result;
}