var cP = {
 addListeners: function(e) {
  $('#paypal select[name="os0"]').change(cP.changeColor);
  $('#paypal select[name="os1"]').change(cP.changePrice);
 },	
 changeColor: function() {	 
   $('#productimage img').attr('src', cP.cache[this.value].src);
 },	
 createCache: function() {
  $('option[value!=null]', '#paypal select[name="os0"]').each(function(index) {  								   
    cP.cache[this.value] = document.createElement('img');
    cP.cache[this.value].src = 'images/rollovers/' + this.value.toLowerCase().replace(/[^a-z0-9]/g, '_') + ".jpg";	
  });	
 },
 init: function() {
  cP.price = $('#paypal input[name="amount"]').val();
  cP.createCache();
  cP.addListeners();
 },
 changePrice: function() {	
   var $price = $('#price'),
       $amount = $('#paypal input[name="amount"]');
   if ('Adult 2X-Large' == this.value) {
    $price.text('$17.95');
	$amount.val('17.95');
   } else {
    $price.text('$' + cP.price);
	$amount.val(cP.price);	   
   }
 },	 
 cache: [],
 price: ''
};

var fV = {
 init: function() {
  $.each(validationSet, function(name, value) { 
    $('[name="' + name + '"]','#paypal').change(fV.checkValid);	
  });
  $('#paypal').submit(fV.checkValidSubmit); 
 },
 validateColor: function(s) { 
  return !(fV.isEmpty(s) || s == 'null'); 
 },
 validateSize: function(s) { 
  return !(fV.isEmpty(s) || s == 'null');
 },
 validateQuantity: function(s) { 
  return (!fV.isEmpty(s) && fV.isInteger(s) && s > 0);
 },	 
 checkValidSubmit: function() {
  var errText = [];
  $.each(validationSet, function(name, value) { 
    $('[name="' + name + '"]','#paypal').each(function() {					 
      var failedE = fV.handleValidity(this),
	      $errDisplay = $('#error_' + this.name);
      if (failedE && $errDisplay.length) {
       $errDisplay.text(validationSet[failedE.name]['error']);
      }	  
      if (!failedE && $errDisplay.length) {
       $errDisplay.text('');
      }	  
      if (failedE) {
       errText[errText.length] = validationSet[failedE.name]['error'];
      }
	});							 
  });
  if (errText.length > 0) {
   alert('Please fix the following and resubmit:\n' + errText.join('\n'));   
   return false;
  } 
  else {
   return true;
  }
 },
 checkValid: function() {
  var failedE = fV.handleValidity(this),
      $errDisplay = $('#error_' + this.name);
  if (failedE && $errDisplay.length) {
   $errDisplay.text(validationSet[this.name]['error']);
   this.focus();
  }
  else if (failedE && !$errDisplay.length) {
   alert(validationSet[failedE.name]['error']);
  }
  else if (!failedE && $errDisplay.length) {
   $errDisplay.text('');
  }
 },
 handleValidity: function(field) { 
  if(!field.value){
   return null;
  }
  if (!validationSet[field.name].isValid(field.value)) {
   return field;
  }
  else {
   return null;
  }
 },
 isDigit: function(c) {
  return ((c >= "0") && (c <= "9"))
 },
 isEmpty: function(s) {
  return ((s == null) || (s.length == 0))
 },
 isInteger: function(s) {
  if (fV.isEmpty(s)) {
   if (isInteger.arguments.length == 1) {
    return true;
   } 
   else {
    return (isInteger.arguments[1] == true);
   }
  }
  for (var i = 0; i < s.length; i++) {
   var c = s.charAt(i);
   if (!fV.isDigit(c)) {
	return false;
   }
  }
  return true;
 }
};

var validationSet = {
 'os0': {
  isValid: function(s) {
   return !(fV.isEmpty(s) || s == 'null');   
  },
  'error': 'Please select a color.'
 },
 'os1': {
  isValid: function(s) {
   return !(fV.isEmpty(s) || s == 'null');   
  },
  'error': 'Please select a size.'  
 },
 'quantity': {
  isValid: function(s) {
   return (!fV.isEmpty(s) && fV.isInteger(s) && s > 0);
  },
  'error': 'Please enter a quantity greater than zero.'   
 }
};

$(document).ready(function(){
 cP.init();
 fV.init();
});


