function createCookie(name, value, days) {
	var date, expires;

	if (days) {
		date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	else expires = "";

	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

function eraseCookie(name) {
	createCookie(name, '', -1);
}

function getCookie(name)
{
	var results = document.cookie.match (name + '=(.*?)(;|$)');

  if (results) return (unescape(results[1]));
  else return null;
}

function getCookieLifeDays()
{
	return 1/8; //3 hours
}

function constrain(n, lower, upper) {
   if (n > upper) return upper;
   else if (n < lower) return lower;
   else return n;
}

/**
 * for calling some event, ex: onchange
 *
 * @param htmlObject element, can be simple element id
 * @param string event, ex: 'change','click' or other events
 * @author kolegm, 4.06.2009
 */
function cspFireEvent(element, event){
  var evt;
  if (document.createEventObject){
    // dispatch for IE
    evt = document.createEventObject();
    return $(element).fireEvent('on' + event, evt);
  }
  else{
    // dispatch for firefox + others
    evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true ); // event type,bubbling,cancelable
    return !$(element).dispatchEvent(evt);
  }
}

function randomString(length)
{
  var
  	i,
  	x,
  	chars,
  	randomString;

  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890";
  randomString = "";

  for( x=0; x<length; x++ )
  {
    i = Math.floor(Math.random() * 62);
    randomString += chars.charAt(i);
  }
  return randomString;
}

/*
 * REMOVE whitespaces before and after string
 */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

/*
 * Returns true if parameter string is valid e-mail.
 */
function is_valid_email(str)
{
  var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  return filter.test(str);

}

/*
 * Returns true if parameter is string with word characters (not empty and not only spaces)
 */
function is_word(str)
{
	return str.match(/\w+/);
}

/*
 * Returns true if parameter is string with only digits
 */
function is_digit(str)
{
	return str.match(/^\d+$/);
}

/*
 * Returns true if parameter is float
 */
function is_float(nbr)
{
	return nbr.match(/^-?\d+[,.]?\d*$/);
}

function callUserFunction(url, function_name)
{
	eval(function_name + "('" + url + "')");
}

function exportToExcel(url)
{
	document.location=url;
}


function positionSignInBox()
{
	var box = $('sign-in-box');
	if (box)
	{
		var vH = document.viewport.getHeight();
		var vW = document.viewport.getWidth();
		box.style.top = (vH/2 - (box.offsetHeight/2)) + 'px';
		box.style.left = (vW/2 - (box.offsetWidth/2)) + 'px';
	}
}


function openPopupWin(url, inputParams)
{
  // Set default params for window
  if (undefined == inputParams)
  {
    this.params = {
      title: 'Pop-Up window',
      width: '800',
      height: '600',
      toolbar: 'no',
      scrollbars: 'yes',
      status: '0',
      resizable: 'yes',
      menubar: 'no'
    }
  }

//  Object.extend(this.params, inputParams);

  // Save new window identifier
  var win = window.open(
    url,
    'popupWin',
    "toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes"
  );
  win.focus();
}

function doPrint()
{
  window.print();
  window.close();
}

var vResellerAllowOwnCommission = {
  doChange: function(t) {
    if (t.checked) {
      $("headResCharges").colSpan = "2";
      $$(".res_commission").each(function(item){ item.hide(); });
    }
    else {
      if ($("v_resellers_web_service_enabled").checked || $("v_resellers_white_label_enabled").checked)
      {
        t.checked = true;
      }
      else
      {
        $("headResCharges").colSpan = "3";
        $$(".res_commission").each(function(item){ item.show(); });
      }
    };
  }
}

