jQuery.noConflict();
(function($) {	
$(document).ready(function() {

// show/hide additional fields - you can add new fields by copying the block of code and adjusting IDs as needed
	$('#industry').change(function(){  // target field
	if(this.selectedIndex == this.length-1)  // If the last option of the target field is selected
		{
			$('#specify-industry').slideDown(); // Then show the container with ID #specify-industry
		}
		else
		{
		$('#specify-industry').slideUp();  // otherwise, keep the container hidden
		$('#other-industry').val("");  // and set the value of the #other-industry field to nothing
		}
	});
	
	$('#role').change(function(){
	if(this.selectedIndex == this.length-1)
		{
			$('#specify-role').slideDown();
		}
		else
		{
		$('#specify-role').slideUp();
		$('#other-role').val("");
		}
	});
	
	$('#state').change(function(){
	if(this.selectedIndex == this.length-1)
		{
			$('#specify-country').slideDown();
			$('#country').addClass('required');
		}
	else
		{
		$('#specify-country').slideUp();
		$('#country').val("");
		$('#country').removeClass('required');
		}
	});
// validation on submit 
$('.submit').click(function() {
	var ret_val = true;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if ($('#email').length) {var emailaddressVal = $("#email").val();}
	if ($('#phone').length) {var phoneVal = $('#phone').val().replace(/[^\d]/g, "");}
  $('.required').each(function(){
    if($(this).val().length === 0 || $(this).find("option:selected").val() == "none" || $(this).val() === $(this).prop('defaultValue')) 
	{
		$('#errormessage').removeClass('requiredmessage');
		ret_val = false;
		$(this).addClass('warning');
    }
	else //if($(this).val().length != 0)
	{
		$(this).removeClass('warning');
	}
  });

  //check existance of #email	
if ($('#email').length) {

  if(!emailReg.test(emailaddressVal))
	{
		$('#emailerrormessage').removeClass('requiredmessage');
		$('#email').addClass('warning');
		ret_val = false;
	}
	else
	{
		$('#emailerrormessage').addClass('requiredmessage');
	}
}
if ($('#phone').length) {
  if(phoneVal.length < 10)
	{
		$('#phoneerrormessage').removeClass('requiredmessage');
		$('#phone').addClass('warning');
		ret_val = false;
	}
	else
	{
		$('#phoneerrormessage').addClass('requiredmessage');
	}
}
	return ret_val;
 });


// real time validation 
$('.required').each(function() {
	var default_val = this.value;
	$(this).blur(function() {
	var ret_val = true;
    
    if($(this).val().length === 0 || $(this).find("option:selected").val() == "none" || $(this).val() == default_val) 
	{
		$('#errormessage').removeClass('requiredmessage');
		ret_val = false;
		$(this).addClass('warning');
    }
	else //if($(this).val().length != 0 || $(this).find("option:selected").val() != "none" || $(this).val() != default_val)
	{
		$(this).removeClass('warning');
	}
	return ret_val;
});
 });

$('#email').blur(function() {
    var ret_val = true;
	var emailaddressVal = $("#email").val();
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!emailReg.test(emailaddressVal))
	{
		$('#emailerrormessage').removeClass('requiredmessage');
		$('#email').addClass('warning');
		ret_val = false;
	}
	else
	{
		$('#emailerrormessage').addClass('requiredmessage');
	}
	
	return ret_val;	
});

$('#phone').blur(function() {
    var ret_val = true;
	var phoneVal = $(this).val().replace(/[^\d]/g, "");
	if(phoneVal.length < 10)
	{
		$('#phoneerrormessage').removeClass('requiredmessage');
		$('#phone').addClass('warning');
		ret_val = false;
	}
	else
	{
		$('#phoneerrormessage').addClass('requiredmessage');
	}
	
	return ret_val;	
});
 
$(':text.grayout').each(function() {
    var default_value = this.value;
    $(this).css('color', '#bcbcbc'); // this could be in the style sheet instead
    $(this).focus(function() {
        if(this.value == default_value) 
		{
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '' || $(this).val().length === 0) 
		{
            $(this).css('color', '#bcbcbc');
            this.value = default_value;
        }
    });
});
$('select.grayout').each(function() {
    var default_value = this.value;
    $(this).css('color', '#bcbcbc'); // this could be in the style sheet instead
    $(this).mousedown(function() {
        if(this.value == default_value) 
		{
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '' || $(this).val().length === 0) 
		{
            $(this).css('color', '#bcbcbc');
            this.value = default_value;
        }
    });
});
$(':text').each(function()  {
	$(this).val($(this).prop('defaultValue'));
});
$('#state').change(function(){
    var selected = $("option:selected", this);
    if(selected.parent()[0].id == "usgroup"){
        $('#loc-state').val(this.value);
    } else if(selected.parent()[0].id == "cagroup"){
        $('#loc-province').val(this.value);
    }
});

});
})(jQuery);

