// Edited By Albert Arul Prakash
// Contact the Author for freelancing job at albertindian2000@yahoo.com
// Website : http://www.indiansinc.com
// Edit History
// Function name 	: ReadForm(obj,tst)
// Edited Details	: Added a functionality to check the color name
//			  and with the color i had added the more price 
//			  in the order
	
var ac = 0; // table for qty/amt pairs
var aqty = new Array (); // qty brkpt
var aamt = new Array (); // amount to charge
var pc = 0; // table for qty/percent pairs
var pqty = new Array (); // qty brkpt
var pper = new Array (); // percent to discount

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;
}

function ReadForm (obj1, tst) { // Read the user form
var i,j,amt,des,qty,obj,pos,val,op1a="",op1b="",op2a="",op2b="";
var mini=1,incr=1; // minimum and increment values
// Edited By Albert Arul Prakash
var lFlgMet =0;
// edit over

  amt = obj1.baseamt.value*1.0; // base amount
  des = args.ides; // base description
  qty = obj1.qty.value; // get user quantity
  if (obj1.mini) mini = obj1.mini.value*1.0;
  if (obj1.incr) incr = obj1.incr.value*1.0;
  if (isNaN (qty) || qty < 1) { // make sure it's good
    alert ('"' + qty + '"' + ' is not a valid number!');
    ac = 0; // always zap the table
    pc = 0;
    return false; // th-th-that's all, folks.
  }
  qty = qty*1.0; // force to numeric
  if (qty < mini) { // not the minimum
    alert ('"' + qty + '"' + ' must be at least ' + mini + '.\n\n' + 
      'Please reenter...');
    obj1.qty.value = "";
    ac = 0; // always zap the table
    pc = 0;
    return false;
  }
  if (qty % incr > 0) { // increment not valid
    alert ('"' + qty + '"' + ' is not increment of ' + incr + '.\n\n' + 
      'Please reenter...');
    obj1.qty.value = "";
    ac = 0; // always zap the table
    pc = 0;
    return false;
  }

  for (i=ac-1; i>=0; i=i-1) { // run table backwards
    if (qty >= aqty[i]) {     // use this entry
      amt = aamt[i];          // this is the real amount
      break;                  // get out, we're done
    }
  }
  for (i=pc-1; i>=0; i=i-1) { // run table backwards
    if (qty >= pqty[i]) {     // use this entry
      amt = amt - (amt/100.0 * pper[i]);
      break;                  // get out, we're done
    }
  }
  des = des + ", " + qty + " Magnets @" +
  Dollar (amt) + " ea";
  amt = amt * qty;
  obj1.qty.value = "1";
  ac = 0; // reset item discount
  pc = 0;

  if (obj1.basesel1) op1a = obj1.basesel1.value; // base options
  if (obj1.basesel2) op1b = obj1.basesel2.value;
  if (obj1.basesel3) op2a = obj1.basesel3.value;
  if (obj1.basesel4) op2b = obj1.basesel4.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
        obj.name == "") {           // must be un-named
      pos = obj.selectedIndex;      // which option selected
      val = obj.options[pos].value; // selected value
      pos = val.indexOf ("@");      // price set?
      if (pos > 0) amt = val.substring (pos + 1)*1.0;
      pos = val.indexOf ("+");      // price increment?
      if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
      pos = val.indexOf ("%");      // percent change?
      if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
      if (des.length == 0) des = val; 
      else des = des + ", " + val;  // accumulate value
    } else
    if (obj.type == "checkbox" ||   // just get checkboxex
        obj.type == "radio") {      // and radios
      if (obj.checked) {
        val = obj.value; // the value of the selection
        pos = val.indexOf ("@");    // price set?
        if (pos > 0) amt = val.substring (pos + 1)*1.0;
        pos = val.indexOf ("+");    // price increment?
        if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
        pos = val.indexOf ("%");    // percent change?
        if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
        if (des.length == 0) des = val;
        else des = des + ", " + val; // accumulate value, or text
	
	// Edited By Albert Arul Prakash
	// Contact the Author for freelancing job at albertindian2000@yahoo.com
	// Website : http://www.indiansinc.com
	
	// Check the color value. If it is metgold or metsilver then
	// Set the flag lFlgMet=1 else
	// Set the flag as 0
	    if (lFlgMet == 0 ){
	        if ((val=="MetGold") || (val=="MetSilver")){
		        lFlgMet=1;
	        }else{
		        lFlgMet=0;
	        }
	    }
	// Editing over
      }
    } 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)
        if (des.length = 0) des = amt;
        else des = des + ", " + val; // accumulate value, or text
      }     
    }
  } else
  if (obj.type == "text" && // just read text,
      obj.name != "tot" &&
      obj.name != "qty") {
    val = obj.value; // get the data
    if (val == "" && tst) { // force an entry
      alert ("Please enter text in ALL the text boxes above");
      return false;
    }
    tag = obj.name.substring (0, 2); // get special 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 (des.length == 0) des = val;
    else des = des + ", " + val;
    }
  }

 /* *********************************************************************
     Edited By Albert Arul Prakash
     Contact the Author for freelancing job at albertindian2000@yahoo.com
     Website : http://www.indiansinc.com
     Edit History
     Function name  : ReadForm(obj,tst)
     Edited Details : Added a functionality to include notes from customer
  ************************************************************************* */

   // for text area that too notes
   if (!obj1.Note) { // check whether there is a object named Note.
	// no notes in the form
   }
   else { // Yes I have a note object
	var nts = obj1.Note;
	des = des + ", Note : " + nts.value;
   }

// Now summarize stuff we just processed, above
  if (op1a.length > 0) obj1.sel1.value = op1a;
  if (op1b.length > 0) obj1.sel2.value = op1b;
  if (op2a.length > 0) obj1.sel3.value = op2a;
  if (op2b.length > 0) obj1.sel4.value = op2b;
  obj1.product.value = des;

	// Edited By Albert Arul Prakash
	// Contact the Author for freelancing job at albertindian2000@yahoo.com
	// Website : http://www.indiansinc.com
	
	// If the flag of metalic data is set then add 25 cents more
	// Else donot add 25 cents to the total amount
	if (lFlgMet==0)
	 {
		obj1.amount.value=Dollar(amt);
	 }
	else
	 {
		var mores=0;
		mores=qty *0.25;
		obj1.amount.value = Dollar (amt+mores);
	 }
	// Editing over

}

function SetAmt (q1, a1) { // set up a quantity-based amount table
var i;
  ac = 0;
  for (i=0; i<arguments.length; i=i+2) { // build the table
    aqty[ac] = arguments[i];             // get real args and store 
    aamt[ac] = arguments[i+1];
    ac = ac + 1; // number of pairs in table
  }
}

function SetPer (q1, p1) { // set up a quantity-based percent table
var i;
  pc = 0;
  for (i=0; i<arguments.length; i=i+2) { // build the table
    pqty[pc] = arguments[i];             // get real args and store 
    pper[pc] = arguments[i+1];
    pc = pc + 1; // number of pairs in table
  }
}




function checking (obj1, tst) { // Read the user form
return false;
// The orginal code
var i,j,amt,des,qty,obj,pos,val,op1a="",op1b="",op2a="",op2b="";
var mini=1,incr=1; // minimum and increment values
  amt = obj1.baseamt.value*1.0; // base amount
  des = args.ides; // base description
  qty = obj1.qty.value; // get user quantity
  if (obj1.mini) mini = obj1.mini.value*1.0;
  if (obj1.incr) incr = obj1.incr.value*1.0;
  if (isNaN (qty) || qty < 1) { // make sure it's good
    alert ('"' + qty + '"' + ' is not a valid number!');
    ac = 0; // always zap the table
    pc = 0;
    return false; // th-th-that's all, folks.
  }
  qty = qty*1.0; // force to numeric
  if (qty < mini) { // not the minimum
    alert ('"' + qty + '"' + ' must be at least ' + mini + '.\n\n' + 
      'Please reenter...');
    obj1.qty.value = "";
    ac = 0; // always zap the table
    pc = 0;
    return false;
  }
  if (qty % incr > 0) { // increment not valid
    alert ('"' + qty + '"' + ' is not increment of ' + incr + '.\n\n' + 
      'Please reenter...');
    obj1.qty.value = "";
    ac = 0; // always zap the table
    pc = 0;
    return false;
  }

  for (i=ac-1; i>=0; i=i-1) { // run table backwards
    if (qty >= aqty[i]) {     // use this entry
      amt = aamt[i];          // this is the real amount
      break;                  // get out, we're done
    }
  }
  for (i=pc-1; i>=0; i=i-1) { // run table backwards
    if (qty >= pqty[i]) {     // use this entry
      amt = amt - (amt/100.0 * pper[i]);
      break;                  // get out, we're done
    }
  }
  des = des + ", " + qty + " Custom Wine Labels @" +
  Dollar (amt) + " ea";
  amt = amt * qty;
  obj1.qty.value = "1";
  ac = 0; // reset item discount
  pc = 0;

  if (obj1.basesel1) op1a = obj1.basesel1.value; // base options
  if (obj1.basesel2) op1b = obj1.basesel2.value;
  if (obj1.basesel3) op2a = obj1.basesel3.value;
  if (obj1.basesel4) op2b = obj1.basesel4.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
        obj.name == "") {           // must be un-named
      pos = obj.selectedIndex;      // which option selected
      val = obj.options[pos].value; // selected value
      pos = val.indexOf ("@");      // price set?
      if (pos > 0) amt = val.substring (pos + 1)*1.0;
      pos = val.indexOf ("+");      // price increment?
      if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
      pos = val.indexOf ("%");      // percent change?
      if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
      if (des.length == 0) des = val; 
      else des = des + ", " + val;  // accumulate value
    } else
    if (obj.type == "checkbox" ||   // just get checkboxex
        obj.type == "radio") {      // and radios
      if (obj.checked) {
        val = obj.value; // the value of the selection
        pos = val.indexOf ("@");    // price set?
        if (pos > 0) amt = val.substring (pos + 1)*1.0;
        pos = val.indexOf ("+");    // price increment?
        if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
        pos = val.indexOf ("%");    // percent change?
        if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
        if (des.length == 0) des = val;
        else des = des + ", " + val; // accumulate value, or text
      }
    } 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)
        if (des.length = 0) des = amt;
        else des = des + ", " + val; // accumulate value, or text
      }     
    }
  } else
  if (obj.type == "text" && // just read text,
      obj.name != "tot" &&
      obj.name != "qty") {
    val = obj.value; // get the data
    if (val == "" && tst) { // force an entry
      alert ("Enter data for " + obj.name);
      return false;
    }
    tag = obj.name.substring (0, 2); // get special 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 (des.length == 0) des = val;
    else des = des + ", " + val;
    }
  }
// Now summarize stuff we just processed, above
  if (op1a.length > 0) obj1.sel1.value = op1a;
  if (op1b.length > 0) obj1.sel2.value = op1b;
  if (op2a.length > 0) obj1.sel3.value = op2a;
  if (op2b.length > 0) obj1.sel4.value = op2b;
  obj1.product.value = des;
  obj1.amount.value = Dollar (amt);
}
