/* New document created: 7.0.2007 20:13:42 */

function notifyme_validate()
{
  var calendarDate = new Date();
  calendarDate.month= calendarDate.getMonth()+1;
  calendarDate.year= calendarDate.getYear();
  calendarDate.day= calendarDate.getDate();
  /*
  var due=document.getElementById('due_date').value;
  var dueday=due.substr(due.indexOf(' ')+1,2);
  var duemonth=due.substr(0,due.indexOf(' '));
  if(dueday.indexOf('0')==0){dueday=dueday.substr(1);}
  var dueyear=due.substr(due.lastIndexOf(' '));
  switch(duemonth)
  {
    case 'January': duemonth=1; break;
    case 'February': duemonth=2; break;
    case 'March': duemonth=3; break;
    case 'April': duemonth=4; break;
    case 'May': duemonth=5; break;
    case 'June': duemonth=6; break;
    case 'July': duemonth=7; break;
    case 'August': duemonth=8; break;
    case 'September': duemonth=9; break;
    case 'October': duemonth=10; break;
    case 'November': duemonth=11; break;
    case 'December': duemonth=12; break;
  }
  */

  var duemonth = document.getElementById('notifyme_duemonth').value;
  var dueday = document.getElementById('notifyme_dueday').value;
  var dueyear = document.getElementById('notifyme_dueyear').value;

  if(!document.getElementById('notify_email').value)
  {
    alert('In order to contact you, we will need a valid email address');
  }
  else if(notifyme_isnotvalidemail(document.getElementById('notify_email').value))
  {
    alert('Your email address does not contain the necessary character @ or .');
  }
  else
  {
    if ((((dueyear<calendarDate.year) || (duemonth<calendarDate.month&&dueyear==calendarDate.year) || (dueday<calendarDate.day&&dueyear==calendarDate.year&&duemonth==calendarDate.month)) && (document.getElementById('due_date').value)) && (document.getElementById('whenever').checked==false))
    {
      alert('Please select a date later than today\'s date');
    }
    else
    {
      return true;
    }
  }
  return false;
}

function notifyme_isnotvalidemail(email)
{
  // use ~^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$~i instead
  if((email.indexOf('@')>-1)&&(email.indexOf('.')>-1))
  {
    return 0;
  }
  else { return 1; }
}

function select_stockchoice(event, arg1)
{
  var targ = get_event_target(event);
  //debug_out('select_stockchoice(). targ.id='+targ.id);

  // make sure the radio-button is the click target.
  // otherwise, exit and wait for this event to be called again.
  if ("input" != targ.tagName.toLowerCase()) { return true; }
  if (!targ.id.match(/^prodoosdo_/)) { return true; }

  //debug_out('select_stockchoice() filtered. targ.id='+targ.id);

  // make sure the radio button is selected.
  targ.checked = true;

  // determine id of radio button's label. don't assume the html is always direct parent child relationship between these two. and don't assume that we should know it from the code above that works around some odd event bubbling in ie6.
  var label = document.getElementById(targ.id.replace(/^prodoosdo_/, 'prodoosdo_label_'));
  //debug_out('selected label: '+label.id);

  // visually depress label.
  label.className += ' depressed'; //add new class without removing old class

  // raise all the labels for the unselected radio options.
  raise_unselected_radios(targ.form.prodoosdo);

  // set focus on the addtocart button.
  document.getElementById('addtocart_button_stockstatus').focus();

  // get out clean and let the event bubble.
  return true;
}

function label_onmouseout(event, arg1)
{
  var targ = get_event_target(event);
  //debug_out('onmouseout target: id='+targ.id + ', tagname='+targ.tagName + ', classname='+targ.className);

  // a label element is the only legitimate target at this point,
  // so get out if we have not found it yet.
  if ("label" != targ.tagName.toLowerCase()) { return true; }
  if (!targ.id.match(/^(opt|prodoosdo)_label_/)) { return true; }

  // set it back to the original class name if the label is not depressed.
  // medication is not an option.
  if (!/depressed/.test(targ.className)) {
	targ.className = targ.className.replace(' hovering', ''); //get rid of new class without killing old class
  }

}
function label_onmouseover(event, arg1)
{
  //debug_out('select_option2() here. event='+event+', arg1='+arg1);
  var targ = get_event_target(event);
  //debug_out('event target: id='+targ.id + ', tagname='+targ.tagName);

  // see if we are hovering over a label or any element inside the label.
  // right now, because html is so simple, only search up 1 level in dom.
  // this is a future source of buggers though. it's pretty simple I
  // suppose, but I also suppose I want to fix a few other things first.
  if (("label" != targ.tagName.toLowerCase()) && ("label" == targ.parentNode.tagName.toLowerCase()))
  {
    targ = targ.parentNode;
  }

  // a label element is the only legitimate target at this point,
  // so get out if we have not found it yet.
  if ("label" != targ.tagName.toLowerCase()) { return true; }
  if (!targ.id.match(/^(opt|prodoosdo)_label_/)) { return true; }

  // make sure all the other labels are unhighlighted.
  // i was bothered by flickering effects when unhighlighting all
  // labels and then (re)highlighting the one we like, so simplifying
  // this part of the concoction may be not good.
  var p = targ.parentNode.childNodes;
  for (var i=0; i<p.length; i++) {
    var l = p[i];
    if ((1 == l.nodeType) && ('label' == l.tagName.toLowerCase())) {
      //if ("depressed" != l.className) l.className = "raised";
	  if (!/depressed/.test(l.className)) {
		l.className = l.className.replace(' hovering', ''); //get rid of new class without killing old class
	  }
    }
  }


  // we now have the label being hovered in the targ var.
  // only do the hover accent if this label is not depressed.
  //if ("depressed" != targ.className) targ.className = "hovering";
  if (!/depressed/.test(targ.className)) {
	targ.className += ' hovering';
  }


  // hovering over a label...
  //debug_out('hovered label. id='+targ.id);
}
function select_prodopt(event, arg1)
{
  //alert('select_option2() here. event='+event+', arg1='+arg1);
  var targ = get_event_target(event);

  // make sure the radio-button is the click target.
  // otherwise, exit and wait for this event to be called again.
  if ("input" != targ.tagName.toLowerCase()) { return true; }
  if (!targ.id.match(/^opt_/)) { return true; }

  //debug_out('event target: id='+targ.id + ', tagname='+targ.tagName);
  //debug_out('parentNode.id='+targ.parentNode.id);

  // make sure the radio button is selected.
  targ.checked = true;

  // determine id of radio button's label. don't assume the html is always direct parent child relationship between these two. and don't assume that we should know it from the code above that works around some odd event bubbling in ie6.
  var label = document.getElementById(targ.id.replace(/^opt_/, 'opt_label_'));
  //debug_out('selected label: '+label.id);

  // visually depress label.
  label.className += ' depressed'; //add new class without removing old class

  // targ.value format: Color|BLK|Black|||instock
  var opt_vals = targ.value.split('|');
  var opttype = opt_vals[0];
  var skusuffix = opt_vals[1];
  var optname= opt_vals[2];
  var q1= opt_vals[3];
  var q2= opt_vals[4];
  var stockstatus = opt_vals[5];

  // raise all the labels for the unselected radio options.
  raise_unselected_radios(targ.form['opt_'+opttype]);

  /*
  // adjust the displayed info.
  var optname_display = (('No Options' == optname) ? document.getElementById('actual_sku').innerHTML : optname.replace(/ /g, '&nbsp;'));
  document.getElementById('selected_optname').innerHTML = optname_display;
  document.getElementById('selected_stocksummary').innerHTML = get_stockstatus_longtext(stockstatus, opttype);
  document.getElementById('selected_opt').style.display = 'block';
  document.getElementById('selected_stockstatus').style.display = 'block';
  document.getElementById('selected_price').style.display = ("outofstock" != stockstatus)?'block':'none';
  document.getElementById('addtocart_button').style.display = ("outofstock" != stockstatus)?'block':'none';
  document.getElementById('selected_stockchoices').style.display = ("outofstock" == stockstatus)?'block':'none';
  */

  // adjust the displayed info.
  var optname_display = (('No Options' == optname) ? document.getElementById('actual_sku').innerHTML : optname.replace(/ /g, '&nbsp;'));
  document.getElementById('selected_optname').innerHTML = optname_display;
  document.getElementById('selected_optname2').innerHTML = optname_display;
  document.getElementById('selected_opt').style.display = 'block';
  document.getElementById('selected_stocksummary').style.display = ("outofstock" != stockstatus)?'block':'none';
  document.getElementById('prod_button_container').style.display = ("outofstock" != stockstatus)?'block':'none';
  document.getElementById('prodoosdo_button_container').style.display = ("outofstock" == stockstatus)?'block':'none';
  document.getElementById('notify_me_container').style.display = ("outofstock" == stockstatus)?'block':'none';
  document.getElementById('coming_soon').style.display = ("outofstock" == stockstatus)?'block':'none';
  var optname_safe = optname.replace(/[^a-zA-Z0-9_]/g, ''); //alert(optname_safe);
  document.getElementById('notifyme_opt_'+optname_safe).selected = true;

  // set focus on the addtocart button.
  document.getElementById(("outofstock" == stockstatus)?'prodoosdo_addtocart_button':'addtocart_button').focus();

  // get out clean and let the event bubble.
  return true;
}

function get_stockstatus_longtext(stockstatus, opttype)
{
  var a = new Array(2);
  a['instock'] = '<font color="green">In stock and ready to ship!</font>';
  a['outofstock'] = '<font color="red">This '+(opttype?opttype.toLowerCase():'item')+' is out of stock.</font>';
  return a[stockstatus];
}

function get_event_target(e)
{
  //debug_out('get_event_target(). e.nodeType='+e.nodeType);
  // first, see if e is actually an element rather than an event.
  if (1 == e.nodeType)
  {
    //debug_out('get_event_target() rcvd element instead of event. e.nodeType='+e.nodeType);
    return e;
  }

  // if e doesn't refer to an event now, get it from the window object.
  if (!e) var e = window.event;

  // get and return ref to element that fired the event.
  e = e.target || e.srcElement;
  return e;
}

function raise_unselected_radios(radios)
{
  if (!radios || "undefined" == radios) return false;
  //debug_out('raise_unselected_radios(). radios.length='+radios.length);
  var label;
  for (i=0;i<radios.length;i++) {
    if (!radios[i].checked) {
      //debug_out('unselected: '+radios[i].className);
      label = document.getElementById(radios[i].id.replace(/^([a-z]+)_/, "$1"+'_label_'));
      //debug_out('unselected label: label.id='+label.id);
	//label.className = 'raised';
	  label.className = label.className.replace(' depressed', ''); //get rid of new class without killing old class
    }
  }
}

function addEvent(obj, type, fn) {
  if (obj && ("undefined" != obj)) {
    if (obj.attachEvent) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function()
        { obj['e'+type+fn](window.event); }
      obj.attachEvent('on'+type, obj[type+fn]);
    } else {
      obj.addEventListener(type, fn, false);
    }
  }
}

function create_handler(func, arg1){
  return function(event){
    return func.call(null, event || window.event, arg1);
  }
}

function debug_out(html)
{
  return;
  document.getElementById('debug_output').innerHTML = html+'<br>' + document.getElementById('debug_output').innerHTML;
}

function find_selected_radio(radios)
{
  if (1 == radios.nodeType) {
    // only one option is available. see if it is selected.
    return radios.checked;
  } else {
    var radio = false;
    for (i=0;i<radios.length;i++) {
      if (radios[i].checked) {
        radio = radios[i].value;
      }
    }
    return radio;
  }
}
function verify_prodopt_selected(form, opttype)
{
  // get/clarify a reference to the form.
  var form = get_event_target(form);

  // make sure a prod_opt is selected.
  var radios = form['opt_'+opttype];
  var selected_radio = find_selected_radio(radios);
  var opt_is_selected = (selected_radio?true:false);


  if (!opt_is_selected)
  {
    alert('Please Select a ' + opttype);
    radios[0].focus();
  }

  return opt_is_selected;
}

