//Written by Muralidhar  and Ramanujam 01-10-2004 
// -------------------------------------------------------------------
// specialChar(fieldname)
//  check special characters 
// -------------------------------------------------------------------
function specialChar(inputString) 
{
	var iChars = "!@#$^&*()+=[]\\\';,./{}|\"<>?`~";
	//alert(inputString.value.length);
	for (var i = 0; i < inputString.value.length; i++) 
	{
		if (iChars.indexOf(inputString.value.charAt(i)) != -1) 
		{
			//alert();
			alert ("special chars like "+iChars+" are not allowed,Please reenter again");
			inputString.select(); 
			return true; 
		}
		//else
		//{
			//return true;    
		//}
		
	}
	return false;   
 }

//function to trim an input string in JavaScript.   
//written by Muralidhar on 07-10-2004
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   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
} // Ends the "trim" function

 // -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (obj.options==null) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	} 

//Modified by muralidhar 
 function moveSelectedOptions5(from,to,formname,comfield) 
 {
	// Unselect matching options, if required
	//alert("Hi1");
	var tempstring;
	if (arguments.length>5) {
		var regex = arguments[5];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) { 
		var o = from.options[i];
		if (o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			//alert(o.value);
			comfield.value=comfield.value+o.text+",";
			//alert(comfield.value);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {  
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<5) || (arguments[4]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	
	
	
	}

//Modified by muralidhar 
	function moveSelectedOptions1(from,to,formname,comfield) {
	// Unselect matching options, if required
	comfield.value="";
	var tempstring;
	if (arguments.length>5) {
		var regex = arguments[5];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
			else
			{
				comfield.value=comfield.value+o.text+",";
				//alert(comfield.value);
			}
		}
	if ((arguments.length<5) || (arguments[4]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	
	
	
	}

	//Modified by Muralidhar
// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
	function selectAllOptions1(obj,to) {
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		moveSelectOptions(obj,to);
		i=i-1;
		}
	}

	// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// ------------------------------------------------------------------- 
//modified on 28-09-2004 by muralidhar for the 'Reset' option 
function moveSelectOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) 
	{
		var o = from.options[i];
		if (o.selected) 
		{
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
		}
	}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) { 
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

	
	//modified by Muralidhar
	// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions1(from,to) { 
	selectAllOptions1(from,to);
	if (arguments.length==2) {
		moveSelectOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectOptions(from,to,arguments[2],arguments[3]);
		}
	}


	//moveselected for Ascending and Descending OrderBy Form // 31-08-04 Added By Ramanujam
	
	function moveSelectedOptions2(from,to,formname,varAscending,varDescending,orderbyfield) {
	// Unselect matching options, if required
	//comfield.value="";
	//alert(ascending);alert(descending); 
	var tempstring;
	if (arguments.length>7) {
		var regex = arguments[7];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);  
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i]; 
		if (o.selected) {
			var t=o.text;
			var strlength=t.length;
			var nstr=t.substring(0,strlength-4); 
				if(varAscending)
					{
			to.options[to.options.length] = new Option( o.text+" "+"Asc", o.value, false, false);
			orderbyfield.value=orderbyfield.value+o.text+" "+"Asc"+","; 
					}
					else if(varAscending==null){
						to.options[to.options.length] = new Option( nstr,o.value, false, false);
						orderbyfield.value="";
					}
				else if(varDescending)
				{
			to.options[to.options.length] = new Option( o.text+" "+"Desc", o.value, false, false);
			orderbyfield.value=orderbyfield.value+ o.text+" "+"Desc"+",";
				}
					else{
						to.options[to.options.length] = new Option( nstr, o.value, false, false);
					} 
			
		
			}
		}
	// Delete them from original 
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
	if (o.selected) {
	
			from.options[i] = null;  
			}
			else
			{
				 //orderbyfield1.value=orderbyfield1.value+o.text+" "+"Asc"+",";   
				
				 //alert(orderbyfield1.value); 
			}
		}
	if ((arguments.length<7) || (arguments[6]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1; 
	
	
	
	}

   
   //moveselected for Ascending and Descending OrderBy Form // 31-08-04 Added By Ramanujam
	
	function moveSelectedOptions4(from,to,formname,varAscending,varDescending,orderbyfield) {
		
	// Unselect matching options, if required 
	orderbyfield.value="";
	//alert(ascending);alert(descending); 
	var tempstring;
	if (arguments.length>7) {
		var regex = arguments[7];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);  
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i]; 
		if (o.selected) {
			var t=o.text;
			var strlength=t.length;
			var nstr=t.substring(0,strlength-4); 
				if(varAscending)
					{
			to.options[to.options.length] = new Option( o.text+" "+"Asc", o.value, false, false);
			//orderbyfield.value=orderbyfield.value+o.text+" "+"Asc"+","; 
					}
					else if(varAscending==null){
						to.options[to.options.length] = new Option( nstr,o.value, false, false);
						orderbyfield.value="";
					}
				else if(varDescending)
				{ 
					to.options[to.options.length] = new Option( o.text+" "+"Desc", o.value, false, false);
			//orderbyfield.value=orderbyfield.value+ o.text+" "+"Desc"+",";
				}
					else{
						to.options[to.options.length] = new Option( nstr, o.value, false, false);
					} 
			
		
			}
		}
	// Delete them from original 
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
	if (o.selected) {
	
			from.options[i] = null;  
			}
			else
			{
				 if(varAscending==null) 
				{
				 orderbyfield.value=orderbyfield.value+o.text+" "+",";  
				}
				else if(varDescending==null)
				{
					orderbyfield.value=orderbyfield.value+o.text+" "+",";
				}
				
				 //alert(orderbyfield1.value); 
			}
		}
	if ((arguments.length<7) || (arguments[6]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1; 
	
	
	
	}

//added by Muralidhar 
function moveSelectedOptions3(from,to,formname,comfield,typefield) { 
	// Unselect matching options, if required
	//alert("Hi");
	var tempstring;
	//comfield.value="";
	//typefield.value=""; 

	if (arguments.length>5) {
		var regex = arguments[5]; 
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			
			comfield.value=comfield.value+o.text+",";
			typefield.value=typefield.value+o.value+",";
			
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {  
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null; 
			}
		}
	if ((arguments.length<5) || (arguments[4]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	
	
	
	}

	// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		}
		sortSelect(from);
	from.selectedIndex = -1;  
	} 


	
	// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one -- murali 09/07/2004
// -------------------------------------------------------------------
function moveOptionUp1(obj,comfield) {  
	// If > 1 option selected, do nothing
	var selectedCount=0;
	comfield.value="";
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount!=1) {
		return;
		}
	// If this is the first item in the list, do nothing 
	var i = obj.selectedIndex;
	if (i == 0) {
		return;
		}
	swapOptions(obj,i,i-1);
	obj.options[i-1].selected = true;

	for (i=0; i<obj.options.length; i++) 
	{
		comfield.value=comfield.value+obj.options[i].text+",";
		
	}
}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one -- murali 09/07/2004
// -------------------------------------------------------------------
function moveOptionDown1(obj,comfield) {
	// If > 1 option selected, do nothing
	comfield.value=""; 
	var selectedCount=0;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount != 1) {
		return;
		}
	// If this is the last item in the list, do nothing
	var i = obj.selectedIndex; 
	if (i == (obj.options.length-1)) {
		return;
		}
	swapOptions(obj,i,i+1);
	obj.options[i+1].selected = true;
	for (i=0; i<obj.options.length; i++) 
	{
		comfield.value=comfield.value+obj.options[i].text+",";
		
	}
	}

	// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	} 

//enhanced script start
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 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=2300;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=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){
		alert("The date format should be : yyyy-mm-dd")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month\nThe date format should be : yyyy-mm-dd")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day\nThe date format should be : yyyy-mm-dd")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"\nThe date format should be : yyyy-mm-dd")
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date\nThe date format should be : yyyy-mm-dd")
		return false;
	}
return true
}

function IsValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {

alert("Time format should be in hh:mm:ss");  
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
alert("Hour must be between 0 and 23");
return false;
}
if(!isInteger(hour))
	{
	alert("Hour must be in numbers only");
	return false;
	}
if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59."); 
return false;
}
if (second != null && (second < 0 || second > 59)) {
alert ("Second must be between 0 and 59."); 
return false;
}
return true;
}

//For Filter Condition Screen			 
function moveUnSelectedOptions2(from,formname,comfield11) 
	{ 
		//var comfield1.value='';
		for (var i=0; i<from.options.length; i++) 
		{ 
			var o = from.options[i];  
			comfield11.value=comfield11.value+o.text+","; 
		} 
	} 
	

function dateDiff(date1,date2)
{
	var dtCh= "-";
	var pos1=date1.indexOf(dtCh)
	var pos2=date1.indexOf(dtCh,pos1+1)
	var strYear=date1.substring(0,pos1)
	var strMonth=date1.substring(pos1+1,pos2)
	var strDay=date1.substring(pos2+1)
	
	var pos11=date2.indexOf(dtCh)
	var pos22=date2.indexOf(dtCh,pos11+1)
	var strYear1=date2.substring(0,pos11)
	var strMonth1=date2.substring(pos11+1,pos22)
	var strDay1=date2.substring(pos22+1)
	
	var date1=new Date(strYear,strMonth,strDay)
	var date2=new Date(strYear1,strMonth1,strDay1)
	var one_day=1000*60*60*24
	
	//Calculate difference btw the two dates, and convert to days
	return Math.ceil((date1.getTime()-date2.getTime())/(one_day));

}

function timeDiff(time1,time2)
{
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = time1.match(timePat);
	var matchArray1=time2.match(timePat);
	hour = matchArray[1];
	hour1=matchArray1[1];
	return Math.ceil(hour-hour1);
	
}

function moveUnSelectedOptionscol(to,formname,comfield12) 
	{ 
		//var comfield1.value='';
for (var i=0; i<to.options.length; i++)      
		{ 
		var o = to.options[i];  
		comfield12.value=comfield12.value+o.text+",";
		 
			} 
	}     
//enhanced script end

//Generate Report -- Choose Columns
function moveGenUnSelectedOptions(from,to,formname,comfield1,comfield2) { 
for (var i=0; i<from.options.length; i++) 
		{ 
		var o = from.options[i];  
	comfield1.value=comfield1.value+o.text+",";
			}
		
		comfield2.value='';     
		for (var i=0; i<to.options.length; i++) 
		{ 
		  var o = to.options[i];
		comfield2.value=comfield2.value+o.text+",";
	//	alert(comfield2.value);
		        
		}       
	}                  