function CommonFunctions(){
  this.id = this;
  this.formatCurrency = formatCurrency;
    
     }
     
     function formatCurrency(strFieldName) 
     { 
     
     num = document.getElementById(strFieldName).value;
         num = num.toString().replace(/\$|\,/g,'');
         if (isNaN(num))
         num = '0';
         sign = (num == (num = Math.abs(num)));
         num = Math.floor(num*100+0.50000000001);
         cents = num%100;
         num = Math.floor(num/100).toString();
         if (cents < 10)
         cents = '0' + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
         document.getElementById(strFieldName).value = (((sign)?'':'-') + num + '.' + cents);
         
     }

	/******************************************************************************
	DESCRIPTION: Validates that a string contains only valid numbers.
	PARAMETERS:
	strValue - String to be tested for validity
	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function  validateNumeric( strValue ) 
	{
		var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

		//check for numeric characters
		return objRegExp.test(strValue);
	}
	
	/******************************************************************************
	DESCRIPTION: Validates that a string contains only valid positive numeric values.
	PARAMETERS:
	strValue - String to be tested for validity
	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function  validatePositiveNumeric( strValue ) 
	{
		var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;

		//check for numeric characters
		return objRegExp.test(strValue);
	}



	/******************************************************************************
	DESCRIPTION: Validates that sring contains valid format for US currency.
	PARAMETERS:
	strValue - String to be tested for validity
	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function  validateCurrency( strValue ) 
	{
						  
		var objRegExp  =/^([\d]{1,3}([\,]{1}[\d]{3})*)*([\.]{1}[\d]{1,2}){0,1}$/ ;
		//var objRegExp = /^[\d]{1,3}([\,]{1}[\d]{3})*[\.]{1}[\d]{2}$/ ;
		//check for valid currency
		return objRegExp.test(strValue);
	}



	/************************************************
	DESCRIPTION: Validates that a string contains only
		valid integer number.

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function validateInteger( strValue ) 
	{
		var objRegExp  = /(^-?\d\d*$)/;

		//check for integer characters
		return objRegExp.test(strValue);
	}
	
	/************************************************
	DESCRIPTION: Validates that a string contains only
		valid positive integer number.

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function validatePositiveInteger( strValue ) 
	{
		var objRegExp  = /(^\d\d*$)/;

		//check for integer characters
		return objRegExp.test(strValue);
	}

	/************************************************
	DESCRIPTION: Validates that a string contains a
	valid email pattern.

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.

	REMARKS: Accounts for email with country appended
	does not validate that email contains valid URL
	type (.com, .gov, etc.) or valid country suffix.
	*************************************************/
	function validateEmail( strValue) 
	{
		var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;

		//check for valid email
		return objRegExp.test(strValue);
	}
	
	/************************************************
	DESCRIPTION: Validates that string starts 
	with alphabet or number,
	and then any alphabet or number or space 

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.
	
	*************************************************/	
	function validateAlphaNumeric(strValue) 
	{
				var objRegExp  = /^[a-z,A-Z,\d]{1}[a-z,A-Z,\d, ]*$/;				

				//check for valid AlphaNumeric
				return objRegExp.test(strValue);
	}



	var whitespace = " \t\n\r";
	// Removes all whitespace characters from s.
	// Global variable whitespace (see above)
	// defines which characters are considered whitespace.
	function stripWhitespace (s)
	{   
		return stripCharsInBag (s, whitespace)
	}

	function stripCharsInBag (s, bag)
	{   
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}


	function CheckValid(UserDate,UserDateFormat)
	{
		var intTemp;
		var blnMonthInLetters = false;
		var blnLeapYear = true;
		var varDay,varMonth,varYear;
		var strTemp = UserDate;
		//strTemp = stripWhitespace(strTemp);
		UserDateFormat = stripWhitespace(UserDateFormat);
		var strTemp1 = strTemp;
		var strUserDateFormat = UserDateFormat.toUpperCase( );
		if (UserDateFormat.length == 0)
		{
			return false;
		}
		if (UserDate.length == 0)
		{
			return true;
		}
		if (strUserDateFormat.substr(0, 2) == "DD")
		{
			//alert("User Has Choosend Us British format");
			/*intTemp = strTemp.indexOf("/")
			if (intTemp == -1)
			{*/
			intTemp = strTemp.indexOf("-")
			if (intTemp == -1)
				return false
			else
			{
				strPreviousSeperator = "-"
				/*}
				else
				{
					strPreviousSeperator = "/"
				}
				if (intTemp == -1)
				{
					// Invalid Seperator character entered return false
					return false
				}
				else
				{*/
				strDay = strTemp.substr(0, intTemp)
				strTemp1 = strTemp.substr(intTemp + 1, strTemp.length - intTemp - 1)
				intTemp = strTemp1.indexOf(strPreviousSeperator)
				if (intTemp == -1)
				{
					// Invalid Seperator character entered return false
					return false
				}
				else
				{
					strMonth = strTemp1.substr(0, intTemp)
					strTemp1 = strTemp1.substr(intTemp + 1, strTemp1.length - intTemp - 1)
					strYear = strTemp1
				}
			}
		}
		else if ((strUserDateFormat.substr(0, 2) == "MM") || (strUserDateFormat.substr(0, 3) == "MMM") || (strUserDateFormat.substr(0, 4) == "MMMM"))
		{
			//alert("User Has Choosend Us English format");
			/*intTemp = strTemp.indexOf("/")
			if (intTemp == -1)
			{*/
			intTemp = strTemp.indexOf("-")
			if (intTemp == -1)
				return false
			else
			{
				strPreviousSeperator = "-"
				/*}
				else
				{
					strPreviousSeperator = "/"
				}
				if (intTemp == -1)
				{
					// Invalid Seperator character entered return false
					return false
				}
				else
				{*/
				strMonth = strTemp.substr(0, intTemp);
				strTemp1 = strTemp.substr(intTemp + 1, strTemp.length - intTemp - 1)
				intTemp = strTemp1.indexOf(strPreviousSeperator);
				if (intTemp == -1)
				{
					// Invalid Seperator character entered return false
					return false;
				}
				else
				{
					strDay = strTemp1.substr(0, intTemp);
					strTemp1 = strTemp1.substr(intTemp + 1, strTemp1.length - intTemp - 1)
					strYear = strTemp1;
				}
			}
		}
		else
		{
			//Invalid date format
			return false;
		}
		
		intTemp = strYear.indexOf(strPreviousSeperator)
		if (intTemp >= 0) 
		{
			//Some characters are there in year
			return false;
        }
       // if (strYear.length != 2 && strYear.length != 4)
        if ( strYear.length != 4)
        {
			return false;
        }
        if (strDay.length != 1 && strDay.length != 2)
        {
			return false;
        }
        if ( strMonth.length != 3)
        {
			return false;
        }
		if (strDay.length > 0 && strMonth.length > 0 && strYear.length > 0)
		{
			for(i=0;i<strDay.length;i++)
			{
				if (isLetter(strDay.charAt(i))) 
				{
					return false;
				}
			}
			for(i=0;i<strYear.length;i++)
			{
				if (isLetter(strYear.charAt(i))) 
				{
					return false;
				}
			}
			intDay = parseInt(strDay, 10)
			if (intDay > 31 || intDay<=0)
			{
				// Invalid Day
				return false;
			}
			for(i=0;i<strTemp.length;i++)
			{
				if (isLetter(strTemp.charAt(i))) 
				{
					intTemp = 0
					break;
				}
				else
				{
					intTemp = -1
				}
			}
			if (intTemp == 0)
			{
				//User Entered characters in date
				strMonth = strMonth.toUpperCase( )
				if ((strMonth == "JANUARY") || (strMonth == "JAN"))
				{
					intMonth = 1;
				}
				else if ((strMonth == "FEBRUARY") || (strMonth == "FEB"))
				{
					intMonth = 2;
				}
				else if ((strMonth == "MARCH") || (strMonth == "MAR"))
				{
					intMonth = 3;
				}
				else if ((strMonth == "APRIL") || (strMonth == "APR"))
				{
					intMonth = 4;
				}
				else if ((strMonth == "MAY"))
				{
					intMonth = 5;
				}
				else if ((strMonth == "JUNE") || (strMonth == "JUN"))
				{
					intMonth = 6;
				}
				else if ((strMonth == "JULY") || (strMonth == "JUL"))
				{
					intMonth = 7;
				}
				else if ((strMonth == "AUGUST") || (strMonth == "AUG"))
				{
					intMonth = 8;
				}
				else if ((strMonth == "SEPTEMBER") || (strMonth == "SEP"))
				{
					intMonth = 9;
				}
				else if ((strMonth == "OCTOBER") || (strMonth == "OCT"))
				{
					intMonth = 10;
				}
				else if ((strMonth == "NOVEMBER") || (strMonth == "NOV"))
				{
					intMonth = 11;
				}
				else if ((strMonth == "DECEMBER") || (strMonth == "DEC"))
				{
					intMonth = 12;
				}
				else
				{
					return false;
				}
			}
			else
			{
				intMonth = parseInt(strMonth, 10)
				if (intMonth > 12 || intMonth <= 0)
				{
					//Invalid Month
					return false;
				}
				for(i=0;i<strMonth.length;i++)
				{
					if (! isDigit(strMonth.charAt(i))) 
					{
						// User entered some invalid characters
						return false;
					}
				}
			}
			intYear = parseInt(strYear, 10)
			if (strYear.length == 4 && intYear <= 0 )
			{
				return false;
			}
			if (strYear.length == 4 && intYear < 1753)
			{
				return false;
			}
			intTemp = daysInFebruary(intYear)
			if ((intMonth == 2) && (intDay > intTemp))
			{
				// More days entered for february
				return false;
	        }
	        if ((intMonth == 4) || (intMonth == 6) || (intMonth == 9) || (intMonth == 11))
	        {
				if (intDay > 30)
				{
					// Days should not be greater than 30
					return false;
				}
			}
			else
			{
				if (intDay > 31)
				{
					// Days should not be greater than 31
					return false;
				}
			}
		}
		else
		{
			return false;
		}
		return true;
	}

	function isDigit (c)
	{   
		return ((c >= "0") && (c <= "9"))
	}

	function isLetter (c)
	{   
		return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
	}

	function daysInFebruary(year)
	{
		if ((year % 4 == 0) && ((! (year % 100 == 0)) || (year % 400 == 0)))
		{
			return 29
		}
		else
		{
	        return 28
		}
	}
	
	
	/************************************************
	DESCRIPTION: Removes leading and trailing spaces.
	PARAMETERS: Source string from which spaces will
	be removed;
	RETURNS: Source string with whitespaces removed.
	*************************************************/
	function trimAll( strValue ) {
		var objRegExp = /^(\s*)$/;

		//check for all spaces
		if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '');
		if( strValue.length == 0)
			return strValue;
		}

	//check for leading &amp; trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) {
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
		}
	return strValue;
	}

/************************************************
	DESCRIPTION: validates that string containt valid date format(dd-MMM-yy)

	PARAMETERS:
	strValue - String to be validated

	RETURNS:
	true or false
	*************************************************/
	function validateGivenDate(strValue)
	{
		var objRegExp = /^\d{1,2}(\-){1}\w{3}(\-){1}\d{2}$/
		
		//check to see if in correct format
		if(!objRegExp.test(strValue))
			return false; //doesn't match pattern, bad date
		else
		{
			var arrayDate = strValue.split('-'); //split date into month, day, year
			//create a lookup for months not equal to Feb.
			var arrayLookup = { 'jan' : 31,'mar' : 31, 'apr' : 30,'may' : 31,'jun' : 30,'jul' : 31,
								'aug' : 31,'sep' : 30,'oct' : 31,'nov' : 30,'dec' : 31}
			
			var intDay = parseInt(arrayDate[0],10);		
			
			//check if month value and day value agree
			if(arrayLookup[arrayDate[1].toLowerCase()] != null) 
			{
				if(intDay <= arrayLookup[arrayDate[1].toLowerCase()] && intDay != 0)
				{				
					return true; //found in lookup table, good date
				}
			}

			//check for February
			var intYear = parseInt(arrayDate[2]);
			if (arrayDate[1].toLowerCase() == 'feb')
			{
				if(((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
					return true; //Feb. had valid number of days
			}
		}
		return false; //any other values, bad date
	}
	// if  year input is after this year considered 20 century.
	var NUMYEAR = 30;		
	/************************************************
	DESCRIPTION: validates that string passed is in dd-mmm-yy format 
			and returns in mm/dd/yy format
	PARAMETERS:
	str_date - date to be formated
	RETURNS:returns Date from  dd-MMM-yy  to mm/dd/yy format	
	*************************************************/			
	function getFormatedDate(str_date)
	{
		var arrMonth = new String("JanFebMarAprMayJunJulAugSepOctNovDec") ;
		var arrDate = str_date.split("-") ;								
		if (arrDate.length != 3)
		{
			return "";				
		}				
		var intIndex = arrMonth.toLowerCase().indexOf(arrDate[1].toLowerCase())
		if (intIndex > -1)
		{
			if (arrDate[2] < 100) arrDate[2] = Number(arrDate[2]) + (arrDate[2] < NUMYEAR ? 2000 : 1900);
			str_date = (intIndex + 3)/3 + "/" + (parseInt(arrDate[0],10) < 10 ? '0' : '') + parseInt(arrDate[0],10) + "/" + arrDate[2] ;
			return str_date;
			
		}
		return "";
	}
	
	/************************************************
	DESCRIPTION: Validates that a string contains only
		valid ten positive integer number.

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.
	******************************************************************************/
	function validatePhoneFax(strValue) 
	{
		//var objRegExp  = /^(\d){10}$/;		
		//var objRegExp  = /^([\(]([\d]*),[\)]){0,1}[\-,\d, ]*$/;				
		//var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
		var objRegExp  = /^[\(,\),\-,\d, ]*$/;				

		//check for integer characters
		return objRegExp.test(strValue);
	}
	/************************************************
	DESCRIPTION: Compares two dates passess in in dd-mmm-yy format 
			and returns false if dateone is earlier than date two
	PARAMETERS:	DateOne,DateTwo
	RETURNS:returns true or false
	*************************************************/			
	//parameters dates are in dd-MMM-yy	
	function CompareDates(DateOne,DateTwo)
	{				
		var strStart =getFormatedDate(DateOne);
		var strEnd =getFormatedDate(DateTwo);								
		if(strStart.length>0 && strEnd.length>0)
		{
			var dtStart=new Date(strStart);
			var dtEnd=new Date(strEnd);
			var diff =dtEnd-dtStart;
			if(diff<0)//startdate is less than end date
			{
				return false;
			}
		}
		return true;			
	}

