/**********************************************************************************************
Email address validation script
***********************************************************************************************/
function email_validation(str)


    	{
    		at_flag=0;
    		dot_flag=0;
    		sp_flag=0;
    		sq_dot=0;
    		email_id="";
    		server="";
    		for(i=0;i<str.value.length;i++)


        		{
        			if(at_flag>0 && str.value.charAt(i)!=".")
        				server+=str.value.charAt(i)
        			if(str.value.charAt(i)==" ")
        				sp_flag++
        			if(str.value.charAt(i)=="@" && i!=0)
        				at_flag++
        			if(at_flag==0)
        				email_id=email_id+str.value.charAt(i)
        			if(str.value.charAt(i)=="." && str.value.charAt(i+1)==".")
        				sq_dot++;
        			if(str.value.charAt(i)=="." && at_flag==1 && (str.value.length-1)!=i && str.value.charAt(i-1)!="@")
        				dot_flag++
        		}
        		if(at_flag>1 || dot_flag==0 || at_flag==0 || sp_flag!=0 || sq_dot!=0 || str.value.charAt(str.value.length-1)=='.')
        			//alert("invalid");
					return("invalid");
        		else


            		{
            			if(email_string_validation(email_id) && email_string_validation(server))
            				return(true)
            			else
						return("invalid");
            			//	alert("invalid");
            		}
            	}
            	
            	//validate email strimg
            	
            	function email_string_validation(str)


                	{
                		flag=0
                		for(i=0;i<str.length;i++)


                    		{
                    			chr=str.charAt(i)
                    			if((chr>='a' && chr<='z') || (chr>='A' && chr<='Z') || (chr>='0' && chr<='9' && i!=0) || (chr=='_') || (i!=0 && chr=='.'))
                    				flag++
                    		}
                    		if(flag==str.length)
                    			return(true)
                    		else
                    			return(false)
                    	}

/**********************************************************************************************
Handle Keycodes script
***********************************************************************************************/
function handleKeycode (field, event,type)
{
	
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if(type=="np")//numeric with dot 0-9,.
	{
			if((keyCode >=48 && keyCode<=57)||keyCode==46||keyCode==8||keyCode==9)
			{
	
				//event.keyCode=event.keyCode
				return true;
			} 
			else
			return false;
	
	}

	if(type=="n")//numeric without dot
	{
			if((keyCode >=48 && keyCode<=57) || keyCode==8 || keyCode==9)
			{
	
				//event.keyCode=event.keyCode
				return true;
			} 
			else
			return false;
	
	}
	if(type=="d")//date
	{
			if((keyCode >=47 && keyCode<=57)|| keyCode==8 || keyCode==9)
			{
	
				//event.keyCode=event.keyCode
				return true;
			} 
			else
			return false;
	
	}
	if(type=="u")//username
	{
			if((keyCode >=46 && keyCode<=95)||(keyCode >=48 && keyCode<=57)||(keyCode >=65 && keyCode<=90)||(keyCode >=97 && keyCode<=122)|| keyCode==45 || keyCode==8 || keyCode==9)
			{
	
				//event.keyCode=event.keyCode
				return true;
			} 
			else
			return false;
	
	}


} 

function checkValid(ctl,dataType)
{
	myctl=ctl.value;
	if(dataType=="n" && (isNaN(myctl) || myctl.indexOf('.')!=-1))
	{
		alert("Invalid Entry! Resetting...");
		ctl.value=0;
	}
	if(dataType=="np" && isNaN(myctl))
	{
		alert("Invalid Entry! Resetting...");
		ctl.value=0;
	}
}

/**********************************************************************************************
changes the row color
changeRowColor(rownum,toggleflag), rownum-identity of row,toggle field-mouseover or out
***********************************************************************************************/
function changeRowColor(rownum,toggleflag)
{
var initColor,hoverColor
var xlip=document.getElementById('tblList').rows
initColor="#FFFFFF"
hoverColor="#f6f6f6"
	if(toggleflag==1)
	{ 
	xlip[rownum].bgColor=hoverColor
	}
	else
	{
	xlip[rownum].bgColor=initColor
	}
}

/***************************************************************************************
for Currency VAlidation
****************************************************************************************/
var theform;
var isIE;
var isNS;

function detectBrowser()
{
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
		theform = document.forms["Form1"];
	else 
		theform = document.Form1;
		
	//browser detection
	var strUserAgent = navigator.userAgent.toLowerCase();
	isIE = strUserAgent.indexOf("msie") > -1;
	isNS = strUserAgent.indexOf("netscape") > -1;
	
}

/*
This function will fire when the control leaves the Text Box.
The function is responsible for formating the numbers to amount type.
*/
function FormatAmtControl(ctl){
	var vMask ;
	var vDecimalAfterPeriod ;
	var ctlVal;
	var iPeriodPos;
	var sTemp;
	var iMaxLen 
	var ctlVal;
	var tempVal;
	ctlVal = ctl.value;
	vDecimalAfterPeriod  = 2
	iMaxLen  = ctl.maxLength;

	if (isNaN(ctlVal))
	{
		// clear the control as this is not a num
		ctl.value=""
	}
	else{
		ctlVal =  ctl.value;
		iPeriodPos =ctlVal.indexOf(".");
		if (iPeriodPos<0)
		{
			if (ctl.value.length > (iMaxLen-3))
			{
				sTemp = ctl.value
				 tempVal = sTemp.substr(0,(iMaxLen-3)) + ".00";
			}
			else
			tempVal = ctlVal + ".00"
		}
		else{
			if ((ctlVal.length - iPeriodPos -1)==1)
				tempVal = ctlVal + "0"
			if ((ctlVal.length - iPeriodPos -1)==0)
				tempVal = ctlVal + "00"
			if ((ctlVal.length - iPeriodPos -1)==2)
				tempVal = ctlVal;
			if ((ctlVal.length - iPeriodPos -1)>2){
				tempVal = ctlVal.substring(0,iPeriodPos+3);
			}


		}
		ctl.value=tempVal;
	}
}

/*
This function is responsible for filtering the keys pressed and the maintain the amount format of the 
value in the Text box
*/
	function HandleAmountFiltering(ctl){
	var iKeyCode, objInput;
	var iMaxLen 
	var reValidChars = /[0-9.]/;
	var strKey;
	var sValue;
	var event = window.event || arguments.callee.caller.arguments[0];
	iMaxLen  = ctl.maxLength;
	sValue = ctl.value;
	detectBrowser();

	if (isIE) {
		iKeyCode = event.keyCode;
			objInput = event.srcElement;
	} else {
		iKeyCode = event.which;
		objInput = event.target;
	}

	strKey = String.fromCharCode(iKeyCode);

	if (reValidChars.test(strKey))
	{
		if(iKeyCode==46)
		{
			if(objInput.value.indexOf('.')!=-1)
				if (isIE)
					event.keyCode= 0;
				 else
				 {
				 	 if(event.which!=0 && event.which!=8)
					return false;
				 }
		}
		else
		{
			if(objInput.value.indexOf('.')==-1)
			{
				
				if (objInput.value.length>=(iMaxLen-3))
				{
					if (isIE)
						event.keyCode= 0;
					 else
					 {
					 	 if(event.which!=0 && event.which!=8)
						return false;
					 }
	
				}
			}
			if ((objInput.value.length==(iMaxLen-3)) && (objInput.value.indexOf('.')==-1))
			{
				objInput.value = objInput.value +'.';
			
			}

	
		}

	}
	else{
		if (isIE)
			event.keyCode= 0;
		 else
		 {
		 	 if(event.which!=0 && event.which!=8)
			 return false;
		 }
	}

}

/************************************************************************************
ENDS
*************************************************************************************/



/***************************************************************************************

	// This file contains the date validation JavaScript functions
	// It is included in the HTML pages with forms that need these
	// date validation routines.
	// DATE VALIDATION

****************************************************************************************/
// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";




// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/****************************************************************/

/* PURPOSE:  Returns true if the string is a valid date number.
	A method is passed in (1 = month, 2 = day).  If the string is
	nonnumeric, false is passed back.  If the day in the date string
	is greater than 31, false is returned.  If the month is greater
	than 12, an error is returned.
*/

function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

	if (method == 2)
		if (parseInt(str) > 32)
			return false;
	if (method == 1)
		if (parseInt(str) > 12)
			return false;

	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;


	return true;
}

/****************************************************************/

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError)
{
	alert("You have entered an invalid date for " + strError + ".  Please make sure your date format is in MM/DD/YY or MM/DD/YYYY or M/D/YY or M/D/YYYY format.");
	Field.focus();
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date.  A valid
	date is defined as any of the following:

		MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
		MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
*/

function ForceDate(strDate,strField)
{
	var str = new String(strDate.value);

	if (isWhitespace(str)) {
		return true;
		// if the field is empty, just return true...
	}

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	if (i == count || i > 2) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),1)) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
		i++;

	if (i+j == count || i > 2) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),2)) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count)
		i++;


	if (i != 2 && i != 4) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),3)) {
		PromptErrorMsg(strDate,strField);
		strDate.value="";
		return false;
	}

	return true;
}
/****************************************************************************************
ENDS
*****************************************************************************************/
