﻿/*
 * Returns true if clients browser is NS version 4 or later
 */
function isNS4() {
	return (document.layers) ? true : false;
}

/*
 * Returns true if clients browser is IE version 4 or later
 */
function isIE4() {
	return (document.all) ? true : false;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

/*
 * Returns the number of characters chr containing in string str
 */
function countChar(str,chr) {
	var _len = str.length;
	var _result = 0;
	for (var _i = 0; _i < _len; _i++) {
		if (str.charAt(_i) == chr) {
			_result++;
		}
	}
	return _result;
}

/*
 * Returns the number of non-ASCII symbols (code greater then 127)
 */
function countNonASCII(str) {
	var _len = str.length;
	var _result = 0;
	for (var _i = 0; _i < _len; _i++) {
		if (str.charCodeAt(_i) > 127) {
			_result++;
		}
	}
	return _result;
}

/*
 * Returns the number of white space characters containing in string str
 */
function countWhiteSpaces(str) {
	var _len = str.length;
	var _result = 0;
	for (var _i = 0; _i < _len; _i++) {
		if (str.charCodeAt(_i) < 33) {
			_result++;
		}
	}
	return _result;
}


/*
 * Tests a string is it like an E-Mail address.
 */
function isEmail(str) {
	var _str = new String(trim(str));
	if (countWhiteSpaces(_str) > 0) {
		return false;
	}
	if (countNonASCII(_str) > 0) {
		return false;
	}
	if ((_str.length < 6) || (countChar(_str,"@") != 1)) {
		return false;
	}
	var leftPart = _str.substring(0,_str.indexOf("@"));
	var rightPart = _str.substr(_str.indexOf("@") + 1);
	return ((leftPart.length > 0) && (rightPart.length > 3)
		&& (countChar(rightPart,".") > 0)
		&& (rightPart.charAt(rightPart.length - 1) != ".")
		&& (rightPart.charAt(0) != "."));
}
