// JavaScript Document

$(function()
{
        $('#foodCategory').chainSelect('#category','http://www.kerryfresh.ie/index.php/food-companion/combo/',
        { 
                before:function (target) //before request hide the target combobox and display the loading message
                { 
                        //$("#loading").css("display","block");
	
                        $(target).css("display","none");
						
                },
                after:function (target) //after request show the target combobox and hide the loading message
                { 
                        //$("#loading").css("display","none");
	
                        $(target).css("display","inline");
						//addListEvents();
						getCategory();
						
						
						
                }
        });
		
	
        
});

function callChainSelect()
{
        $('#foodCategory').chainSelect('#category','http://www.kerryfresh.ie/index.php/food-companion/combo/',
        { 
                before:function (target) //before request hide the target combobox and display the loading message
                { 
                        //$("#loading").css("display","block");
	
                        $(target).css("display","none");
                },
                after:function (target) //after request show the target combobox and hide the loading message
                { 
                        //$("#loading").css("display","none");
	
                        $(target).css("display","inline");
						//addListEvents();
						getCategory();
						
						
						
                }
        });
		
		}

function getQueryVariable(variable) 
{ 
var query = window.location.search.substring(1); 
var vars = query.split("&"); 
for (var i=0;i<vars.length;i++) 
{ 
var pair = vars[i].split("="); 
if (pair[0] == variable) 
{ 
return pair[1]; 
} 
} 
}

$(document).ready(function() {
	
	var xDesc=getQueryVariable("srDescription");
	var xPartC=getQueryVariable("srPartCode");
	
	$("#srText").DefaultValue("Search by Keywords or Part Code...");
	
	if(xDesc!="")
	{
	$("#srText").val(xDesc);
	}
	
	if(xPartC!="")
	{
	$("#srText").val(xPartC);
	}
	
	


});
		
		function showHelperText()
		{
		$("#spClickEnter").fadeIn();
		}
		function hideHelperText()
		{
		$("#spClickEnter").fadeOut();
		}
		
		function checkForNumber(NumText)
{

	lenNum = NumText.length;

	for(i=0; i<lenNum; i++)
	{

		if(NumText.charAt(i)<'0' || NumText.charAt(i)>'9')
		{

			return false;
			break;

		}
	}

	return true;

}

function getCategory()
{

var curX =document.getElementById("foodCategory").value; 
		if(curX!==""){
	 
	 	//alert(curX);
		
		var urlLoad = 'http://www.kerryfresh.ie/index.php/food-companion/details/?category='+curX+''; 
		
		//alert(urlLoad);
		// slide up the div to hide the swap 
		$("#news-item").fadeOut(function() { 
		  // load the second template with AJAX 
		  $("#srText").val("");
		  $("#srText").DefaultValue("Search by Keywords or Part Code...");
		  
		  $("#searching").fadeIn(); 
		  $("#news-item").load(urlLoad, function() { 
			// once the load is complete, slide back down 
			$("#searching").fadeOut(); 
			$("#news-item").fadeIn(); 
			
		  }); 
		}); 
		
		// just make sure the page doesn’t reload 
		return false; 
		}

}

function searchSr(){
var srDescription;
		var srPartCode;
		var sr=document.getElementById("srText").value;
		//alert(sr);
		var urlLoad;
		var result =(checkForNumber(sr));
		if (result==true){
		urlLoad = 'http://www.kerryfresh.ie/index.php/food-companion/details/?srPartCode='+sr+'';
		//alert(urlLoad); 
		
		//alert(urlLoad);
		// slide up the div to hide the swap 
		$("#news-item").fadeOut(function() { 
		  // load the second template with AJAX 
		  $("#searching").fadeIn();
		  $("#news-item").load(urlLoad, function() { 
			// once the load is complete, slide back down 
			$("#searching").fadeOut();
			$("#spClickEnter").fadeOut(); 
			$("#news-item").fadeIn(); 
			
			
		  }); 
		}); 
		
		// just make sure the page doesn’t reload 
		document.getElementById("foodCategory").selectedIndex=0;
		document.getElementById("category").options.length=0;
		document.getElementById("category").options[0]=null;
		document.getElementById("category").options[0]=new Option("Select a Sub-Category", "", true, false);
		callChainSelect();
		return false;
		}
		
		if (result==false){
		
		 urlLoad = 'http://www.kerryfresh.ie/index.php/food-companion/details/?srDescription='+sr+''; 
		 //alert(urlLoad);
		
		//alert(urlLoad);
		// slide up the div to hide the swap 
		$("#news-item").fadeOut(function() { 
		  // load the second template with AJAX 
		  $("#searching").fadeIn();
		  $("#news-item").load(urlLoad, function() { 
			// once the load is complete, slide back down 
			$("#searching").fadeOut(); 
			$("#spClickEnter").fadeOut();
			$("#news-item").fadeIn(); 
			
		  }); 
		}); 
		
		// just make sure the page doesn’t reload 
		document.getElementById("foodCategory").selectedIndex=0;
		document.getElementById("category").options.length=0;
		document.getElementById("category").options[0]=null;
		document.getElementById("category").options[0]=new Option("Select a Sub-Category", "", true, false);
		callChainSelect();
		return false;
		}
		
}


function loadSearchSr(){
		var srDescription;
		var srPartCode;
		
		var sr=document.getElementById("srText").value;
		//alert(sr);
		var urlLoad;
		var result =(checkForNumber(sr));
		if (result==true){
			location.href = "http://www.kerryfresh.ie/index.php/food-companion/?srPartCode="+sr+""; 
		}
		
		if (result==false){
			location.href = "http://www.kerryfresh.ie/index.php/food-companion/?srDescription="+sr+""; 
		}
		window.name = "Fnord";
		
}


function getVal()
{
		var curX =document.getElementById("category").value; 
		if(curX!==""){
	 
	 	//alert(curX);
		
		var urlLoad = 'http://www.kerryfresh.ie/index.php/food-companion/details/?category='+curX+''; 
		
		//alert(urlLoad);
		// slide up the div to hide the swap 
		$("#news-item").fadeOut(function() { 
		  // load the second template with AJAX 
		  $("#searching").fadeIn();
		  $("#news-item").load(urlLoad, function() { 
			// once the load is complete, slide back down 
			$("#searching").fadeOut(); 
			$("#news-item").fadeIn(); 
			
		  }); 
		}); 
		
		// just make sure the page doesn’t reload 
		return false; 
		}
}

/*
Disable right click script II (on images)- By Dynamicdrive.com
For full source, Terms of service, and 100s DTHML scripts
Visit http://www.dynamicdrive.com
*/

var clickmessage="Right click disabled on images!"

function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
//alert(clickmessage);
return false;
}
}
}
else if (document.layers) {
if (e.which == 3) {
//alert(clickmessage);
return false;
}
}
else if (document.getElementById){
if (e.which==3&&e.target.tagName=="IMG"){
//alert(clickmessage)
return false
}
}
}

function associateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}

if (document.all)
document.onmousedown=disableclick
else if (document.getElementById)
document.onmouseup=disableclick
else if (document.layers)
associateimages()
