// JavaScript Document
function Dollar (val) {  // force to valid dollar 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,ftonttxt,backtxt,emb_cost,print_cost;
 
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 * 1.0;
    if (tok == "+") amt = amt + val*1.0;
    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 ReadForm (obj1, tst) { // Read the user form
var i,j,pos;
  amt=0;des="";op1a="";op1b="";op2a="";op2b="";itmn="";emb_cost="";print_cost;
  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;
  if (obj1.baseemb) emb_cost = obj1.baseemb.value*1.0;
  if (obj1.baseprt) print_cost = obj1.baseprt.value*1.0;
 
 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
	  if (val == ""){
		alert ("Please select a " + obj.name);
        return false;
      }
      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;
 
      StorVal ();
 
    } else
    if (obj.type == "checkbox" ||     // just get checkboxex
        obj.type == "radio") {        //  and radios
      if (obj.checked) {
        val = obj.value;              // the value of the selection
        ChkTok (obj1);
        StorVal ();
      }
    } 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)
			  alert ("test");
          ChkTok (obj1);
          StorVal ();
        }
      }
    } else
    if ((obj.type == "text" ||        // just read text,
         obj.type == "textarea") &&
         obj.name != "tot" &&         //  but not from here
         obj.name != "quantity" ) {
      val = obj.value;                // get the data
	
    if (obj.name == "front-1a") { //this is the front personalisation
            fronttxt= obj.value;
			if (fronttxt!=""){
                val = "Front Personalisation - " + "\'" + fronttxt + "\'"
                amt=amt+emb_cost;
			}
	}
 
    if (obj.name == "back-2a") { //this is the front personalisation
            backtxt= obj.value;
			if (backtxt!=""){
                val = "Back Personalisation - " + "\'" + backtxt + "\'"
                amt=amt+print_cost;
			}
 
          } else
             if (obj.name == "mobile-1a") { //this is the mobile number
            mobilenum = obj.value;
            if ((mobilenum == "" || mobilenum<7000000000 || isNaN (mobilenum)) && tst) { // test
                alert ("Please enter a valid mobile number");
                return false;
               }
            mobilestn = mobilenum
            val = "Mobile Number " + "\'" + mobilestn + "\'"
          }
       else
            if (obj.name == "deliveryaddress-2b") { // this is the delivery address, optional only if delivery paid for
			address= obj.value;
			if (address == "" && tst) {
                alert ("PLEASE ENTER A DELIVERY ADDRESS" + "\n\n" +  "If you are going to collect your stash from College, then simply enter 'College'" + "\n" + "If you've opted for delivery to your house in Durham / or to your home, then please give your postal address.");
                return false;
               }
			obj1.on1.value = "Deliver to"
            val = "Address - " + "\'" + address + "\'"
         }
      else
 
	      if (obj.name == "Name") { // this is the person's Full Name, which needs to be entered on the main index page
		     if (obj.value == "" && tst) {
				 alert ("Please enter your First name and Surname");
				 return false;
			 }
		  }		
	  else
            if (obj.name == "imageno-1a") { // this is the photo number
            imagenum= val;
			re1 = /^\w+[_]\d+$/i
			re2 = /^\w+(sport|team|sports|teams)[_]\d+$/i			
			if (imagenum == "") {
			  alert ("Please enter an image number");
                return false;
               }	
			if (imagenum != "") {
				  if (re1.test(imagenum) != re2.test(imagenum) ) {
					val = "Image Number - " + "\'" + imagenum + "\'"
				    }
				  else {
				    alert ("Sorry - this is not a valid image number. \nThe image name may be in the wrong format, or you may be trying to order a Sports Team / Survivors photo, which, due to their size and shape, cannot be printed onto this product");
				    return false;
			        }
			}
}
 
            if (obj.name == "photono-1a") { // this is the optional photo number - if selected, need to add £1 to the price
            photonum= val;
			re1 = /^\w+[_]\d+$/i
			re2 = /^\w+(sport|team|sports|teams)[_]\d+$/i
			  if (photonum != "") {
				  if (re1.test(photonum) != re2.test(photonum) ) {
					val = "Image Number - " + "\'" + photonum + "\'"
			        amt=amt+1.0;
				    }
				  else {
				    alert ("Sorry - this is not a valid image number. \nThe image name may be in the wrong format, or you may be trying to order a Sports Team / Survivors photo, which, due to their size and shape, cannot be printed onto this product");
				    return false;
			        }
			  }
			 else {
			  val = "No Photo "
			 }
			}
 
	  else
            if (obj.name == "event-1b") { // this is the event description, only needed if the optional photo number has been entered
            eventname= val;
			if (eventname == "" && photonum!= "") {
			  alert ("Please enter a description of the event at which the photo was taken, along with an approx date when it was held");
                return false;
               }
			 else
			   if (eventname != "" && photonum == "") {
			   alert ("You have entered an event description, but no photo image number - please enter a photo number, or delete the event description if you don't want a photo");
			    return false;
               }
			 else
			    if (eventname != "" && photonum != "") {
             val = "Event - " + "\'" + eventname + "\'"
               }
	    }
	  else
            if (obj.name == "eventdesc-1b") { // this is the event description
            eventdes= val;
			if (eventdes == "") {
			  alert ("Please enter a description of the event at which the photo was taken, along with an approx date when it was held");
                return false;
               }	
             val = "Event - " + "\'" + eventdes + "\'"
         }
 
       else
            if (obj.name == "extratext-2a") { // this is the extra
            extratxt= val;
            if (extratxt!= ""){
                val = "Additional text - " + "\'" + extratxt + "\'"
            }
         }
 
		
      if (obj.name != "back-2a" &&
		  obj.name != "front-1a" &&
		  		  obj.name != "extratext-2a" &&
				  obj.name != "photono-1a" &&
				  obj.name != "event-1b" &&
		  val == "" && tst) {         // force an entry if its not a back / front personalisation entry
        alert ("Enter data for " + obj.name);
        return false;
      }
      StorVal ();
    }
  }
// Now summarize stuff we just processed, above
  if (op1a.length > 0) obj1.on0.value = op1a;
  if (op1b.length > 0) obj1.os0.value = op1b;
  if (op2a.length > 0) obj1.on1.value = op2a;
  if (op2b.length > 0) obj1.os1.value = op2b;
  if (itmn.length > 0) obj1.item_number.value = itmn;
  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = Dollar (amt); }
 
 

