
/*
####################################     
# File Name: functions.js
# Created By: Umesh Mundhe
# Created On: November 18 ,2005
# Updated By: Amol Divalkar
# This file contain all the common javascript functions
####################################
*/
	/*
     #############################################################################
     # Function Name: isBlank()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Function to check wether given value is blank or not.    
     # Parameters: string strValue : string. 
     # ON SUCCESS: Returns TRUE if value is blank.     
     # ON FAILURE: Returns FLASE  if value is not blank.     
     #############################################################################
	 */

	 function isBlank(strValue)
	 {
		 for(var i = 0; i < strValue.length; i++)
		 {
			 var c = strValue.charAt(i);
			 if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		 }
		 return true; 
	 }

	/*
     #############################################################################
     # Function Name: isValidEmail()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Validate an email address.
     # Parameters: string strEmail : Email address
     # ON SUCCESS: Returns TRUE if string is in valid email format.     
     # ON FAILURE: Returns FLASE if string is not in valid email format.
     #############################################################################
	 */
	 function isValidEmail(strEmail) 
	 {
		 // assume an email address cannot start with an @ or white space, but it 
		 // must contain the @ character followed by groups of alphanumerics and '-' 
		 // followed by the dot character '.' 
		 // It must end with 2 or 3 alphanumerics. 
		 // 
		 var alnum="a-zA-Z0-9"; 
		 exp="^[^@\\s]+@(["+alnum+"+\\-]+\\.)+["+alnum+"]{2,6}$"; 
		 emailregexp = new RegExp(exp); 
		 result = strEmail.match(emailregexp); 
		 if (result != null) 
		 {
			 return true; 
		 }
		 else 
		 {
			 return false; 
		 }
	 }
	 function jsEmailValidation(entered)
	 {
			var regex = /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-zA-Z]{2,7}$/ ; 	
			if(!regex.test(entered)) 
			{
				return false;
			}
			return true;
		
	  }


	/*
     #############################################################################
     # Function Name: isValidString()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if field value contains only alphanumeric and '_' charactes. Also checks  that alphabetical chars. and '_' must have to be come first and followed by numbers. It returns false if above conditions will not satisfy otherwise true.
     # Parameters: string strVal : string to be validated
     # ON SUCCESS: Returns TRUE if string is in valid format.  
     # ON FAILURE: Returns FLASE if string is not in valid format.  
     #############################################################################
	 */
	 function isValidString(strVal) 
	 {
		 //var pattern = "^[a-zA-Z]+[a-zA-Z0-9_\s\.]*";
		 var regex = /^[a-zA-Z0-9_\s-'.,&amp;#@:?!()$\/]+$/;
         
		 //var regex = new RegExp(pattern);
		
		 if(regex.test(strVal)) 
		 {
			 return true; 
		 }
		 else
		 {
			 return false; 
		 }
	 }
	 /*
     #############################################################################
     # Function Name: fnIsValidUsername()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if field value contains valid username. 
     #          Username allows letters , digits and '_'
     # Parameters: string strVal : string to be validated
     # ON SUCCESS: Returns TRUE if string is in valid format.  
     # ON FAILURE: Returns FLASE if string is not in valid format.  
     #############################################################################
	 */
	 function fnIsValidUsername(strVal) 
	 {
		 var regex = /^[a-zA-Z0-9_]+$/;
         
		 //var regex = new RegExp(pattern);
		
		 if(regex.test(strVal)) 
		 {
			 return true; 
		 }
		 else
		 {
			 return false; 
		 }
	 }
    /*
     #############################################################################
     # Function Name: isString(strVal)
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if field value contains only  characters. 
     # Parameters: string strVal : string to be validated
     # ON SUCCESS: Returns TRUE if string is in valid format.  
     # ON FAILURE: Returns FALSE if string is not in valid format.  
     #############################################################################
	 */
	 function isString(strVal) 
	 {
		 var regex = /([a-zA-Z\.])+([\s])*$/;     
		 if(regex.test(strVal)) 
		 {
			//alert('true'); 
			return true; 
		 }
		 else
		 {
			//alert('false');
			return false; 
		 }
	 }


	/*
     #############################################################################
     # Function Name: IsAlphaNumeric()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if field value contains only alphanumeric charecters.
     # Parameters: string strVal : string to be validated
     # ON SUCCESS: Returns TRUE if string is valid.     
     # ON FAILURE: Returns FLASE if string is not valid.     
     #############################################################################
	 */
	 function IsAlphaNumeric(strVal) 
	 {
		 var regex = /^[a-zA-Z0-9\-]*$/; 
		 if(!regex.test(strVal)) 
		 {
			 return false; 
		 }
		 return true; 
	 }
	 /*
     #############################################################################
     # Function Name: isNumeric()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if field value contains only numeric positive value.
     # It returns false if above conditions will not satisfy otherwise true.
     # Parameters: int val : value to be validated
     # ON SUCCESS: Returns TRUE if value is in valid format.  
     # ON FAILURE: Returns FLASE if value is not in valid format.  
     #############################################################################
	 */
	 function isNumeric(intVal) 
	 {
		 var regex = /^[0-9]+$/; 
		 if(!regex.test(intVal) || intVal < 0) 
		 {
		 	return false; 
		 }
		 else
		 {
			 return true; 
		 }
	 }
	  /*
     #############################################################################
     # Function Name: isValidPhone()
     # Created By: PHP Team   
     # Created on: 27 June 2006
     # Purpose: Checks if field value contains valid US format phone number (222 222 4444 or 222-333-4444).
     #          US phone number with or without dashes   
     # It returns false if above conditions will not satisfy otherwise true.
     # Parameters: int val : value to be validated
     # ON SUCCESS: Returns TRUE if value is in valid format.  
     # ON FAILURE: Returns FALSE if value is not in valid format.  
     #############################################################################
	 */
	 function isValidPhone(intVal) 
	 {
		 //var regex = /^\D?(\(\d{3}\))\D?\D?(\d{3})\D?(\d{4})$/; 
		// var regex = '/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
		 var regex = /^[+0-9\s\(\)]*$/; 
		 if(!regex.test(intVal) || intVal < 0) 
		 {
		 	return false; 
		 }
		 else
		 {
			 return true; 
		 }
	 }
	  function isValidCCCode(intVal) 
	 {
		 //var regex = /^\D?(\(\d{3}\))\D?\D?(\d{3})\D?(\d{4})$/; 
		// var regex = '/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
		 var regex = /^[0-9\+]*$/; 
		 if(!regex.test(intVal) || intVal < 0) 
		 {
		 	return false; 
		 }
		 else
		 {
			 return true; 
		 }
	 }
	  /*
     #############################################################################
     # Function Name: isValidZip()
     # Created By: PHP Team   
     # Created on: 27 June 2006
     # Purpose: Checks if field value contains valid US format Zip code (22256 or 66788-9879).
     #          US zip code with optional dash-four    
     # It returns false if above conditions will not satisfy otherwise true.
     # Parameters: int val : value to be validated
     # ON SUCCESS: Returns TRUE if value is in valid format.  
     # ON FAILURE: Returns FALSE if value is not in valid format.  
     #############################################################################
	 */
	 function isValidZip(intVal)
	 {
	 	//var regex = /^\d{5}(-\d{4})?$/;
	 	var regex = /^[a-zA-Z]*[0-9]+$/;
	 	if(!regex.test(intVal) || intVal < 0) 
		 {
		 	return false; 
		 }
		 else
		 {
			 return true; 
		 }
	 }
	 /*
     #############################################################################
     # Function Name: isValidURL()
     # Created By: PHP Team   
     # Created on: 27 June 2006
     # Purpose: Checks if field value contains valid URL or Web address.
     #        
     # 
     # Parameters: int val : value to be validated
     # ON SUCCESS: Returns TRUE if value is in valid format.  
     # ON FAILURE: Returns FALSE if value is not in valid format.  
     #############################################################################
	 */ 
	 function isValidURL(val)
	 {
	 	var regex = /^(http[s]?)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$/;
	 	if(!regex.test(val)) 
		 {
		 	return false; 
		 }
		 else
		 {
			 return true; 
		 }
	 }


	/*
     #############################################################################
     # Function Name: checkFileType ()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: It checks the file types.
     # Parameters: obj fld : object of file inout type 
	 #					   string strTypes : valid file types seperated by " "
     # ON SUCCESS: Returns TRUE if object contain valid file type.
     # ON FAILURE: Returns FLASE if object contain invalid file type.
     #############################################################################
	 */
	 function checkFileType(fld, strTypes) 
	 {
		 var strTypeList="";
		 strArray = strTypes.split(" "); 
		 for (i = 0;i < strArray.length;i++) 
		 {
			 if ( i < strArray.length && i > 0) 
			 {
				 strTypeList += "|";
			 }
                strTypeList += "."+strArray[i];
		 }
		 exp="("+strTypeList+")$"; 
		 var regex = new RegExp(exp);
		 if(!regex.test(fld.value)) 
		 {
			 return false; 
		 }
         return true; 
	 }

	/*
     #############################################################################
     # Function Name: isFileSize()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: It ckecks the size of the image file. You can check either in terms of width and height of image or size of image in bytes.
     # Parameters: obj fld : object of file input type for which we want to check size
	 #					   int size : Max file size allowed
     # ON SUCCESS: Returns TRUE file size is less than or equal to max size allowed.
     # ON FAILURE: Returns FLASE  file size is greater than max size allowed.
     #############################################################################
	 */

	 /* ********** Commented By Jayashree ************************
	 function isFileSize(fld, size) 
	 {
	     alert(fld.value);
		 var img = new Image(); 
		 img.src = fld.value; 
		 //alert(img.fileSize);
		 // var Dimensions = img.width + 'x' + img.height; 
		 // var File Size = img.fileSize; 
		alert(img.fileSize);
		alert(size);
		 if(img.fileSize > size) 
		 {
			 return false; 
		 }
		 else
		 {
		      return true; 
	     }
	 }
	*************************************************************/

	/*
     #############################################################################
     # Function Name: LTrim ()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Remove leading spaces.
     # Parameters: string str : String Value
     # ON SUCCESS: Remove leading spaces and return the string.     
     #############################################################################
	 */
	 function LTrim(str)
	 {
		 if(str==null)
		{
			return str;
		}
		for(var i=0;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i++);
		return str.substring(i,str.length);
	 }

	/*
     #############################################################################
     # Function Name: RTrim ()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Remove trailing spaces.
     # Parameters: string str : String Value
     # ON SUCCESS: Remove trailing spaces and return the string.      
     #############################################################################
	 */

	 function RTrim(str)
	 {
		
	 	if(str==null)
		{
			return str;
		}
		for(var i=str.length-1;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i--);
		return str.substring(0,i+1);
	 }

	/*
     #############################################################################
     # Function Name: Trim ()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Remove leading and trailing spaces.
     # Parameters: string str : String Value
     # ON SUCCESS: Remove leading and trailing spaces and return the string.     
     #############################################################################
	 */

	 function Trim(str)
	 {
		 return LTrim(RTrim(str));
	 }


	/*
     #############################################################################
     # Function Name: stripHTMLTags ()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: A simple routine to strip HTML tags from supplied string It's not very clever but it's quite useful. Everything between each "<" and the subsequent ">" is ignored hence it could get confused with any javascript comparisons or comments that contain comparisons.
     # Parameters: string str : String Value
     # ON SUCCESS: Removes html tags from string and return string.     
     #############################################################################
	 */

	 function stripHTMLTags(str) 
	 {
		 var mystr=""; 
		 var chr=""; 
		 var skip=false; 
		 var skipcancel=false; 
		 for (x=0; x<str.length; x++) 
		 {
			 if (skipcancel==true)
			 {
				 skip=false;
			 } 
			 chr=str.charAt(x); 
			 if (chr=="<")
			 {
				 skip=true;skipcancel=false;
			 } 
			 else if (chr==">" && skip==true)
			 {
				 skipcancel=true;
			 } 
			 if (skip==false) 
			 {
				 mystr=mystr+chr; 
			 }
		 }
		 return mystr; 
	 } 

	/*
     #############################################################################
     # Function Name: isValidTime()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if time is in HH:MM:SS format. The seconds are optional.
     # Parameters: string strTime : Time 
     # ON SUCCESS: Returns TRUE if strTime contains time in HH:MM:SS format.
     # ON FAILURE: Returns FLASE  if strTime does not contains time in HH:MM:SS format.
     #############################################################################
	 */

	 function isValidTime(strTime) 
	 {
		 var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?$/; 
		 var matchArray = strTime.match(timePat); 
		 if (matchArray == null) 
		 {
			 return false; 
		 }

		 hour = matchArray[1]; 
		 minute = matchArray[2]; 
		 second = matchArray[4]; 

		 if (isBlank(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 Name: isValidNumber()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if the passed string contain valid number or not. 
     # Parameters: string numval : Number to check 
     # ON SUCCESS: Returns TRUE if value is numeric.     
     # ON FAILURE: Returns FLASE  if value is not numeric.
     #############################################################################
	 */

	 function isValidNumber(numval) 
	 {
		 if (isBlank(numval))
		 {
			 return false;
		 }
		 var myRegExp = new RegExp("^[/+|/-]?[0-9]*[/.]?[0-9]*$"); 
		 return myRegExp.test(numval); 
	 }
	 function isValidFloat(numval) 
	 {
		 if (isBlank(numval))
		 {
			 return false;
		 }
		 var myRegExp = new RegExp("^[0-9]*[/.]?[0-9]*$"); 
		 return myRegExp.test(numval); 
	 }

	/*
     #############################################################################
     # Function Name: isValidInterval()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Check we ther the passed value is valid interval or not ( i.e. 1 Year, 1 day, 20 days etc).
     # Parameters: string interval : Value
     # ON SUCCESS: Returns TRUE parameter contains valid interval.   
     # ON FAILURE: Returns FLASE  parameter is not a valid interval.
     #############################################################################
	 */

	 function isValidInterval(interval) 
	 {
		 var strIntervals = new Array("yrs","year","years","mos","month","months","day","days","week","weeks","hrs","hour","hours","mins","min","minutes","secs","sec","second","seconds"); 
		 strArray = interval.split(" "); 
		 
		 // need at least two items 
		 if (strArray.length < 2) 
		 {
			 return false;
		 }
		 
		 // check all pairs of values to be valid intervals (e.g 2 hrs 5 mins) 
		 for (i = 0;i<strArray.length-1;i=i+2) 
		 {
			 if (isNaN(strArray[i]))
			 {
				 return false;
			 }
			 found=false; 
			 for (var x = 0;x<strIntervals.length;x++) 
			 {
				 if (strArray[i+1].toUpperCase() == strIntervals[x].toUpperCase()) 
				 {
					 found=true;
				 }
			 }
			 if (!found)
			 {
				 return false;
			 }
		 }
		 return true; 
	 }


	/*
     #############################################################################
     # Function Name: isValidDate()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Checks if date is in MM-DD-YYYY format  format. 
     # Parameters: string d : Date
	 #                    
     # ON SUCCESS: Returns TRUE if value is blank.     
     # ON FAILURE: Returns FLASE  if value is not blank.     
     #############################################################################
	 */

	 function isValidDate(d) 
	 {
		 var strDatestyle = "US"; //United States date style 
		 //var strDatestyle = "EU";  //European date style 
		 var strDate; 
		 var strDateArray; 
		 var strDay; 
		 var strMonth; 
		 var strYear; 
		 var intDay; 
		 var intMonth; 
		 var intYear; 
		 var booFound = false; 
		 var strSeparatorArray = new Array("-"," ","/","."); 
		 var intElementNr; 
		 var err = 0; 
		 var strMonthArray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
		 strDate = d; 
		 if (strDate.length < 1) 
		 {
			 return false; 
		 }
		 
		 if (strDate.toLowerCase()=="today" || strDate.toLowerCase()=="now")
		 {
			 return true;
		 }
		 
		 for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
		 {
			 if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
			 {
				 strDateArray = strDate.split(strSeparatorArray[intElementNr]); 
				 if (strDateArray.length != 3) 
				 {
					 err = 1; 
					 return false; 
				 }
				 else 
				 {
					 strDay = strDateArray[0]; 
					 strMonth = strDateArray[1]; 
					 strYear = strDateArray[2]; 
				 }
				 booFound = true; 
			 }
		 }

		 if (booFound == false) 
		 {
			 if (strDate.length>5) 
			 {
				 strDay = strDate.substr(0, 2); 
				 strMonth = strDate.substr(2, 2); 
				 strYear = strDate.substr(4); 
			 }
			 else 
			 {
				 return false; 
			 }
		 }

		 // verify year part   2 or 4 digits 
		 if (strYear.length != 2 && strYear.length != 4) 
		 {
			 return false;
		 }

		 if (isNaN(strYear))
		 {
			 return false;
		 }
		 
		 // US style (swap month and day) 
		 if (strDatestyle == "US") 
		 {
			 strTemp = strDay; 
			 strDay = strMonth; 
			 strMonth = strTemp; 
		 }

		 // verify 1 or 2 digit integer day 
		 if (strDay.length<1 || strDay.length>2) 
		 {
			 return false;
		 }
		 
		 if (isNaN(strDay))
		 {
			 return false;
		 }
		 
		 // month may be digits of characters, hence following check 
		 intMonth = parseInt(strMonth, 10); 
		 if (isNaN(intMonth)) 
		 {
			 for (i = 0;i<12;i++) 
			 {
				 if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
				 {
					 intMonth = i+1; 
					 strMonth = strMonthArray[i]; 
					 i = 12; 
				 }
			 }
			 if (isNaN(intMonth)) 
			 {
				 err = 3; 
				 return false; 
			 }
		 }

		 intDay=parseInt(strDay,10); 
		 intYear = parseInt(strYear, 10); 

		 if (intMonth>12 || intMonth<1) 
		 {
			 err = 5; 
			 return false; 
		 }

		 // day in month check 
		 if (intDay < 1 || intDay > 31)
		 {
			 return false;
		 }
		 
		 if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) 
		 {
			 return false; 
		 }

		 if (intMonth == 2) 
		 {
			 if (LeapYear(intYear)) 
			 {
				 if (intDay > 29) 
				 {
					 return false;
				 }
			 }
			 else 
			 {
				 if (intDay > 28) 
				 {
					 return false;
				 }
			 }
		 }

		 if (intYear<=99)
		 {
		      intYear=intYear+2000;
	     }
		 return intDay+"/"+intMonth+"/"+intYear; 
	 }

	/*
     #############################################################################
     # Function Name: isValidDateTime()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: check a composite date/time field  assume date is everything up to first space  and time is everything after first space.
     # Parameters: string strDateTime : Date time string in DD-MM-YYYY HH:MM:SS
     # ON SUCCESS: Returns TRUE date time is valid.
     # ON FAILURE: Returns FLASE  date time is invalid.
     #############################################################################
	 */

	 function isValidDateTime(strDateTime) 
	 {
		 var dt = Trim(strDateTime); 
		 var intMatch; 
		 var intDateOnly = false; 
		 if (strDateTime.toLowerCase()=="today" || strDateTime.toLowerCase()=="now")
		 {
			 return true;
		 }
		 
		 intMatch=dt.indexOf(":"); 
		 if (intMatch < 0) 
		 {
			 intDateOnly = true; 
			 intMatch=dt.length; 
		 }
		 else 
		 {
			 intMatch=dt.indexOf(" "); 
		 }
		 
		 if (intMatch < 0) 
		 {
			 return false;
		 }
		 
		 // check date 
		 if (!isValidDate(dt.substr(0,intMatch)))
		 {
			 return false;
		 }
		 
		 // check time 
		 if (!intDateOnly) 
		 {
			 if (!isValidTime(dt.substr(intMatch+1,dt.length-intMatch)))
			 {
				 return false;
			 }
		 }
		 return true; 
	 }

	/*
     #############################################################################
     # Function Name: LeapYear()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: check year wether it is a leap year or not.
     # Parameters: string intYear : Year
     # ON SUCCESS: Returns TRUE given year is leap year.
     # ON FAILURE: Returns FLASE  given year is not a leap year.
     #############################################################################
	 */

	 function LeapYear(intYear) 
	 {
		 if (intYear % 100 == 0) 
		 {
			 if (intYear % 400 == 0) 
			 {
				 return true; 
			 }
		 }
		 else 
		 {
			 if ((intYear % 4) == 0) 
			 { 
				 return true; 
			 }
		 }
		 return false; 
	 }

	/*
     #############################################################################
     # Function Name: isEarlierOrEqual()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: This function will accept start date and end date and will check for date format and it will also check wether end date is earlier than start date.
     # Parameters: string start : Start date 
	 #                    string end  : End date 
     # ON SUCCESS: Returns TRUE If both date are in valid format and start date is earlier than end date.
     # ON FAILURE: Returns FLASE If either of date are not in valid format or end date is before start date.
     #############################################################################
	 */

	 function isEarlierOrEqual(start,end) 
	 {
		 // convert dates to dd/mm/yyyy 
		 var myStart = isValidDate(start,true);
		 var myEnd = isValidDate(end,true); 
		 if (myStart=="" || myEnd=="") 
		 {
			 return false; 
		 }

		 var startparts= myStart.split("/"); 
		 var endparts=myEnd.split("/"); 

		 if (Date.UTC(startparts[2],startparts[1],startparts[0]) <= Date.UTC(endparts[2],endparts[1],endparts[0])) 
		 {
			 return true; 
		 }
		 else 
		 {
			 return false; 
		 }
	 }

	/*
     #############################################################################
     # Function Name: isTimeEarlierOrEqual()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: This function will accept start time and end time and will check for time format and it will also check wether end time is earlier than start time.
     # Parameters: string start : Start time 
	 #                    string end  : End time 
     # ON SUCCESS: Returns TRUE If both time are in valid format and start time is earlier than end time.
     # ON FAILURE: Returns FLASE If either of time are not in valid format or end time is before start time.
     #############################################################################
	 */

	 function isTimeEarlierOrEqual(start,end) 
	 {
		 // convert times to UTC dates 
		 if (start=="" || end=="") 
		 {
			 return false; 
		 }
		 var startparts= start.split(":"); 
		 var endparts=end.split(":"); 
		 if (Date.UTC(2000,1,1,startparts[0],startparts[1]) <= Date.UTC(2000,1,1,endparts[0],endparts[1])) 
		 {
			 return true; 
		 }
		 else 
		 {
			 return false; 
		 }
	 } 

	/*
     #############################################################################
     # Function Name: NewWindow()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: This function will open passed url in new window.
     # Parameters: string url : Url
	 #                    string title : Window Title
	 #                    string w : window width
	 #                    string h : window height 
	 #                    string scroll : boolean value will indicate wether window should contain scroll bars or not (true/false, 'yes'/ 'no')
	 #                    string resize : boolean value will indicate wether window is resizable or not (true/false, 'yes'/ 'no')
     # ON SUCCESS: open url in new window.     
     #############################################################################
	 */

	 function NewWindow(url, title, w, h, scroll, resize) 
	 {
		 
		 if (scroll==true || scroll=='yes') 
		 {
			 scroll='yes'; 
		 }
		 else 
		 {
			 scroll='no'; 
		 }
		 if (resize==true || resize=='yes') 
		 {
			 resize=', resizable'; 
		 }
		 else 
		 {
			 resize=''; 
		 }
		 var winl = (screen.width - w) / 2; 
		 var wint = (screen.height - h) / 2; 
		 winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+resize;
		 win = window.open(url, title, winprops);
	 }
	/*
     #############################################################################
     # Function Name: DisplayStatusMsg()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Display passed string in status bar.
     # Parameters: string msgStr : value
     # ON SUCCESS: Display passed string in status bar. 
     #############################################################################
	 */

	 function DisplayStatusMsg(msgStr)
	 { 
	    status=msgStr; document.MM_returnValue = true; 
	 }
	/*
     #############################################################################
     # Function Name: checkLength(element, length)
     # Created By: PHP Team   
     # Created on: July 25, 2006
     # Purpose: function to check length of text in passed element
     # Parameters: element : form element
     #             len : max allowed length
     # ON SUCCESS: Truncate  value of element to given length
     #############################################################################
	 */

	 function checkLength(element,len)
	 {
		if(element.value.length > len)
		{
			element.value = element.value.substring(0,len);
		}
	 }
	 /*
     #############################################################################
     # Function Name: popupPrintWindow(url)
     # Created By: PHP Team   
     # Created on: July 25, 2006
     # Purpose: function to open a popup print window
     # Parameters: url : url of new popup window
     #            
     # ON SUCCESS: Opens a popup window with url passed
     #############################################################################
	 */
	 function popupPrintWindow(url)
	 {
	
		var printWindow = window.open(url,'newwin','height=500,width=800,screenX=50,screenY=50,resizable,scrollbars,status');
		printWindow.print();
	 }

	
	 function addZero(vNumber)
	 { 
    	return ((vNumber < 10) ? "0" : "") + vNumber 
  	 } 
  	 function takeYear(theDate)
	 {
		x = theDate.getYear();
		var y = x % 100;
		y += (y < 38) ? 2000 : 1900;
		return y;
	 }
  	   /*
     #############################################################################
     # Function Name: fnFormatDate(element,strDate, vFormat)
     # Created By: PHP Team   
     # Created on: July 31, 2006
     # Purpose: function to format a date as per passed format
     # Parameters: elelment : date element which is to be formatted
     #             strDate : string date
     #             vformat : format like 'mm-dd-yyyy'
     #            
     # ON SUCCESS: Formatts date as per format passed in
     #############################################################################
	 */
     function fnFormatDate(element,strDate, vFormat)
	 {      
	 		if(strDate.indexOf('-') != -1)
	 		{
	 			strDateArray = strDate.split('-');
	 		}
	 		else if(strDate.indexOf('/') != -1)
	 		{
	 			strDateArray = strDate.split('/');
	 		}
	 		else
	 		{
	 			strDateArray = "";
	 		}
	 		//alert(typeof(strDateArray));
	        if(typeof(strDateArray) == 'object')
	        {
		        strDay = strDateArray[1]; 
			 	strMonth = strDateArray[0] - 1; 
			 	strYear = strDateArray[2]; 
			 	vDate = new Date(strYear,strMonth,strDay);
			    var vDay              = addZero(vDate.getDate()); 
			    var vMonth            = addZero(vDate.getMonth() + 1); 
			    var year              = takeYear(vDate);
			    var vYearLong         = addZero(year); 
			    var vYearShort        = addZero(year.toString().substring(3,4)); 
			    var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
			    var vHour             = addZero(vDate.getHours()); 
			    var vMinute           = addZero(vDate.getMinutes()); 
			    var vSecond           = addZero(vDate.getSeconds()); 
			    var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
			    vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
			  
			    if(!isNaN(vDay) && !isNaN(vMonth) && !isNaN(vYear))
			    {
			    	element.value =  vDateString;
			    }
		    }
	 }
	 /*
     #############################################################################
     # Function Name: AddOptionToList(strList,text, val)
     # Created By: PHP Team   
     # Created on: October 12, 2006
     # Purpose: function to add a new option in a select list
     # Parameters: strList : Select list element 
     #             text : string text for new option to be added
     #             val :value for new option to be added
     #            
     # ON SUCCESS: Adds a new option to given select list
     #############################################################################
	 */
	 function AddOptionToList(strList,text,val)
	{
		var elOptNew = document.createElement('option');
		elOptNew.text = text;
		elOptNew.value = val;
		try 
	    {
		  strList.add(elOptNew, null); // standards compliant; doesn't work in IE
	    }
	    catch(ex) 
	    {
		  strList.add(elOptNew); // IE only
	    }
	    for(j= 0;j< strList.options.length;j++)
	    {
	    	strList.options[j].selected = true; 
	    }
	}
	 /*
     #############################################################################
     # Function Name: fnFormatCurrency(num , element)
     # Created By: PHP Team   
     # Created on: Aug 9, 2006
     # Purpose: function to format a number as per US currency format
     # Parameters: num : number
     #             element : element whose value is to be formatted
     #           
     #            
     # ON SUCCESS: Formatts a number as per US currency format
     #############################################################################
	 */
	function fnFormatCurrency(num,element) 
	{
	    //checkLength(element,5);
	    num = element.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));
		element.value = (((sign)?'':'-') + '$' + num + '.' + cents);
		//return (((sign)?'':'-') + '$' + num + '.' + cents);
	}
	/*
     #############################################################################
     # Function Name: isValidCurrency()
     # Created By: PHP Team   
     # Created on: Aug 9 , 2006
     # Purpose: Checks if the passed string contain valid number or not. 
     # Parameters: string numval : Number to check 
     # ON SUCCESS: Returns TRUE if value is  valid currency.     
     # ON FAILURE: Returns FLASE  if value is not valid currency format.
     #############################################################################
	 */
	 function isValidCurrency(numval) 
	 {
		 if (isBlank(numval))
		 {
			 return false;
		 }
		 var myRegExp = new RegExp("^[/$+|/-]?[0-9]*[/.]?[0-9]*$"); 
		
		 return myRegExp.test(numval); 
	 }
    /*
     #############################################################################
     # Function Name: fnIsSafeText()
     # Created By: PHP Team   
     # Created on: Aug 20 , 2006
     # Purpose: Checks if the passed text is safe or not. Letters , digits, space 
	 #          and some chars are allowed in safe text 
     # Parameters: string strtext : text to check
     # ON SUCCESS: Returns TRUE if value is  safe text 
     # ON FAILURE: Returns FALSE  if value is not safe text.
     #############################################################################
	 */
     function fnIsSafeText(strText)
	 {
	 	var myRegExp = /^[a-zA-Z0-9_.\s\'\-\?\,\/]+$/;
	 	//var myRegExp = /^[a-zA-Z0-9_.']+$/; 
	 	if(!myRegExp.test(strText)) 
		{
			 return false; 
		}
		else
		{
			 return true; 
		}
	 }
	 
	/*
     #############################################################################
     # Function Name: getAge()
     # Created By: PHP Team   
     # Created on: 14  Nov 2006
     # Purpose: This function will open passed url in new window.
     # Parameters: string varBirthDate : 'mm-dd-yyyy' format date string
     # ON SUCCESS: open url in new window.     
     #############################################################################
	 */
	 function getAge(varBirthDate) {
 
		 arrBithDate = varBirthDate.split('-');
		
		 month = (arrBithDate[0] -1);
		 date = arrBithDate[1];
		
		 year = arrBithDate[2];
		
		 if (month != parseInt(month,10)) { alert('Type Month of birth in digits only!'); return false; }
		 if (date != parseInt(date,10)) { alert('Type Date of birth in digits only!'); return false; }
		 if (year != parseInt(year,10)) { alert('Type Year of birth in digits only!'); return false; }
		 if (year.length < 4) { alert('Type Year of birth in full!'); return false; }
		
		 today = new Date();
		 dateStr = today.getDate();
		 monthStr = today.getMonth();
		 yearStr = today.getFullYear();
		
		 theYear = yearStr - year;
		 theMonth = monthStr - month;
		 theDate = dateStr - date;
		
		 var days = "";
		 if (monthStr == 0 || monthStr == 2 || monthStr == 4 || monthStr == 6 || monthStr == 7 || monthStr == 9 || monthStr == 11) days = 31;
		 if (monthStr == 3 || monthStr == 5 || monthStr == 8 || monthStr == 10) days = 30;
		 if (monthStr == 1) days = 28;
		
		  var strYear = "";
		  var strMonth = "";
		  var strDays = "";
		 //document.form1.inYears.value = theYear;
		
		 strYear = theYear;
		
		
		 if (month < monthStr && date > dateStr) { strYear = parseInt(strYear) + 1;
		                                           strMonth = theMonth - 1; }
		 if (month < monthStr && date <= dateStr) { strMonth = theMonth; }
		 else if (month == monthStr && (date < dateStr || date == dateStr)) { strMonth = 0; }
		 else if (month == monthStr && date > dateStr) { strMonth = 11; }
		 else if (month > monthStr && date <= dateStr) { strYear = strYear - 1;
		                                                 strMonth = ((12 - -(theMonth)) + 1); }
		 else if (month > monthStr && date > dateStr) { strMonth = ((12 - -(theMonth))); }
		
		
		 if (date < dateStr) { strDays = theDate; }
		 else if (date == dateStr) { strDays = 0; }
		 else { strYear = strYear - 1; strDays = days - (-(theDate)); }
		 var resultText = " Age : ";
		 if(strYear > 1)
		 {
		 	resultText += strYear+" years ";
		 }
		 else
		 {
		 	resultText += strYear+" year ";
		 }
		 if(strMonth > 1)
		 {
		 	resultText += strMonth+" months ";
		 }
		 else
		 {
		 	resultText += strMonth+" month ";
		 }
		 
		 if(strDays > 1)
		 {
		 	resultText += strDays+" days ";
		 }
		 else
		 {
		 	resultText += strDays+" day ";
		 }
		 return resultText;
	 }
	 /*
     #############################################################################
     # Function Name: isValidMedicationName()
     # Created By: Ghanshyam Rokde   
     # Created on: 19 Dec 2006
     # Purpose: Checks if field value contains only alphanumeric, '_', '&', '#', '@', ':' And '%' charactes. Also checks  that alphabetical chars. and '_' must have to be come first and followed by numbers. It returns false if above conditions will not satisfy otherwise true.
     # Parameters: string strVal : string to be validated
     # ON SUCCESS: Returns TRUE if string is in valid format.  
     # ON FAILURE: Returns FLASE if string is not in valid format.  
     #############################################################################
	 */
	 function isValidMedicationName(strVal) 
	 {
		 //var pattern = "^[a-zA-Z]+[a-zA-Z0-9_\s\.]*";
		 var regex = /^[a-zA-Z0-9_\s-'.,&amp;#@:%?!()$\/]+$/;
        
		 //var regex = new RegExp(pattern);
		
		 if(regex.test(strVal)) 
		 {
			 return true; 
		 }
		 else
		 {
			 return false; 
		 }
	 }
// stop hiding --> 
function convertToMilliliter(val,unit)
{
  var arrVolumeConversion = new Array();
  arrVolumeConversion['cc'] = 1;
  arrVolumeConversion['liters'] = 1000;
  arrVolumeConversion['ounces'] = 29.57;
  arrVolumeConversion['pints'] = 473.17;
  arrVolumeConversion['gallons'] = 3785.41;
  var convertedVal = parseFloat(arrVolumeConversion[unit] * val);
  var convertedVal = convertedVal.toString();
  if(convertedVal.indexOf('.',0) > 0)
  {
	var decimalPtIndex = new Number(convertedVal.indexOf('.',0)) + 3;
	var convertedVal = convertedVal.substr(0,decimalPtIndex);
  }
  var convertedVal = new Number(convertedVal);
  return convertedVal;
 }
 //Login form validation function for TRade Account Login
 function fnCheckLogin()
{
	var f = document.frmLogin;
	if(Trim(f.txtUsername.value) == "")
	{
		alert("Please enter username");
		f.txtUsername.focus();
		return false;		
	}
	else
	{
		if(!fnIsValidUsername(f.txtUsername.value))
		{
			alert("Please enter valid username");
			f.txtUsername.focus();
			return false;	
		}
	}
	if(Trim(f.txtPass.value) == "")
	{
		alert("Please enter password");
		f.txtPass.focus();
		return false;		
	}
	return true;
}
