// Supplemental UL.com Javascript

// Special select boxes
$(document).ready(function () {
    // Set special class to body on main landing page (to override gray background)
    if ($("#main").hasClass("landing")) {
        $("body").addClass("landing");
    }

    // Fancy select boxes
    $("#searchBarSite").selectbox();
    $("#searchBarSite_container > ul li:first").addClass("firstOption");
    $("#searchBarSite_container").append('<div class="selectbox-wrapper-bottom"></div>');
    $("#searchBarSite_container.selectbox-wrapper ul, #searchBarSite_container .selectbox-wrapper-bottom").pngfix();

    // These particular boxs need this class added specifically
    $("#resultsperpage_container ul > li:first").addClass("firstOption");
    $("#subject_container ul > li:first").addClass("firstOption");
    $("#industry_container ul > li:first").addClass("firstOption");

    // Validation for contact us
    $("#contactUsForm").submit(function () {
        return validateContactUs();
    });

    // Validation for feedback form
    $("#Feedback").submit(function () {
        return validateFeedbackForm();
    });

    // Validation for MyHome login form
    $("#loginForm").submit(function () {
        return validateLoginForm();
    });

    // Clear all textareas (they are output with a space due to XSL)
    $("textarea").val("");

    // Social bookmark site links
    $("#delicious_link").attr("href", "http://del.icio.us/post?url=" + escape(document.URL) + "&title=" + escape(document.title));
    $("#digg_link").attr("href", "http://digg.com/submit?url=" + escape(document.URL).replace(/\//g, "%2F") + "&title=" + escape(document.title) + "&topic=general_sciences");
    $("#furl_link").attr("href", "http://furl.net/storeIt.jsp?t=" + escape(document.title) + "&u=" + escape(document.URL));
    $("#magnolia_link").attr("href", "http://ma.gnolia.com/bookmarklet/add?url=" + escape(document.URL) + "&title=" + escape(document.title));
    $("#reddit_link").attr("href", "http://reddit.com/submit?url=" + escape(document.URL) + "&title=" + escape(document.title));
    $("#stumbleupon_link").attr("href", "http://www.stumbleupon.com/submit?url=" + escape(document.URL) + "&title=" + escape(document.title));
    $("#technorati_link").attr("href", "http://technorati.com/cosmos/search.html?url=" + escape(document.URL));
});

// Validation function for contact us
function validateContactUs() {
    var failFlag = false;
    // Get form elements with class 'required'
    $("#contactUsForm input.required, #contactUsForm select.required, #contactUsForm textarea.required").each(function () {
        // Trim the input
        $(this).val(jQuery.trim($(this).val()));

        if ($(this).val() == "" || $(this).val() == "none") {
            // Field is blank, check to see if there is already an error label
            if ($(".error[for='" + this.id + "']").length == 0){
                // Add error label after form element, show with effect
                $("<label for='" + this.id + "' class='error'>This field is required.</label>").insertAfter(this).hide().slideDown();
            }
            failFlag = true;
        } else {
            // Field is not blank, make sure we're not showing an error label
            $(".error[for='" + this.id + "']").slideUp();
            $(".error[for='" + this.id + "']").remove();
        }
    });

    return !failFlag;
}

// Validation function for feedback form
function validateFeedbackForm() {
    var failFlag = false;
    // Get form elements
    $("input, select, textarea", $("#Feedback")).each(function () {
        // Trim the input
        $(this).val(jQuery.trim($(this).val()));

        if ($(this).hasClass("required") && $(this).val() == "") {
            // Field is blank, check to see if there is already an error label
            if ($(".error[for='" + this.id + "']").length == 0){
                // Add error label after form element, show with effect
                var errorlabel = $("<label for='" + this.id + "' class='error'>This field is required.</label>");
                
                if ($(this).parent().attr("class") == "textarea_border") {
                    errorlabel.insertAfter(".textarea_border:has(#" + this.id + ")").hide().slideDown();
                } else {
                    errorlabel.insertAfter(this).hide().slideDown();
                }
            }
            failFlag = true;
        } else if ($(this).val() && $(this).hasClass("val_email") && !$(this).val().match(/^.+@.+$/i)) {
            // Not email address, check to see if there is already an error label
            if ($(".error[for='" + this.id + "']").length == 0){
                // Add error label after form element, show with effect
                var errorlabel = $("<label for='" + this.id + "' class='error'>Please enter an email address.</label>");
                errorlabel.insertAfter(this).hide().slideDown();
            }
        } else {
            // Field is not blank, make sure we're not showing an error label
            $(".error[for='" + this.id + "']").slideUp();
            $(".error[for='" + this.id + "']").remove();
        }
    });

    return !failFlag;
}

// Validation function for MyHome login form
function validateLoginForm() {
    var failFlag = false;
    $("#loginForm input.required").each(function () {
        if ($(this).val() == "" || $(this).val() == "User Name") {
            if ($(".error[for='" + this.id + "']").length == 0){
                $("<label for='" + this.id + "' class='error'>This field is required.</label>").insertAfter(this).hide().slideDown();
            }
            failFlag = true;
        } else {
            $(".error[for='" + this.id + "']").slideUp();
            $(".error[for='" + this.id + "']").remove();
        }
    });
    return !failFlag;
}

// Show the "Email page" page in a popup window
function showEmailPage() {
    window.open("/global/eng/core/emailpage.jsp?p=" + escape(document.URL),
                "emailpage", "width=350,height=425,toolbar=no,directories=no,status=no,menubar=no,resizable=yes");
}

// Functions related to subscribe open/close divs
function showSubscribeDiv() {
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}

	if (navigator.appName == "Netscape") {
		if (qsParm["show"] != null) {
			if (qsParm["show"] == "thankyou") {
				document.getElementById("subscribe").style.display = "none";
				document.getElementById("thankyou").style.display = "visible";
				document.getElementById("error").style.display = "none";
			} else if (qsParm["show"] == "error") {
				document.getElementById("subscribe").style.display = "none";
				document.getElementById("thankyou").style.display = "none";
				document.getElementById("error").style.display = "visible";
			}
		} else {
			document.getElementById("subscribe").style.display = "visible";
			document.getElementById("thankyou").style.display = "none";
			document.getElementById("error").style.display = "none";	
		}
	} else {
		if (qsParm["show"] != null) {
			if (qsParm["show"] == "thankyou") {
				document.getElementById("subscribe").style.visibility = "hidden";
				document.getElementById("thankyou").style.visibility = "visible";
				document.getElementById("error").style.visibility = "hidden";
			} else if (qsParm["show"] == "error") {
				document.getElementById("subscribe").style.visibility = "hidden";
				document.getElementById("thankyou").style.visibility = "hidden";
				document.getElementById("error").style.display = "visible";
			}
		} else {
			document.getElementById("subscribe").style.visibility = "visible";
			document.getElementById("thankyou").style.visibility = "hidden";
			document.getElementById("error").style.visibility = "hidden";	
		}
	}
} 

