// mediumUtils.js
// Defines a generic container for the
// mediumUtils namespace. This namespace contains
// very generic methods and objects for re-use
// across projects. VERY basic stuff that one gets
// sick of re-writing for every project out of
// necessity.
// (c) 2006 David D'Amico



var mediumUtils = {
	
	// pass it an image object and a url for the image to
	// swap to, and this gentleman will totally swap the
	// image object's src!
	swapImage: function (swapTarget, newImageUrl) {
		//alert(swapTarget.src);
		swapTarget.src = newImageUrl;
	},
	
	
	// checks whether a given string is a valid email address.
	validateEmailString: function (emailString) {
		
		if (!(typeof(emailString) == "string"))
		{ return false; }
		
		if (mediumUtils.getTotalOccurrencesOfCharacterInString(emailString, "@") > 1
			|| emailString.indexOf("@") == -1
			|| emailString.indexOf(".") == -1) {
			return false;
		}
		
		else { return true; }
	},
	
	
	// a genericised method for checking forms for required
	// fields, validated fields (email), and length-limited
	// fields.
	checkForm: function (targetFormName, requiredFieldNames, validatedEmailFieldNames, lengthRestrictedFieldNames) {
		
		/*if (!(lengthRestrictedFieldNames)){
			alert (targetFormName + " | " + requiredFieldNames + " | " + validatedEmailFieldNames + " | " + lengthRestrictedFieldNames);
		}*/
		
		// first check that everything was passed that was supposed to be
		if (!(targetFormName && requiredFieldNames && validatedEmailFieldNames && lengthRestrictedFieldNames))
		{ return "INTERNAL ERROR: checkForm method called improperly."}
		
		// now let's try to get the form element by its id
		var targetForm = document.getElementById(targetFormName);
		
		// now check if we got anything back above
		if (!targetForm) { return "INTERNAL ERROR: there is no form corresponding to the name that was passed."; }
		else {
			
			if (requiredFieldNames != "null") {
			// if the fields specified as required are not blank
				for (n in requiredFieldNames) {
				
					// if the targeted parameter from the passed requiredFieldNames object
					// is a string, and if there is a field in targetForm with that name,
					// then check its value . If its value is NOT blank, then flow through.
					if (typeof(requiredFieldNames[n]) == "string"
						&& targetForm[requiredFieldNames[n]]) {
							if (targetForm[requiredFieldNames[n]].value == "")
							{ return (requiredFieldNames[n] + " is a required field."); }
					}
					else { return false; }
				}
			}
					
			// if the targeted parameter from the passed validatedEmailFieldNames object
			// is a string, and if there is a field in targetForm with that name,
			// then send its value to the email validation method. If it validates, then
			// flow through.
			if (validatedEmailFieldNames != "null") {
				for (n in validatedEmailFieldNames) {
					if (typeof(validatedEmailFieldNames[n]) == "string"
						&& targetForm[validatedEmailFieldNames[n]]) {
							if (!(mediumUtils.validateEmailString(targetForm[validatedEmailFieldNames[n]].value)))
							{ return "You have entered an invalid email address."; }
					}
					else { return false; }
				}
			}
			
			// next we check our length-limited fields.
			if (lengthRestrictedFieldNames != "null") {
				for (n in lengthRestrictedFieldNames) {
				
					// if this param has come in in the proper type
					if (typeof(lengthRestrictedFieldNames[n]) == "object") {
					
						// and if the param is *composed* of the proper types, and
						// if the string in position 0 corresponds to a field in the
						// target form that actually exists
						if (typeof(lengthRestrictedFieldNames[n][0]) == "string"
							&& typeof(lengthRestrictedFieldNames[n][1]) == "number"
							&& targetForm[lengthRestrictedFieldNames[n][0]]){
							
								// then check if the value of the field corresponding to
								// the passed field name is less than the passed upper
								// limit. If it is not, then flow through.
								if (targetForm[lengthRestrictedFieldNames[n][0]].value.length > lengthRestrictedFieldNames[n][1]) {
									return "Sorry, the length of \'" + lengthRestrictedFieldNames[n][0] + "\' is limited to " + lengthRestrictedFieldNames[n][1] + " characters.";
								}
							
							}
						else { return "INTERNAL ERROR: Improper value passed for lengthRestrictedFieldNames"; }
					}
					else { return "INTERNAL ERROR: Improper value passed for lengthRestrictedFieldNames"; }
				}
			}
		}
		
		return true;
	},
	
	
	// takes a string and a targetted single character for input, and
	// returns the total number of occurrences of that character in
	// the target string.
	getTotalOccurrencesOfCharacterInString: function (targetString, targetChar) {
		
		if (!(typeof(targetString) == "string") || !(typeof(targetChar) == "string"))
		{ return false; }
		
		var stringLength = targetString.length;
		var totalOccurrences = 0;
		var nextIndexOfChar, charIndexHolder;
		
		for (i = 0; i < stringLength; i++) {
			//alert("at beginning of loop, i = " + i);
			
			charIndexHolder = targetString.indexOf(targetChar, i);
			if (charIndexHolder != -1) {
				totalOccurrences++;
				nextIndexOfChar = (charIndexHolder + 1);
				i = charIndexHolder;
			}
		}
		return totalOccurrences;
	}
	
}