////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : trim
//	Prototype : trim(inputString)
//	Input Parameter : inputString
//	return value : String
//	Purpose : It will truncate leading and trailing white spaces from given string and return it.
//	Usage : trim(inputString)

	function trim(inputString)
	{
 	   if (typeof inputString != "string") { return inputString; }
		  var retValue = inputString;
		  var ch = retValue.substring(0, 1);
		   while (ch == " ") { // Check for spaces at the beginning of the string
      			retValue = retValue.substring(1, retValue.length);
			     ch = retValue.substring(0, 1);
   			}
		   ch = retValue.substring(retValue.length-1, retValue.length);
		  while (ch == " ") { // Check for spaces at the end of the string
		      retValue = retValue.substring(0, retValue.length-1);
      			ch = retValue.substring(retValue.length-1, retValue.length);
		   }
   		while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   			}
   		return retValue; // Return the trimmed string back to the user
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	function name : CompareDate
//	Prototype : CompareDate(Format,From,To)	
//	Input Parmeter : Format(It may take any one of 3 values 1."mdy" , 2."dmy" , 3."ymd")
//					 From (String contains the valid start date)
//					 To (String contains the valid end date)
//	return value : False -->  if 'To' date is greter then 'From' date
//	               True -->   if 'To' date is less then or equal to 'From' date
//	Purpose : compare the two dates(string) and return the result
//	Usage : CompareDate(Format,From,To).where Format="mdy" Or Format="dmy" Or Format="ymd")

	function CompareDate(Format,From,To)
	{
		//var chr;
		if(To.indexOf(" ") > 0)
		{
			chr = " ";
		}
		if(To.indexOf("-") > 0)
		{
			chr = "-";
		}
		if(To.indexOf("/") > 0)
		{
			chr = "/";
		}
		if(To.indexOf(".") > 0)
		{
			chr = ".";
		}
		
		//Element[0] For Month
		//Element[1] For Day
		//Element[2] For Year
		
		RegTempArray1=From.split(chr);
		RegTempArray2=To.split(chr);
		if(Format=="mdy")
		{
			RegFromArray = new makeArray(RegTempArray1[0],RegTempArray1[1],RegTempArray1[2]);
			RegToArray = new makeArray(RegTempArray2[0],RegTempArray2[1],RegTempArray2[2]);
		}
		else if(Format=="dmy")
		{
			RegFromArray = new makeArray(RegTempArray1[1],RegTempArray1[0],RegTempArray1[2]);
			RegToArray = new makeArray(RegTempArray2[1],RegTempArray2[0],RegTempArray2[2]);
		}
		else if(Format=="ymd")
		{
			RegFromArray = new makeArray(RegTempArray1[1],RegTempArray1[2],RegTempArray1[0]);
			RegToArray = new makeArray(RegTempArray2[1],RegTempArray2[2],RegTempArray2[0]);
		}
		
		if(RegFromArray[1].substr(0,1) == "0")
		{
			RegFromArray[1] = RegFromArray[1].substr(1);
		}
		if(RegToArray[1].substr(0,1) == "0")
		{
			RegToArray[1] = RegToArray[1].substr(1);
		}
		if(RegFromArray[0].substr(0,1) == "0")
		{
			RegFromArray[0] = RegFromArray[0].substr(1);
		}
		if(RegToArray[0].substr(0,1) == "0")
		{
			RegToArray[0] = RegToArray[0].substr(1);
		}
		
		if(parseInt(RegToArray[2]) == parseInt(RegFromArray[2]))
		{
			if(parseInt(RegToArray[0]) == parseInt(RegFromArray[0]))
			{
				if(parseInt(RegToArray[1]) == parseInt(RegFromArray[1]))
				{
					return true;
				}	
				else if(parseInt(RegToArray[1]) > parseInt(RegFromArray[1]))
				{
					
					return true;
				}
				else
				{
					return false;
				}
			}
			else if(parseInt(RegToArray[0]) > parseInt(RegFromArray[0]))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else if	(parseInt(RegToArray[2]) > parseInt(RegFromArray[2]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : ValidateYear
//	Prototype : ValidateYear()
//	Input Parameter : this
// 	Return Value : True (if  Year is a valid Year)
//				   False (if Year is Not a valid Year)
//	Purpose : It will check if Year entered  is valid or not
// 	Usage : ValidateYear(this). Here "this" indicates object itself

	function ValidateYear()
	{
		var smRegExpr=new RegExp('[1-2][0-9][0-9][0-9]');
		if (smRegExpr.test(arguments[0]))
			return true;
		return false;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : sValidateMailAddress
//	Prototype : sValidateMailAddress()
//	Input Parameter : this Or objectcontainername.value
// 	Return Value : True (if  email is a valid email)
//				   False (if email is Not a valid email)
//	Purpose : It will check if email entered  is valid or not
// 	Usage : ValidateYear(this). Here 'this' indicates object itself or objectcontainername.value

function sValidateMailAddress()
{
	var smRegExp = new RegExp('^[^@. ][^@ ]\*@[^@. ][^@ ]\*[.][^.@ ]\*[^@ ]\*[^@. ]$');

	if (arguments.length != 1) 
		return false;
	if (smRegExp.test(arguments[0]))
		return true;
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : isInteger
//	Prototype : isInteger(strValue)
//	Input Parameter : strValue
// 	Return Value : True (if  entered value is a valid integer value)
//				   False (if entered value is Not a valid integer value)
//	Purpose : It will check if value entered  is valid integer value or not
// 	Usage : ValidateYear(strValue)

	function isInteger(strValue)
	{
		var smRegExpr=new RegExp('^[0-9]*$')
		if(smRegExpr.test(strValue))
			return true;
		return false
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : checkExt
//	Prototype : checkExt(fileName)
//	Input Parameter : fileName
// 	Return Value : True (if  entered fileName contains valid extension)
//				   False (if entered fileName does not contain valid extension)
//	Purpose : It will check if filename entered contains valid extension or not.
// 	Usage : checkExt(fileName)

	function checkExt(fileName)
	{
		if(fileName.substring(fileName.lastIndexOf(".")+1,fileName.length).toUpperCase() == "JPG" ) 
			return true;
		else if(fileName.substring(fileName.lastIndexOf(".")+1,fileName.length).toUpperCase() == "JPEG" )
		    return true;
		else if(fileName.substring(fileName.lastIndexOf(".")+1,fileName.length).toUpperCase() == "GIF" ) 			 
		    return true;
		else if(fileName.substring(fileName.lastIndexOf(".")+1,fileName.length).toUpperCase() == "PNG" ) 			 
		    return true;
		alert("Invalid Image File Type. You can only Upload 'jpg' , 'JPEG' , 'Gif' , 'Png' file Type");
		return false;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : isEmpty
//	Prototype : isEmpty(strValue)
//	Input Parameter : strValue
// 	Return Value : True (if enterd String value is an empty string)
//				   False (if entered String value is not an empty string)
//	Purpose : It will check if String value entered is an empty string or not
// 	Usage : isEmpty(strValue)

	function isEmpty(strValue)
	{
		var smRegExpr=new RegExp('^[\r\n ]*$')
		if(smRegExpr.test(strValue))
			return true;
		return false;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : chkDate
//	Prototype : chkDate(month,day,year)
//	Input Parameter : month,day,year
// 	Return Value : True (if enterd date is a valid date value)
//				   False (if entered date is not a valid date value)
//	Purpose : It will check if entered date is a valid date or not based on given parameters
// 	Usage : chkDate(month,day,year)
	
	function chkDate(month,day,year)
	{
	    months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
	    //Convert values to integer types
	    month = month * 1;
	    day = day * 1;
	    year = year * 1;
	    var month_string = months[month];
	  	var leapYear = false;
	  	
		if(year % 400 == 0)
		{
	    	leapYear = true;
	  	} 
	  	else if(year % 100 == 0)
		{
	    	leapYear = false;
	  	} 
	  	else if(year % 4 == 0)
	  	{
	   		leapYear = true;
	  	}
	  	var days_in_feb = 28;
	  	if(leapYear)
		{
	    	days_in_feb = 29;
	  	}
	  	days_in_month = new Array(0,31,days_in_feb,31,30,31,30,31,31,30,31,30,31);

	    if (day > days_in_month[month])
		{
	        alert(month_string + " does not have " + day + " days.");
	        return false;
		}
		return true;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : occurs
//	Prototype : occurs(strVal,strSrc)
//	Input Parameter : strVal,strSrc
// 	Return Value : Integer
//	Purpose : It will return no of occurances of strVal within strSrc
// 	Usage : occurs(strVal,strSrc)

function occurs(strVal, strSrc)
{
	
	var nCnt = 0, strBuffer=strSrc;

	while (strBuffer.indexOf(strVal) >= 0)
	{
		strBuffer = replace(strBuffer, strVal, "");
		nCnt++;
	}
		
	return (nCnt);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : isDigit
//	Prototype : isDigit(val)
//	Input Parameter : val
// 	Return Value : True (if value entered contains digit only)
//				   False (if value entered does not contain only digits)
//	Purpose : It will check if value entered contains only digits[0-9] or not
// 	Usage : isDigit(val)

	function isDigit(val)
	{
		var strBuffer = new String(val);
		var nPos = 0;

		if (isEmpty(strBuffer))
			return false;

		for (nPos = 0; nPos < strBuffer.length; nPos++)
			if (strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9')
				return false;

		return true;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : isDate
//	Prototype : isDate(dVal)
//	Input Parameter : dVal
// 	Return Value : True (if value entered is valid date)
//				   False (if value entered is not a valid date)
//	Purpose : It will check if value entered is a valide date or not
// 	Usage : isDate(dval)

	function isDate(Format,dVal)
	{
		var strBuffer;
		var cDelimeter='';
		var strMonth=0, strDay=0, strYear=0;
		var nPos=-1;
	
		// Get the delimiter used
		if (occurs('/', dVal) == 2)
			cDelimeter = '/';
		else if (occurs('-', dVal) == 2)
			cDelimeter = '-';
		else if (occurs('.', dVal) == 2)
			cDelimeter = '.';
		else if (occurs(' ', dVal) == 2)
			cDelimeter = ' ';

		// If no '/' or '-' found return false
		if (cDelimeter == '')
			return false;

		RegTempArray=dVal.split(cDelimeter);
		if(Format=="mdy")
		{
			RegDateArray = new makeArray(RegTempArray[0],RegTempArray[1],RegTempArray[2]);
		}
		else if(Format=="dmy")
		{
			RegDateArray = new makeArray(RegTempArray[1],RegTempArray[0],RegTempArray[2]);
		}
		else if(Format=="ymd")
		{
			RegDateArray = new makeArray(RegTempArray[1],RegTempArray[2],RegTempArray[0]);
		}
		
		dVal=RegDateArray[0]+cDelimeter+RegDateArray[1]+cDelimeter+RegDateArray[2]
		
		strBuffer = new String(dVal);
		// validate month, date, and year (Y, YY, YYYY are valid year formats)
		nPos = strBuffer.indexOf(cDelimeter);
		strMonth = strBuffer.substring(0, nPos);
		if (strMonth.length > 2 || !isDigit(strMonth))
			return false;
		
		strBuffer = strBuffer.substring(nPos+1);
		nPos = strBuffer.indexOf(cDelimeter);
		strDay = strBuffer.substring(0, nPos);
		if (strDay.length > 2 || !isDigit(strDay))
			return false;
		
		strBuffer = strBuffer.substring(nPos+1);
		strYear = strBuffer;
		if ((strYear.length > 4) || (strYear.length == 3) || !isDigit(strYear))
			return false;
	
		// if YY < 50 then YYYY=20YY, else if YY >= 50 then YYYY=19YY
		var iYear = parseInt(strYear);
		if (iYear < 50)
			strYear = "20" + (strYear < 10 ? '0' + strYear:strYear);
		else if (iYear >= 50 && iYear < 100)
			strYear = "19" + strYear;

		strBuffer = strMonth + cDelimeter + strDay + cDelimeter + strYear;

		// validate date
		var dBuffer = new Date(strBuffer);
		if(strDay.substr(0,1) == "0")
		{
			strDay = strDay.substr(1);
		}
		if(strMonth.substr(0,1) == "0")
		{
			strMonth = strMonth.substr(1);
		}
	
		if (dBuffer.getDate() != parseInt(strDay) || 
				dBuffer.getMonth()+1 != parseInt(strMonth) || 
				dBuffer.getFullYear() != parseInt(strYear))
		return false;

	return true;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : replace
//	Prototype : replace(strSrc,strVal,strWith)
//	Input Parameter : strSrc,strVal,strWith
// 	Return Value : String
//	Purpose : It will replace strVal with strWith in strSrc
// 	Usage : replace(strSrc,strVal,strWith)

	function replace(strSrc, strVal, strWith)
	{
		var nPos = 0, strLeft="", strRight="";

		// check if empty (or) no string is found to replace
		if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0))
			return strSrc;

		nPos = strSrc.indexOf(strVal);
		strLeft = strSrc.substring(0, nPos);
		nPos += strVal.length;
		strRight = strSrc.substring(nPos);

		return (strLeft + strWith + strRight);
	}	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : start
//	Prototype : start()
//	Input Parameter : No Parameters Required
// 	Return Value : String
//	Purpose : It will clock with date  and time on screen
// 	Usage : start()

	var timer = null;
	function start()
	{
		
	    var months = new makeArray('January','February','March','April','May','June','July','August','September','October','November','December');
		var days = new makeArray('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		
		var time = new Date();
		var hours = time.getHours();
		var minutes = time.getMinutes();
		var month = time.getMonth();
		var day = time.getDay();
		
		minutes=((minutes < 10) ? "0" : "") + minutes;
		var seconds = time.getSeconds();
		seconds=((seconds < 10) ? "0" : "") + seconds;
		//var clock =time.getMonth()+1 + "/" + time.getDate() + "/" + time.getYear() + " " + hours + ":" + minutes + ":" + seconds
		var clock = days[day] + ", " + months[month] + " " + time.getDate() + ", " + time.getYear() + " " + hours + ":" + minutes + ":" + seconds;
	 	divTime.innerText = clock; 
		//times.value=clock;
		timer = setTimeout("start()",1000);
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : stop
//	Prototype : stop()
//	Input Parameter : No Parameters Required
// 	Return Value : 
//	Purpose : It will stop the clock generated by start function
// 	Usage : stop()

	function stop()
	{
		clearTimeout(timer);
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : makeArray
//	Prototype : makeArray()
//	Input Parameter : Array elements separated with commas
// 	Return Value : Array
//	Purpose : It will create required array
// 	Usage : makeArray(x,y,...). 

	function makeArray() 
	{
		for (i = 0; i<makeArray.arguments.length; i++)
		this[i] = makeArray.arguments[i];
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Function Name : checkAll
//	Prototype : checkAll(frmname,chkname)
//	Input Parameter : frmname,chkname
// 	Return Value : True
//	Purpose : It will check all the checkboxes on form(frmname) with common name(chkname) which is provided to function
// 	Usage : checkAll(frmname,chkname) 
	
	function checkAll(frmname,chkname)
	{
		if(document.forms.length==0)
			return true;
		
		for(j=0;j<document.forms.length;j++)
		{	
			if(document.forms[j].name==frmname)
			{
				for(i=0;i<document.forms[j].elements.length;i++)
				{
					if(document.forms[j].elements[i].name==chkname)
					{
						document.forms[j].elements[i].checked=true;
					}
				}
				break;
			}
		}
		return true;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
//	Function Name : clearAll
//	Prototype : clearAll(frmname,chkname)
//	Input Parameter : frmname,chkname
// 	Return Value : True
//	Purpose : It will Clear all the checkboxes on form(frmname) with common(chkname) name which is provided to function
// 	Usage : clearAll(frmname,chkname)
	
	function clearAll(frmname,chkname)
	{	
		if(document.forms.length==0)
			return true;
		for(j=0;j<document.forms.length;j++)
		{	
			if(document.forms[j].name==frmname)
			{
				for(i=0;i<document.forms[j].elements.length;i++)
				{
					if(document.forms[j].elements[i].name==chkname)
					{
						document.forms[j].elements[i].checked=false;
					}
				}
				break;
			}
		}
		return true;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
//	Function Name : TextToHtml
//	Prototype : TextToHtml(content)
//	Input Parameter : content
// 	Return Value : String
//	Purpose : It will take a String in Text Format and will Put <br> tag in place of 
//				carrige return and line feed characters.So that it can be display normaly in browser.
// 	Usage : TextToHtml(content)

	function TextToHtml(content)
	{	
		while(content.indexOf("\r\n") >= 0)
		{
			content = replace(content,"\r\n","<br>");
		}
		return content;
	}
	
