/*
 * Description: miscelaneouse functions for validation purposes
 *
 * Target: client
 *
 * Author: Dmitriy Olshanskiy
 * Author: Maxim Pustovoy
 * Create date: 2000-05-05
 *
 * Last update date: 2000-09-05
 * Last update by: Dmitriy Olshanskiy
 *
 */

/*
 * 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;
}

/*
 * Returns the number of characters chr containing in string str
 */
function countChar(str,chr)
{
	var _len=str.length, _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, _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, _result=0;
	for(var _i=0;_i<_len;_i++)if(str.charCodeAt(_i)<33)_result++;
	return _result;
}

/*
 * Returns the number of non-digits characters including white space and non-ASCII characters
 */
function countNondigits(str)
{
	var _len=str.length, _result=0;
	for(var _i=0;_i<_len;_i++)if((str.charCodeAt(_i)<48)||(str.charCodeAt(_i)>57))_result++;
	return _result;
}

/*
 *********************** DEPRECATED ***********************
 * Returns true if parameter is a number (integer or float)
 */
function isNumber(n)
{
	if(isNaN(parseFloat(n)))return false;
	return true;
}

/*
 * Returns true if parameter is an integer number
 */
function isInteger(n)
{
	if(!isNumber(n))return false;
	if(Math.ceil(n)!=Math.floor(n))return false;
	return true;
}

/*
 * Returns true if parameter is an natural integer number (more or equal to zero)
 */
function isNatural(n)
{
	if(!isInteger(n))return false;
	if(n<0)return false;
	return true;
}

/*
 * Returns true if parameter is an positive number (integer or float)
 */
function isPositiveNumber(n)
{
	if(!isNumber(n))return false;
	if(n>0)return true;
	return false;
}

/*
 * Returns true if parameter is an positive integer number
 */
function isPositiveInteger(n)
{
	if(!isInteger(n))return false;
	if(n<1)return false;
	return true;
}

/*
 * Takes a string and returns its trimmed copy on the left side.
 */
function trimLeft(str)
{
	var _len=str.length;
	var _i,_result=new String(str);
	if(_len<1)return _result;
	for(_i=0;_i<_len;_i++)if(str.charCodeAt(_i)>32 || str.charCodeAt(_i)<-1)break;
	if(_i<1)return _result;
	_result=_result.substring(_i,_len);
	return _result;
}

/*
 * Takes a string and return its trimmed copy on the right side.
 */
function trimRight(str)
{
	var _len=str.length;
	var _i,_result=new String(str);
	if(_len<1)return _result;
	for(_i=0;_i<_len;_i++)if(str.charCodeAt(_len-_i-1)>32 || str.charCodeAt(_i)<-1)break;
	if(_i<1)return _result;
	_result=_result.substring(0,_len-_i);
	return _result;
}

/*
 * Takes a string and return its trimmed copy on the both left and right sides.
 */
function trim(str)
{
	var _result=trimLeft(str);
	if(_result.length>0)_result=trimRight(_result);
	return _result;
}

/*
 * Tests a string is it a blank string (consists of white space characters only).
 */
function isBlank(str)
{
	if(trimLeft(str)=="")return true;
	return false;
}

/*
 * 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);
	if((leftPart.length<1)||(rightPart.lenght<4))return false;
	if(countChar(rightPart,".")<1)return false;
	if(rightPart.charAt(rightPart.length-1)==".")return false;
	return true;
}

/*
 * Takes two strings as passwords and a numbers as minimal and maximal password lengths.
 * Check if passwords are equal, consists of characters between 32 an 126 ASCII codes
 * and has valid length.
 */
function isValidPasswords(psw1,psw2,minLen,maxLen)
{
	if((psw1!=psw2)||(psw1.length<minLen)||(psw1.length>maxLen))return false;
	var _len=psw1.length;
	for(var _i=0;_i<_len;_i++)if((psw1.charCodeAt(_i)<32)||(psw1.charCodeAt(_i)>126))return false;
	return true;
}

/*
 * Returns true if year is leap year
 */
function isLeapYear(year) {
	if(year % 4 != 0)return false;
	if(year % 400 == 0)return true;
	if(year % 100 != 0)return true;
	return false;
}

/*
 * Returns number of days in month depending on year or 0 if error
 */
function getDaysInMonth(month,year) {
	if((month<1)||(month>12))return 0;
	var _daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var _result=_daysInMonth[month-1];
	if((month==2)&& isLeapYear(year))_result++;
	return _result;
}

/*
 * Tests arguments if there represents a correct date.
 */
function isCorrectDate(year,month,day) {
	if((!isNatural(year))||(!isNatural(month))||(!isNatural(day)))return false;
	if((year<1)||(month<1)||(month>12)||(day<1))return false;
	var _daysInMonth=getDaysInMonth(month,year);
	if(day>_daysInMonth)return false;
	return true;
}

/*
 * Compares two dates and returns: -1 if date1 earlier date2, 0 if date1 equals date2 and 1 if date1 later date2.
 * date1 and date2 are objects of type Date
 */
function compareDates(date1,date2) {
	var dateInt1=date1.getTime();
	var dateInt2=date2.getTime();
	if(dateInt1==dateInt2)return 0;
	if(dateInt1<dateInt2)return -1;
	return 1;
}

/*
 * Tests a string is it like an telephone number.
 */
function isPhoneNumber(str)
{
	var _len=str.length, _result=true, _code;
	if(_len<1)return false;
	for(var _i=0;_i<_len;_i++)
	{
		_code=str.charCodeAt(_i);
		if(
			(_code==32) // space
			||((_code>=48)&&(_code<=57)) // digits
			||(_code==40) // left brace
			||(_code==41) // right brace
			||(_code==43) // plus sign
			||(_code==45) // minus sign
			)continue;
		_result=false;
		break;
	}
	return _result;
}

/*
 * Tests a string is it like a WWW URL address.
 */
function isWWW(str)
{
	var _str=new String(trim(str));
	if(countWhiteSpaces(_str)>0)return false;
	if(countChar(_str,".")<1)return false;
	return true;
}

/*
 * Tests a string is it represents a file name with 'jpg', 'jpeg' or 'gif' extentions.
 */
function isImageExtention(str)
{
	var _ext=str.substr(str.lastIndexOf(".")+1).toLowerCase();
	if(
		(_ext=="jpg")||
		(_ext=="jpeg")||
		(_ext=="gif")
		)return true;
	return false;
}

/*
 * Tests a string is it represents a file name with valid extention.
 */
function isValidExtention(str)
{
	var _ext=str.substr(str.lastIndexOf(".")+1).toLowerCase();
	if(
		(_ext=="avi")||
		(_ext=="mpe")||
		(_ext=="mpeg")||
		(_ext=="mpg")||
		(_ext=="mp3")||
		(_ext=="pdf")
		)return true;
	return false;
}

/*
 *  Tests a string if it's correct currency. Should contain only digits or "," or "."
 *  Should conform to specified in regExp format.
 */
function isFloat(inputField)
{ 
  inputField=trim(inputField);
  var _numberToParse=inputField;
  if(isBlank(_numberToParse)==true)return false;
  var status=true;
  var replaceCommas=new RegExp(",","g");
  _numberToParse=_numberToParse.replace(replaceCommas,".");
  for(var i=0;i<=_numberToParse.length-1;i++)
      if(isNaN(parseInt(_numberToParse.charAt(i)))&&(_numberToParse.charAt(i)!="."))status=false;
	    if (status)
         {	var regexp=/^(\d{1,}|\d{1,}.\d{1,2}|\d{1,},\d{1,2})$/;
	        if(regexp.test(_numberToParse))
	        {
			   return true;
			}
			else status=false;
		 }
	 
    if (!status) 
       return false;
}
/* Formats and checks string passed as currency. Substitudes "," with "."
*/

function formatFloat(inputField)
{ 
   inputField=trim(inputField);
   var _numberToParse=inputField;
   if(isFloat(_numberToParse)!=false)
   {
     var replaceCommas=new RegExp(",","g");
	 _numberToParse=_numberToParse.replace(replaceCommas,".");
	 if(_numberToParse.indexOf(".")==-1)
	 {
	    _numberToParse+=".0";
	 }
	 return _numberToParse;	
   }
   else 
     return inputField;
}


/* 
	Open new window 'popup' 616x420. 
	Need specific parametrs [mSpecParams]: resizable=yes|no,scrollbars=yes|no
	(Maxim Subbotin)
*/

function openPopup(mSource,mSpecParams){
	var mParams = 'toolbar=no,location=no,status=no,menubar=no,width=616,height=420,top=100,left=100' + ',' + mSpecParams;
	popupWindow = window.open('','popup',mParams);
	popupWindow.focus();
	popupWindow = window.open(mSource,'popup',mParams);
	return;
}
