// Vipermailer Common Java Script 
/*
 *
 *  Function : CheckUnCheckAllCheckBox() 
 *  Param    :
						 :    frmform     Form
             :    bIsFilter   bool false if u want for all  / true for specific group
             :    strIDGroup  string that is part of checkbox id
             :    bStatus     Set / Reset Status   
 *  By       : Patrick Lara
 *  Date     : 5 April 2005
 *  Desc     : Check / Uncheck All / Specific Group CheckBox on the PAGE.
 *   
 */
function CheckUnCheckAllCheckBox( frmform , bIsFilter , strIDGroup , bStatus )
    {
       var inputs = new Array();
       var strID ;
              
       inputs =    frmform.getElementsByTagName('input');
         
       var checkboxes = new Array();
       for (i = 0; i < inputs.length; i++)
          {
          
			if (inputs[i].type == 'checkbox')
			 {
			   if (bIsFilter == false )
			       ;
			    else
			     {
					strID = inputs[i].id ;									
					if ( strID.indexOf( strIDGroup ) < 0 )			   
					continue ;
			   	}
			   	
			   checkboxes[checkboxes.length] = inputs[i];	
            }
          }

	/**
	 * checkboxes now is an array of all checkboxes
	 * in the table with id "mytablesid".  Now perform some
	 * action on the checkboxes.  In this case, check
	 * them all.
	 *
	 **/
	 
     for (i = 0; i < checkboxes.length; i++)
       checkboxes[i].checked = bStatus;      
     
 }
/**
*
*  Function : CheckAllCheckBox() 
*  Param    :
            :    frmform     Form
            :    bIsFilter   bool false if u want for all  / true for specific group
            :    strIDGroup  string that is part of checkbox id
*  Date     : 27 May 2005
*  Desc     : Check  All / Specific Group CheckBox on the PAGE.
*   
**/ 
function CheckAllCheckBox( frmform , bIsFilter , strIDGroup )
{
    var inputs = new Array();
    var strID ;
            
    inputs =    frmform.getElementsByTagName('input');
        
    var checkboxes = new Array();
    for (i = 0; i < inputs.length; i++)
        {
        
		if (inputs[i].type == 'checkbox')
			{
			if (bIsFilter == false )
			    ;
			else
			    {
				strID = inputs[i].id ;									
				if ( strID.indexOf( strIDGroup ) < 0 )			   
				continue ;
			}
			
			checkboxes[checkboxes.length] = inputs[i];	
        }
        }

/**
	* checkboxes now is an array of all checkboxes
	* in the table with id "mytablesid".  Now perform some
	* action on the checkboxes.  In this case, check
	* them all.
	*
	**/
	
    for (i = 0; i < checkboxes.length; i++)
    {
		if(checkboxes[i].checked)
			return true; 
	}
    return false;
}

/**
*
*  Function : Trim() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Function to trim leading spaces
*   
**/
function Trim(str)
{
	 return RTrim(LTrim(str));
}
/**
*
*  Function : LTrim() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Function To Strip Leading Blanks at left side
*   
**/
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) 
   {
    var j=0, i = s.length;
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
      j++;
    s = s.substring(j, i);
   }
  return s;
}
/**
*
*  Function : RTrim() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Function To Strip Leading Blanks right side
*   
**/
function RTrim(str)
 {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
   {
     var i = s.length - 1;
     while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
       i--;
     s = s.substring(0, i+1);
    }
  return s;
}
var aWindow;
/**
*
*  Function : popUp() 
*  Param    :
            :  aLink    as string
*  Date     : 28 April 2005
*  Desc     : Function To open url in new window
*   
**/
function popUp(aLink) 
{
	  aWindow = window.open(aLink,"Disp","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=500,height=300");
}

/**
*
*  Function : isNumber() 
*  Param    :
            :  elm    as txtControl
            : fieldName as string
*  Date     : 28 April 2005
*  Desc     : Check if number
**/
function isNumber(elm,fieldName)
{
	var checkStr = elm.value ;
	
	if (isNaN(checkStr))
	{ 
		alert("Please Enter only digits for "+fieldName)
		elm.focus();
		return false;
	}
	return true;
}

/**
*  Function : isValidEmailId() 
*  Param    :
            :  elm    as txtControl
*  Date     : 28 April 2005
*  Desc     : Following function is for email validation
**/
function isValidEmailId(elm){
	var string = elm.value;
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
    {
	    return true;
    }
    else
    {
		alert("Email Address is not valid!");
		elm.focus();
        return false;
    }
}
/**
*  Function : isValidEmailId() 
*  Param    :
            :  str  as string
*  Date     : 05 May 2005
*  Desc     : Following function is for email validation
**/
function CheckEmailId(str){
    if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
    {
	    return true;
    }
   return false;
}
var whitespace = " \t\n\r";
/**
*  Function : isWhitespace() 
*  Param    :
            :  s    as string
*  Date     : 28 April 2005
*  Desc     : check white space
**/
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 : isEmpty() 
*  Param    :
            :  s    as string
*  Date     : 28 April 2005
*  Desc     : Check whether string s is empty.
**/
function isEmpty(s)
{  
	 return ((s == null) || (s.length == 0))
}

/**
*  Function : isValidPhoneNo() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Check if Phone number is valid
**/
function isValidPhoneNo(str)
{
	 if(isValidCharSet(str,"0123456789-")){
		return true;
	 }
	 return false; 
}
/**
*  Function : isValidMobileNo() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Check if Phone number is valid
**/
function isValidMobileNo(str)
{
	 if(isValidCharSet(str,"0123456789-")){
		return true;
	 }
	 return false; 
}
/**
*  Function : isValidCardNo() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Check if card number is valid
**/
function isValidCardNo(s)
{
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) != -1)
	w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);	 
}

/**
*  Function : isAllDigits() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Check if all are digits
**/
function isAllDigits(str)
{
	if(isValidCharSet(str,"0123456789")){
		return true;
	}
	return false;
}
/**
*  Function : isValidCharSet() 
*  Param    :
            :  str    as string
            : charset as char array
*  Date     : 28 April 2005
*  Desc     : Check if isValidCharSet
**/
function isValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}
/**
*  Function : allChar() 
*  Param    :
            :  str    as string
*  Date     : 28 April 2005
*  Desc     : Check if input string contains only a to z letters
**/
function allChar(str)
{   
	testExp=/[a-zA-Z]/;
	return testExp.test(str);
}
/**
*  Function : isEqualString() 
*  Param    :
            : strFirst  as string
            : strSecond as string
*  Date     : 28 April 2005
*  Desc     : Check if two string are equal
**/
function isEqualString(strFirst,strSecond)
{
	if(strFirst.length!=strSecond.length)
		return false;
	for(var i=0;i<strFirst.length;i++)
	{
		if(strFirst.charAt(i)!=strSecond.charAt(i))
			return false;
	}	
	return true;
}


/************************************************
FUNCTION : IsValidDate

DESCRIPTION: Validates that a string only in these format
		( mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy )

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

ADDED BY: 

DATE : 
	29th April 2005.	

*************************************************/
function IsValidDate( strValue ) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
  
  	strValue = Trim(strValue);
		
   if(!objRegExp.test(strValue))
   {
	 alert("Please enter date in the format mm/dd/yyyy or mm-dd-yyyy ");
	 return false; //doesn't match pattern, bad date
    }
  else{
    var strSeparator = strValue.substring(2,3); //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1]);
    
       
       if(arrayDate[0]==0 || arrayDate[1]==0 || arrayDate[2]==0)
        {
			alert("Please enter date in the format mm/dd/yyyy or mm-dd-yyyy ");
			return false;
	     }
    if(arrayLookup[arrayDate[0]] != null) 
	 {
		if( arrayDate[1] <= arrayLookup[arrayDate[0]] && arrayDate[1] != 0)
        return true; 
	}

    //check for February 
    var intMonth = parseInt(arrayDate[0]);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
          return true; 
       }
  }
  alert("Please enter date in the format mm/dd/yyyy or mm-dd-yyyy ");
  return false; 
}
