var whitespace="\t\n\r";
var total=0;

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace(s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function checkstring (s,msg) {
	if (isWhitespace(s)) {
		alert(msg);
		return false;
	}
	return true;
}

function checkmem (field,msg) {
	if (field[0].checked || field[1].checked) {
		return true;
	} else {
		alert (msg);
		return false;
	}
}
function cleanupstring (s) {

	// Remove whitespace from beginning and ending of string

	var b,e,flag;

	if (isWhitespace(s)) {
		return "";
	}

	// check beginning of string for whitespace

	b=0;flag=true;

	while (flag) {
		var c=s.charAt(b);
		if (allspace.indexOf(c)!=-1) {
			b++;
		} else
			flag=false;
	}

	// check end of string for whitespace

	e=s.length;flag=true;
	while (flag) {
		if (allspace.indexOf(s.charAt(e-1))!=-1) {
			e--;
		} else
			flag=false;
	}

	return s.substring (b,e);
}
function checkname (name,msg) {
	var na,s;
	s=cleanupstring(name);
	na=s.split(" ");

	if ((na.length != 3) && (na.length !=2 )) {
		alert (msg);		
		return false;
	}

	return true;
}
function validemail (o) {
	if ((isEmpty(o)) || (isWhitespace(o))) {
		alert ("Cardholders Email address is a required value");	
		return false;
	}
	/*if (!isemail(o)) {
		alert("Email address does not appear to be valid");
		return false;
	}*/
	return true;
}

function isemail (s) {
	if (isWhitespace(s))
		return false;

	var i=1;
	var slength=s.length;

	while ((i=slength)||(s.charAt(i)!="@"))
		return false;

	i+=2;

	while ((i=slength-1)||(s.charAt(i)!="."))
		return false;

	return true;
}
function checkstring (s,msg) {
	if (isWhitespace(s)) {
		alert(msg);
		return false;
	}
	return true;
}

function validatebuttons (form) {
	var a,a1,a2,a3,b,c,d,s;

	a=form.A.checked;
//	a1=form.A1.checked;
//	a2=form.A2.checked;
//	a3=form.A3.checked;
	b=form.B.checked;
//	c=form.C.checked;
//	d=form.D.checked;

	if (!a && !b) {
		alert ("You did not select any order options");
		return false;
	}


	return true;
}
function calc_total () {
	var total=0;

	var a,b; // payment option variables

	a=document.info.A.checked;
	b=document.info.B.checked;
	

	

	if (a)
		total+=129;
	if (b)
		total+=199;
		


//	var s="$"+total+".00";
	document.info.sub_total.value=to_currency(total); //changed to sub_total as we now add the gst

GST_rate = 0.05;
cost = total

  finalgst = cost * GST_rate;
  finalcost = (cost + finalgst);

  document.info.gst.value =    (to_currency(finalgst));
  document.info.thetotal.value =  (to_currency(finalcost));


	return true;
}
function validateid (form) {
	var button_yes=form.visa_master[0],
		button_no=form.visa_master[1];

	if (button_yes.checked) {
		if (isWhitespace(form.ID_number.value)) {
			alert("If you selected YES for 'previous member' you must enter your membership ID");
			return false;
		}
	} else if (!button_no.checked) {			
			alert ("You must select either YES or NO for previous member");
			return false;
	}

	return true;
}

function checknewmember (form) {

	var button_no=form.visa_master[1];
	
	if (button_no.checked) {
		return checkstring (form.how_hear.value, "Please tell us how you heard about us.");
	}
	
	return true;
}

function validateform (form) {
	return	checkstring (form.name_member.value,
		"Name of the member is a required field") &&
		checkstring (form.street_address.value,
		"Street address is a required field") &&
		checkstring (form.town_city.value,
		"Town / City is a required field") &&
		checkstring (form.postal_code.value,
		"Postal Code is a required field") &&
		checkmem (form.visa_master,
		"Previous member must be selected" ) &&
		checkstring (form.client_email.value,
		"Email is a required field") &&		
		checknewmember(form) &&
		validemail(form.client_email.value) &&
		validatebuttons (form) &&
		validateid(form);
}

function to_currency(val,currency) {  

if(!val) { val= 0 } 

 if(isNaN(val))
{	  alert(val+" is an invalid input");	 
 return; 
 }  

val=""+Math.round(100*val);  
 var dec_point=val.length-2;  
var first_part=val.substring(0,dec_point); 
 var second_part=val.substring(dec_point,val.length);  
var result=first_part+"."+second_part;
 

 currency="$";  

result=currency+result;  

if (result == "$.0") {result="$0.00"} //hack to get around unchecking all boxes

 return result;} 
