//////////////////////////////////////////////////////////////////////////////
//																			//
//           COPYRIGHT INFORMATION                                          //
//           Copyright (c) 2001,                                            //
//           e-MarketingUSA, Inc.                							//
//           All Rights Reserved.                							//
//                                               							//
//           Contact information:                							//
//           http://www.e-MarketingUSA.com       							//
//																			//
////////////////////////////////////////////////////////////////////////////// 
// Confirm Password.
function confirmpass(obj1, obj2, str){
	if(obj1.value != obj2.value){
		obj2.value=""
		selfocobj(obj1, str)
		return false;	
	}
	return true
}

//Select and focus on object.
function selfocobj(obj, str){
		alert(str);
		obj.select();
		obj.focus();
}

// checks whether string is numeric or not.
function isNumeric(obj,s){
	if(isEmpty(obj.value) | isNaN(obj.value)){
			selfocobj(obj, s)
			return false;
		}
	return true;
}

// checks whether string is empty or not
function required(obj, s){
	if(isEmpty(obj.value)){
		selfocobj(obj, s)
		return false;
	}
	return true;
}

// Checks for the credit card number VISA, Master-Card and American Express
function isCreditCardNo(obj, objcred, s){	
	switch(selrad(objcred)){
	case 0:
		if(!isVisa(obj.value)){
			selfocobj(obj, s)
			return false;
		}
		break;
	case 1:
		if(isMasterCard(obj.value)){
			selfocobj(obj, s)
			return false;
		}
		break;
	case 2:
		if(!isAmericanExpress(obj.value)){
			selfocobj(obj, s)
			return false;
		}
		break;
	case 3:
		if(!isDiscover(obj.value)){
			selfocobj(obj, s)
			return false;
		}
		break;
	}
	return true;
}

// Check whether string s is empty.
function isEmpty(s)
{  
	return ((s == null) || (s.length == 0)||s.charAt(0) == ' ')
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (obj, s) {   
	var reEmail = /^.+\@.+\..+$/
	if (isEmpty(obj.value) || !reEmail.test(obj.value)) {
	    selfocobj(obj, s)
		return false;
	  }
	return true;
}

/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
	if (st.length > 19)
    	return (false);
	var digit;
	var tproduct;
	var sum = 0; var mul = 1; var l = st.length;
	for (var i = 0; i < l; i++) {
    	digit = st.substring(l-i-1,l-i);
		tproduct = parseInt(digit ,10)*mul;
		if (tproduct >= 10)
			sum += (tproduct % 10) + 1;
		else
			sum += tproduct;
		if (mul == 1)
			mul++;
		else
			mul--;
}
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
// alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()
/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()




/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  var firstdig = cc.substring(0,1);
  var seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()





/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  var firstdig = cc.substring(0,1);
  var seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()

function isDiscover(cc){
	first4digs = cc.substring(0,4);
	if ((cc.length == 16) && (first4digs == '6011'))
		return isCreditCard(cc);
	return false;
} // END FUNCTION isDiscover()

//==================================================================================
//==================  return the index of currently selected radio button =================
//==================================================================================
function selrad(obj){
	if(obj.type == "select-one"){
		return obj.selectedIndex;
	}	
	for(var i=0;i<obj.length;i++){		
		if(obj[i].checked)
			return i;
	}
}
