﻿// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value) {
	this.strings = new Array("");
	this.append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value) {
	if (value) {
		this.strings.push(value);
	}
}

// Clears the string buffer
StringBuilder.prototype.clear = function () {
	this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.toString = function () {
	return this.strings.join("");
}

String.prototype.contains = function(value) {
    return (this.toString().indexOf(value) > -1);
}

// swap navigation edge image by CSS class
function navEdge(id) {
    var el = document.getElementById(id);
    
    if(id == "firstMN" || id == "lastMN"){
		if(el.className.indexOf("selected") != -1) {
			el.className = el.className.replace("selected", "");
		} else { // if non-hover, make it hover
			el.className = "selected";
		}
    }
    else{
		// if hover, make it non-hover
		if(el.className.indexOf("Hover") != -1) {
			el.className = el.className.replace("Hover", "");
		} else { // if non-hover, make it hover
			el.className += "Hover";
		}
	}
}

// swap css class & hide/show divs for SharePost control
function swapIcons(el, id) {
	var thisClass = el.className;
	var formToModify = document.getElementById(id);
	
	if(thisClass == "buttonCollapsed") {
	    el.className = "buttonExpanded";
	    formToModify.style.display = "block";
	} else {
	    el.className = "buttonCollapsed";
	    formToModify.style.display = "none";
	}
}

$(function() {
	$("span.scfValidator").remove();
	$("div.scfValidationSummary").css("visibility", "hidden").css("position", "absolute");

	$(".scfRadioButtonListGeneralPanel").each(
		function() {
		    $(this).insertAfter($(this).parent());

		    var radioButtons = "";

		    $(this).children("table").children("tbody").children("tr").children("td").each(
				function() {
				    radioButtons += $(this).html();
				}
			);

		    $(this).children("table").remove();
		    $(this).append(radioButtons);

		    $(this).find("label").each(
				function() {
				    if ($(this).text() === "Yes" || $(this).text() === "No") {
				        $(this).addClass("shortLabel");
				    }
				}
			);
		}
	);

    $(".scfSectionContent > div").each(
		function() {
		    var div = $(this);
		    div.append("<div class=\"clear\"></div>");

		    if (div.attr("class").contains("Border")) {
		        div.children(".scfRequired").each(
				    function() {
				        var span = $(this);

				        if (span.text() === "") {
				            span.text("*");
				        }

				        div.find("label").prepend(span);				            
				    }
                );
		    }
		}
	);

	//removes empty option elements generated by webforms.
	$("select.scfDropList > option[value='']").remove();

	$(".scfForm").css("visibility", "visible");

	$("input.scfSubmitButton").click(function() {
		var valid = true;

		$("div.scfSectionContent > div").each(function() {
			var div = $(this);
			var label = div.children("label");
			var spanRequired = label.children("span");

			switch (div.attr("class")) {
				case "scfEmailBorder":
					if (valid) {
						valid = validateEmptyValue(div, label, spanRequired);

						if (valid) {
							valid = validateEmail(div, label, spanRequired);
						}
					}
					else {
						validateEmptyValue(div, label, spanRequired);
						validateEmail(div, label, spanRequired);
					}
					break;

				case "scfSingleLineTextBorder":
					if (valid) {
						valid = validateEmptyValue(div, label, spanRequired);
					}
					else {
						validateEmptyValue(div, label, spanRequired);
					}
					break;

				case "scfDropListBorder":
					if (valid) {
						valid = validateDropDownList(div, label, spanRequired);
					}
					else {
						validateDropDownList(div, label, spanRequired);
					}
					break;

				case "scfRadioButtonListBorder":
					validateRadioButtonList(div, label, spanRequired);
					break;

				default:
					break;
			}
		});

		return valid;
	});

	function validateEmptyValue(div, label, spanRequired) {
		if (spanRequired.text() === "*") {
			var input = div.children("div").find("input");

			if (input.val() === "") {
				label.addClass("requiredField");
				return false;
			}
			else {
				label.removeClass("requiredField");
			}
		}

		return true;
	}

	function validateDropDownList(div, label, spanRequired) {
		if (spanRequired.text() === "*") {
			var dropdownlist = div.children("div.scfDropListGeneralPanel").find("select");

			if (dropdownlist[0].selectedIndex === 0) {
				label.addClass("requiredField");
				return false;
			}
			else {
				label.removeClass("requiredField");
			}
		}

		return true;
	}

	function validateRadioButtonList(div, label, spanRequired) {
		if (spanRequired.text() === "*") {
			var radioBtnList = div.next();

			if (radioBtnList.find("input:checked").size() === 0) {
				label.addClass("requiredField");
				return false;
			}
			else {
				label.removeClass("requiredField");
			}
		}

		return true;
	}

	function validateEmail(div, label, spanRequired) {
		if (spanRequired.text() === "*") {
			var input = div.children("div").find("input");
			var emailValidator = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

			if (emailValidator.test(input.val())) {
				label.removeClass("requiredField");
			}
			else {
				label.addClass("requiredField");
				return false;
			}
		}

		return true;
	}
});