window.onload = resetform;

function resetform()
{
    document.orders.ordernow.disabled=false;
    //document.orders.ordernow.value='Submitting...';
    document.getElementById('siimage').src = '/securimage_show.php?' + Math.random();
}

function isInt(s)
{
	s = s.toString();
	
	for (var i = 0; i < s.length; i++)
	{
		if ( !(parseInt(s.charAt(i)) || s.charAt(i) == '0') )
			return false;
	}
		
	return true;
}

function formatCurrency(rlNum, intDPs)
{
	var intDotIndex = rlNum.toString().indexOf('.');
	if (intDotIndex == -1)
	{
		return rlNum + '.00';
	}
	else if (intDotIndex == rlNum.toString().length - 2)
	{
		return rlNum + '0';
	}
	else
	{
		return round(rlNum);
	}
}

function isblank(s)
{
	for(var i=0; i <s.length; i++)
	{
		var c = s.charAt(i);
		if ((c != " ") && (c != "\n") && (c != "\t")) return false;
	}		
	return true;
}

function checkmail(email)
{
	var	filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email))
	{
		return true;
	}
	else
	{
		return false
	}
}

function calcTotal()
{
	var myform = document.orders;
	var total = 0;
	
	if (isInt(myform.LR3IGF1.value))
	{
		total += myform.LR3IGF1.value * 90;
	}
	
	if (isInt(myform.MelanotanI.value))
	{
		total += myform.MelanotanI.value * 30;
	}
	
	if (isInt(myform.MelanotanII.value))
	{
		total += myform.MelanotanII.value * 35;
	}
	
	if (isInt(myform.BacteriostaticWater.value))
	{
		total += myform.BacteriostaticWater.value * 5;
	}
	
	if (isInt(myform.Syringes.value))
	{
		total += myform.Syringes.value * 5;
	}
	
	total += 5;
	document.getElementById('total').innerHTML = '&pound;' + formatCurrency(total, 2);
	
}

function validate()
{

var finalmessage = "You have not entered the following details:-\n\n";
var message = "";
var myform = document.orders;		
var errorcol = "#FFCCCC";
var normcol = "#FFFFFF";
var el, i = 0;

// reset background colours
while (el = myform.elements[i++]) el.style.background = normcol;
		
// do validation	
	
	// This checks that the fields selected
	if  (isblank(myform.FullName.value))
	{
		message += "Please enter your full name\n" ; 
		myform.FullName.style.background = errorcol;
	}
	
	if  (isblank(myform.Address.value))
	{
		message += "Please enter your address\n" ; 
		myform.Address.style.background = errorcol;
	}
	
	if  (isblank(myform.Email.value))
	{
		message += "Please enter your email\n" ; 
		myform.Email.style.background = errorcol;
	}
	else if (!checkmail(myform.Email.value))
	{
		message += "Please enter a valid email address\n" ; 
		myform.Email.style.background = errorcol;
	}
	else if  (myform.Email.value != myform.ConfirmEmail.value)
	{
		message += "Your email and confirmation do not match\n" ; 
		myform.ConfirmEmail.style.background = errorcol;
	}
	
	if (myform.MethodOfPayment.options[myform.MethodOfPayment.selectedIndex].value == 0)  
	{
		message += "Please select your method of payment\n" ; 
		myform.MethodOfPayment.style.background = errorcol;
	}
	
	if (!isInt(myform.LR3IGF1.value))
	{
		message += "IGF order quantity must be numeric\n";
		myform.LR3IGF1.style.background = errorcol;
	}
	
	if (!isInt(myform.MelanotanI.value))
	{
		message += "Melanotan I order quantity must be numeric\n" ;
		myform.MelanotanI.style.background = errorcol;
	}
	
	if (!isInt(myform.MelanotanII.value))
	{
		message += "Melanotan II order quantity must be numeric\n" ;
		myform.MelanotanII.style.background = errorcol;
	}
	
	if (!isInt(myform.BacteriostaticWater.value))
	{
		message += "Bacteriostatic Water order quantity must be numeric\n" ;
		myform.BacteriostaticWater.style.background = errorcol;
	}
	
	if (!isInt(myform.Syringes.value))
	{
		message += "Syringe order quantity must be numeric\n" ;
		myform.Syringes.style.background = errorcol;
	}
	
/*	if ((isblank(myform.LR3IGF1.value)) && (isblank(myform.MelanotanII.value)))
	{
		message += "Please enter a quantity of LR3 IGF-1 or Melanotan II\n" ;
		myform.LR3IGF1.style.background = errorcol;
		myform.MelanotanII.style.background = errorcol;			
	}		
*/	
	//	handle errors
	if (message)
	{
		alert(finalmessage + message);
		return false;
	}
	else
	{
		myform.submit();
		myform.ordernow.disabled=true;
        myform.ordernow.value='Submitting...';
		return true;
	}
}
