/* --------------------------------------------------
	Author:		Donna Latto
	Email:		dlatto@paciolan.com
	Created:	March 6, 2007
	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";

	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;
		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;
	}
	
	function printCustomField(theField) {
		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 labelMax = 35;
		
		if(input_options.length>0) {
			for(var cfCount=0; cfCount<input_options.length; cfCount++) {
				if(typeof(input_options[cfCount])=="string") {
					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')) {
			document.write((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>'));
			return false;
		}
		
		// 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) {
			document.write((customFormFormat=="table" ? '<tr><td colspan="2">' : ''), '<p class="error">Check form settings.  Field <i>',input_name,'</i> requires options.</p>', (customFormFormat=="table" ? '</td></tr>' : ''));
			return false;
		}
		
		// Print opening tags and label
		if(input_type!='hidden') {
			if(customFormFormat=="table") {
				if(input_label.length > labelMax) {
					document.write(
						'<tr><td align="center" colspan="2" style="padding:1em 5px;">', (input_req ? '<span class="req">*</span> ' : '') , input_label ,'<br>'
					);
				} else {
					document.write(
						'<tr valign="top">',
						'	<td style="padding:1em 5px;" align="right" nowrap>', (input_req ? '<span class="req">*</span> ' : '') ,input_label,'</td>',
						'	<td style="padding:1em 5px;">'
					);
				}
			} else {
				document.write(
					'<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"';
			document.write('<input type="text" name="',input_name,'" value="',input_value,'" ',input_misc,' />');

		} 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"';
			document.write('<textarea name="',input_name,'" ',input_misc,'>',input_value,'</textarea>');

		} else if(input_type=='select') {
			document.write('<select name="',input_name,'" ',input_misc,'><option value="">-- select an option --</option>');
			for(var cfCount=0; cfCount<input_options.length; cfCount++) document.write('<option value="',input_options[cfCount][0],'"', (input_value!='' && input_options[cfCount][0]==input_value ? ' selected="selected"' : '') ,'>',input_options[cfCount][1],'</option>');
			document.write('</select>');

		} else if(input_type=='checkbox') {
			if(input_label.length > labelMax) document.write('<div style="margin: 0 0 0 25%; text-align: left;">');
			for(var cfCount=0; cfCount<input_options.length; cfCount++) document.write('<input type="checkbox" name="',input_name,'" value="',input_options[cfCount][0],'"', (input_options[cfCount][0]==input_value ? ' checked="checked"' : '') ,' ',input_misc,'> ',input_options[cfCount][1], (cfCount<input_options.length ? '<br>' : ''));
			if(input_label.length > labelMax) document.write('</div>');
		
		} else if(input_type=='radio') {
			if(input_label.length > labelMax) document.write('<div style="margin: 0 0 0 25%; text-align: left;">');
			for(var cfCount=0; cfCount<input_options.length; cfCount++) document.write('<input type="radio" name="',input_name,'" value="',input_options[cfCount][0],'"', (input_options[cfCount][0]==input_value ? ' checked="checked"' : '') ,' ',input_misc,'> ',input_options[cfCount][1], (cfCount<input_options.length ? '<br>' : ''));
			if(input_label.length > labelMax) document.write('</div>');
		
		} else if(input_type=='hidden') {
			document.write('<input type="hidden" name="',input_name,'" value="',input_value,'" ',input_misc,' />');

		} else if(input_type=='password') {
			document.write('<input type="password" name="',input_name,'" value="',input_value,'" ',input_misc,' />');

		} else if(input_type=='submit') {
			if(input_value=='') input_value = 'Submit';
			document.write('<input type="submit" name="',input_name,'" value="',input_value,'" ',input_misc,' />');

		} else if(input_type=='reset') {
			if(input_value=='') input_value = 'Reset';
			document.write('<input type="reset" name="',input_name,'" value="',input_value,'" ',input_misc,' />');

		} else {
			document.write('<p class="error">Invalid Field Type: <i>',input_type,'</i>.</p>');
		}
		
		// Print closing tags
		if(input_type!='hidden') {
			if(customFormFormat=="table") {
				document.write(
					'</td></tr>'
				);
			} else {
				document.write(
					'</p>'
				);
			}
		}
	}
	
	function checkCustomForm (theForm, customFields, hiddenCommentField) {

		var cfInfo = "";
		var cfSeparator = " ; ";
		var result = "";
		
		for(var ccfi=0; ccfi<customFields.length; ccfi++) {
			var temp = customFields[ccfi];

			if(temp.input_type=='radio' || (temp.input_type=='checkbox' && theForm[temp.input_name].length>0)) {
				result = "";
				for (var ccfj=0; ccfj<theForm[temp.input_name].length; ccfj++) {
					if (theForm[temp.input_name][ccfj].checked)
						result += theForm[temp.input_name][ccfj].value + ", ";
				}
				result = result.substring(0, result.length-2);
			} else {
				result = theForm[temp.input_name].value;
			}
			
			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();
				}
				return false;
			} else if (result != "") {
				if(hiddenCommentField.indexOf('addr') >= 0 || hiddenCommentField.indexOf('line') >= 0) {				// custom register forms
					cfInfo += result + cfSeparator;
				
				} else if(hiddenCommentField.indexOf('renewSH_')==0) {	// custom SR forms
					cfInfo += temp.input_name.slice(0, temp.input_name.lastIndexOf("_")) + ": " + result + cfSeparator;
				
				} else if(hiddenCommentField.indexOf('contentSH_')==0 || hiddenCommentField.indexOf('contentSHi_')==0) {	// custom Package forms
					var t_input_name = temp.input_name.slice(0, temp.input_name.lastIndexOf("_"))
					t_input_name = t_input_name.slice(0, t_input_name.lastIndexOf("_"))
					cfInfo += t_input_name + ": " + result + cfSeparator;
				
				} else {													// everything else
					cfInfo += temp.input_name + ": " + result + cfSeparator;
				}
			}
		}
		cfInfo = cfInfo.substring(0, cfInfo.length-cfSeparator.length);
		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;
	}
