﻿function Utils_GetFormRef() {
	if (window.document.forms.length > 0) {
		return window.document.forms.item(0);
	}
	return null;
}
//-------------------------------------------------------------------------------
function Utils_LTrim(str)
{
	return str.replace( /^\s*/, "" )
}
//-------------------------------------------------------------------------------
function Utils_RTrim(str)
{
	return str.replace( /\s*$/, "" );
}
//-------------------------------------------------------------------------------
function Utils_Trim(str)
{
	return Utils_RTrim(Utils_LTrim(str));
}
//-------------------------------------------------------------------------------
function Utils_IsBlank(sStr) {
	sStr = new String(sStr)
	if (!sStr)
		return true;
		
	if (Utils_Trim(sStr) == "") 
		return true;
		
	return false;
}
//-------------------------------------------------------------------------------
function Utils_IsNumber(sStr, bFloat) {
	sStr = Utils_Trim(sStr);
	return !((Utils_IsBlank(sStr)) || isNaN(sStr) || 
				sStr.substr(sStr.length - 1) == "." || 
				sStr.substr(0, 1) == "." || 
				(!bFloat && sStr.indexOf(".") > -1))
}
//-------------------------------------------------------------------------------
function Utils_IsDate(sStr)
{
	// Begin
	// Checks for the following valid date formats:
	// DD/MM/YYYY  DD-MM-YYYY
	// Also separates date into month, day, and year variables
	var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;
  
	/*
	^^^^ Information about this string ^^^^
	Ignore first and last '/' it is code for RegExp
	Anything between () will be matched and remembered for later use
	^ matches first input
	$ matches last input
	\ means that the next char after the '\' has a special meaning
	\2 means same thing as second operation in this case its : (\/|-)
	d means digit, it matches a number from 0 to 9
	{n,m] = matches at least N and at most M occurences. N & M are assumed to be positive
	*/
   
	var matchArray = sStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		errMsg ='Date is not in a valid format.\nUse the DD/MM/YYYY format'
		isError=true
		return false;
	}  
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	  
	if (month < 1 || month > 12)  // check month range
	{ 
		errMsg ='Month must be between 1 and 12.'
		isError=true
		return false;
	}
	if (day < 1 || day > 31) 
	{
		errMsg ='Day must be between 1 and 31.'
		isError=true
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		errMsg ="Month "+month+" doesn't have 31 days!"
		isError=true
		return false
	}
	if (month == 2)  // check for february 29th
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day == 29 && !isleap)) {
			errMsg = "February " + year + " doesn't have " + day + " days!";
			isError=true
			return false;
		}
	}
	return true;  // date is valid
}
//-------------------------------------------------------------------------------
function Utils_IsTime(sStr)
{
	var timePat = /^(\d{1,2})(:)(\d{2})(\2(\d{1,2}))?$/;
	var matchArray = sStr.match(timePat);
	if (matchArray == null)
		return false;
	hour = matchArray[1];
	minute = matchArray[3];
	second = matchArray[4];

	if (second=="") 
		second = null;
	if (hour < 0  || hour > 23)
	    return false;
	if (minute < 0 || minute > 59) 
	  return false;
	if (second != null && (second < 0 || second > 59)) 
	  return false;

	return true;
}
//-------------------------------------------------------------------------------
function Utils_StringToDate(sDate){
	var arr
	sDate = sDate.toString()
	arr = sDate.split("/")
	if (arr.length !=1)
		// date
		var sDate = new Date(arr[1] + '/' + arr[0] + '/' + arr[2])
	else {
		arr = sDate.split(":")
		
		if (arr.length == 2) {
			arr[2] = 0 //set seconds
			arr[3] = 0 // set milliseconds
		}
		if (arr.length == 3)
			arr[3] = 0 // set milliseconds
		
		var sDate = new Date ()
		sDate.setHours(arr[0]) 
		sDate.setMinutes(arr[1]) 
		sDate.setSeconds(arr[2]) 
		sDate.setMilliseconds(arr[3])
	}
	
	return sDate
}
//-------------------------------------------------------------------------------
function Utils_OpenForm(sURL) {
	top.window.location.assign(encodeURI(sURL));
}
//-------------------------------------------------------------------------------
function Utils_ValidateDates(sSmallerDate, sBiggerDate, bWithMessage) {
	var bRetVal = true
	
	if (Utils_StringToDate(sSmallerDate) > Utils_StringToDate(sBiggerDate)) {
		bRetVal = false
		if (bWithMessage)
			JG_MessageBox("תאריך מ - חייב ליהיות קטן או שווה לתאריך עד", "", 1)
	}
	
	return bRetVal;
}
//-------------------------------------------------------------------------------
function Utils_GotoPrevScreen() {
	var oField = null;
	
	oField = document.all.item(FORM_SUBMIT_COUNTER_FIELD);
	if (oField) {
		window.history.go(eval(oField.value) * (-1));
	}
}
//-------------------------------------------------------------------------------
function Utils_ClearCombo(oCombo) {
	var i = 0;
	
	for (i = oCombo.options.length - 1; i >= 0; i--) {
		oCombo.options.remove(i);
	}
}
//--------------------------------------------------------------------------------------
function Utils_AddComboOption(oCombo, sText, sID) {
	var oOption = document.createElement("OPTION");
	
	oCombo.options.add(oOption);
	oOption.innerText = sText;
	oOption.value = sID;
}
//--------------------------------------------------------------------------------------
function Utils_DateToString(dDate) {
	var sDate = "";
	
	if (dDate.getDate() <= 9)
		sDate += "0";
	sDate += dDate.getDate() + "/";
	if (dDate.getMonth() < 9)
		sDate += "0";
	sDate += (dDate.getMonth() + 1) + "/" + dDate.getFullYear();
	return sDate;
}
//-------------------------------------------------------------------------------------------
function Utils_GetDate() {
	return (Utils_DateToString(new Date()));
}
//--------------------------------------------------------------------------------------