//****************************************************************************
// Function to delete blank/white spaces Between Data Entered
//****************************************************************************
function deleteBlanks(entry)
{
	var len = entry.length ;
	var foundBlank = 1;
	while(foundBlank == 1 && len > 0) 
	{
		var indx = entry.indexOf(" ");
		if(indx == -1) 
			foundBlank = 0 ;
		else
			entry = entry.substring(0,indx) + entry.substring(indx+1,len);
		len = entry.length;
	}
	return entry;
}



//****************************************************************************
// Function To Check Entered Email Is Containing Any Special Characters or Not
//****************************************************************************
function emailCheck (emailStr) {
	femailStr= emailStr;
	emailStr = emailStr.value;

/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/

/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]$&#?"

/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"

/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"

/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'

/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* Finally, let's start trying to figure out if the supplied address is
   valid. */
/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)

if (matchArray==null)
{

  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Invalid E-mail.")
	femailStr.focus();
	return false
}

var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) 
{
    // user is not valid
    alert("Invalid E-mail. The email address doesn't seem to be valid.")
	femailStr.focus();
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) 
{
    // this is an IP address
	  for (var i=1;i<=4;i++) 
	  {
	    if (IPArray[i]>255) 
		{
	        alert("Invalid E-mail. Destination IP address is invalid!")
			return false
	    }

    }

    return true

}



// Domain is symbolic name

var domainArray=domain.match(domainPat)

if (domainArray==null) {

	alert("Invalid E-mail. The domain name doesn't seem to be valid.")
	femailStr.focus();
    return false
}



/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */
/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */

var atomPat=new RegExp(atom,"g")

var domArr=domain.match(atomPat)

var len=domArr.length

if (domArr[domArr.length-1].length<2 || 

    domArr[domArr.length-1].length>3) {

   // the address must end in a two letter or three letter word.

   alert("Invalid E-mail. The E-mail must end in a three-letter domain, or two letter country.")
   femailStr.focus();
   return false;
}



// Make sure there's a host name preceding the domain.

if (len<2) {

   var errStr="Invalid E-mail. This address is missing a hostname!"
   alert(errStr)
	femailStr.focus();
   return false
}



// If we've gotten this far, everything's valid!

    var domainarr = emailStr.split("@");
	var tottaldot = domainarr[1].split(".");
	if(tottaldot.length>4) {
		alert("The email address doesn't seem to be valid.");
		return false; 
	}
    return true;

}

//  End -->



//****************************************************************************

// Function To Check Text box is empty or not

//****************************************************************************


function isEmpty(val,valName)

{

	//alert(val);

	if(!deleteBlanks(val.value))

	{

		alert( pleaseenter + valName);
		
		val.focus();

		return false;	

	}

	return true;

}

//****************************************************************************

// Function To Check Telephone Number Is Valid or Not

//****************************************************************************

function checktel(tel,valname)

{

	val1=tel.value;

	if (val1!="")

	{

		for (var i=0;i<val1.length;i++)

		{

			var val = val1.charAt(i);

			if ((val1.length<12 ) || (i!=7 && i!=3 && (val<"0" || val>"9") ) || ( (i==3 || i==7) && val!="-" ))

			{

				alert(pleaseenter+ valname + " in the format 999-999-9999");
				
				tel.focus();

				tel.select();

				return false;

			}

		}

	}

	return true;

}



//****************************************************************************

// Function To Check Entered Date Is Valid or Not

//****************************************************************************

/*function isDate(sdd,smm,syy)

{

	day=sdd[sdd.selectedIndex].value;

	month=smm[smm.selectedIndex].value;

	year=syy[syy.selectedIndex].value;



	errflag=0;

	if (day==0 || month==0 || year==0 || year=='')

		errflag=1;

	if (errflag==0)

	{

		switch(month)

		{

    	  case '2':

	 			leap=year%4;

 				if(leap>0 && day>28)

					errflag=1;

				else if (day>29)

					errflag=1;

				break;

		  case '4':case '6':case '9':case '11':

				if(day>30)

					errflag=1;

		}

	}

	if(errflag==1)

	{

		alert(validdatemsg)

		smm.focus();

		return false;

	}

	return true;

}

//****************************************************************************

// Function To Check Entered Date Is Valid or Not
//	If the date drop down used is in smarty ,to validate the date
// Differences from isDate function. The value of month select box is for Jan 1 for Feb 2 
// In smarty these are Jan for Jan feb for Feb (first three letters of the month)	

//****************************************************************************


function islte(day1,month1,year1,day2,month2,year2,valName1,valName2)

{

	dd1=day1[day1.selectedIndex].value;

	mm1=month1[month1.selectedIndex].value;

	yy1=year1[year1.selectedIndex].value;

	if (!isDate(day1,month1,year1,valName1))

		return false;



	dd2=day2;

	mm2=month2;

	yy2=year2;



	if(dd1<10)

		dd1="0"+dd1;

	if(mm1<10)

		mm1="0"+mm1;

	if(dd2<10)

		dd2="0"+dd2;

	if(mm2<10)

		mm2="0"+mm2;

	

	if((yy1+"-"+mm1+"-"+dd1) > (yy2+"-"+mm2+"-"+dd2))

	{

		alert(valName1 + " can only be on or before " + valName2)
		month1.focus();
		return false;
	}

	return true;

}
*/


function isTelephone(val1,val2,val3,valName)

{

   

	inv=0;

	v=val1.value+val2.value+val3.value;

	if (v!="")

	{

		if (v.length<10)

			inv=1;

		for (var i=0;i<v.length && inv==0;i++)

		{

			if ( v.charAt(i)<"0" || v.charAt(i)>"9")

				inv=1;

		}



		if (inv==1)

		{

			alert (valName + invalidmsg)

			val1.select();

			val1.focus();

			

			return false;

		}

	}

	return true;

}


/*to check the URL*/
/*function isUrl(val)

{

	theString = val.value;

	if (isEmpty(val,"<?php echo $g_url_label;?>"))
	{

//		var FirstAtTheRate = theString.indexOf("http://www.");

		var StartsWith = theString.indexOf("http://");

		var Contains = theString.indexOf("//");

		var FirstAtTheRate = theString.indexOf("www.");

		var FirstPeriod = theString.indexOf(".");

		var LastPeriod = theString.lastIndexOf(".");

		var illegalChars= /[\(\)\<\>\,\;\#\$\@\^\*\[\]]/;

		var lengthip=val.value.length;

		var validaip="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:/";

		var ipalpha=false;



	    for (var i = 0; i < lengthip; i++) 

		{

			if (validaip.indexOf(val.value.charAt(i)) != -1)

			{

				if (val.value.match(illegalChars)) 

				{

					ipalpha = false;

				}

				else

				 	ipalpha = true;

			}

			else 

				ipalpha =false; 

		}



		var j=0;

		for (i=0;i<val.value.length;i++)

		{

			if ((val.value.charAt(i)) == ".")

			{

				if ((val.value.charAt(i+1)) == ".")

					j=1;

			}

		}



		if(ipalpha && j==0)

		{

			if(Contains >= 0 || StartsWith == 0 || FirstAtTheRate == -1 || FirstPeriod <= 0 || LastPeriod<=1  || LastPeriod == theString.length-1 ||	val.value.charAt(FirstPeriod+1) == "." || val.value.charAt(LastPeriod-1) == "." || LastPeriod == FirstPeriod)

			{

				alert("<?php echo $g_invalid_lable.' '.$g_url_label;?>");

				val.focus();

				val.select();

				return false;	

			}	

		}

		else

		{		

			val.focus();
			val.select();

			return false;	

		}

	}

	return true;

}
*/


/* to check numbers*/
function isNum(val, valName)
{	
	if(isNaN(val.value))
	{

		alert(valName + numericvaluemsg);
		val.select();
		val.focus();
		return false;

	}

	if(val.value<0)

	{

		alert(valName + notnegativemsg);

		val.select();

		val.focus();

		return false;

	}	

return 	true;

}

/* Function to check for decimal value in zip */
function isZip(frmElement) {
	var str = frmElement.value;
	for(var i=0; i < str.length; i++) {
		if(str.charAt(i) == '.') {
			alert(invalidzipmsg);
			frmElement.select();
			frmElement.focus();
			return false;
		}
	}
	return true;	
}

//****************************************************************************
// Function To get user confiremation for delete
//****************************************************************************

/*function confirmUserDelete(msg) {   

   if (confirm("Are you sure you want to delete " + msg +" permanently?"))
	//if (confirm("<?php echo $g_confirmdelete_msg;?> " + msg +" <?php echo $g_permanently_lable;?>"))
		return true;
	else
		return false;
}// end of function
*/


//****************************************************************************
// Function allow a-z ,0-9 and _ only in a testbox --  Added by vineet
// object is accepted as an  parameter 
//****************************************************************************


/*function checkTestbox(val) {

myRegExp = new RegExp("[^a-z,0-9,_]", "i"); 

res=myRegExp.test(val.value);


 if(res)
{

		alert(onlyalphamsg);
		val.focus();
		val.select();
		return false;
}
return true;

}

//****************************************************************************
// Function To Open page in new window (Bigger Window)
//****************************************************************************
function NewWindowView(mypage, myname, w, h, scroll)
{
	var winl = (screen.width - w) / 4 ;
	var wint = (screen.height - h)/4 ;
	winprops = 'height='+h+',width='+w+',scrollbars=yes,top='+wint+',left='+winl+''
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//****************************************************************************
// Function allows only character values to be entered from a-z and A-Z
//****************************************************************************
function characters1(str,str1)
{
 if (str == "")
	{
		alert("Please specify "+str1);
		
		return false;
	}
	
	for (var i = 0; i < str.length; i++) 
	  {
		var ch = str.substring(i, i + 1);
		if ( (ch <"a" || ch>"z") && (ch <"A" || ch>"Z")  && (ch!="'")) 
		{	
			alert("Numeric values and special characters not allowed in "+str1);
			return false;
		}
     }
     
     return true;
     
}	


function ValidateEmail(email)
{
email.value=removespaces(email.value);
  if(email.value=="")
	{
		alert(specifyemailmsg);
		email.focus();
		return false;
	}
		
	else
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))
		{

		}
		else
		{
			alert(invalidemailmsg);
			email.focus();
			email.select();
			return false;
		}
		
	}
		var str=email.value;
		var flag=false;	
		var j=0;
		for(i=0;i< str.length;i++)
		{
		if(str.charAt(0)=="_" )
			{
			alert(emailbeginalphamsg);
			makeEmpty(email);
			email.focus();
			return false;
			}
		if(str.charAt(i)=="@" )	
		{
		flag=true;
		j=i-1;
		}
		if(flag==true && str.charAt(i)=="_")
		{
		alert(invalidurlinemailmsg);
		email.focus();
		email.select();
		return false;
		}
		if(flag==true && str.charAt(j)=="_")
		{
		alert(invalidemailmsg);
		email.focus();
		email.select();
		return false;
		}
	   	}
	   	return true;
}
		
//****************************************************************************
// Function Trims the leading AND trailing Spaces --  Added by Mir
// object is accepted as an  parameter 
//****************************************************************************

function removespaces(test)
{ 
  size = test.length
 
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
  return test;
  }
  
  //Compares date
  
  function compareDates (theMonth,theDay,theYear,mal1,mal2,mal3) {

   var date1, date2;
   var month1, month2;
   var year1, year2;

   month1 = theMonth;
   date1 = theDay;
   year1 = theYear;


   month2 = mal1;
   date2 = mal2;
   year2 = mal3;


   if (year1 > year2) return 1;
  else if (year1 < year2) return -1;
   else if (month1 > month2) return 1;
  else if (month1 < month2) return -1;
   else if (date1 > date2) return 1;
   else if (date1 < date2) return -1;
   else return 0;
} 

//****************************************************************************
// Function allows only character values to be entered from a-z and A-Z in 
//otherthan name fields -pooja
//****************************************************************************
function characters2(str,str1)
{
 if (str == "")
	{
		alert("\nPlease Specify " +str1 );
		
		return false;
	}
	if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z"))
	{
		alert(str1+" should begin with an alphabet");
		return false;
	}
	for (var i = 1; i < str.length; i++) 
	  {
		var ch = str.substring(i, i + 1);
		if ( (ch <"a" || ch>"z") && (ch <"A" || ch>"Z")) 
		{
			alert("Numeric values and special characters not allowed in "+str1);
			return false;
		}
     }
     
     return true;
     
}	
*/
//alows only A-Z, 0-9 and spaces
/* Modified by Kedar on 06022004 */
function isValidText(frmElement, fieldName) {
	myRegExp = new RegExp("[^a-z,0-9,\\s]", "i"); 
	//alert(res);
	if(myRegExp.test(frmElement.value)) {
		alert(specialCharactersNotAllowedIn + fieldName);
		frmElement.focus();
		frmElement.select();
		retVal = false;
	}
	else {
		retVal = true;
	}
	return retVal;
}

/*function checkPassword(val,valname) {
	var passwordValue = val.value;
	// This is a set of characters other than these characters all r invalid 
	testPattern = /[^A-Z,0-9,_]/i;
	// It checks for the occurance of special cha
	if(!testPattern.test(passwordValue)) {
		blnErrorFound = false;
	}
	else {
		blnErrorFound = true;
	}
	if (blnErrorFound==true) {
		msg=valname+" cannot contain special characters"
		alert(msg);
	}
	return blnErrorFound;
}
*/
/* Fucntion to check the difference between two dates */
/*function checkDateDiff(startDate, endDate){
	return Date.parse(startDate) >= Date.parse(endDate) ? false : true;
}
function checkTodayDateDiff(startDate, endDate){
	return Date.parse(startDate) > Date.parse(endDate) ? false : true;
}*/
/* Fucntion to check whethere start date is same or greater than end date */
/*function checkSameOrLessDate(startDate, endDate) {
	return Date.parse(startDate) >= Date.parse(endDate) ? false : true;
}
*/
/* Fucntion to check whethere start date is same as end date */
/*function checkSameDate(startDate, endDate) {
	return Date.parse(startDate) == Date.parse(endDate) ? false : true;
}

// Function to check for valid User Name
function isValidUserName(userNameObj, userNameField) {
	while(1) {
		var retVal = false;
		
		// Check for spaces in the User Name
		if (userNameObj.value != deleteBlanks(userNameObj.value)) {
			alert(theUserNameShouldNotHaveSpaces);
			userNameObj.focus();
			userNameObj.select();
			break;
		}
		
		// Checks for blank User Name
		if(!isEmpty(userNameObj, userNameField)) {
			break;
		}
		else {
			var userNameValue = userNameObj.value;

			// This is a set of valid characters 
			var testPattern = /[^A-Z,0-9,_]/i;

			// It checks for the occurance of special characters
			if(!testPattern.test(userNameValue)) {
				blnErrorFound = false;
			}
			else {
				blnErrorFound = true;
			}
			if (blnErrorFound == true) {
				alert(userNameCannotContainSpecialCharacters);
				userNameObj.focus();
				break;
			}
			
			// To test if User Name starts with number.
			testPattern = /^\D/i;
			if(!testPattern.test(userNameValue)) {
				alert(userNameCannotStartWithNumber);
				userNameObj.focus();
				break;
			}
			
			// Check for length of User Name. It should be between 4 - 10
			if(userNameValue.length < 4 || userNameValue.length > 10) {
				alert(userNameMustBeBetweeb4to10Characters);
				userNameObj.select();
				userNameObj.focus();
				break;
			}
		}
		retVal = true;
		break;
	}
	return retVal;
}
*/
/* To check whole numbers */
/*function isWholeNum(frmElement, fieldName) {	
	var retVal = false;
	while(1) {
		if(isNaN(frmElement.value)) {
			alert(fieldName + numericvaluemsg);
			frmElement.select();
			frmElement.focus();
			break;
		}
		if(frmElement.value < 0) {
			alert(valName + notnegativemsg);
			frmElement.select();
			frmElement.focus();
			break;
		}
		if(parseInt(frmElement.value) != parseFloat(frmElement.value)) {
			alert(fieldName + isNotAWholeNumber);
			frmElement.select();
			frmElement.focus();
			break;
		}
		retVal = true;
		break;
	}
	return retVal;
}
*?
/* Check whethere number is in proper number format - 12345.12 */
/*function isValidDecimalNumber(fmElement, separator, fieldName) {
	var retVal = true;
	var num = fmElement.value;
	re = /[^0-9,\.]/;
	if(re.test(num)) {
		alert(fieldName + " " + numericvaluemsg);
		fmElement.focus();
		fmElement.select();
		retVal = false;
	}
	else {
		if (separator == ".") {
			re = /^([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)$/;
		}
		else {
			re = /^([0-9]*,?[0-9]+|[0-9]+,?[0-9]*)$/;
		}
		if(!re.test(num)) {
			alert(invalidNumberAtBegin + " '" + separator + "'");
			fmElement.focus();
			fmElement.select();
			retVal = false;
		}
		else {
			if (separator == ".") {
				re = /^\d{1,5}(\.\d{1,2})?$/;
			}
			else {
				re = /^\d{1,5}(,\d{1,2})?$/;
			}
			if(!re.test(num)) {
				alert(invalidNumberFormat);
				fmElement.select();
				fmElement.focus();
				retVal = false;
			}
		}
	}
	return retVal;
}
*/
/* Function to check for valid password */
/*function isValidPassword(passwordObj, passwordField) {
	while(1) {
		var retVal = false;
		
		// Check for spaces in the Password
		if (passwordObj.value != deleteBlanks(passwordObj.value)) {
			alert(passwordShouldNotHaveSpaces);
			passwordObj.focus();
			passwordObj.select();
			break;
		}

		// Checks for blank Password
		if(!isEmpty(passwordObj, passwordField)) {
			break;
		}
		else {
			var passwordValue = passwordObj.value;

			// This is a set of valid set of characters for Password 
			testPattern = /[^A-Z,0-9,_]/i;
			
			// It checks for the occurance of special character.
			if(!testPattern.test(passwordValue)) {
				blnErrorFound = false;
			}
			else {
				blnErrorFound = true;
			}
			if (blnErrorFound == true) {
				alert(passwordCannotContainSpecialCharacters);
				passwordObj.focus();
				break;
			}
			
			// Check for length of password. It should be between 4 - 10
			if(passwordValue.length < minPasswordLength || passwordValue.length > maxPasswordLength) {
				alert(passwordMustBeBetweenXToYCharacters);
				passwordObj.select();
				passwordObj.focus();
				break;
			}
		}
		retVal = true;
		break;
	}
	return retVal;
}
*/
//Function using to enter numbers only
	function numbersonly(e, decimal) {
		var key;
		var keychar;
	  
		 if (window.event) {
			key = window.event.keyCode;
		  }
		  else if (e) {
			 key = e.which;
		  }
		  else {
			 return true;
		  }
		  
		  keychar = String.fromCharCode(key);
	  
		  if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
			 return true;
		  }
		  else if ((("0123456789").indexOf(keychar) > -1)) {
			 return true;
		  }
		  else if (decimal && (keychar == ".")) { 
			return true;
		  }
		  else
			 return false;
	}

   function trim_text(str,controlname,frm)
   {
	str = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
	cname = eval("document.forms[frm]."+controlname);
	cname.value=str;
	return cname;
   }

function checkAlphaChar(strval) {
	var retVal = true;
	var strval = strval;
	myRegExp = new RegExp("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$", "i"); 
	if(myRegExp.test(strval)) {
		retVal = false;
	} else {
		myRegExp = new RegExp("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$", "i"); 
		if(myRegExp.test(strval)) {
			retVal = false;
		}
	}
	/*Here checking the field contains only alpha characters*/
   if (!strval) return false;
   var iChars = "!*|,\":<>[]{}.`;()@&$#%^+-_=?/'%~'\\1234567890";

   for (var i = 0; i < strval.length; i++) {
	  if (iChars.indexOf(strval.charAt(i)) != -1)
		 return false;
   }
	/*Checking the value is blank*/
	var len = strval.length ;
	var foundBlank = 1;
	while(foundBlank == 1 && len > 0) 
	{
		var indx = strval.indexOf(" ");
		if(indx == -1) 
			foundBlank = 0 ;
		else
			strval = strval.substring(0,indx) + strval.substring(indx+1,len);
		len = strval.length;
	}
	retVal = len;
	return retVal;
}