﻿    function IsValidCVV(cvv) {
       var cvvRegex = new RegExp('[0-9]{3}');
        return cvvRegex.test(cvv);
    }
    function IsValidCC(ccNumb) {  // v2.0
        var valid = "0123456789"  // Valid digits in a credit card number
        var len = ccNumb.length;  // The length of the submitted cc number
        var iCCN = parseInt(ccNumb);  // integer of ccNumb
        var sCCN = ccNumb.toString();  // string of ccNumb
        sCCN = sCCN.replace(/^\s+|\s+$/g, '');  // strip spaces
        var iTotal = 0;  // integer total set at zero
        var bNum = true;  // by default assume it is a number
        var bResult = false;  // by default assume it is NOT a valid cc
        var temp;  // temp variable for parsing string
        var calc;  // used for calculation of each digit

        // Determine if the ccNumb is in fact all numbers
        for (var j = 0; j < len; j++) {
            temp = "" + sCCN.substring(j, j + 1);
            if (valid.indexOf(temp) == "-1") { bNum = false; }
        }

        // if it is NOT a number, you can either alert to the fact, or just pass a failure
        if (!bNum) {
            /*alert("Not a Number");*/bResult = false;
        }

        // Determine if it is the proper length 
        if ((len == 0) && (bResult)) {  // nothing, field is blank AND passed above # check
            bResult = false;
        } else {  // ccNumb is a number and the proper length - let's see if it is a valid card number
            if (len >= 15) {  // 15 or 16 for Amex or V/MC
                for (var i = len; i > 0; i--) {  // LOOP throught the digits of the card
                    calc = parseInt(iCCN) % 10;  // right most digit
                    calc = parseInt(calc);  // assure it is an integer
                    iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
                    i--;  // decrement the count - move to the next digit in the card
                    iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
                    calc = parseInt(iCCN) % 10;    // NEXT right most digit
                    calc = calc * 2;                                 // multiply the digit by two
                    // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
                    // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
                    switch (calc) {
                        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
                        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
                        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
                        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
                        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
                        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
                    }
                    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
                    iTotal += calc;  // running total of the card number as we loop
                }  // END OF LOOP
                if ((iTotal % 10) == 0) {  // check to see if the sum Mod 10 is zero
                    bResult = true;  // This IS (or could be) a valid credit card number.
                } else {
                    bResult = false;  // This could NOT be a valid credit card number
                }
            }
        }
        return bResult; // Return the results
    }
    
     function isValidPostalCode(postalCode, countryCode) {
        switch (countryCode) {
            case "231":
                postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
                break;
            case "46":
                postalCodeRegex = /^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/;
                break;
            default:
                postalCodeRegex = /^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/;
        }
        return postalCodeRegex.test(postalCode.toUpperCase());
    }
    function IsValidEmail(Email) {
        EmailRegex = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        return EmailRegex.test(Email);
    }
    function IsValidPhoneNumber(PhoneNumber) {
        var PhoneNumberRegex = /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/
        return PhoneNumberRegex.test(PhoneNumber);
    }
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    function SetCartInfo() {    
	
            if(document.location.hostname.toLowerCase().indexOf("www") == -1)
            {
                window.location = "www." + document.location.hostname;
            }
            
        VisitorUrl();
        VisitorReferer();        
//        alert(readCookie("visitor_request"));
        var cart_info = readCookie("ContourCart");
        document.getElementById('cartinfo').innerHTML = cart_info;
        

    }
      function createCookie(name,value,days) {
    var ck = readCookie(name);
    if(ck == null)
    {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; domain=\"thecontour.com\" path=/";
            }
        }
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    function VisitorReferer() {    
          if (!document.referrer) {
            //document.write("No referrer, pal.")
          }
          else {
            SetCookie("visitor_ref",document.referrer,"1");
          }
          
    }
    function VisitorUrl() {
           SetCookie("visitor_request",location.href,"1");
          
    }
    
       function SetCookie(cookieName,cookieValue,nDays) {
    if(RdCookie(cookieName) == "")
    {
        
         var today = new Date();
         var expire = new Date();
         if (nDays==null || nDays==0) nDays=1;
         expire.setTime(today.getTime() + 3600000*24*nDays);
         document.cookie = cookieName+"="+cookieValue
                         + ";expires="+expire.toGMTString();
    }
}
function RdCookie(cookieName) {
 var theCookie=" "+document.cookie;
 var ind=theCookie.indexOf(" "+cookieName+"=");
 if (ind==-1) ind=theCookie.indexOf(";"+cookieName+"=");
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(";",ind+1);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+2,ind1));
}
