var loaded = false;
//=====================================
function doamts()
{
	if (!loaded) { return; }
	doallupdate(document.f.f_swap,"vswap",35)
	doallupdate(document.f.f_comswap,"vcomswap",75)
	doallupdate(document.f.f_bbq,"vbbq",15)
	doallupdate(document.f.f_dash,"vdash",7)
	doallupdate(document.f.f_pin,"vpin",4)
	doallupdate(document.f.f_tshirt,"vtshirt",16)
	doallupdate(document.f.f_sweat,"vsweat",28)
	regupdate();
	grandtot();
} 
//=====================================
function doreg()
{
	//alert("Registration begins June 1"); return false;
	if (checkit(document.f))
	{
	   var d = new Date();
	   var t = d.getTime();
	   t = t.toString().substring(7);
	   document.f.item_name.value = "["+t+"] "+document.f.item_name.value
	   tempWin1 = window.open("","mail");
       document.mail.mess.value = document.getElementById("regform").innerHTML
	   document.mail.order.value = "<hr>key="+t+"<hr>"
	   document.mail.order.value = document.mail.order.value + getvalues();
	   document.mail.from.value = document.f.f_name.value;
	   saveform();
	   document.mail.submit();
	}
}	  
var reg = 0;
var what = "";
//=====================================
function getvalues()
{

	 var v = "<table width=600 cellspacing=2><tr><td width='20%'><b>name:</b></td><td>"+ document.f.f_name.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>address: </b></td><td>"+ document.f.f_add.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>city: </b></td><td> "+ document.f.f_city.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>state/prov: </b></td><td>"+ document.f.f_state.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>zip/postal code: </b></td><td>"+ document.f.f_zip.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>home phone: </b></td><td>"+ document.f.f_hphone.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>business phone: </b></td><td>"+ document.f.f_bphone.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>email: </b></td><td> "+ document.f.f_email.value+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>club: </b></td><td>"+ document.f.f_club.value+"</td></tr>\n";
	 
	 v = v +"<tr style='border-top : thin solid Navy;'><td colspan=2><b>Car information </b></td></tr>";
	 v = v +"<tr><td colspan='2'>First vehicle registration:<hr><table width=600><tr><td width='20%'><b>make: </b>"+ document.f.f_make1.value+"</td>";
	 v = v +"<td width='20%'><b>year: </b>"+ document.f.f_year1.value+"</td>";
	 v = v +"<td width='20%'><b>model: </b>"+ document.f.f_model1.value+"</td>";
	 v = v +"<td width='20%'><b>style: </b>"+ document.f.f_style1.value+"</td>";
	 v = v +"<td width='20%'><b>class: </b>"+ document.f.f_class1.value+"</td></tr></table></td></tr>\n";


	 v = v +"<tr><td colspan='2'>Second vehicle registration:<hr><table width=600><tr><td width='20%'><b>make: </b>"+ document.f.f_make2.value+"</td>"
	 v = v +"<td width='20%'><b>year: </b>"+ document.f.f_year2.value+"</td>"
	 v = v +"<td width='20%'><b>model: </b>"+ document.f.f_model2.value+"</td>"
	 v = v +"<td width='20%'><b>style: </b>"+ document.f.f_style2.value+"</td>"
	 v = v +"<td width='20%'><b>class: </b>"+ document.f.f_class2.value+"</td></tr></table></td></tr>\n"
	 
	 v = v +"<tr><td colspan='2'>Third vehicle registration:<hr><table width=600><tr><td width='20%'><b>make: </b>"+ document.f.f_make3.value+"</td>"
	 v = v +"<td width='20%'><b> </b>year:"+ document.f.f_year3.value+"</td>"
	 v = v +"<td width='20%'><b>model: </b>"+ document.f.f_model3.value+"</td>"
	 v = v +"<td width='20%'><b>style: </b>"+ document.f.f_style3.value+"</td>"
	 v = v +"<td width='20%'><b>class: </b>"+ document.f.f_class3.value+"</td></tr></table></td></tr>\n"
	 
	 v = v +"<tr><td colspan='2'>Fourth vehicle registration:<hr><table width=600><tr><td width='20%'><b>make: </b>"+ document.f.f_make4.value+"</td>"
	 v = v +"<td width='20%'><b>year: </b>"+ document.f.f_year4.value+"</td>"
	 v = v +"<td width='20%'><b>model: </b>"+ document.f.f_model4.value+"</td>"
	 v = v +"<td width='20%'><b>style: </b>"+ document.f.f_style4.value+"</td>"
	 v = v +"<td width='20%'><b>class: </b>"+ document.f.f_class4.value+"</td></tr></table></td></tr>\n"
	 
	 v = v +"<tr><td colspan=2><hr  style='border-top : thin solid Navy;'><b>Fees </b></td></tr>"
	 v = v +"<tr><td width='20%'><b>registration: </b></td><td> amount: $"+document.getElementById("vreg").innerHTML+"</td></tr>\n";
	 v = v +"<tr><td width='20%'><b>swap spaces: </b></td><td>"+document.f.f_swap.value+", amount: $"+document.getElementById("vswap").innerHTML+"</td></tr>\n";
 	 v = v +"<tr><td width='20%'><b>commercial swap spaces: </b></td><td>"+document.f.f_comswap.value+", amount: $"+document.getElementById("vcomswap").innerHTML+"</td></tr>";
	 v = v +"<tr><td width='20%'><b>bbq tickets: </b></td><td>"+document.f.f_bbq.value+", amount: $"+document.getElementById("vbbq").innerHTML+"</td></tr>";
	 v = v +"<tr><td width='20%'><b>Extra Dash Plaque: </b></td><td>"+document.f.f_dash.value+", amount: $"+document.getElementById("vdash").innerHTML+"</td></tr>";	
	 v = v +"<tr><td width='20%'><b>Field Meet Pin: </b></td><td>"+document.f.f_pin.value+", amount: $"+document.getElementById("vpin").innerHTML+"</td></tr>"	; 
	 v = v +"<tr><td width='20%'><b>T-shirt(s): </b></td><td>"+document.f.f_tshirt.value+", amount: $"+document.getElementById("vtshirt").innerHTML+"</td></tr>";
	 v = v +"<tr><td width='20%'><b>T-shirt sizes(s): </b></td><td>"+document.f.f_tshirtnotes.value+"</td></tr>";	 
	 v = v +"<tr><td width='20%'><b>sweat-shirt(s): </b></td><td>"+document.f.f_sweat.value+", amount: $"+document.getElementById("vsweat").innerHTML+"</td></tr>";
	 v = v +"<tr style='border-top:double solid navy'><td><b>sweat-shirt sizes(s): </b></td><td>"+document.f.f_tSweatnotes.value+"</td></tr>";
	 v = v +"<tr><td width='20%'><b><B>Grand total</B>:</td><td>$"+document.getElementById("vgtotal").innerHTML+"</td></tr></table>"; 	
	 return v; 	  
}
//=====================================
function checkit(o)
	{
	  doamts();
	  var err = ""
	  reg = 0;
	  what = "";
	  var cnt = 0;
	  if (o.f_name.value == "") { err = err + "Your name is required\n" }
	  if (o.f_add.value == "") { err = err + "Missing street address\n" }
	  if (o.f_city.value == "") { err = err + "Missing City\n" }
	  if (o.f_state.value == "") { err = err + "Missing State/Province\n" }
	  if (o.f_zip.value == "") { err = err + "Missing Zip/Postal Code\n" }
	  if (o.f_make1.value != 0 && o.f_make1.value != ""){ what = "1st reg."}
	  if (o.f_make2.value != 0 && o.f_make2.value !=""){ cnt = cnt + 1}
	  if (o.f_make3.value != 0 && o.f_make3.value !=""){ cnt = cnt + 1}
	  if (o.f_make4.value != 0 && o.f_make4.value !=""){ cnt = cnt + 1}
	  if (cnt > 0) { what = what + "; "+cnt +" add.reg";}
	  testit(o.f_swap,";swap sp.")	  
	  testit(o.f_comswap,";com. sp.")
      if (o.f_make1.value == "" && ( o.f_make2.value != "" || o.f_make3.value != "" || o.f_make4.value != "" )) 
	  { err = err + "You can not register a second vehicle with out a primary one" }
 	  testit(o.f_bbq,";bbq")
	  testit(o.f_pin,";pin")
	  testit(o.f_dash,";plaques")
	  testit(o.f_tshirt,";tshirt")
	  if (o.f_tshirt.value != 0 && o.f_tshirt.value != "" &&  o.f_tshirtnotes.value == "")
	  {
	       err = err + "You need to specify the t-shirt size you want.\n" 
	  }
	  if (o.f_tshirt.value == 0 && o.f_tshirt.value == "" &&  o.f_tshirtnotes.value != "")
	  {
	       err = err + "You specified a tshirt size, but haven't selected a tshirt quantity.\n" 
	  }	  
	  testit(o.f_sweat,";sweat")
	  if (o.f_sweat.value != 0 && o.f_sweat.value != "" &&  o.f_tSweatnotes.value == "")
	  {
	       err = err + "You need to specify the sweat size you want.\n" 
	  }	  
	  if (o.f_sweat.value == 0 && o.f_sweat.value == "" &&  o.f_tSweatnotes.value != "")
	  {
	      err = err + "You specified a sweat shirt  size, but haven't selected a sweat shirt  quantity.\n" 
	  }
	  if (!o.f_agree.checked) { err = err + "\nYou must click the agreement checkbox.\n"}
	  if (err != "") { alert(err); return false; }
	  if (document.getElementById("vgtotal").innerHTML == "0")
	  {
	  		alert("You haven't selected anything yet, please try again"); return false;
	  }
	  o.item_name.value = what
	  o.amount.value = document.getElementById("vgtotal").innerHTML + ".00";
	return true;
}
function testit(f,s)
{
   if (f.value != 0 && f.value != "") 
   { 
   		what = what + s + "[qnt:" + f.value + "]";
   }
}
var vgtotal = 0;
//=====================================
function doupdate(o,a,v)
{
   var val = o.value*1

   if (! val.toString().match(/^\s*[0-9]*\s*$/)) 
   { alert("Enter a number"); } 
   else 
   { 
   if (a == "vswap")
   { val = o.value*1 -1; if (val == -1) { val = 0} }
   document.getElementById(a).innerHTML = v * val; grandtot(); 
   }
}
//=====================================
function doallupdate(o,a,v)
{
   var val = o.value*1
   if (a == "vswap")
   { val = o.value*1 -1; if (val == -1) { val = 0} }
   document.getElementById(a).innerHTML = v * val; grandtot(); 
}
//=====================================
function grandtot()
{
    var f = "vswap,vreg,vcomswap,vbbq,vdash,vpin,vtshirt,vsweat".split(",")
	var gt = 0;
	for (var i=0;i<f.length;i++)
	{
	   gt = gt*1 + (document.getElementById(f[i]).innerHTML) * 1
	}
	document.getElementById("vgtotal").innerHTML = gt;
}
 
//=====================================
function showPropsInWindow(obj,objName) {
	var result = ""
	var p=window.open("","props");
	for (var i in obj) {
		result += objName + "." + i + " = " + obj[i] + "<br>"
		}
	p.document.write("<html><head></head><body>");
	p.document.write(result);
	p.document.write("</body></html>");
}
  
//=====================================
function saveform()
{ 
   var d = new Date("September 31, 2007");
   var fm = document.forms["f"];
   var c = "";
   for (var i in fm.elements)
   {
       if ( i.indexOf("f_") == 0 )
	   {
	      c = c + i + "|" + fm.elements[i].value + "|";
	   }
   }
   //alert(c)
   setCookie("abfm",c,d);
}
//=====================================
function setform()
{
   var fm =document.forms[0]
   var c = getCookie("abfm").split("|")
   if (c =="") { return; }
   for (var i=0;i<c.length-2;i=i+2)
   {
		 fm.elements[c[i]].value = c[i+1]
   }
   loaded = true;
   doamts()
}
//=====================================
function regupdate()
{
    var r = document.f
	var a = 0
	if (!r.f_make1.value.match(/^\s*$/)) { a = a*1 + 35; }
	if (!r.f_make2.value.match(/^\s*$/)) { a = a*1 + 10; }
	if (!r.f_make3.value.match(/^\s*$/)) { a = a*1 + 10; }
	if (!r.f_make4.value.match(/^\s*$/)) { a = a*1 + 10; }
	document.getElementById("vreg").innerHTML = a;
	grandtot()
}
//=====================================
function def()
{
   alert("Commercial means you represent a company doing business.  Commercial vendors are grouped together in a central location" )
}	
function setCookie(name, value, expires, path, domain, secure) {
  path = "/"; // forces cookie to work from your root on down
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
//=====================================
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return "";
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

