// JavaScript Document

function compareData() {
	var theForm = document.getElementById('orderForm');
	var enabled = theForm.useshipping.checked;
	
	if (enabled == true) {
		theForm.name.value = theForm.b_name.value;
		theForm.add1.value = theForm.b_add1.value;
		theForm.add2.value = theForm.b_add2.value;
		theForm.city.value = theForm.b_city.value;
		theForm.st.value = theForm.b_state.value;
		theForm.zip.value = theForm.b_zip.value;
	} else {
		theForm.name.value = '';
		theForm.add1.value = '';
		theForm.add2.value = '';
		theForm.city.value = '';
		theForm.st.value = '';
		theForm.zip.value = '';
	}
}

function ValidatePublicForm(theForm) 
		{
				var reason = "";
				
				reason += validateField(theForm.b_name, "BILLING NAME");
				reason += validateField(theForm.b_email, "BILLING EMAIL");
				reason += validateField(theForm.b_add1, "BILLING ADDRESS");
				reason += validateField(theForm.b_city, "BILLING CITY");
				reason += validateField(theForm.b_state, "BILLING STATE");
				reason += validateField(theForm.b_zip, "BILLING ZIP");
				reason += validateField(theForm.ccname, "NAME ON CARD");
				reason += validateCard(theForm.cctype, theForm.ccnum);
				reason += validateField(theForm.ccexp, "CREDIT CARD EXPIRATION DATE");
				reason += validateField(theForm.cccode, "CREDIT CARD SECURITY CODE");
				reason += validateField(theForm.name, "SHIPPING NAME");
				reason += validateField(theForm.add1, "SHIPPING ADDRESS");
				reason += validateField(theForm.city, "SHIPPING CITY");
				reason += validateField(theForm.st, "SHIPPING STATE");
				reason += validateField(theForm.zip, "SHIPPING ZIP CODE");
				
				
				if (theForm.shipvia.selectedIndex == 0) {
					//reason += "SELECT A SHIPPING METHOD" + "\n";
					//theForm.shipvia.style.background = 'Yellow';
				} else {
					theForm.shipvia.style.background = 'White';
				}
				
					if (reason != "") {
    					alert("These fields need values:\n" + reason);
    					return false;
  					} else {
						return true;
					}

  			
		}
		
function validateCard(cardTypeFld, cardFld) 
{
	var error = '';
	var cardType = cardTypeFld.options[cardTypeFld.selectedIndex].value.toLowerCase();
	var cardNumber = cardFld.value;
	var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
      case "mc":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;

      case "visa":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;

      case "amex":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;
		
	  case "discover":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^6011/;
        break;

      default:
        prefixRegExp = /^$/;
        alert("Card type not found");
    }

    // Test if card prefix is valid
	prefixIsValid = prefixRegExp.test(cardNumbersOnly);
	
    isValid = prefixIsValid && lengthIsValid;

	if (!isValid) {
		cardFld.style.background = 'Yellow';
		error = "CREDIT CARD IS NOT VALID \n"; 
	} else {
		cardFld.style.background = 'White';
	}
	return error;
}
		
function validateField(fld, fieldName) 
{
    var error = "";
	var name = fieldName;
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = name + "\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
