//***---  This function is used to check if the field is empty  ---***//
	function isEmpty(strField)
	{
		var bRetVal=true;
		if('' != strField)
		{
			bRetVal = (strField.length + 1 == strField.split(' ').length);
		}
		return bRetVal;	
	}	
//***--- This function is used to check the e-mail address {@,.} ---***//
	function isEmail(strEmail)
	{
		var bRetVal,strText,i,j;
	
		bRetVal = true ;
		strText = new String(strEmail);
		i = strText.indexOf("@");
		j = strText.indexOf(".");
		if (i==-1 || j==-1)
		{
			bRetVal = false;
		}	
		return bRetVal;
	}
//***--- This function is used to check the URL of a web site {.,.} ---***//
	function isURL(strField)
	{
		var bRetVal,strURL,nPos1,nPos2;
		bRetVal = true;
		strURL  = new String(strField);
		nPos1 = strURL.indexOf(".",0)
		if (nPos1>-1)
		{
			nPos2 = strURL.indexOf(".",nPos1+1)
		}	
		else
		{
			nPos2 = -1;
		}		
		if (strURL=="" || strURL=="http://")
		{
			bRetVal=false;
		}
		if (nPos2 == -1 && bRetVal==true) 
		{
			bRetVal=false
		}
		return bRetVal;			
	}
//***--- This function is used to check the length of any string ---***//
	function isValidLength(strField,nLength)
	{
		var bRetVal;
		bRetVal=true;
		strText = new String(strField);
		if (strText.length<nLength)
		{
			 bRetVal=false;
		}
		return bRetVal;
	}
//***--- This function is used to check the selected value of combo ---***//
	function isValidSelection(nSelVal,nRefVal)
	{
		var bRetVal;
		bRetVal = true;
		if (nSelVal==nRefVal)
		{
			 bRetVal=false;
		}
		if (isNaN(nSelVal)==true)	 
		{
			bRetVal=false;
		}
		return bRetVal;
	}
//***--- This function is used to check if the field is Number ---***//
	function isNumber(strField)
	{
		var bRetVal,bTempVal;
		bRetVal = true;
		bTempVal = isNaN(strField)
		if (bTempVal==true)
		{
			 bRetVal = false;
		}
		return bRetVal;
	}


	function isTime(timeValue)
	{	
		var bRetVal = false;
		var intHour, intMinute, timeSeparator;
		if ( (isValidLength(timeValue, 5)) )
		{
			intHour = timeValue.substring(0, 2);
			timeSeparator = timeValue.substring(2,3);
			intMinute = timeValue.substring(3, 5);
			//alert(" eval = "+(eval(intMinute) <= 60));
			if ( (isNumber(intHour)) && (isNumber(intMinute)) && timeSeparator == ":")
			{
				if ((eval(intHour) <= 24 ) && (eval(intMinute) <= 60) )
					bRetVal = true;
			}	
		}
		return bRetVal;		
	}
  
//***---  This function returns JavaScript Date Object after specifying the given pattern ---***//
//***---  Note that the return pattern is yyyy/mm/dd ---***//
	function getDateObj(dateValue, inPattern)
	{
		if (inPattern == null || inPattern.charAt == 'undefined')
		{
			inPattern = "dmy";
		}
		return new Date(getDateFormat(dateValue, inPattern, 'ymd'));
	}
	
//***---  This function is used to return date(dateValue) in specific pattern(outPattern) after specifing the given format(inPattern) ---***//
//***---  Default is to enter date in format of dd/mm/yyyy & return in format yyyy/mm/dd
	function getDateFormat(dateValue, inPattern, outPattern, bIncludeTime)
	{
		var strDay, strMonth, strYear, dmyDate;
    var strHour, strMinute;
		switch (inPattern)
		{
			case "mdy": case "MDY":
				strMonth = dateValue.substring(0, 2);
				strDay = dateValue.substring(3, 5);
				strYear = dateValue.substring(6, 10);
        strHour = dateValue.substring(11, 13);
				strMinute = dateValue.substring(14, 16);
				//dateValue = strDay+"/"+strMonth+"/"+strYear;
			break;
			case "ymd": case "YMD":
				strYear = dateValue.substring(0, 4);
				strMonth = dateValue.substring(5, 7);
				strDay = dateValue.substring(8, 10);
        strHour = dateValue.substring(11, 13);
				strMinute = dateValue.substring(14, 16);
				//dateValue = strDay+"/"+strMonth+"/"+strYear;
			break;
			case "dmy": case "DMY": default:
				strDay = dateValue.substring(0, 2);
				strMonth = dateValue.substring(3, 5);
				strYear = dateValue.substring(6, 10);
        strHour = dateValue.substring(11, 13);
				strMinute = dateValue.substring(14, 16);
				//dateValue = strDay+"/"+strMonth+"/"+strYear;
			break;
		}
		switch (outPattern)
		{
			case "dmy": case "DMY": 
				dateValue = strDay+"/"+strMonth+"/"+strYear; //+" "+strHour+":"+strMinute;
			break;
			case "mdy": case "MDY":
				dateValue = strMonth+"/"+strDay+"/"+strYear; //+" "+strHour+":"+strMinute;
			break;
			case "ymd": case "YMD": default:
				dateValue = strYear+"/"+strMonth+"/"+strDay; //+" "+strHour+":"+strMinute;
			break;
			if (bIncludeTime)
				dateValue = dateValue +" "+strHour+":"+strMinute;
		}
		//alert(dateValue);
		return dateValue;
	}
	
	function isDateDMY(dmyDate)
	{
	
		var strDay, strMonth, strYear;
		var strDate = dmyDate;
		var pos;
		pos = strDate.indexOf("/");
		strDay = strDate.substr(0,pos);
		strDate = strDate.substr(pos+1,strDate.length);
		pos = strDate.indexOf("/");
		strMonth = strDate.substr(0,pos);
		strDate = strDate.substr(pos+1,strDate.length);
		strYear = strDate;
		/*
		strDay = dmyDate.substring(0, 2);
		strMonth = dmyDate.substring(3, 5);
		strYear = dmyDate.substring(6, 10);
		*/
		return isDate(strDay, strMonth, strYear);
	}
//***---  This function is used to check if the date is correct  ---***//
	function isDate(day,month,year) 
	{
		if (day < 1 || day > 31) 
		{
		    alert("من فضلك : لابد أن يكون اليوم من 1 إلى 31");
		    return false;
		}
		if (month < 1 || month > 12) 
		{ 
		    alert("من فضلك : لابد أن يكون الشهر من 1 إلى 12");
		    return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
		    alert("شهر "+month+" لا يحتوى على 31 يوم !")
		    return false;
		}
		if (month == 2) 
		{ 
		    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		    if (day > 29 || (day==29 && !isleap)) 
		    {
		        alert("فبراير " + year + " لا يحتوى على " + day + " يوم!");
		        return false;
		    }
		}
		if (year < 1) 
		{ 
		    alert("من فضلك : إختر السنة");
		    return false;
		}
		return true; 
	}
//***---  This function is used to check the date interval is correct  ---***//
	function getDateDiff(start,end,interval,rounding) 
	{
	    var iOut = 0;
	    // Create 2 error messages, 1 for each argument. 
	    var startMsg = "من فضلك تأكد من تاريخ البداية والنهاية"
	    var bufferA = Date.parse( start ) ;
	    var bufferB = Date.parse( end ) ;
	    // check that the start parameter is a valid Date. 
	    if ( isNaN (bufferA) || isNaN (bufferB) ) 
	    {
	  	    alert( startMsg ) ;
	        return null ;
	    }
		// check that an interval parameter was not numeric. 
	    if ( interval.charAt == 'undefined' ) 
	    {
	        // the user specified an incorrect interval, handle the error. 
	  //    alert( intervalMsg ) ;
	        return null ;
	    }
	    var number = bufferB-bufferA ;
	    // what kind of add to do? 
	    switch (interval.charAt(0))
	    {
	        case 'd': case 'D': 
	            iOut = parseInt(number / 86400000) ;
	            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
	            break ;
	        case 'h': case 'H':
	            iOut = parseInt(number / 3600000 ) ;
	            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
	            break ;
	        case 'm': case 'M':
	            iOut = parseInt(number / 60000 ) ;
	            if(rounding) iOut += parseInt((number % 60000)/30001) ;
	            break ;
	        case 's': case 'S':
	            iOut = parseInt(number / 1000 ) ;
	            if(rounding) iOut += parseInt((number % 1000)/501) ;
	            break ;
	        default:
			   return null ;
	    }
	    return iOut ;
	}
//***--- Check a certain date is within a certain interval ---***//
	function checkDateBoundry(strdate,start,end) 
	{
	    var nTemp1;
	    var nTemp2;
	    nTemp1 = getDateDiff(start,strdate,'d',true);
	    nTemp2 = getDateDiff(strdate,end,'d',true);
	    if (nTemp1 != null && nTemp2 != null && nTemp1>=0 && nTemp2>=0)
	    {
			return true;
	    }
	    return false;
	}
//***--- add a certain interval to a specified date ---***//

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	return returnDate;
}

