function Pound (val) {  // force to valid pound amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

var amt,des,obj,val,op1a,op1b,op2a,op2b,itmn,classID;

function ChkTok (obj1) {
var j,tok,ary=new Array ();       // where we parse
  ary = val.split (" ");          // break apart
  for (j=0; j<ary.length; j++) {  // look at all items
// first we do single character tokens...
    if (ary[j].length < 2) continue;
    tok = ary[j].substring (0,1); // first character
    val = ary[j].substring (1);   // get data
    if (tok == "@") amt = (( val / 0.966)  + (0.20 / 0.966));
    if (tok == "+") amt = amt + ( val / 0.966);
    if (tok == "%") amt = amt + (amt * val/100.0);
    if (tok == "#") {             // record item number
      if (obj1.item_number) obj1.item_number.value = val;
      ary[j] = "";                // zap this array element
    }
// Now we do 3-character tokens...
    if (ary[j].length < 4) continue;
    tok = ary[j].substring (0,3); // first 3 chars
    val = ary[j].substring (3);   // get data
    if (tok == "s1=") {           // value for shipping
      if (obj1.shipping)  obj1.shipping.value  = val;
      ary[j] = "";                // clear it out
    }
    if (tok == "s2=") {           // value for shipping2
      if (obj1.shipping2) obj1.shipping2.value = val;
      ary[j] = "";                // clear it out
    }
  }
  val = ary.join (" ");           // rebuild val with what's left
}

function StorVal () {
var tag;
  tag = obj.name.substring (obj.name.length-2);  // get flag
  if      (tag == "1a") op1a = op1a + " " + val;
  else if (tag == "1b") op1b = op1b + " " + val;
  else if (tag == "2a") op2a = op2a + " " + val;
  else if (tag == "2b") op2b = op2b + " " + val;
  else if (tag == "3i") itmn = itmn + " " + val;
  else if (des.length == 0) des = val;
  else des = des + ", " + val;
}

function StoreID (obj1) {
	if (classID.length > 1) {
	if (obj1.class_nos.value.length == 0) {obj1.class_nos.value = classID.substring(1);}
	else if (obj1.class_nos.value.length > 0) {obj1.class_nos.value = obj1.class_nos.value + "," + classID.substring(1);}
	}
}

function ReadForm (obj1, tst) { // Read the user form
var i,j,pos;
  amt=0;des="";op1a="";op1b="";op2a="";op2b="";itmn="";obj1.class_nos.value="";
  if (obj1.baseamt) amt  = obj1.baseamt.value*1.0;  // base amount
  if (obj1.basedes) des  = obj1.basedes.value;  // base description
  if (obj1.baseon0) op1a = obj1.baseon0.value;  // base options
  if (obj1.baseos0) op1b = obj1.baseos0.value;
  if (obj1.baseon1) op2a = obj1.baseon1.value;
  if (obj1.baseos1) op2b = obj1.baseos1.value;
  if (obj1.baseitn) itmn = obj1.baseitn.value;
  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    if (obj.type == "select-one") {   // just selects
      if (obj.name == "quantity" ||
          obj.name == "amount") continue;
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      ChkTok (obj1);                  // check for any specials

      if (obj.name == "on0" ||        // let this go where it wants
          obj.name == "os0" ||
          obj.name == "on1" ||
          obj.name == "os1") continue;


    } else
    if (obj.type == "checkbox" ||     // just get checkboxex
        obj.type == "radio") {        //  and radios
      if (obj.checked) {
        val = obj.value;              // the value of the selection
		classID = obj.id;
        ChkTok (obj1);
		StoreID (obj1);
      }
    } else
    if (obj.type == "select-multiple") {  //one or more
      for (j=0; j<obj.options.length; j++) {  // run all options
        if (obj.options[j].selected) {
          val = obj.options[j].value; // selected value (default)
          ChkTok (obj1);
        }
      }
    } else
    if ((obj.type == "text" ||        // just read text,
         obj.type == "textarea") &&
         obj.name != "tot" &&         //  but not from here
         obj.name != "tott" &&
         obj.name != "quantity") {
      val = obj.value;                // get the data
      if (val == "" && tst) {         // force an entry
		 if (obj.name != "memb_no+1b" &&
		 obj.name != "memb_age+1b" &&
		 obj.name != "memb_no+2b" &&
		 obj.name != "horse_height+2b" &&
		 obj.name != "Owner_Number") {
		 obj.focus();
		alert ("Enter data for " + obj.name);
        return false; }
      }
      StorVal ();
    }
  }
// Now summarize stuff we just processed, above
  if (obj1.class_nos.value.substring(0,1) == ",") obj1.class_nos.value = obj1.class_nos.value.substring (1);
  obj1.item_name.value = des;
  obj1.amount.value = Pound (amt);
  if (obj1.tot) obj1.tot.value = "£" + Pound (amt);
    if (obj1.tott) obj1.tott.value = "£" + Pound ((amt*0.034)+0.20);
  return true;
}

function SubClear (obj1,obj2) {  // submit, then clear a FORM
  if (ReadForm(obj1,true)) {
	ChangeChecks ("", "all");
	obj1.submit();
    obj1.reset();
  }
  return false;
}

function ChangeChecks (uncv, chk) { // Read the user form
var j1,stAlrt2,stAlrt,elemnt,ob2;
 var unch = uncv.split(",");
stAlrt = 0;
stAlrt2 = 0;

if (chk == 'all') { // Reset all boxes
	elemnt = document.shop_check;
	for (j1=0; j1<elemnt.length; j1++) {     // run entire form
		ob2 = elemnt[j1];	// a form element
    	if (ob2.type == "checkbox") { ob2.disabled = false; }    // set checkboxe to enable
	}
} else {
for (jl=0; jl<unch.length; jl++) {
	elemnt = (document.shop_check.elements["chk"+unch[jl]]);
 if (chk == true) {
	if (elemnt.checked) {elemnt.checked = false; stAlrt2 = stAlrt2 + 1;}
		elemnt.disabled = true;
	stAlrt = 1;
	}  else {
		elemnt.disabled = false;
	}
 }	}
	if (stAlrt2 == 0) {stAlrt2 = "";} else {stAlrt2 = stAlrt2 + " class(es) you had selected have been removed.";}
	if (stAlrt == 1) alert("The class you have chosen does not allow you to enter selected other class(es) in this competition. \r\nThese check boxes for these class(es) have been disabled \r\n"+ stAlrt2); 
} 
