/* ==================================================
	Author:		Donna Latto
	Email:		dlatto@paciolan.com
	Version:	2.0 (23 Dec 2011)
	Additions:	Added multi-guest functionality
	Purpose:	Functions to create and validate 
				custom forms.  Can be used on almost 
				any page but mainly:  
				- displayEventInfo.html 
				- displayItemInfo 
				- displayNewApp/srComponents
	Version Notes:
				3/30/07 - Updated checkCustomForm 
				function so that the results are 
				passed on differently depending 
				on where the form is used.
				
				5/31/07 - Cleaned up label of 
				&#39; and &quot; characters for 
				alert function.
================================================== */
	var customFormFormat = "table";
	var labelMax = 35;

	function makeCustomField(input_type, input_name, input_label, input_value, input_options, input_misc, input_req) {
		this.input_type		= input_type;
		this.input_name		= input_name.replace(/\s/ig, "_");
		this.input_label	= input_label;
		this.input_value	= input_value;
		this.input_options	= input_options;
		this.input_misc		= input_misc;
		this.input_req		= input_req;
	}
	
// Multi-user update (returns form fields in a string instead of prints fields)
	function getFieldTxt(theField, guestNumber) {
		var input_type		= theField.input_type;
		var input_name		= theField.input_name;
		var input_label		= theField.input_label;
		var input_req		= theField.input_req;
		var input_options	= theField.input_options;
		var input_value		= theField.input_value;
		var input_misc		= theField.input_misc;
		var fieldTxt = '';
		if(typeof(guestNumber)=="undefined") {
			guestNumber = "";
		} else if(guestNumber!="") {
			input_name += "_" + guestNumber;
		}
		
		if(input_options!=0 && input_options.length>0) {
			for(var cfCount=0; cfCount<input_options.length; cfCount++) {
				if(typeof(input_options[cfCount])!="object") {
					input_options[cfCount] = new Array(input_options[cfCount], input_options[cfCount]);
				}
			}
		}
		
		// If there are missing parameters, abort function.
		if(input_type=='' || input_name=='' || (input_label=='' && input_type!='submit' && input_type!='reset')) {
			return ((customFormFormat=="table" ? '<tr><td colspan="2">' : '') + '<p class="error">Check form settings.  One or more required parameters are missing for field "'+input_name+'."</p>' + (customFormFormat=="table" ? '' : '</td></tr>'));
		}
		
		// If it is a certain type, make sure there are options.  Otherwise, abort.
		if((input_type=='select' || input_type=='checkbox' || input_type=='radio') && (input_options.length < 1 || typeof(input_options)!="object")) {
			return ((customFormFormat=="table" ? '<tr><td colspan="2">' : '')+ '<p class="error">Check form settings.  Field <i>'+input_name+'</i> requires options.</p>'+ (customFormFormat=="table" ? '</td></tr>' : ''));
		}
		
		// Print opening tags and label
		if(input_type!='hidden') {
			if(customFormFormat=="table") {
				if(input_label.length > labelMax) {
					fieldTxt += '<tr><td colspan="2" class="labelMax"><div>'+ (input_req ? '<span class="req">*</span> ' : '') + input_label +'</div><div>';
				} else {
					fieldTxt += ''+
						'<tr>'+
						'	<th>'+ (input_req ? '<span class="req">*</span> ' : '') +input_label+'</th>'+
						'	<td>';
				}
			} else if(customFormFormat=="dl") {
				fieldTxt += '<dt>'+ (input_req ? '<span class="req">*</span> ' : '') +input_label+'</dt><dd>';
			} else {
				fieldTxt += '<p><b>'+ (input_req ? '<span class="req">*</span> ' : '') +input_label+'</b><br>';
			}
		}
		
		// Print field based on type
		if(input_type=='text') {
			if(input_misc.indexOf('maxlength=')==-1) input_misc+=' maxlength="50"';
			fieldTxt += '<input type="text" name="'+input_name+'" value="'+input_value+'" '+input_misc+' />';
			if(input_misc.indexOf("dynamicDate_toggleCalendar")>-1) {
				fieldTxt += '<br /><small>(format: mm/dd/yyyy)</small>';
			}

		} else if(input_type=='textarea') {
			if(input_misc.indexOf('cols=')==-1) input_misc+=' cols="30"';
			if(input_misc.indexOf('rows=')==-1) input_misc+=' rows="4"';
			fieldTxt += '<textarea name="'+input_name+'" '+input_misc+'>'+input_value+'</textarea>';

		} else if(input_type=='select') {
			fieldTxt += '<select name="'+input_name+'" '+input_misc+'>';
			if(input_options[0][0]!="") fieldTxt += '<option value="">-- select an option --</option>';
			for(var cfCount=0; cfCount<input_options.length; cfCount++) fieldTxt += '<option value="'+input_options[cfCount][0]+'"'+ (input_value!='' && input_options[cfCount][0]==input_value ? ' selected="selected"' : '') +'>'+input_options[cfCount][1]+'</option>';
			fieldTxt += '</select>';

		} else if(input_type=='checkbox' || input_type=='radio') {
			fieldTxt += '<ul class="customFormOptions">';
			for(var cfCount=0; cfCount<input_options.length; cfCount++) {
				var cf_labelID = 'cf_'+input_name+"_"+cfCount;
				fieldTxt += '<li><input type="'+(input_type=='checkbox' ? 'checkbox' : 'radio')+'" name="'+input_name+'" id="'+cf_labelID+'" value="'+input_options[cfCount][0]+'"'+ (input_options[cfCount][0]==input_value ? ' checked="checked"' : '') +' '+input_misc+'> <label for="'+cf_labelID+'">'+input_options[cfCount][1]+'</label></li>';
			}
			fieldTxt += '</ul>';
		
		} else if(input_type=='hidden') {
			fieldTxt += '<input type="hidden" name="'+input_name+'" value="'+input_value+'" '+input_misc+' />';

		} else if(input_type=='password') {
			fieldTxt += '<input type="password" name="'+input_name+'" value="'+input_value+'" '+input_misc+' />';

		} else if(input_type=='submit') {
			if(input_value=='') input_value = 'Submit';
			fieldTxt += '<input type="submit" name="'+input_name+'" value="'+input_value+'" '+input_misc+' />';

		} else if(input_type=='reset') {
			if(input_value=='') input_value = 'Reset';
			fieldTxt += '<input type="reset" name="'+input_name+'" value="'+input_value+'" '+input_misc+' />';

		} else {
			fieldTxt += '<p class="error">Invalid Field Type: <i>'+input_type+'</i>.</p>';
		}
		
		// Print closing tags
		if(input_type!='hidden') {
			if(customFormFormat=="table") {
				if(input_label.length > labelMax) {
					fieldTxt += '</div>';
				}
				fieldTxt += '</td></tr>';
			} else if(customFormFormat=="dl") {
				fieldTxt += '</dd>';
			} else {
				fieldTxt += '</p>';
			}
		}
		return fieldTxt;
	}
	
// Multi-user update (old function - prints fields)
	function printCustomField(fi) {
		document.write(getFieldTxt(fi));
	}
	
	function checkCustomForm (theForm, customFields, hiddenCommentField) {

		var cfInfo = new Array();
		var cfSeparator = " ; ";
		var result = "";
		
		// Multi-user update
		var cfContainer = document.getElementById(customFields.containerID);
		var t_customFields = new Array();
		
		if(typeof(customFields.qtyFields)!="undefined" && customFields.qtyFields.length>0 && cfContainer!=null) {
			var activeQty = 0;
			for(var a=0; a<customFields.qtyFields.length; a++) {
				activeQty += Number(theForm[customFields.qtyFields[a]].value);
			}
			for(var a=1; a<=activeQty; a++) {
				for(var b=0; b<customFields.length; b++) {
					t_customFields.push( new makeCustomField(
						customFields[b].input_type,
						customFields[b].input_name+"_"+a,
						customFields.guestLabel+" "+a+" - "+customFields[b].input_label,
						customFields[b].input_value,
						customFields[b].input_options,
						customFields[b].input_misc,
						customFields[b].input_req
					) );
				}
			}
		} else {
			t_customFields = customFields;
		}
		
		//tester: alert(t_customFields.length +"|"+ customFields.length); return;
		// END Multi-user update
		
		for(var ccfi=0; ccfi<t_customFields.length; ccfi++) {
			var temp = t_customFields[ccfi]; // Multi-user update

			if(temp.input_type=='radio' || (temp.input_type=='checkbox' && theForm[temp.input_name].length>0)) {
				result = new Array();
				for (var ccfj=0; ccfj<theForm[temp.input_name].length; ccfj++) {
					if (theForm[temp.input_name][ccfj].checked==true)
						result.push(theForm[temp.input_name][ccfj].value);
				}
				result = result.join(", ");
			} else if(temp.input_type=='checkbox') {
				if (theForm[temp.input_name].checked==true)
					result = theForm[temp.input_name].value;
				else
					result = '';
			} else {
				result = theForm[temp.input_name].value;
			}
			
			var resultLabel = temp.input_name;
			
			if(temp.input_req && result=="") {
				var t_missing = temp.input_label.replace(/&#39;/g, "'");
				t_missing = t_missing.replace(/&quot;/g, '"');
				/*
				alert('You must enter a value in "'+ stripHTML(t_missing) +'."');
				if(temp.input_type=='checkbox' || temp.input_type=='radio') {
					theForm[temp.input_name][0].focus();
				} else {
					theForm[temp.input_name].focus();
				}*/
				var postAction = 'document.'+theForm.name+'.'+temp.input_name+'.focus();';
				if(temp.input_type=='checkbox' || temp.input_type=='radio') {
					postAction = 'document.'+theForm.name+'.'+temp.input_name+'[0].focus();';
				}
				showBox('You must enter a value in "'+ stripHTML(t_missing) +'."', '', '', '', '', '', postAction);
				return false;
			} else if (result != "") {
				// custom register forms
				if(hiddenCommentField.indexOf('addr')>=0 || hiddenCommentField.indexOf('line')>=0) {
					resultLabel = "";
				
				// custom SR forms
				} else if(hiddenCommentField.indexOf('renewSH_')==0) {
					resultLabel = resultLabel.slice(0, resultLabel.lastIndexOf("_"));
				
				// custom Package forms
				} else if(hiddenCommentField.indexOf('contentSH_')==0 || hiddenCommentField.indexOf('contentSHi_')==0) {
					if(typeof(customFields.qtyFields)!="undefined" && customFields.qtyFields.length>0 && cfContainer!=null) {
						t_resultLabel = resultLabel.split("_");
						resultLabel = [];
						for(var a=0; a<t_resultLabel.length; a++) {
							if(a!=t_resultLabel.length-3 && a!=t_resultLabel.length-2) {
								resultLabel.push(t_resultLabel[a]);
							}
						}
						resultLabel = resultLabel.join("_");
					} else {
						resultLabel = resultLabel.slice(0, resultLabel.lastIndexOf("_"));
						resultLabel = resultLabel.slice(0, resultLabel.lastIndexOf("_"));
					}
				
				}
				
				cfInfo.push( (resultLabel!="" ? resultLabel+": " : "") + result );
			}
		}
		cfInfo = cfInfo.join(cfSeparator);
		cfInfo = cfInfo.replace(/\r|\n|\r\n/g, " ");
		cfInfo = cfInfo.replace(/\'|\"/g, "-"); // remove single and double quotes - causes error
		theForm[hiddenCommentField].value = cfInfo;
		return true;
	}

/* ==================================================
	Multi-user update
================================================== */
	
	function toggleMultiForm(theForm, cfArray) {
		var cfContainer = document.getElementById(cfArray.containerID);
		if(
		   	cfContainer==null ||
			typeof(cfArray.qtyFields)=="undefined" ||
			cfArray.qtyFields.length==0 ||
			cfArray.length==0
		) {
			return false;
		}
		
		var activeQty = 0;
		for(var a=0; a<cfArray.qtyFields.length; a++) {
			activeQty += Number(theForm[cfArray.qtyFields[a]].value);
		}
		
		if(activeQty==0) {
			cfContainer.innerHTML = '<p align="center" class="error">Please enter a quantity.</p>';
			return false;
		}
		
		var guestFormsTxt = '';
		for(var a=1; a<=activeQty; a++) {
			// print opening containers for each guest
			guestFormsTxt += '<div id="guest_'+a+'" class="guestFormContainer">'+
				(customFormFormat=="table" ? '<table class="formTable">' : '');
			
			// print guest number above each form
			guestFormsTxt += (customFormFormat=="table" ? '<tr><td colspan="2">' : '') +
				'<div class="guestFormLabel">'+ cfArray.guestLabel +' '+a+'</div>'+
				(customFormFormat=="table" ? '</td></tr>' : '');
			
			for(var b=0; b<cfArray.length; b++) {
				guestFormsTxt += getFieldTxt(cfArray[b], a);
			}
			
			// print closing containers for each guest
			guestFormsTxt += (customFormFormat=="table" ? '</table>' : '')+
				'</div>';
			
		}
		
		cfContainer.innerHTML = guestFormsTxt;
	}
	
	function addMultiToggleToQty(theForm, cfArray, cfArrayString) {
		if(typeof(cfArray.qtyFields)!="object") cfArray.qtyFields = new Array(cfArray.qtyFields);
		
		for(var a=0; a<cfArray.qtyFields.length; a++) {
			var qtyObj = theForm[cfArray.qtyFields[a]];
			
			if(qtyObj!=null) {
				var qtyChanger = "onblur";
				var oldfunc = qtyObj.onblur;
				
				if(qtyObj.tagName.toLowerCase()=="select") {
					qtyChanger = "onchange";
					oldfunc = qtyObj.onchange;
				}
				
				if(typeof(oldfunc)=="function") {
					oldfunc = oldfunc + "";
					oldfunc = oldfunc.slice(oldfunc.indexOf("{")+1, oldfunc.lastIndexOf("}"));
				} else {
					oldfunc = "";
				}
				
				eval(
					'document.'+theForm.name+'.'+cfArray.qtyFields[a]+'.'+qtyChanger+' = function(event) {' +
						oldfunc +
					'	toggleMultiForm(this.form, '+cfArrayString+');' +
					'}'
				);
				eval(
					'addLoadEvent(function() {' +
					'	toggleMultiForm(document.'+theForm.name+', '+cfArrayString+');' +
					'});'
				);
			}
		}
	}
