/*
 * Classes:
 *  CBrowser()
 * Objects:
 *  Validator
 *  Keys
 *  Logger
 */

function CBrowser()
{
	this._init = function ()
	{
		var len = this.Profiles.length;
		for (var i = 0; i < len; i++) {
			if (this.Profiles[i].Criterion) {
				this.Name = this.Profiles[i].Id;
				this.Version = this.Profiles[i].Version();
				this.Allowed = (this.Version >= this.Profiles[i].AtLeast);
				break;
			}
		}
		this.IE = (this.Name == 'Microsoft Internet Explorer');
		this.Opera = (this.Name == 'Opera');
		this.Mozilla = (this.Name == 'Mozilla' || this.Name == 'Firefox' || this.Name == 'Netscape' || this.Name == 'Chrome');
		this.Safari = (this.Name == 'Safari');
		this.Chrome = (this.Name == 'Chrome');
		this.Gecko = (this.Opera || this.Mozilla);
	};

	this.Profiles = [
		{
			Id: 'Opera',
			Criterion: window.opera,
			AtLeast: 8,
			Version: function () {
				var start, end, r, start1, start2;
				r = navigator.userAgent;
				start1 = r.indexOf('Opera/');
				start2 = r.indexOf('Opera ');
				if (-1 == start1) {
					start = start2 + 6;
					end = r.length;
				} else {
					start = start1 + 6;
					end = r.indexOf(' ');
				}
				r = parseFloat(r.slice(start, end));
				return r;
			}
		},
		{
			Id: 'Chrome',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('chrome') != -1)
			),
			AtLeast: 0,
			Version: function () {
				return parseFloat(navigator.userAgent.split('Chrome/').reverse().join('Chrome/'));
			}
		},
		{
			Id: 'Safari',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('safari') != -1)
			),
			AtLeast: 1.2,
			Version: function () {
				var r = navigator.userAgent;
				return parseFloat(r.split('Version/').reverse().join(' '));
			}
		},
		{
			Id: 'Firefox',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				((navigator.userAgent.toLowerCase().indexOf('firefox') != -1) ||
				(navigator.userAgent.toLowerCase().indexOf('iceweasel') != -1))
			),
			AtLeast: 1,
			Version: function () {
				var userAgent = navigator.userAgent.toLowerCase();
				if (userAgent.indexOf('firefox/') != -1) {
					return parseFloat(userAgent.split('firefox/').reverse().join('firefox/'));
				}
				if (userAgent.indexOf('iceweasel/') != -1) {
					return parseFloat(userAgent.split('iceweasel/').reverse().join('iceweasel/'));
				}
				return 0;
			}
		},
		{
			Id: 'Netscape',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('netscape') != -1)
			),
			AtLeast: 7,
			Version: function () {
				var r = navigator.userAgent.split(' ').reverse().join(' ');
				r = parseFloat(r.slice(r.indexOf('/') + 1, r.indexOf(' ')));
				return r;
			}
		},
		{
			Id: 'Mozilla',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('mozilla') != -1)
			),
			AtLeast: 1,
			Version: function () {
				var r = navigator.userAgent;
				return parseFloat(r.split('Firefox/').reverse().join('Firefox/'));
			}
		},
		{
			Id: 'Microsoft Internet Explorer',
			Criterion:
			(
				(navigator.appName.toLowerCase() == 'microsoft internet explorer') &&
				(navigator.appVersion.toLowerCase().indexOf('msie') !== 0) &&
				(navigator.userAgent.toLowerCase().indexOf('msie') !== 0) &&
				(!window.opera)
			),
			AtLeast: 5,
			Version: function () {
				var r = navigator.userAgent.toLowerCase();
				r = parseFloat(r.slice(r.indexOf('msie') + 4, r.indexOf(';', r.indexOf('msie') + 4)));
				return r;
			}
		}
	];

	this._init();
}

var Validator = {
    IsEmpty: function (strValue)
    {
		return (strValue.replace(/\s+/g, '') == '');
    },
    
    HasEmailForbiddenSymbols: function (strValue)
    {
		return (strValue.match(/[^A-Z0-9\"!#\$%\^\{\}`~&'\+\-=_@\.]/i));
    },
    
    IsCorrectEmail: function (strValue)
    {
		return (strValue.match(/^[A-Z0-9\"!#\$%\^\{\}`~&'\+\-=_\.]+@[A-Z0-9\.\-]+$/i));
    },
    
    IsCorrectServerName: function (strValue)
    {
		return (!strValue.match(/[^A-Z0-9\.\-\:\/]/i));
    },
    
    IsPositiveNumber: function (intValue)
    {
        if (isNaN(intValue) || intValue <= 0 || Math.round(intValue) != intValue) {
            return false;
        }
        return true;
    },
    
    CorrectNumber: function (value, minValue, maxValue)
    {
        if (isNaN(value) || value <= minValue) {
            return minValue;
        }
        if (maxValue != undefined && value >= maxValue) {
			return maxValue;
		}
        return Math.round(value);
    },
    
    IsPort: function (intValue)
    {
		return (this.IsPositiveNumber(intValue) && intValue <= 65535);
    },
    
    HasSpecSymbols: function (strValue)
    {
		return (strValue.match(/["\/\\*?<>|:]/));
    },
    
    IsCorrectFileName: function (strValue)
    {
        if (!this.HasSpecSymbols(strValue)) {
			return !strValue.match(/^(CON|AUX|COM1|COM2|COM3|COM4|LPT1|LPT2|LPT3|PRN|NUL)$/i);
        }
        return false;
    },
    
    CorrectWebPage: function (strValue)
    {
        return strValue.replace(/^[\/;<=>\[\\#\?]+/g, '');
    },
    
    HasFileExtention: function (strValue, strExtension)
    {           
		return (strValue.substr(strValue.length - strExtension.length - 1, strExtension.length + 1).toLowerCase() == '.' + strExtension.toLowerCase());
    }
};

var Keys = 
{
	Enter: 13,
	Shift: 16,
	Ctrl: 17,
	Space: 32,
	PageUp: 33,
	PageDown: 34,
	End: 35,
	Home: 36,
	Up: 38,
	Down: 40,
	Delete: 46,
	A: 65,
	C: 67,
	N: 78,
	P: 80,
	R: 82,
	S: 83,
	Comma: 188,
	Dot: 190,

	GetCodeFromEvent: function (ev)
	{
		var key = -1;
		if (window.event) {
			key = window.event.keyCode;
		} else if (ev) {
			key = ev.which;
		}
		return key;
	}
};

var Logger = {
	_container: null,
	_initialized: false,
	
	_init: function ()
	{
		if (this._initialized == true) {
			return;
		}
		this._container = CreateChild(document.body, 'div');
		this._container.dir = 'ltr';
		with (this._container.style) {
			color = 'black';
			border = 'solid 2px black';
			background = 'white';
			width = '700px';
			height = '100px';
			bottom = '0px';
			right = '0px';
			position = 'absolute';
			zIndex = '10';
			textAlign = 'left';
			overflow = 'auto';
		}
		this._initialized = true;
	},
	
	_write: function (msg)
	{
		this._init();
		if (!this._initialized) return;
		this._container.innerHTML = this._container.innerHTML + msg;
	},
	
	Write: function (msg)
	{
		this._write(msg + '; ');
	},
	
	WriteLine: function (msg)
	{
		this._write(msg + '<br />');
	},
	
	Clear: function ()
	{
		this._init();
		if (!this._initialized) return;
		this._container.innerHTML = '';
	}
};

if (typeof window.JSFileLoaded != 'undefined') {
	JSFileLoaded();
}
