//
// JavaScript Utilities
// Copyright (c) 2003 Gulliver S.r.L.
// All Rights Reserved.
//


// - COOKIE UTILITIES -
// VERSION: 1.0
// DATE: 03-01-24
// AUTHOR: David Flanagan
// TESTED WITH: IE 6.0, NN 7.0
//
//----------------------------------------------------------------------------------------------------

function Cookie(document, name, hours, path, domain, secure) {
  this.$document = document;
  this.$name = name;

  if (hours) {
    this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
  } else {
    this.$expiration = null;
  }

  if (path) {
    this.$path = path;
  } else {
    this.$path = null;
  }

  if (domain) {
    this.$domain = domain;
  } else {
  	this.$domain = null;
  }

  if (secure) {
  	this.$secure = true;
  } else {
  	this.$secure = false;
  }
}


function CookieStore() {
  var cookieVal = "";
  for (var prop in this) {
  	if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) {
	  continue;
	}
	if (cookieVal != "") {
		cookieVal += '&';
	}
	cookieVal += prop + ':' + escape(this[prop]);
  }

  var cookie = this.$name + '=' + cookieVal;
  if (this.$expiration) {
    cookie += '; expires=' + this.$expiration.toGMTString();
  }
  if (this.$path) {
    cookie += '; path=' + this.$path;
  }
  if (this.$domain) {
  	cookie += '; domain=' + this.$domain;
  }
  if (this.$secure) {
    cookie += '; secure';
  }

  this.$document.cookie = cookie;
}


function CookieLoad() {
  var allCookies = this.$document.cookie;
  if (allCookies == "") {
    return false;
  }

  var start = allCookies.indexOf(this.$name + '=');
  if (start == -1) {
    return false;
  }
  start += this.$name.length + 1;

  var end = allCookies.indexOf(';', start);
  if (end == -1) {
    end = allCookies.length;
  }

  var cookieVal = allCookies.substring(start, end);
  var a = cookieVal.split('&');
  for (i = 0; i < a.length; i++) {
    a[i] = a[i].split(':');
  }

  for (i = 0; i < a.length; i++) {
    this[a[i][0]] = unescape(a[i][1]);
  }

  return true;
}


function CookieRemove() {
  var cookie = this.$name + '=';
  if (this.$path) {
    cookie += '; path=' + this.$path;
  }
  if (this.$domain) {
    cookie += '; domain=' + this.$domain;
  }
  cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

  this.$document.cookie = cookie;
}


new Cookie();
Cookie.prototype.store = CookieStore;
Cookie.prototype.load = CookieLoad;
Cookie.prototype.remove = CookieRemove;

//----------------------------------------------------------------------------------------------------


// VERSION: 1.0
// DATE: 02-12-06
// AUTHOR: Silvano Ondei
// TESTED WITH: IE 6.0, NN 7.0, Opera 6.04
function openBrWindow(url, winName, winFeatures) {
  var win, x, y, width, height;
  var i, j;
  i = winFeatures.indexOf("width=");
  if (i == -1) {
    width = screen.width / 2;
	i = 0;
  } else {
  	i += 6;
	j = winFeatures.indexOf(",", i);
	if (j == -1) {
	  j = winFeatures.length;
	}
	width = parseInt(winFeatures.substring(i, j));
  }
  i = winFeatures.indexOf("height=", i);
  if (i == -1) {
    height = screen.height / 2;
	i = 0;
  } else {
  	i += 7;
	j = winFeatures.indexOf(",", i);
	if (j == -1) {
	  j=winFeatures.length;
	}
	height = parseInt(winFeatures.substring(i, j));
  };
  i = winFeatures.indexOf("left=");
  j = winFeatures.indexOf("screenX=");
  if ((i == -1) && (j == -1)) {
    x = (screen.width - width) / 2;
	winFeatures += ",left=" + x + ",screenX=" + x;
  }
  i = winFeatures.indexOf("top=");
  j = winFeatures.indexOf("screenY=");
  if ((i == -1) && (j == -1)) {
    y = (screen.height - height) / 2;
	winFeatures += ",top=" + y + ",screenY=" + y;
  }
  win = window.open(url, winName, winFeatures);
  win.focus();
  
  return win;
}


// VERSION: 1.0
// DATE: 02-12-13
// AUTHOR: Silvano Ondei
// TESTED WITH: IE 6.0, NN 7.0, Opera 6.04
function getCheckedRadioCtrlValue(radioCtrlsArray) {
  var checkedRadioCtrlValue = null;

  for (var i = 0; i < radioCtrlsArray.length; i++) {
    if (radioCtrlsArray[i].checked) {
	  checkedRadioCtrlValue = radioCtrlsArray[i].value;
	  break;
	}
  }

  return checkedRadioCtrlValue;
}


// VERSION: 1.0
// DATE: 02-12-06
// AUTHOR: Silvano Ondei
// TESTED WITH: IE 6.0, NN 7.0, Opera 6.04
function isValidEmailAddrs(emailAddrs) {
  var emailPat = /^(.+)@(.+)$/;
  var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars = "\[^\\s"+specialChars+"\]";
  var quotedUser = "(\"[^\"]*\")";
  var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom = validChars + '+';
  var word = "(" + atom + "|" + quotedUser + ")";
  var userPat = new RegExp("^" + word + "(\\."+word+")*$");
  var domainPat = new RegExp("^" + atom + "(\\."+atom+")*$");
  var matchArray = emailAddrs.match(emailPat);
  if (matchArray == null) {
	return false
  }
  var user = matchArray[1];
  var domain = matchArray[2];
  if (user.match(userPat) == null) {
    return false
  }
  var ipArray = domain.match(ipDomainPat);
  if (ipArray != null) {
    for (var i = 1; i <= 4; i++) {
      if (ipArray[i] > 255) {
	    return false;
	  }
    }
    return true;
  }
  var domainArray = domain.match(domainPat);
  if (domainArray == null) {
    return false;
  }
  var atomPat = new RegExp(atom, "g");
  var domArr = domain.match(atomPat);
  var len = domArr.length;
  if ((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) {
    return false;
  }
  if (len < 2) {
    return false;
  }

  return true;
}
