var errorColor = "rgb(250, 171, 132)";
var regColor = "#ffffff";
var reqText = "required";
var invalText = "invalid";
var selText = "select an option";
var j = 0;
	
function isValidReturn(isValid){
	if (isValid==false){
		$("#ValidMsg").show();
	} else {
		$("#ValidMsg").hide();
	}	
}

// initializing form validations
$(document).ready(function() {
    $("input:text, textarea").blur(fnValidateInput);
    $("a.mandatory").focus(fnValidateSelection);

    $("div.jqTransformSelectWrapper").find('ul li a').click(function() {
        var hasholder = false;

        if ($(this).parents("div.jqTransformSelectWrapper div").hasClass("bubbleholder")) {
            hasholder = true;
        }

        if ($(this).parent().parent().prevAll("div").find("a").hasClass("mandatory")) {
            if ($(this).html() == "- Select -") {
                if (hasholder == false) {
                    $(this).parents("div.jqTransformSelectWrapper").wrapInner("<div class='bubbleholder'></div>");
                    $(this).parent().parent().before("<div class='bubble' style='display:none;'><div>" + selText + "</div></div>");
                }

                $(this).parent().parent().prev("div.bubble").css("right", "0px");
                $(this).parent().parent().prev("div.bubble").css("top", "-29px");
                $(this).parent().parent().prev("div.bubble").animate({ opacity: "show" }, "slow");
            } else {
                $(this).parent().parent().prev("div.bubble").animate({ opacity: "hide" }, "slow");
            }
        }
    });
});


// Function to be called on form submit
function validate() {
    j = 0;
	
	$("input:text, textarea").each(fnValidateInput);

	$("a.mandatory").each(fnValidateSelection);
		
	if ($("#captchaValue").val() == "") {

		if ($("#captchaValue").parent().attr("class") != "bubbleholder") {
			$("#captchaValue").parent().wrapInner("<div class='bubbleholder'></div>");
			$("#captchaValue").after("<div class='bubble'><div style=''>" + reqText + "</div></div>");
		}

		$("#captchaValue").next("div.bubble").css("right", "39px");
    	$("#captchaValue").next("div.bubble").css("top", "8px");
		$("#captchaValue").next("div.bubble").animate({ opacity: "show" }, "slow");
		j = j + 1;
	}
	
	
	if (j!=0) {
		isValidReturn(false);
		return false;
	} else {
		return true;
	}
}


// Helper function to validate input tags
function fnValidateInput() {
    var MandClass = $(this).hasClass("mandatory");
    var MandClassE = $(this).hasClass("mandatoryE");

    if (MandClassE == true) {

        var FNameID = $(this).attr("id");

        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var address = $(this).val();

        if (reg.test(address) == false) {
            if ($(this).parent().attr("class") != "bubbleholder") {
                $(this).parent().wrapInner("<div class='bubbleholder'></div>");
                $(this).after("<div class='bubble'><div>" + invalText + "</div></div>");
            }

            var FWidth = $(this).css("width");
            $(this).next("div.bubble").css("right", "0px");
            $(this).next("div.bubble").css("top", "-29px");
            $(this).next("div.bubble").stop(true, true).animate({ opacity: "show" }, "slow");
            j = j + 1;
        } else {
            $(this).next("div.bubble").stop(true, true).animate({ opacity: "hide" }, "slow");
        }

    } else {
        if ((MandClass == true) && ($(this).val() == "")) {
            if ($(this).parent().attr("class") != "bubbleholder") {
                $(this).parent().wrapInner("<div class='bubbleholder'></div>");
                $(this).after("<div class='bubble'><div>" + reqText + "</div></div>");
            }

            var FWidth = $(this).css("width");
            $(this).next("div.bubble").css("right", "0px");
            $(this).next("div.bubble").css("top", "-29px");
            $(this).next("div.bubble").stop(true, true).animate({ opacity: "show" }, "slow");
            j = j + 1;
        } else {
            $(this).next("div.bubble").stop(true, true).animate({ opacity: "hide" }, "slow");
        }
    }
}


// Helper function for validating Selections
function fnValidateSelection() {
    if ($(this).prev("span").html() == "- Select -") {

        if ($(this).parent().parent().attr("class") != "bubbleholder") {
            $(this).parent().parent().wrapInner("<div class='bubbleholder'></div>");
            $(this).parent().after("<div class='bubble' style='display:none;'><div>" + selText + "</div></div>");
        }

        $(this).parent().next("div.bubble").css("right", "0px");
        $(this).parent().next("div.bubble").css("top", "-29px");
        $(this).parent().next("div.bubble").animate({ opacity: "show" }, "slow");
        j = j + 1;
    } else {
        $(this).parent().next("div.bubble").animate({ opacity: "hide" }, "slow");
    }
}