var isExpanded=0;	

jQuery(document).ready( function () {  
  
//var searchresults=document.getElementById('searchresults');
//if(searchresults!=null) searchresults.style.display="none";

  // pre-processing
  jQuery(".error").hide();

  // validation
	function validate(that, url, data, callback) {
		data.name = jQuery(that).attr("name");
		jQuery.getJSON( url, data,
			function (message) {
				jQuery(that).parents('p').contents('.error').
					html(message).
					css("display", message.length == 0 ? "none" : "");
				if (!!callback) callback(message);
			}
		);
	}
	
	jQuery("input[name='postcode']").blur( function () {
		validate(this, "../mysony/ajaxcontent/qmysony_validation_generic.html", {
			mysony_id: jQuery("form#dealerlocatorForm input[name='postcode']").val()
		});
	});
	
	jQuery("input[name='dealerName']").blur( function () {
		validate(this, "../mysony/ajaxcontent/qmysony_validation_generic.html", {
			mysony_id: jQuery("input[name='dealerName']").val()
		});
	});
	
	jQuery("select[name='selectState']").blur( function () {
		validate(this, "../mysony/ajaxcontent/qmysony_validation_generic.html", {
			mysony_id: jQuery("form#dealerlocatorForm select[name='selectState']").val()
		});
	});
	
	jQuery("select[name='selectDistrict']").blur( function () {
		validate(this, "../mysony/ajaxcontent/qmysony_validation_generic.html", {
			mysony_id: jQuery("form#dealerlocatorForm select[name='selectState']").val()
		});
	});
	
	jQuery("select[name='selectMunicipality']").blur( function () {
		validate(this, "../mysony/ajaxcontent/qmysony_validation_generic.html", {
			mysony_id: jQuery("form#dealerlocatorForm select[name='selectState']").val()
		});
	});

	
});



jQuery(document).ready( function () {
		
		var criterias = ['postcode', 'address', 'dealerName','streetName','productcategory'];
								  
	jQuery("input[@name=radioType]").click(function(){
			jQuery("td.content").removeClass("active");
			jQuery(this).parents("td.content").addClass("active");
		});
	
	jQuery("td.content").click(function(){
			jQuery("td.content").removeClass("active");
			jQuery(this).addClass("active");
			var selRadio=jQuery(this).contents().find("input[@name=radioType]").val();
			for(var i=0;i<criterias.length;i++)
			{
			 if(criterias[i]!=selRadio)
			 {
			    var radioObj=document.getElementById('r'+criterias[i]);
			    if(radioObj!=null) radioObj.checked=false; 
			 }
			
			}
			var selectCriteriaError=document.getElementById('selectCriteriaError');
	        if(selectCriteriaError!=null)
	         selectCriteriaError.style.display="none";
			jQuery(this).contents().find("input[@name=radioType]").attr('checked',"checked");
		});

});


// Clicking Expand in Search Results 

jQuery(document).ready( function () {
				

		var expandText=document.getElementById('expandText');
		if(expandText!=null)
		jQuery("#searchresults p.expand a").html(expandText.innerHTML);
		var collapseText=document.getElementById('collapseText');
		if(collapseText!=null)
		jQuery("#searchresults p.expand a").html(collapseText.innerHTML);
								  
jQuery("#searchresults p.expand a").click(function(){

	if (jQuery("#searchresults").hasClass("expanded"))
		{
		isExpanded=0;
		
		
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="block";
	    }
	       
			jQuery("#searchresults").removeClass("expanded");
			var collapseText=document.getElementById('collapseText');
           if(collapseText!=null)
			jQuery(this).html(collapseText.innerHTML);
			jQuery(this).removeClass("collapse");
			jQuery("#searchresults a.pointers").css("background-position","100% 0px");
			jQuery("#searchresults .productGroups").hide();
			 jQuery("#searchresults .banners").hide();
			 jQuery("a.toggleInfo").html(moreInfoText + " &raquo;");
		}
		else
		{
		isExpanded=1;
		
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="none";
	    }
			jQuery("#searchresults").addClass("expanded");
			var expandText=document.getElementById('expandText');
            if(expandText!=null)
			jQuery(this).html(expandText.innerHTML);
			jQuery(this).addClass("collapse");
			jQuery("#searchresults a.pointers").css("background-position","100% -31px");
		}
		return false;
});


jQuery("#searchresults a.pointers").click(function(){

	if (jQuery("#searchresults").hasClass("expanded"))
		{
		isExpanded=0;
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="block";
	    }
			jQuery("#searchresults").removeClass("expanded");
			var collapseText=document.getElementById('collapseText');
           if(collapseText!=null)
			jQuery("#searchresults p.expand a").html(collapseText.innerHTML);
			jQuery("#searchresults p.expand a").removeClass("collapse");
			jQuery(this).css("background-position","100% 0px");
			jQuery("#searchresults .productGroups").hide();
			 jQuery("#searchresults .banners").hide();
			 jQuery("a.toggleInfo").html( moreInfoText + " &raquo;");
		}
		else
		{
		isExpanded=1;
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="none";
	    }
			jQuery("#searchresults").addClass("expanded");
			var expandText=document.getElementById('expandText');
            if(expandText!=null)
			jQuery("#searchresults p.expand a").html(expandText.innerHTML);
			jQuery("#searchresults p.expand a").addClass("collapse");
			jQuery(this).css("background-position","100% -31px");
		}
		return false;
												   });
});



// Toggler in Expanded View > Product Groups

jQuery(document).ready( function () {
  jQuery(".productGroups a.toggler").click(function(){
		if (jQuery(this).prev("span.hidden:visible").length !=1)
		{
			// Reset All
			jQuery(".productGroups span.hidden").hide();
			jQuery(".productGroups a.toggler").html("[+]");
			jQuery(".productGroups span.category").css("font-weight","normal");
			
			
			jQuery(this).prev("span.hidden").show();
			jQuery(this).prev("span.hidden").prev("span.category").css("font-weight","bold");
			jQuery(this).html("[&minus;]");
			return false;
		}
		else
		{
			jQuery(this).prev("span.hidden").hide();
			jQuery(this).prev("span.hidden").prev("span.category").css("font-weight","normal");
			jQuery(this).html("[+]");
			return false;
		}
		});
  });




// Toggle More iNformation Link
jQuery(document).ready( function () {
	//alert(1);
	jQuery("a.toggleInfo").click(function(){

	if (jQuery(this).prevAll(".productGroups:visible").length !=1)
	{
	jQuery(this).prevAll(".productGroups").show();
	jQuery(this).prevAll(".banners").show();
	jQuery(this).html("&laquo; " + lessInfoText);
	}
	else	 
	{
	jQuery(this).prevAll(".productGroups").hide();
	jQuery(this).prevAll(".banners").hide();
	jQuery(this).html(moreInfoText + " &raquo;");
	
	}
	return false;
	});
});



// Search Results Pagination 
// Plugin Used : http://plugins.jquery.com/project/pagination

function pageselectCallback(page_id, jq){
		jQuery('.pageStatus').text(((page_id*5)+1)+"-"+((page_id*5)+5) + " of 300 shops ");
		return false;
	}

jQuery(document).ready(function(){
// Create pagination element
jQuery(".Pagination").pagination(300, {
	items_per_page:5, 
	num_edge_entries: 0,
	num_display_entries: 0,
	
	// Callback function to Search the DB and display the results
	callback: pageselectCallback
});

});





// If No results, Remove Expand/Toggle Buttons 
function noResults()
{
    //alert('Noreults');
	jQuery("#searchresults a.pointers").hide()
	jQuery("#searchresults p.expand").hide()
}

function showResults()
{
jQuery("#searchresults a.pointers").show()
jQuery("#searchresults p.expand").show()
}

function collpaseSearchResultsPanel(){
	if (jQuery("#searchresults").hasClass("expanded"))
	{
		isExpanded=0;
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="block";
	    }
		jQuery("#searchresults").removeClass("expanded");
		var collapseText=document.getElementById('collapseText');
        if(collapseText!=null)
		jQuery("#searchresults p.expand a").html(collapseText.innerHTML);
		jQuery("#searchresults p.expand a").removeClass("collapse");
		jQuery("#searchresults a.pointers").css("background-position","100% 0px");
		jQuery("#searchresults .productGroups").hide();
		jQuery("#searchresults .banners").hide();
		jQuery("a.toggleInfo").html(moreInfoText + " &raquo;");
	}
}

function expandSearchResultsPanel(){
	if (!jQuery("#searchresults").hasClass("expanded"))
	{
		isExpanded=1;
		var images=document.getElementsByName('ballon');
	    if(images!=null)
	    {
	    	for(var i=0;i<images.length;i++) 
	    		images[i].style.display="none";
	    }
		jQuery("#searchresults").addClass("expanded");
		jQuery("#searchresults p.expand a").html("Collapse panel");
		jQuery("#searchresults p.expand a").addClass("collapse");
		jQuery("#searchresults a.pointers").css("background-position","100% -31px");
	}
}

function expandProductMoreInfo(rownum,customerNo)
{

   for(var j=0;j<10;j++)
   {
     var prdgrp=document.getElementById(j+'productGroups');
     var toggleInfo=document.getElementById(j+'toggleInfo');
    
     if(prdgrp!=null && prdgrp.id!=rownum+'productGroups')
     {     
      prdgrp.style.display='none';
      if(toggleInfo!=null)
      toggleInfo.innerHTML=moreInfoText + " &raquo;";
     }
   }

var productGroup=document.getElementById(rownum+'productGroups');
var toggleInfo=document.getElementById(rownum+'toggleInfo');
//ResetALL
for(var i=0; i<40;i++)
{
var categoryInfo=document.getElementById(customerNo+'categoryinfo'+i);
if(categoryInfo!=null) categoryInfo.style.display='none';
var toggler=document.getElementById(customerNo+'toggler'+i);
if(toggler!=null) toggler.innerHTML='[+]';
var category=document.getElementById(customerNo+'category'+i);
if(category!=null) category.style.fontWeight='normal';
}

if(productGroup.style.display=='block')
{
productGroup.style.display='none';
toggleInfo.innerHTML=moreInfoText + " &raquo;";
}
else
{
productGroup.style.display='block';
toggleInfo.innerHTML="&laquo; " + lessInfoText;
}
	
}

function expandCategoryInfo(customerNo,counter)
{



    		  
			var category=document.getElementById(customerNo+'category'+counter);
			var categoryInfo=document.getElementById(customerNo+'categoryinfo'+counter);
			var toggler=document.getElementById(customerNo+'toggler'+counter);
			
			if(categoryInfo.style.display!='inline')
			{
				for(var i=0; i<40;i++)
				{
				var categoryInfo1=document.getElementById(customerNo+'categoryinfo'+i);
				if(categoryInfo1!=null) categoryInfo1.style.display='none';
				var toggler1=document.getElementById(customerNo+'toggler'+i);
				if(toggler1!=null) toggler1.innerHTML='[+]';
				var category1=document.getElementById(customerNo+'category'+i);
				if(category1!=null) category1.style.fontWeight='normal';
				}
			}
						
			if(categoryInfo.style.display=='inline')
			{		
  		    categoryInfo.style.display='none';
  		    toggler.innerHTML='[+]';
  		    category.style.fontWeight='normal';   
  		 
  		    }
  		    else
  		    {
  		    categoryInfo.style.display='block';
  		    categoryInfo.style.display='inline';
  		    category.style.fontWeight='bold';   
  		    toggler.innerHTML='[&minus;]';
  		    }
  		    
 }
function expandCategoryInfoBubble(customerNo,counter)
{    		  
			var category=document.getElementById(customerNo+'categorybubble'+counter);
			var categoryInfo=document.getElementById(customerNo+'categoryinfobubble'+counter);
			var toggler=document.getElementById(customerNo+'togglerbubble'+counter);
				
			if(categoryInfo.style.display!='inline')
			{
				for(var i=0; i<40;i++)
				{
				var categoryInfo1=document.getElementById(customerNo+'categoryinfobubble'+i);
				if(categoryInfo1!=null) categoryInfo1.style.display='none';
				var toggler1=document.getElementById(customerNo+'togglerbubble'+i);
				if(toggler1!=null) toggler1.innerHTML='[+]';
				var category1=document.getElementById(customerNo+'categorybubble'+i);
				if(category1!=null) category1.style.fontWeight='normal';
				}
			}						
			if(categoryInfo.style.display=='inline')
			{		
  		    categoryInfo.style.display='none';
  		    toggler.innerHTML='[+]';
  		    category.style.fontWeight='normal';   
  		 
  		    }
  		    else
  		    {
  		    categoryInfo.style.display='block';
  		    categoryInfo.style.display='inline';
  		    category.style.fontWeight='bold';   
  		    toggler.innerHTML='[&minus;]';
  		    }
  		    
 }
