/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(function(){
	var inputprevious = '';
	$('.inputfocus').focus(function() {
		inputprevious = $(this).attr('title');
		if($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});

	$('.inputfocus').blur(function() {
		if($(this).val() == '') {
			$(this).val(inputprevious);
		}
	});
});

$(function() {
	$('#send_enhance').click(function(e) {
		//var ajax_root = "http://172.20.101.62/co-phil/barnsley4me/bc/";
		var ajax_root = "http://www.barnsley4me.com/bc/"
		var fullname = '';
		var business = '';
		var email = '';
		var telephone = '';
		var filter = /^[A-Za-z0-9!#$%&'*+-\/=?^_`{|}~.]{1,64}@[A-Za-z0-9-]{1,63}.[A-Za-z0-9-.]{1,192}$/;
		var testemail = $('#email').val();
     if ($('#fullname').val() == '' || $('#fullname').val() == 'Your Name') {
       $('#fullname').css('border','1px solid red');
     } else {
     	$('#fullname').css('border','1px solid #ccc');
     	fullname = $('#fullname').val();
     }
     if ($('#email').val() == '' || $('#email').val() == 'Email Address' || !filter.test(testemail)) {
       $('#email').css('border','1px solid red');
     } else {
    		$('#email').css('border','1px solid #ccc');
    		email = $('#email').val();
     }
     if ($('#telephone').val() == '' || $('#telephone').val() == 'Telephone Number' || !isNaN($('#telephone'))) {
       $('#telephone').css('border','1px solid red');
     } else {
     	$('#telephone').css('border','1px solid #ccc');
     	telephone = $('#telephone').val();
     }
     if ($('#business').val() == '' || $('#business').val() == 'Business Name') {
       $('#business').css('border','1px solid red');
     } else {
			$('#business').css('border','1px solid #ccc');
     	business = $('#business').val();
     }
     if (fullname != '' && filter.test(testemail) && email != '' && telephone != '' && business != '') {
			$('#enhance_container').empty();
     		$('#enhance_container').css('text-align','center');
			$('#enhance_container').empty().html('<p style="text-align: center; align: center;"><img src="http://www.barnsley4me.com/bc/assets/images/lightbox-ico-loading.gif" alt="Loading" /></p>');
     		$.post(ajax_root + 'ajax/enhanceListing', $('#enhanceContact').serialize(), function(data) {
				$('#enhance_container').empty().html('<p style="text-align: center; align: center;">Thankyou, we will be in touch shortly.</p>');
			});
     }
		return false;
    e.preventDefault();
  });
 });

$(document).ready(function () {

    $("ul.tabs").tabs("div.panes > div", {

        // enable "cross-fading" effect
        fadeOutSpeed: "slow",

        // start from the beginning after the last tab
        rotate: true

    // use the slideshow plugin. It accepts its own configuration
    });
    $('button[name=newBusinessSubmit]').click(function() {


            // empty error messages

            $('ul.errors').empty();

            var form            = $(this).parents('form');
            var input_company   = $('input[name=company_name]').val();
            var input_bustype   = $('input[name=business_type]').val();
            var input_contact   = $('input[name=contact_name]').val();
            var input_address   = $('input[name=address]').val();
            var input_village   = $('input[name=village]').val();
            var input_city      = $('input[name=city]').val();
            var input_postcode  = $('input[name=postcode]').val();
            var input_email     = $('input[name=email]').val();
            var input_tel       = $('input[name=telephone]').val();
            var input_website   = $('input[name=website]').val();
            var input_upsell    = $('input[name=upsell]').val();
            //var security_answer  = $('input[name=security_answer]').val();
            var security_input   = $('input[name=security_input]').val();

            
            

            var errCount        = 0;

            // check for required fields, highlight any that are missing.

            // company name

           if(input_company.length == 0) {

               errCount = errCount + 1;
               $('input[name=company_name]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Company Name</li>');
           }

           // business type

           if(input_bustype.length == 0) {

               errCount = errCount + 1;
               $('input[name=business_type]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Business Type</li>');
           }

           // contact name

           if(input_contact.length == 0) {

               errCount = errCount + 1;
               $('input[name=contact_name]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Contact Name</li>');
           }

           // address

           if(input_address.length == 0) {

               errCount = errCount + 1;
               $('input[name=address]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Address</li>');
           }

           

           // city

           if(input_city.length == 0) {

               errCount = errCount + 1;
               $('input[name=city]').css('border','1px solid #F00');
               $('ul.errors').append('<li>City</li>');
           }

           // post code

           if(input_postcode.length == 0) {

               errCount = errCount + 1;
               $('input[name=postcode]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Post Code</li>');
           }

           // e-mail


           if(input_email.length == 0) {

               errCount = errCount + 1;
               $('input[name=email]').css('border','1px solid #F00');
               $('ul.errors').append('<li>E-mail</li>');
           }

           // telephone

           if(input_tel.length == 0) {

               errCount = errCount + 1;
               $('input[name=telephone]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Telephone</li>');
           }

		   if(security_input.length == 0) {
                errCount = errCount + 1;
                $('input[name=security_input]').css('border','1px solid #F00');
                $('ul.errors').append('<li>Security question is wrong</li>');
           }
		   
		   /*
           if(security_input != security_answer) {

                errCount = errCount + 1;
                $('input[name=security_input]').css('border','1px solid #F00');
                $('ul.errors').append('<li>Security question is wrong</li>');
           }
			*/          
           
           if(errCount == 0) {

               // hide form error message
               $('.form-errors:visible').hide();

               $(form).submit();


           } else {

                $('.form-errors:hidden').toggle();

           }

           

            

    });


    $('button[name=sendEnquiry]').click(function() {

       var form = $(this).parents('form');
       var input_name   = $('input[name=name]').val();
       var input_mail   = $('input[name=mail]').val();
       var input_telephone  = $('input[name=telephone]').val();
       var input_message   = $('textarea[name=message]').val();
       //var security_answer   = $('input[name=security_answer]').val();
       var security_input   = $('input[name=security_input]').val();
       //var security_hint = security_answer - 1;
       var errCount        = 0;

       $('ul.errors').empty();
       
       if(input_name.length == 0) {

               errCount = errCount + 1;
               $('input[name=name]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Please enter your name.</li>');
       }

       if(input_mail.length == 0) {

               errCount = errCount + 1;
               $('input[name=mail]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Please supply an e-mail address</li>');
       }

       if(input_telephone.length == 0) {

               errCount = errCount + 1;
               $('input[name=telephone]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Telephone Number</li>');
       }

       if(input_message.length == 0) {

               errCount = errCount + 1;
               $('textarea[name=message]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Message</li>');
       }
	   
       if(security_input.length == 0) {

               errCount = errCount + 1;
               $('input[name=security_input]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Security answer is wrong. <em>hint: ' + security_hint +' + 1</em></li>');
       }
	   
	   /*
       if(security_answer != security_input) {

               errCount = errCount + 1;
               $('input[name=security_input]').css('border','1px solid #F00');
               $('ul.errors').append('<li>Security answer is wrong. <em>hint: ' + security_hint +' + 1</em></li>');
       }
	   */


       if(errCount == 0) {

               // hide form error message
               $('.form-errors:visible').hide();

               $(form).submit();


           } else {

                $('.form-errors:hidden').toggle();


      }

       


    });


    
    // highlight selected input box.
    $('input[type=text], textarea').focus(function() {var input=$(this);$(input).css('border','1px solid #879ac1');});
    
    // reset focus
    $('input[type=text], textarea').blur(function() {var input=$(this);$(input).css('border','1px solid #c9c9c9');});



    $('button[name=compEntrySubmit]').click(function() {

        var form = $(this).parents('form');
        var contact      = $('input[name=contact]').val();
        var telephone    = $('input[name=telephone]').val();
        var email        = $('input[name=email]').val();
        var answer       = $('textarea[name=answer]').val();
        var sec_answer   = $('input[name=security_answer]').val();
        var sec_input    = $('input[name=security_input]').val();
        var errCount     = 0; // error counter
        
        // empty error holder prior to checking form.
        $('ul.errors').empty();

        if(contact.length == 0) {
            
            errCount = errCount + 1;
            $('input[name=contact]').css('border','1px solid #FF0000');
            $('ul.errors').append('<li>Please enter your name.</li>');
            
        }

        if(telephone.length == 0) {

            errCount = errCount + 1;
            $('input[name=telephone]').css('border','1px solid #FF0000');
            $('ul.errors').append('<li>Please enter your telephone number.</li>');

        }

        if(email.length == 0) {

            errCount = errCount + 1;
            $('input[name=email]').css('border','1px solid #FF0000');
            $('ul.errors').append('<li>Please enter your e-mail address.</li>');

        }

        if(answer.length == 0) {

            errCount = errCount + 1;
            $('input[name=answer]').css('border','1px solid #FF0000');
            $('ul.errors').append('<li>Please enter an answer.</li>');

        }

        if(sec_input != sec_answer) {

            errCount = errCount + 1;
            $('input[name=security_input]').css('border','1px solid #FF0000');
            $('ul.errors').append('<li>The security question is wrong.</li>');

        }

        // if errors found, show them.

        if(errCount == 0) {

            // hide form error message
            $('.form-errors:visible').hide();
            $(form).submit();


        } else {

            $('.form-errors:hidden').toggle();
            $('span.error-counter').text(errCount);
        }




    });



});
