
productsPrice = new Array();
productsPrice[0] = new Array();		productsPrice[0][0] = "PN100_Q001";		productsPrice[0][1] = 299.95;	productsPrice[0][2] = 1.00;
productsPrice[1] = new Array();		productsPrice[1][0] = "PN100_H001";		productsPrice[1][1] = 259.95;	productsPrice[1][2] = 1.00;
productsPrice[2] = new Array();		productsPrice[2][0] = "PN100_N001";		productsPrice[2][1] = 199.95;	productsPrice[2][2] = 9.00;
productsPrice[3] = new Array();		productsPrice[3][0] = "PN200_S011";		productsPrice[3][1] = 50.00;	productsPrice[3][2] = 12.00;
productsPrice[4] = new Array();		productsPrice[4][0] = "PN300_bbc1";		productsPrice[4][1] = 555.00;	productsPrice[4][2] = 0.00;
productsPrice[5] = new Array();		productsPrice[5][0] = "PN400_W001";		productsPrice[5][1] = 9.95;		productsPrice[5][2] = 0.00;
productsPrice[6] = new Array();		productsPrice[6][0] = "PN400_W003";		productsPrice[6][1] = 14.95;	productsPrice[6][2] = 0.00;
productsPrice[7] = new Array();		productsPrice[7][0] = "PN500_CF01";		productsPrice[7][1] = 69.95;	productsPrice[7][2] = 0.00;
productsPrice[8] = new Array();		productsPrice[8][0] = "PN500_CF01-6m";productsPrice[8][1] = 49.95;	productsPrice[8][2] = 1.00;
productsPrice[9] = new Array();		productsPrice[9][0] = "PN500_PMM";		productsPrice[9][1] = 39.95 ;	productsPrice[9][2] = 0.00;
productsPrice[10] = new Array();	productsPrice[10][0] = "PN600_V001";	productsPrice[10][1] = 19.95;	productsPrice[10][2] = 0.00;
productsPrice[11] = new Array();	productsPrice[11][0] = "PN600_V001-D";productsPrice[11][1] = 19.95;	productsPrice[11][2] = 0.00;
productsPrice[12] = new Array();	productsPrice[12][0] = "PN600_V002";	productsPrice[12][1] = 24.95;	productsPrice[12][2] = 0.00;
productsPrice[13] = new Array();	productsPrice[13][0] = "PN600_V002-D";productsPrice[13][1] = 24.95;	productsPrice[13][2] = 0.00;
productsPrice[14] = new Array();	productsPrice[14][0] = "PN600_V005";	productsPrice[14][1] = 14.95;	productsPrice[14][2] = 0.00;
productsPrice[15] = new Array();	productsPrice[15][0] = "PN600_V007";	productsPrice[15][1] = 19.95;	productsPrice[15][2] = 0.00;
productsPrice[16] = new Array();	productsPrice[16][0] = "PN600_MemKit";productsPrice[16][1] = 89.95;	productsPrice[16][2] = 0.00;
productsPrice[17] = new Array();	productsPrice[17][0] = "PN100_P001";	productsPrice[17][1] = 30.00;	productsPrice[17][2] = 0.00;
productsPrice[18] = new Array();	productsPrice[18][0] = "PN100_P002";	productsPrice[18][1] = 25.00;	productsPrice[18][2] = 0.00;
productsPrice[19] = new Array();	productsPrice[19][0] = "PN100_P003";	productsPrice[19][1] = 35.00;	productsPrice[19][2] = 0.00;
productsPrice[20] = new Array();	productsPrice[20][0] = "PN100_P004";	productsPrice[20][1] = 45.00;	productsPrice[20][2] = 0.00;
productsPrice[21] = new Array();	productsPrice[21][0] = "PN100_P005";	productsPrice[21][1] = 60.00;	productsPrice[21][2] = 0.00;
productsPrice[22] = new Array();	productsPrice[22][0] = "PN100_P006";	productsPrice[22][1] = 10.00;	productsPrice[22][2] = 0.00;
productsPrice[23] = new Array();	productsPrice[23][0] = "PN100_P007";	productsPrice[23][1] = 21.00;	productsPrice[23][2] = 0.00;
productsPrice[24] = new Array();	productsPrice[24][0] = "PN100_P008";	productsPrice[24][1] = 3.50;	productsPrice[24][2] = 0.00;
productsPrice[25] = new Array();	productsPrice[25][0] = "PN100_P011";	productsPrice[25][1] = 5.00;	productsPrice[25][2] = 0.00;
productsPrice[26] = new Array();	productsPrice[26][0] = "PN100_P012";	productsPrice[26][1] = 6.00;	productsPrice[26][2] = 0.00;
productsPrice[27] = new Array();	productsPrice[27][0] = "PN100_P013";	productsPrice[27][1] = 12.00;	productsPrice[27][2] = 0.00;
productsPrice[28] = new Array();	productsPrice[28][0] = "PN700_C001";	productsPrice[28][1] = 495.00; productsPrice[28][2] = 0.00;
productsPrice[29] = new Array();	productsPrice[29][0] = "PN700_C002";	productsPrice[29][1] = 250.00; productsPrice[29][2] = 0.00;

var prodsArray = new Array();

function calcTotal(thisForm, thisProd){
	//alert(4)
	var subtotal = 0;
	var numProds = 0;
	var shippingCosts = 0;
	for (i=0; i < productsPrice.length; i++) {
		subtotal += ((productsPrice[i][1]*100) * thisForm.elements[productsPrice[i][0]].value);
		numProds += parseInt(thisForm.elements[productsPrice[i][0]].value);
		shippingCosts += (thisForm.elements[productsPrice[i][0]].value * productsPrice[i][2]);
	}
	//alert(numProds);
	subtotal = (Math.round(subtotal)/100);
	
	if (!numProds) {
		shippingCosts = 0;
	} else {
		if (shippingCosts == 0) {
			shippingCosts = 7.00;
		}
	}
	
	
	if (!numProds) {
		shippingCosts = 1;
	} else {
		if (shippingCosts == 1) {
			shippingCosts = 0.00;
		}
	}
	
	
	var pushProd = true;
	for (i=0; i < prodsArray.length; i++) {
		if(thisProd == prodsArray[i]) {
			pushProd = false;
			break;
		} else {
			pushProd = true;
		}
	}
	if (parseInt(thisForm.elements[thisProd].value) > 0) {
		if (pushProd) {
			prodsArray.push(thisProd);
		}
	} else {
		prodsArray.pop(thisProd);
	}
	if ((prodsArray.length == 1) && (prodsArray[0] == "PN300_bbc1")) {
		shippingCosts = 0.00;
	}
	if ((prodsArray.length == 1) && (prodsArray[0] == "PN500_CF01-6m")) {
		shippingCosts = 0.00;
	}
	thisForm.prodsOrdered.value = prodsArray.toString();
	//alert(thisForm.prodsOrdered.value);
	
	thisForm.subtotal.value = calcDecimal(subtotal);
	thisForm.shipping.value = calcDecimal(shippingCosts);
	thisForm.total.value = calcDecimal(parseFloat(subtotal) + shippingCosts);
	
}

function calcDecimal(thisVal) {
	thisVal = thisVal.toString();
	if(thisVal.indexOf(".") > 0) {
		tempArray = thisVal.split(".");
		if(tempArray[1].length == 1){
			return tempArray[0] + "." + tempArray[1] + "0";
		}
		if(tempArray[1].length == 2) {
			return thisVal;
		}
	} else {
		return thisVal + ".00";
	}
	return thisVal;
}

var strAlert = "";
function validate(thisForm) {
	var returnResult = true;
	
	if(isEmpty(thisForm.elements["A. First Name"], "Please enter your first name.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["B. Last Name"], "Please enter your last name.\n")) {
		returnResult = false;
	}
	
	
	if(isEmpty(thisForm.elements["H. Phone"], "Please enter a valid phone number.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["I. EMail"], "Please enter a valid email address.\n")) {
		returnResult = false;
	} else {
		if(!isValidEmail(thisForm.elements["I. EMail"].value)) {
			strAlert += "Please enter a valid email address.\n";
			returnResult = false;
		}	
	}
	
	if(isEmpty(thisForm.elements["C. Address 1"], "Please enter your address.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["E. City"], "Please enter your City.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["G. Zip"], "Please enter your Zip.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["K. CCNumber"], "Please enter a valid credit card number.\n")) {
		returnResult = false;
	}
	
	if(isEmpty(thisForm.elements["L. CCExpiration"], "Please enter a valid credit card expiration date.\n")) {
		returnResult = false;
	}
	
	/*if(thisForm.fax.value) {
		if(!validatePhone(thisForm.fax)) {
			strAlert += "Please enter a valid fax number.\n";
			returnResult = false;
		}
	}
	
	if (thisForm.contactprefs.options[thisForm.contactprefs.selectedIndex].value == "") {
		strAlert += "Please select a contact preference.\n";
		returnResult = false;
	}
	
	if(thisForm.howhear.options[thisForm.howhear.selectedIndex].value == ""){
		strAlert += "Please select how you heard about GuruLabs Training.\n";
		returnResult = false;
	} else {
		if(thisForm.howhear.options[thisForm.howhear.selectedIndex].value == "other") {
			if(isEmpty(thisForm.howhear_other, "Please enter how you heard about GuruLabs in the \"other\" field.\n")) {
				returnResult = false;
			}
		}
	}*/
	
	if(returnResult == false) {
		alert(strAlert);
	}
	strAlert = "";
	return returnResult;
}

function isEmpty(thisField,thisAlert) {
	var thisVal = thisField.value;
	if(thisVal == "") {
		strAlert += thisAlert;
		return true;
	} else {
		return false;
	}
}

function isValidEmail(email){
	invalidChars = " /;:,";
	
	for(i=0; i < invalidChars.length; i++){
		badChar = invalidChars.charAt(i)
		if(email.indexOf(badChar,0) > -1){
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if(atPos == -1){
		return false
	}
	if(email.indexOf("@",atPos+1) > -1){
		return false
	}
	periodPos = email.indexOf(".",1)
	if(periodPos == -1){
		return false
	}
	if(periodPos+3 > email.length){
		return false
	}
	return true
}

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Master Card") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "American Express") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
