 $(document).ready(function() {
	$("#sidebarMenu").corner("10px");
	var currentPage = $("#contentWrap h2:first").text()
	$("#supplies input:lt(25)").val(0)

	$("#supplies tr:odd").addClass('striped');

	$("#supplies tr td:not('#info') input:not(:submit)").blur(function() {
		
		if($(this).val().length > 0){
		var itemChoosenBgColor = "#337ded"
		var currentBgColor = ""
		
		if( $(this).parent().parent().attr("class") == "striped") {
			currentBgColor= "#1996e4"
		} else {currentBgColor= "#3caef5"}
		
		
		//rounds value
		$(this).val(Math.round($(this).val()))
		
		
		//subtotals item cost and check
		if(!isNaN($(this).val()) && $(this).val() != 0){
			
			//add color to indicate choosen
			$(this).parent().parent().animate({ backgroundColor: itemChoosenBgColor }, 1000);
			
			$(this).parent().children(".error").fadeOut();
		
			var itemPrice = $(this).parent().prev().prev().text();
			itemPrice = itemPrice.substring(1,itemPrice.length)
			//var itemPrice = new Number(itemPrice)
	
			var itemQuan = $(this).val()
			var finalItemCost = itemQuan * itemPrice
			finalItemCost = roundPrice(finalItemCost,3)
		
			//if user enter new quan.
			if($(this).parent().children("small").length == 0){
				
				$(this).parent().append("<small>$"+finalItemCost+"</small>");
				} else {
				$(this).parent().children("small").replaceWith("<small>$"+finalItemCost+"</small>");
			}
			//console.log("1st if")
		} else {
		//removes warning if 0
			if($(this).val() == 0){
			$(this).parent().children(".error").fadeOut();
			}
			
			$(this).parent().parent().animate({ backgroundColor: currentBgColor }, 1000);
			
			$(this).parent().children("small").fadeOut();
			$(this).parent().parent().removeClass("itemChoosen")
			
			
			if($(this).parent().children(".error").length == 0 && $(this).val() != 0){ 
				$(this).parent().append("<span class='error'>Please enter a number</span>");
				
			}
			$(this).val(0);
			
			//console.log("2nd if");
		}

		
	}
		cartTotal()
	});
	
	
	$("#sidebarMenu li a").each(function (i) {
        if ($(this).text() == currentPage) {
          $(this).addClass("current");
        } 
      });

	$("form").submit(function() {
	var error = 0
		$("#info input:not(:submit)").each( function(){
			
			$(this).parent().removeClass('error')
			
			
			switch($(this).attr('name')){
				case 'email':
					//checks email validation
  					var emailCheck = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					var emailVal = $(this).val()
					if(!emailVal.match(emailCheck)){
						$(this).parent().addClass('error');
						error++
					}
				break;
				
				case 'phone':
					var phone = $(this).val()
  					var phoneStripped = phone.replace(/[\(\)\.\-\ ]/g, '');
					//strip out acceptable non-numeric characters
					if (isNaN(parseInt(phoneStripped))) {
				    //console.log("The phone number contains illegal characters.");
					}
					if (!(phoneStripped.length == 10)) {
						$(this).parent().addClass('error');
						error++
					}
				break;
				
				case 'zip':
					var zipVal = $(this).val()
					if(isNaN(zipVal) || zipVal.length < 5) {
						$(this).parent().addClass('error');
						//console.log(zipVal.length)
						error++;
					}
				break;
				
				default:
				//console.log('case: '+$(this).attr('name'))
				if(!$(this).val().length > 0 ) {
					$(this).parent().addClass('error');
					error++;
				}
				
			}//end switch
	
		
		});
		
		if(error > 0) {
		
			if($("#submit").parent().children('strong').length == 0){
				$("#submit").parent().append("<strong style='color:#ff0'><br />Error: Invalid or Incomplete fields!</strong>")} else {
				
				$("#submit").parent().children('strong').replaceWith("<strong style='color:#ff0'><br />Error: Invalid or Incomplete fields!</strong>")
				}
				return false;
			}
		
		//console.log('submit')
		
	});

 });  
 
  function cartTotal() {
 	var totalCost = 0
	for(i=0;i<25;i++) {
 		var itemQuan = $("#supplies input:eq("+i+")").val()
 		itemQuan = parseFloat(itemQuan)
 		
 		var itemCost =  $("#supplies input:eq("+i+")").parent().prev().prev().text();
 		itemCost = itemCost.substring(1,itemCost.length);
 		
 		totalCost += itemQuan * itemCost
 		
 	
 		
 	}
 	totalCost = roundPrice(totalCost,2)
 	$("#totalCost").text("$"+totalCost)
 	
 }
 
 function roundPrice(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	//console.log("result: "+ result)
	return result;
}