var root_dir = (window.location.href.indexOf("192.168.0.202") != -1 ? "/epic" : "");
var browser, verStr = navigator.appVersion, app = navigator.appName, version = parseFloat(verStr);

function test_browser() {
  if ((app.indexOf('Netscape') != -1) && version >= 5) {
    browser = "netscape";
  }
  else if ((app.indexOf('Microsoft') != -1) && version >= 4) {
    browser = "ie";
  }
}

var body_content = new Array("bodycontent", "preloader");
test_browser();
function show_sub(show_which_sub, var_array, mode) {

  if (mode == "preloader") {
    for (i = 0; i < eval(var_array + ".length"); i++) {
      if (eval(var_array + "[i]") != show_which_sub) {
        hide_sub(eval(var_array + "[i]"), mode);
      }
    }
  }

  if (show_which_sub != "") {
    if (browser == "netscape") {
      if (document.getElementById(show_which_sub).style.display == 'block') {
        document.getElementById(show_which_sub).style.display = 'none';
      }
      else {
        document.getElementById(show_which_sub).style.display = 'block';
      }
    }
    else if (browser == "ie") {
      show_menu = eval(show_which_sub);
      if(show_menu.style.display == 'block'){
        show_menu.style.display = 'none';
      }
      else {
        show_menu.style.display = 'block';
      }
    }
  }

}

function hide_sub(which_sub, mode) {
  if (browser == "netscape") {
    document.getElementById(which_sub).style.display = 'none';
  }
  else if (browser == "ie") {
    sub_menu = eval(which_sub);
    sub_menu.style.display = 'none';
  }
}

function auto_hide() {
  show_sub("");
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){
    if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
      if (a[i].indexOf("#")!=0){
        d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
      }
      }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){
      document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
    }
}

function scroll_up() {
  current_pos = document.getElementById('scroll_clipper').scrollTop;
  document.getElementById('scroll_clipper').scrollTop = current_pos + 20;
}

function scroll_down() {
  current_pos = document.getElementById('scroll_clipper').scrollTop;
  document.getElementById('scroll_clipper').scrollTop = current_pos - 20;
}

function char_count(eid, maxlen, max_lines) {

  var inputstr;
  var strlen;
  var charleft;
  var numcr;
  var temp = new Array();

  if (document.getElementById(eid).value != "") {
    inputstr = document.getElementById(eid).value;
    strlen = inputstr.length;

    if (strlen > maxlen) {
      document.getElementById(eid).value = document.getElementById(eid).value.substring(0,maxlen);
      charleft = 0;
      alert("Exceeds maximum number of characters!");
    }
    else {
      charleft = maxlen - strlen;
    }

    document.getElementById('charleft' + eid).innerHTML = charleft + " / " + maxlen;

  }

  if (max_lines > 0) {

    var temp2 = document.getElementById(eid).value.split('\r\n');
    var temp_val = "";
    counter = 0;
    if (temp2.length > max_lines) {
      alert("Exceeds maximum number of lines!");
      for (i = 0; i < max_lines; i++) {
        temp_val = temp_val + temp2[i];
        if (i < (max_lines - 1)) {
          temp_val = temp_val + "\n";
        }
      }
      document.getElementById(eid).value = temp_val;
    }

  }

}

function row_over(row_id, row_color) {
  document.getElementById(row_id).style.backgroundColor = row_color;
}

function add_to_favorite(url, title) {
  if (window.sidebar) {
    window.sidebar.addPanel(title, url, "");
  }
  else if( window.external ) {
    window.external.AddFavorite(url, title);
  }
  else if (window.opera && window.print) {
    return true;
  }
}

function mdiv(menu_id) {

  ffile = "flash/mdiv.swf";
  mn_name = "MM" + menu_id;
  document.write("<span id='" + mn_name + "'></span>");

  var fmn = new SWFObject(ffile, "mn", "282", "5", "5", "#FFFFFF");
  fmn.addParam("wmode", "transparent");
  fmn.write(mn_name);

}

function mm(page_title, menu_id, level, page, submenu, quick_link) {

  ffile = "flash/m" + level + ".swf";
  mn_name = "MM" + menu_id;
  document.write("<span id='" + mn_name + "'></span>");

  var fmn = new SWFObject(ffile, "mn", "282", "35", "6", "#FFFFFF");
  fmn.addVariable("page_id",    menu_id);
  fmn.addVariable("page",       page);
  fmn.addVariable("page_title", page_title);
  fmn.addVariable("submenu",     submenu);
  fmn.addVariable("quick_link",     quick_link);
  fmn.addParam("wmode", "transparent");
  fmn.write(mn_name);

}

var w_width = w_height = 0;

function get_window_size() {
  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
      w_width = window.innerWidth;
      w_height = window.innerHeight;
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      w_width = document.body.offsetWidth;
      w_height = document.body.offsetHeight;
    }
  }
}

active_image = "";

function show_image(img_id, pic, pic_width, pic_height) {

  //get_window_size();

  hide_image();

  var so = new SWFObject("flash/pop.swf", "pop_up", 330, 255, "6", "#FFFFFF");
  so.addParam("wmode", "transparent");
  so.addVariable("pic", pic);
  so.addVariable("pic_width", pic_width);
  so.addVariable("pic_height", pic_height);
  so.write('big_pic');

  document.getElementById(img_id).style.display = "block";

  active_image = img_id;

}

function hide_image() {

  if (document.getElementById(active_image)) {
    document.getElementById(active_image).style.display = "none";
  }

}

function youtube(youtube_code) {

  url = "youtube_viewer.php?youtube=" + youtube_code;
  window.open(url,'browser','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=445,height=375')

}

var min = 8;
var max = 16;

function font_up() {

  var p = document.getElementsByTagName('body');
  for(i=0;i<p.length;i++) {
    if(p[i].style.fontSize) {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    } else {
      var s = 12;
    }
    if(s!=max) {
      s += 1;
    }
    p[i].style.fontSize = s+"px"
  }
}

function font_down() {

  var p = document.getElementsByTagName('body');
  for(i=0;i<p.length;i++) {
    if(p[i].style.fontSize) {
      var s = parseInt(p[i].style.fontSize.replace("px",""));
    } else {
      var s = 12;
    }
    if(s!=min) {
      s -= 1;
    }
    p[i].style.fontSize = s+"px"
  }

}

function CreateBookmarkLink(title, url) {

  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  }
  else if( window.external ) { // IE Favorite
    window.external.AddFavorite( url, title);
  }
  else if(window.opera && window.print) { // Opera Hotlist
    return true;
  }
}

if (window.external) {
//document.write('<a href =\"javascript:CreateBookmarkLink()\");">Add to Favorites</a>');
}
else  if (window.sidebar) {
//document.write('<a href =\"javascript:CreateBookmarkLink()\");">Bookmark Page</a>');
}
else if (window.opera && window.print) {
//document.write('<a href =\"javascript:CreateBookmarkLink()\");">Add Bookmark</a>');
}

function show_subscription_box() {
  document.getElementById('newsletter_subscription_box_1').style.display='block'
  document.getElementById('subs_name').focus();
}

var active_faq = "";

function show_faq (eid) {

  if (active_faq != "") {
    document.getElementById(active_faq).style.display = "none";
  }
  document.getElementById(eid).style.display = "block";
  active_faq = eid;

}

function refresh_calendar() {
  cyear  = document.getElementById('cyear').value;
  cmonth = document.getElementById('cmonth').value;
  window.location = "index.php?year=" + cyear + "&month=" + cmonth;
}

var event_req = "";

function get_event(date) {

  var query = "&date=" + date;
  var url   = "ajax/events.ajx.php";

  document.getElementById('selected_event').innerHTML = "Loading event...";

  tmp_http_req   = new init_post_request();
  event_req      = tmp_http_req.fn_request;

  event_req.onreadystatechange = show_event;

  post_request(event_req, url, query);

  return;

}

function show_event() {

  if (event_req.readyState == 4 || event_req.readyState == "complete") {
    data = event_req.responseText;
    document.getElementById('selected_event').innerHTML = data;
  }

}

var active_answer = "";

function show_answer(eid) {

  if (active_answer != "") {
    document.getElementById(active_answer).style.display = "none";
  }
  document.getElementById(eid).style.display = "block";
  active_answer = eid;

}

function update_team_details() {

  if (document.getElementById('team_name').value != "") {
    document.getElementById('new_team_details').style.display = "block";
  }
  else {
    document.getElementById('new_team_details').style.display = "none";
  }
  document.getElementById('new_team_name').value = document.getElementById('team_name').value;

}

function disable_rego_buttons() {
  var buttons = new Array("reset", "cmd", "cmd1", "cmd2", "cmd3");
  for (i = 0; i <= buttons.length; i++) {
    if (document.getElementById(buttons[i])) {
      document.getElementById(buttons[i]).disabled = true;
    }
  }
}

function alert_elite() {

  if (document.getElementById('category').value == 0 && document.getElementById('category').value != "") {
    alert("Qualifying time required: \n- Male: < 4:15\n- Female: < 4:45");
  }

}

function select_category (current) {

  dob_year = document.getElementById('dob_year').value;

  if (current - dob_year <= 14) {
    document.getElementById('category').value = 1;
  }
  else if (current - dob_year <= 17) {
    document.getElementById('category').value = 2;
  }
  else if (current - dob_year <= 34) {
    document.getElementById('category').value = 3;
  }
  else if (current - dob_year <= 49) {
    document.getElementById('category').value = 4;
  }
  else {
    document.getElementById('category').value = 5;
  }

}

function check_source() {
  document.getElementById('other_source').style.display = "none";
  if (document.getElementById('source').value == "Other") {
    document.getElementById('other_source').style.display = "block";
  }
}

function check_billing_address() {
  var fields = new Array("address_1", "address_2", "city", "state", "zip");
  if (document.getElementById('billing_address').value == "same") {
    for (i = 0; i < fields.length; i++) {
      document.getElementById('bill_' + fields[i]).value = document.getElementById(fields[i]).value;
    }
  }
}

function getViewportHeight() {
  if (window.innerHeight!=window.undefined) return window.innerHeight;
  if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
  if (document.body) return document.body.clientHeight;

  return window.undefined;
}

function getViewportWidth() {
  var offset = 16;
  var width = null;
  if (window.innerWidth!=window.undefined) return window.innerWidth + offset;
  if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth + offset;
  if (document.body) return document.body.clientWidth + offset;
}

function getScrollTop() {
  if (self.pageYOffset) // all except Explorer
  {
    return self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  // Explorer 6 Strict
  {
    return document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
    return document.body.scrollTop;
  }
}

function getScrollLeft() {
  if (self.pageXOffset) // all except Explorer
  {
    return self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollLeft)
  // Explorer 6 Strict
  {
    return document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
    return document.body.scrollLeft;
  }
}

var actively_displayed_div = "";

function center_div(which_div) {

  actively_displayed_div = which_div;
  document.getElementById(which_div).style.display = "block";

  newWidth  = document.getElementById(which_div).offsetWidth;
  newHeight = document.getElementById(which_div).offsetHeight;

  var theBody = document.getElementsByTagName("body")[0];
  var scTop = parseInt(getScrollTop(),10);
  var scLeft = parseInt(theBody.scrollLeft,10);
  var fullHeight = getViewportHeight();
  var fullWidth = getViewportWidth();
  var sc_left = getScrollLeft();

  document.getElementById(which_div).style.top     = (scTop + ((fullHeight - newHeight) / 2)) + "px";
  document.getElementById(which_div).style.left    = (scLeft + sc_left + ((fullWidth - newWidth) / 2)) + "px";

}

function re_center_div () {
  if (actively_displayed_div != "" && document.getElementById(actively_displayed_div).style.display == "block") {
    center_div(actively_displayed_div);
  }
}

function addevent(obj,evt,fn,capt){
  if(obj.addEventListener) {
    obj.addEventListener(evt, fn, capt);
    return true;
  }
  else if(obj.attachEvent) {
    obj.attachEvent('on'+evt, fn);
    return true;
  }
  else return false;
}

if (document.getElementById && document.getElementsByTagName) {
  addevent(window, 'load', re_center_div, false);
  addevent(window, 'resize', re_center_div, false);
  addevent(window, 'scroll', re_center_div, false);
}

function open_in_shadowbox (source, type, title, width, height) {
  Shadowbox.open({
    player:     type,
    title:      title,
    content:    source,
    width:			width,
    height:			height
  });
}

//Shadowbox.loadSkin('classic', '/javascripts/skin');
//Shadowbox.loadLanguage('en', '/javascripts/lang');
//Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], '/javascripts/player');

function sb_init() {
  Shadowbox.init({
    // let's skip the automatic setup because we don't have any
    // properly configured link elements on the page
    skipSetup: true
  });
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


var current_adding_cat;

/*
function load_grades(commodity_id, grade_id, select_id, select_name, empty_option)
{
	var wheat_grades =
		['H1', 'H2', 'AUH2', 'APW1', 'ASW1', 'GP1', 'FED1', 'AUW1'];
	var barley_grades =
		['Malt SC1', 'Malt GA1', 'Malt Other', 'FB1', 'FB2', 'FB3'];
	var oats_grades =
		['Moat', 'Foat', 'Foa2', 'Foa3', 'Foa4'];
	var triticale_grades =
		['TRIT', 'TRIF', 'TRI2', 'TRI3'];
	var lupins_grades =
		['LUPN', 'LUAF'];
	var peas_grades =
		['PEAS'];
	var canola_grades =
		['CANH', 'CANA', 'CAN2'];

	var arr = [];

	switch (document.getElementById(commodity_id).value)
	{
	case 'Wheat':
		arr = wheat_grades;
		break;
	case 'Barley':
		arr = barley_grades;
		break;
	case 'Oats':
		arr = oats_grades;
		break;
	case 'Triticale':
		arr = triticale_grades;
		break;
	case 'Lupins':
		arr = lupins_grades;
		break;
	case 'Peas':
		arr = peas_grades;
		break;
	case 'Canola':
		arr = canola_grades;
		break;
	default:
		break;
	}

	var html = '<select name="';

	if (select_name != null)
	{
		html = html + select_name;
	}
	else
	{
		html = html + 'grade';
	}

	html = html + '"';

	if (select_id != null)
	{
		html = html + ' id="' + select_id + '"';
	}

	html = html + '>';

	if (empty_option != null)
	{
		html += '<option value="">' + empty_option + '</option>';
	}

	var value;

	for (var i = 0; i < arr.length; i++)
	{
		value = arr[i];
		html += '<option value="' + value + '">' + value + '</option>';
	}

	html += '</select>';

	document.getElementById(grade_id).innerHTML = html;
}
*/

function validate_add_listing_form()
{
  if (!document.add_listing_form_warehoused.grade) {
    alert("Please wait for the form elements to finish loading.");
    return false;
  }

  var b1 = !document.add_listing_form_warehoused.quantity.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b11 = document.add_listing_form_warehoused.quantity.value.match(/^0+\.?0*$/);
  var b2 = document.add_listing_form_warehoused.target_price.value != ""
  && !document.add_listing_form_warehoused.target_price.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  if (b1 || b11 || b2)
  {
    var s = "Please correct the following.";

    if (b1)
    {
      s += "\n* Please enter the quantity.";
    }

    if (b11)
    {
      s += "\n* Quantity should not be zero.";
    }

    if (b2)
    {
      s += "\n* Target price should be a number.";
    }

    alert(s);

    return false;
  }

  return true;
}

function validate_add_listing_form_on_farm()
{
  if (!document.add_listing_form_on_farm.grade) {
    alert("Please wait for the form elements to finish loading.");
    return false;
  }

  var b1 = !document.add_listing_form_on_farm.quantity.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b11 = document.add_listing_form_on_farm.quantity.value.match(/^0+\.?0*$/);
  var b2 = !document.add_listing_form_on_farm.test_weight.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b21 = document.add_listing_form_on_farm.test_weight.value.match(/^0+\.?0*$/);
//  var b3 = !document.add_listing_form_on_farm.protein.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b31 = document.add_listing_form_on_farm.protein.value != "" && document.add_listing_form_on_farm.protein.value.match(/^0+\.?0*$/);
  var b4 = !document.add_listing_form_on_farm.moisture.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b41 = document.add_listing_form_on_farm.moisture.value.match(/^0+\.?0*$/);
  var b5 = !document.add_listing_form_on_farm.screenings.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b51 = document.add_listing_form_on_farm.screenings.value.match(/^0+\.?0*$/);
  var b6 = document.add_listing_form_on_farm.target_price.value != ""
  && !document.add_listing_form_on_farm.target_price.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  /*var b7 = (document.getElementById('silo_radio').checked
			&& !document.add_listing_form_on_farm.silo_fenced.checked
			&& !document.add_listing_form_on_farm.silo_weed_control.checked
			&& !document.add_listing_form_on_farm.silo_vermin_controlled.checked
			&& !document.add_listing_form_on_farm.silo_water_proof.checked
		|| document.getElementById('shed_radio').checked
			&& !document.add_listing_form_on_farm.shed_fenced.checked
			&& !document.add_listing_form_on_farm.shed_weed_control.checked
			&& !document.add_listing_form_on_farm.shed_vermin_controlled.checked
			&& !document.add_listing_form_on_farm.shed_water_proof.checked);*/
//  var b8 = (document.add_listing_form_on_farm.treatment_date.value == "");

  if (b1 || b11 || b2 || b21 || b31 || b4 || b41 || b5 || b51 || b6)
  {
    var s = "Please correct the following.";

    if (b1)
    {
      s += "\n* Please enter the quantity.";
    }

    if (b11)
    {
      s += "\n* Quantity should not be zero.";
    }

    if (b2)
    {
      s += "\n* Please enter the test weight.";
    }

    if (b21)
    {
      s += "\n* Test weight should not be zero.";
    }

    if (b31)
    {
      s += "\n* Protein should not be zero.";
    }

    if (b4)
    {
      s += "\n* Please enter the moisture.";
    }

    if (b41)
    {
      s += "\n* Moisture should not be zero.";
    }

    if (b5)
    {
      s += "\n* Please enter the screenings.";
    }

    if (b51)
    {
      s += "\n* Screenings should not be zero.";
    }

    if (b6)
    {
      s += "\n* Target price should be a number.";
    }

    alert(s);

    return false;
  }

  return true;
}

function validate_add_listing_form_contracts()
{
  if (!document.add_listing_form_contracts.grade) {
    alert("Please wait for the form elements to finish loading.");
    return false;
  }

  var b1 = !document.add_listing_form_contracts.quantity.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b11 = document.add_listing_form_contracts.quantity.value.match(/^0+\.?0*$/);
  var b2 = document.add_listing_form_contracts.target_price.value != ""
  && !document.add_listing_form_contracts.target_price.value.match(/^[0-9]+\.?[0-9]{0,2}$/);

  if (b1 || b11)
  {
    var s = "Please correct the following.";

    if (b1)
    {
      s += "\n* Please enter the quantity.";
    }

    if (b11)
    {
      s += "\n* Quantity should not be zero.";
    }

    if (b2)
    {
      s += "\n* Target price should be a number.";
    }

    alert(s);

    return false;
  }

  return true;
}

function set_current_adding_cat(cat)
{
  if (cat == "warehoused")
  {
    document.getElementById("add_grain_current_cash").className = "add_grain_parent_cat";
    document.getElementById("warehoused_tab").className = "particular hand_pointer";
    document.getElementById("on_farm_tab").className = "hand_pointer";
    document.getElementById("contracts_tab").className = "hand_pointer add_grain_parent_cat_normal";
    document.getElementById("add_listing_form_warehoused").style.display = "";
    document.getElementById("add_listing_form_on_farm").style.display = "none";
    document.getElementById("add_listing_form_contracts").style.display = "none";
    current_adding_cat = "warehoused";
  }
  else if (cat == "on_farm")
  {
    document.getElementById("add_grain_current_cash").className = "add_grain_parent_cat";
    document.getElementById("warehoused_tab").className = "hand_pointer";
    document.getElementById("on_farm_tab").className = "particular hand_pointer";
    document.getElementById("contracts_tab").className = "hand_pointer add_grain_parent_cat_normal";
    document.getElementById("add_listing_form_warehoused").style.display = "none";
    document.getElementById("add_listing_form_on_farm").style.display = "";
    document.getElementById("add_listing_form_contracts").style.display = "none";
    current_adding_cat = "on_farm";
  }
  else if (cat == "contracts")
  {
    document.getElementById("add_grain_current_cash").className = "add_grain_parent_cat_normal";
    document.getElementById("warehoused_tab").className = "hand_pointer";
    document.getElementById("on_farm_tab").className = "hand_pointer";
    document.getElementById("contracts_tab").className = "particular hand_pointer add_grain_parent_cat";
    document.getElementById("add_listing_form_warehoused").style.display = "none";
    document.getElementById("add_listing_form_on_farm").style.display = "none";
    document.getElementById("add_listing_form_contracts").style.display = "";
    current_adding_cat = "contracts";
  }
}

function set_current_tab(tab)
{
  if (tab == "grain_details")
  {
    document.getElementById("tab1").className = "tab_front";
    document.getElementById("tab2").className = "tab_back";
    document.getElementById("on_farm_form1").style.display = "";
    document.getElementById("on_farm_form2").style.display = "none";
  }
  else if (tab == "quality")
  {
    document.getElementById("tab1").className = "tab_back";
    document.getElementById("tab2").className = "tab_front";
    document.getElementById("on_farm_form1").style.display = "none";
    document.getElementById("on_farm_form2").style.display = "";
  }
}

function silos_show_hide_checkboxes(silo)
{
  if (silo == 1)
  {
    document.getElementById('silo_checkboxes').style.display = 'none';
    document.getElementById('shed_checkboxes').style.display = 'none';
  }
  else if (silo == 2)
  {
    document.getElementById('silo_checkboxes').style.display = 'none';
    document.getElementById('shed_checkboxes').style.display = 'none';
  }
  else if (silo == 3)
  {
    document.getElementById('silo_checkboxes').style.display = '';
    document.getElementById('shed_checkboxes').style.display = 'none';
  }
  else if (silo == 4)
  {
    document.getElementById('silo_checkboxes').style.display = 'none';
    document.getElementById('shed_checkboxes').style.display = '';
  }
}

function my_grain_show_hide_result(id)
{
  if (document.getElementById('c_' + id).style.display == '')
  {
    document.getElementById(id).className = 'hidden_tab';
    document.getElementById('c_' + id).style.display = 'none';
  }
  else
  {
    document.getElementById(id).className = 'reveal_tab';
    document.getElementById('c_' + id).style.display = '';
  }
}

function my_grain_delete_item_from_list(id, commodity, grade, site, parent_cat, sub_cat)
{
  var parent_category = null;
  var sub_category = null;

  switch (parent_cat)
  {
    case "current_cash":
      parent_category = "Current Cash";
      break;
    case "current_cash_contracts":
      parent_category = "Current Cash Contracts";
      break;
  }

  switch (sub_cat)
  {
    case "warehoused":
      sub_category = "Warehoused";
      break;
    case "on_farm":
      sub_category = "On Farm";
      break;
  }

  if (confirm(
    "You are about to delete this grain.\n"
    + commodity + " - " + grade + " (" + site + ") from " + parent_category + (sub_category ? " - " + sub_category : "") + "\n"
    + "Are you sure you want to continue?"
    ))
    {
    document.getElementById('my_grain_list_form_delete_id').value = id;
    document.getElementById('my_grain_list_form').submit();
  }
}

function my_grain_dismiss_message()
{
  document.getElementById('my_grain_message').style.display = 'none';
}

var editing_id;
var commodity_temp;
var grade_temp;
var quantity_temp;
var site_temp;
var quality_temp;
var moisture_temp;
var protein_temp;
var screenings_temp;
var harvest_year_temp;
var target_price_temp;
var cash_deferred_temp;
var no_price_established_temp;
var actions_temp;

function init_item_editing()
{
  editing_id = 0;
  commodity_temp = "";
  grade_temp = "";
  quantity_temp = "";
  site_temp = "";
  quality_temp = "";
  moisture_temp = "";
  protein_temp = "";
  screenings_temp = "";
  target_price_temp = "";
  cash_deferred_temp = "";
  no_price_established_temp = "";
  actions_temp = "";
}

var commodities_load_done = 1;
var grades_load_done = 1;
var sites_load_done = 1;

function my_grain_edit_item_in_list(id, actions, cat)
{
  if (commodities_load_done == 0 || grades_load_done == 0 || sites_load_done == 0) {
    alert("Please wait for the form elements to finish loading.");
    return;
  }

  var commodity_td = document.getElementById("list_item_commodity_" + id);
  var grade_td = document.getElementById("list_item_grade_" + id);
  var quantity_td = document.getElementById("list_item_quantity_" + id);
//  var site_td = document.getElementById("list_item_site_" + id);
//  var quality_td = document.getElementById("list_item_quality_" + id);
  var moisture_td = document.getElementById("list_item_moisture_" + id);
  var protein_td = document.getElementById("list_item_protein_" + id);
  var screenings_td = document.getElementById("list_item_screenings_" + id);
  var harvest_year_td = document.getElementById("list_item_harvest_year_" + id);
  var target_price_td = document.getElementById("list_item_target_price_" + id);
  var cash_deferred_td = document.getElementById("list_item_cash_deferred_" + id);
  var no_price_established_td = document.getElementById("list_item_no_price_established_" + id);
  var actions_td = document.getElementById("list_item_actions_" + id);

  if (editing_id == 0)
  {
    commodities_load_done = 0;
    grades_load_done = 0;
//    sites_load_done = 0;

    editing_id = id;
    commodity_temp = commodity_td.innerHTML;
    grade_temp = grade_td.innerHTML;
    quantity_temp = quantity_td.innerHTML;
//    site_temp = site_td.innerHTML;
//    quality_temp = quality_td.innerHTML;
    moisture_temp = moisture_td.innerHTML;
    protein_temp = protein_td.innerHTML;
    screenings_temp = screenings_td.innerHTML;
    harvest_year_temp = harvest_year_td.innerHTML;
    target_price_temp = target_price_td.innerHTML;
    cash_deferred_temp = cash_deferred_td.innerHTML;
    no_price_established_temp = no_price_established_td.innerHTML;
    actions_temp = actions_td.innerHTML;

    if (grade_temp.indexOf('(Multi)') == -1) grade_td.innerHTML = '<span id="grade_editing_container"></span>';

    get_commodities("list_item_commodity_" + id, "commodity_editing", "commodity", null, function() {
      var obj = document.getElementById("commodity_editing");
      if (grade_temp.indexOf('(Multi)') == -1) addevent(obj, 'change', function() {
        get_grades('commodity_editing', 'grade_editing_container', 'grade_editing');
      }, false);
      select_option_from_value('commodity_editing', commodity_temp);
      obj.style.width = "40px";
      if (grade_temp.indexOf('(Multi)') == -1) get_grades('commodity_editing', 'grade_editing_container', 'grade_editing', null, null, function() {
        obj = document.getElementById("grade_editing");
        obj.style.width = "40px"
        select_option_from_value('grade_editing', grade_temp);
        grades_load_done = 1;
      });
      else
        grades_load_done = 1;
      commodities_load_done = 1;
    }, cat);

    quantity_td.innerHTML = '<input type="text" style="width: 30px;" name="quantity" id="quantity_editing" value="' + quantity_temp + '" maxlength="9" />';

//    get_sites("list_item_site_" + id, "site_editing", "site", null, function() {
//      var obj = document.getElementById("site_editing");
//      obj.style.width = "60px"
//      select_option_from_value('site_editing', site_temp);
//      sites_load_done = 1;
//    });

//    quality_td.innerHTML = '<input type="text" style="width: 30px;" name="quality" value="' + quality_temp + '" maxlength="9" />';
    moisture_td.innerHTML = '<input type="text" style="width: 30px;" name="moisture" value="' + moisture_temp + '" maxlength="9" />';
    protein_td.innerHTML = '<input type="text" style="width: 30px;" name="protein" value="' + protein_temp + '" maxlength="9" />';
    screenings_td.innerHTML = '<input type="text" style="width: 30px;" name="screenings" value="' + screenings_temp + '" maxlength="9" />';

    var d = new Date();
    var year = d.getFullYear();
    harvest_year_td.innerHTML = '<select style="width: 40px;" name="harvest_year" id="harvest_year_editing"><option value="' + year + '">' + year + '</option><option value="' + (year - 1) + '">' + (year - 1) + '</option><option value="' + (year - 2) + '">' + (year - 2) + '</option></select>';
    select_option_from_value('harvest_year_editing', harvest_year_temp);
    target_price_td.innerHTML = '<input type="text" style="width: 30px;" name="target_price" id="target_price_editing" value="' + target_price_temp.substring(1) + '" maxlength="9" />';

    cash_deferred_td.innerHTML = '<select style="width: 40px;" name="cash_deferred" id="cash_deferred_editing"><option value="1">Yes</option><option value="0">No</option></select>';
    select_option_from_value('cash_deferred_editing', (cash_deferred_temp == "No" ? "0" : "1"));
    no_price_established_td.innerHTML = '<select style="width: 40px;" name="no_price_established" id="no_price_established_editing"><option value="1">Yes</option><option value="0">No</option></select>';
    select_option_from_value('no_price_established_editing', (no_price_established_temp == "No" ? "0" : "1"));

    if (actions != null)
    {
      actions_td.innerHTML = unescape(actions);
    }
  }
  else if (editing_id == id)
  {
    commodity_td.innerHTML = commodity_temp;
    if (grade_temp.indexOf('(Multi)') == -1) grade_td.innerHTML = grade_temp;
    quantity_td.innerHTML = quantity_temp;
//    site_td.innerHTML = site_temp;
//    quality_td.innerHTML = quality_temp;
    moisture_td.innerHTML = moisture_temp;
    protein_td.innerHTML = protein_temp;
    screenings_td.innerHTML = screenings_temp;
    harvest_year_td.innerHTML = harvest_year_temp;
    target_price_td.innerHTML = target_price_temp;
    cash_deferred_td.innerHTML = cash_deferred_temp;
    no_price_established_td.innerHTML = no_price_established_temp;
    actions_td.innerHTML = actions_temp;

    editing_id = 0;
  }
  else
  {
    my_grain_edit_item_in_list(editing_id);
    my_grain_edit_item_in_list(id, actions, cat);
  }
}

function select_option_from_value(select_id, value)
{
  var select_element = document.getElementById(select_id);

  for (var i = 0; i < select_element.options.length; i++)
  {
    var option_element = select_element.options[i];

    if (option_element.value == value)
    {
      option_element.selected = true;
    }
    else
    {
      option_element.selected = false;
    }
  }
}

function my_grain_edit_item_do_save(id)
{
  if (commodities_load_done == 0 || grades_load_done == 0 || sites_load_done == 0) {
    alert("Please wait for the form elements to finish loading.");
    return;
  }

  var b1 = !document.my_grain_list_form.quantity.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
//  var b2 = !document.my_grain_list_form.quality.value.match(/^[0-9]+$/);
  var b3 = !document.my_grain_list_form.moisture.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b4 = !document.my_grain_list_form.protein.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b5 = !document.my_grain_list_form.screenings.value.match(/^[0-9]+\.?[0-9]{0,2}$/);
  var b6 = !document.my_grain_list_form.target_price.value.match(/^[0-9]+\.?[0-9]{0,2}$/);

  if (b1 /*|| b2*/ || b3 || b4 || b5 || b6)
  {
    var s = "Please correct the following.";

    if (b1)
    {
      s += "\n* Please enter a valid quantity (2 decimal point allowed).";
    }

//    if (b2)
//    {
//      s += "\n* Please enter a valid quality (a number without decimal point).";
//    }

    if (b3)
    {
      s += "\n* Please enter a valid moisture (2 decimal point allowed).";
    }

    if (b4)
    {
      s += "\n* Please enter a valid protein (2 decimal point allowed).";
    }

    if (b5)
    {
      s += "\n* Please enter a valid screenings (2 decimal point allowed).";
    }

    if (b6)
    {
      s += "\n* Please enter a valid target price (2 decimal point allowed).";
    }

    alert(s);

    return;
  }
// save change to DB by AJAX
 
      var commodity               = $("#commodity_editing").val();
      var grade                   = $("#grade_editing").val();
      var quantity                = $("#quantity_editing").val();
      var harvest_year            = $("#harvest_year_editing").val();
      var target_price            = $("#target_price_editing").val();
      var cash_deferred           = $("#cash_deferred_editing").val();
      var no_price_established    = $("#no_price_established_editing").val();
      
          $.blockUI({ message: '<h1 style="margin: 10px; padding: 10px; font-size: 17px;">Please wait...</h1>' });
          $.ajax({
            type: "POST",
            url: root_dir+"/ajax/my_grain_edit_item_do_save.ajx.php",
            data: {
                id:id,
                commodity:commodity,
                grade:grade,
                quantity:quantity,
                harvest_year:harvest_year,
                target_price:target_price,
                cash_deferred:cash_deferred,
                no_price_established:no_price_established
                },
            success: function(msg){
              var arr = msg.split("{ecube:sep}");
              if(arr[0]=="succeed"){
                commodity_temp = commodity;
                grade_temp = grade;
                quantity_temp = quantity;
                harvest_year_temp = harvest_year;
                target_price_temp = '$' + target_price;
                if(cash_deferred==1){
                  cash_deferred_temp = 'Yes';
                } else {
                  cash_deferred_temp = 'No';
                }
                if(no_price_established==1){
                  no_price_established_temp = 'Yes';
                } else {
                  no_price_established_temp = 'No';
                }
              }
              $.unblockUI();
              $.blockUI({ message: '<h1 style="margin: 10px; padding: 10px; font-size: 17px;">' + arr[1] + '</h1>' });
              setTimeout($.unblockUI, 1500);
              my_grain_edit_item_in_list(id);
            }
          });
}

function validate_search_warehouse_form()
{

  var b3 = !document.searchfield_warehouse.port_lincoln_from.value.match(/^[0-9]+(\.[0-9]{0,2})?$/) ||
  !document.searchfield_warehouse.port_lincoln_to.value.match(/^[0-9]+(\.[0-9]{0,2})?$/);
  var b4 = !document.searchfield_warehouse.grade.value;

  if (b3 || b4)
  {
    var s = "Please correct the following.";

    if (b4)
    {
      s += "\n* Please enter grade.";
    }

    if (b3)
    {
      s += "\n* Please enter the price range.";
    }


    alert(s);

    return false;
  }

  return true;
}

function validate_search_on_farm__form()
{
  var b3 = !document.add_listing_form_on_farm.port_lincoln_from.value.match(/^[0-9]+(\.[0-9]{0,2})?$/) ||
  !document.add_listing_form_on_farm.port_lincoln_to.value.match(/^[0-9]+(\.[0-9]{0,2})?$/);
  var b4 = !document.searchfield_warehouse.grade.value;

  if (b3 || b4)
  {
    var s = "Please correct the following.";

    if (b4)
    {
      s += "\n* Please enter grade.";
    }

    if (b3)
    {
      s += "\n* Please enter the price range.";
    }

    alert(s);

    return false;
  }

  return true;
}

function validate_search_contracts_form()
{
  var b3 = !document.add_listing_form_contracts.port_lincoln_from.value.match(/^[0-9]+(\.[0-9]{0,2})?$/) ||
  !document.add_listing_form_contracts.port_lincoln_to.value.match(/^[0-9]+(\.[0-9]{0,2})?$/);
  /*var b7 = !document.add_listing_form_contracts.quantity.value.match(/^[1-9][0-9]*$/);*/
  var b4 = !document.searchfield_warehouse.grade.value;

  if (b3 || b4/*|| b7*/)
  {
    var s = "Please correct the following.";
    /*
		if (b7){
			s += "\n* Please enter quantity.";
		}
		*/

    if (b4)
    {
      s += "\n* Please enter grade.";
    }

    if (b3)
    {
      s += "\n* Please enter the price range.";
    }

    alert(s);

    return false;
  }

  return true;
}

function show_hide_contracts_grade(grade_type)
{
  if (grade_type == 'multi') {
    document.getElementById('contracts_grade').style.display = 'none';
  } else {
    document.getElementById('contracts_grade').style.display = '';
  }
}

function grain_price_charts_set_current_tab(tab)
{
  if (tab == "apw1")
  {
    document.getElementById("apw1_tab").className = "tab_front";
    document.getElementById("fb1_tab").className = "tab_back";
    document.getElementById("apw1").style.display = "";
    document.getElementById("fb1_barley").style.display = "none";
  }
  else if (tab == "fb1_barley")
  {
    document.getElementById("apw1_tab").className = "tab_back";
    document.getElementById("fb1_tab").className = "tab_front";
    document.getElementById("apw1").style.display = "none";
    document.getElementById("fb1_barley").style.display = "";
  }
}
//font-end function
function grain_prices_set_current_tab_front(tab)
{
  if (tab == "port_lincoln")
  {
    document.getElementById("tab1").className = "tab_front";
    document.getElementById("tab2").className = "tab_back";
    document.getElementById("tab3").className = "tab_back";
    document.getElementById("tab5").className = "tab_back";
    document.getElementById("tab4").className = "tab_back";
    document.getElementById("port_lincoln").style.display = "";
    document.getElementById("thevenard").style.display = "none";
    document.getElementById("contracts").style.display = "none";
    document.getElementById("contracts_thev").style.display = "none";
    document.getElementById("graph").style.display = "none";
  }
  else if (tab == "thevenard")
  {
    document.getElementById("tab1").className = "tab_back";
    document.getElementById("tab2").className = "tab_front";
    document.getElementById("tab3").className = "tab_back";
    document.getElementById("tab5").className = "tab_back";
    document.getElementById("tab4").className = "tab_back";
    document.getElementById("port_lincoln").style.display = "none";
    document.getElementById("thevenard").style.display = "";
    document.getElementById("contracts").style.display = "none";
    document.getElementById("contracts_thev").style.display = "none";
    document.getElementById("graph").style.display = "none";
  }
  else if (tab == "contracts")
  {
    document.getElementById("tab1").className = "tab_back";
    document.getElementById("tab2").className = "tab_back";
    document.getElementById("tab3").className = "tab_front";
    document.getElementById("tab5").className = "tab_back";
    document.getElementById("tab4").className = "tab_back";
    document.getElementById("port_lincoln").style.display = "none";
    document.getElementById("thevenard").style.display = "none";
    document.getElementById("contracts").style.display = "";
    document.getElementById("contracts_thev").style.display = "none";
    document.getElementById("graph").style.display = "none";
  }

  else if (tab == "contracts_thev")
  {
    document.getElementById("tab1").className = "tab_back";
    document.getElementById("tab2").className = "tab_back";
    document.getElementById("tab3").className = "tab_back";
    document.getElementById("tab5").className = "tab_front";
    document.getElementById("tab4").className = "tab_back";
    document.getElementById("port_lincoln").style.display = "none";
    document.getElementById("thevenard").style.display = "none";
    document.getElementById("contracts").style.display = "none";
    document.getElementById("contracts_thev").style.display = "";
    document.getElementById("graph").style.display = "none";
  }

  else if (tab == "graph")
  {
    document.getElementById("tab1").className = "tab_back";
    document.getElementById("tab2").className = "tab_back";
    document.getElementById("tab3").className = "tab_back";
    document.getElementById("tab5").className = "tab_back";
    document.getElementById("tab4").className = "tab_front";
    document.getElementById("port_lincoln").style.display = "none";
    document.getElementById("thevenard").style.display = "none";
    document.getElementById("contracts").style.display = "none";
    document.getElementById("contracts_thev").style.display = "none";
    document.getElementById("graph").style.display = "";
  }
}
//adminx function used
function grain_prices_set_current_tab(tab)

{

  if (tab == "port_lincoln")

  {

    document.getElementById("tab1").className = "tab_front";

    document.getElementById("tab2").className = "tab_back";

    document.getElementById("tab3").className = "tab_back";

    document.getElementById("tab4").className = "tab_back";

    document.getElementById("port_lincoln").style.display = "";

    document.getElementById("thevenard").style.display = "none";

    document.getElementById("contracts").style.display = "none";

    document.getElementById("contracts_thev").style.display = "none";

  }

  else if (tab == "thevenard")

  {

    document.getElementById("tab1").className = "tab_back";

    document.getElementById("tab2").className = "tab_front";

    document.getElementById("tab3").className = "tab_back";

    document.getElementById("tab4").className = "tab_back";

    document.getElementById("port_lincoln").style.display = "none";

    document.getElementById("thevenard").style.display = "";

    document.getElementById("contracts").style.display = "none";

    document.getElementById("contracts_thev").style.display = "none";

  }

  else if (tab == "contracts")

  {

    document.getElementById("tab1").className = "tab_back";

    document.getElementById("tab2").className = "tab_back";

    document.getElementById("tab3").className = "tab_front";

    document.getElementById("tab4").className = "tab_back";

    document.getElementById("port_lincoln").style.display = "none";

    document.getElementById("thevenard").style.display = "none";

    document.getElementById("contracts").style.display = "";

    document.getElementById("contracts_thev").style.display = "none";

  }

  else if (tab == "contracts_thev")

  {

    document.getElementById("tab1").className = "tab_back";

    document.getElementById("tab2").className = "tab_back";

    document.getElementById("tab3").className = "tab_back";

    document.getElementById("tab4").className = "tab_front";

    document.getElementById("port_lincoln").style.display = "none";

    document.getElementById("thevenard").style.display = "none";

    document.getElementById("contracts").style.display = "none";

    document.getElementById("contracts_thev").style.display = "";

  }

}


function numeric_only(e) {
  var unicode = e.charCode ? e.charCode : e.keyCode;
  if (unicode != 8 && unicode != 9 && unicode != 46) {
    if (unicode < 48 || unicode > 57)
      return false;
  }
  return true;
}

function silos_set_active(active) {
  if (active == 1) {
    document.getElementById("sealed_radio").disabled = false;
    document.getElementById("unsealed_radio").disabled = false;
    document.getElementById("silo_radio").disabled = false;
    document.getElementById("shed_radio").disabled = false;
    document.getElementById("sealed_radio").checked = true;
    document.getElementById("unsealed_radio").checked = false;
    document.getElementById("silo_radio").checked = false;
    document.getElementById("shed_radio").checked = false;
    silos_show_hide_checkboxes(1);
  } else {
    document.getElementById("sealed_radio").disabled = true;
    document.getElementById("unsealed_radio").disabled = true;
    document.getElementById("silo_radio").disabled = true;
    document.getElementById("shed_radio").disabled = true;
    document.getElementById("sealed_radio").checked = true;
    document.getElementById("unsealed_radio").checked = false;
    document.getElementById("silo_radio").checked = false;
    document.getElementById("shed_radio").checked = false;
    silos_show_hide_checkboxes(1);
  }
}

$(document).ready(function(){
  MM_preloadImages(
    '/images/exa-logo-h.gif'
    );
});