function go(form) {
   window.location = form.range.options[form.range.selectedIndex].value;

}

function trim() {
  // remove all spaces from aText text field 
  // and place the result into the right text field
  document.ship_form.gift_certificate.value = 
        document.ship_form.gift_certificate.value.replace(/ /g, "");
}

function trimWSemail() {
  // remove all spaces from aText text field 
  // and place the result into the right text field
  document.ws_register.email.value = 
        document.ws_register.email.value.replace(/ /g, "");
}
 
function preload_images () {
   var d = document;
	 if (!d.imgs) { d.imgs = new Array(); }
	 var j = d.imgs.length, args = preload_images.arguments, i;
	 for (i = 0; i < args.length; i++) {
		  d.imgs[j] = new Image;
			d.imgs[j].src = args[i];
			j++;
	 }
}


 function bookmark(title)
 {

   var urlAddress = location.href;
   var pageName = title;
   var browser = navigator.appName;

  if (browser == 'Microsoft Internet Explorer')
  {
     window.external.AddFavorite(urlAddress,pageName)
  }
  else if (browser == 'Netscape')
  { 
     alert("Your browser does not support this feature.  Use CTRL-D to bookmark this page");
  }
  else
  { 
   alert("Your browser does not support this feature.");
  }
 }



function textBuild() 
{
     document.quickfind.keyword.value = document.quickfind.elements[3].value + ' ' + document.quickfind.elements[4].value + ' ' + document.quickfind.elements[5].value + ' ' + document.quickfind.elements[6].value;
}


function checkCheckBoxforcheckout_shippingaddressTEMPLATE(f,ship_num){
  if (f.readShipping.checked == false) {
    alert('To complete your order, you must accept the Chukar Cherries Shipping Policy by selecting the checkbox above.');
    return false;
  } else {
    return concatGiftMessage(f,ship_num);
  }
}

function checkCheckBoxforcheckout_wholesale_order_form2TEMPLATE(f){
if (f.skipit != 1) {
if (f.readwholesale.checked == false )
{
alert('To complete your order, you must accept the Chukar Cherries Minimum Order Policy by selecting the checkbox above.');
return false;
}else
return true;
}else
return true;
}

function checkReviewTitle(f){
  if (f.ratingComment.value == '') {
      if (f.ratingTitle.value == 'Your Review Title') {
      alert('Please enter a Title for your review.');
      return false;
      } else {
      return true;
      }
  } else {
    return false;
  }
}

/* ***************************************
   * verifyRecipients() function.        *
   * Checks the checkout_shippingdetail  *
   * page to ensure that a shipping      *
   * recipient was chosen for each       *
   * product and that there weren't any  *
   * missed recipients.  If gives the    *
   * customer the option of ignoring     *
   * missed ship-tos since that doesn't  *
   * break the checkout it just causes   *
   * blank spaces in some spots.         *
   *                                     *
   * ~Michael - 20060111                 *
   *************************************** */

function verifyRecipients(theForm) {
  var intLength = theForm.length;            // Grab the total length of the form.
  var firstStep = 1, shipTos = new Array();  // Initialize a couple vars to be used later.
  for(var i=0;i<intLength;i++) {             // Loop through the form elements.
    if(firstStep && theForm.elements[i].name.substr(0,4) == 'recp') {
      // If it's the first dropdown setup the number of ship-tos it contains to compare later
      var numShipTos = +theForm.elements[i].options.length;
      // Turn this off so it doesn't repeat this step (it won't hurt anything but would waste processing power).
      firstStep = 0;
    }
    if(theForm.elements[i].name.substr(0,4) == 'recp' && theForm.elements[i].value == '') {
      // If we encounter a dropdown that hasn't had a ship-to selected..
      alert('You must choose a recipient for each of the products you are purchasing.');
      return false;
    } else if (theForm.elements[i].name.substr(0,4) == 'recp') {
      // Otherwise add the item to an array to compare later.
      var shipTo = theForm.elements[i].selectedIndex;
      if(!in_array(shipTo, shipTos)) {
        // If the ship-to isn't in the array already, add it.
        var stLen = shipTos.length;
        shipTos.push(shipTo);
      }
    }
  }
  if(shipTos.length != numShipTos) {
    // If the # of ship-tos in the array don't match the number of ship-tos that exist, prompt the user
    var verifyShipTos = confirm("Some of your shipping address do not have products assigned to them.\n\nDo you wish to continue anyway?");
    if(verifyShipTos) {
      // User says it's OK
      return true;
    } else {
      // User says it's not OK
      return false;
    }
  }
  // If we've gotten this far, everything on the form has checked out OK.
  return true;
}

/* ***************************************
   * in_array() function for JS.         *
   * Works the same as the PHP function. *
   *                                     *
   * ~Michael - 20060111                 *
   * ~Michael - Fixed undefined vars     *
   *            20060111                 *
   *************************************** */

function in_array(needle, haystack) {
  if(typeof needle == undefined) {
    // If needle is undefined:
    alert("Needle is undefined.\nError: in_array");
    return false;
  } else if(typeof haystack == undefined) {
    alert("Needle is undefined.\nError: in_array");
    return false;
  }
  for(i=0,n=haystack.length;i<n;i++) {
    // Loop through the array.
    if(haystack[i] == needle) {
      // If the needle was found:
      return true;
    }
  }
  // If the code reaches this point, needle wasn't found:
  return false;
}

//check for text in search box before submitting
function search_help() {
  //conditions for most searched for terms
  //to help aid navigation

  if(document.searchform.keyword.value.toLowerCase() == 'shipping' || document.searchform.keyword.value.toLowerCase() == 'tracking' || document.searchform.keyword.value.toLowerCase() == 'delivery' || document.searchform.keyword.value.toLowerCase() == 'international' || document.searchform.keyword.value.toLowerCase() == 'po box' || document.searchform.keyword.value.toLowerCase() == 'weather' || document.searchform.keyword.value.toLowerCase() == 'carrier' || document.searchform.keyword.value.toLowerCase() == 'carrier options' || document.searchform.keyword.value.toLowerCase() == 'shipping methods' || document.searchform.keyword.value.toLowerCase() == 'shipping options' || document.searchform.keyword.value.toLowerCase() == 'warm weather' || document.searchform.keyword.value.toLowerCase() == 'charges' || document.searchform.keyword.value.toLowerCase() == 'hi' || document.searchform.keyword.value.toLowerCase() == 'ak' || document.searchform.keyword.value.toLowerCase() == 'apo')
  {
     window.location = "http://www.chukar.com/faqchukar#about03";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'kosher')
  {
     window.location = "http://www.chukar.com/faqchukar#about04";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'dried fruit')
  {
     window.location = "http://www.chukar.com/ShowView/category/26";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'faq' || document.searchform.keyword.value.toLowerCase() == 'ingredients' || document.searchform.keyword.value.toLowerCase() == 'support' || document.searchform.keyword.value.toLowerCase() == 'help')
  {
     window.location = "http://www.chukar.com/faqchukar";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'about chukar')
  {
     window.location = "http://www.chukar.com/about_chukar";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'phone' || document.searchform.keyword.value.toLowerCase() == 'email' || document.searchform.keyword.value.toLowerCase() == 'contact')
  {
     window.location = "http://www.chukar.com/contact";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'jobs' || document.searchform.keyword.value.toLowerCase() == 'positions' || document.searchform.keyword.value.toLowerCase() == 'openings' || document.searchform.keyword.value.toLowerCase() == 'employment')
  {
     window.location = "http://www.chukar.com/positions";
     return false;
  }
  if(document.searchform.keyword.value.toLowerCase() == 'payment' || document.searchform.keyword.value.toLowerCase() == 'payment options' || document.searchform.keyword.value.toLowerCase() == 'payment method' || document.searchform.keyword.value.toLowerCase() == 'password' || document.searchform.keyword.value.toLowerCase() == 'message' || document.searchform.keyword.value.toLowerCase() == 'passwords' || document.searchform.keyword.value.toLowerCase() == 'payment methods' || document.searchform.keyword.value.toLowerCase() == 'payment option')
  {
     window.location = "http://www.chukar.com/faqchukar#about02";
     return false;
  }
}


function concatGiftMessage(theForm,sNum) {
  var numFields = 5; // Number of giftmessage fields.
  for (var i=1;i<=sNum;i++) {
    document.getElementById('gift_message'+i).value = '';
    for (var x=1;x<=numFields;x++) {
      if (x<5) {
        document.getElementById('gift_message'+i).value += document.getElementById('gm'+x+'_'+i).value + '|';
      } else {
        document.getElementById('gift_message'+i).value += document.getElementById('gm'+x+'_'+i).value;
      }
    }
  }
  document.forms[1].submit();
}


/**************************************************************
 * Generic toggle button
 * usage: test_id = id for testing (ex: select box)
 *        change_id = id for displaying (ex: div)
 *        change_val = toggle the display of the change_id depending on what value to set the test_id has when the test_id = change_val
 *        display_val = 'none' or 'block' depending on which should be used when test_id = change_val
 **************************************************************/
function toggleBlock(test_id,change_id,change_val,display_val) {
  if (document.getElementById(test_id).value == change_val) {
    var set_display = display_val;
  } else {
    var set_display = (display_val == 'block') ? 'none' : 'block';
  }
  document.getElementById(change_id).style.display=set_display;

  return false;
}


function cartChange(id,num,qty,total,name) {

 // Cart Change v1.4:
 // id = an id to represent the price and qty (so we know what qty box and price to update).
 // num = the original cost of the item(s) (if there were 3x a 10-dollar item, num would = 30.00).
 // qty = the original quantity that existed in the cart before modification
 // total = the original total of the cart items before modification.
 // 
 // --[ Revisions ]--
 // 20050620 - Original Script Creation ~Michael@ColorMaria
 // 20050629 - Added a line to change the color of the update cart message ~Michael@ColorMaria
 // 20050705 - Added code (accompanied by cookies) to check the quantities of items in the cart
 //          - to see whether they match what the user entered for better checking to see if 
 //          - the update cart button needs to be pressed.
 //          - 
 //          - Please note: This update requires the cookie functions above the priceChange
 //          - function. ~Michael@ColorMaria
 // 20050722 - Added error handling for NaN errors.
 // 20051215 - Fixed a bug with commas
 ///////////////////////////////////////////////////////////////////////////////////////////////
 // Setup our Variables:
  var num2 = 0;
  var qty2 = document.getElementById('qty_' + id).value;
  if(isNaN(qty2)) { // If qty2 isNaN that means someone's made a typo and hit a letter or other non-number key
    document.getElementById('qty_' + id).value = '';
    qty2 = '';
  }
  var num3 = document.getElementById('price_' + id).value.split("$");
  var num3 = num3[1].replace(',','');
  // Check to see if we're dividing by 0:
  if(qty != '0' || !qty) {
    // If not, get the real price (rPrice):
    var rPrice = num.replace(',','') / qty;
  } else {
    // If we are, set the total price for that item to 0:
    document.getElementById('price_' + id).value = '$0.00';
  }
  // Setup our new prices:
  num2 = (rPrice * qty2);
  document.getElementById('price_' + id).value = '$' + num2.toFixed(2);
  // We gotta do this differently depending on if the total we're modifying 
  // is the REAL total or if it's one that was previously modified.
  if(total == document.getElementById('total').value) {
    // If we are modifying the current REAL total, do it this way:
    // Figure out what total would be if the item we're modifying didn't exist.
    total = total - num3;
    // Add the new value to the total:
    total = total + num2;
    document.getElementById('total').value = '$' + total.toFixed(2);
  } else {
    // Setup our fake_total variable so we can essentially do the same 
    // thing we did with the real total
    var fake_total = document.getElementById('total').value.split("$");
    fake_total = fake_total[1].replace(',','');
    // Figure out what the total would be without this product.
    total = fake_total - num3;
    // Readd the new value to the total.
    total = total + num2;
    document.getElementById('total').value = '$' + total.toFixed(2);
  }
  // Just in case they think this will automagically update the real prices for them,
  // setup a fail safe the function below will read and evaluate:
  var nQty = getCookie(name);
  arQty = nQty.split('|');
  // Note: the last element of the array will be empty, so ignore it.
  cntQty = arQty.length - 1;
  for(i=0;i<cntQty;i++) {
    arID = arQty[i].split(','); // Hoo Hoo (owl)
    if(document.getElementById('qty_'+arID[0]).value == arID[1]) {
      // It equals the default quantity, yay! No need to update cart.
      document.getElementById('hasUpdated').value = '1';
      // Change the color of the update cart message.
      document.getElementById('update_msg').style.color = '#000';
      continue;
    } else {
      // It doesn't, they need to update.  No need to check further since if one hasn't been updated
      // the whole cart needs to be updated.
      document.getElementById('hasUpdated').value = '0';
      // Change the color of the update cart message so it stands out after a change to the qty is made.
      document.getElementById('update_msg').style.color = '#F00';
      // Break the loop.
      break;
    }
  }
}

function updateRecipient(pid) {
  document.getElementById('recipient_mapping'+pid).value = document.getElementById('recipient_mapping').value;
  document.getElementById('new_recipient_mapping'+pid).value = document.getElementById('new_recipient_mapping').value;
}

function suggestedSearchField(value,typ) {
  if (typ == 'out') {
    value.className = 'suggestion';
  } else if (typ == 'over') {
    value.className = 'suggestion_hover';
} else if (typ == 'click') {
    document.getElementById('search_input').value=value.innerHTML;
    document.getElementById('suggestions').innerHTML = '';
    document.getElementById('suggestions').className = '';
  }
}

var suggested_val = null;
function suggestedSearchSniff(e,typ,theclass,func,params) {
  if (e.keyCode == 40) {
    // nothing is selected yet, so let's get it started
    if (suggested_val == null) {
      suggested_val = 0;
    // clear out the current selection and move to next
    } else {
      document.getElementById('suggestion_' + suggested_val).className = 'suggestion';
      suggested_val++;
    }
    // highlight next selection if it exists 
    if (document.getElementById('suggestion_' + suggested_val) != undefined) {
      fld = document.getElementById('suggestion_' + suggested_val);
      document.getElementById('search_input').value=fld.innerHTML;
      fld.className = 'suggestion_hover';
    // if it doesn't exist, highlight none
    } else {
      suggested_val = null;
    }
  } else if (e.keyCode == 38) {
    // don't allow up arrow to be used until in list
    if (suggested_val != null && suggested_val != 0) {
      // undo current selection
      document.getElementById('suggestion_' + suggested_val).className = 'suggestion';
      suggested_val--;
      if (document.getElementById('suggestion_' + suggested_val) != undefined) {
        fld = document.getElementById('suggestion_' + suggested_val);
        document.getElementById('search_input').value=fld.innerHTML;
        fld.className = 'suggestion_hover';
      } else if (document.getElementById('suggestion_0') != undefined) {
        fld = document.getElementById('suggestion_0');
        document.getElementById('search_input').value=fld.innerHTML;
        fld.className = 'suggestion_hover';
        suggested_val = 0;
      } else {
        suggested_val = null;
      }
    // clear all highlighting if at the top of list
    } else if (suggested_val == 0) {
      document.getElementById('suggestion_0').className = 'suggestion';
      suggested_val = null;
    }
  } else {
    suggested_val = null;
    getInfo(typ,theclass,func,params);
  }
}

/**************************************************************
*EMAIL FORM STUFF
**************************************************************/
if (questions==null) {
  var questions = new Array(4);

          questions[0] = new Array(2, false, 6, 'First Name');
 
          questions[1] = new Array(3, false, 6, 'Last Name');
 
          questions[2] = new Array(1, true, 6, 'E-Mail');
 
          questions[3] = new Array(4, false, 2, 'E-Mail Preferences');
 
}
  function validate(){
    var count = 0;
    while(true){
      if(eval("document.questions.q_"+count) != null){
        if(eval("document.questions.answer_"+eval("document.questions.q_"+count+".value")) != null){
          var question = eval("document.questions.q_"+count);
          var answer = eval("document.questions.answer_"+eval("document.questions.q_"+count+".value"));
          // Geting question data from Array
          for(var i=0 ; i<questions.length ; i++){
            if(questions[i][0] == question.value){
              if(questions[i][1]){
                if(questions[i][2] == 3 ||
                  questions[i][2] == 2){
                  checked = false;
                  for(var j=0 ; j<answer.length ; j++)
                    if(answer[j].checked)
                      checked = true;
                  if(!checked){
                    alert(questions[i][3] + ' is required!');
                    return false;
                  }
                }else{
                  if(answer.value == ''){
                    alert(questions[i][3] + ' is required!');
                    answer.focus();
                    return false;
                  }
                }
              }
            }
          }
        }
      }else{
        break;
      }
      count++;
    }
    return true;
  }

function imposeMaxLength(obj, mlength){
  if (obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}
//END EMAIL FORM STUFF

function priceChange(id,oper,num) {
 // Price Change v2:
 // id = the id of the product (so we know what qty box and price to update).
 // oper = what operation to use: dynamic:add:sub:dropdown
 // num = the original price of the product
 //
 // --[ Revisions ]--
 // 20101005 - Rewrote the priceChange function.
 /////////////////////////////////////////////////////////////////////////////////////////////
  if (num == '') {
    num = document.getElementById('hidden_price_' + id).value.replace('$','');
  }
 
  // initialize num2 in case it's used
  var num2 = 0;

  // grab our price box and qty box:
  var price = (typeof document.getElementById == 'undefined') ? document.all('price_'+id) : document.getElementById('price_'+id);
  var qty = (typeof document.getElementById == 'undefined') ? document.all('qty_'+id) : document.getElementById('qty_'+id);

  // adjust the qty box if they're using the + or - buttons:
  if (oper == 'add') {
    ++qty.value;
  } else if (oper == 'sub' && qty.value >= '1') {
    --qty.value;
  } else if (qty.value < 1 || isNaN(qty.value)) {
    qty.value = '';
  }

  // reset the price if the qty goes below 1, is blank, or isNaN:
  if (qty.value < 1 || qty.value == '' || isNaN(qty.value)) {
    num = parseFloat(num);
    price.value = '$' + num.toFixed(2);
  } else {
    // everything is OK, get the new price:
    price.value = '$' + ((num.replace(',','') * qty.value)).toFixed(2);
  }
}

function hideShowCalcStateZip(recip,country) {
 if (country == 'United States') {
   document.getElementById('state'+recip).selectedIndex = 0;
   document.getElementById('zip'+recip).value = '';
   document.getElementById('staterow'+recip).style.display = '';
   document.getElementById('ziprow'+recip).style.display = '';
 } else {
   document.getElementById('staterow'+recip).style.display = 'none';
   document.getElementById('ziprow'+recip).style.display = 'none';
 }
}
