<!--
	// _validate_input.js _ 1252468840
	// Copyright (c) 2009 appGenerator.  All Rights Reserved.
	// To obtain a legal license for this software, contact info@egauge.com.
	// Unauthorized usage, distribution and/or reverse engineering are strictly prohibited and may result in legal action.
	// code generated by DOKUMENTS on September 8, 2009


	function test_for_required(str, id, required) {
		if (required) {
			// used the value passed to the function;
		} else {
			// assume it is not required;
			var required = 0;
		}

		var thisId = document.getElementById(id);
		var feedback_id = id + "_feedback";

		if (thisId) {
			// okay			
		} else {
			// element does not exist
			write_to_div("<font color=red>" + id + " does not exist. (id: 2091097)</font>", "submit_feedback");
		}


		if (document.getElementById(feedback_id)) {
			// okay			
		} else {
			// element does not exist
			write_to_div("<font color=red>" + feedback_id + " does not exist. (id: 2171097)</font>", "submit_feedback");
		}


		if (thisId) {
			if (thisId.type == "radio") { 
				var str = 0;
				checked_counter = 0;
				var this_element = document.inputForm.elements[id];
				for (counter = 0; counter < this_element.length; counter++) {			
					if ((document.inputForm.elements[id][counter]).checked) {
						checked_counter++;
						write_to_div("<font color=green>" + id + "[" + checked_counter + "] is checked. (id: 2291097)</font>", "submit_feedback");		
					}
				}

				str = checked_counter;
			} else if (thisId.type == "checkbox") { 
				var str = 0;
				checked_counter = 0;

				var num_checkboxes = document.inputForm.elements[id].length;
				if (num_checkboxes == undefined) {
					// only one checkbox;
					if (thisId.checked) {
						checked_counter++;
					} 
				} else {
					// multiple checkboxes;
					for (var i=0; i< document.inputForm.elements[id].length; i++) {
						if (document.inputForm.elements[id][i].checked) {
							checked_counter++;
							write_to_div("<font color=green>" + id + "[" + checked_counter + "] is checked. (id: 2481097)</font>", "submit_feedback");		
						}
					}
				}

				str = checked_counter;
			} else {
				// use the str sent to the function
			} 
		} else {
			// element does not exist
			write_to_div("<font color=red>" + id + " does not exist. (id: 2381097)</font>", "submit_feedback");		
		}


		if (required == 1) {
			if (str) {
				// ok

				// clear any feedback messages;
				write_to_div(" ", feedback_id);
				write_to_div(" ", "submit_feedback");	

				return true;
			} else {
				write_to_div("<span class=highlight>This field is required.</span>", feedback_id);
				document.getElementById("submit_button").disabled = true;
				write_to_div("<font color=red>Please correct the problem(s) above.</font>", "submit_feedback");
				return false;
			}
			
		} else {
			// this field is not required;

			// clear any feedback messages;
			write_to_div(" ", feedback_id);
			write_to_div(" ", "submit_feedback");	

			document.getElementById("submit_button").disabled = false;
			return true;
		}
	}


	function test_string(str, id, type, required, auto_save_string) {
		var inputOk = false;

		var feedback_id = id + "_feedback";

		if (document.getElementById(feedback_id)) {
			// okay			
		} else {
			// element does not exist
			write_to_div("<font color=red>" + feedback_id + " does not exist. (id: 2701097)</font>", "submit_feedback");
		}

		write_to_div("<font color=blue>" + str + "</font>", "submit_feedback");

 
		if ((str) && (id)) {				
			if (type) {
				if (type == "integer") {
					var inputOk = test_character(str,"-0123456789", feedback_id);
				} else if (type == "decimal") {
					var inputOk = test_character(str,"-.0123456789", feedback_id);
				} else if (type == "upload") {
					var inputOk = test_character(str,"/.01234-5 _()6789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", feedback_id);
				} else if (type == "test_email_characters") {
					var inputOk = test_character(str,"@.01234-56789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", feedback_id);
				} else if (type == "test_email_address") {
					var inputOk = test_email(str, feedback_id);
				} else {
					var inputOk = test_character(str,"\r\n\t,.?:-_$ 012<>3@4567^89ab|=cd/efg*hijklmnop;qrst+uvw&x#yzABCDEFGH\'&IJKLMNOP\"QRSTUVWXYZ()!", feedback_id);		
				}
			}
		}

		if ((required) && ((str == null) || (str == ""))) {
			test_for_required(str, id, required);
		}

	
		if (inputOk) {
			// the string is valid;	

			write_to_div(" ", feedback_id);
			write_to_div(" ", "submit_feedback");	

			if (required) {
				// used the value passed to the function;
				test_for_required(str, id, required);
			} else {
				// assume it is not required;
				var required = 0;
			}
	
					
			if (auto_save_string) {
				// run auto-save, if there were no errors;				
				auto_save(auto_save_string);			
			}

			document.getElementById("submit_button").disabled = false;
		} else {
			/// something is wrong;
			// document.getElementById("submit_button").disabled = true;
		}
	}
	



	function test_email(str, id) {
		if (id) {
			if (document.getElementById(id)) {
				// okay			
			} else {
				// element does not exist
				write_to_div("<font color=red>" + id + " does not exist. (id: 3771097)</font>", "submit_feedback");
			}
		}


		var result = true;
		if (str != "") {
			var emailFilter=/^.+@.+\..{2,3}$/;
			if (!(emailFilter.test(str))) {
				write_to_div("<span class=highlight>Please enter a valid email address.</span>", id);
				document.inputForm.submit_button.disabled = true;
				write_to_div("<font color=red>Please correct the problem(s) above.</font>", "submit_feedback");
				result = false;
			 } else {
				//test email for illegal characters;
				var illegalChars= /[\(\)\<\>\,\;\:\"\[\]]/;
			 	if (str.match(illegalChars)) {
					write_to_div("<span class=highlight>The email address contains an invalid character.</span>", id);
					document.inputForm.submit_button.disabled = true;
					write_to_div("<font color=red>Please correct the problem(s) above.</font>", "submit_feedback");
					result = false;
				}
		  	}
		 }
		return result;
	}







	function test_character(str,charset, feedback_id)	{
		if (feedback_id) {
			if (document.getElementById(feedback_id)) {
				// okay			
			} else {
				// element does not exist
				write_to_div("<font color=red>" + feedback_id + " does not exist. (id: 4161097)</font>", "submit_feedback");
			}
		}


		if ((str) && (feedback_id) && (charset)) {
			var result = true;
			
			if (document.getElementById("username_1097")) {
				// user is logged in;
			} else {

				// if the string has two consecutive slashes, then block it becase its probably a link;
				var has_consecutive_slashes = str.indexOf("//");

				if (has_consecutive_slashes != -1) {
					write_to_div("<span class=highlight>You may not use two consecutive slashes. (id: 4501097)</span>", feedback_id);
					write_to_div("<font color=red>You are using two consecutive slashes above, which is not allowed.</font>", "submit_feedback");
					document.inputForm.submit_button.disabled = true;
					return false;
				}
			}
				
		
			for (var i=0;i<str.length;i++) {
				if (charset.indexOf(str.substr(i,1)) == -1) {
					write_to_div("<span class=highlight>" + str.substr(i,1) + " is not allowed here. (id 4471097)</span>", feedback_id);
					document.inputForm.submit_button.disabled = true;
					write_to_div("<font color=red>Please correct the problem(s) above.</font>", "submit_feedback");
					result = false;
					break;
				}
			}

			return result;
		}
	}


	function inValidCharSet(str,charset)	{
		if ((str) && (charset)) {
			var result = true;
			for (var i=0;i<str.length;i++)
			if (charset.indexOf(str.substr(i,1)) < 0) {
				result = false;
				break;
			}
			return result;
		}
	}



	function write_to_div(text,id) {
		if (id) {
			if (document.getElementById(id)) {
				// okay			
			} else {
				// element does not exist
				write_to_div("<font color=red>" + id + " does not exist. (id: 4571097)</font>", "submit_feedback");
			}
		} else {
				write_to_div("<font color=red>the id is missing; cannot write to div. (id: 4781097)</font>", "submit_feedback");
		}
				
		if ((text) && (id)) {
			if (document.getElementById) {
				x = document.getElementById(id);
				if (x) {
					x.innerHTML = "";
					x.innerHTML = text;
				}
			} else if (document.all) {
				x = document.all[id];
				if (x) {
					x.innerHTML = text;
				}
			} else if (document.layers) {
				x = document.layers[id];
				if (x) {
					text2 = "<P CLASS=testclass>" + text + "</P>";
					x.document.open();
					x.document.write(text2);
					x.document.close();
				}
			}
		}
	}




	// highlight the background color of a question when it is required, and is not answered (until onChange);
	function changeit(obj) {
		if (obj) {
			thisCell = document.getElementById(obj);
			if (thisCell.className='highlight') {
				thisCell.className='normal';
			}
		}
	}

	// slight modification for integer and decimal types; they may have a math calculation associated with them.
	// Just in case they do, call a function that will be at the end of this file to process any equations that may exist.;

	function changeitCalc(obj) {
		if (obj) {
		//	thisCell = document.getElementById(obj);
		//	if (thisCell.className='highlight') {
		//		thisCell.className='normal';
		//	}
			return calc_values();
		}
	}

	// restrict the number of characters that can be typed into a textarea;
	function textCounter (field, countfield, maxlimit) {
		if (field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit);
		} else {
			countfield.value = maxlimit - field.value.length;
		}
	}

	// safari will not render the toFixed javascript feature, so we have to use the following
	// Original:  Cyanide_7 (leo7278@hotmail.com)
	// Web Site:  http://www7.ewebcity.com/cyanide7

	function formatCurrency(num) {
		if (num) {
			num = num.toString().replace(/\$|\,/g,'');
			if(isNaN(num))
				num = "0";
				sign = (num == (num = Math.abs(num)));
				num = Math.floor(num*100+0.50000000001);
				cents = num%100;
				num = Math.floor(num/100).toString();
			if(cents<10)
				cents = "0" + cents;
				for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
					num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			return (((sign)?'':'-') + num + '.' + cents);
			}
	}

	function validateInput() {
		var continue_with_submit = true;
		var error_counter = 0;
		var err_msg = "";

		frm = false;
		if (document.inputForm) {
			// if the form (element) exists;
			var frm = document.inputForm;
		}

		if (document.modifyForm) {
			// if the form (element) exists;
			var frm = document.modifyForm;
		}

		if (frm) {
			// if either the modify or input form exists;

			//var autosaveall_ok = false;
			//autosaveall_ok = autoSaveAll();

			var record_id = document.getElementById("record_id").value;
			var nsrid = document.getElementById("nsrid").value;


		
		 if (document.getElementById('mode').value == 'saveNewRecord') {
		  // only run this in input mode;
			 if ((frm.photo.value == "") || (frm.photo.value == null)) {
				 alert("'Your Photo:' is required.");
				 document.getElementById("form_photo").className = "highlight";
				 frm.photo.focus();
				 return false;
			 }
		 }

		 document.getElementById("form_photo").className = "normal";


			if (document.getElementById('first_name')) {
				continue_with_submit = test_for_required(document.getElementById('first_name').value, 'first_name', 1); 		
				if (!continue_with_submit) { 
					err_msg += "'First Name' is required.\n ";
					error_counter++;
				}
			} else {
				alert('first_name does not exist, and cannot be validated.');
			}
				
			
			if (document.getElementById('last_name')) {
				continue_with_submit = test_for_required(document.getElementById('last_name').value, 'last_name', 1); 		
				if (!continue_with_submit) { 
					err_msg += "'Last Name' is required.\n ";
					error_counter++;
				}
			} else {
				alert('last_name does not exist, and cannot be validated.');
			}
				
			
			if (document.getElementById('email_address')) {
				continue_with_submit = test_for_required(document.getElementById('email_address').value, 'email_address', 1); 		
				if (!continue_with_submit) { 
					err_msg += "'Your Email Address' is required.\n ";
					error_counter++;
				}
			} else {
				alert('email_address does not exist, and cannot be validated.');
			}
				
			
			if (document.getElementById('image_authorization[]')) {
				continue_with_submit = test_for_required(document.getElementById('image_authorization[]').value, 'image_authorization[]', 1); 		
				if (!continue_with_submit) { 
					err_msg += "'Image Authorization' is required.\n ";
					error_counter++;
				}
			} else {
				alert('image_authorization does not exist, and cannot be validated.');
			}
				
			
		
			//err_msg_len = err_msg.length;
			//new_err_msg_len = err_msg_len - 2;
			// remove the last comma;			
			//err_msg = err_msg.substr(0,new_err_msg_len);
		 			
			if ((error_counter > 0) && (error_counter < 15)) {
				alert(err_msg);
				return false;
			} else if (error_counter >= 15) {
				// if there are too many errors, the msg box may be too tall; 
				alert("There are " + error_counter + " questions that must be answered before you can submit this form.");
				return false;
			} else {
				
				toggle();
			
				//calc_values();

				var autosaveall_ok = false;
				autosaveall_ok = autoSaveAll();

				if (autosaveall_ok) {
					return true;
				} else {
					alert("an error occured. \n autosaveall_ok = " + autosaveall_ok + "\n error  #17991097.");
					return false;
				}
			}
		} // end if-frm is defined 18121097;
	} // close validate_input function;


function delay(gap) {    // thanks Rodney @ http://lists.evolt.org/pipermail/javascript/2001-June/001044.html
  var then,now; 
  then=new Date().getTime();
  now=then;
  while((now-then)<gap) {
    now=new Date().getTime();
  }
}


function calc_values(num) {

	if (num) {
		// it was provided;
	} else {
		// disregard - this is not needed;
		var num = 0;
	}


	frm = false;
	if (document.inputForm) {
		// if the form exists;
		var frm = document.inputForm;
	}

	if (document.modifyForm) {
		// if the form exists;
		var frm = document.modifyForm;
	}

	if (frm) {

	} // end if-frm;
} // close calc_values;

	// custom math formulas;


	// end custom math formulas;
//-->