function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=args[i+1]; if (!nm) { nm=val.name; } if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('Missing information:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}


jQuery(document).ready(function($){
   
    /* ======================================================================================================================================= */
    //Nav numbers   
   //$('.sidenav li a').prepend('1) ');
    var navcount = 0;
    
    /* Removed at request of client
    $('.sidenav ul li > a').each(function() {
        
        //alert($(this).parent().parent().c);
        
        if($(this).parent().parent().attr('class') != "children") {        
            navcount = navcount + 1;
            $(this).prepend(navcount + ") ");
        }
    });*/

   
   
    /* ======================================================================================================================================= */
    //Alphabet section    
    jQuery("#click-fname").click(function() {
        
        if(jQuery("#click-fname").attr("class") != "active") {
            jQuery('.box-fname').slideToggle('fast');
            jQuery('.box-sname').slideToggle('fast');
            jQuery("#click-fname").toggleClass('active');
            jQuery("#click-sname").toggleClass('active');
        }
        return false;        
    });
    
    jQuery("#click-sname").click(function() {
        if(jQuery("#click-sname").attr("class") != "active") {
            jQuery('.box-fname').slideToggle('fast');
            jQuery('.box-sname').slideToggle('fast');
            jQuery("#click-fname").toggleClass('active');
            jQuery("#click-sname").toggleClass('active');
        }
        return false;
    });
   
    /* ======================================================================================================================================= */
    //Sector boxes       
    jQuery(".expander").hoverIntent( function(){
    	
    	jQuery(".expander").children('.expander-results').css('display','none');
    	jQuery(this).children('.expander-results').slideDown('slow');
    	jQuery(this).children('h4').toggleClass("active");
    	
    }, function(){
    	
        jQuery(this).children('.expander-results').slideUp('slow');
        jQuery(this).children('h4').toggleClass("active");
    	
    });
    
    
    /* ======================================================================================================================================= */
    //HP News   
    jQuery(".hp-news-drop a").click(function() {
            jQuery(this).toggleClass("close");
            jQuery(this).parent().siblings('.hp-hidden-news').slideToggle('fast');        
        return false;
    });
    
    /* ======================================================================================================================================= */
    //Service team  
    /* Removed at clients request
     * jQuery(".extrateam-button a").click(function() {
            jQuery(this).toggleClass("close");
            jQuery(this).parent().siblings('.extrateam').slideToggle('fast');        
        return false;
    });*/
    
    /* ======================================================================================================================================= */
    //Social links   
    $("#social-links a").hover(function() {
        $(this).stop().animate({
                height: '45px' /* Set new height */
            }, 200); 
    
        } , function() {
        $(this).animate({
                height: '30px'
            }, 400);
    });
    
    /* ======================================================================================================================================= */
    //Quote
    
   
    
    $("#quote-zone #next1").click(function() {
        $('#quote-form-step1').submit();
        return false;
    });
    
    
    $("#quote-zone #next2").click(function(){        
        
        $("#stage3").slideDown("fast", function() {             
            //Anything need doing after slide?             
        });
        return false;
    });
    
    $('#quote-form-step1').submit(function() {
        //Make sure one area is selected...        
        if($('input:radio[name=quotetype]').is(':checked')) {
            
            
            
            //Check value
            if($('input:[name=quote-value]').val() != "" && $('input:[name=quote-postcode]').val() != "") {
                
                //Check for valid postcode
                var pcode = isValidPostcode($('input:[name=quote-postcode]').val());
                
                if(pcode == false) {
                    alert('Please ensure you enter a valid postcode.');
                    return false;
                } else {
                    return true;
                }
            } else {
                alert('Please ensure you enter the property value and postcode.');
                return false;
            }
        } else {
            alert('Please ensure you select at least one item.');
            return false;
        }
    });
    
    
    $('#quote-form').submit(function() {
        if($('input:checkbox[name=emailquote]:checked').val() && $('input:checkbox[name=callback]:not(:checked)').val()) {
            
            MM_validateForm('quote-name','Name','R','quote-email','Email address','RisEmail'); return document.MM_returnValue;
            
        } else if($('input:checkbox[name=callback]:checked').val() && $('input:checkbox[name=emailquote]:not(:checked)').val()) {            
            MM_validateForm('quote-name','Name','R','quote-number','Telephone number','R'); return document.MM_returnValue;        
        } else if($('input:checkbox[name=callback]:checked').val() && $('input:checkbox[name=emailquote]:checked').val()) {            
            MM_validateForm('quote-name','Name','R','quote-number','Telephone number','R','quote-email','Email address','RisEmail'); return document.MM_returnValue;        
        } else {
            alert('Please ensure you select contact method.');
            return false;
        }
    });
    
    /* ======================================================================================================================================= */
    //TABS
	$(".quote_tab_content").hide(); //Hide all content
	$("ul.quote_tabs li:first").addClass("active").show(); //Activate first tab
	$(".quote_tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.quote_tabs li").click(function() {
		$("ul.quote_tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".quote_tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("rel"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
       
		return false;
	});
    
    
    
    
    
    
});
