// --------------------- Email Verification Function ----------------------
function isEmail(objField, strFieldDisplayName, blnCheckEmpty) {
	if (blnCheckEmpty){	//can't be empty
		if (!isNotEmptyText(objField)){	//but is empty
			//alert('Please fill out the field "'+strFieldDisplayName+'"');
			objField.focus();			
			return false;
		}
	} else {	//might be empty
		if (!isNotEmptyText(objField)) return true;	//and is empty	
	}
	
	if (objField.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	} else {
		alert('The value in the field "'+strFieldDisplayName+'" should have valid email format');
		objField.focus();
		return false;
	}
}

function isNotEmptyText(objField) {
	var strTrim = objField.value.replace(/\s+/g, "");
	if (strTrim.length>0) return true;	//field is filled out
	else return false;						//field is empty	
}


function validEmail(emailid) {
	var strEmail = emailid ;
	if(!validcharOnly(strEmail,"Email Address") )
	return false ; 
	if(strEmail.charAt(strEmail.length-1) == ".")
	return false ;
	for(i=0;i<strEmail.length-1;i++)
	{	
	if(strEmail.charAt(i) == " ")
		return false ;
	}
	pos = strEmail.search("@");
	if(pos != -1)
	{
	part = strEmail.slice(pos+1) ;
	flag = -1;
	pos = part.search("@") ;
	if(pos != -1)
		return false ;
	for(i=0;i<part.length;i++)
	{
		if(part.charAt(i)=='.')
		{	
			flag = 0 ;
			break;
		}
	}
	if(flag != -1)
		return(true) ;
	else
		return(false) ;
	}
	else
	return(false) ;
}

// --------------------- Number Verification Function ----------------------
function blockNumbers(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return !reg.test(keychar);
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative) 
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

function numberVerify(numbervalue) {
	if(isNaN(numbervalue))
		return(false) ;	
	else
		return(true) ;
}
// --------------------- Phone No. Verification Function ----------------------

function phoneVerify(phonevalue) {
	valid = "0123456789-()"
	phonevalue = allTrim(phonevalue) ;
	for(i=0;i<phonevalue.length;i++) {
		flag = 0 ;
		for(j=0;j<valid.length;j++) {
			if(phonevalue.charAt(i) == valid.charAt(j)) {
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
			return(false) ;
	}
	return(true) ; 
}

// --------------------- Date Verification Function ----------------------

function dateVerify(datevalue)
{
	var strDate ;
	var month,day,year ;
	var datearray ;
	strDate = datevalue ;
	if(strDate.search("/") != -1 )
		datearray = strDate.split("/") ;
	else if(strDate.search("-") != -1 )
		datearray = strDate.split("-") ;
	else
		return(false) ;
	if(( datearray.length < 3 ) || ( datearray.length > 3 ) )
		return(false);
	
	m = datearray[0] ;
		d = datearray[1] ;
		y = datearray[2] ;
	if(isNaN(m))
		return(false) ;
	month = parseInt(m,10) ;
	if(isNaN(d))
		return(false) ;
	day = parseInt(d,10) ;
	if(isNaN(y))
		return(false) ;
	year = parseInt(y,10) ;
	if((day >= 1 && day <= 31) && ( month == 1 ) ||
		(day >= 1 && day <= 31) && ( month == 3 ) ||
		(day >= 1 && day <= 31) && ( month == 5 ) ||
		(day >= 1 && day <= 31) && ( month == 7 ) ||
		(day >= 1 && day <= 31) && ( month == 8 ) ||
		(day >= 1 && day <= 31) && ( month == 10 ) ||
		(day >= 1 && day <= 31) && ( month == 12 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((day >= 1 && day <= 30) && ( month == 4 ) ||
		(day >= 1 && day <= 30) && ( month == 6 ) ||
		(day >= 1 && day <= 30) && ( month == 9 ) ||
		(day >= 1 && day <= 30) && ( month == 11 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;	
	}
	else if((month == 2) && (year%4 == 0) && (day >= 1 && day <= 29))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((month == 2) && (day >= 1 && day <= 28))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
		}
	else
		return(false) ;
}

// --------------------- Save message alert Function ----------------------

function saveRecordMsg()
{
	if(confirm("Do you wish to save this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Edit message alert Function ----------------------

function editRecordMsg()
{
	if(confirm("Do you wish to edit this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Delete message alert Function ----------------------

function deleteRecordMsg()
{
	if(confirm("Do you wish to delete this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Exit message alert Function ----------------------

function exitMsg()
{
	if(confirm("Do you wish to exit from this window"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Customized message alert Function ----------------------

function customizedMsg(msg)
{
	if(confirm(msg))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Email Only field Function ----------------------

function	emailOnly(emailvalue,fieldname)
{
	if(emailVerify(emailvalue))
		return(true);
	else
	{
		alert("Please enter valid email address in "+fieldname) ;
		return(false) ;
	}
}

// --------------------- Number Only field Function ----------------------

function	numberOnly(numbervalue,fieldname)
{
	if(numberVerify(numbervalue))
		return(true);
	else
	{
		//alert("The "+fieldname+" can contain numeric values only") ;
		return(false) ;
	}
}

// --------------------- Phone Only field Function ----------------------

function	phoneOnly(phonevalue,fieldname)
{
	if(phoneVerify(phonevalue))
		return(true);
	else
	{
		alert("The "+fieldname+" can contain phone number values only") ;
		return(false) ;
	}
}

// --------------------- Date Only field Function ----------------------

function	dateOnly(datevalue,fieldname)
{
	if(dateVerify(datevalue))
		return(true);
	else
	{
		//alert("The "+fieldname+" is Invalid Date. Please Enter Valid Date mm/dd/yyyy.") ;
		return(false) ;
	}
}

// --------------------- Triming string from left sides Function ----------------------

function leftTrim(ltrimvalue)
{
	while(ltrimvalue.charAt(0)==' ')
		ltrimvalue = ltrimvalue.substr(1,ltrimvalue.length-1);
		
	return(ltrimvalue) ;
}

// --------------------- Triming string from right sides Function ----------------------

function rightTrim(rtrimvalue)
{
	while(rtrimvalue.charAt(rtrimvalue.length-1)==' ')
		rtrimvalue = rtrimvalue.substr(0,rtrimvalue.length-1);
	
	return(rtrimvalue) ;
}


 // --------------------- Triming string from both sides Function ----------------------

function allTrim(atrimvalue)
{
	return(rightTrim(leftTrim(atrimvalue))) ;
}


 // --------------------- Character validation Function ----------------------

function validcharOnly(charvalue,fieldname)
{
	invalid = "~/:<>`|^!';"
	charvalue = allTrim(charvalue) ;
	for(i=0;i<invalid.length;i++)
	{
		for(j=0;j<invalid.length;j++)
		{
			if(charvalue.charAt(j) ==	invalid.charAt(i))
			{
				alert("Only valid characters are allowed in "+fieldname) ;
				return(false);
			}
		}
	}
	for(i=0;i<charvalue.length;i++)
	{
		if(charvalue.charAt(i) == 66 || charvalue.charAt(i) == 39)
		{
			alert("Only valid characters are allowed in "+fieldname) ;
			return(false);
		}
	}
	return(true);
}


 // --------------------- Not Allowed Null Function ----------------------

function notallowedNull(charvalue,fieldname)
{
	if(allTrim(charvalue) == "")
	{
		alert("Value for "+fieldname+" is not allowed to be blank") ;
		return(false);
	}
	return(true) ;
}


 // --------------------- Money Validation Function ----------------------

function moneyOnly(moneyvalue,fieldname)
{	
	valid = "0123456789."
	moneyvalue = allTrim(moneyvalue) ;
	for(i=0;i<moneyvalue.length;i++)
	{
		flag = 0 ;
		for(j=0;j<valid.length;j++)
		{
			if(moneyvalue.charAt(i) == valid.charAt(j))
			{
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
		{
			alert("Only Money values are allowed in " + fieldname);
			return(false) ;
		}
	}
	return(true) ; 
}


 // --------------------- Dates Comparison Function ----------------------

function compareDates(datevalue1,datevalue2)
{
	
	var strDate = datevalue1;
	var month1,day1,year1,month2,day2,year2 ;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month1 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day1 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year1 = parseInt(y,10) ;
		}
	}
	var strDate = datevalue2;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month2 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day2 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year2 = parseInt(y,10) ;
		}
	}
	if(year1 > year2)
	{
		return(datevalue1) ;
	}
	else if(year2 > year1)
	{
		return(datevalue2) ;
	}
	else if(month1 > month2)
	{
		return(datevalue1) ;
	}
	else if(month2 > month1)
	{
		return(datevalue2) ;
	}
	else if(day1 >day2)
	{
		return(datevalue1) ;
	}
	else if(day2 > day1)
	{
		return(datevalue2) ;
	}
	else
	{
		return(datevalue1) ;
	}
}
 // --------------------- Date Range restriction Function ----------------------
function daterangeVerify(date1,date2,datediff)
{
	sStartDate = date1;
	sEndDate = date2;
		DateRange = datediff; 
	bStartDateSpecified = false;
	bEndDateSpecified = false;
	startDateObj = null;
	endDateObj = null;
	if ( sStartDate.length > 0 )
	{
		bStartDateSpecified = true;
		nStartYear = parseInt(sStartDate.substring(6, 10));
		sStartMonth = sStartDate.substring(0, 2);
		if ( sStartMonth.charAt(0) == '0' )
		{
			nStartMonth = parseInt(sStartMonth.substring(1, 2));
		}
		else
		{
			nStartMonth = parseInt(sStartMonth.substring(0, 2));
		}
		sStartDay = sStartDate.substring(3, 5);
		if ( sStartDay.charAt(0) == '0' )
		{
			nStartDay = parseInt(sStartDay.substring(1, 2));
		}
		else
		{
			nStartDay = parseInt(sStartDay.substring(0, 2));
		}
		startDateObj = new Date(nStartYear, nStartMonth-1, nStartDay);
	}
	if ( sEndDate.length > 0 )
	{
		bEndDateSpecified = true;
		nEndYear = parseInt(sEndDate.substring(6, 10));
		sEndMonth = sEndDate.substring(0, 2);
		if ( sEndMonth.charAt(0) == '0' )
		{
			nEndMonth = parseInt(sEndMonth.substring(1, 2));
		}
		else
		{
			nEndMonth = parseInt(sEndMonth.substring(0, 2));
		}
		sEndDay = sEndDate.substring(3, 5);
		if ( sEndDay.charAt(0) == '0' )
		{
			nEndDay = parseInt(sEndDay.substring(1, 2));
		}
		else
		{
			nEndDay = parseInt(sEndDay.substring(0, 2));
		}
		endDateObj = new Date(nEndYear, nEndMonth-1, nEndDay);
	}
	// Only need to check the date range if both the start and end date are specified
	if ( bStartDateSpecified && bEndDateSpecified )
	{
		nDayDiff = Math.round((endDateObj.getTime() - startDateObj.getTime())/86400000);
		if ( (nDayDiff >= 0) && (nDayDiff <= DateRange) )
		{
			return true;
		}
		else if ( nDayDiff > DateRange )
		{
			alert("An invalid date range was entered.	To Date cannot be more than " + DateRange + " days ahead of From Date.");
			return false;
		}
		
	}
	return true;	
}

function DateMonth(mon) {
	switch(mon) {
		case 'Jan':
			return 1;
		case 'Feb':
			return 2;
		case 'Mar':
			return 3;
		case 'Apr':
			return 4;
		case 'May':
			return 5;
		case 'Jun':
			return 6;
		case 'Jul':
			return 7;
		case 'Aug':
			return 8;
		case 'Sep':
			return 9;
		case 'Oct':
			return 10;
		case 'Nov':
			return 11;
		case 'Dec':
			return 12;
		default:
			return 1;
	}
}

//--------------------------- remove all spaces in string --------------
function spacenotallowed(str) {
	for(i=0;i<str.length;i++) {
		if(str.charAt(i)==' ')
			str = str.substr(1,str.length-1);
	}
	return(str) ;
}

function HaveSpecialChars(str) {
	for(i=0;i<str.length;i++) {
		//alert(str.charAt(i))
		if(str.charAt(i)=='"')
			return true;
		else if(str.charAt(i)=="'")
			return true;
	}
	return false;
}
