// JavaScript Document

<!--
function vcc(textObj) {

   var ccNum;
   var odd = 1;
   var even = 2;
   var calcCard = 0;
   var calcs = 0;
   var ccNum2 = "";
   var aChar = '';
   var cc;
   var r;
   
   ccNum = textObj;
   for(var i = 0; i != ccNum.length; i++) {
      aChar = ccNum.substring(i,i+1);
      if(aChar == '-') {
         continue;
      }

      ccNum2 = ccNum2 + aChar;
   }
   
   cc = parseInt(ccNum2);
   if(cc == 0) {
      return false;
   }
   r = ccNum.length / 2;
   if(ccNum.length - (parseInt(r)*2) == 0) {
      odd = 2;
      even = 1;
   }
   
   for(var x = ccNum.length - 1; x > 0; x--) {
      r = x / 2;
      if(r < 1) {
         r++;
      }
      if(x - (parseInt(r) * 2) != 0) {
         calcs = (parseInt(ccNum.charAt(x - 1))) * odd;
      }
      else {
         calcs = (parseInt(ccNum.charAt(x - 1))) * even;
      }
      if(calcs >= 10) {
         calcs = calcs - 10 + 1;
      }
      calcCard = calcCard + calcs;
   }
   
   calcs = 10 - (calcCard % 10);
   if(calcs == 10) {
      calcs = 0;
   }
   
   if(calcs == (parseInt(ccNum.charAt(ccNum.length - 1)))) {
      return true;
   }
   else {
      return false;
   }
}
function required_field(valor){
	var error ="";
	if(valor ==""){
		return  "Please fill all fields. \n ";
	}else{
		return "";
	}
}


 
function checkRadio(checkvalue,queja) {
var error = "";
   if (!(checkvalue)) {
       error = queja + "\n";
    }
return error;    
}

function required_field2(valor,queja){
	var error ="";
	if(valor ==""){
		return  "Please enter a "+ queja + ". \n ";
	}else{
		return "";
	}
}
function validate_cc(valor){
	if(valor != ""){
		var error = "";
		if(vcc(valor)==false){
			error =  "Invalid credit card number. \n ";
		}
		return error;
	
	}else{
		return "Please enter a credit card number. \n ";
	}
}

function month(choice ) {
    var error = "";
    if (choice == 0) {
       error = "Please select an expiration month.\n";
    }    
	return error;
} 

function year(choice ) {
    var error = "";
    if (choice == 0) {
       error = "Please select an expiration year.\n";
    }    
	return error;
} 
function drop_down (choice, queja ) {
    var error = "";
    if (choice == 0) {
       error = "Please select a " + queja +".\n";
    }    
	return error;
} 



function only_numbers(obj) {
	var valid = "0123456789"
	var ok = "yes";
	var error = "";
	var temp;
	for (var i=0; i < obj.length; i++) {
		temp = "" + obj.substring(i, i+1);
		if (valid.indexOf(temp) == "-1"){ 
			ok = "no";
		}
	}


	if (ok == "no") {
		error ="Please enter only numbers in the credit card field \n ";
	}
	return error;
}
function blank_or_numbers(obj,queja) {
	if(obj != ""){
		var valid = "0123456789"
		var ok = "yes";
		var error = "";
		var temp;
		for (var i=0; i < obj.length; i++) {
			temp = "" + obj.substring(i, i+1);
			if (valid.indexOf(temp) == "-1"){ 
				ok = "no";
			}
		}
	
	
		if (ok == "no") {
			error ="Please enter only numbers in " + queja+" \n ";
		}
		return error;
	}else{
		return "";
	}
}

function validate_form(forma){
	var	mensaje = "";
	
	// Billing address
	mensaje += required_field2(forma.b_first_name.value, "billing first name");
	mensaje += required_field2(forma.b_last_name.value, "billing last name");
	mensaje += required_field2(forma.b_address_1.value, "billing address");
	mensaje += required_field2(forma.b_city.value, "billing city");
	mensaje += drop_down(forma.b_state.value,"billing state");
	mensaje += drop_down(forma.b_country.value,"billing country");
	mensaje += required_field2(forma.b_zip_code.value, "billing address zip code");
	mensaje += required_field2(forma.b_phone_number.value, "billing address phone number");
	mensaje += required_field2(forma.b_email.value, "billing address email");
	
	// forma.ship_address.value
	for (i=0, n=forma.ship_address.length; i<n; i++) {
	   if (forma.ship_address[i].checked) {
		  var checkvalue = forma.ship_address[i].value;
		  break;
	   }
	}
	mensaje += checkRadio(checkvalue ,"Please select or enter a shipping address");
	if(checkvalue == 'new'){
		// la direccion es nueva tenemos que validarla ....
		// Shipping address
		mensaje += required_field2(forma.s_first_name.value, "shipping first name");
		mensaje += required_field2(forma.s_last_name.value, "shipping last name");
		mensaje += required_field2(forma.s_address_1.value, "shipping address");
		mensaje += required_field2(forma.s_city.value, "shipping city");
		mensaje += drop_down(forma.s_state.value,"shipping state");
		mensaje += drop_down(forma.s_country.value,"shipping country");
		mensaje += required_field2(forma.s_zip_code.value, "shipping address zip code");
	}
	
	// credit card info
	mensaje += drop_down(forma.order_card_type.value,"credit card type");
	mensaje += only_numbers(forma.order_card_number.value);
	mensaje += validate_cc(forma.order_card_number.value);
	mensaje += blank_or_numbers(forma.order_ccid.value,"CVV2 field") 
	mensaje += month(forma.order_expiration_month.value);
	mensaje += year(forma.order_expiration_year.value);
	

	if(mensaje !=""){
		alert(mensaje);
		return false;
	}else{
		return true;
	}

}


// -->

