$(function(){

    $('input[placeholder]').placeholder();

	$("#fRegisterSubmit").click(function(event){
		event.preventDefault();
	    
	    if(!(doEmailValidation(false) & doNameValidation())){
	        return false;
	    }
		
		submitRegisterForm();
	});
	
	function submitRegisterForm(){	 	
		$('#fPersonal, #fSelect, #fCheck, #fBotCheck, #fError').fadeOut();
		$('#fRegisterSubmit').fadeOut( function(){
        	$('#fSending').fadeIn();
        	var data = $('#fPersonal input, #fPersonal textarea, #fSelect select, #fCheck input, #fBotCheck select').serialize();
		    $.post("/Custom/Handlers/RegisterForm.ashx", data, function(response) { registerFormResponse(response); });
		});
	};
	
 	function registerFormResponse(response){
        billformSubmitted = false;
        if(response != "ok")
        {		        
            $('#fSending').fadeOut( function (){
             $('#fThankyou').hide();
            	$('#fError').fadeIn();
            	$('#fPersonal, #fSelect, #fCheck, #fRegisterSubmit, #fBotCheck').fadeIn();
            }); 
            return;
        };

        $('#fSending').fadeOut( function (){$('#fThankyou').fadeIn();}); 
    };


	$("#fContactSubmit").click(function(event){
		event.preventDefault();
	    
	    if(!(doEmailValidation(false) & doEnquiryValidation())){
	        return false;
	    }
		
		submitContactForm();
	});
	
	function submitContactForm(){	 	
		$('#fPersonal, #fBotCheck, #fError').fadeOut();
		$('#fContactSubmit').fadeOut( function(){
        	$('#fSending').fadeIn();
        	var data = $('#fPersonal input, #fPersonal textarea, #fBotCheck select').serialize();
		    $.post("/Custom/Handlers/ContactForm.ashx", data, function(response) { contactFormResponse(response); });
		});
	};
	
 	function contactFormResponse(response){
        billformSubmitted = false;
        if(response != "ok")
        {		        
            $('#fSending').fadeOut( function (){
             $('#fThankyou').hide();
            	$('#fError').fadeIn();
            	$('#fPersonal, #fBotCheck, #fContactSubmit').fadeIn();
            }); 
            return;
        };

        $('#fSending').fadeOut( function (){$('#fThankyou').fadeIn();}); 
    };


	function validateEmailAddress(email, emailVal)
	{
		if(email == "")
		{
			return "Enter email address.";
		}
		
		if(email != emailVal)
		{
			return "Email addresses do not match.";	
		}
			
		if(!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(email))
		{
			return "Oh no! That email address doesn't look right...";
		}
			
		return "";
	}
	
	function requiredField(fieldid)
	{
	    var err = $('#' + fieldid).val() == "" ? "Required" : "";
	    return setValidationResult(err, '#' + fieldid, false);
	}
 	
 	function doNameValidation() { return requiredField('fName'); }
 	function doEnquiryValidation() { return requiredField('fEnquiry'); }
 	
 	function setValidationResult(error, icon, suppressError)
	{
		if(error == "")
		{
			$(icon).removeClass('v-pass').removeClass('v-fail');
			$(icon).addClass('v-pass');
			return true;
		}
		else
		{
			if(!suppressError)
			{
				$(icon).removeClass('v-pass').removeClass('v-fail');
				$(icon).addClass('v-fail');
			}
			return false;
		}
	}
	
 	function doEmailValidation(suppressError)
	{
		var err = validateEmailAddress($('#fEmail').val(), $('#fEmailVal').val());
		return setValidationResult(err, '#fEmail, #fEmailVal', suppressError);
	};



});

/*!
 * HTML5 Placeholder jQuery Plugin v1.8.2
 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
 * @author Mathias Bynens <http://mathiasbynens.be/>
 */
(function(f){var e='placeholder' in document.createElement('input'),a='placeholder' in document.createElement('textarea');if(e&&a){f.fn.placeholder=function(){return this};f.fn.placeholder.input=f.fn.placeholder.textarea=true}else{f.fn.placeholder=function(){return this.filter((e?'textarea':':input')+'[placeholder]').bind('focus.placeholder',b).bind('blur.placeholder',d).trigger('blur.placeholder').end()};f.fn.placeholder.input=e;f.fn.placeholder.textarea=a}function c(h){var g={},i=/^jQuery\d+$/;f.each(h.attributes,function(k,j){if(j.specified&&!i.test(j.name)){g[j.name]=j.value}});return g}function b(){var g=f(this);if(g.val()===g.attr('placeholder')&&g.hasClass('placeholder')){if(g.data('placeholder-password')){g.hide().next().attr('id',g.removeAttr('id').data('placeholder-id')).show().focus()}else{g.val('').removeClass('placeholder')}}}function d(h){var l,k=f(this),g=k,j=this.id;if(k.val()===''){if(k.is(':password')){if(!k.data('placeholder-textinput')){try{l=k.clone().attr({type:'text'})}catch(i){l=f('<input>').attr(f.extend(c(this),{type:'text'}))}l.removeAttr('name').data('placeholder-password',true).data('placeholder-id',j).bind('focus.placeholder',b);k.data('placeholder-textinput',l).data('placeholder-id',j).before(l)}k=k.removeAttr('id').hide().prev().attr('id',j).show()}k.addClass('placeholder').val(k.attr('placeholder'))}else{k.removeClass('placeholder')}}f(function(){f('form').bind('submit.placeholder',function(){var g=f('.placeholder',this).each(b);setTimeout(function(){g.each(d)},10)})});f(window).bind('unload.placeholder',function(){f('.placeholder').val('')})}(jQuery));


/*
 * jQuery Expander plugin
 * Version 0.4  (12/09/2008)
 * @requires jQuery v1.1.1+
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


(function($) {

  $.fn.expander = function(options) {

    var opts = $.extend({}, $.fn.expander.defaults, options);
    var delayedCollapse;
    return this.each(function() {
      var $this = $(this);
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
     	var cleanedTag, startTags, endTags;	
     	var allText = $this.html();
     	var startText = allText.slice(0, o.slicePoint).replace(/\w+$/,'');
     	startTags = startText.match(/<\w[^>]*>/g);
   	  if (startTags) {startText = allText.slice(0,o.slicePoint + startTags.join('').length).replace(/\w+$/,'');}
   	  
     	if (startText.lastIndexOf('<') > startText.lastIndexOf('>') ) {
     	  startText = startText.slice(0,startText.lastIndexOf('<'));
     	}
     	var endText = allText.slice(startText.length);    	  
     	// create necessary expand/collapse elements if they don't already exist
   	  if (!$('span.details', this).length) {
        // end script if text length isn't long enough.
       	if ( endText.replace(/\s+$/,'').split(' ').length < o.widow ) { return; }
       	// otherwise, continue...    
       	if (endText.indexOf('</') > -1) {
         	endTags = endText.match(/<(\/)?[^>]*>/g);
          for (var i=0; i < endTags.length; i++) {

            if (endTags[i].indexOf('</') > -1) {
              var startTag, startTagExists = false;
              for (var j=0; j < i; j++) {
                startTag = endTags[j].slice(0, endTags[j].indexOf(' ')).replace(/(\w)$/,'$1>');
                if (startTag == rSlash(endTags[i])) {
                  startTagExists = true;
                }
              }              
              if (!startTagExists) {
                startText = startText + endTags[i];
                var matched = false;
                for (var s=startTags.length - 1; s >= 0; s--) {
                  if (startTags[s].slice(0, startTags[s].indexOf(' ')).replace(/(\w)$/,'$1>') == rSlash(endTags[i]) 
                  && matched == false) {
                    cleanedTag = cleanedTag ? startTags[s] + cleanedTag : startTags[s];
                    matched = true;
                  }
                };
              }
            }
          }

          endText = cleanedTag && cleanedTag + endText || endText;
        }
     	  $this.html([
     		startText,
     		'<span class="read-more">',
     		o.expandPrefix,
       		'<a href="#">',
       		  o.expandText,
       		'</a>',
        '</span>',
     		'<span class="details">',
     		  endText,
     		'</span>'
     		].join('')
     	  );
      }
      var $thisDetails = $('span.details', this),
        $readMore = $('span.read-more', this);
   	  $thisDetails.hide();
 	    $readMore.find('a').click(function() {
 	      $readMore.hide();

 	      if (o.expandEffect === 'show' && !o.expandSpeed) {
          o.beforeExpand($this);
 	        $thisDetails.show();
          o.afterExpand($this);
          delayCollapse(o, $thisDetails);
 	      } else {
          o.beforeExpand($this);
 	        $thisDetails[o.expandEffect](o.expandSpeed, function() {
            $thisDetails.css({zoom: ''});
            o.afterExpand($this);
            delayCollapse(o, $thisDetails);
 	        });
 	      }
        return false;
 	    });
      if (o.userCollapse) {
        $this
        .find('span.details').append('<span class="re-collapse">' + o.userCollapsePrefix + '<a href="#">' + o.userCollapseText + '</a></span>');
        $this.find('span.re-collapse a').click(function() {

          clearTimeout(delayedCollapse);
          var $detailsCollapsed = $(this).parents('span.details');
          reCollapse($detailsCollapsed);
          o.onCollapse($this, true);
          return false;
        });
      }
    });
    function reCollapse(el) {
       el.hide()
        .prev('span.read-more').show();
    }
    function delayCollapse(option, $collapseEl) {
      if (option.collapseTimer) {
        delayedCollapse = setTimeout(function() {  
          reCollapse($collapseEl);
          option.onCollapse($collapseEl.parent(), false);
          },
          option.collapseTimer
        );
      }
    }
    function rSlash(rString) {
      return rString.replace(/\//,'');
    }    
  };
    // plugin defaults
  $.fn.expander.defaults = {
    slicePoint:       100,  // the number of characters at which the contents will be sliced into two parts. 
                            // Note: any tag names in the HTML that appear inside the sliced element before 
                            // the slicePoint will be counted along with the text characters.
    widow:            4,  // a threshold of sorts for whether to initially hide/collapse part of the element's contents. 
                          // If after slicing the contents in two there are fewer words in the second part than 
                          // the value set by widow, we won't bother hiding/collapsing anything.
    expandText:       'read more', // text displayed in a link instead of the hidden part of the element. 
                                      // clicking this will expand/show the hidden/collapsed text
    expandPrefix:     '&hellip; ',
    collapseTimer:    0, // number of milliseconds after text has been expanded at which to collapse the text again
    expandEffect:     'fadeIn',
    expandSpeed:      '',   // speed in milliseconds of the animation effect for expanding the text
    userCollapse:     true, // allow the user to re-collapse the expanded text.
    userCollapseText: '[collapse expanded text]',  // text to use for the link to re-collapse the text
    userCollapsePrefix: ' ',
    beforeExpand: function($thisEl) {},
    afterExpand: function($thisEl) {},
    onCollapse: function($thisEl, byUser) {}
  };
})(jQuery);
