
function isNumeric(fieldname , fieldvalue,fieldcount)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")))
		{
			alert(fieldname+' contains only numeric value');
			return false;
		}
		i++;
	}
	return true;
}


//The following function is created/modified by Bhatt
function isNumericFinal(fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")))
		{
			return false;
		}
		i++;
	}
	return true;
}


function isvalidEmail(fieldvalue)
{
	
	var str = fieldvalue;
	i = 0;
	j=0;
	while(i < str.length)
	{
		if(str.charAt(i) == '@' || str.charAt(i) == '.')
		{
			j++;
		}
		i++;
	}
	
	if(j<2)
		return false;
	return true;
}


function hasOnlyAlphabets(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))))
		{
			alert(fieldname+' contains only alphabets');
			document.frmSearch.txtFirstName.focus();
			return false;
		}
		i++;
	}
	return true;
}

//The following function is created/modified by Bhatt
function isOnlyAlphabets(fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))))
		{
			return false;
		}
		i++;
	}
	return true;
}


function hasOnlyAlphaNumeric(fieldname , fieldvalue,fieldcount)
{
	var str = fieldvalue;
	i = 0;

	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z')) ||((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))))
		{
			alert(fieldname+' contains only alphanumeric value');
			//document.frmSearch.elements[fieldcount].focus();
			//document.frmSearch.txtFirstName.focus();
			return false;
		}
		i++;
	}
	return true;
}


//The following function is created/modified by Bhatt
function isOnlyAlphaNumeric(fieldvalue)
{
	var str = fieldvalue;
	i = 0;

	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z')) ||((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))))
		{
			return false;
		}
		i++;
	}
	return true;
}

//Added by Siddharth and Bhatt
function chkDate(MonIndex,SelDay,SelYear)
	{
		if(MonIndex > 12)
		{
			return false;
		}
		if(SelDay > 31)
		{
			return false;
		}
		if(MonIndex==4 || MonIndex==6 || MonIndex==9 || MonIndex==11)
		{
			if(parseInt(SelDay)==31)
			{
				return false;
			}
		} 
		
		if(MonIndex==2)	//For Feb.
		{
			if((parseInt(SelYear) % 4)!=0)
			{
				if(parseInt(SelDay)>28)
				{
					return false;
				}
			}
			else if((parseInt(SelYear) % 400)==0)
			{
				if(parseInt(SelDay)>29)
				{
					return false;
				}
			}
			else if((parseInt(SelYear) % 100)==0)
			{
				if(parseInt(SelDay)>28)
				{
					return false;
				}
			}
			else
			{
				if(parseInt(SelDay)>29)
				{
					return false;
				}
			}
		}
		return true;
	}
	

function isOnlyAlphaNumericSpaces1(string)
{

if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
}
	
	
function isOnlyAlphaNumericSpaces(fieldvalue)
{
	var str = fieldvalue;
	i = 0;

	while(i < str.length)
	{
	if ( str.charAt(i) == ' ' )
		alert("space");






	if(! ( ( (str.charAt(i) >= 'a') && (str.charAt(i) <= 'z')) || ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z')) ||((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))) || (str.charAt(i) != ' '))
		{
			alert(str.charAt(i));
			return false;
		}
		i++;
	}
	return true;
}

function showSampleTicket()
{
	var win;
	if(win==null)
	{
		var myBars='directories=no,location=no,menubar=no,status=yes,resizeable=yes,scrollbars=yes,left=100,top=0,,width=550, height=500';
		win=open("SampleTicket.jsp","",myBars) //,"Sample Traffic Ticket",myBars);
	}
	if(win!=null && win.closed)
	{
		var myBars='directories=no,location=no,menubar=no,status=yes,resizeable=yes,scrollbars=yes,left=100,top=0,,width=550, height=500';
		win=open("SampleTicket.jsp","",myBars) //,"Sample Traffic Ticket",myBars);
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toUpper(obj){
	var name = obj.value;
	obj.value = name.toUpperCase();
}

function validateDate(e)
{
	var key;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	
	if ((key==47) || (key==48) || (key==49) || (key==50) || (key==51) || (key==52) || (key==53) || (key==54) || (key==55)  || (key==56)  || (key==57))
	   return true;
	
	if (key>127)
		return true;
	else
	   return false;
}

//Validation for field which should not be empty
function isEmpty2(fieldname,fieldvalue)
{
	
	var str=checkTrim(fieldvalue);
	if(str.length==0)
	{
		return true;
	}
	return false;
}

//returns the string after deleting the leading and trailing spaces

function checkTrim(txtString)
{
	txtString = LTrim(txtString);
	txtString = RTrim(txtString);
	return txtString;
}

//returns the string after deleting  the trailing spaces
function LTrim(txtString) 
{
	ctr = 0;
	while( ctr < txtString.length && (txtString.substring(ctr,ctr+1) == " "))
	{
		ctr=ctr+1;
	}
	return txtString.substring(ctr);
}

// returns the string after deleting the leading spaces
function RTrim(txtString) 
{
	ctr = txtString.length;
	while( ctr > 0  && (txtString.substring(ctr,ctr-1) == " "))
	{
		ctr = ctr - 1;
	}
	return txtString.substring(0,ctr);
}

//return false if it has invalid characters(validation for invalid characters)
function hasInvalidChar(fieldvalue,charToMatch)
{

	var str=fieldvalue;
	//var valid=true;
	var str=checkTrim(str)
	var chars=charToMatch;//invalid characters
	for(i=0;i<str.length;i++)
	{
		for(j=0;j<chars.length;j++)
		{
		
			if(str.charAt(i)==chars.charAt(j))
			{
				return true;
			}	
		}
	}
	
	return false;
}	

/*
function formatDate(field){
		
	var fieldValue = field.value;

	if( !isEmpty2( "Inactive Date", fieldValue ) ){
		if( !hasInvalidChar(fieldValue,"/") ){
			if( fieldValue.length == 6 ){
				var newValue = fieldValue.substring(0,2)+"/"+fieldValue.substring(2,4)+"/19"+fieldValue.substring(4,6);
				field.value = newValue;
			}
			else if ( fieldValue.length == 8 ) {
				var newValue = fieldValue.substring(0,2)+"/"+fieldValue.substring(2,4)+"/"+fieldValue.substring(4,8);
				field.value = newValue;
			}//end of else if
		}//has valid characters		
		else  if ( (fieldValue.length == 8) && (fieldValue.indexOf('/') > 0)) {				
				var newValue = fieldValue.substring(0,2)+"/"+fieldValue.substring(3,5)+"/19" + fieldValue.substring(6,8);
				field.value = newValue;
			}
	}//end of if - not empty
}//end of function - formatDate
*/

function formatDate(field)
{
 var emptyStringRegEx = /^\s*$/ ;
 if (emptyStringRegEx.test(field.value))
  return;
 var fieldvalue = field.value.replace(/\s/g,'');
 var dateFormatRegEx1 = /^\d+\/\d+\/\d+$/; 
 var isValidDate = true;
 var month, year, date;
 var message;
 if (dateFormatRegEx1.test(fieldvalue)){
  var array = fieldvalue.split('/');
  if (array.length == 3){
   month = array[0];
   if (month.length == 1){
    month = "0" + month;
   }else if (month.length != 2){
    isValidDate = false;
    //message = "Invalid Month";
   }   
   date = array[1];
   if (date.length == 1){
    date = "0" + date;
   }else if (date.length != 2){
    isValidDate = false;
    //message = "Invalid Date";
   }
   year = array[2];
   if (year.length == 2){    
    year = "19" + year;
   }else if (year.length != 4){
    isValidDate = false;
    //message = "Invalida Year";
   }
  }else{
   isValidDate = false;
   //message = "No Three Elements in the date";
  }
 }else{
  isValidDate = false;
  //message = "Regular Expression does not match";
 }
 if (isValidDate){
  field.value = month + "/" + date + "/" + year;
 }else{  
  alert("Please enter the birth date in the format MM/DD/YYYY ");
  field.value = "";
  field.focus();
 } 
}

function convertcasenumber(field){
	
	var emptyStringRegEx = /^\s*$/ ;
	if (emptyStringRegEx.test(field.value))
		return;
		
	var casetyperegex1 = /^\d{2}\s*[a-zA-Z][a-zA-Z]\s*\d+$/;	
	var casetyperegex2 = /^\d{2}\s*[a-zA-Z]\s*\d+$/;
	var casetyperegex3 = /^\d{4}\s*[a-zA-Z][a-zA-Z]\s*\d+$/;
	var casetyperegex4 = /^\d{4}\s*[a-zA-Z]\s*\d+$/;

	var caseNumber = field.value.replace(/\s/g,'');
	var caseyear;
	var casetype;
	var caseseqno;
	if (casetyperegex1.test(caseNumber)) // YY TT NNNN format
	{
		caseyear = caseNumber.substr(0,2);
		casetype = caseNumber.substr(2,2);
		caseseqno = caseNumber.substr(4,caseNumber.length);
		if (caseyear < 30)
		{
			caseyear = '20' + caseyear;
		}
		else
		{
			caseyear = '19' + caseyear;		
		}		
	}
	else if (casetyperegex2.test(caseNumber))// YY T NNNN format
	{
		caseyear = caseNumber.substr(0,2);
		casetype = caseNumber.substr(2,1) + ' ';
		caseseqno = caseNumber.substr(3,caseNumber.length);
		if (caseyear < 30)
		{
			caseyear = '20' + caseyear;
		}
		else
		{
			caseyear = '19' + caseyear;		
		}		
	}
	else if (casetyperegex3.test(caseNumber))// YYYY TT NNNN format
	{
		caseyear = caseNumber.substr(0,4);
		casetype = caseNumber.substr(4,2);
		caseseqno = caseNumber.substr(6,caseNumber.length);
	}
	else if (casetyperegex4.test(caseNumber))// YYYY T NNNN format
	{
		caseyear = caseNumber.substr(0,4);
		casetype = caseNumber.substr(4,1) + ' ';
		caseseqno = caseNumber.substr(5,caseNumber.length);		
	}
	else //INVALID FORMAT
	{
		field.focus();	
		return false;
	}	
	
	if (caseseqno.length > 6)
	{
		return false;
	}
	
	field.value = caseyear + casetype.toUpperCase() + getFormattedSeqNo(caseseqno);
	return true;
	
	
	
		
/*	var casenumber = field.value;
	
	var str = checkTrim(casenumber);
	if( str.length == 0 ){
		return false;
	}
	   
	var i,before,middle,after,i1;
	i = 0;

	while(i < casenumber.length)
	{
		if((((casenumber.charAt(i) >= 'a') && (casenumber.charAt(i) <= 'z'))||((casenumber.charAt(i) >= 'A') && (casenumber.charAt(i) <= 'Z'))))
		{
			break;			
		}
		i++;
	}
	
	if(i > 4 )
	{
		return false;
	}
	
	var j;
	before ="";
	after="";
	middle="";
	
	for(j=0;j < i ;j++)
	{
		before = before + casenumber.charAt(j);
	}
	
	if((casenumber.charAt(j+1) >= 'a' &&  casenumber.charAt(j+1) <= 'z') || (casenumber.charAt(j+1) >= 'A' &&  casenumber.charAt(j+1) <= 'Z'))
	{
		middle = middle + casenumber.charAt(j) +casenumber.charAt(j+1);	
		j = j+2;
	}
	else
	{
		middle = middle + casenumber.charAt(j)+ " " ;
		j=j+1;
	}
	
	for(i = j ; i < casenumber.length ;i++)
	{
		after = after + casenumber.charAt(i);
	}


	for(i=after.length;i < 6;i++)
	{
		after = "0"+after;
	}	

	//here middle denotes to the case type. Case type can be of two characters. So adding space to behind the middle if case type equals to one helps to build the proper case number
	//The new logic is if the year is between 0-9 then precede 20 and year is between 10-99 then precede 19
	if (before.length == 2 )
	{
		var year=parseInt(before);
		if (year >=0 && year <= 9)
			before="20"+before;
		else
			before="19"+before;
	}

	field.value = before + middle + after;

	return true;
*/	
}



function convertcase(fieldvalue){
	
	var casenumber = fieldvalue;
	
	var str = checkTrim(casenumber);
	if( str.length == 0 ){
		return '';
	}
	   
	var i,before,middle,after,i1;
	i = 0;

	while(i < casenumber.length)
	{
		if((((casenumber.charAt(i) >= 'a') && (casenumber.charAt(i) <= 'z'))||((casenumber.charAt(i) >= 'A') && (casenumber.charAt(i) <= 'Z'))))
		{
			break;			
		}
		i++;
	}
	
	if(i > 4 )
	{
		return fieldvalue;
	}
	
	var j;
	before ="";
	after="";
	middle="";
	
	for(j=0;j < i ;j++)
	{
		before = before + casenumber.charAt(j);
	}
	
	if((casenumber.charAt(j+1) >= 'a' &&  casenumber.charAt(j+1) <= 'z') || (casenumber.charAt(j+1) >= 'A' &&  casenumber.charAt(j+1) <= 'Z'))
	{
		middle = middle + casenumber.charAt(j) +casenumber.charAt(j+1);	
		j = j+2;
	}
	else
	{
		middle = middle + casenumber.charAt(j)+ " " ;
		j=j+1;
	}
	
	for(i = j ; i < casenumber.length ;i++)
	{
		after = after + casenumber.charAt(i);
	}


	for(i=after.length;i < 6;i++)
	{
		after = "0"+after;
	}	

	//here middle denotes to the case type. Case type can be of two characters. So adding space to behind the middle if case type equals to one helps to build the proper case number
	//The new logic is if the year is between 0-9 then precede 20 and year is between 10-99 then precede 19
	if (before.length == 2 )
	{
		var year=parseInt(before);
		if (year >=0 && year <= 9)
			before="20"+before;
		else
			before="19"+before;
	}

	newvalue = before + middle + after;

	return newvalue;
}

//Validation for field which can allow numbers and single dot
function isValidValue(fieldname , fieldvalue)
{
	

	var str = fieldvalue;
	i = 0;
	doti =0;
	while(i < str.length)
	{
		
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")||(str.charAt(i)==".")))
		{
			alert('Please enter valid value for '+fieldname);
			return false;
		}
		if(str.charAt(i)==".")
		{
			doti++;
			if(doti > 1)
			{
				alert('Please enter valid value for '+fieldname);				
				return false;
			}	
		}	
				
			
		
		i++;
	}
	
	return true;
}

function getFormattedSeqNo(obj)
{
	var retVal= '';
	for (i = obj.length; i < 6; i++)
	{
		retVal = '0' + retVal;
	}
	return retVal + obj;	
}
function getFormattedZipCode(fieldvalue)
{
	var str = checkTrim(fieldvalue);
	var zipCode=new Array(2); 
	zipCode[0] = "";
	zipCode[1] = "";
	
	if(str.length == 0){
		return zipCode;
	}
	
	if(str.indexOf("-")>-1){		
		zipCode[0]=str.split("-")[0];
		zipCode[1]=str.split("-")[1];
	}else if(str.length>5){
		zipCode[0]=str.substring(0,5);
		zipCode[1]=str.substring(5,str.length);
	}else{
		zipCode[0]=str;
		zipCode[1]="";
	}
	return zipCode;
}