/*------------------------------------------------------------------------------------------+
 | Class: objAvail.js																		|
 | --																						|
 | Use in prod.php to handle showing availability information and stopping the client from	|
 | order if they product's not available.													|
 | 																							|
 | Usage																					|
 | --																						|
 | var obj = new objAvail( { 'availString' : 'availCode' , ...} );							|
 | Constructor takes an object (assoc array) of availability info from shopProdAvail 		|
 | The showAvail method is automatically added to the document's existing onload event		|
 |																							|
 | Properties (configuration)																|
 | --																						|
 | .msgDiv - page element ID for availability message, default 'panel_avail'				|
 | .formID - ID of basket-add form, default '0' for first form on page						|
 | .saveBtn - name of form's submit button, default 'save'									|
 | .avMsgs - HTML of messages to show for different availability types						|
 | .avTypes - option types and the form field names that correspond to them					|
 +-----------------------------------------------------------------------------------------*/
 
 function objAvail(avData, avTypes) {
 
	// Load availability data from constructor
	this.avData = avData ? avData : {};
	this.avTypes = avTypes ? avTypes : {};

	// Make sure we can talk back from the window ...
	window.ss_av_object = this;

	// Automatically set the window to call 'showAvail' on loading
	var currOnload = window.onload; // keep existing onload
	window.onload = function() {
		if(currOnload) currOnload();
		ss_av_object.showAvail();
	}

	// And now the bits you may want to change after creating the object
	this.avMsgs = { 'ot' : "(temporarily unavailable - <a href=\"javascript:enquiryWin();\">enquire</a>)", 'os' : "(sorry, out of stock)" };
	this.formID = '0';
	this.saveBtn = 'save';
	this.msgDiv = 'ss_panel_avail';
    this.msgDisplay = null;

	/*============= Begin method defintions ... ================================================================*/
	
	// Get a document element by ID ...
	this.gEl = document.all ? function(i){return document.all[i]} : function(i){return document.getElementById(i)};

	// Determine availability of current selection and update form accordingly (error-checked) ...
	this.showAvail = function(elId) {
		var msgArea, objForm, btnSave;
		if(!(msgArea = this.gEl(this.msgDiv))) {
			this.handleErr("Couldn't access the " + this.msgDiv + " element to show availability.");
		} else if(!(objForm = document.forms[this.formID])) {
			this.handleErr("Couldn't find form with ID '" + this.formID + "'");
		} else if(!(btnSave = objForm.elements[this.saveBtn])) {
			this.handleErr("Couldn't a submit button called '" + this.saveBtn + "'");
		} else {
            if(!this.msgDisplay) {
               this.msgDisplay = msgArea.style.display == '' ? 'block' : msgArea.style.display;
            }
			var strOptions = this.getOptionString();
			msgArea.innerHTML = (avCode = this.getAvCode(strOptions)) ? this.avMsgs[avCode] : "";
            msgArea.style.display = avCode ? this.msgDisplay : 'none';
			btnSave.disabled = (avCode != undefined);
			objForm.onsubmit = avCode ? function(){return false;} : function(){return true;};
		}
	}

	// Build an options query string from options selected in form ...
	this.getOptionString = function() {
		var arrOptions = new Array();
		for(var i in this.avTypes) {
			var optField = document.forms[this.formID].elements['options[' + i + ']'];
			optField.onchange = function() { ss_av_object.showAvail(); }
			var optValue = optField.type == "hidden" ? "" : optField.options[optField.selectedIndex].text;
			arrOptions.push(i + '=' + escape(optValue));
		}
		return arrOptions.join("&");
	}
	
	// avData value encoded by PHP, but strOpts by Javascript (doesn't encode *, +, /, @)
	this.getAvCode = function(strOpts) {
		for(var i in this.avData) { if(strOpts == this.urlConvert(i)) return this.avData[i]; }
	}
	
	// Convert PHP-escaped string to match JS-escaped one
	this.urlConvert = function(qs) {
		var pairs = qs.split("&");
		for(var i in pairs) {
			var bits = pairs[i].split("=");
			pairs[i] = bits[0] + "=" + escape(unescape(bits[1]));
		}
		return pairs.join("&");
	}
	
	// Error handling - by default, show message in status bar
	this.handleErr  = function(msg) {
		window.status = "objAvail Error: " + msg;
	}

}
