// JavaScript Document

function paypal() {
	//alert("paypal");
}
// calculations
function MM_callJS(jsStr) { //v2.0
  alert("xxx");
  return eval(jsStr)
}

pnp = 0;
grand = 0;


/* Take a number and produce a string with 2 decimal places */
function twodec(inp) {
   result = "";
   var rounded = Math.round(100 * inp) / 100;
   //alert("inp = "+inp+" inp2 = "+rounded);
   if (rounded > 0) {
      result = ""+rounded;
	  if ((rounded % 1) == 0) {
	     result = rounded + ".00";
	  } else {
	     if (((rounded * 10) % 1) == 0) {
		 result = rounded + "0";
	     }
	  }
   } else {
      result = "";
   }
   return result;
}

function book2(quant_string, tot_string, raw_price, pand, cd_sel, cd_price) {
  //alert("book "+cd_sel);
  var quant_element = document.getElementById(quant_string);
  if (!quant_element) {
    alert("Error - field "+quant_string+" not on document");
    return;
  }
  q = quant_element.value;
  tot = document.getElementById(tot_string);
  if(!tot) {
	  alert("Error field "+tot_string+" not on the order form");
      return;
  }
  price = 0;
  if (cd_sel == null) {
     price = raw_price;
  } else {
     var cd_sel_element = document.getElementById(cd_sel);
    
	 cd_value = cd_sel_element.value;
	  if ((q != "0") && (q != " ") && (cd_value == "undef")) {
		  cd_sel_element.value = "yes";
 		  cd_value = cd_sel_element.value;
	  } 
     if (cd_value == "yes") {
	    price = cd_price;
     } else {
    	 price = raw_price;
	}
 }
  //alert("pand = "+pand);
  t = q * price + q * pand;
  grand = grand + q * price;
  //alert("t = "+t+" := "+twodec(t));
  if (t > 0) {
     tot.innerHTML = "&pound;"+ twodec(t);
  } else {
     tot.innerHTML = "&nbsp;";
  }
  pnp = pnp + q * pand;
  //alert ("end book");
  return 0;
}
function comments() {

 }
 /* new_quantity(selectionbox, mnemonic) 
    copy the value to the printer equivalelt field mnemonic_p
    save the value to the cookie
    The selection fiels will be called mnemonic_sel and the printer equivalent mnemonic_sel_p
    */
 
 function new_quantity(source, mnemonic) {
    var value = source.value;
    if (source.id != mnemonic+"_sel") {
        alert("Warning source.id wrong at "+source.id);
    }
    var el_p = document.getElementById(mnemonic+"_sel_p");
    if (el_p) {
        //alert("source = "+source.value);
        if (value == "0") {
            el_p.innerHTML ="&nbsp;";
        } else {
            el_p.innerHTML= value;
        }
    } else {
        alert("warning element "+mnemonic+"_sel_p not on form");
    }
    amend_order(mnemonic, value);
 }    

function calculate() {
  //alert("calc");
  pnp = 0;
  grand = 0;

  book2("sgy2010_sel", "sgy2010_tot", 8.95, 1.25, null, null);
  book2("ssei_sel", "ssei_tot", 8.00, 1.50, null, null);
  book2("green_sel", "green_tot", 9.95, 1.50, null, null);
  book2("pmr_sel", "pmr_tot", 12.0, 1.25, null, null);
  book2("acc_sel", "acc_tot", 7.95, 1.25, null, null);
  book2("cg_sel", "cg_tot", 4.25, 0.75, null, null);
  book2("fg_sel", "fg_tot", 4.25, 0.75, null, null);
  book2("lag_sel", "lag_tot", 4.25, 0.75, null, null);
  book2("pg_sel", "pg_tot", 4.25, 0.75, null, null);
  book2("sg_sel", "sg_tot", 4.25, 0.75, null, null);
  book2("pol_sel", "pol_tot", 16.95, 2, null, null);
  book2("hag_sel", "hag_tot", 7.50, 1.25, null, null);
  book2("mon_sel", "mon_tot", 7.50, 1.25, null, null);
//  book2("insp_sel", "insp_tot", 6.75, 1.25, null, null);
  book2("start_sel", "start_tot", 2.5, .5, null, null);
  book2("jug_sel", "jug_tot", 11, 1.25, null, null);
  book2("mwgb_sel", "mwgb_tot", 46.00, 0, null, null);
  book2("ta_sel", "ta_tot", 16, 2.75, null, null);
  book2("bs_sel", "bs_tot", 7.50, 1.25, null, null);
  comments();  
  var item_tot = document.getElementById("item_tot");
  if(!item_tot) {
	  alert("Field 'item_tot' missing from order form HTML");
  }
  if (grand > 0) {
     item_tot.innerHTML = "&pound;" + twodec(grand);
	 } else {
	 item_tot.innerHTML = "&nbsp;";
	 }

  var pnp_tot = document.getElementById("pnp_tot");
  if(!pnp_tot) {
	  alert("Field 'pnp_tot' missing from order form HTML");
  }
  if (pnp > 10) {
     pnp_tot.innerHTML = "&pound;10.00 *";
	 grand = grand + 10;
  } else {
	 grand = grand + pnp;
     pnp_tot.innerHTML  = "&pound;"+twodec(pnp);
  } 
  
  var grand_tot = document.getElementById("grand_tot");
  if(!grand_tot) {
	  alert("Field 'grand_tot' missing from order form HTML");
  }
  if (grand > 0) {
     grand_tot.innerHTML = "&pound;"+twodec(grand);
  } else {
     grand_tot.innerHTML = "&nbsp;";
  }
  //Following 2 lines required for Paypal implementation
 // _xclick.amount.value = twodec(grand);
 // _xclick.item_name.value = "grand total";
}

/* work through order form updating the cookies with the details 
@param an array of all the mnemonics used on the order form
*/
function remember_all() {
}
/* work through order form populating the selection boxes with the values from the cookies 
@param an array of all the mnemonics (eg "pmr" used on the order form
*/
function populate_all(mnemonic_array) {
    var new_value = "";
    for(var i = 0; i < mnemonic_array.length; i++) {
        var mnemonic = mnemonic_array[i];
        var element_name =  mnemonic+"_sel";
        var el = document.getElementById(element_name); 
        if (el) {
            /*if(mnemonic == "pol_cd") {
                alert("new value for pol_cd_sel = "+get_order(mnemonic));
            } */
            new_value =  get_order(mnemonic);
            if (new_value != "")  {
                el.value = new_value;
            }
        } else {
            alert("element "+element_name+" not found on order form");
        }  
        var printer_element_name =  mnemonic+"_sel_p";
        var printer_el = document.getElementById(printer_element_name); 
        if (printer_el) {
            //printer_element new value should be same as screen element
            new_value =  get_order(mnemonic);
            if ((new_value == "")||(new_value == "0"))  {
                new_value = "&nbsp";
            }
            printer_el.innerHTML = new_value;
        } else {
            alert("element "+element_name+" not found on order form");
        } 
       /* if (mnemonic == "ta") {
            alert("mnemonic ta new_value = "+new_value);
        }*/ 
    }
}
function populate_text_fields(text_fields_array) {
    for(var i = 0; i < text_fields_array.length; i++) {
        var field_id = text_fields[i];
        var el = document.getElementById(field_id); 
        if (el) {
            el.value = get_order(field_id);
        } else {
            alert("element "+field_id+" not found on order form");
        }  
    }
}
function populate_radio_fields(radio_fields_array) {
    for(var i = 0; i < radio_fields_array.length; i++) {
        var field_id = radio_fields[i];
        var el = document.getElementById(field_id); 
        if (el) {
            var value = get_order(field_id);
            //alert("radio button "+field_id+ " = "+value);
            if (value == "true") {
                el.checked = true;
            } else {
                el.checked = "";
            }
        } else {
            alert("element "+field_id+" not found on order form");
        }  
    }
}
function new_data(el) {
    var name = el.id;
    value = el.value;
    amend_order(name, value);
}
/* a Radio button has changed - save away the new checked status */
function new_data_radio(id1, id2, id3) {
    var el1 = document.getElementById(id1);
    if (!el1) {
        alert("cannot find element "+id1);
    }
    value = el1.checked;
    //alert("value for "+id1+" = "+value);
    amend_order(id1, value);
    var el2 = document.getElementById(id2);
    value = el2.checked;
    //alert("value for "+id2+" = "+value);
    amend_order(id2, value);
    var el3 = document.getElementById(id3);
    value = el3.checked;
    //alert("value for "+id3+" = "+value);
    amend_order(id3, value);
}
/* called from individual pages 
get the current total ordered from the cookie and add one to it */
function add_to_order(caller, mnemonic) {
    var count = get_order(mnemonic);
    var old_number = parseInt(count);
    var new_number;
    //alert("add to"+old_number);
    if (isNaN(old_number)) {
        new_number = 1;
    } else {
        new_number = old_number + 1;
    }
    //alert("add to"+old_number);
    amend_order(mnemonic,new_number);
    update_count(mnemonic);
    if (new_number >= 1) {
        caller.src = 'buttons/add_another_copy_proud.gif';
    }
    //alert("result="+ get_order(mnemonic));
    return new_number;
}
function add_copy_hover(caller, mnemonic) {
    var count = get_order(mnemonic);
    var old_number = parseInt(count);
    if (old_number < 1) {
        caller.src = 'buttons/add_a_copy_hover.gif';
    } else {
        caller.src = 'buttons/add_another_copy_hover.gif';
    }
}
function add_copy_out(caller, mnemonic) {
    var count = get_order(mnemonic);
    var old_number = parseInt(count);
    if (old_number < 1) {
        caller.src = 'buttons/add_a_copy_proud.gif';
    } else {
        caller.src = 'buttons/add_another_copy_proud.gif';
    }
}
function add_copy_depressed(caller, mnemonic) {
    var count = get_order(mnemonic);
    var old_number = parseInt(count);
    if (old_number < 1) {
        caller.src = 'buttons/add_a_copy_depressed.gif';
    } else {
        caller.src = 'buttons/add_another_copy_depressed.gif';
    }
}
/* look for a field called xxx_count and if found change it to blank or
"(y currently on order form)" */
function update_count(mnemonic) {
    var el = document.getElementById(mnemonic+"_count");
    if(el) {
        var count = get_order(mnemonic);
        if (count > 0) {
            el.innerHTML = " ("+count+" currently on order form)";
        } else {
            el.innerHTML = "(Click once for each copy required)";
        }
    } else {
        //alert("Element "+mnemonic+"_count"+" not found on this document");
    }
}
function update_button(mnemonic) {
    var el = document.getElementById(mnemonic+"_button");
    if(el) {
        add_copy_out(el, mnemonic);
    } else {
        //alert("button "+mnemonic+"_button not found");
    }
}


/* change the current order for this item to the new value */
function amend_order(mnemonic, amount) {
    var cookie_name = "order_"+mnemonic;
    //alert("set coolie "+mnemonic+ " = "+amount);
    set_cookie(cookie_name, amount);
}
/* get current order for this item 
@param the mnemonic for this product eg. ras for Recruitment and Selection (always lower case)
*/
function get_order(mnemonic) {
    result = get_cookie("order_"+mnemonic);
    //alert("coolie "+mnemonic+ " = "+result);
    return result;
}
