// SurveySolutions/EFM Client Side Survey API
// (c) 2006 Perseus Development Corp.
// Version: 1.0.0

function PerseusSurvey() {
	
	this.Form = document.PdcSurvey;
	this.NumberDecimalSeparator = ".";	
	this.NumberGroupSeparator = ",";	
	this.NumberGroupSizes = 3;
	
	this.IsAnswered = IsAnswered;
	this.IsInteger = IsInteger;
	this.IsFloat = IsFloat;
	this.IsEmail = IsEmail;
	this.NumericTotal = NumericTotal;
	this.StringTotal = StringTotal;
	
	this.CalculateTotal = CalculateTotal;
	this.ToEnglishNumber = ToEnglishNumber;
	
	// Given a field heading, indicates if the specified essay question
	// fill in the blank topic, or choose one list/dropdown has been answered.
	function IsAnswered(heading) {
		var inputField = document.getElementById(heading);
		if (inputField) {
			switch (inputField.tagName) {
				// Fill In
				case "INPUT":
					switch(inputField.type) {
						case "text":
						case "password":
						case "hidden":
							var value = inputField.value;
							return (value != '');				
					}
					break;
				// Essay
				case "TEXTAREA":
					var value = inputField.value;
					return (value != '');	
					break;
				// Choose One List/Dropdown
				case "SELECT":
					var selectedIndex = inputField.selectedIndex;
					var value = inputField.options[selectedIndex].value;
					return (value != '0' && value != '');
					break;
			}
		}
		return false;
	}

	// Given a field heading, indicates if it's value contains a valid whole number.
	function IsInteger(heading) {
		var inputField = document.getElementById(heading);
		if (inputField) {
			var text = inputField.value;
			// If number contains both a group seperator and a decimal seperator
			// We can determine if they entered the number for the wrong region.
			var groupSep = text.indexOf(this.NumberGroupSeparator);
			// If seperator is ascii 160 space, and was not found, also search for ascii 32 space
			if (groupSep == -1 && this.NumberGroupSeparator == "\u00A0") {
				groupSep = text.indexOf("\ ");
			}			
			var decimalSep = text.indexOf(this.NumberDecimalSeparator);
			if (groupSep != -1 && decimalSep != -1 && (groupSep > decimalSep)) return false;
			var number = this.ToEnglishNumber(text);
			if (number.match(/^-?\d+$/)) return true;
		}
		return false;
	}
	
	// Given a field heading, indicates if it's value contains a valid real number.
	function IsFloat(heading) {
		var inputField = document.getElementById(heading);
		if (inputField) {
			var text = inputField.value;
			// If number contains both a group seperator and a decimal seperator
			// We can determine if they entered the number for the wrong region.
			var groupSep = text.indexOf(this.NumberGroupSeparator);
			// If seperator is ascii 160 space, and was not found, also search for ascii 32 space
			if (groupSep == -1 && this.NumberGroupSeparator == "\u00A0") {
				groupSep = text.indexOf("\ ");
			}			
			var decimalSep = text.indexOf(this.NumberDecimalSeparator);
			if (groupSep != -1 && decimalSep != -1 && (groupSep > decimalSep)) return false;
			var number = this.ToEnglishNumber(text);
			if (number.match(/^-?\d*(\.\d+)?$/)) return true;			
		}
		return false;
	}
	
	// Given a field heading, indicates if it's value contains a valid e-mail address.
	function IsEmail(heading) {
		var inputField = document.getElementById(heading);
		if (inputField) {
			var email = inputField.value;
			if (email.match(/^[\w_\-\.]+[\%\+]?[\w_\-\.]*\@[0-9a-zA-Z\-]+\.[0-9a-zA-Z\-\.]+$/)) return true;
		}
		return false;
	}
	
	// Private - Given an array of field headings, returns their total as a number
	function CalculateTotal(headings) {
		var total = 0;
		var decimalPlaces = 0;

		for (var i = 0; i < headings.length; i++) {
			var heading = headings[i];
			var inputField = document.getElementById(heading);
			if (inputField) {
				var number = this.ToEnglishNumber(inputField.value);
				// Add leading 0 if missing
				if (number.indexOf(".") == 0) number = "0" + number;
				// Get decimal precision
				var decimalIndex = number.indexOf(".");
				if (decimalIndex != -1) {
					var decimalPlacesCount = number.length - (decimalIndex + 1);
					if (decimalPlacesCount > decimalPlaces) decimalPlaces = decimalPlacesCount;
				}
				if (number.match(/^-?\d+(\.\d+)?$/)) {
					total += parseFloat(number);
				}
			}
		}

		if (decimalPlaces > 0) total = total.toFixed(decimalPlaces);		

		return total;		
	}
	
	// Given a list of field headings, returns their total as a number
	function NumericTotal() {		
		return this.CalculateTotal(arguments);
	}
	
	
	// Given a list of field headings, returns their total as a string
	function StringTotal() {
		var total = this.CalculateTotal(arguments);
		
		var negative = (total < 0);
		
		// Convert to string
		total = total.toString();		
						
		// Insert group seperators
		var parts = total.split('.');
		var base = Math.abs(parts[0]).toString();
		var groups = [];
		while(base.length > this.NumberGroupSizes) {
			var temp = base.substr(base.length - this.NumberGroupSizes);
			groups.unshift(temp);
			base = base.substr(0, base.length - this.NumberGroupSizes);
		}
		if(base.length > 0) { groups.unshift(base); }
		base = groups.join(this.NumberGroupSeparator);
		
		// Add decimal places if any with decimal symbol
		if (parts.length > 1) {
			total = base + this.NumberDecimalSeparator + parts[1];
		} else {
			total = base;
		}

		// Add minus sign if negative value
		if (negative) total = "-" + total;
		
		return total;		
	}	
	
	// Convert the region based number to US English Format
	function ToEnglishNumber(number) {
		while (number.indexOf(this.NumberGroupSeparator) != -1)
			number = number.replace(this.NumberGroupSeparator, "");
		// If seperator is ascii 160 space, also strip out ascii 32 space
		if (this.NumberGroupSeparator == "\u00A0") {
			while (number.indexOf("\ ") != -1)
				number = number.replace("\ ", "");
		}
		number = number.replace(this.NumberDecimalSeparator, ".");
		return number;
	}
}
