// --- Function to left trim a string ---

	var ERR_DATEFORMAT1 = "The date's format is not like dd/mm/yyyy";
	var ERR_DATEFORMAT2 = "The date's format is not like dd/mm/yyyy";
	var ERR_DATEFORMAT3 = "The day, month or year is not numeric";
	var ERR_DATEFORMAT4 = "The day or the month's length is not 2 (dd or mm)";
	var ERR_DATEFORMAT5 = "The year's length is not 4 (yyyy)";
	var ERR_DATEFORMAT6 = "The day's value is not in the correct month range";
	var ERR_DATEFORMAT7 = "The month's value is not between 1 and 12";
	var ERR_DATEFORMAT8 = "The month is February, is not a leap-year and the day's value is greatter than 28";
	var ERR_DATEFORMAT9 = "The month is February, is a leap-year and the day's value is greatter than 29";
	var ERR_DATEFORMAT10 = "The date value is blank and not BlankPermited";


function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}


// --- Function to right trim a string ---

function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}


// --- Function to trim a string both left and right ---

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);
}


// function to remove single quote 
function removeQuote(sObject) {
	
	if (sObject != null) {
		var checkStr;
		var newString="";
		var ch;
		sObject.value = trim(sObject.value);
		checkStr = sObject.value;
		for (i = 0;  i < checkStr.length;  i++)	{
			ch = checkStr.charAt(i);
			if ( (ch=="'") || (ch=="<") || (ch==">") || (ch=="%") || (ch=="=") || (ch=='"') || 
				 (ch=="`") || (ch=="^") || (ch=="$") || (ch=="#") || (ch=="!") || (ch=='~') || (ch=='&') ||
				 (ch=="_") || (ch=="+") || (ch=="{") || (ch=="}") || (ch=='[') || (ch==']') ||
				 (ch=="#") || (ch=="$") || (ch=="^") || (ch=='%') || 
				 (ch=="|")  || (ch=="?")|| (ch=="*") || (ch=='(') || (ch==')') || (ch=='\\') 
			){
				newString=newString + "";
			}
			else {
				newString=newString+ch;
			}
		}
		if( sObject.value != newString ) {
			sObject.value = newString;
			sObject.focus();
			alert(" Avoid entrying of Special character in any field ");
			return false;
		}	
		sObject.value=newString;
		return true;
	}
}

function checkDecimalNumber(valuetoCheck, numberLength, decimalValue, errorScreen, errorMessage,textField ) {
	isDecimalvalue=false;
	decimalPosition=-1;
	
	decimalPosition = valuetoCheck.indexOf(".");

	if( decimalPosition != -1){
		isDecimalvalue = true;
		if(isNaN(valuetoCheck.substring(0,decimalPosition-1))){
				alert(errorMessage);
				textField.select();
				textField.focus();
				return false;
		}
		if(isNaN(valuetoCheck.substring(decimalPosition+1,valuetoCheck.length))){
				alert(errorMessage);
				textField.select();
				textField.focus();
				return false;
		}
	}
	else{
		if(isNaN(valuetoCheck)){
				alert(errorMessage);
				textField.select();
				textField.focus();
				return false;
		}
	}

		
	if(isDecimalvalue ) {
		if( decimalPosition > numberLength )	{
			alert(errorMessage );
			textField.select();
			textField.focus();
			return false;
		} else {
			if( valuetoCheck.length > ( numberLength + decimalValue + 1 ) )	{
				alert(errorMessage + " \n < for decimal value maximum Length of field is  " + numberLength + " >");
				textField.select();
				textField.focus();
				return false;
			}			
		}		
	} else {
		if( valuetoCheck.length > numberLength )	{
			alert(errorMessage + " \n < for non decimal value maximum Length is " + numberLength + " >");
			textField.select();
			textField.focus();
			return false;
		}			
	}
	return true;
}//Function


// This function evaluate the String for valid date
function checkValidDate( cDate ) {
	/*
		RETURNS
			0 -> The date is correct
			1 | 2 -> The date's format is not like dd/mm/yyyy
			3 -> The day, month or year is not numeric
			4 -> The day or the month's length is not 2 (dd or mm)
			5 -> The year's length is not 4 (yyyy)
			6 -> The day's value is not in the correct month range
			7 -> The month's value is not between 1 and 12
			8 -> The month is February, is not a leap-year and the day's value is greatter than 28
			9 -> The month is February, is a leap-year and the day's value is greatter than 29
			10 -> The date value is blank and not BlankPermited
	*/

	var daysInMonth = new Array(31, 31, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if(trim(cDate.value) == ""){
		return 0;
	}
	if (cDate.value.length !=10 || cDate.value.length > 10) {
		alert(ERR_DATEFORMAT1);
		cDate.select();
		cDate.focus();
		return 1;
	}
	var aDate=cDate.value.split("/");
	if (aDate.length == 1) {
		aDate=cDate.value.split("-");
	}
	if (aDate.length != 3) {
		alert(ERR_DATEFORMAT2);
		cDate.select();
		cDate.focus();
		return 2;
	}
	if (isNaN(aDate[0]) || isNaN(aDate[1]) || isNaN(aDate[2])) {
		alert(ERR_DATEFORMAT3);
		cDate.select();
		cDate.focus();
		return 3;
	}
	if (aDate[0].length !=2 || aDate[1].length !=2){
		alert(ERR_DATEFORMAT4);
		cDate.select();
		cDate.focus();
		return 4;
	}
	if (aDate[2].length != 4){
		alert(ERR_DATEFORMAT5);
		cDate.select();
		cDate.focus();
		return 5;
	}
	if (aDate[0] < 1 || aDate[0] > daysInMonth[parseInt(aDate[1])-1]){
		alert(ERR_DATEFORMAT6);
		cDate.select();
		cDate.focus();
		return 6;
	}
	if (aDate[1] < 1 || aDate[1] > 12){
		alert(ERR_DATEFORMAT7);
		cDate.select();
		cDate.focus();
		return 7;
	}
	var cLY=checkLeapYear(aDate[0],aDate[1],aDate[2]);
	if (cLY > 0){
		eval('alert(ERR_DATEFORMAT'+(7+cLY)+')');
		cDate.select();
		cDate.focus();
		return 7+cLY;
	}
	return 0;

}


// --- Function to check if the date is correct for a leap-year ---

function checkLeapYear(iDay,iMonth,iYear) {
	/* 
		RETURNS
			0 -> The day is correct for both leap-year and not leap-year
			1 -> Not leap-year but the day for February is greatter than 28
			2 -> Leap-year and the day for February is greatter than 29
	*/

	if (iYear==2000 || (iYear % 4 ==0 && iYear % 100 != 0)) {	// Leap-year
		if (iMonth==2 && iDay <= 29) return 0;
		else if (iMonth==2 && iDay > 29) return 2;
	} else {													// Not leap-year
		if (iMonth==2 && iDay <=28) return 0;
		else if (iMonth==2 && iDay > 28) return 1;
	}
}

// --- Function to compare two date fields ---
//	   dateFrom < dateTo
//	   dateTo < today date
//	   period between dateFrom and dateTo less than the difference parameter

function compareDates(dateFrom,dateTo,difference) {
	/*
		RETURNS
			0 -> The dates are correct & equals
			1 -> The from date is greatter than the date to
			2 -> The date to is greatter than the date of today
			3 -> The period between the date from and the date to is greatter than 'difference'
	*/
	
	var aDate=dateFrom.value.split("/");
	dateFrom=new Date(aDate[2],aDate[1]-1,aDate[0]);
	
	aDate=dateTo.value.split("/");
	dateTo=new Date(aDate[2],aDate[1]-1,aDate[0]);
	
	var today=new Date();
	
	if (dateFrom > dateTo) 
		return 1;
	//if (dateTo > today) 
	//	return 2;
	
	if (difference !="undefined" && difference !=null || difference) {
		var difDate=new Date();
		difDate.setTime(Math.abs(dateTo.getTime() - dateFrom.getTime()));
		var numDaysDif=Math.floor(difDate/(24*60*60*1000));
		if (numDaysDif > difference) return 3;
	}	
	return 0;
}


// This function evaluate the String for valid date
function checkValidDateArabic( cDate ) {
	/*
		RETURNS
			0 -> The date is correct
			1 | 2 -> The date's format is not like dd/mm/yyyy
			3 -> The day, month or year is not numeric
			4 -> The day or the month's length is not 2 (dd or mm)
			5 -> The year's length is not 4 (yyyy)
			6 -> The day's value is not in the correct month range
			7 -> The month's value is not between 1 and 12
			8 -> The month is February, is not a leap-year and the day's value is greatter than 28
			9 -> The month is February, is a leap-year and the day's value is greatter than 29
			10 -> The date value is blank and not BlankPermited
	*/

	var daysInMonth = new Array(31, 31, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if(trim(cDate.value) == ""){
		return 0;
	}
	if (cDate.value.length !=10 || cDate.value.length > 10) {
		alert(ERR_DATEFORMAT1);
		cDate.select();
		cDate.focus();
		return 1;
	}
	var aDate=cDate.value.split("/");

	if (aDate.length == 1) {
		aDate=cDate.value.split("-");
	}
	if (aDate.length != 3) {
		alert(ERR_DATEFORMAT2);
		cDate.select();
		cDate.focus();
		return 2;
	}
	
	if (aDate[0].length !=2 || aDate[1].length !=2){
		alert(ERR_DATEFORMAT4);
		cDate.select();
		cDate.focus();
		return 4;
	}
	if (aDate[2].length != 4){
		alert(ERR_DATEFORMAT5);
		cDate.select();
		cDate.focus();
		return 5;
	}
	if (aDate[0] < 1 || aDate[0] > daysInMonth[parseInt(aDate[1])-1]){
		alert(ERR_DATEFORMAT6);
		cDate.select();
		cDate.focus();
		return 6;
	}
	if (aDate[1] < 1 || aDate[1] > 12){
		alert(ERR_DATEFORMAT7);
		cDate.select();
		cDate.focus();
		return 7;
	}
	var cLY=checkLeapYear(aDate[0],aDate[1],aDate[2]);
	if (cLY > 0){
		eval('alert(ERR_DATEFORMAT'+(7+cLY)+')');
		cDate.select();
		cDate.focus();
		return 7+cLY;
	}
	return 0;

}

