function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return (result3)
}


function calculateTotal(name) {
	var getState = name.state.value
	if (name.state.value == "CA"){
		getState = 1.0725
	} else { getState = 1 }
	
	
	var packageNum = name.PackageQuantity.value * 295
	var packSalesTax =  ((getState * packageNum) - packageNum)
	name.SalesTax.value = round_decimals(packSalesTax, 2)
	var packageTax = ((getState * packageNum) + 12.50)
	if (name.PackageQuantity.value == "0"){
	packageTax = 0 }
	name.PackageSubTotal.value = round_decimals(packageTax, 2)
	
	var shirtNum = (eval(name.youthMedShirt.value * 12) + eval(name.youthLargeShirt.value * 12) + eval(name.SmallShirt.value * 14) + eval(name.MediumShirt.value * 14) + eval(name.LargeShirt.value * 14) + eval(name.XLargeShirt.value * 14)) 
	var shirtSalesTax =  ((getState * shirtNum) - shirtNum)
	name.SalesTax2.value = round_decimals(shirtSalesTax, 2)
	var shirtTax = (getState * shirtNum)
	name.ShirtSubTotal.value = round_decimals(shirtTax, 2)
	var total = shirtTax + packageTax
    name.TotalOrder.value = round_decimals(total,2)
}

function setText(text){
var settext = order.name.value
	document.confirm.conf_name.value = settext
	settext = order.phone.value
	document.confirm.phone.value = settext
	settext = order.email.value
	document.confirm.email.value = settext
	settext = order.school.value
	document.confirm.school.value = settext
	settext = order.street.value + '\n'+ order.city.value + ', ' + order.state.value + ' ' + order.zip.value
	document.confirm.address.value = settext
	settext ='Help-A-Thon Package: ' + order.PackageQuantity.value + '\n' + 'T-Shirts: ' + 'YM-' + order.youthMedShirt.value + ' YL-' + order.youthLargeShirt.value + ' Sm-' + order.SmallShirt.value + ' Md-' + order.MediumShirt.value + ' Lg-' + order.LargeShirt.value + ' Xl-' + order.XLargeShirt.value
	document.confirm.ProductsOrdered.value = settext 
	settext = order.TotalOrder.value
	document.confirm.TotalCharge.value = settext
	
settext = order.cardholder_name.value
	document.confirm.card_name.value = settext
	
settext = order.credit_card_number.value
	document.confirm.card_number.value = settext
	
settext = order.Expiration_Month.value + '/' + order.Expiration_Year.value
	document.confirm.expiration.value = settext
	document.confirm.submitcheck.value = 1
	document.confirm.heardabout.value = order.HearAbout.value
}

//checks to make sure that the user has pressed the confirm button befor submitting the 'confrim' form
function checkforsubmit(){
if (document.confirm.submitcheck.value == 0)
{ alert("Please enter all of your order information and click the Confirm Button above")
	location = "#order";
return false
}
if (document.confirm.submitcheck == 1)
{
//Otherwise, go ahead and submit
    return true
}
 
}



//Confirm button validated the fields and the writes the fields for the Confirmation form 
function validate(current_form) {
	//checkShirtNumber()
    var missing_fields = new Array()
    var total_missing = 0
    
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Is this a visible text field that's mandatory?
        if ((current_form[counter].type == "text" ||
            current_form[counter].type == "textarea" ||
            current_form[counter].type == "password") &&
            current_form[counter].mandatory) 
			

			{
            
            // Is it empty?
            if (its_empty(current_form[counter].value)) {
              
                // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                total_missing++
            }
        }
    }

    // Were there any fields missing?
    if (total_missing > 0) {
    
        // Start the message
        var missing_message = "Sorry, you must fill in the following " +
                              (total_missing == 1 ? " field:" : " fields:") +
                              "\n______________________________\n\n"
        
        // Loop through the missing fields
        for (counter = 0; counter < missing_fields.length; counter++) {
            missing_message += missing_fields[counter].name + "\n"
        }
        //now check to see if they ordered enough T-Shirts
		var shirtNumCheck = (eval(order.youthMedShirt.value) + eval(order.youthLargeShirt.value) + eval(order.SmallShirt.value) + eval(order.MediumShirt.value) + eval(order.LargeShirt.value) + eval(order.XLargeShirt.value))
        if (shirtNumCheck < 24 && shirtNumCheck > 0){
		// Finish up and display the message
        missing_message += "\n______________________________\n\n" +
                       "Please fill in these fields, make sure you order at least 24 T-Shirts and then resubmit the form."
        alert(missing_message)
		}
		if (shirtNumCheck >= 24 ){
		// Finish up and display the message
        missing_message += "\n______________________________\n\n" +
                       "Please fill in these fields and then resubmit the form."
        alert(missing_message)
        }
		if(shirtNumCheck == 0){
		missing_message += "\n______________________________\n\n" +
                       "Please fill in these fields and then resubmit the form."
        alert(missing_message)
        }
		
        // For emphasis, put the focus on the first missing field
        missing_fields[0].focus()
    }
    else {
	//since all the fields are filled in, check to make sure that they ordered a minimum of 24 T-Shirts
		var shirtNumCheck = (eval(order.youthMedShirt.value) + eval(order.youthLargeShirt.value) + eval(order.SmallShirt.value) + eval(order.MediumShirt.value) + eval(order.LargeShirt.value) + eval(order.XLargeShirt.value))
        if (shirtNumCheck < 24 && shirtNumCheck > 0){
		alert("Please choose a minimum of 24 T-Shirts.")
		}
		if (shirtNumCheck >= 24){
		setText()
    	location = "#confirmAnchor";
		}
		if (shirtNumCheck == 0){
		setText()
    	location = "#confirmAnchor";
		}


    }
}


function its_empty(string_value) {

    // Check for the empty string and null
    if (string_value == "" || string_value == null) {
    
        // If either, it's empty so return true
        return true
    }
    
    // Otherwise, it's not empty so return false
    return false
}




function validate_email2(){
//email validation, very important no valid return email address, mail is undeliverable
		if (document.order.email.value==""){
			alert("Please enter your email address.");
			document.order.email.focus();
			document.order.email.select();
			return false;		
		}

		//run email validation 
		if (!validateEmail(document.order.email.value)){
			alert("The email address you entered is invalid.  Your order cannot be sent without a valid email address.");
			document.order.email.focus();
			document.order.email.select();
			return false;
		}
	 
	function validateEmail(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;
		}
		 peroidPos = email.indexOf(".", atPos)
		 if (peroidPos ==-1) {
		 	return false;
		}
		if (peroidPos+3 > email.length) {
			return false;
		}
		return true; 
	}
	}