/***********************************************
 multiCheckbox
 Creates enough checkboxes to load a hidden input with multiple checkbox options.
 Copyright 2008, Lewis Lorenz, multiCheckbox@LorenzCom.com.
 Usage (HTML): <script language="JavaScript"> multiCheckboxInput(inputname, inputvalue, [checkboxvalues], [checkboxlabels], [separator[\t]])</script>
 ************************************************/

// Stylesheet definition for the calendar
/*
with (document) {
   writeln('<style>');
   writeln('td.multiCheckboxInput {letter-spacing:normal;line-height:normal;font-family:Verdana,Sans-Serif;font-size:11px;vertical-align: middle;}');
   writeln('input.multiCheckboxInput {letter-spacing:.06em;font-family:Verdana,Sans-Serif;font-size:11px;vertical-align: middle;}');
   writeln('</style>');
}
*/

function adjustInputValue(obj){
	var objindex = parseInt((obj.id.substr(obj.id.lastIndexOf('_') + 1)), 10); // Get the index value off the end.
	this.checked[objindex] = (obj.checked) ? ' checked':'';
	var x = this.checked.length;
	this.hiddenFieldValue = ''; //Clear.
	for(var i=0; i<x; i++){
		this.hiddenFieldValue += (this.checked[i]) ? this.theseparator + this.values[i] : ''; // Create string with separator separating values (front, too).
	}
	this.mcb_setHiddenValue(this.theseparator + this.hiddenFieldValue + this.theseparator); // End, too. Framed in separator for easier searching of keywords/tags column.
}

function mcb_setHiddenValue(v){
	if (this.formNumber >= 0) this.mcb_getHiddenField().value = v;
}

function mcb_getHiddenField(){
	return document.forms[this.formNumber].elements[this.hiddenFieldName];
}

//Main multi-checkbox object class.
function multiCheckboxObject(inputname, inputvalue, checkboxvalues, checkboxlabels, separator){
	// Variables (Set to defaults.)
    this.hiddenFieldName = inputname;
	this.theseparator = separator;
	checkboxvalues = unescape(checkboxvalues); // Parameters encoded via PHP with rawurlencode to allow single and double quotes and \r \t to be passed etc.
	inputvalue = (unescape(inputvalue)).replace(/^\s+|\s+$/g,""); // Remove white-space (e.g., tabs) from front and end of inputvalues.
	this.ivalues = inputvalue.split(separator); // Split the string ito an array (first element is the string if separator not found).
	x = this.ivalues.length;
	for (var i=0; i<x; i++){ // Append to checkboxvalues if in inputvalues and not passed as a value option.
		if (this.ivalues[i]){
			checkboxvalues += ((separator + checkboxvalues + separator).indexOf(separator + this.ivalues[i] + separator) == -1) ? separator + this.ivalues[i] : '' ; // Append missing value.
		}
	}
	this.values = (checkboxvalues) ? checkboxvalues.split(separator) : ((inputvalue) ? inputvalue.split(separator) : new Array('Undefined')); // Default to passed values || passed data || 'Undefined'
    if(checkboxlabels){ // Default to passed labels or the same as values array.
		this.labels = (unescape(checkboxlabels)).split(separator);
	}else{
		this.labels.concat(this.values);
	}
	this.hiddenFieldValue = separator + inputvalue + separator; // String: '\tvalue\tvalue\tvalue\t' for initialization below.
	this.checked = new Array('');
	this.formNumber = -1;
	
	// Class Methods
	this.adjustInputValue = adjustInputValue;
	this.mcb_setHiddenValue = mcb_setHiddenValue;
	this.mcb_getHiddenField = mcb_getHiddenField;
	
	// Functions
	//this.getHiddenValue = new Function('if (this.formNumber >= 0) return this.mcb_getHiddenField().value'); // Not used.
	//this.mcb_setHiddenValue = new Function('V','if (this.formNumber >= 0) this.mcb_getHiddenField().value = V');
	//this.mcb_getHiddenField = new Function('return document.forms[this.formNumber].elements[this.hiddenFieldName]');

	// Constructor
	var x = this.values.length;
	this.checked.length = x; // Make it the same size.
	for (var i=0;i<x;i++){
		this.labels[i] = (this.labels[i]) ? this.labels[i] : this.values[i]; // Make sure labels are something (at least the values).
		this.checked[i] = (this.hiddenFieldValue.indexOf(this.theseparator + this.values[i] + this.theseparator) > -1) ? ' checked':'';
	}
}

// Main function that creates the multiple checkbox form elements
function multiCheckboxInput(inputname, inputvalue, checkboxvalues, checkboxlabels, separator) {
	if (arguments.length == 0){
		document.writeln('<span style="color:red;font-size:' + FontSize + 'px;font-family:' + FontFamily + ';">ERROR: Missing required parameter in call to \'multiCheckboxInput\': [name of hidden field].</span>');
	}else{
		if (arguments.length < 5){
			separator = '\t'; // Default string.split separator is a tab.
			if (arguments.length < 4){
				checkboxlabels = ''; 
					if (arguments.length < 3){
					checkboxvalues = '';
					if (arguments.length < 2){
						inputvalue = ''; // Default to nothing if not sent.
					}
				}
			}
		}
	  eval(inputname + '_Object = new multiCheckboxObject(\'' + inputname + '\',\'' + inputvalue + '\',\'' + checkboxvalues + '\',\'' + checkboxlabels + '\',\'' + separator + '\')');
      with (document) {
         writeln('<input type="hidden" name="' + inputname + '" id="' + inputname + '" value="' + inputvalue + '">'); // Set form input and passed values.
         // Find this form number
         for (var f=0;f<forms.length;f++) {
            for (var e=0;e<forms[f].elements.length;e++) {
               if (typeof forms[f].elements[e].type == 'string') {
                  if ((forms[f].elements[e].type == 'hidden') && (forms[f].elements[e].name == inputname)) {
                     eval(inputname + '_Object.formNumber='+f);
                     break;
                  }
               }
            }
         }
		 eval(inputname + '_Object.mcb_setHiddenValue(inputvalue)'); // Initialize the hidden field value.
		 var cr = String.fromCharCode(13);
		 var fn = eval(inputname + '_Object.formNumber');
         writeln('<table cellpadding="0" cellspacing="0">');
		 var x = eval(inputname + '_Object.labels.length') - 1;
		 for (var i=0;i<=x;i++) {
			writeln('<tr>'+ cr +'<td valign="top">');
			writeln('<input type="checkbox" id="'+ inputname +'_multiCheckbox_ID_'+ i +'" value="'+ eval(inputname + '_Object.values['+ i +']') +'" ' + '" onClick="' + inputname + '_Object.adjustInputValue(this)"'+ eval(inputname + '_Object.checked['+ i +']') +'>');
			writeln('<span id="'+inputname +'_multiCheckbox_span_'+i+'" onClick="document.forms['+ fn +'].elements[\''+inputname +'_multiCheckbox_ID_'+ i +'\'].click()">'+ eval(inputname + '_Object.labels['+ i +']')+'</span>');
			writeln(cr +'</td>'+ cr +'</tr>');
		 }  
         writeln(cr +'</table>');
      }
   }
}
