var infoShip = {
  check : function () {
    if ($("#ship_same_bill").attr('checked') == true) {
      $.each(infoShip.s, function (i, selector) {
        $.each(infoShip.r, function (k, entry) {
          var input_obj = $("#"+selector+"_"+entry);
          if (selector == 'billing') {
            input_obj.addClass('dimmed');
            input_obj.attr("disabled","disabled");
          } else {
            input_obj.attr('link_to', '#billing_'+entry);
            input_obj.keyup(function () { $($(this).attr('link_to')).val($(this).val());});
            input_obj.change(function () { $($(this).attr('link_to')).val($(this).val());});
      			input_obj.click(function () { $($(this).attr('link_to')).val($(this).val());});
          }
        });
      });
    } else {
      $.each(infoShip.s, function (i, selector) {
        $.each(infoShip.r, function (k, entry) {
          var input_obj = $("#"+selector+"_"+entry);
          input_obj.removeClass('dimmed');
          input_obj.change(false);
    			input_obj.click(false);
    			input_obj.keyup(false);
          input_obj.removeAttr("disabled");
        });
      });
    }
  },

  r : ["firstname", "lastname", "company", "street_address", "country", "state_tb", "state_38", "state_223", "suburb", "city", "postcode"],
  s : ["shipping", "billing"]
}

function checkform(form) {
	if (checkselect(form.subject, 'Please select what you are interested in.') == false) {return false;}
	if (checkvalue(form.name, 'Please enter your name.') == false) {return false;}
	if (checkemailaddress(form.email, 'Please enter your email address in the correct format (e.g. john.doe@your-isp.com).') == false) {return false;}
	if(checkvalue(form.enquiry, 'Please enter a question or comment.') == false) {return false;}
	return true;
}

function checkvalue(formfield, message) {
	if (formfield.value == "") {
		alert (message);
		formfield.focus();
		return false;
	}
	return true;
}

function checkselect(formfield, message) {
	// Checks if return option value is not defined or is "na"
	if (formfield.options[formfield.selectedIndex].value == "" || formfield.options[formfield.selectedIndex].value == "0") {
		alert (message);
		formfield.focus();
		return false;
	}
	return true;
}


function checkemailaddress(formfield, message) {
	var str = formfield.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (str == "") {
		alert(message);
		formfield.focus();
		return false;
	} else if (!str.match(re)) {
		alert(message);
		formfield.focus();
		return false;
	} else {
		return true;
	}
}

$(function () {
  infoShip.check();
  
  $("#ship_same_bill").change(infoShip.check);

  $('form#add_product_form').submit(function () {
    $('select').val()
    
    var errors = true;
    $('form#add_product_form input.quantity').each(function () {
      if ($(this).val() < 1) {
        alert('Please enter a valid quantity.')        
      }
    });
    $('form#add_product_form select').each(function () {
      if ($(this).val() == -1) {
        alert('Please select a ' + $(this).parent().parent().children('td.option').text() +'.');
        errors = false;
      }
    });
    
    return errors;
  });
  
  if ($.browser.msie) {
    $("#main_left").width($("#main_photo img").width() + 25);
  }
  
  setTimeout(function () {
    $('.cat_links tbody tr').each(function () {
      var largest = 0;
      $(this).find('td').each(function () {
        if ($(this).height() > largest) 
          largest = $(this).height();
      });
      $(this).find('td').each(function () {
        $(this).height(largest).css({'vertical-align': 'bottom','text-align': 'center', width: '33%'});
      })
    });
  }, 100);

  var keep = { 'alpacas': false, 'gifts' : false, 'farm' : false, 'pets' : false};
  $.each(keep, function (i, j) {
    $("ul#navigation li a."+i).mouseover(function(e) {
      eval("keep."+i+" = true;");
      $("#navigation li."+i+" div").css({
        'width' : 125,
        'top': $("ul#navigation li a."+i).offset().top - 12,
        'left': $("ul#navigation li a."+i).position().left
      }).slideDown("fast", function () {
        if (eval('keep.'+i+' == false')) $("#navigation li."+i+" div").fadeOut('normal');
      });
    });
    $("ul#navigation li a."+i).mouseout(function(e){
      if (e.relatedTarget != this) {
        eval("keep."+i+" = false;");
        setTimeout(function () {
          if (eval('keep.'+i+' == false')) $("#navigation li."+i+" div").fadeOut('normal');
        }, 100);
      };
    });
    $("#navigation li."+i+" div").mouseover(function () {
      eval("keep."+i+" = true;");
    });
    $("#navigation li."+i+" div").mouseout(function () {
      eval("keep."+i+" = false;");
      setTimeout(function () {
        if (eval('keep.'+i+' == false')) $("#navigation li."+i+" div").fadeOut('normal');
      }, 100);
    });
  });
});