function checkCSC(cardTyp,fldValue){
    var re = null;
    if(cardTyp != null){
        if(cardTyp == "American Express"){
            re = /^\d{4}$/;
            return re.test(fldValue);
            //Mastercard, Visa, Discover
        } else {
             re = /^\d{3}$/;
            return re.test(fldValue);

        }
    }
}

//credit-card number, security code, credit-card type, and expiration date
function verify(ccard, secure_code, cctype, ccexp) {
    var res;
    var today = new Date();
    var monthYear = ccexp.split("/");
    var dateExp = new Date(parseInt("20" + monthYear[1]), parseInt(monthYear[0]) - 1, today.getDate());
    if(!esFecha2aIgualoSup(today, dateExp)) {
        res = -1;
    } else if(secure_code.length < 3) {
        //eMsg("Please enter a valid value for the security code.","red");
        res = 1;
    } else if(cctype=="Choose one...") {
        //eMsg("Please enter a valid value for the credit card type.","red");
        res = 2;
    } else if(!clientsideVerify(ccard)) {
        //eMsg("Please enter a valid value for the credit card.","red");}
        res = 3;
    } else {
        //eMsg("Please wait while we process the credit card.","blue");
        res = remDashSpace(ccard);
        if(res == ccard) res = 0;
//        url="http://www.parkerriver.com/s/verify?cc="+
//            encodeURIComponent(ccard)+"&scode="+
//            encodeURIComponent(secure_code)+"&type="+
//            encodeURIComponent(cctype)+"&exp="+
//            encodeURIComponent(ccexp);
//        httpRequest("GET",url,true,handleCheck);
    }
    
    return res;
}

/* Check whether the credit-card entry is null, not lengthy enough,
  or contains any letters. Remove any dashes or spaces from the entry, then
   run the Luhn algorithm on the resulting number. */
function clientsideVerify(ccVal){
    if(ccVal == null || ccVal.length < 13 ||
       ccVal.search(/[a-zA-Z]+/) != -1){ return false; }
    ccVal=remDashSpace(ccVal);
    return (applyLuhn(ccVal) % 10) == 0;

}

function remDashSpace(_number){
    //remove dashes or spaces
    _number = _number.replace(/-/g,"");
    _number = _number.replace(/ /g,"");
    return _number;
}

//http://en.wikipedia.org/wiki/Luhn_formula
function applyLuhn(cc){
    //reverse the String
    var rev = reverse(cc);
    //get array of character Strings
    var revArr = rev.split("");
    var total = 0;
    var tmp = 0;
    //add up the numbers
    for(var i = 0; i < revArr.length; i++){
        if((i % 2) > 0){
            tmp = revArr[i]*2;
            tmp= (tmp < 9 ? tmp : (tmp - 9) );
            total += tmp;
        }   else {
            total += Number(revArr[i]);
        }
    }//end for
    //alert(total);
    return total;
}

//event handler for XMLHttpRequest
function handleCheck(){
    var sTag,answer,xmlReturnVal;
    if(request.readyState == 4){
        if(request.status == 200){
            //Implement document object in DOM
            xmlReturnVal = request.responseXML;
            sTag = xmlReturnVal.getElementsByTagName("cc_status")[0];
            answer= sTag.childNodes[0].data;
            if(answer=="okay"){ eMsg("Your purchase information has"+
                                     " been submtted to our online store.","blue");  }
            else { eMsg("There was a problem with processing the credit card.","red"); }
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}

/* Utility functions:
reverse a string. */
function reverse(str){

    var sArray = str.split("");
    var newS="";
    for(var i = sArray.length-1; i >= 0; i--){
        newS += sArray[i];
    }
    return newS;
}

/*Generate a styled message */
function eMsg(msg,sColor){
    var div = document.getElementById("message");
    div.style.color=sColor;
    div.style.fontSize="0.9em";
    //remove old messages
   clearMsg();
   div.appendChild(document.createTextNode(msg));
}

function clearMsg(){
   document.getElementById("message").innerHTML="";
}
