/*Functions and their functionalities---------
0. trim (inputStringTrim)
	This function is for triming the blank spaces.
1. fvalidateBlank(ct1,msg)
	This function is for checking the blank spaces or null values.
2. fvalidateNumericZero(ctl,msg)
	 This function to Validate Numeric Fields where Zero Values should not be allowed
3. femailCheck (emailString)
	This function is for email Validation
4. fcheckLength(ctl,msg,len)
	This function is for checking the length of a field
5. fdatevalidation(arg,msg) 
	This function is for validating the date field
6. fcheck_others(cmbtemp,cmbtext,cmbtitle,otherval)
	This function is for checking values entered other than the given combo values
7. function fcheckchkrbo(temptext,chkrbotemp,msg1,msg2)	
	This function is for checking respective TextFields if corresponding Checkboxes or Radiobuttons are checked
8. function fCompareDate(date1,date2,msg1,msg2)
	This function is for comparing two date fields
9. fvalidateString(ctl,msg)
	This function is for checking the Alphanumeric Values
10. function fvalidateNumeric(ctl,msg)
	This function is to Validate Numeric Fields. 
11. function fcheckAge(TempAge,Msg)
	This function is to check whether age field is numeric or not, if it is not blank
12. function fcheckSequenceNo(TempAge,Msg)
    This function is to check format of Sequence Number, if it is not Blank
13.function fcheckURL(tempURL)
	This Function is to check Valid URL
14.	function checkBoxKit(temp1,temp2)
	 This function to check whether both Box and kit are selected or not
15.	function fchkCardiffSequenceNo(TempAge,Ctype,Msg)
	 This function to check format of Sequence Number, if it is not Blank
16.	function checkMgmt(temp1,temp2)
	 This function to check whether both Box and kit are selected or not
17. function fcheckfldlength (temp1,Msg)
	  This function to check the length of a textarea
34. function fCompareString (temp1, temp2, Msg1, Msg2)
	
	  
*/
/***********************************************************************************************/
//0.  function to trim the blank spaces

function trim ( inputStringTrim ) {
fixedTrim = "";
lastCh = " ";
for (x=0; x < inputStringTrim.length; x++) {
ch = inputStringTrim.charAt(x);
if ((ch != " ") || (lastCh != " ")) { fixedTrim += ch; }
lastCh = ch;
}
if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1); }
return fixedTrim
}



/***********************************************************************************************/
//1.  function to validate blank space
function fvalidateBlank(ctl,msg)
{   
	
	//if(ctl.value =="")
	if(trim(ctl.value) == "")
		{
			alert(msg+" cannot be blank");
			ctl.value = "";
			ctl.focus();
			return false;
		}
	else
	{
	  return true;
	}
	
}


/***********************************************************************************************/
//2.  function to Validate Numeric Fields where Zero Values should not be allowed
function fvalidateNumericZero(ctl,msg)
{
	if(isNaN(ctl.value) || ctl.value == 0 || ctl.value <  0)
	{
			if (ctl.value == 0)
			{
				alert(msg+" cannot be Zero");
				ctl.value = "";
				ctl.focus();
			}
			if (ctl.value < 0)
			{
				alert(msg+" cannot be Negative");
				ctl.value = "";
				ctl.focus();
			}		
			else
			{			
			alert(msg+" should be a Number");
			ctl.value = "";
			ctl.focus();
			}
			
			return false;
		}
	
		
}

/***********************************************************************************************/
//3.    function to validate Email Ids

function femailCheck (emailString)
{
	emailStr = emailString.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("Email Id seems incorrect (e.g. abc@abc.com)")
		emailString.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("The username in Email Id doesn't seem to be valid.")
	    emailString.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("Destination IP address in Email Id is invalid!")
		       emailString.focus()
			return false
		    }
	    }
	    return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name in Email Id doesn't seem to be valid.")
		emailString.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("The Email Id must end in a three-letter domain, or two letter country.")
	   emailString.focus()
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This Email Id is missing a hostname!"
	   alert(errStr)
	   emailString.focus()
	   return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

/***********************************************************************************************/
//4. To check the length of the field

function fcheckLength(ctl,msg,len)
{
     if(ctl.value.length < len)
		{
			alert(msg+" should be of Minimum" +len +" digits");
			ctl.value = "";
			ctl.focus();
			return false;
		}
		
}

/***********************************************************************************************/
//5. Function for date validation

function fdatevalidation(arg1, arg2, arg3, msg, msg1, msg2, msg3) 
{
	var retval;

	dopmonth = arg1.value
	dopday = arg2.value
	dopyear = arg3.value
	
	var n=parseInt(dopmonth,10);
	var n1=parseInt(dopday,10);
	var n2=parseInt(dopyear,10);
	var now=new Date();
	var dtYYYY=now.getYear();

		
	var now=new Date();
	var dtYYYY=parseInt(now.getYear(),10);
	var dtMonth=parseInt(now.getMonth(),10)+1;
	var dtDay=parseInt(now.getDate(),10);

	retval=fvalidateBlank(arg2,msg2)
	if(retval!=false)retval=fvalidateBlank(arg1,msg1)
	if(retval!=false)retval=fvalidateBlank(arg3,msg3)

	if(retval!=false)
	{
		if(n2>dtYYYY)
		{
			alert(msg+" cannot exceed Current Date");
			arg3.focus();
			return false;
		}
		else
		{
			if((n>dtMonth)&& (n2==dtYYYY))
			{
				alert(msg+" cannot exceed Current Date");
				arg1.focus();
				return false;
			}		
			else
			{	
				if((n1>dtDay) && (n==dtMonth) && (n2==dtYYYY))
				{
					alert(msg+" cannot exceed Current Date");
					arg2.focus();
					return false;
				}
			}
		}
	}
	
	if(retval!=false)retval = fCheckDate(arg1, arg2, arg3, msg1, msg2, msg3) 
	if(retval!=false)		
		return true;
	else
		return false;
	
}
//*******************************************************************************************
//6. Function for checking format of date

function fCheckDate(arg1, arg2, arg3, msg1, msg2, msg3) 
{
	var dopmonth=arg1.value
	var dopday=arg2.value
	var dopyear=arg3.value

	var n=parseInt(dopmonth,10);
	var n1=parseInt(dopday,10);
	var n2=parseInt(dopyear,10);
	var now=new Date();
	var dtYYYY=now.getYear();
	
	if (n2 < 1900)
	{
		alert("Invalid Year, Year should be greater than 1800");
		arg3.focus();
		return false;
	}	


	if (fCheckDay(n,n1,n2) == false)
		{
			arg2.focus();
			return false;
		}
		
}

//*******************************************************************************************
//7. Function to checking day for particular month
function fCheckDay(mm,dd,yy)
{
	if ((dd > 31) || (dd < 1))
	{
		alert("Invalid Day, day should not exceeds 30 or 31");
		return false;
	}
	else
	{
		if (((mm == 04) || (mm == 06) || (mm == 09) || (mm == 11)) && (dd > 30))
		{  						
			alert("Invalid Day, day should not exceeds 30");
			return false;
		}
		else 
			if ((mm == 2) && (dd >= 29))
			{
					if ( ((yy % 4) == 0) && 
						 ((yy % 100) != 0) || ((yy % 400 == 0))
						&& (dd == 29))
					{ }
					else
					{		
						alert("Invalid Day, day should not exceeds 28");
						return false;
					}
			} 
	}
}

/***********************************************************************************************/
//9. Function for checking the respective TextFields if corresponding Checkboxes or Radiobuttons are checked
function fcheckchkrbo(temptext,chkrbotemp,msg1,msg2)
{
    
      
       if((chkrbotemp.checked == true) && (temptext.value == ""))
       {      	
			alert("If you have selected " + msg1 + ", Enter " + msg2)
			temptext.focus();            
			return false;
       }
} 

/***********************************************************************************************/
//10. This function will compare Two Dates
function fCompareDate(arg,arg1,msg,msg1)
{
	
	dop=arg.value;
	dopmonth=dop.substring(0,2)
	dopday=dop.substring(3,5)
	dopyear=dop.substring(6,10)	

	var n=parseInt(dopmonth,10);
	var n1=parseInt(dopday,10);
	var n2=parseInt(dopyear,10);

	dop1=arg1.value;
	dopmonth1=dop1.substring(0,2)	
	dopday1=dop1.substring(3,5)	
	dopyear1=dop1.substring(6,10)	
	
	var mn=parseInt(dopmonth1,10);
	var mn1=parseInt(dopday1,10);
	var mn2=parseInt(dopyear1,10);

	if(n2>mn2)
	{
		alert(msg+" cannot exceed "+msg1);
		//arg.focus();
		return false;
	}
	else
	{
		if((n>mn)&& (n2==mn2))
		{
			alert(msg+" cannot exceed "+msg1);
			//arg.focus();
			return false;

		}		
		else
		{	
			if((n1>mn1) && (n==mn) && (n2==mn2))
			{
				alert(msg+" cannot exceed "+msg1);
				//arg.focus();
				return false;
			}
		}
	}

}

/***********************************************************************************************/
//11.

function fvalidateString(ctl,msg)
{
   var checkch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
   var checkStr = ctl.value;
   
   var allValid = true;
   for (i = 0;i < checkStr.length;  i++)
   {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkch.length;  j++)
		if (ch == checkch.charAt(j))
			break;
		if (j == checkch.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		alert( msg + " should be a String");
		ctl.value="";
		ctl.focus();
		return (false);
	}
}

/***********************************************************************************************/
//12. function to Validate Numeric Fields 
function fvalidateNumeric(ctl,msg)
{
	if(isNaN(ctl.value))
	{			
		alert(msg+" should be a Number");
		ctl.value = "";
		ctl.focus();
		return false;
	}
	else if (ctl.value.indexOf('.',0) != -1) 
	{
		alert("'.' not allowed in "+msg+"\n ");		
		ctl.focus();
		return false;
	}	
}

//*****************************************************************************************
//15. Function to check Valid URL
function fcheckURL(tempURL) {
 tempURL.value = tempURL.value.toLowerCase();	
 if ((tempURL.value.indexOf("http://",0) != 0) && (tempURL.value.indexOf("www.",0) != 0 && (tempURL.value.indexOf(" .com",0) != 0)))
  {
		alert("Please provide a valid URL")
		tempURL.focus();
		return (false);
	}
else 
		return (true);
}

//*****************************************************************************************
//18. Check for the Single Qoute 
function fkillQuotes(temp,Msg)
{

 if (( temp.value.indexOf("\'") >-1 ) || ( temp.value.indexOf("\"") >-1 ))
  {
        alert (" Single Quotes(') or Double Quotes(\") are  not allowed in "+Msg);
        temp.value ="";
        temp.focus();
        return false ;
    }
 else
   return true ;
}

//*****************************************************************************************
//19. Check for #, & and %
function fkillSpecialChars(temp,Msg)
{

 if (( temp.value.indexOf("#") >-1 ) || ( temp.value.indexOf("&") >-1 ) || ( temp.value.indexOf("%") >-1 ))
  {
        alert ("#, & and % are  not allowed in "+Msg);
        temp.value ="";
        temp.focus();
        return false ;
    }
 else
   return true ;
}

//********************************************************************************
//21. function to check the length of a textarea

function fcheckfldlength (temp1,Msg)
{
	if(temp1.value.length > 4000)
	{
		alert(Msg +" cannot have more than 4000 characters");
		temp1.focus();
		return false;
		
	}		
}

//********************************************************************************
//33 fucntion to ignore the spaces in the String
function fIgnoreSpaces(string) {
	var temp = "";
	
	string = ' ' + string;
	splitstring = string.split(" ")
	for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
	return temp;
}

//********************************************************************************






