
<!--//

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters;
var minDigitsInIPhoneNumber = 10;
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var isIE = document.all ? true : false;
var isNS = document.layers ? true : false;

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

//////////////////////////////////////////////////////////////////////////////

if (isNS) 
{
	document.captureEvents(Event.KEYPRESS);
}

//////////////////////////////////////////////////////////////////////////////
function validateInfo() 
{

var frmElements = document.getElementsByTagName("input"); 
var blInvalidData = false;
var TagValidate;
var TagValidateType;
var ElementValue;
var TagValidateNull;
var strReturnMsg = 'All fields are required, make sure all necessary values are entered correctly:\r\n\r\n';

if (frmElements.length) 
{
    for (var i=0; i<frmElements.length; i++) 
	{
	TagValidate = frmElements[i].getAttribute("TagValidate");
	TagValidateType = frmElements[i].getAttribute("TagValidateType");
	ElementValue = frmElements[i].value;
	TagValidateNull = frmElements[i].getAttribute("TagValidateNull"); 
	
	if (TagValidateType != null)
		{ 
  			/*
  				0 = Null & Dropdown
  				1 = Phone
  				2 = Email
  				3 = Date
  				4 = Number (check limit on keypress)
  				5 = SSN
  				6 = Same Email
  				7 = Credit Card
  				* = Char Limit (KeyMax Property)
  			*/
  			switch (TagValidateType)
  			{
  			case '0':	if (isEmpty(ElementValue))
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;
  					
  			case '1':	if (isEmpty(ElementValue) && TagValidateNull == "False")
  						{
  						break;
  						}
  						
  						if (checkInternationalPhone(ElementValue) == false)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;
  						
  			case '2':	if (isEmpty(ElementValue) && TagValidateNull == "False")
  						{
  						break;
  						}
  						
  						if (IsValidEmail(ElementValue) == false)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;
  						
  			case '3':	if (isEmpty(ElementValue) && TagValidateNull == "False")
  						{
  						break;
  						}
  						
  						if (isDate(ElementValue) == false)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;
  						
  			case '4':	if (isEmpty(ElementValue) && TagValidateNull == "False")
  						{
  						break;
  						}
  						
  						if (IsValidNumber(ElementValue) == false)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						else
  						{
   						if (TagValidate == 'Amount to pay' && parseInt(ElementValue) < 35)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Amount to pay Minimun is $35.00' + '.\r\n';
  						}
  						}
  						break;
  		
  			case '5':	if (isEmpty(ElementValue) && TagValidateNull == "False")
  						{
  						break;
  						}
  						
  						if (isValidSSN(ElementValue) == false)
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;	
  						
	        case '6':   if (ElementValue != document.getElementById('ctl00_ContentPlaceHolder1_txtEmail').value)
			            {
    				 
			            blInvalidData = true;
			            strReturnMsg = strReturnMsg + '> Email Addresses do not match' + '.\r\n';
			            }
			            break;
  					  
  		    case '7': 
  		                myCardNo = ElementValue;
                        myCardType = document.getElementById('ctl00_ContentPlaceHolder1_ddCardType').value;

                        if (checkCreditCard (myCardNo,myCardType) == false)
                        {
  					    blInvalidData = true;
  					    strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  					    }
  					    break;
  					    
  			//Zip code for US/Canada
  			case '8' : 	if (isEmpty(ElementValue)&& ((document.getElementById('ctl00_ContentPlaceHolder1_ddCountry').value == '1') ||(document.getElementById('ctl00_ContentPlaceHolder1_ddCountry').value == '44')) )
  						{
  						blInvalidData = true;
  						strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + '.\r\n';
  						}
  						break;
  			
  		    }
		}
     } 
}

//////////////////////////////////////////////////////////////////////////////

var frmElements = document.getElementsByTagName("textarea"); 

if (frmElements.length) 
{
    for (var i=0; i<frmElements.length; i++) 
	{
	TagValidate = frmElements[i].getAttribute("TagValidate");
	TagValidateType = frmElements[i].getAttribute("TagValidateType");
	ElementValue = frmElements[i].value;
	
	if (TagValidateType != null && ElementValue.length < 700)
		{ 
		  	blInvalidData = true;
  			strReturnMsg = strReturnMsg + '> Enter a valid ' + TagValidate + ' (500 words minimum).\r\n';
        }
        
     } 
}

//////////////////////////////////////////////////////////////////////////////

var frmElements = document.getElementsByTagName("select"); 

if (frmElements.length) 
{
    for (var i=0; i<frmElements.length; i++) 
	{
	TagValidate = frmElements[i].getAttribute("TagValidate");
	TagValidateType = frmElements[i].getAttribute("TagValidateType");
	ElementValue = frmElements[i].value;
	
	if (TagValidateType != null && ElementValue == 0)
		{ 
		  	blInvalidData = true;
  			strReturnMsg = strReturnMsg + '> Select a valid ' + TagValidate + '.\r\n';
        }
     } 
}

//////////////////////////////////////////////////////////////////////////////
   
if (blInvalidData)
	{	 
		alert(strReturnMsg);
		return false;
	}
else
	{
		return true;
		document.forms[0].submit();
	}

}

//////////////////////////////////////////////////////////////////////////////

function isEmpty(iValue)
{
if (iValue == '' || iValue == null)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//////////////////////////////////////////////////////////////////////////////

function isInteger(s)
{   var i;
    for (i=0; i<s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }

    return true;
}

//////////////////////////////////////////////////////////////////////////////

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i=0; i<s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

//////////////////////////////////////////////////////////////////////////////

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length == minDigitsInIPhoneNumber);
}

//////////////////////////////////////////////////////////////////////////////

function IsValidEmail(strEmailValue)
{     

		var tmpReturn = false;
        var strEmail = new String(strEmailValue);
        var index = strEmail.indexOf("@");
        if (index > 0) 
        {
        var pindex = strEmail.indexOf(".",index);
        if ((pindex > index+1) && (strEmail.length > pindex+1))
			{
			tmpReturn = true;
			}
		}
	
		return tmpReturn;
}

//////////////////////////////////////////////////////////////////////////////

function daysInFebruary (year)
{
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

//////////////////////////////////////////////////////////////////////////////

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

//////////////////////////////////////////////////////////////////////////////

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1)
	{
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		return false
	}
		
	return true

}

//////////////////////////////////////////////////////////////////////////////

function IsValidNumber(eSrc)
{

 	if (isNaN(eSrc))
 	{
		return false;
  	}
	if (eSrc <= 0) 
  	{
		return false;
	}

	return true;
}

//////////////////////////////////////////////////////////////////////////////

function isValidSSN(ssn) 
{
var matchArr = ssn.match(/^(\d{3})-\d{2}-\d{4}$/);
var numDashes = ssn.split('-').length - 1;
var tmpReturn;

if (matchArr == null || numDashes == 1) 
	{
		tmpReturn = false;
	}
else 
if (parseInt(matchArr[1],10)==0) 
	{
		tmpReturn = false;
	}
else 
	{
		tmpReturn = true;
    }
    
		return tmpReturn;
}
	
//////////////////////////////////////////////////////////////////////////////

function onlyDigits(e)
{
        var ret = true;
   
        if (isIE) {
                if (window.event.keyCode == 47 || window.event.keyCode < 46 || window.event.keyCode > 57) 
                {
                        window.event.keyCode = 0;
                        ret = false;
                }
        }
        if (isNS) 
        {
                if (e.which == 47 || e.which < 46 || e.which > 57) 
                {
                        e.which = 0;
                        ret = false;
                }
        }

        return (ret);
}

//////////////////////////////////////////////////////////////////////////////

function checkCreditCard (cardnumber, cardname) {
     
  var cards = new Array();

 //Visa
  cards [0] = {name: "1", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
 //Mastercard
  cards [1] = {name: "2", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "DinersClub", 
               length: "14,16", 
               prefixes: "300,301,302,303,304,305,36,38,55",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  //American Express
  cards [4] = {name: "4", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  //Discover
  cards [5] = {name: "3", 
               length: "16", 
               prefixes: "6011,650",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "15,16", 
               prefixes: "3,1800,2131",
               checkdigit: true};
  cards [7] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [8] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334, 6767",
               checkdigit: true};
  cards [9] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [10] = {name: "Maestro", 
               length: "16", 
               prefixes: "5020,6",
               checkdigit: true};
  cards [11] = {name: "VisaElectron", 
               length: "16", 
               prefixes: "417500,4917,4913",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}



//-->
