/* Courtesy of Alen Grakalic http://cssglobe.com/post/2494/using-form-labels-as-text-field-values */
/* Fixed a bug when the form postbacks and resets the users entry to the label again - Tim Powell =) */
this.label2value = function() {

    var inactive = "inactive";
    var active = "active";
    var focused = "focused";

    $("label").each(function() {
        var strLabelText = $(this).val();
        objTextBox = document.getElementById($(this).attr("for"));
        if (($(objTextBox).attr("type") == "text") || (objTextBox.tagName.toLowerCase() == "textarea")) {

            $(objTextBox).addClass(inactive);
            var labeltext = $(this).text();

            $(this).css("display", "none");

            alert($(objTextBox).val().substring($(objTextBox).val().indexOf(". ") + 1, $(objTextBox).val().length) + "---@---" + labeltext.substring(labeltext.indexOf(". ") + 1, labeltext.length));
            alert($(objTextBox).val().substring($(objTextBox).val().indexOf(". ") + 1, $(objTextBox).val().length) == labeltext.substring(labeltext.indexOf(". ") + 1, labeltext.length));
            alert($(objTextBox).val().substring($(objTextBox).val().indexOf(". ") + 1, $(objTextBox).val().length) == labeltext.substring(labeltext.indexOf(". ") + 2, labeltext.length));

            // if text box has nothing in then set the label text into the text box
            if ($(objTextBox).val().length == 0
                // if text box has label text in without the error number then set the label text into the text box
                  || $(objTextBox).val().substring($(objTextBox).val().indexOf(". ") + 1, $(objTextBox).val().length) == labeltext.substring(labeltext.indexOf(". ") + 1, labeltext.length)
                  || $(objTextBox).val().substring($(objTextBox).val().indexOf(". ") + 1, $(objTextBox).val().length) == labeltext.substring(labeltext.indexOf(". ") + 2, labeltext.length))
                $(objTextBox).val(labeltext);

            $(objTextBox).focus(function() {
                $(this).addClass(focused);
                $(this).removeClass(inactive);
                $(this).removeClass(active);



                //
                if ($(this).val() == labeltext
                // Check the text box text against the label with the error message in
                    || $(this).val() == labeltext.substring(labeltext.indexOf(". ") + 2, labeltext.length)
                // Check the text box text is actually equal to our error message
                //|| $(this).val().substring($(this).val().indexOf(". ") + 2, $(this).val().length) == labeltext.substring(labeltext.indexOf(". ") + 2, labeltext.length))
                    )
                    $(this).val("");
                //else alert("(" + $(this).val() + ") --- @ --- (" + labeltext.length + ")");
                //else alert("(" + $(this).val() + ") --- @ --- (" + labeltext.substring(labeltext.indexOf(". ")+2, labeltext.length) + ")");
                //if ($(this).val() == labeltext || $(objTextBox).val() == $(this).val())
                //    $(this).val("");


            });
            //            $(objTextBox).blur(function() {
            //                $(this).removeClass(focused);
            //                if ($(this).val() == "") {
            //                    $(this).val(labeltext);
            //                    $(this).addClass(inactive);
            //                } else {
            //                    $(this).addClass(active);
            //                };
            //            });
        };
    });
};

$(document).ready(function(){
	//label2value();	
});
