/* The VehicleTypeManager class controls tab selection the "Place an Ad" page, and updates a hidden variable on the form */

var VehicleTypeManager = Class.create();

VehicleTypeManager.prototype = {
	initialize : function(type, types, makes, engines) {
		this.currenttype = "";
		this.types = types;
		this.makes = makes;
		this.engines = engines;
		if (arguments[4]) { this.make_id = arguments[4]; }
		if (arguments[5]) { this.engine_id = arguments[5]; }
		this.select(type, types, makes, engines);
	},
	
	select : function(type) {
		if (this.currenttype != "") {
			$(this.currenttype).removeClassName('selected');
		}
		
		if (type == "car") {
			Element.show('options');
		} else {
			Element.hide('options');
		}
		
		if (type == "other" || type == "vintage" || type == "golfcart") {
			Element.show('serialnumber');
		} else {
			Element.hide('serialnumber');
		}
		
		if (type == "other") {
			Element.hide('make_menu');
			Element.hide('engine_menu');
		} else {
			Element.show('make_menu');
			Element.show('engine_menu');
		}
		
		if (type == "golfcart") {
			$('vehicle_mileage').value = '';
			Element.hide('mileage_field');
		} else {
			Element.show('mileage_field');
		}
		
		$(type).addClassName('selected');
		this.currenttype = type;
		
		$('vehicle_type_id').value = this.types[type];
		var m = $('vehicle_make_id');
		this.populate(this.makes, type, m, 'make');
		var e = $('vehicle_engine_id');
		this.populate(this.engines, type, e, 'engine');
	},
	
	populate : function(vars, type, field, what) {
	  /* Grab the current value, repopulate and select it again */
	  var thing_id = eval("this."+what+"_id");
    var selected_value = field.options[field.selectedIndex].value;
    if (thing_id) { selected_value = 0; }
	  field.length = 0;
    var i = 1;
    field.options[0] = new Option('Select '+what,'-1');
    for (n in vars) {
      if (vars[n][0] == type) {
        field.options[i] = new Option(vars[n][1],vars[n][2]);
        if (thing_id && vars[n][2] == thing_id) {
          field.options[i].selected = true;
        } else if (vars[n][2] == selected_value) {
          field.options[i].selected = true;
        }
        i++;
      }
    }
	}
	
}

/* Utility function: fprintf
 * From: http://markos.gaivo.net/blog/?p=63 */
function fprintf(S, L) {
		var nS = "";
		var tS = S.split("%s");
		if (tS.length != L.length+1) throw "Input error";

		for(var i=0; i<L.length; i++)
			nS += tS[i] + L[i];
		return nS + tS[tS.length-1];
}

/* MessageManagers care about the message size.
	Significantly borrowed from HCTC
 */
var MessageManager = Class.create();

MessageManager.prototype = {
	maxCharacters : null,
	textAreaId : null,
	statsDivId : null,
	optionsCharsUsed : null,
	optionsArray : null,
  
  initialize : function(textAreaId, statsDivId, statsMessage, maxCharacters, optionsArray) {
	this.textAreaId = textAreaId;
	this.maxCharacters = maxCharacters;
	this.statsDivId = statsDivId;
	this.statsMessage = statsMessage;
	this.optionCharsUsed = 0;
	this.optionsArray = optionsArray;

	this.checkOptions();
    this.textChanged();
  },
  
	checkOptions : function() {
		for (var i = 0; i < this.optionsArray.length; i++) {
			if ($(this.optionsArray[i][0]).checked == true) {
				this.updateOption($(this.optionsArray[i][0]), this.optionsArray[i][1]);
			}
		}
	},

  textChanged : function() {
		if($('vehicle_type_id').value > 1) {
			var charactersUsed = $(this.textAreaId).value.length;
			if (charactersUsed > this.maxCharacters) {
			  $(this.textAreaId).value = $(this.textAreaId).value.slice(0, (this.maxCharacters));
				charactersUsed -= 1;
		    }			
		} else {
			var charactersUsed = $(this.textAreaId).value.length + this.optionCharsUsed;
			if (charactersUsed > this.maxCharacters) {
		  	$(this.textAreaId).value = $(this.textAreaId).value.slice(0, (this.maxCharacters - this.optionCharsUsed));
	    }
			charactersUsed = $(this.textAreaId).value.length + this.optionCharsUsed;
		}
    $(this.statsDivId).innerHTML = fprintf(this.statsMessage, [ charactersUsed, this.maxCharacters, (this.maxCharacters - charactersUsed)]);
  },

	updateOption : function(option, optionname) {
		if(option.checked == true) {
			// If optionCharsUsed is 0 this is the first option we have added
			// so we also calculate in the space and dash at the end of the
			// description options list: " -"
			if(this.optionCharsUsed == 0) {
				this.optionCharsUsed = 2;
			}
			this.optionCharsUsed += (optionname.length + 2);
		} else {
			this.optionCharsUsed -= (optionname.length + 2);
			// If after removing the option abbreviation from the count
			// optionCharsUsed is 2, we know that we are only calculating for
			// the space and dash in the description: " -". No options, no need.
			if(this.optionCharsUsed == 2) {
				this.optionCharsUsed = 0;
			}
		}
		this.textChanged();
	}
}

var EmailManager = Class.create();

EmailManager.prototype = {
	maxCharacters : null,
	textAreaId : null,
	warningDivId : null,
	optionsCharsUsed : null,
  
  initialize : function(textAreaId, maxCharacters) {	
    this.textAreaId = textAreaId;
	this.maxCharacters = maxCharacters;
    
    this.textChanged();
  },
  
  textChanged : function() {
	var charactersUsed = $(this.textAreaId).value.length;
	
	if (charactersUsed > this.maxCharacters) {
		if ($('emailError').style.display == "none") {
			Effect.Appear('emailError');
			Element.hide('vehicle_contact_print');
			Element.show('lockedphone');
			$('vehicle_contact_print').value = "phone";
		}
	} else {
		Element.hide('emailError');
		Element.hide('lockedphone');
		Element.show('vehicle_contact_print');
	}
  }
}

var PriceOptionManager = Class.create();

PriceOptionManager.prototype = {
	// Separate functions for each possible checkbox because the behaviour varies slightly
  
	initialize : function() {
		if ($('vehicle_obo').checked) {
			this.oboClicked($('vehicle_obo'))
		} else if ($('vehicle_call_for_price').checked) {
			this.callClicked($('vehicle_call_for_price'))
		} else if ($('vehicle_monthly_price').checked) {
			this.monthlyClicked($('vehicle_monthly_price'))
		}
	},

	oboClicked : function(checkbox) {
		if (checkbox.checked == true) {
			$('obo').removeClassName('disabled');
			$('vehicle_price').disabled = false;
			$('call-for-price').addClassName('disabled');
			$('monthly-price').addClassName('disabled');
			$('vehicle_call_for_price').checked = false;
			$('vehicle_monthly_price').checked = false;
		} else if (checkbox.checked == false) {
			$('call-for-price').removeClassName('disabled');
			$('monthly-price').removeClassName('disabled');
		}
	},

	callClicked : function(checkbox) {
		if (checkbox.checked == true) {
			$('call-for-price').removeClassName('disabled');
			$('obo').addClassName('disabled');
			$('monthly-price').addClassName('disabled');
			$('vehicle_obo').checked = false;
			$('vehicle_monthly_price').checked = false;
			$('vehicle_price').value = '';
			$('vehicle_price').disabled = true;
		} else if (checkbox.checked == false) {
			$('obo').removeClassName('disabled');
			$('monthly-price').removeClassName('disabled');
			$('vehicle_price').disabled = false;
		}
	 },

	monthlyClicked : function(checkbox) {
		if (checkbox.checked == true) {
			$('monthly-price').removeClassName('disabled');
			$('vehicle_price').disabled = false;
			$('obo').addClassName('disabled');
			$('call-for-price').addClassName('disabled');
			$('vehicle_obo').checked = false;
			$('vehicle_call_for_price').checked = false;
		} else if (checkbox.checked == false) {	
			$('obo').removeClassName('disabled');
			$('call-for-price').removeClassName('disabled');
		}
 	}
}

function clearMe(obj, text) {
	if(obj.value == text) {
		obj.value = '';
		obj.addClassName('cleared');
	}
}

function blankMe(obj, text) {
	if(obj.value != text) {
		obj.addClassName('cleared');
	}
}