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 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(" ") == 0)
	{
		alert("Please enter a valid '" + fieldName + "'");		
		txtElement.focus();
		return false;
	}
	return true;
}

//check  for other value
function chkOthers(txtElement,fieldName)
{
	if(txtElement.value == "Other" || txtElement.value == "")
	{
		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 checkNumeric(txtElement,fieldName,maxNum){
	

	if(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(maxNum != 0 && parseInt(txtElement.value) > maxNum){
		alert("'" + fieldName + "' should be less than " + maxNum);
		txtElement.focus();
		return false;
	}
	

	return true;
}

function chktell(txtElement1,fieldName1,txtElement2,fieldName2,allowEmpty)
{

	if (checkNumeric1(txtElement1,fieldName1,0,allowEmpty)==false)
	 return false;
	else
	{
		if( txtElement1.value.length != 0 && txtElement2.value.length == 0){
			alert("Please enter '"+ fieldName2 +"'");
			txtElement2.focus();
			return false;
		}
		if(isNaN(txtElement2.value))	{
			alert("Please enter a valid '"+ fieldName2 +"'");
			txtElement2.focus();
			return false;
		}
		
	}

}


//function to validate a Numeric Field
function checkNumeric1(txtElement,fieldName,maxNum,allowEmpty){
	
	if(allowEmpty==false && txtElement.value.length == 0){
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	
	//if(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(maxNum != 0 && parseInt(txtElement.value) > maxNum){
		alert("'" + fieldName + "' should be less than " + maxNum);
		txtElement.focus();
		return false;
	}

}

//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 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 checkTextBox(txtElement,fieldName){
	if(txtElement.value.length == 0){
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	return true;
}


function checkTextBoxAlpa(txtElement,fieldName){
	var str = ltrim(txtElement.value);
	if(str.length == 0){
		alert("Please enter '" + fieldName + "'");
		txtElement.focus();
		return false;
	}
	
	if(str.search("[^a-z ,',/ABCDEFGHIJKLMNOPQRSTUVWXYZ/]")!=-1)
		{
			alert("Please enter a valid " + fieldName + ".\nIt can only include alphabets");
			txtElement.focus();
			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 '" + fieldName + "'")
		lstElement.focus()
		return false;
	}
	else
	{
		return true;
	}
}

function chkpastexp(txtElementorg,txtElementdesig,txtElementyy,txtElementmm,txtElementjobres)
{
	
	if(ltrim(txtElementorg.value).length != 0)
	{
		if (chkUserName(txtElementdesig,"Designation")==false)return false;
		if (chkDDList(txtElementyy,"Year")==false)return false;
		if (chkDDList(txtElementmm,"Months")==false)return false;
		if (txtElementyy.value=="0" && txtElementmm.value=="0" )
		{
			alert("Please enter valid Experience");
			txtElementyy.focus();
			return false;
		}
		if (chkTxtArea(txtElementjobres,500,"Job responsibilties",false)==false)return false;
	}

}

function chkdatediff(field1,field2,field3,age)
{

var startingdate=new Date(field1.value, field2.value, field3.value) //Month is 0-11 in JavaScript
var today=new Date()
//Get 1 day in milliseconds
var one_day=1000*60*60*24
//Calculate difference btw the two dates, and convert to days
if(Math.ceil((today.getTime()-startingdate.getTime())/(one_day)/365)>age)
{
	alert("The required age for this post should not be more than "+age);
	field1.focus();
	return false;
}

}

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)
	{
		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 to validate verification code
function SecurityCode(txtElement,txtElement1,fieldName)
{
//    alert(txtElement1.value);
    var imgCode=document.getElementById("ctl00_cphLNG_hdnCheck").value;
//    alert(imgCode);
	if(ltrim(txtElement.value) != ltrim(txtElement1.value))
	{
		document.getElementById('ctl00_cphBody_lblErrMsg').innerHTML=fieldName;	
		txtElement.focus();
		return false;
	}
	
	return true;
}
