/* This is file is contain all common functions, please if you add new JavaScript function and think
   that function can usefull for another people, add it to this file (with comments).
   To add this file to your htm/cfm write this
   &lt;script type="text/javascript" language="JavaScript" src="/Common/functions.js"&gt;&lt;/script&gt;
*/
//		Bursov

// Function returns first n characters
function Left(str, n)
{
	return str.substring(0, n);
}

// Function returns random string
//	Bursov
function getUniqueString()
{
	lpStr = '';
	
	for (i=0; i<10; i++)
		lpStr += ''+Math.ceil(Math.random()*10);
	
	return lpStr;
}

// Function remove spaces from left & right
//	Bursov
function trim(str)
{
	if (str.length<=0) return '';
	
	str = '' + str; // for NN
	
	i1 = 0;
	i2 = str.length-1;
	if (isNaN(i2)) return str; // for NN

	while ( (str.substring(i1, i1+1) == ' ') && (i1<=i2) ) i1++;
	while ( (str.substring(i2, i2+1) == ' ') && (i2>=0) ) i2--;

	if (i1>i2) return '';
	return (str.substring(i1, i2+1));
}

// Functions set cookie (expires in 10 years)
//	Bursov
function setCookie(lpName, lpValue)
{
	setCookieWithPath(lpName, lpValue, '');
}

function setCookieWithPath(lpName, lpValue, lpPath)
{
	var d = new Date();
	d.setYear(d.getFullYear()+10);
	setCookieWithExpiresAndPathAndDomain(lpName, lpValue, d.toGMTString(), lpPath, '');
}

function setCookieWithExpiresAndPathAndDomain(lpName, lpValue, lpExpires, lpPath, lpDomain)
{
	var cookStr = lpName + '=' + escape(lpValue) + ';';
	if (lpExpires != '')
		cookStr +=	'expires=' + lpExpires + ';';
	if (lpPath != '')
		cookStr +=	'path=' + lpPath + ';';
	if (lpDomain != '')
		cookStr +=	'domain=' + lpDomain + ';';
	document.cookie = cookStr;
}

// Function get cookie
//	Bursov
function getCookie(lpName)
{
	aCookies = document.cookie.split("; ");
	for (i = 0; i<aCookies.length; i++)
	{
		aToken = aCookies[i].split("=");
		if (aToken[1]==null)
			aToken[1]='';
		if (trim(lpName)==trim(aToken[0]))
			return unescape(aToken[1]);
	}
	return null;
}

// function removes all useless spaces
// example:
// lpStr = '    a     b cccc   '
// function return 'a b cccc'
// Bursov
function NormalizeSpaces(lpStr)
{
	lpStr = trim(lpStr);

	nPos = lpStr.indexOf('  ');	
	while (nPos!=-1)
	{
		lpStr = lpStr.substring(0, nPos) + lpStr.substring(nPos+1, lpStr.length);
		nPos = lpStr.indexOf('  ');	
	}
	
	return lpStr;
}

// Function check input e-mail and return false if it not valid
//	Bursov
function CheckEMail(e_mail)
{
	e_mail = trim(e_mail);
	i = e_mail.indexOf("@");
	if (i<1) return false;
	if (e_mail.lastIndexOf(".")<i) return false;
	if (i>(e_mail.length-4)) return false;
	return true;
}

// function returns browser number
// 0 - Netscape (full zero)
// 1 - Explorer
// 2 - Opera
// support Explorer, Netscape
// HotJava & Mozilla identify as Netscape
// Opera can be identified as IE, Netscape or Opera
//	Bursov
function getBrowserID()
{
	if (navigator.appName=='Netscape')
		return 0;
	if (navigator.appName=='Microsoft Internet Explorer')
		return 1;
	if (navigator.appName=='Opera')
		return 2;
}

// funtion returns browser version
//	Bursov
function getBrowserVersion()
{
	if (getBrowserID()==1)
	{
		var sS = ' MSIE ';
		var nPos = navigator.appVersion.indexOf(sS);
		if (nPos>=0)
		{
			nPos += sS.length;
			sS = navigator.appVersion.substring(nPos, navigator.appVersion.length);
			return sS.substring(0, sS.indexOf(';'));
		}
	}
	return (navigator.appVersion.substring(0, navigator.appVersion.indexOf(' ')));
}

// function returns index of any symbol
// Bursov
function FindOneOf(lpSString, lpStr)
{
	for (i=0; i<lpStr.length; i++)
	{
		nPos = lpSString.indexOf(lpStr.substring(i, i+1));
		if (nPos!=-1)
			return nPos;
	}
	
	return -1;
}

// function remove any special symbols
// Bursov
function RemoveSpecialSymbols(lpStr)
{
	lpSymbols = "<>\\/'\"";
	return RemoveSpecialSymbolsCustom(lpStr, lpSymbols);
}

function RemoveSpecialSymbolsCustom(lpStr, lpSymbols)
{
	return ReplaceSpecialSymbolsCustom(lpStr, lpSymbols, "");
}

function ReplaceSpecialSymbolsCustom(lpStr, lpSymbols, lpRepSt)
{
	nPos = FindOneOf(lpStr, lpSymbols);
	while (nPos!=-1)
	{
		lpStr = lpStr.substring(0, nPos) + lpRepSt + lpStr.substring(nPos+1, lpStr.length);
		nPos = FindOneOf(lpStr, lpSymbols);
	}
	return lpStr;
}

function makeed2(sn, v)
{
	sn.style.border = ((v)?("1px solid black"):("1px solid #d0d0d0"));
}

var bIsFunctions_JS_Loaded = "functions.js";
