﻿function setFieldValue (objfield1,strValue)
{
	objfield = eval(objfield1);
    if(objfield == null)
			return;

	if(objfield.options != null) // Field is a drop-down
	{
		for( var n =0; n < objfield.options.length ; n++)
			if( objfield.options[n].value == strValue )
			{
				objfield.selectedIndex = n;
				break;
			}
	}
	else // Field is a text field
		objfield.value = strValue;
}

function getFieldValue (objfield1,strValue)
{
	objfield = eval(objfield1);
    if(objfield == null)
			return;
	for( var n =0; n < objfield.options.length ; n++)
		if( objfield.options[n].text == strValue )
		{
			strCountryCode = objfield.options[n].value;
			objfield.selectedIndex = n;
			break;
		}
	return getFieldValue = strCountryCode;
}

function ChkCreditCard(strCardType, strCardNumber)
{
    if(strCardNumber == null)
		return true;
		
	if(strCardNumber == '')
		return true;
   
    var cc = StripChar(strCardNumber, " -");

     // alert(cc);
	var strMessage = "We're sorry, the credit card information you've entered is incorrect.\n\nPlease try again.";

	if( cc.length > 19 || ! isCreditCard(cc) )
    {
      alert(strMessage);
      return false ;
  	}

	if(!ChkCardType(strCardType, cc))
    {
      alert(strMessage);
      return false ;
  	}

	return true;
}
function Check()
    {
    var controlPrefix = "ctl00_ContentPlaceHolder1_";
    var ccnumber = document.getElementById(controlPrefix + "txtCardNumber").value;
    var cctype = document.getElementById(controlPrefix + "ddlCardType").value;

    ChkCreditCard(cctype, ccnumber);
    }
    
function Check2()
    {
    var controlPrefix = "ctl00_ContentPlaceHolder1_";
    var ccnumber = document.getElementById(controlPrefix + "tbCreditCardNumber").value;
    var cctype = document.getElementById(controlPrefix + "ddlCardType").value;

    ChkCreditCard(cctype, ccnumber);
    }
function ChkCreditCardII(strCardType, strCardNumber)
{
    if(strCardNumber == null)
		return true;
		
	if(strCardNumber == '')
		return true;
   
    var cc = StripChar(strCardNumber, " -");

     // alert(cc);
	var strMessage = "We're sorry, the credit card information you've entered is incorrect.\n\nPlease try again.";

	if( cc.length > 19 || ! isCreditCard(cc) )
    {
      alert(strMessage);
      return false ;
  	}

	if(!ChkCardTypeII(strCardType, cc))
    {
      alert(strMessage);
      return false ;
  	}

	return true;
}

function isCreditCard(st) 
{
  if (st.length > 19)
    return false;

  sum = 0; mul = 1; l = st.length;
  for (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;
	  
   (mul == 1) ? mul++ : mul-- ;
   
  }
  return ((sum % 10) == 0) ? true : false;
}


function ChkCardType(strCardType,strCardNumber)
{
	//strCardType = strCardType.toUpperCase();
	var doesMatch = true;
	if ( strCardType == "VI" && !isVisa(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "MC" && !isMasterCard(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "AX"  && !isAmericanExpress(strCardNumber) ) 
	    doesMatch = false;
	if ( strCardType == "DI" && !isDiscover(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "JC" && !isJCB(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "DC" && !isDinersClub(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "CB" && !isCarteBlanche(strCardNumber) )
		doesMatch = false;
	if ((strCardType == "ER") && (!isEnRoute(strCardNumber)))
		doesMatch = false;
		
	return doesMatch;

}

function ChkCardTypeII(strCardType,strCardNumber)
{
	//strCardType = strCardType.toUpperCase();
	var doesMatch = true;
	if ( strCardType == "VISA" && !isVisa(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "MC" && !isMasterCard(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "AMX"  && !isAmericanExpress(strCardNumber) ) 
	    doesMatch = false;
	if ( strCardType == "DINER" && !isDiscover(strCardNumber) )
		doesMatch = false;
/*
	if ( strCardType == "JC" && !isJCB(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "DC" && !isDinersClub(strCardNumber) )
		doesMatch = false;
	if ( strCardType == "CB" && !isCarteBlanche(strCardNumber) )
		doesMatch = false;
	if ((strCardType == "ER") && (!isEnRoute(strCardNumber)))
		doesMatch = false;
*/		
	return doesMatch;

}

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (parseInt(cc.substring(0,1)) == 4))
    return isCreditCard(cc);
	
  return false;
}


function isMasterCard(cc)
{
  firstdig = parseInt(cc.substring(0,1));
  seconddig = parseInt(cc.substring(1,2));
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);

  return false;

}


function isAmericanExpress(cc)
{
  firstdig = parseInt(cc.substring(0,1));
  seconddig = parseInt(cc.substring(1,2));
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
	
  return false;

}


function isDinersClub(cc)
{
  firstdig = parseInt(cc.substring(0,1));
  seconddig = parseInt(cc.substring(1,2));
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
	
  return false;
}

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}

function isDiscover(cc)
{
  first4digs = parseInt(cc.substring(0,4));
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
	
  return false;

}

function isEnRoute(cc)
{
  first4digs = parseInt(cc.substring(0,4));
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
	
  return false;
}

function isJCB(cc)
{
  firstdig = parseInt(cc.substring(0,1));
  
  if ( cc.length == 16 &&
	    firstdig == "3"  )
    return isCreditCard(cc);
	
  return false;

}

function isUSZip(strZipCode)
{
   v = StripChar(strZipCode, "- ");

   if( isNaN(v) || (v.length != 5 && v.length != 9) )
   {
	  alert("We're sorry, the US Zip Code you've entered is incorrect.\n\nPlease try again.");
	  return false;
	}
   return true;

}
function isCAZip(strZipCode)
{
    v = StripChar(strZipCode, "- ");

	var regexp = /^[A-Va-v]|[X,Y,x,y]\d[A-Za-z]\d[A-Za-z]\d$/;

	if( v.length != 6 || hasChars(v,'D', 'F', 'I', 'O', 'Q', 'U') || v.search(regexp) == -1 )
   {
	  alert("We're sorry, the Canada Zip Code you've entered is incorrect.\n\nPlease try again.");
	  return false;
	}

	return true;

}

function hasChars(str)
{
    if(str == null)
		return false;

	str = str.toLowerCase();

	for(var i = 1; i < arguments.length ; i++)
	{
	      arguments[i] = arguments[i].toLowerCase();
	      if( str.indexOf( arguments[i] ) != -1 )
		  		return true;
	}

	return false;
}

function StripChar (strText, strTextToRemove)

{   
	var i;
    var returnString = "";

    for (i = 0; i < strText.length; i++)
    {
        var c = strText.charAt(i);
        if (strTextToRemove.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function ChkEmail(strText)
{
	var regexp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

    if(strText == null)
		return true;
		
	if(strText == '')
		return true;
   
	if(strText.search(regexp) == -1)
	{
	    alert("We're sorry, the Email Address you've entered is incorrect.\n\nPlease try again.");
   		return false;
	}
	
	return true;
}

function ChkCardDate(strTransYear,strTransMonth,strCardYear,strCardMonth)
{
	if( strCardMonth.charAt(0) == '0')
      strCardMonth = strCardMonth.substr(1);
	  
	if (strCardYear.length == 2) 
		 strCardYear = '20' + year ;
	  
	if ( parseInt(strCardYear) < parseInt(strTransYear) || ( parseInt(strCardYear) == parseInt(strTransYear) && parseInt(strCardMonth) < parseInt(strTransMonth)) )
	{
	    alert("We're sorry, the credit card Expiration Date information you've entered is incorrect.\n\nPlease try again.");
		return false;
	}
	
	
	return true;
}







