$(document).ready(function()
{
	$('body').append('<div id="screen"></div>');
	$('a[rel="contact"]').click(function() {
	    $('#screen, #contact').show();
		return false;
	});
	$('#screen, #contact a[rel="close"]').click(function(){
	    $('#screen, #contact').hide();
        //$('#contactform').validate().resetForm();
        clearForm($('#contactform'));
	});	
	
	$('div.get-an-invite a').hover(
	    function() { $(this).next().show(); },
	    function() { $(this).next().hide(); }
	);

	$('#contact textarea').bind('keydown keypress keyup',function() {
		if ($(this).val().length > 500 ) 
			$(this).val($(this).val().substring(0, 500));
        $('#contact .section-4 span i').text(500 - $(this).val().length);
    });
    
    $('div.main form input.text').focus(function(){ $(this).toggleClass('active'); } );
    $('div.main form input.text').blur(function(){ $(this).toggleClass('active'); } );
	
	$('div.main form textarea').focus(function(){ $(this).parent().toggleClass('active'); } );
    $('div.main form textarea').blur(function(){ $(this).parent().toggleClass('active'); } );
	
	$('div#contact form div.section-2 input').focus(function(){ $(this).toggleClass('active'); } );
    $('div#contact form div.section-2 input').blur(function(){ $(this).toggleClass('active'); } );
	
	$('div#contact form div.section-4 textarea').focus(function(){ $(this).parent().toggleClass('active'); } );
    $('div#contact form div.section-4 textarea').blur(function(){ $(this).parent().toggleClass('active'); } );
	
	$('div#login form div.section-2 input').focus(function(){ $(this).toggleClass('active'); } );
    $('div#login form div.section-2 input').blur(function(){ $(this).toggleClass('active'); } );
	
	$('div#login form div.section-5 input.text').focus(function(){ $(this).toggleClass('active'); } );
    $('div#login form div.section-5 input.text').blur(function(){ $(this).toggleClass('active'); } );
	
	$('div#login form div.section-4 a').click(function(){
		$('div#login form>div').hide();
		$('div#login form div.section-1, div#login form div.section-5').show();
	});
	
	/*
	$('input.email:eq(0)').bind('keyup keydown keypress', function(){
		if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($(this).val())))
			$(this).addClass('error');
		else 
			$(this).removeClass('error');
	});
	
	$('input.email:eq(1)').bind('keyup keydown keypress', function(){
		if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($(this).val())) || ($('div.main form input.email:eq(0)').val() != $('div.main form input.email:eq(1)').val()))
			$(this).addClass('error');
		else 
			$(this).removeClass('error');
	});
	
	$('#contact input.email').bind('keyup keydown keypress', function(){
		if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($(this).val())) || ($('div.main form input.email:eq(0)').val() != $('div.main form input.email:eq(1)').val()))
			$(this).addClass('error');
		else 
			$(this).removeClass('error');
	});	
	*/
	
	$('form#signupForm input.checkbox').prettyCheckboxes({
	    checkboxWidth: 19,
		checkboxHeight: 19,
		display: 'inline'
	});
	$('#emp-count').selectbox();
	
	$('#contactform input#phoneInp').live('keyup', function() {
        clearInputCustomError($('#contactform input#phoneInp'));
    });

	/*
	$('div.main form input.submit').click(function() {
		//$('div.main form').hide();
		//$('div#thankyou').show();
		//window.location.href += '#thankyou';
		flag = true;
		if ($('div.main form input.first-name').val().length  == 0) { $('div.main form input.first-name').addClass('error'); flag = false; } else $('div.main form input.first-name').removeClass('error');
		if ($('div.main form input.last-name').val().length  == 0) { $('div.main form input.last-name').addClass('error'); flag = false; } else $('div.main form input.last-name').removeClass('error');		
		if ($('div.main form input.email:eq(0)').val().length  < 5 || (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('div.main form input.email:eq(0)').val())))) { $('div.main form input.email:eq(0)').addClass('error'); flag = false; } else $('div.main form input.email:eq(0)').removeClass('error');
		if ($('div.main form input.email:eq(1)').val().length  < 5 || (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('div.main form input.email:eq(1)').val()))) || ($('div.main form input.email:eq(0)').val() != $('div.main form input.email:eq(1)').val())) { $('div.main form input.email:eq(1)').addClass('error'); flag = false; } else $('div.main form input.email:eq(1)').removeClass('error');
		if ($('div.main form input#tos:checked').length == 0) flag = false;
		return flag;
	});*/
	
	$('input#email, input#phone').click(function(){
		if ($(this).attr('checked'))		
			$('#not').removeAttr('checked');
	});
	
	$('input#not').click(function(){		
		if ($(this).attr('checked'))		
			$('#email, #phone').removeAttr('checked');
	});

    
    if ($('#tabs').length > 0)
    	$('#tabs').tabs();
	
	$('#contact').draggable();
	//$('#login').draggable();
    
    //ie6 bugs
    $('div.main form input.submit').hover(
        function()  { $(this).css('backgroundPosition', '0 -41px'); },
        function()  { $(this).css('backgroundPosition', '0 0'); }
    );
    
    if ($.browser.msie && $.browser.version <=6)
     {
        $('html').height('100%');
        $('body').height('100%');
     }
	 
	 contact_bg = new Image();
	 contact_bg.src = "http://cdn.pricegrid.com/www-2009-09-17/img/window.png";

});

function closeContactForm() {
    setTimeout(function() {
        $(this).next().show();
        br = $.browser.msie ? "Internet Explorer" : ($.browser.safari ? "Safari" : "Firefox");
        $('#contact form').append('<input type="hidden" name="platform" value="' + navigator.platform + '" />');
        $('#contact form').append('<input type="hidden" name="browser" value="' + br + ' ' + $.browser.version + '" />');
        $('#screen, #contact').hide();

        clearForm($("#contactform"));
    }, 2000);
    return true;
};

function validateContactForm() {
    var form = $('#contactform');

    form.validate({
        rules: {
          name: {
            required: true,
            minlength: 2,
            maxlength: 255
          },
          email:{
            required: true,
            email:true
          },
          phoneInp: {
            minlength:5
          }
        },messages: {
           name: {
              required: "Please, enter your name",
              minlength: "Min length of name is 2 symbols",
              maxlength: "Max length of name is 255 symbols"
          },
           email: {
               required: "Please, enter an email address"
           },
           phoneInp: {
               minlength: "Min length of phone number is 5"
           }
        }
    });

    //validate phone
    clearCustomErrors(form);

    if(form.find('input#phone').is(':checked') && (form.find('input#phoneInp').val().length < 1)) {
        form.find('input#phoneInp').addClass("error-custom");
        form.find('input#phoneInp').parent().append('<label class="error-custom" for="'+
                                                    form.find('input#phoneInp').attr('id')+'"> ' +
                                                    'Please, enter phone number</label>');
    }

};

function clearForm(form) {
    form.validate().resetForm();
    clearCustomErrors(form);
};

function clearCustomErrors(form) {
    form.find('input').removeClass("error-custom");
    form.find('label.error-custom').remove();
}

function clearInputCustomError(input) {
    if(input.val().length > 0) {
        input.removeClass("error-custom");
        input.parent().find('label.error-custom').remove();
    }
}

function isContactFormValid(form) {
    if(!form.valid() || form.find('input').hasClass('error-custom')){
        return false;
    }
    return true;
}

function prepareSignupForm(){
	var companyTypeValue = "";
	companyTypeValue = getStringFromCheckbox(companyTypeValue,"ecomm","eCommerce");
	companyTypeValue = getStringFromCheckbox(companyTypeValue,"manuf","Manufacturer");
	companyTypeValue = getStringFromCheckbox(companyTypeValue,"retail","Retailer");
	
	
	$("#00NA0000009F2PR").val(companyTypeValue); 
	
	var companySells = "";
	companySells = getStringFromCheckbox(companySells,"type-1","We sell the same products as our competition (MFG item #, UPC, etc)");
	companySells = getStringFromCheckbox(companySells,"type-2","We sell products that are similar to our competition but not an exact match");
	companySells = getStringFromCheckbox(companySells,"type-3","We sell unique products and want to match them manually to competitors");
	$("#00NA0000009F2U1").val(companySells);
}

function getStringFromCheckbox(companyTypeValue, checkboxId,value){
	if ($("#"+checkboxId).attr('checked')){
		if (companyTypeValue == "")
			return value;
		else
			return companyTypeValue + "," +  value;
	}else
		return companyTypeValue;
}

function hide(id){
	$("#"+id).hide();
}
