// VARIABLE DECLARATIONS

var digits = "0123456789";

// decimal point character differs by language and culture
var decimalPointDelimiter = "."

function valid_email(x)
{
	  /*var str = x.value; // email string
	  
	  //Create Regular Expression Patterns
	  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 								// not valid
	  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;	// valid
	  
	  // if syntax is valid
	  if (!reg1.test(str) && reg2.test(str)) 
	  {		return true;	}
	  else 
	  {		return false;	}
	*/
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(x))
	{	return true;	}
	else
		return false;

}


function isAlphaNumeric(string) 
{	
	//items to be avoided
	var iChars = "*|\":<>[]{}`\;()@&$%_^~+?!";
	
	//In a for loop check the values
	for (var i = 0; i < string.length; i++)
	{	if (iChars.indexOf(string.charAt(i)) != -1){	return false;	}	}
	
	//else is Alpha Numeric
	return true;
}

function onlyCharacters(inputString)
{	
	var iChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	
	for (var i = 0; i < inputString.length; i++)
	{	if (iChars.indexOf(inputString.charAt(i)) == -1){	return true;	}	}
	
	return false;
}

function onlyNumbers(inputString)
{
	var iChars = "0123456789-";
	
	for (var i = 0; i < inputString.length; i++)
	{	if (iChars.indexOf(inputString.charAt(i)) == -1){	return true;	}	}
	
	return false;
} 


function checkCardNumWithMod10(cardNum) {
	var i;
	var cc = new Array(16);
	var checksum = 0;
	var validcc;

	// assign each digit of the card number to a space in the array	
	for (i = 0; i < cardNum.length; i++) {
		cc[i] = Math.floor(cardNum.substring(i, i+1));
	}

	// walk through every other digit doing our magic
	// if the card number is sixteen digits then start at the
	// first digit (position 0), otherwise start from the
	// second (position 1)
	for (i = (cardNum.length % 2); i < cardNum.length; i+=2) {
		var a = cc[i] * 2;
		if (a >= 10) {
			var aStr = a.toString();
			var b = aStr.substring(0,1);
			var c = aStr.substring(1,2);
			cc[i] = Math.floor(b) + Math.floor(c);
		} else {
			cc[i] = a;
		}
	}

	// add up all of the digits in the array
	for (i = 0; i < cardNum.length; i++) {
		checksum += Math.floor(cc[i]);
	}

	// if the checksum is evenly divisble by 10
	// then this is a valid card number
	validcc = ((checksum % 10) == 0);

	return validcc;
}

function cleanCardNum(cardNum) {
	var i;
	var ch;
	var newCard = "";

	// walk through the string character by character to build
	// a new string with numbers only
	i = 0;
	while (i < cardNum.length) {
		// get the current character
		ch = cardNum.substring(i, i+1);
		if ((ch >= "0") && (ch <= "9")) {
			// if the current character is a digit then add it
			// to the numbers-only string we're building
			newCard += ch;
		} else {
			// not a digit, so check if its a dash or a space
			if ((ch != " ") && (ch != "-")) {
				// not a dash or a space so fail
				alert("The card number contains invalid characters.");
				return "";
			}
		}
		i++;
	}

	// we got here if we didn't fail, so return what we built
	return newCard;
}

function checkCard(cardType, cardNum) {
	var validCard;
	var cardLength;
	var cardLengthOK;
	var cardStart;
	var cardStartOK;
	// check if the card type is valid
	if ((cardType != "Visa") && (cardType != "MasterCard") && (cardType != "Discover") && (cardType != "Amex") ) {
		alert("Please select a card type.");
		return false;
	}

	// clean up any spaces or dashes in the card number
	validCard = cleanCardNum(cardNum);
	if (validCard != "") {
		// check the first digit to see if it matches the card type
		cardStart = validCard.substring(0,1);
		cardStartOK = ( ((cardType == "Visa") && (cardStart == "4")) ||
				((cardType == "MasterCard") && (cardStart == "5")) ||
				((cardType == "Discover") && (cardStart == "6")) ||
				((cardType == "Amex") && (cardStart == "3")) );
				
		if (!(cardStartOK)) {
			// card number's first digit doesn't match card type
			alert("Please make sure the card number you've entered matched the card type you selected.");
			return false;
		}

		// the card number is good now, so check to make sure
		// it's a the right length
		cardLength = validCard.length;		
		cardLengthOK = ( ((cardType == "Visa") && ((cardLength == 13) || (cardLength == 16))) ||
				 ((cardType == "MasterCard") && (cardLength == 16)) ||
				 ((cardType == "Discover") && (cardLength == 16)) ||
				 ((cardType == "Amex") && (cardLength == 15)) );
				 
		if (!(cardLengthOK)) {
			// not the right length
			alert("Please make sure you've entered all of the digits on your card.");
			return false;
		}

		// card number seems OK so do the Mod10
		if (checkCardNumWithMod10(validCard)) {
			return true;
		} else {
			alert("Please make sure you've entered your card number correctly.");
			return false;
		}
	} else {
		return false;
	}
}


function checkExpiration(expiryYear, expiryMonth) {
	
    var expired = false;
    
    if (expiryYear.length == 0) {
        alert("You must select an expiration year");
    	return false;
    }
    
    if (expiryMonth.length == 0) {
        alert("You must select an expiration month");
     	return false;
    }
    
    //Use a radix value of 10 because the parseInt method was failing for 08, 09 months
    var eMonth = parseInt(expiryMonth,10);
    var eYear  = parseInt(expiryYear, 10);
                
    var today = new Date();
    var thisYear = today.getYear();
    var thisMonth = 1 + today.getMonth();
       
    expired = (thisYear > eYear) || ((thisYear == eYear ) && (thisMonth > eMonth));
    
    if ( expired ) {
         alert("Your credit card has expired. Please select the correct expiration month and year.");
         return false;
    }
    
    
    return true;
 }


function OpenPopUp(sURL)
{
	var sAttr;
	var sWidth;
	var sHeight;
	var sWindowName;
	var left;
	var top;
	var objPopUp = new Object()
	objPopUp.open = false;

	sAttr = "width=600,height=600,top=200,left=200,toolbar=no,scrollbars=yes,location=no,menubar=no,resizable=yes";
	sWindowName = "PopUp";

	objPopUp.win = window.open(sURL, sWindowName, sAttr);
	objPopUp.open = true;
}

    
// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (s == decimalPointDelimiter) return false;

   	// if the first character is a decimal, we don't allow it
	if (s.substring(0, 1) == decimalPointDelimiter)
	{
		return false;
	}

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (c == decimalPointDelimiter) 
        {	
        	if(!seenDecimalPoint)
        		seenDecimalPoint = true;
        	else
        		return false;
        }
        else if (!isDigit(c)) 
           return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
