
jQuery.fn.outerHTML = function() {
return $( $('<div></div>').html(this.clone()) ).html();
}

jQuery(function() {
	   jQuery.support.placeholder = false;
	   webkit_type = document.createElement('input');
	   if('placeholder' in webkit_type) jQuery.support.placeholder = true;});
/*
 -- expand or collapse all items:
 -- add a button or a link with class="expandcollapseall collapsed" and data attribute "data-parent"=<<id of the outer container>>
 */
$('.expandcollapseall').click(function() {

	var collapsed = $(this).hasClass( "collapsed" ),
		icon = collapsed ? "minus" : "plus",
		dataParent = $(this).attr('data-parent');		

	if ( collapsed ) {
		$(dataParent).find('div.toggled_element:not(.in)').collapse('show');
		$(dataParent).find("[data-toggle='collapse']").removeClass( "collapsed" );
		$(this).removeClass( "collapsed" );
		
	}
	else {
		$(dataParent).find('div.toggled_element.in').collapse('hide');
		$(dataParent).find("[data-toggle='collapse']").addClass( "collapsed" );
		$(this).addClass( "collapsed" );
	}
});



$(function() {
	
	
	//HTML5 Placeholders for troublesome browsers (ie. IE9) 
	if(!$.support.placeholder) {
        var active = document.activeElement;
        $('textarea[placeholder]').each(function(index, element) {
         if($(this).val().length == 0) {
             $(this).html($(this).attr('id')).addClass('hasPlaceholder');
             }
      });
        $('input[placeholder], textarea[placeholder], search[placeholder]').focus(function () {
             if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                  $(this).val('').removeClass('hasPlaceholder');
             }
        }).blur(function () {
             if (($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')))) {
                  $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
                  //$(this).css('background', 'red');
             }
        });
        //$(':text').blur();
        $('input[placeholder], textarea[placeholder], search[placeholder]').trigger("blur");
        
        $(active).focus();
        $('form').submit(function () {
             $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
        });
   }

	/* blur the fields with readonly property*/
	$("input[readonly]").on("focus", function(){
		if($(this).attr("readonly")) {
			$(this).blur();
		} else {
			$(this).focus();
		}
	});
	
	/* listen for readonly property abd blur the field if the attribute is present*/
	if ($("input[type=text],input[type=password],textarea").watch) { 
		
		$("input[type=text],input[type=password],textarea").watch('readonly', function(){
			$(this).on("focus", function(){
				if($(this).attr("readonly")) {
					$(this).blur();
				} else {
					$(this).focus();
				}
			});
		});
	}

	
	/*
	--used for the forms with captcha to submit the form on pressing enter button
	*/
	
	$('.submitDefault').click(function(event) {
		var form = getDefaultForm();
		initErrorsDiv(form);
		var isvalid = false;
		
		var targetValidationFormDynamic = 'validate'+form.attr('name').substring(0, 1).toUpperCase() + form.attr('name').substring(1)+"Dynamic";
		var targetValidationForm = 'validate'+form.attr('name').substring(0, 1).toUpperCase() + form.attr('name').substring(1);			
		
		var formObject= form;
		if(form instanceof jQuery){
			formObject = form.get(0);
		}
		if (typeof window[targetValidationFormDynamic] === 'function'){
			isvalid = window[targetValidationFormDynamic](formObject);
		} else if (typeof window[targetValidationForm] === 'function'){
			isvalid = window[targetValidationForm](formObject);
		} else {
			return true;
		}
		
		if(isvalid == false){
			initFocus(form);
			initFieldsClass(form);
		}
	
		
		
		if(isvalid && checkSubmit()) {
			$(this).closest("form").submit(function(){
			   $('#changecaptchatype').attr('disabled',true); 
			});
			return true;
		} else {
			event.preventDefault();
		    event.stopPropagation();
			return false;
		}

	});

    $("form input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        	$(this).closest("form").find('.submitDefault').click();
	       	e.preventDefault();
		    e.stopPropagation();
		    return false;
            
        } else {
            return true;
        }
    });
	

	
	$('.checkSubmit').click(function() {
		return checkSubmit();
	});
	
	
	
	//css class = numericKeyOnly
	$('.numericKeyOnly').on('keypress', function(event) {
		return isNumberKey(event, event.target, 0);
	});
	
	//css class = numericAndDecimalsKeyOnly
	$('.numericAndDecimalsKeyOnly').on('keypress', function(event) {
		return isNumberKey(event, event.target, 2);
	});
	
	$("textarea").each(function(){
        //this.value = this.value.replace("AFFURL",producturl);
		var maxSize = 4000;
		var cssClass = $(this).attr('class');
		var textareaId = $(this).attr('id');
		var counter = $("#"+textareaId.replace('.','\\.')+'_rest');
		if(cssClass!=null && cssClass.search("max-size\-.*")>0){
			try{
				maxSize = parseInt(cssClass.substring(cssClass.lastIndexOf('-')).match(/[0-9]+/));
			} catch(e) {}
		}
		textDecrement($(this),maxSize,counter);
				
		$(this).keydown(function(event ) {
				textDecrement(event.target,maxSize,counter);
			});
		$(this).keyup(function(event ) {
				textDecrement(event.target,maxSize,counter);
			});
    });
	
	$(".showHideNextRow").each(function(){
		var nextRow = $(this).next().children(":first");
		$(this).find('input[type=radio]').change(function () {
			if($(this).val() == 'YES'){
				nextRow.removeClass( "js-hidden" );
			} else {
				nextRow.addClass( "js-hidden" );
			}
		});
	});
	
	
	//add collapsible behaviour to pannels:
	$("div.panel.collapsible").each(function(){
		var pannelHeading = $(this).children(".panel-heading");
		pannelHeading.addClass( "clearfix" );
		pannelHeading.find('h4').addClass( "pull-left" );
		var rightContainer = pannelHeading.find('.pull-right');
		var panelClass = $(this).attr('class').split(' ');
		var btnStyle = "info";
		
		for(var i=0;i<panelClass.length;i++){
			if(/^panel-/.test(panelClass[i])){
				btnStyle=panelClass[i].split('-')[1];
			}
		}
		
		
		if(rightContainer!=null && rightContainer.length>0){
			rightContainer.append( "<span class='hidden-print clickable btn btn-hover btn-"+btnStyle+"'><i class='fa fa-minus-square-o'></i></span>" );
		} else {
			$(this).children(".panel-heading").append( "<span class='hidden-print pull-right clickable btn btn-hover btn-"+btnStyle+"'><i class='fa fa-minus-square-o'></i></span>" );
		}
		
		if($(this).hasClass('panel-collapsed')){
			$(this).find('.panel-body').slideUp();
			var collapseBtn = $(this).find('.panel-heading span.clickable');
			collapseBtn.addClass('panel-collapsed');
			collapseBtn.find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
			
		}
		
	});
	$('.panel-heading span.clickable').on("click", function (e) {
		if ($(this).hasClass('panel-collapsed')) {
			// expand the panel
			$(this).parents('.panel').find('.panel-body').slideDown();
			$(this).removeClass('panel-collapsed');
			$(this).find('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
		}
		else {
			// collapse the panel
			$(this).parents('.panel').find('.panel-body').slideUp();
			$(this).addClass('panel-collapsed');
			$(this).find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
		}
	});
	
	var pageNav = $("#pageNav");
	if (pageNav.length) {
		
		pageNav.addClass('hidden-print');
		
		$('.container').scrollNav({
			sections: 'h4.panel-title',
			subSections: false,
			sectionElem: 'section',
			showHeadline: false,
			showTopLink: false,
			insertTarget: "#pageNav",
			insertLocation: 'appendTo',
			fixedMargin: 33,
			scrollOffset: 90,
			onRender: function() {
				var $item = $('.scroll-nav__item');
					var pageNavWidth = 0;
					$.each($item, function(){
						var link = $(this).find('a');						
						var text = $(link.attr('href')).find('h4 strong').html();
						link.attr('data-toggle','tooltip');
						link.attr('title',text);
						link.html($(link.attr('href')).find('h4 i').clone());
						pageNavWidth +=40;
					});
					var $subnav = $("#pageNav nav");
					var $placeholder = $subnav.clone().addClass("scroll-nav-placeholder");		
					$subnav.after($placeholder);
					
					$(window).resize(function () {
						var pagenav = $("#pageNav");
						var pagenav_wrapper = $("#pageNav .scroll-nav__wrapper");
						var windowWidth = $(".container").outerWidth();
						var windowHeight = $(window).outerHeight();
						
						if(windowWidth<768 && windowHeight < pagenav.outerHeight()){
							var percentage = windowHeight/pagenav.outerHeight();
							$("#pageNav a .fa").css( {"font-size":(20*percentage)+"px", "line-height":(15*percentage)+"px"} );
						} else {
							$("#pageNav a .fa").css( {"font-size":"", "line-height":""} );
						}
						
						var largeScreen = { "margin-left" : ""+((windowWidth - pageNavWidth)/2-40)+"px", "top" : ""+$('#menu .container-fluid').height()+"px"};
						var smallScreen = { "margin-left" : "", "top" : ""+(windowHeight - pagenav.outerHeight())/2+"px"};
						
						if(windowWidth>720){				
							pagenav.css( largeScreen );
							pagenav_wrapper.css( {"top":""+$('#menu .container-fluid').height()+"px"} );
							$('#pageNav a[data-toggle="tooltip"]').tooltip({
								animated: 'fade',
								placement: 'bottom',
							});
						} else {
							pagenav.css( smallScreen );
							pagenav_wrapper.css( smallScreen );
							$('#pageNav a[data-toggle="tooltip"]').tooltip('destroy');
						}
					}); 
					$(window).resize();
			
			 }
		});
		
		
		
		

	}


	$(function () {

	    $('td').each(function(i, el) {
	    	if ($(el).hasClass && $(el).hasClass( "overflow" )) {
	    		var element = $(this)
                .clone()
                .css({display: 'inline', width: 'auto', visibility: 'hidden'})
                .appendTo('body');

				if( element.width() > $(this).width() ) {
				    $(this).tooltip({
		                title: $(el).html(),
		                container: 'body'
		            });
				}
				
				element.remove();
	    	}
	        
	    });

	});
	

});



	

