
function trim(strText)
{
	if (strText.length > 0)
	{
		while (strText.indexOf(" ")==0)
		{
			strText = strText.replace(" ","")
		}

		while (strText.lastIndexOf(" ")==strText.length-1 && strText.length > 0)
		{
			strText = strText.substring(0,(strText.length-1))
		}
	}
	return strText;
}

	
function ltrim(strText)
{
	while (strText.indexOf(" ")==0)
	{
	strText=strText.replace(" ","")
	}
	return strText;
}
 
//function to compare 2 dates. This function takes in 2 parameters (i.e dates ) and 
//the format of both the parameters is mm/dd/yy .This function returns the difference in days
function jsDateDiff( start, end) 
{
    var iOut = 0;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    var number = bufferB-bufferA ;
    
    iOut = parseInt(number / 86400000) ;
    return iOut ;
}




function openWin(strURL,strWinName,winWidth,winHeight)
{
	var chngpass;
	winTop=0
	winLeft=0
	winLeft=Math.floor((Math.abs(screen.availWidth-winWidth))/2);
	winTop=Math.floor((Math.abs(screen.availHeight-winHeight))/2);

	strWin=window.open(strURL,strWinName,'top='+ winTop + ',left=' + winLeft + ',width=' + winWidth + ',height=' + winHeight + ',toolbar=no menubar=no,location=no,directories=no,status=no,resizable=no,scrollbars=auto');
	return strWin;
}

function confDelete(section,message)
{
	strSection = section
	strMessage = message.substr(0,50) 
	if (message.length>50)
	{
		strMessage = strMessage + "..."
	}

	if (section!="" && message!="")
	{
		return confirm("Proceed with deletion of '" + strSection + "'\n '" + strMessage + "'?" ) 
	}
	else
	{
		return confirm("Proceed with deletion?") 
	}
}

//function to validate a User Name TextBox
function chkUserName(txtElement,fieldName)
{
	if(ltrim(txtElement.value).length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	if(txtElement.value.indexOf(" ") != -1)
	{
		alert("Please enter a valid '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	return true;
}

function chkPassword(txtElement,fieldName)
{
	var checkstring = /[^-_()&+*\"\'@!%\/?:=~#a-zA-Z0-9]/
	if(txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	if(txtElement.value.search(checkstring)!=-1)
	{
		alert("Please enter a valid password.\nIt can only include numbers, upper and lower case letters\nor the following special characters . - _ ( ) & + * \" ' @ ! % / ? : = ~ # ");
		txtElement.focus();
		return false;
	}
	return true;
}


//function to validate Date (only month and year)
function chkMMYY(txtMMElement,txtYYElement,fieldName,allowEmpty)
{	
	if(allowEmpty == false)
	{
		if (txtMMElement.selectedIndex==0)
		{
			alert("Select a Month  for '" + fieldName + "'")
			txtMMElement.focus()
			return false
		}
		if (txtYYElement.selectedIndex==0)
		{
			alert("Select a Year for '" + fieldName + "'")
			txtYYElement.focus()
			return false
		}
	}

	if( allowEmpty == true && txtMMElement.selectedIndex==0 && txtYYElement.selectedIndex==0)
	{
		//empty value is allowed
		return true;
	}
	else
	{
		var i=0;
		var j=0;
		var strDate = "" + txtMMElement[txtMMElement.selectedIndex].value;
		strDate = strDate + "/" + "1" ;
		strDate = strDate + "/" + txtYYElement[txtYYElement.selectedIndex].value;

		j= strDate.indexOf("/",i);
		var strMnth=strDate.substring(i,j);
		
		i=strMnth.length + 1;
		j= strDate.indexOf("/",i);
		var strDay=strDate.substring(i,j);
		
		j=j+3;
		i=strDate.length
		var strYear=strDate.substring(j,i);

		strMnth--;
		dtDate=new Date(strYear,strMnth,strDay);
		var dtDay=dtDate.getDate();
		var dtMnth=dtDate.getMonth();
		var dtYear=dtDate.getYear();

		if (strYear == "")
		{
			alert("Invalid '" + fieldName + "'")
			txtMMElement.focus()
			return false;
		}
		
		if((strDay!=dtDay) || (strMnth!=dtMnth) || (strYear!=dtYear))
		{
			alert("Invalid '" + fieldName + "'")
			txtMMElement.focus()
			return false;	
		}
		return true;
	}
}

//function to validate Date
function chkDDMMYY(txtDDElement,txtMMElement,txtYYElement,fieldName,allowEmpty)
{	
	if(allowEmpty == false)
	{
		if (txtDDElement.selectedIndex==0)
		{
			alert("Select a Day for '" + fieldName + "'")
			txtDDElement.focus()
			return false
		}
		if (txtMMElement.selectedIndex==0)
		{
			alert("Select a Month  for '" + fieldName + "'")
			txtMMElement.focus()
			return false
		}
		if (txtYYElement.selectedIndex==0)
		{
			alert("Select a Year for '" + fieldName + "'")
			txtYYElement.focus()
			return false
		}
	}

	if( allowEmpty == true && txtDDElement.selectedIndex==0 && 
		txtMMElement.selectedIndex==0 && txtYYElement.selectedIndex==0)
	{
		//empty value is allowed
		return true;
	}
	else
	{
		var i=0;
		var j=0;
		var strDate = "" + txtMMElement[txtMMElement.selectedIndex].value;
		strDate = strDate + "/" + txtDDElement[txtDDElement.selectedIndex].value;
		strDate = strDate + "/" + txtYYElement[txtYYElement.selectedIndex].value;

		j= strDate.indexOf("/",i);
		var strMnth=strDate.substring(i,j);
		
		i=strMnth.length + 1;
		j= strDate.indexOf("/",i);
		var strDay=strDate.substring(i,j);
		
		j=j+3;
		i=strDate.length
		var strYear=strDate.substring(j,i);

		strMnth--;
		dtDate=new Date(strYear,strMnth,strDay);
		var dtDay=dtDate.getDate();
		var dtMnth=dtDate.getMonth();
		var dtYear=dtDate.getYear();

		if (strYear == "")
		{
			alert("Invalid '" + fieldName + "'")
			txtDDElement.focus()
			return false;
		}
		
		if((strDay!=dtDay) || (strMnth!=dtMnth) || (strYear!=dtYear))
		{
			alert("Invalid '" + fieldName + "'")
			txtDDElement.focus()
			return false;	
		}
		return true;
	}
}

//function to validate an Email
function chkEmail(txtElement,fieldName,allowEmpty)
{
	var exclude=/[^@\-\.\w\_]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	var strEmail = txtElement.value
	var email_array=strEmail.split(",");

	if(allowEmpty == false && txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus()
		return false;
	}

	if(allowEmpty == true && txtElement.value.length == 0)
	{
		//empty value is allowed
		return true;
	}
	else 
	{
		var email_num=0;
		var checkEmail;
		while (email_num < email_array.length)
		{ 
			var trimemail = trim(email_array[email_num]);
			if(((trimemail.search(exclude) != -1) || 
			(trimemail.search(check)) == -1)   ||	
			(trimemail.search(checkend) == -1))
			{
				checkEmail = "false";
			}
			else
			{
				checkEmail = "true";
			}
			email_num++;

			if(checkEmail == "false")
			{
				alert("Incorrect email address!");
				txtElement.focus()
				return false;
			}
		}
	}
	return true;
}

/*
	Notes:
	'exclude' checks 5 conditions:
	a) characters that should not be in the address
	b) characters that should not be at the start
	c) & d) characters that shouldn't be together
	e) there's not more than one '@'
	'check' checks there's at least one '@', later followed by at least one '.'
	'checkend' checks the address ends with a period followed by 2 or 3 alpha characters
	N.B. Javascript 1.2 only works with version 4 browsers and higher.
*/	

function chkPinPhoneFax(txtElement,fieldName,allowEmpty)
{
	if(allowEmpty==false && txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}

	if(txtElement.value.search("[^0-9 ,/+-]")!=-1)
	{
		alert("Please enter a valid " + fieldName + ".\nIt can only include numbers and the following special characters:\n space , / + -");
		txtElement.focus();
		return false;
	}
	return true;
}

function chkPhone(txtElement,fieldName,allowEmpty)
{
	if(allowEmpty==false && txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}

	if(txtElement.value.search("[^0-9]")!=-1)
	{
		alert("Please enter a valid " + fieldName + ".\nIt can only include numbers");
		txtElement.focus();
		return false;
	}
	return true;
}


function chkalpha(txtElement,fieldName,allowEmpty)
{
	if(allowEmpty==false && txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}

	if(txtElement.value.search("[^a-z A-Z]")!=-1)
	{
		alert("Please enter a valid " + fieldName + ".\nIt can only include characters and Space");
		txtElement.focus();
		return false;
	}
	return true;
}


function chkAmountExchange(txtElement,fieldName,allowEmpty)
{
	if(allowEmpty==false && txtElement.value.length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}

	if(txtElement.value.search("[^0-9,.]")!=-1)
	{
		alert("Please enter a valid " + fieldName + ".\nIt can only include numbers and the following special characters:, .");
		txtElement.focus();
		return false;
	}
	return true;
}

//function to validate a Numeric Field
function chkNumeric(txtElement,fieldName,minValue,maxValue,allowEmpty)
{
	if(allowEmpty==false && txtElement.value.length == 0){
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	if(isNaN(txtElement.value))	{
		alert("Please enter a valid '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	if(minValue != 0 && parseFloat(txtElement.value) < minValue){
		alert("'" + fieldName + "' should not be less than " + minValue);
		txtElement.focus();
		return false;
	}
	if(maxValue != 0 && parseFloat(txtElement.value) > maxValue){
		alert("'" + fieldName + "' should not be greater than " + maxValue);
		txtElement.focus();
		return false;
	}
	return true;
}



//function to validate a TextArea
function chkTxtArea(txtElement,maxAllowedLength,fieldName,allowEmpty)
{
	if(allowEmpty == false && ltrim(txtElement.value).length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	if(txtElement.value.length > maxAllowedLength)
	{			
		alert("You have entered " + txtElement.value.length + " characters in the " + fieldName + ". \nThe Maximum number of characters allowed for this field is " + maxAllowedLength);
		//Truncate Statement
		txtElement.value = txtElement.value.substring(-1,maxAllowedLength);
		return false;
	}
	return true;
}


//function to validate a TextBox Input
function chkTxtBox(txtElement,fieldName)
{

	if(ltrim(txtElement.value).length == 0)
	{
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	return true;
}

//checking if a checkbox is selected
function chkCheckBox(chkElement,fieldName,isAlert) 
{ 
	if(isNaN(chkElement.length)) //ie if its not a group
	{ 
		if(!chkElement.checked) 
		{
			if (isAlert)
			{
				alert("Please select  '" + fieldName + "'")
			}
			return false;
		}
		else
		{
			return true;
		}
	}
	else      //ie if it is a group
	{	var isChecked=false
		for(i=0;i<chkElement.length;i++)  //
		{
			isChecked = isChecked || chkElement[i].checked
		}
		if (!isChecked)
		{
			if (isAlert)
			{
				alert("Please choose  '" + fieldName + "'")
			}
				return false;
		}
		else
		{
			return true;
		}
	}
}


function chkRadio(optElement,fieldName)
{
	if(isNaN(optElement.length))
	{ 
		if(!optElement.checked) 
		{
			alert("Please select a '" + fieldName + "'")
		
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{	var isChecked=false
		for(i=0;i<optElement.length;i++)  //
		{
			isChecked = isChecked || optElement[i].checked					 
			
		}
		if (!isChecked)
		{
			alert("Please select a '" + fieldName + "'")
			optElement[0].focus();
			return false
		}
		else
		{
			return true;
		}
	}
}	


//checking if a dropdown listbox is selected. 
//ASSUMPTION: a dropdown  listbox i considered to be not selected if its first element is currently selected
function chkDDList(lstElement,fieldName)
{
	if (lstElement.selectedIndex<=0)
	{
		alert("Please select a '" + fieldName + "'")
		lstElement.focus()
		return false;
	}
	else
	{
		return true;
	}
}



//// added by priya 
////to check date validation in mm/dd/yyyy format

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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

function isDate( txtElement,fieldName) 
{  
    

    if(ltrim(txtElement.value).length == 0)
	{      
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
    
   
    
    if (isValidDate ( txtElement.value ) == false){
               alert("Please enter '" + fieldName + "' in mm/dd/yyyy format");
		txtElement.focus();
               return false; 
    }
	
    return true;    	
    
}

////to check time validation in HH:MM:SS format(allowing both standard time & military time format)


function isTime( txtElement,fieldName) 
{  
    
    
    if(ltrim(txtElement.value).length == 0)
	{      
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
    
    
    
    if ( isValidTime ( txtElement.value ) == false){
               alert("Please enter '" + fieldName + "' in HH:MM:SS format");
		txtElement.focus();
               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 is not in a valid format.");
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  1 and 12. (or 0 and 23 for military time)");
return false;
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
alert("You must specify AM or PM.");
return false;
   }
}
if  (hour > 12 && ampm != null) {
alert("You can't specify AM or PM for military time.");
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;
}





