
function checkForspaces(strVal)
{
    if(strVal.indexOf(" ") == -1)
        return false;
    else 
        return true;
}

function checkForallspaces(strVal)
{
 if(strVal.length == 0)
  return false;
 
 var result = true;
 for (var i=0; i < strVal.length; i++)
 {
   if(strVal.substring(i,i+1) != ' ')
    result = false;
 }
 return result;
}

function checkForlength(strVal,wantLen)
{
 if(strVal.length < wantLen)
  return false;
 else 
  return true;
}

function checkForexactlength(strVal,wantLen)
{
 if(strVal.length != wantLen)
  return false;
 else 
  return true;
}

function checkForemptyfield(strVal)
{
  if((checkForallspaces(strVal)) || (!checkForlength(strVal,1))) 
   return true;
  else
   return false; 
}

// Get value for a Group of RadioButton objects
function radioValue(radioObject)
{
  for(var i = 0; i < radioObject.length; i++)
   if(radioObject[i].checked)
     return radioObject[i].value;
}

// Generic positive number decimal formatting function
function format (expr, decplaces) {
	
	// raise incoming value by power of 10 times the number of decimal places; round to an integer; convert to a string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) {
	 str = "0" + str
	}
	
	// establish location of decimal point
	var decpoint = str.length - decplaces
	
	// ---assemble final result from: (a) the string up to the position of the decimal point; (b) the decimal point; and (c) the balance of the string. Return finished product.
	if(decplaces > 0)
	 return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
	else 
	 return str.substring(0,decpoint) + str.substring(decpoint,str.length);
}

function dollarize (expr) {
    return "$" + commaSplit(TrimString(format(expr,2)));    
}

function decimaldollarize (expr,decimals) {
    return "$" + commaSplit(TrimString(format(expr,decimals)));    
}

function commaSplit(expr)
{
 if (isNaN(expr) || expr == "") 
  return expr;
 else 
  {
  var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
  var arrNumber = expr.split('.');
  arrNumber[0] += '.';
  
  do 
   {
   arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
   } while (rxSplit.test(arrNumber[0]));

  if (arrNumber.length > 1) 
   {
   return arrNumber.join('');
   }
  else 
  {
  return arrNumber[0].split('.')[0];
  }
 }
}

function validateCreditCard(cardNumber,cardType,cardMonth,cardYear)
{
 
 if( cardNumber.length == 0 )
 {
  alert("Please enter a valid card number.");
  return false;				
 }
 
 for( var i = 0; i < cardNumber.length; ++i )
 {
  var c = cardNumber.charAt(i);
  
  if( c < '0' || c > '9' )
  {
   alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
   return false;
  }
 }
  
 var length = cardNumber.length;
 
 switch( cardType )
 {
   case 'A':
  
    if( length != 15 )
    {
     alert("Please enter a valid American Express Card number.");
     return false;
    }
   
    var prefix = parseInt( cardNumber.substring(0,2));

    if( prefix != 34 && prefix != 37 )
    {
     alert("Please enter a valid American Express Card number.");
     return false;
    }
   break;
  
   case 'D':


    if( length != 16 )
    {
     alert("Please enter a valid Discover Card number.");
     return false;
    }
    
    var prefix = parseInt( cardNumber.substring(0,4));

    if( prefix != 6011 )
    {
     alert("Please enter a valid Discover Card number.");
     return false;
    }
   break;
   
   case 'M':

    if( length != 16 )
    {
     alert("Please enter a valid MasterCard number.");
     return false;
    }
    
    var prefix = parseInt( cardNumber.substring(0,2));

    if( prefix < 51 || prefix > 55)
    {
     alert("Please enter a valid MasterCard Card number.");
     return false;
    }
   break;
   
   case 'V':

    if( length != 16 && length != 13 )
    {
     alert("Please enter a valid Visa Card number.");
     return false;
    }
    
    var prefix = parseInt( cardNumber.substring(0,1));

    if( prefix != 4 )
    {
     alert("Please enter a valid Visa Card number.");
     return false;
    }
   break;
   
   default:
    alert("Please enter a valid Card Type.");
    return false;
   break;
 }
    
 if( !mod10( cardNumber ) )
  {
   alert("Sorry! this is not a valid credit card number.");
   return false;
  }
    
 if( expired( cardMonth, cardYear ) )
  {
   alert("Sorry! The expiration date you have entered would make this card invalid.");
   return false;
  }
 return true;
}

function mod10( cardNumber )
{ 
 var ar = new Array( cardNumber.length );
 var i = 0,sum = 0;
 for( i = 0; i < cardNumber.length; ++i )
 {
  ar[i] = parseInt(cardNumber.charAt(i));
 }
 
 for( i = ar.length -2; i >= 0; i-=2 )
 { 
 ar[i] *= 2;							 
 if( ar[i] > 9 )
  ar[i]-=9;			 
 }										 

 for( i = 0; i < ar.length; ++i )
 {
  sum += ar[i];						 
 }
 
 return (((sum%10)==0)?true:false);	 	
}
 
function expired( month, year ) 
{
 var now = new Date();							
 var expiresIn = new Date(year,month,0,0,0);		
 expiresIn.setMonth(expiresIn.getMonth()+1);		
 
 if( now.getTime() < expiresIn.getTime() )
  return false;
 
 return true;									
}

function ValidDate( month, day, year ) 
{
    var now = new Date();							
    var expiresIn = new Date("20" + year,month -1,day -1,0,0,0);		

    if( now.getTime() >= expiresIn.getTime() )
        return false;
 
    return true;									
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function Win(URL, Width, Height)
{
  window.open(URL, "AstonishingMail", "width=" + Width + ", height=" + Height + ", location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes");
}

//A cross-browser replacement for getElementById function
function returnObjById( id ) 
{ 
    if (document.getElementById) 
        return document.getElementById(id); 
    else if (document.all) 
        return document.all[id]; 
    else if (document.layers) 
        return  document.layers[id]; 
}

//Select all or no checkboxs on page.
function CBSelect(Select)
{
    for (var n=0; n < document.forms[0].length; n++)
    { 
        if (document.forms[0].elements[n].type == 'checkbox')
        {
            document.forms[0].elements[n].checked=Select; 
        }
    }
    
    return false; 
}

function pausecomp(millis) 
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); } 
    while(curDate-date < millis);
} 

function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }

    if (str.indexOf(" ") != -1) {
        return false
    }

    return true
}