// JavaScript Document
function ltrim( source )
{
	var index = 0;
	while( source.charAt(index) == " " ) index++;
	return source.substr(index);
}

function rtrim( source )
{
	var index = source.length - 1;
	while( source.charAt(index) == " " ) index--;
	return source.substring(0, index + 1);
}

function trim( source )
{
	return ltrim(rtrim(source));
}
 
function ZoomImage( img_type, img_file, width, height, description )
{
	var img_src;
	img_src = cfgRootPath + 'includes/showimage.php?type=' + img_type + '&id=' + img_file; // + '&w=' + width + '&h=' + height;

	nw = window.open('', 'ZoomImage', 'toolbar=0,location=0,directories=0,resizable=0,status=0,menubar=0,scrollbars=0,width=' + (width + 20) + ',height=' + (height + 50) + ',top='+eval(screen.height/2-(height+50)/2)+',left='+eval(screen.width/2-(width+20)/2));
	nw.focus();
	nw.document.write('<html>');
	nw.document.write('<head>');
	nw.document.write('<title>21plus.co.il</title>');
	nw.document.write('<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">');
	nw.document.write('</head>');
	nw.document.write('<body style="margin:0px 0px 0px 0px; padding:0px 0px 0px 0px;">');
	nw.document.write('<style type="text/css"> ');
	nw.document.write('body { margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px; background-color: #ffffff; } ');
	nw.document.write('body, div, td { font-family: "Lucida Grande", "Trebuchet MS", "Bitstream Vera Sans", Verdana, Helvetica, sans-serif; font-size: 15px; color: #000000; font-weight: bold; } ');
	nw.document.write('a { font-size: 13px; color: #0055CC; font-weight: normal; text-decoration:underline; } ');
	nw.document.write('a:hover { font-size: 13px; color: #CC0000; font-weight: normal; text-decoration:underline; } ');
	nw.document.write('</style>');
	nw.document.write('<br />');
	nw.document.write('<div align="center">');
	nw.document.write('<img src="' + img_src + '" border="0" onClick="window.close();" alt="Ð â€”Ð Â°Ð Ñ”Ð¡Ð‚Ð¡â€¹Ð¡â€šÐ¡ÐŠ Ð Ñ•Ð Ñ”Ð Ð…Ð Ñ•" title="Ð â€”Ð Â°Ð Ñ”Ð¡Ð‚Ð¡â€¹Ð¡â€šÐ¡ÐŠ Ð Ñ•Ð Ñ”Ð Ð…Ð Ñ•" style="cursor:pointer;" onload="window.stop();">');
	nw.document.write('<br />');
	nw.document.write('<a href="" onClick="window.close();">Ð â€”Ð Â°Ð Ñ”Ð¡Ð‚Ð¡â€¹Ð¡â€šÐ¡ÐŠ Ð Ñ•Ð Ñ”Ð Ð…Ð Ñ•</a>');
	nw.document.write('</div>');
	//nw.document.write('<div align="left">');
	//nw.document.write(description);
	//nw.document.write('</div>');
	nw.document.write('</body>');
	nw.document.write('</html>');
	if( navigator.appName.substring(0,8) == "Netscape" )
	{
		//nw.location = '#';
	}
	nw.focus();
	return false;
}

// Ajax
function PHPtoDIV(div_id, php_str)
{
	var AddScr = document.createElement('script');
	
	found = 0;
	i = 0;
	while(ch = php_str.substr(i,1))
	{
		if ( ch == '?' )
		{
			found = 1;
			break;
		}
		i++;
	}
	
	if ( found == 1)
	{
		str = '&';
	}
	else
	{
		str = '?';
	}
	
	AddScr.src = php_str + str + 'div_id=' + div_id;
	document.body.appendChild(AddScr);
	return false;
}

function NumberCheck(obj)
{
	var NumberPat = /^([0-9.]+)/;	
	var matchArray = obj.value.match(NumberPat);
	
	if ( obj.oldValue == null )
	{
		obj.oldValue = '0';
	}
	
	if ( matchArray == null )
	{
		obj.value = obj.oldValue;
		return false;
	}
	else if ( matchArray[0] != obj.value )
	{
		obj.value = obj.oldValue;
		return false;
	}
	else
	{
		obj.oldValue = obj.value;
		return true;	
	}
}

function UserNameCheck (UserName)
{
	var UserNamePat = /^([a-zA-Z0-9._-]+)/;	
	var matchArray = UserName.match(UserNamePat);
	
	if ( matchArray == null )
	{
		return false;	
	}
	else if ( matchArray[0] != UserName )
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function emailCheck (emailStr)
{
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format. It also is used to separate the username
	from the domain. */

	//var emailPat=/^(.+)@(.+)$/
	var emailPat=/^([a-zA-Z0-9._-]+)@(.+)$/;
	/* The following string represents the pattern for matching all special
	characters. We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	/* The following string represents the range of characters allowed in a 
	username or domainname. It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]";
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes). E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names. E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	/* The following string represents an atom (basically a series of
	non-special characters.) */
	var atom=validChars + '+';
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		alert("Your email is wrong!");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// See if "user" is valid 
	if (user.match(userPat)==null)
	{
		// user is not valid
		alert("Your email is wrong!");
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		// this is an IP address
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				alert("Your email is wrong!");
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null)
	{
		alert("Your email is wrong!");
		return false;
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	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)
	{
		// the address must end in a two letter or three letter word.
		alert("Your email is wrong!");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
		var errStr="Your email is wrong!";
		alert(errStr);
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function OpenPage(page)
{
	//if ( page.length > 0 )
	//{
	//	page = page + '/';	
	//}
	document.location.href = cfgRootPath + page;	
}

// ôîðìàòèðóåò âûâîä ÷èñëà, àíàëîã number_format() â PHP
function number_format(number, decimals, dec_point, thousands_sep)
{
	var exponent = "";
	var numberstr = number.toString ();
	var eindex = numberstr.indexOf ("e");
	var i, z;
	if(eindex > -1)
	{
		exponent = numberstr.substring (eindex);
		number = parseFloat (numberstr.substring (0, eindex));
	}
	if(decimals != null)
	{
		var temp = Math.pow (10, decimals);
		number = Math.round (number * temp) / temp;
	}
	var sign = number < 0 ? "-" : "";
	var integer = (number > 0 ?	Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	var fractional = number.toString ().substring (integer.length + sign.length);
	dec_point = dec_point != null ? dec_point : ".";
	fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
	if(decimals != null && decimals > 0)
	{
		for(i = fractional.length - 1, z = decimals; i < z; ++i)
		fractional += "0";
	}
	thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;
	if (thousands_sep != null && thousands_sep != "")
	{
		for (i = integer.length - 3; i > 0; i -= 3)
		integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	}
	return sign + integer + fractional + exponent;
}

