
/*** HELPER FUNCTIONS ***/

// Email Validation script- © Dynamic Drive (www.dynamicdrive.com)
function validateEmail(e){
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    var returnval=emailfilter.test(e);
    return returnval;
}

function isTotal(name)
{
	if (name.match("_total")) return true;
	else return false;
}

function isInput(name)
{
	if (name.match("_input")) return true;
	else return false;
}

function stripTotal(value)
{
	return parseFloat(value.replace(/\$/,"0"));
}

function UpdateValue(item, baseship)
{
	ignore_shipping = document.getElementsByName("ignore_shipping");
	ignore_shipping = ignore_shipping[0].checked;

	// update price of clicked item
	if (item)
	{
    	input = document.getElementsByName(item + "_input");
    	price = document.getElementsByName(item + "_price");
    	total = document.getElementsByName(item + "_total");
    	total[0].value = "$" + (price[0].value * Math.round(input[0].value)).toFixed(2);
    }

	// update price of total purchase
	sum = 0;
	i = 0;
	for( i=0; i<document.storeform.length; i++ )
		if (isTotal(document.storeform[i].name))
			sum += stripTotal(document.storeform[i].value);
	
    // update shipping cost		
	firstship = 0; // shipping for first item
	othership = 0; // shipping for remaining items
	itemcount = 0;
	firstname = "";
	i = 0;
	// choose item as first item to ship (largest shipping cost)
	for( i=0; i<document.storeform.length; i++ )
		if (isInput(document.storeform[i].name))
		    if (document.storeform[i].value > 0)
		    {
		        name = document.storeform[i].name.substring(0, document.storeform[i].name.length - 6);
		        s    = document.getElementsByName(name + "_ship1");
		        s    = stripTotal(s[0].value);
		        if (s > firstship)
		        {
		            firstship = s;
		            firstname = name;
		        }
		    }
    // add shipping for remaining items
	for( i=0; i<document.storeform.length; i++ )
		if (isInput(document.storeform[i].name))
		    if (document.storeform[i].value > 0)
		    {
		        name = document.storeform[i].name.substring(0, document.storeform[i].name.length - 6);
		        shipitem = document.getElementsByName(name + "_ship2");
		        if (name == firstname)
		        {
		            othership += (stripTotal(document.storeform[i].value) - 1) * shipitem[0].value;
		        }
                else
                {
		            othership += (stripTotal(document.storeform[i].value)    ) * shipitem[0].value;
                }    
		    }
    // sum total	
    ship = baseship + firstship + othership;

    // print totals
	total = document.getElementsByName("total_grand");
	total[0].value = "$" + sum.toFixed(2);
	total = document.getElementsByName("total_shipping");
	total[0].value = "$" + ship.toFixed(2);
	total = document.getElementsByName("total_final");
	if (ignore_shipping)
	   total[0].value = "$" + (sum     ).toFixed(2);
    else
	   total[0].value = "$" + (sum+ship).toFixed(2);
}

function OnSubmit1()
{
    if ( document.storeform.name.value  == "" ) { alert("Please specify a name");         return; }
    if ( document.storeform.email.value == "" ) { alert("Please specify an email");       return; }
    if ( document.storeform.phone.value == "" ) { alert("Please specify a phone number"); return; }
    if ( document.storeform.address1.value == "" ) { alert("Please complete both parts of your address"); return; }
    if ( document.storeform.address2.value == "" ) { alert("Please complete both parts of your address"); return; }
    if ( document.storeform.total_grand.value == "$0.00" ||
         document.storeform.total_grand.value == "$0") { alert("Your order is empty!"); return; }
    
    if ( !validateEmail(document.storeform.email.value) ) { alert("Please enter a valid email address"); return; }
    
    document.storeform.submit();
}        

function OnSubmit2()
{
    document.checkoutform.submit();
}

function SwitchSectionVisible(number, total)
{
    i = 0;
    for( i=0; i<total; i++)
    {
        if (i == number)
            document.getElementById("hide" + i).style.display = 'block';
        else    
            document.getElementById("hide" + i).style.display = 'none';
    }        
}
function SwitchSectionVisibleAll(total)
{
    for( i=0; i<total; i++)
        document.getElementById("hide" + i).style.display = 'block';
}