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=yes');

	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 your " + 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 a valid " + 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!");*/

				alert("Please enter a valid " + fieldName);

				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 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 + "' starts from " + minValue);

		txtElement.focus();

		return false;

	}

	if(maxValue != 0 && parseFloat(txtElement.value) > 5){

		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 your " + 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;
}

function chkNumber(txtElement, txtEcno)
{
	if(ltrim(txtElement.value).length > 5)
		{
			alert("Please enter '" + txtEcno + "'");
			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 '" + fieldName + "'")

		lstElement.focus()

		return false;

	}

	else

	{

		return true;

	}

}



