var IE4 = (document.all && !document.getElementById) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var IE6 = (document.all && document.getElementById) ? true : false;
var IE7 = (document.all && document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
var MAC = (window.navigator.userAgent.indexOf("Mac") >= 0) ? true : false;
 
var Opened = "";

// -------------------------------------------------------------------
function GoTo(thisURL) {
     //alert(thisURL);
     location = thisURL;
}
// -------------------------------------------------------------------
function OpenWindow(thisURL,x,y) {
  //   var x = 300;
  //   var y = 300;

     var gWin = window.open(thisURL,'GraphicWindow',
     'width='+500+',height='+500+',resizable=0,scrollbars=0');
}


function popup(img,h,w,title){

    var sWidth = screen.width / 2 - w;

     var win = window.open("","graphics","top=100,left=" + sWidth + ",scrollbars=0,resizable=1");
     win.resizeTo(w,h + 100);
     win.document.open("text/html", "replace");
     win.document.write("<html><head><title>"+title+"</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>");
     win.document.write("<img src="+img+" width="+w+" height="+h+"><br>");
     win.document.write("<form><div align=center><input type=button name=Close value=\"Close Window\" onclick=window.close()></div></form>");
     win.document.write("</body></html>");
     win.status = "Zoom Image of " + title;
     win.focus();
}


// -------------------------------------------------------------------
function qtyCheck(form){

     var qtySelected = parseInt(0);

     for(i = 0; i < form.elements.length; i++){

          var fieldName = form.elements[i].name;

          if(fieldName.substring(0,8) == "QUANTITY"){
               var fieldValue = parseInt(form.elements[i].value);

               if(fieldValue > 0){
                    qtySelected += parseInt(fieldValue);

                        // Check availability

                    var id = fieldName.substring(9,fieldName.length);
                    var availFld = "available|" + id;
                    var minField = "minimum|" + id;

                    var nameFld = "name|" + id;
                    var productName = form.elements[nameFld].value

                    var pattern = /\s{2,}/g;
                    var pName = productName.replace(pattern,"");


                    if(eval(form.elements[availFld])){
                        var qtyAvailable = parseInt(form.elements[availFld].value);
                        if(fieldValue > qtyAvailable){
                            if(qtyAvailable <= 0){
                                alert("Sorry, the '" + pName + "' is not available at this time.");
                            }
                            else{
                                alert("There are only " + qtyAvailable + " of the '" + pName + "s' available.\n\n" +
                                      "Please reduce the quantity so it is equal to or fewer than " + qtyAvailable + " item(s).");
                            }
                            form.elements[i].value = "";
                            form.elements[i].focus();
                            return false;
                        }
                    }
                    if(eval(form.elements[minField])){
                        var qtyMinimum = parseInt(form.elements[minField].value);
                        if(fieldValue < qtyMinimum){
                            alert("You must purchase a minimum of " + qtyMinimum + ".");
                            form.elements[i].value = "";
                            form.elements[i].focus();
                            return false;
                        }
                    }
               }
          }
     }

     if(qtySelected == 0){
          alert("You have not entered a quantity of an item to add to the cart.");
          return false;
     }

return true;
}

// -------------------------------------------------------------------
function stripChars(form,fld) {

     // Strips the dashes, spaces, etc
     // from the credit card number

     var val = form.elements[fld].value;

     val = "" + val;
     if (!val)
          return "";
     var result = "";
     for (i=0; i < val.length; i++) {
          character = val.charAt(i);
          if ("0123456789".indexOf(character) != -1)
          result += character;
     }

     form.elements[fld].value = result;
}

// ------------------------------------------------------------------
function checkRegFields(frm){

    // Set the list of required registration fields
    var requiredFields = new Array("fst_nam:First Name",
                                   "lst_nam:Last Name",
                                   "adrs_1:Address Line 1",
                                   "city:City",
                                   "state:State",
                                   "zip_cod:Postal Code",
                                   "country:Country",
                                   "phone_no_1:Phone Number",
                                   "email_adrs:Email Address",
                                   "password:Password",
                                   "confirm_password:Confirm Password"
                                  );

    // Loop through the form fields and
    // test each of the required fields
    for(j = 0; j < requiredFields.length; j++){
        var elmnt = requiredFields[j].split(":");
        var requiredField = frm.elements[elmnt[0]].value;
        if(requiredField == ""){
            var label = elmnt[1];
            alert("You forgot to fill in the \"" + label + "\" field.\n" +
                  "This field is required before processing your order.");
            frm.elements[elmnt[0]].focus();
            return false;
        }
    }

    // Make sure the password is not blank
    if(frm.elements['password'].value == ""){
        alert('You must enter a password.');
        frm.elements['password'].focus();
        return false;
    }

    // Make sure that the password and confirmation matches
    if(frm.elements['password'].value != frm.elements['confirm_password'].value){
        alert('The confirmation password does not match the password entered.');
        frm.elements['password'].focus();
        return false;
    }

    // Make sure that a valid e-mail address was entered
    //var str = frm.email_adrs.value;
    //var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

    //if(!filter.test(str)){
    //    alert('The email address you entered is invalid.');
    //    frm.email_adrs.focus();
    //    return false;
    //}

    var str = frm.email_adrs.value;
    var atpos = str.indexOf("@");
    var dotpos = str.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=str.length){
       alert('The email address you entered is invalid.');
       return false;
    }

    return true;
}

// -------------------------------------------------------------------
function strip(val) {

     // Strips the dashes, spaces, etc
     // from the credit card number

	 val = "" + val;
     if (!val)
          return "";
     var result = "";
     for (i=0; i < val.length; i++) {
          character = val.charAt(i);
          if ("0123456789".indexOf(character) != -1)
          result += character;
     }
return result;
}


$(document).ready(function () {
  var menuitems = $('#categoryMenu div.catItem');
  for (x = 0;menuitems.length > x;x++) {
    menuid = $(menuitems[x]).children("div").attr('id');
    $(menuitems[x]).mouseenter(function (e) {
      menuid = $(this).children("div").attr('id')
      for (x = 0;menuitems.length > x;x++) {
        closeThis = $(menuitems[x]).children("div").attr('id');
        if (closeThis != menuid) closeIt(Opened);
      }
      Opened = showIt(e,menuid);
    }).mouseleave(function (e) {
      menuid = $(this).children("div").attr('id');
      subs = $(e.relatedTarget).attr('class');
      if (Opened == menuid&&subs != 'offset1'&&subs != 'subListItem') closeIt(Opened);
    })
  }
})

// --------------------------------------------------------------
function showIt(e,whichEl){
  var openEl = whichEl;
  var imageID = "img." + whichEl;

  if(document.all){
    whichEl = document.all[whichEl];
    imageEl = document.all[imageID];
  }
  else{
    whichEl = document.getElementById(whichEl);
    imageEl = document.getElementById(imageID);
  }
  if (!whichEl) return;

  whichEl.style.position = 'absolute';
  whichEl.style.top = $(e.target).offset().top+'px';
  whichEl.style.left = $(e.target).offset().left+150+'px';
  whichEl.style.visibility = '';

  return openEl;
}
 
 // --------------------------------------------------------------
function closeIt(openEl){

  var imageID = "img." + openEl;

  if(document.all){
    openEl = document.all[openEl];
    imageEl = document.all[imageID];
  }
  else {
    openEl = document.getElementById(openEl);
    imageEl = document.getElementById(imageID);
  }
  if (!openEl) return;     

  openEl.style.visibility = 'hidden';

}

