///////////////////
// Thanks to Kevin Yank @ Sitepoint.com for the externalLinks function.
// It's a quick way to make xhtml compliant external links (target=_blank)
// It won't work w/ Javascript off-but those with it off are used to that
// and will know to use the 'back' button.
// I've modified it to allow other 'rel' types (popup,close,print)


function confirmDelete(name) {
 var message1='';
 var message2='';
 message1='Are you sure you want to remove this item: (' + name + ') ?';
 message2='This action cannot be undone!!! Are you still sure?';
 if(confirm(message1)) {
   if(confirm(message2)) {
      return true;
   }
 }
 return false;
}

function cartConfirmCancel() {
 var message1='';
 var message2='';
 message1='Are you sure you want to cancel your order?';
 message2='This action cannot be undone!!! Are you still sure?';
 if(confirm(message1)) {
   if(confirm(message2)) {
      return true;
   }
 }
 return false;
}

function cartCheckQuantity(TheForm) {
 var qnum = TheForm.quantity.value-0;
 if( isNaN(qnum)) {
   alert("Please use a number for quantity. You used a "+typeof(TheForm.quantity.value));
   return false;
 }
 if(qnum < 1) {
   alert("The quantity entered was less than 1.\nIf you wish to remove the item from your cart, please use the delete/remove button");
   return false;
 }
 return true;
}

var popChild;

function myPopUpWin(t) {
 popChild = window.open(t.href,'',
 'height=600,width=700,directories=0,location=0,menubar=0,scrollbars=1,status=0,toolbar=0,resizable=1' );
 if(popChild) { setTimeout('popChild.focus()',500); }
 return false;
}

function myPrint() { window.print(); }
function myClose() { if(opener) { opener.location.reload(1);self.close(); } }
function myConfirm(t) {
   var item=t.getAttribute("name");
   if(confirmDelete(item)) { return true; }
   return false;
}

function showNHideForm(t) {
   if(!t) return false;
  // var goodboy = t.getAttribute("href").substring(1);
   var me = document.getElementById(goodboy); if(!me) return false;
   var s = goodboy.indexOf('_');
   var surname=goodboy.substring((s+1));
   var mom = document.getElementById(surname); if(!mom) return false;
   var kids = mom.getElementsByTagName("form");
   for(var c=0;c<kids.length;c++) {
  //    var given = kids[c].getAttribute("id");
      if(given && (given.indexOf(surname)>= 0)) {
         if(given == goodboy) { kids[c].style.display=(kids[c].style.display=="block" ? "none" : "block"); }
         else { kids[c].style.display="none"; }
      }
   }
   return true;
}

function fillNHide(t)
{
  var fn = t.value;
  if(fn=="deliver_s" && t.checked) {
  /*
    t.form.name_s.value='';
    t.form.address1_s.value='';
    t.form.address2_s.value='';
    t.form.city_s.value='';
    t.form.region_s.value='';
    t.form.region_s.selectedIndex=0;
    t.form.zip_s.value='';
    t.form.country_s.selectedIndex=0;
    */
  }
  else if(t.checked) {
    t.form.firstname_s.value=t.form.firstname_b.value;
    t.form.lastname_s.value=t.form.lastname_b.value;
    t.form.name_s.value=t.form.name_b.value;
    t.form.address1_s.value=t.form.address1_b.value;
    t.form.address2_s.value=t.form.address2_b.value;
    t.form.city_s.value=t.form.city_b.value;
    t.form.region_s.value=t.form.region_b.value;
    t.form.region_s.selectedIndex=t.form.region_b.selectedIndex;
    t.form.zip_s.value=t.form.zip_b.value;
    t.form.country_s.selectedIndex=t.form.country_b.selectedIndex;
  }
  var td=document.getElementById('deliver');
  var tp=document.getElementById('pickup');
  if(t.value.indexOf('pickup') > -1) {
    if(td) { td.style.display = 'none'; }
    if(tp) { tp.style.display = 'block'; }
  }
  if(t.value.indexOf('deliver')> -1) {
    if(td) { td.style.display = 'block'; }
    if(tp) { tp.style.display = 'none'; }
  }
}

var skipShip=0;
function valInput(rid)
{ var ifield,itype;
   // skip _b validation if pickup is shipping choice. php will autofill values.
   if((skipShip==1) && (rid.indexOf('_s')>=0)) { return false; }
   ifield=document.getElementById(rid);
   if(ifield)
   {
      if(ifield.parentNode.lastChild && /img/i.test(ifield.parentNode.lastChild.nodeName))
      { ifield.parentNode.removeChild(ifield.parentNode.lastChild); }
      else if(ifield.previousSibling && /img/i.test(ifield.previousSibling.nodeName))
      { ifield.parentNode.removeChild(ifield.previousSibling); }
      ifield.style.background='transparent';
      itype=ifield.type.toLowerCase();
      if(!itype) {
         //radio group. will break if you have more than 1 group per id
         var q = rid.indexOf('_radio');
         if(q > -1)
         {
            rid = rid.substr(0,q);
            var radios = ifield.getElementsByTagName('input');
            itype = 'radio';
         }
      }
      switch(itype)
      {
         case 'text':     if(ifield.value=='') {showError(ifield.id)} break;
         case 'textarea': if(ifield.value=='') {showError(ifield.id)} break;
         case 'checkbox': if(!ifield.checked)  {showError(ifield.id)} break;
         case 'select-one':
         case 'select-multiple':
            if(ifield.selectedIndex<1) {showError(ifield.id)} break;
         case 'radio':
            if(radios) {
               var ok = 'false';
               for(var c=0;c<radios.length;c++) { if(radios[c].checked) {ok=radios[c].value; break;} }
               if( ok=='false') { showError(ifield.id); }
               if(rid=='shippingchoice' && (ok=='pickup')) { skipShip=1; valInput('photolab_radio'); }
            }
            break;
      }
   }
   return true;
}

function checkForm(theform)
{ var req,elem,i;
  if(document.getElementById('required'))
  {
   req=document.getElementById('required').value.split(',');
   if(document.getElementById('invalidentry')){
    elem=document.getElementById('invalidentry');
    elem.parentNode.removeChild(elem);
   }
   for(i=0;i<req.length;i++) { valInput(req[i]); }
  }
  if(document.getElementById('invalidentry')) { return false; }
  return true;
}

function showError(id)
{ var se,icon,nli,na;
  icon=document.createElement('img');
  icon.src='images/alert.gif'; icon.alt='Required';
  icon.title='This field is required!';
  se=document.getElementById(id);
  if(se.type==false) { se.parentNode.insertBefore(icon,se); }
  else { se.parentNode.appendChild(icon); }
  se.style.background='#FDD';
  if(!document.getElementById('invalidentry')){
    var elem=document.createElement('p');
    elem.id='invalidentry';
    elem.style.border='2px solid #c00';
    elem.style.padding='5px';
    elem.style.textAlign='center';
    elem.appendChild(document.createTextNode('Please enter or change the fields marked with '))
    icon=document.createElement('img');
    icon.src='images/alert.gif';
    icon.alt='Error';
    icon.title='This field has an error!';
    elem.appendChild(icon);
    var sb = document.getElementById('cartsubmit');
    sb.parentNode.insertBefore(elem,sb);
  }
  if(((id=='email_b') || (id=='email_s')) && !isEmailAddr(document.getElementById(id).value)){
    var pn=document.createElement('p');
    pn.appendChild(document.createTextNode('Your email seems to be invalid')) 
    document.getElementById('invalidentry').appendChild(pn);
  }
}

function isEmailAddr(str) { return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/); }


function myEvents() {
 if(!document.getElementsByTagName) return;
 var i=0;
 var njsl= document.getElementById("njslinks");
 if(njsl) njsl.style.display="none";

 var ptags = document.getElementById("config");
 if(ptags) {
   var c=1;
   var tags= ptags.getElementsByTagName("fieldset");
   if(location.href.indexOf('#')>=0) { c = 0; var tg = location.href.substring( (location.href.indexOf('#')+1)); }
   for(c;c<tags.length;c++) { if((tags[c].id) && tags[c].id.indexOf('_config') && tags[c].id!=tg) {tags[c].style.display="none"; } }
 }

 ptags = document.getElementById("shippingchoice_radio");
 if(ptags)
 {
    tags= ptags.getElementsByTagName("input");
    for (i=0; i<tags.length; ++i) {
      var tagn = tags[i].getAttribute("name");
      if(!tagn) tagn="foo";
        switch(tagn) {
          case "shippingchoice": 
            tags[i].onclick=function(){ return fillNHide(this); };
            if(tags[i].checked)
            {
              var td=document.getElementById('deliver');
              var tp=document.getElementById('pickup');
              if(tags[i].value.indexOf('pickup') > -1) {
                if(td) { td.style.display = 'none'; }
                if(tp) { tp.style.display = 'block'; }
              }
              else if(tags[i].value.indexOf('deliver')> -1) {
                if(td) { td.style.display = 'block'; }
                if(tp) { tp.style.display = 'none'; }
              }
            }
            break;
        }
      }
   }
 var boxes = document.getElementsByTagName("textarea");
 for (i=0; i<boxes.length; i++) { if(boxes[i].className && (boxes[i].className.indexOf('cof')>=0)){ boxes[i].onfocus=function() { this.value='';this.onfocus=null;return false;}}}
 
 var anchors = document.getElementsByTagName("a");
 for (i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if(anchor.getAttribute("href"))
   {
     var rel = anchor.getAttribute("rel");
     switch(rel) {
       case "external": anchor.target = "_blank"; break;
       case "internal": anchor.onclick=function(){ return showNHideForm(this); }; break;
       case "popup": anchor.onclick =function(){ myPopUpWin(this); return false; }; break;
       case "print": anchor.onclick = myPrint; break;
       case "close": anchor.onclick = myClose; break;
       case "confirm": anchor.onclick=function(){ return myConfirm(this); }; break;
     }
   }
 }
}

function initPage() { myEvents(); }

window.onload = initPage;

