﻿// JScript File
//
//

var Validator = Validator ? Validator : {

    OnlyInt : function(evt)
    { 
        var charCode = (evt.which != undefined) ? evt.which : event.keyCode;
        if (charCode == 13 || (charCode > 31 && (charCode < 48 || charCode > 57)))
            return false;
         return true;
    },
    
    BillingScreen : function()
    {
        var retVal = true;
        
        if (this.ValidateValue ('bFirstName', 'bvFirstName') == false)
            retVal = false;
        if (this.ValidateValue ('bLastName', 'bvLastName') == false)
            retVal = false;
        if (this.ValidateValue ('bStAddress1', 'bvStAddress1') == false)
            retVal = false;
        if (this.ValidateValue ('bCity', 'bvCity') == false)
            retVal = false;
        if (this.ValidateValue ('bZip', 'bvZip') == false)
            retVal = false;
        if (this.ValidateValue ('bPhone', 'bvPhone') == false)
            retVal = false;
        if (this.ValidateEmail ('bEmail', 'bvEmail') == false)
            retVal = false;
        if (this.ValidateValue ('bVerify', 'bvVerify') == false)
            retVal = false;
        if (this.ValidateValue ('bState', 'bvState') == false)
            retVal = false;

        
        //emails must match
        var clr = '#FFFFFF';
        if ((document.getElementById('bEmail').value != document.getElementById('bVerify').value) || document.getElementById('bEmail').value.length == 0)
        {
            clr = 'Red';
            retVal = false;
        }
        document.getElementById('bvEmail').style.color = clr;
        document.getElementById('bvVerify').style.color = clr;
        
        if (document.getElementById('divPassword').className != 'hide')
        {
            if (document.getElementById('bPwd').value != document.getElementById('bConfirmPWD').value)
            {
                document.getElementById('bvPwd').style.color = "Red";
                document.getElementById('bvConfirmPwd').style.color = "Red";
                retVal = false;
            }
        }
        
        //if we dont have an error then validate the CC info
        if (retVal == true)
        {
            if(this.ValidateCCInfo() == false)
                retVal = false;
        }
        else
        {
            d.Message("Errors", "Please correct the errors before continuing.");
        }

        
        return retVal;
    },
    
    ShippingScreen : function()
    {
        var retVal = true;
        
        if (this.ValidateValue ('sFirstName', 'svFirstName') == false)
            retVal = false;
        if (this.ValidateValue ('sLastName', 'svLastName') == false)
            retVal = false;
        if (this.ValidateValue ('sStAddress1', 'bvStAddress1') == false)
            retVal = false;
        if (this.ValidateValue ('sCity', 'svCity') == false)
            retVal = false;
        if (this.ValidateValue ('sZip', 'svZip') == false)
            retVal = false;
        if (this.ValidateValue ('sPhone', 'svPhone') == false)
            retVal = false;
        if (this.ValidateValue ('sState', 'svState') == false)
            retVal = false;
               
        return retVal;
    },
    
    SignInScreen : function(fields)
    {
        var retVal = true;
        
        for (i=0; i<fields.length; i++)
        {
            if (this.ValidateValue (fields[i][0], fields[i][1]) == false)
                retVal = false;
        }
        
        return retVal;                                                  
    },
    
    ValidateValue : function(f, errorField)
    {
        var retVal = true;
        
        if(document.getElementById(f).value.length == 0)
        {
            document.getElementById(errorField).style.color = "Red";
            retVal = false;
        }
        else
            document.getElementById(errorField).style.color = "#FFFFFF";
            
        return retVal;        
    },
    
    ValidateEmail : function(f, errorField)
    {
        var s = document.getElementById(f).value;
        var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        if(pattern.test(s))
		    return "true";
        else
		    return "false";
    },
    
    ValidateCCInfo : function()
    {        
        if (document.getElementById('txtbCID').value.length == 0)
        {
            d.Message("Invalid CID Number", "Please insert the credit card CID number");
            return false;        
        }
        if (document.getElementById('ccExpMonth').value.length == 0)
        {
            d.Message("Invalid Month", "Please select the credit card expiration month");
            return false;
        }
        if (document.getElementById('ccExpYr').value.length == 0)
        {
            d.Message("Invalid Year", "Please select the credit card expiration year");
            return false;
        }

        var cardType = document.getElementById('ddlPaymentType').value;
        cardType = cardType.substring(0,1);
        return this.checkCard(cardType, document.getElementById('txtbCCNum').value);
    },
    
    
    checkCardNumWithMod10 : function(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;
    },

    cleanCardNum : function(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
				    d.Message("Invalid Card Number", "The card number contains invalid characters.");
				    return "";
			    }
		    }
		    i++;
	    }

	    // we got here if we didn't fail, so return what we built
	    return newCard;
    },

    checkCard : function(cardType, cardNum) 
    {
	    var validCard;
	    var cardLength;
	    var cardLengthOK;
	    var cardStart;
	    var cardStartOK;
    	
	    // check if the card type is valid
	    if ((cardType != "V") && (cardType != "M") && (cardType != "A") && (cardType != "D")) {
		    d.Message("Select Credit Card Type", "Please select a card type.");
		    return false;
	    }

	    // clean up any spaces or dashes in the card number
	    validCard = this.cleanCardNum(cardNum);
	    if (validCard != "") {
		    // check the first digit to see if it matches the card type
		    cardStart = validCard.substring(0,1);
		    cardStartOK = ( ((cardType == "V") && (cardStart == "4")) ||
				    ((cardType == "M") && (cardStart == "5")) ||
				    ((cardType == "A") && (cardStart == "3")) ||
				    ((cardType == "D") && (cardStart == "6")) );
		    if (!(cardStartOK)) {
			    // card number's first digit doesn't match card type
			    d.Message("Invalid Card", "You have entered an invalid credit card number.");
			    return false;
		    }

		    // the card number is good now, so check to make sure
		    // it's a the right length
		    cardLength = validCard.length;		
		    cardLengthOK = ( ((cardType == "V") && ((cardLength == 13) || (cardLength == 16))) ||
				     ((cardType == "M") && (cardLength == 16)) ||
				     ((cardType == "A") && (cardLength == 15)) ||
				     ((cardType == "D") && (cardLength == 16)) );
		    if (!(cardLengthOK)) {
			    // not the right length
			    d.Message("Invalid Card", "Please make sure you've entered all of the digits on your card.");
			    return false;
		    }

		    // card number seems OK so do the Mod10
		    if (this.checkCardNumWithMod10(validCard)) {
			    return true;
		    } else {
			    d.Message("Invalid Card", "You have entered an invalid credit card number.");
			    return false;
		    }
	    } 
	    else 
	    {
	        d.Message("Invalid Card", "You have entered an invalid credit card number.");
		    return false;
	    }
    }
}
