﻿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;
	var oFrames = null;
	var i = 0;
	var iCnt = 0;
	
	oFrames = window.frames;
	
	for (i = 0; i < oFrames.length; i++) {
		if (oFrames[i].document.all.item(FORM_SUBMIT_COUNTER_FIELD)) {
			iCnt += eval(oFrames[i].document.all.item(FORM_SUBMIT_COUNTER_FIELD).value) - 1;
		}
	} 
	
	oField = document.all.item(FORM_SUBMIT_COUNTER_FIELD);
	if (oField) {
		iCnt += eval(oField.value);
		window.history.go(iCnt * (-1));
	}
}
//-------------------------------------------------------------------------------
function GetSumbitCounter() {
    var oField = document.all.item(FORM_SUBMIT_COUNTER_FIELD);
	
	if (oField) 
		return oField.value;
	else
	    return 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()));
}
//--------------------------------------------------------------------------------------
function Utils_Round(nValue, iDecimals) {
	var iMultiply = 1;
	var i = 0;
	
	while (i < iDecimals) {
		iMultiply = iMultiply * 10;
		i++;
	}
	return Math.round(nValue * iMultiply) / iMultiply;
}
//--------------------------------------------------------------------------------------
function Utils_CheckIdNum(idnum)
{
    while (idnum.length < 9)
    {
        idnum = "0" + idnum;
    }
    if (idnum == "000000000")
    {
		JG_MessageBox("תעודת זהות שגויה.", "", 1);
		return false;
	}
		
    idnum1=idnum.substr(0,1)*1;
    idnum2=idnum.substr(1,1)*2;
    idnum3=idnum.substr(2,1)*1;
    idnum4=idnum.substr(3,1)*2;
    idnum5=idnum.substr(4,1)*1;
    idnum6=idnum.substr(5,1)*2;
    idnum7=idnum.substr(6,1)*1;
    idnum8=idnum.substr(7,1)*2;
    idnum9=idnum.substr(8,1)*1;
 
    if (idnum1>9) idnum1=(idnum1%10)+1
    if (idnum2>9) idnum2=(idnum2%10)+1
    if (idnum3>9) idnum3=(idnum3%10)+1
    if (idnum4>9) idnum4=(idnum4%10)+1
    if (idnum5>9) idnum5=(idnum5%10)+1
    if (idnum6>9) idnum6=(idnum6%10)+1
    if (idnum7>9) idnum7=(idnum7%10)+1
    if (idnum8>9) idnum8=(idnum8%10)+1
    if (idnum9>9) idnum9=(idnum9%10)+1
 
    var sumval=idnum1+idnum2+idnum3+idnum4+idnum5+idnum6+idnum7+idnum8+idnum9;
 
    sumval=sumval%10
    if (sumval>0){
        JG_MessageBox("תעודת זהות שגויה.", "", 1);
        return false;
    }
 
  return true;
}
//--------------------------------------------------------------------------------------
function Utils_CheckEmail(sEMail) 
{
	var filter = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	
	if (!filter.test(sEMail)) 
	{
		JG_MessageBox("כתובת דואר אלקטרוני שגויה.", "", 1);
		return false;
	}
	
	return true;
}
//--------------------------------------------------------------------------------------
