//------------------------------------------------------------------------------
// **** Global variables ****
//------------------------------------------------------------------------------
// Returns GMT offset in hours


var DATE_SEPARATOR = "\/";
var TIME_SEPARATOR = ":";
var DATE_FORMAT = "mm\/dd\/yyyy";
var TIME_FORMAT = "hh:mm";
var DAY_POS		= 0;
var MONTH_POS	= 3;
var YEAR_POS	= 6;




window.offscreenBuffering = false;

///////////////////////////////////////////////////////////////////////////////
// Implementing OnClick event handler for Mozilla
try {
	document.createElement('A');
	HTMLElement.prototype.click = function () {
		if (typeof this.onclick == 'function') {
			if (this.onclick({type: 'click'}) && this.href)
				window.open(this.href, this.target ? this.target : '_self');
		}
		else if (this.href)
			window.open(this.href, this.target ? this.target : '_self');
	};
}
catch(e) {};


//------------------------------------------------------------------------------
// Checks if object is defined
function isdef(obj) 	{ return (String(obj) != "undefined") && (String(obj) != "null") && (String(obj) != ""); };
function trim(s)			{ return s.replace(/^\s*/,'').replace(/\s*$/,'') };
function innerText(s)	{ return s.replace(/<[^>]+>/g,""); };

String.prototype.trim = function ()	 			{ return this.replace(/^\s*/,'').replace(/\s*$/,'') };
String.prototype.innerText = function ()	{ return this.replace(/<[^>]+>/g,""); };

//------------------------------------------------------------------------------
// Cookie class
function TCookie() {
	this.domain = ".sexsearch.com";
	this.host = location.hostname.toLowerCase();
	if (this.host.indexOf(".bonati") > 0)
		this.domain = this.host.substring(this.host.indexOf(".bonati"));

	//  Corrects 2.x Mac date bug
	this._FixDate = function(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0)
			date.setTime(date.getTime() - skew);
	};

	// "Internal" function to return the decoded value of a cookie
	this._Get = function(offset) {
		var endstr = top.document.cookie.indexOf(";", offset);
		if (endstr == -1)
				endstr = top.document.cookie.length;
		return unescape(top.document.cookie.substring(offset, endstr));
	};

	//  Returns the value of the cookie
	this.Get = function(name) {
		var result = "";
		var arg = name + "=";
		var aLen = arg.length;
		var cLen = top.document.cookie.length;
		var i = 0;
		while (i < cLen) {
			var j = i + aLen;
			if (top.document.cookie.substring(i,j) == arg) {
				result = this._Get(j);
				break;
			};
			i = top.document.cookie.indexOf(" ",i) + 1;
			if (i == 0)
				break;
		};
		return result;
	};

	//  Creates/updates a cookie.
	this.Set = function() {
		var name = arguments[0];
		var value = arguments[1];
		var path = arguments[2];
		var domain = arguments[3];
		var expires = arguments[4];
		var secure = arguments[5];
		var result = "";
		if (name)   result += name + "=";
		if (value)  result += value;
		if (domain) result += "; domain=" + domain;
		if (path)   result += "; path=" + path;
		if (expires) {
			this._FixDate(expires);
			result += "; expires=" + expires.toGMTString();
		};
		if (secure)
			result += "; secure=" + secure;
		top.document.cookie = result;
	};

	//  Deletes a cookie
	this.Delete = function() {
		var name = arguments[0];
		var domain = arguments[1] ? arguments[1] : "";
		var path = arguments[2] ? arguments[2] : "";
		var expires = new Date(0);
		if (name)
			if (this.Get(name))
				this.Set(name, "", expires, domain, path, false);
	};
};


//------------------------------------------------------------------------------
// Browser class
function TBrowser() {
	this.agent = navigator.userAgent.toLowerCase();

	if ( /opera/.test(this.agent) )
		this.name = "opera";
	if ( !(/opera/.test(this.agent)) && /msie/.test(this.agent) )
		this.name = "ie";
	if ( !(/opera/.test(this.agent)) && /gecko/.test(this.agent) )
		this.name = "mozilla";
	if ( !(/opera/.test(this.agent)) && /netscape/.test(this.agent) )
		this.name = "netscape";
	if ( /aol/.test(this.agent) )
		this.name = "aol";
	if ( /navio|navio_aoltv/.test(this.agent) )
		this.name = "aoltv";
	if ( /webtv/.test(this.agent) )
		this.name = "webtv";
	if ( /hotjava/.test(this.agent) )
		this.name = "hotjava";

	this.gecko = (this.name == "mozilla") || (this.name == "netscape");

	switch (this.name) {
		case "opera":
		case "ie":
			this.version = String(parseFloat(this.agent.substr(this.agent.indexOf(this.name)+this.name.length, this.agent.length)));
			if (this.version.indexOf(".") == -1) this.version += ".0";
			break;
		case "mozilla":
			this.version = String(parseFloat(this.agent.substr(this.agent.indexOf("rv:")+3, this.agent.length)));
			if (this.version.indexOf(".") == -1) this.version += ".0";
			break;
		case "netscape":
			this.version = this.agent.substr(this.agent.indexOf(this.name)+this.name.length, this.agent.length);
			this.version = String(parseFloat(this.version.substr(this.version.indexOf("/")+1, this.version.length)));
			if (this.version.indexOf(".") == -1) this.version += ".0";
			break;
	};

	this.major = this.version.substr(0, this.version.indexOf("."));
	this.minor = this.version.substr(this.version.indexOf(".")+1, this.version.length);
};


//------------------------------------------------------------------------------
// DateTime class
function TDateTime() {
	var now = new Date();
	var jan = new Date(now.getFullYear(),0,1, 0,0,0,0);
	var jul = new Date(now.getFullYear(),6,1, 0,0,0,0);
	var dec = new Date(jan.toGMTString().substring(0,jan.toGMTString().lastIndexOf(" ")-1));
	var jun = new Date(jul.toGMTString().substring(0,jul.toGMTString().lastIndexOf(" ")-1));
	var stdGMT = (jan - dec) / 3600000;	// difference in hours
	var dlgGMT = (jul - jun) / 3600000;	// difference in hours with daylight saving
	this.GMT = Number(stdGMT);

	// Returns time in current timezone (in milliseconds)
	this.Zero2Local = function(dtGMT0) {
		if (parseInt(dtGMT0) == NaN)
			return false;
		return Number(dtGMT0) + this.GMT*3600000;
	};

	// Checking validity of the date
	this.CheckDate = function(obj) {
		var result = true;
		var S = obj.value;
		if (S.substr(1,1) == DATE_SEPARATOR)	S = "0"+ S;
		if (S.substr(4,1) == DATE_SEPARATOR)	S = S.substr(0,3) +"0"+ S.substr(3,7);
		result = (S.length == 10) || (S.length == 11);
		if ( result ) {
			var dd   = parseInt((S.substr(DAY_POS,1) 		== "0") ? S.substr(DAY_POS+1,1) 	: S.substr(DAY_POS,2));
			var mm   = parseInt((S.substr(MONTH_POS,1) == "0") ? S.substr(MONTH_POS+1,1)	: S.substr(MONTH_POS,2));
			var yyyy = parseInt(S.substr(YEAR_POS,4));
			result = !( isNaN(mm) || isNaN(dd) || isNaN(yyyy) );
			if ( result ) {
				var date = new Date(yyyy,mm-1,dd);
				result = (date.getDate() == dd) && (date.getMonth() == mm-1) && (date.getFullYear() == yyyy);
				if ( result ) {
					dd   = date.getDate();
					mm   = date.getMonth()+1;
					yyyy = date.getFullYear();
					S = DATE_FORMAT;
					S = S.replace(/dd/g, 	((dd < 10) ? "0"+dd : dd));
					S = S.replace(/mm/g, 	((mm < 10) ? "0"+mm : mm));
					S = S.replace(/yyyy/g,	(((yyyy >= 0) && (yyyy <= 99)) ? "20"+yyyy : yyyy));
				};
			};
		};
		if ( result )
			obj.value = S;
		else {
			alert("Date is not valid!\nPlease, enter the date in the following format:\n\n"+ DATE_FORMAT);
			//obj.focus();
		};
		return result;
	};

	// Checking validity of the time
	this.CheckTime = function(obj) {
		var result = true;
		var S = obj.value;
		if (S.substr(1,1) == ":")	S = "0"+ S;
		if (S.substr(4,1) == "")	S = S.substr(0,3) +"0"+ S.substr(3,1);
		result = S.length == 5;
		if ( result ) {
			var hh = parseInt((S.substr(0,1) == "0") ? S.substr(1,1) : S.substr(0,2));
			var mm = parseInt((S.substr(3,1) == "0") ? S.substr(4,1) : S.substr(3,2));
			result = !( isNaN(hh) || isNaN(mm) );
			if ( result ) {
				var time = new Date(0000,00,00, hh,mm,00,000);
				result = (time.getHours() == hh) && (time.getMinutes() == mm);
				if ( result ) {
					hh = time.getHours();
					mm = time.getMinutes();
					S  = ((hh < 10) ? "0"+hh : hh) +":";
					S += ((mm < 10) ? "0"+mm : mm);
				};
			};
		};
		if ( result )
			this.value = S;
		else {
			alert("Time is not valid!\nPlease, enter the time in the following format:\n\n"+ TIME_FORMAT);
			//this.focus();
		};
		return result;
	};
};


var Cookie = new TCookie();
var Browser = new TBrowser();
var DateTime = new TDateTime();

var LOCAL_GMT = DateTime.GMT;
