function initPage() {
    clearFormFields({
        clearInputs: true,
        clearTextareas: true,
        passwordFieldText: true,
        addClassFocus: "focus",
        filterClass: "default"
    });
}
function clearFormFields(o) {
    if (o.clearInputs == null) o.clearInputs = true;
    if (o.clearTextareas == null) o.clearTextareas = true;
    if (o.passwordFieldText == null) o.passwordFieldText = false;
    if (o.addClassFocus == null) o.addClassFocus = false;
    if (!o.filter) o.filter = "default";
    if (o.clearInputs) {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if ((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
                inputs[i].valueHtml = inputs[i].value;
                inputs[i].onfocus = function () {
                    if (this.valueHtml == this.value) this.value = "";
                    if (this.fake) {
                        inputsSwap(this, this.previousSibling);
                        this.previousSibling.focus();
                    }
                    if (o.addClassFocus && !this.fake) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                inputs[i].onblur = function () {
                    if (this.value == "") {
                        this.value = this.valueHtml;
                        if (o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
                    }
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                }
                if (o.passwordFieldText && inputs[i].type == "password") {
                    var fakeInput = document.createElement("input");
                    fakeInput.type = "text";
                    fakeInput.value = inputs[i].value;
                    fakeInput.className = inputs[i].className;
                    fakeInput.fake = true;
                    inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
                    inputsSwap(inputs[i], null);
                }
            }
        }
    }
    if (o.clearTextareas) {
        var textareas = document.getElementsByTagName("textarea");
        for (var i = 0; i < textareas.length; i++) {
            if (textareas[i].className.indexOf(o.filterClass)) {
                textareas[i].valueHtml = textareas[i].value;
                textareas[i].onfocus = function () {
                    if (this.value == this.valueHtml) this.value = "";
                    if (o.addClassFocus) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                textareas[i].onblur = function () {
                    if (this.value == "") this.value = this.valueHtml;
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                }
            }
        }
    }
    function inputsSwap(el, el2) {
        if (el) el.style.display = "none";
        if (el2) el2.style.display = "inline";
    }
}
if (window.addEventListener)
    window.addEventListener("load", initPage, false);
else if (window.attachEvent)
    window.attachEvent("onload", initPage);


// TODO: Add PDF Aloqua scripts

/*jQuery(document).ready(function () {
    $("a[href$='.pdf']").each(function () {
        $(this).attr('href', '/elqNow/elqRedir.htm?ref=http://' + window.location.hostname + $(this).attr('href'));
        $(this).attr('target', '_Blank');
    });

});
*/


jQuery(document).ready(function () {
    $("a[href$='.pdf']").each(function () {
		if($(this).attr('href').toLowerCase().indexOf('http://imaginit.rand.com') == 0) {
			$(this).attr('href', '/elqNow/elqRedir.htm?ref=' + $(this).attr('href'));
		} else {
			$(this).attr('href', '/elqNow/elqRedir.htm?ref=http://' + window.location.hostname + $(this).attr('href'));
		}
        $(this).attr('target', '_Blank');
    });

});



$(function() {


// Contact BizForm

	//add newsletter copy above the newsletter checkboxes
	$("table[id$='newsletters'] tbody").prepend('<tr><td><p>Please sign me up for the iCommuniqu&eacute; eNewsletter and send me email updates on the latest news, information, and events in my industry.</p></td></tr>');

	//display state/provinces based on country
	var countryValue = $("#bizform select[id$='country'] option:selected").val();
	whichState(countryValue, 1);
	$("#bizform select[id$='country']").change(function() {
		countryValue = $("#bizform select[id$='country'] option:selected").val();
		whichState(countryValue, 0);
	});	
	$("#bizform select[id$='us_states_list'], #bizform select[id$='ca_provinces_list']").change(function() { 
		popState($("option:selected",this).val());
	});
	
	$("#bizform select[id$='Primary_Industry']").change(function() {
		whichSector($(this).val().replace(/[^\w]/ig, ''));
	});
	
	$("#bizform div#sectors>div select").change(function() {
		popSector($(this).val());
	});
	

	$('#header .header-bar .top-menu li:last-child').addClass('last');

	var links = jQuery('#sidebar .sidebar-holder .subnav li .sub_CMSListMenuUL > li a');
	

    links.each(function () {
        var link = jQuery(this);

        if (link.siblings().length > 0){
            link.append("<span class='expandable'>+</span>");
		}
    });

    links = jQuery('#sidebar .sidebar-holder #menuElem .expandable');

    links.click(function(E) {
		// prevent the default link click behavior
		E.preventDefault();
		
		var method = 'slideDown';
		var matches = null;
		
		var $this = $(this);
		
		// If the link is already expanded, slide it up and remove the class
		if( $this.parent().hasClass('sub_CMSListMenuLinkHighlighted') ) {
			method = 'slideUp';
			$this.parent().removeClass('sub_CMSListMenuLinkHighlighted');
			$this.addClass('expandable');
		} else { //  if the link is not expanded, do so
			$this.parent().addClass('sub_CMSListMenuLinkHighlighted');
			$this.removeClass('expandable').addClass('expand-btn');
		}
		
		matches = $this.parent().siblings().first(); // siblings because the trigger is on the same level as the ul that slides.
		matches[method]();
		
		return false;
	});


	
});

function whichState(c,s) {
	
	if(s == 1) {
		$("#bizform #us_states, #bizform #ca_provinces").hide();
		if(c == 'US') {
			$("#bizform #us_states").show();
		}
		if(c == 'CA') {
			$("#bizform #ca_provinces").show();	
		}
	}
	
	if(s == 0) {
		popState('');
		if(c != 'US' && c!= 'CA') {
			$("#bizform #us_states, #bizform #ca_provinces").fadeOut();
		} else {
			$("#bizform #us_states, #bizform #ca_provinces").hide();
		}
		if(c == 'US') {
			$("#bizform #us_states").fadeIn();
		}
		if(c == 'CA') {
			$("#bizform #ca_provinces").fadeIn();
		}
	}
}

function popState(x) {
	$("#bizform div[id='state'] input[id$='state']").val(x);
	if(x == '') {
		$("#bizform select[id$='us_states_list'], #bizform select[id$='ca_provinces_list']").val('');
	}		
}

function whichSector(x) {
	popSector('');
	$('#bizform div#sectors>div select').each(function(e) { $(this).val(''); });
	if(x == '') {
		$('#bizform div#sectors').fadeOut();
	} else {
		$('#bizform div#sectors div select').hide();
		$('#bizform div#sectors div select#' + x + '_sectors').show();
		$('#bizform div#sectors').fadeIn();
	}
}

function popSector(x) {
	$("#bizform div[id='Sector'] input[id$='Sector']").val(x);
	if(x == '') {
		$('#bizform div#sectors>div select').each(function(e) { $(this).val(''); });
	}
}





