/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function CalculateTotal(frm) {
    var qty_total = 0
    var type_selected = 0
	
	//tshirt_qty_YXS, tshirt_type_YXS, tshirt_qty_YS, tshirt_type_YS, tshirt_qty_YM, tshirt_type_YM, tshirt_qty_YL, tshirt_type_YL, 
	//tshirt_qty_YXL, tshirt_type_YXL, tshirt_qty_AS, tshirt_type_AS, 
	//tshirt_qty_AM, tshirt_type_AM, tshirt_qty_AL, tshirt_type_AL, tshirt_qty_AXL, tshirt_type_AXL, tshirt_total_qty
	
	qty_YXS = 0
	qty_YS = 0
	qty_YM = 0
	qty_YL = 0
	qty_YXL = 0
	qty_AS = 0
	qty_AM = 0
	qty_AL = 0
	qty_AXL = 0	
	type_YS = ""
	type_YM = ""
	type_YL  = ""
	type_YXL = ""
	type_AS  = ""
	type_AM = ""
	type_AL = ""
	type_AXL = ""
	total_qty = 0

    isNaN(parseInt(document.buytshirts.tshirt_qty_YXS.value)) ? qty_YXS = 0 : qty_YXS = parseInt(document.buytshirts.tshirt_qty_YXS.value)
 	type_YXS = document.buytshirts.tshirt_type_YXS.value    
	
	isNaN(parseInt(document.buytshirts.tshirt_qty_YS.value)) ? qty_YS = 0 : qty_YS = parseInt(document.buytshirts.tshirt_qty_YS.value)
	type_YS = document.buytshirts.tshirt_type_YS.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_YM.value)) ? qty_YM = 0 : qty_YM = parseInt(document.buytshirts.tshirt_qty_YM.value)
	type_YM = document.buytshirts.tshirt_type_YM.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_YXS.value)) ? qty_YL = 0 : qty_YL = parseInt(document.buytshirts.tshirt_qty_YL.value)
	type_YL  = document.buytshirts.tshirt_type_YL.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_YXL.value)) ? qty_YXL = 0 : qty_YXL = parseInt(document.buytshirts.tshirt_qty_YXL.value)
	type_YXL = document.buytshirts.tshirt_type_YXL.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_AS.value)) ? qty_AS = 0 : qty_AS = parseInt(document.buytshirts.tshirt_qty_AS.value)
	type_AS  = document.buytshirts.tshirt_type_AS.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_AM.value)) ? qty_AM = 0 : qty_AM = parseInt(document.buytshirts.tshirt_qty_AM.value)
	type_AM = document.buytshirts.tshirt_type_AM.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_AL.value)) ? qty_AL = 0 : qty_AL = parseInt(document.buytshirts.tshirt_qty_AL.value)
	type_AL = document.buytshirts.tshirt_type_AL.value

    isNaN(parseInt(document.buytshirts.tshirt_qty_AXLvalue)) ? qty_AXL = 0 : qty_AXL = parseInt(document.buytshirts.tshirt_qty_AXL.value)
	type_AXL = document.buytshirts.tshirt_type_AXL.value
	
	type_selected = 1
	
	if (qty_YXS > 0 && type_YXS == "")
	{	type_selected = 0	}
	
	if (qty_YS > 0 && type_YS == "")
	{	type_selected = 0	}

	if (qty_YM > 0 && type_YM == "")
	{	type_selected = 0	}

	if (qty_YL > 0 && type_YL == "")
	{	type_selected = 0	}

	if (qty_YXL > 0 && type_YXL == "")
	{	type_selected = 0	}

	if (qty_AS > 0 && type_AS == "")
	{	type_selected = 0	}

	if (qty_AM > 0 && type_AM == "")
	{	type_selected = 0	}

	if (qty_AL > 0 && type_AL == "")
	{	type_selected = 0	}

	if (qty_AXL > 0 && type_AXL == "")
	{	type_selected = 0	}

	total_qty = qty_YXS + qty_YS + qty_YM + qty_YL + qty_YXL + qty_AS + qty_AM + qty_AL + qty_AXL

    frm.tshirt_total_qty.value = total_qty
    if (total_qty >= 10 && type_selected == 1)
    {	frm.txt_qty_message.value = "You qualify for discount rate"	
        frm.Submit.disabled = false;
    }
    else
    {	frm.txt_qty_message.value = "** Need min qty of 10 + Select T-Shirt Type"	
        frm.Submit.disabled = true;
    }

}

function oldCalculateTotal(frm) {
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) 
	{        
        form_field = frm.elements[i]				// Get the current field        
        field_name = form_field.name					// Get the field's name
        //alert ("form name: " + field_name)
        
        if (field_name == "tshirt_qty_YXS") 
		{
            // Get the quantity
            qty_YXS = parseInt(form_field.value)
            // Update the order total
            if (qty_YXS >= 0) 
			{	qty_total += qty_YXS 	}
        }

        if (field_name == "tshirt_qty_YS") 
		{
            // Get the quantity
            qty_YS = parseInt(form_field.value)
            // Update the order total
            if (qty_YS >= 0) 
			{	qty_total += qty_YS	}
        }

        if (field_name == "tshirt_qty_YM") 
		{
            // Get the quantity
            qty_YM = parseInt(form_field.value)
            // Update the order total
            if (qty_YM >= 0) 
			{	qty_total += qty_YM	}
        }

        if (field_name == "tshirt_qty_YL") 
		{
            // Get the quantity
            qty_YL = parseInt(form_field.value)
            // Update the order total
            if (qty_YL >= 0) 
			{	qty_total += qty_YXL	}
        }
	
        if (field_name == "tshirt_qty_YXL") 
		{
            // Get the quantity
            qty_YXL = parseInt(form_field.value)
            // Update the order total
            if (qty_YXL >= 0) 
			{	qty_total += qty_YXL	}
        }

        if (field_name == "tshirt_qty_AS") 
		{
            // Get the quantity
            qty_AS = parseInt(form_field.value)
            // Update the order total
            if (qty_AS >= 0) 
			{	qty_total += qty_AS	}
        }

        if (field_name == "tshirt_qty_AM") 
		{
            // Get the quantity
            qty_AM = parseInt(form_field.value)
            // Update the order total
            if (qty_AM >= 0) 
			{	qty_total += qty_AM	}
        }

        if (field_name == "tshirt_qty_AL") 
		{
            // Get the quantity
            qty_AL = parseInt(form_field.value)
            // Update the order total
            if (qty_AL >= 0) 
			{	qty_total += qty_AL	}
        }

        if (field_name == "tshirt_qty_AXL") 
		{
            // Get the quantity
            qty_AXL = parseInt(form_field.value)
            // Update the order total
            if (qty_AXL >= 0) 
			{	qty_total += qty_AXL	}
        }
        
        alert ("Qty YXS is: " + qty_YXS)
        alert ("Field YXS selected index is: " + field_name.tshirt_type_YXS.selectedindex)
        //alert ("field value length is: " + form_field.value.length)
        if (qty_YXS > 0 && field_name.tshirt_type_YXS.selectedindex > 0)
        {	type_selected = 1	}
        
        /*if (field_name == "tshirt_type_YXS" && form_field.value.length > 0 && qty_YXS > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_YS" && form_field.value.length > 0 && qty_YS > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_YM" && form_field.value.length > 0 && qty_YM > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_YL" && form_field.value.length > 0 && qty_YL > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_YXL" && form_field.value.length > 0 && qty_YXL > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_AS" && form_field.value.length > 0 && qty_AS > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_AM" && form_field.value.length > 0 && qty_AM > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_AL" && form_field.value.length > 0 && qty_AL > 0) 
		{	type_selected = 1	}

        if (field_name == "tshirt_type_AXL" && form_field.value.length > 0 && qty_AXL > 0) 
		{	type_selected = 1	}*/
    
	}
	
    frm.tshirt_total_qty.value = qty_total
    if (qty_total >= 10 && type_selected == 1)
    {	frm.txt_qty_message.value = "You qualify for discount rate"	
        frm.Submit.disabled = false;
    }
    else
    {	frm.txt_qty_message.value = "** Need min qty of 10 + Select T-Shirt Type"	
        frm.Submit.disabled = true;
    }

}

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 pad_with_zeros(result3, decimals)
}


