	function validateFilename(formid){
		var valuetobevalidated = document.getElementById(formid).value;
		var filevalid = /^[a-zA-Z_0-9.:\\/-]+$/;
		if (!filevalid.test(valuetobevalidated)) { 
			return false; 
		}
		return true;
	}
	
	function checkFilename(formid){
		var valuetobevalidated = document.getElementById(formid).value;
		var filevalid = /^[a-zA-Z_0-9./-]+$/;
		if (!filevalid.test(valuetobevalidated)) { 
			return false; 
		}
		return true;
	}

	//Trims spaces both left and right
	function trim(string){
		string = ltrim(string);
		return rtrim(string);
	}
	
	//Trims spaces at the right
	function rtrim(string){
		while(string.charAt((string.length -1))==" "){
			string = string.substring(0,string.length-1);
		}
		return string;
	}
	
	//Trims spaces at the left
	function ltrim(string){
		while(string.charAt(0)==" "){
			string = string.replace(string.charAt(0),"");
		}
		return string;
	}
	
	//Validates numeric value
	function IsNumeric(sText){
	   	var ValidChars = "0123456789.";
	   	var IsNumber=true;
	   	var Char;
	   	for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
		  	if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
	   	return IsNumber;   
	}

	//Gets part of a string 
	function Mid(STRING,START,END){
		if(!START){START=0};
		if(!END || END > STRING.length){END=STRING.length};
		if(END!=STRING.length){END = START + END};
		return STRING.substring(START,END);
	}
	
	//Format Date
	function FormatDate(DateToFormat,FormatAs){
		if(DateToFormat==""){return"";}
		if(!FormatAs){FormatAs="dd/mm/yyyy";}

		var strReturnDate;
		FormatAs = FormatAs.toLowerCase();
		DateToFormat = DateToFormat.toLowerCase();
		var arrDate
			var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var strMONTH;
		var Separator;
		
		while(DateToFormat.indexOf("st")>-1){
		DateToFormat = DateToFormat.replace("st","");
		}
		
		while(DateToFormat.indexOf("nd")>-1){
		DateToFormat = DateToFormat.replace("nd","");
		}
		
		while(DateToFormat.indexOf("rd")>-1){
		DateToFormat = DateToFormat.replace("rd","");
		}
		
		while(DateToFormat.indexOf("th")>-1){
		DateToFormat = DateToFormat.replace("th","");
		}
		
		if(DateToFormat.indexOf(".")>-1){
		Separator = ".";
		}
		
		if(DateToFormat.indexOf("-")>-1){
		Separator = "-";
		}
		
		
		if(DateToFormat.indexOf("/")>-1){
		Separator = "/";
		}
		
		if(DateToFormat.indexOf(" ")>-1){
		Separator = " ";
		}
		
		arrDate = DateToFormat.split(Separator);
		DateToFormat = "";
			for(var iSD = 0;iSD < arrDate.length;iSD++){
				if(arrDate[iSD]!=""){
				DateToFormat += arrDate[iSD] + Separator;
				}
			}
		DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
		arrDate = DateToFormat.split(Separator);
		
		if(arrDate.length < 3){
		return "";
		}
		
		var DAY = arrDate[0];
		var MONTH = arrDate[1];
		var YEAR = arrDate[2];
	
		if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
		}
		
		if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
		}
		
		
		for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
			if(MonthPosition > -1){
			MONTH = iSD + 1;
				if(MonthPosition == 0){
				DAY = arrDate[1];
				YEAR = arrDate[2];
				}
			break;
			}
		}
		
		var strTemp = YEAR.toString();
		if(strTemp.length==2){
		
			if(parseFloat(YEAR)>40){
			YEAR = "19" + YEAR;
			}
			else{
			YEAR = "20" + YEAR;
			}
		
		}
		
		
			if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
			MONTH = "0" + MONTH;
			}
			if(parseInt(DAY)< 10 && DAY.toString().length < 2){
			DAY = "0" + DAY;
			}
			switch (FormatAs){
			case "dd/mm/yyyy":
			return DAY + "/" + MONTH + "/" + YEAR;
			case "mm/dd/yyyy":
			return MONTH + "/" + DAY + "/" + YEAR;
			case "dd/mmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
			case "mmm/dd/yyyy":
			return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
			case "dd/mmmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
			case "mmmm/dd/yyyy":
			return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
			}
		
		return DAY + "/" + strMONTH + "/" + YEAR;;
		
		
	}
	
	//Converts a value to String
	function CStr(VALUE){
		return VALUE.toString();	
	}

	//Rounds-off number
	function Round(NUMBER,PLACES){
		if(!IsNumeric(NUMBER)){
			return "0";
		}
		NUMBER = CStr(NUMBER);
		arrNUMBER = NUMBER.split(".");
		if(arrNUMBER.length==1){return NUMBER;}
		if(PLACES){
			NUMBER = Math.round(NUMBER*Math.pow(10,PLACES))/Math.pow(10,PLACES);
		}
		else{
			NUMBER = Math.round(NUMBER);
		}
		return NUMBER;
	}
	

	//Formats number to general format
	function FormatCurrency(FIGURE){
		if(!FIGURE||FIGURE==""){
			return "0.00";
		}
		var strTemp = FIGURE.toString();
		while(strTemp.indexOf(",") > -1){
			strTemp = strTemp.replace(",","");
		}
		
		strTemp = parseFloat(strTemp);
		
		FIGURE = Round(FIGURE,2);
		FIGURE = FIGURE.toString();
		var Place = FIGURE.indexOf(".");
		if(Place >-1){
			if((FIGURE.length - Place) == 2){
				FIGURE += "0";
			}
		}
		else{
			FIGURE += ".00";
		}
		
		if(FIGURE=="0.00"){
			return"";
		}
		return FIGURE;
	}

	//Start of Date Validation
	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
	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++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	
	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   } 
	   return this
	}
	
	function isDate(dtStr){
		var dtCh= "/";
		var minYear=1900;
		var maxYear=2100;
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strMonth=dtStr.substring(0,pos1)
		var strDay=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			return false
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			return false
		}
	return true
	}
	
	//Validates Email Address
	function checkEmail(vValue) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(vValue)){
			return true;
		}
		return false;
	}
	
	function checkWebsite(argvalue) {
      if(argvalue!=""){
	        if (argvalue.indexOf ('www', 0) == -1 || argvalue.length < 10){
		        if(argvalue.indexOf ('.com', 0) == -1 || argvalue.length < 10){
			        return false;
		        }
	        }
        }

      return true;
    } 
	
	function addCommas(nStr){
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
		
	//Validates a number	
	function checkNumber(x){
		var vthenumber = x;
		var anum=/(^\d+$)|(^\d+\.\d+$)/;
		if ( anum.test(vthenumber) ){
			return true;
		}
		return false;
	}

	//Validates a year	
	function checkYear(x){
		var vthenumber = x;
		var anum=/(^\d+$)|(^\d+\.\d+$)/;
		if ( anum.test(vthenumber) ){
			return true;
		}
		return false;  
	}


	function checkPhoneNumber(strPhone){
		// Declaring required variables
		var digits = "0123456789";
		
		// non-digit characters which are allowed in phone numbers
		var phoneNumberDelimiters = "()- ";
		
		// characters which are allowed in international phone numbers; a leading + is OK
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		
		// Minimum no of digits in an international phone no.
		var minDigitsInIPhoneNumber = 10;
		
		s = stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
	
	
	// make sure start date is before the end date
	function checkStartEndDates(vStartDate,vEndDate){
		var basicage;
		
		var newBirthDate = new Date(vStartDate);
		var newDateToday = new Date(vEndDate);
	
		var vBirthDay = newBirthDate.getDate();
		var vBirthMonth = newBirthDate.getMonth() + 1;
		var vBirthYear = newBirthDate.getFullYear();
	
		var vDayToday = newDateToday.getDate();
		var vMonthToday = newDateToday.getMonth() + 1;
		var vYearToday = newDateToday.getFullYear();
		
		basicage = vYearToday - vBirthYear;
		
		if (basicage >= 0 ){
			if ( vBirthMonth > vMonthToday ){
				basicage = basicage - 1;
			}else if (vBirthMonth == vMonthToday ){
				if ( vBirthDay > vDayToday ){
					basicage = basicage - 1;
				}
			}
		}
		
		if (basicage >=0 )
			return true;
		else
			return false;
	}
	
	//Gets the age of an individual
	function getAge(vBirth){
		var basicage
		
		var vToday = new Date();
		var newBirthDate = new Date(vBirth);
		var newDateToday = new Date();
	
		var vBirthDay = newBirthDate.getDate();
		var vBirthMonth = newBirthDate.getMonth() + 1;
		var vBirthYear = newBirthDate.getFullYear();
	
		var vDayToday = newDateToday.getDate();
		var vMonthToday = newDateToday.getMonth() + 1;
		var vYearToday = newDateToday.getFullYear();

		basicage = vYearToday - vBirthYear;
		if ( vBirthMonth > vMonthToday ){
			basicage = basicage - 1;
		}else if (vBirthMonth == vMonthToday ){
			if ( vBirthDay > vDayToday ){
				basicage = basicage - 1;
			}
		}

		return basicage;
	}
	
	function PCase(STRING){
		var strReturn_Value = "";
		var iTemp = STRING.length;
		if(iTemp==0){
			return"";
		}
	
		var UcaseNext = false;
		strReturn_Value += STRING.charAt(0).toUpperCase();
		for(var iCounter=1;iCounter < iTemp;iCounter++){
			if(UcaseNext == true){
				strReturn_Value += STRING.charAt(iCounter).toUpperCase();
			}else{
				strReturn_Value += STRING.charAt(iCounter).toLowerCase();
			}
			var iChar = STRING.charCodeAt(iCounter);
			if(iChar == 32 || iChar == 45 || iChar == 46){
				UcaseNext = true;
			}else{
				UcaseNext = false
			}
			if(iChar == 99 || iChar == 67){
				if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
					UcaseNext = true;
				}
			}	
		} 
		return strReturn_Value;
	}


	//Compare any date data versus the Birth
	/*function getAgeDifference(vBirth,vDateToBeCompared){
		var diff, basicage
	
		var newBirthDate = new Date(vBirth);
		var DateToBeCompared = new Date(vDateToBeCompared);
	
		var vBirthDay = newBirthDate.getDate();
		var vBirthMonth = newBirthDate.getMonth() + 1;
		var vBirthYear = newBirthDate.getFullYear();
		
		alert('Birth Day: ' + vBirthDay);
		alert('Birth Month: ' + vBirthMonth);
		alert('Birth Year: ' + vBirthYear);
	
		var vDayToBeCompared = DateToBeCompared.getDate();
		var vMonthToBeCompared = DateToBeCompared.getMonth() + 1;
		var vYearToBeCompared = DateToBeCompared.getFullYear();

		alert('Compared Day: ' + vDayToBeCompared);
		alert('Compared Month: ' + vMonthToBeCompared);
		alert('Compared Year: ' + vYearToBeCompared);
		
		basicage = vYearToBeCompared - vBirthYear;
		alert('Old Age: ' + basicage);
		
		if ( vBirthMonth > vMonthToBeCompared ){
			diff = basicage - 1;
		}else if (vBirthMonth == vMonthToday ){
			if ( vBirthDay > vDayToBeCompared )
				diff = basicage - 1;
		}
		
		alert('New Age: ' + diff);
		return diff;
		
	}*/
	

function calculateTimeDiff(vTime1,vTime2){
	var newtime1 = new Date(vTime1);
	var newtime2 = new Date(vTime2);
	var sec = newtime2.getTime() - newtime1.getTime();
	
	//var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;

	//var days = Math.floor(sec / day);
	//sec -= days * day;
	//var hours = Math.floor(sec / hour);
	//sec -= hours * hour;
	//var minutes = Math.floor(sec / minute);
	//sec -= minutes * minute;
	//var seconds = Math.floor(sec / second);
	
	
	//alert('Hours: ' + hours);
	//alert('Minutes: ' + minutes);
	
	if (sec < 0)
		return false;
	return true;
}
	
function SetAllCheckBoxes(FormName, FieldName, CheckValue){
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

function isNumberKey(evt, controlid){
	//alert(evt);
	//alert(controlid);
	var controltovalidate = document.getElementById(controlid).value;
	var charCode = (evt.which) ? evt.which : event.keyCode
    
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
    	    return false; 
    	    
    if ( (controltovalidate.indexOf(".",0) >= 0) && (charCode==46) )
        return false;
        
	return true;
}

	function left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}
	
	function right(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}