
jQuery.validator.setDefaults({
	success: "valid"
});
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

jQuery.validator.addMethod("state", function(state, element) {
  return state != "";
}, "Please select a state/province");

jQuery.validator.addMethod("notDefault", function(val, element) {
  orig = $(element).attr("default");
	return val != orig;
}, "This field is required");

function render_form_fields(scope){
	if (scope)	{ scope = scope+" "} else { scope = "" }
  // $(scope+"form").append("<input type='hidden' name='redir' value='"+window.location.href+"'/>");
	
	$(scope+"select.elqField").focus(function()	{
		$(this).addClass("active");
	}).blur(function()	{
		$(this).removeClass("active");
	});
	$(scope+"input.elqField").each(function() {
	  if ($(this).attr("default") && $(this).val() == "") {
	    $(this).val($(this).attr("default"));
	  } 
	}).focus(function()	{
		$(this).addClass("active");
		if ($(this).val() == $(this).attr("default"))	{
			$(this).val("");
		}
	}).blur(function()	{
		$(this).removeClass("active");
		if ($(this).val() == "")	{
			$(this).val($(this).attr("default"));
		}
	});
	
	$(scope+"textarea.elqField").each(function() {
	  if ($(this).attr("default") && $(this).val() == "") {
	    $(this).val($(this).attr("default"));
	  } 
	}).focus(function()	{
		$(this).addClass("active");
		if ($(this).val() == $(this).attr("default"))	{
			$(this).val("");
		}
	}).blur(function()	{
		$(this).removeClass("active");
		if ($(this).val() == "")	{
			$(this).val($(this).attr("default"));
		}
	});
}


jQuery(document).ready(function($) {
	
	$(".networking input").change(function () {
	  if ($(this).attr("checked"))  {
	    $(".networking_guest input").attr("disabled", null);
		$(".networking_guest").css({"color":"#666"});
	  } else {
 	    $(".networking_guest input").attr("disabled", "disabled");
			$(".networking_guest input").removeAttr("checked");
			$(".networking_guest").css({"color":"#cecece"});
	  }
  });
	
	var major_version =$.browser.version.substr(0,1);
	if ($.browser.msie && major_version <= "6")	{
		$("#frame").css("background", "transparent url(/images/framebg.jpg) repeat-y scroll 4px top");
		$("#tail").remove();
	}
	
	$("#sendtofriend a").facebox({opacity: 0.5});
	render_form_fields();
	$("#na_supplychain_forum").validate({
			errorPlacement: function(error, element) {
	    	if (element.attr("name") == "C_FirstName" || element.attr("name") == "C_LastName" )
	      	error.insertAfter("#name");
	    	else if (element.attr("name") == "C_State_Prov" || element.attr("name") == "C_Zip_Postal" )
		    	error.insertAfter("#state_zip");
				else
	      	error.insertAfter($(element).parent());
	    },
			groups: {
			    username: "C_FirstName C_LastName",
			    state_zip: "C_State_Prov C_Zip_Postal"
			},
			rules:	{
				"C_FirstName": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_LastName": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_Title": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_Company": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_Address1": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_City": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_State_Prov":{
					state: true,
					required: true  
				},
				"C_Zip_Postal": {
					required: true,
					notDefault: true,
					minlength: 2,
					maxlength: 250
				},
				"C_EmailAddress": {
					required: true,
					notDefault: true,
					email: true
				},
				"C_BusPhone": {
	    	  required: true,
					notDefault: true,
		      phoneUS: true
				}
			}
		});
});



