// Common Js functions 

// if ie6 this will become true, otherwise it will remain undefined/false
var isRunningIE6OrBelow; 

function help_appear(popup)
{
	var object = $(popup);
	new Effect.Appear(object, { duration: 1.0, from: 0.0, to: 1.0 });
}
function hide_help (popup)
{
	var object = $(popup);
	new Effect.Fade(object, { duration: 1.0 });
}

// refresh_cart_content() - Refresh Cart Summary
function refresh_cart_content() {

	if($('refresh_cart_ajax_box')) { // if the side bar app for the cart content is showing on this page, then...
	
		new Ajax.Updater('refresh_cart_ajax_box', 'apps/sidebar_apps/cart_summar_refreshy.inc.php', { method: 'get' });
	 }
}


var myfunc;

/* Function that allows you to add onload events with ease 
 */
function addNewOnload(myfunc) {

	if(window.addEventListener)
		 window.addEventListener('load', myfunc, false);
	else if(window.attachEvent)
		 window.attachEvent('onload', myfunc);
}
//addNewOnload(alert('a'));


/* Function that allows you to add functions to different events on the go
 */
function addNewEvent(e, myfunc) {  
	 
	 var old_event = this[e];  
	 if (old_event) {  
		 this[e] = function(e) {  
			 old_event();  
			 myfunc();  
		 };  
	 }  
	 else this[e] = myfunc();  
} 
//addNewEvent(onload,alert('b'));


/* Function that tests if another function exists
 */
 function functionPreExists(myfunc) {
 
	if (typeof myfunc == "function") return true;
	else return false;
 }
//if(functionPreExists(addNewEvent)) { addNewEvent(onload,alert('b')); }	


// Highlight product 
function highlightProduct(productId,newColor,newBorder) {
	
	var newID = $(productId);
	newID.style.background = newColor;
	newID.style.borderTop = newBorder;
}


/**
 * AJAX 
 */	
function makeObject(){ 
	var x; 
	if (window.ActiveXObject) { 
		x = new ActiveXObject("Microsoft.XMLHTTP"); 
	}else if (window.XMLHttpRequest) { 
		x = new XMLHttpRequest(); 
	} 
	return x; 
} 
var request = makeObject(); 

var the_content; 
<!--// 1st //-->
function check_content(the_content){ 
	request.open('get', the_content); 
	request.onreadystatechange = parseCheck_content; 
	request.send(''); 
} 
function parseCheck_content(){ 
	if(request.readyState == 1){ 
	$('recently_used').innerHTML = "<img src='images/4-1.gif' alt='loading...' title='loading...' />"; 
	} 
	if(request.readyState == 4){ 
		var answer = request.responseText; 
		$('recently_used').innerHTML = answer; 
	}
} 


// Contact form functions  
function pleasewait()
{
	$('submit').value = 'Sending, Please wait...';
}

// Callback function that will update the response_form1 div with the response that comes from the server

function updateResponseDiv(req) 
{
	$('submit').value = 'Re-send';
	Spry.Utils.setInnerHTML('response_form1', req.xhRequest.responseText);

	whenContactFinished();
}



var mainModTitle, modaltoppart, mainInnerContent;
function openModalTop(mainModTitle, mainInnerContent) {
		
	if(mainModTitle==undefined) mainModTitle = "";
	if(mainInnerContent==undefined) mainInnerContent = "<h1> TEST </h1>";
	
	// Init New Modal Window
	modaltoppart = new ModalWindow({
		title	 	: mainModTitle,
		content	 	: '<div class="obj_modal_slide_top"> \
						<div class="obj_main"> \
						 '+ mainInnerContent +'\
						</div> \
						<a class="obj_modal_close" onclick="modaltoppart.close();">close</a> \
					   </div>',
		scrollTo 	: true,
		closeAfter	: 8000
	});
}

var mainModTitle, cartbox, mainInnerContent, modBoxWidth;
function openModalBox(mainModTitle, mainInnerContent, modBoxWidth) {
	
	if(mainModTitle==undefined) mainModTitle = "";
	if(mainInnerContent==undefined) mainInnerContent = "<h1> TEST </h1>";
	if(modBoxWidth==undefined) modBoxWidth = "400px";
	
	var boxContent;
	boxContent  = '<div class="obj_modal_box" style="width:'+ modBoxWidth +'">';
	boxContent += '<a class="obj_modal_close" onclick="cartbox.close();">close</a>';
	boxContent += mainInnerContent;
	boxContent += '</div>';
		
	// Init New Modal Window
	cartbox = new ModalWindow({
		title	 	: mainModTitle,
		content	 	: boxContent,
		position	: 'fixed',
		closeAfter	: 8000
	});
}

var product_name, enquireBox;
function enquireAboutProduct(product_name) {
	
	var mainModTitle = "Enquiry about: " + product_name;
	
	var boxContent;
	boxContent  = '<div class="obj_modal_box enquire_about_products">';
	boxContent += '<a class="obj_modal_close" onclick="enquireBox.close();"><img src="images/icons/cancel.png" alt="Cancel" title="Cancel" /></a>';
	boxContent += '<h2> '+ mainModTitle +' </h2>';
	boxContent += '\
	<form method="POST" onsubmit="enquireAboutProductSubmit();return false;">\
	<table width="100%" cellspacing="0" cellpadding="0">\
		<tr>\
			<td> Product Name: </td>\
			<td> <input type="text" name="enquire_product_name" id="enquire_product_name" value="'+ product_name +'" size="40" /> </td>\
		</tr>\
		<tr>\
			<td> Your Name: </td>\
			<td> <input type="text" name="enquire_your_name" id="enquire_your_name" value="" size="40" /> </td>\
		</tr>\
		<tr>\
			<td> Company Name: </td>\
			<td> <input type="text" name="enquire_company_name" id="enquire_company_name" value="" size="40" /> </td>\
		</tr>\
		<tr>\
			<td> Telephone Number: </td>\
			<td> <input type="text" name="enquire_tel_no" id="enquire_tel_no" value="" size="40" /> </td>\
		</tr>\
		<tr>\
			<td> Email Address: </td>\
			<td> <input type="text" name="enquire_email_address" id="enquire_email_address" value="" size="40" /> </td>\
		</tr>\
		<tr>\
			<td colspan="2"> Message: </td>\
		</tr>\
		<tr>\
			<td colspan="2"> <textarea name="enquire_message" id="enquire_message" cols="40" rows="4"></textarea> </td>\
		</tr>\
		<tr>\
			<td colspan="2" align="center"> <input type="submit" name="enquire_submit" id="enquire_submit" value="Submit" /> </td>\
		</tr>\
	</table>\
	';
	boxContent += '</div>';
		
	// Init New Modal Window
	enquireBox = new ModalWindow({
		title	 	: mainModTitle,
		content	 	: boxContent,
		position	: 'fixed'
	});
}

function enquireAboutProductSubmit() {
	
	if($('enquire_product_name')) {
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if($('enquire_product_name').value=='') {
			alert('Please enter a product name!');
		}
		else if($('enquire_your_name').value=='') {
			alert('Please enter a your own name!');
		}
		else if($('enquire_tel_no').value=='') {
			alert('Please enter a telephone number.');
		}
		else if($('enquire_email_address').value=='') {
			alert('Please enter an email address.');
		}
		else if($('enquire_message').value=='') {
			alert('Please enter your enquiry text.');
		}
		
		else if(reg.test($('enquire_email_address').value) == false) {
			alert('Please enter a valid email address.');
		}
		
		else {
		new Ajax.Request('includes/enquire_about_product.inc.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     product_name: $('enquire_product_name').value,
					     your_name: $('enquire_your_name').value,
					     company_name: $('enquire_company_name').value,
					     tel_no: $('enquire_tel_no').value,
					     email_address: $('enquire_email_address').value,
					     message: $('enquire_message').value
					    },		 
			onLoading: function(){				
			  $('enquire_submit').value = "Sending...";
			}, 
			onSuccess: function(transport){			  
			  var response = transport.responseText || "no response text";
			  $('enquire_submit').value = response;			 
			  // finished and send the user to the thanks page
			  enquireBox.close(); 
			  document.location = "thanks.php";			  
			},
			onFailure: function(){ 
			  $('enquire_submit').value = "Failed to send enquiry, please try again";
			  enquireBox.close();
		   }
		});
		}
	}
}


/**
 * Affiliates Links Modal Functions 
 */

// Affiliates Links , bring up the charitys picker and then forward the user on to the other site. 
var product_id, pickerBox; 
function affiliate_link_charity_picker(product_id) {
	
	// bring in the charity picker
	new Ajax.Request('includes/affiliate_link/charity_picker.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     product_id: product_id
					    },		 
			onLoading: function(){			
				//alert('loading...');	
			}, 
			onSuccess: function(transport){			  
			  var response = transport.responseText || "no response text";
			  	
				// Init New Modal Window
				pickerBox = new ModalWindow({
					content	 	: response,
					scrollTo 	: true
				});
			},
			onFailure: function(){ 
				alert('Error, please contact support!');
		   }
		});
}

// if the charity/school is not found...
var linkURL;
function affiliateCharityNotFound(linkURL) {
	
	
	// bring in the charity picker
	new Ajax.Request('includes/affiliate_link/request_new_school.inc.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     search_term: $('search_term_input').value,
						 site_url: linkURL
					    },		 
			onLoading: function(){	
				$('charity_picker_search').innerHTML = '<img src="images/ajax-loader.gif" alt="Loading..." title="Loading..." />Loading...';
			}, 
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('charity_picker_search').innerHTML = response;
			  
			  // Now redirect user to the affilate site 
			  setTimeout(function(){ window.location = linkURL; }, 1200);
			  
			},
			onFailure: function(){ 	
			  $('charity_picker_search').innerHTML = 'Error, please contact support!';
		   }
		});

}

// if the charity/school is not found...
function affiliateCharitySearch(linkURL) {
	 
	// bring in the charity picker
	new Ajax.Request('includes/affiliate_link/loop_all_charitys.inc.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     search_term: $('search_term_input').value,
						 site_url: linkURL
					    },		 
			onLoading: function(){	
				$('charity_picker_search').innerHTML = '<img src="images/ajax-loader.gif" alt="Loading..." title="Loading..." />Loading...';
			}, 
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('charity_picker_search').innerHTML = response;
			  Effect.Highlight('charity_picker_search');
			  
			},
			onFailure: function(){ 	
			  $('charity_picker_search').innerHTML = 'Error, please contact support!';
		   }
		});

}

/**
* Set Homepage Link plugin
*
* Copyright (c) 2009 Bibby Chung
* Blog: http://bibby.be
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* just for ie and firefox
*
* @example 
* $j('#Button1').click(function() {
*  $j.setHomepage('http://google.com');
* });     
*                    
* or set an elements onClick to... 
* $j.setHomepage(window.location);
*/
                
jQuery.extend({
setHomepage: function(url) {
if (document.all) {
 document.body.style.behavior = 'url(#default#homepage)';
 document.body.setHomePage(url);
}
else if (window.sidebar) {
 if (window.netscape) {
  try {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  }
  catch (e) {
   var strTemp = '';       
   strTemp += " Your browser does not allow us to do this automatically. ";
   strTemp += " To do so manually try going to: Tools -> Options -> Homepage";
   strTemp += " Or to enable us to do so, please enter about:config in your address line,";
   strTemp += " and change the value of signed.applets.codebase_principal_support to true";
   alert(strTemp);
  }
 }
 var prefs = Components.classes['@mozilla.org/preferences-service;1']
     .getService(Components.interfaces.nsIPrefBranch);
 prefs.setCharPref('browser.startup.homepage', url);
}
}
});



var menuSliderId = "bottom_menu_slider";
var menuSliderBackId = "bottom_menu_slider_back";

function open_bottom_menu() {	
	
	new Effect.SlideUp(menuSliderId, { 
		duration: 0.9, 
		scaleFrom: 0, 
		scaleTo: 100, 
		scaleContent:true, 
		beforeStart: function() { // on start of this effect
			new Effect.Opacity(menuSliderId, { from: 0, to: 1.1, duration: 0.6 });
		}, 
		afterFinish: function() { // on finish of this effect
			$(menuSliderId).style.display = 'block';
		} 
	});
	
	new Effect.SlideUp(menuSliderBackId, { 
		duration: 0.9, 
		scaleFrom: 0, 
		scaleTo: 100, 
		scaleContent:true, 
		beforeStart: function() { // on start of this effect
			new Effect.Opacity(menuSliderBackId, { from: 0, to: 0.9, duration: 0.9 });
		}, 
		afterFinish: function() {// on finish of this effect
			$(menuSliderBackId).style.display = 'block';
		} 
	});
	
}

function close_bottom_menu() {	
	
	new Effect.SlideDown(menuSliderId, { 
		duration: 0.9, 
		scaleFrom: 100, 
		scaleTo: 0, 
		scaleContent:true, 
		beforeStart: function() { // on start of this effect
			new Effect.Opacity(menuSliderId, { from: 1.0, to: 0, duration: 0.6 });
		}, 
		afterFinish: function() { // on finish of this effect
			$(menuSliderId).style.display = 'none';
		} 
	});
	
	new Effect.SlideUp(menuSliderBackId, { 
		duration: 0.9, 
		scaleFrom: 100, 
		scaleTo: 0, 
		scaleContent:true, 
		beforeStart: function() { // on start of this effect
			new Effect.Opacity(menuSliderBackId, { from: 1.0, to: 0, duration: 0.9 });
		}, 
		afterFinish: function() {// on finish of this effect
			$(menuSliderBackId).style.display = 'none';
			//document.body.removeChild($(menuSliderBackId)); 
		} 
	});	
		
}


// Open the hidden secure site box 
function openHiddenSecureBox() {

	var theUrlToRequest = "includes/request_secure_file_content.php";
	
	new Ajax.Request(theUrlToRequest,
	  {
		method:'post',
  		parameters: {
			url: document.domain, 
			limit: 12
		},
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  $('hidden_secure_box').innerHTML = response;
		  Effect.toggle('hidden_secure_box', 'appear', { duration: 0.4 });
		},
		onFailure: function(){ 
			return true;
		}
	  });
}


// Bookmark Site  
var title;
var url;
function bookmarkSite(title, url)
{

 if (window.opera && window.print) // opera
  {
	 var element = document.createElement("a");
	 element.setAttribute("href", url);
	 element.setAttribute("title", title);
	 element.setAttribute("rel", "sidebar");
	 element.click();
  } 
  else if (document.all) // ie
  {
	 window.external.AddFavorite(url, title);
  } 
  else if (window.sidebar) // firefox
  {
	 window.sidebar.addPanel(title, url, "");
  }
}

function toggleAddThis() {

	Effect.toggle('faves_box_hidden', 'blind', { duration: 0.4 }); return false;
}
	
	
var the_new_content;
function change_faq_content(the_new_content){ 
	
var this_new_content = 'includes/site_construct/expert_system.inc.php'+the_new_content;
	
	new Ajax.Request(this_new_content, {
		method:'get',
		onLoading: function(){
			//$('return_ajax_content').innerHTML = '<img src="images/ajax-loader.gif"/>'; 
		},
		onSuccess: function(transport){
		 	var response = transport.responseText || "no response text";
			$('return_ajax_content').innerHTML = response;
		},
		onFailure: function(){ 
			// alert('Something went wrong...');
		}
	  });

} 


/**
* Refreshing function for all png capture images 
*/
var epci, elImg;
function refresh_php_png_img(elImg) {
	
	// if specific img defined, just refresh that one,
	if(elImg != undefined) {
		var queryMark = "?";
		if(elImg.src.indexOf("?")!=-1) queryMark = "&";
		elImg.src = elImg.src + queryMark + 'additional=' + Math.random();
	} 
	// otherwise refresh them all to be sure 
	else {
		$$('img.png_capture_img').each(function(epci) {
						
			//var newImage = document.createElement("img"); 
			var newImage = new Image();
			var queryMark = "?";
			if(epci.src.indexOf("?")!=-1) queryMark = "&";
			newImage.src = epci.src + queryMark + 'additional=' + Math.random();
			newImage.onclick = function() { refresh_php_png_img(this); };
			newImage.className = "png_capture_img";
			newImage.title = "Please type in the value of this image";
			newImage.alt = newImage.title;
			newImage.align = "middle";
			newImage.width = "75";
			newImage.height = "35";
			epci.parentNode.replaceChild(newImage, epci); 
			//alert('src = ' + newImage.src);
			//epci.src = epci.src;
		});
	}
}


// Init canvas text 
if(!functionPreExists('initCanvas')) { 
	function initCanvas(canvas) {
		if (window.G_vmlCanvasManager && window.attachEvent && !window.opera) {
		  canvas = window.G_vmlCanvasManager.initElement(canvas);
		}
		return canvas;
	}
}



// Toggle Category 
var obj;
var subObj;
var parentObj;
function toggleCategory(obj) {

	parentObj = $(obj.id).up(0);
	subObj = parentObj.down('div');

	if(parentObj.className != 'current') {
		//subObj.style.display = 'block';
		Effect.toggle(subObj.id,'slide', {
			duration: 0.8,
			afterFinish: function() {
				parentObj.addClassName('current');
				obj.innerHTML = '&laquo;';//'<img src="images/icons/delete.png" alt="Close" />'; 
				obj.style.fontSize = '18px';
			}
		});
	}
	else {
		//subObj.style.display = ''; 
		Effect.toggle(subObj.id,'slide', {
			duration: 0.8,
			afterFinish: function() {
				parentObj.removeClassName('current');
				obj.innerHTML = '&raquo;';//'<img src="images/icons/add.png" alt="Open" />';
				obj.style.fontSize = '18px';//'24px';
			}
		});
	}
	
	//if(parentObj.up(0).tagName=='LI');
	if(parentObj.up(0).up(0).up(0).tagName=='LI') {
		
		setTimeout(function(){ 
			var above_span = parentObj.up(0).up(0).up(0).down('span');
			var above_div = parentObj.up(0).up(0).up(0).down('div');
			
			if(above_div.style.display=='none') {
				toggleCategory( above_span ); 
			}
		}, 1000);
	}
}



// Insert a flash file 
var myFlashVideo = {}; // Flash video config object
function insertFlashObject(myFlashVideo) {
	
  var randNum = Math.floor(Math.random()*999);
  
  myFlashVideo.elementId = "myFlashSliderContent_" + randNum;
  myFlashVideo.flashVersion = "9.0.0";
  myFlashVideo.params = { // Flash object params
    menu: "false",
    play: "true",
    wmode: "transparent",
    bgcolor: "#000000",
    allowfullscreen: "false",
    allowscriptaccess: "sameDomain"
  };
  myFlashVideo.attributes = { // Flash object attributes
    id: "myDynamicContent_" + randNum, name: "myDynamicContent_" + randNum
  };
    
  document.write('<div id="myFlashSliderContent_'+randNum+'"><!-- If flash is not installed... -->\
  <a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>\
</div>');
  // Embed the flash into the page
  return swfobject.embedSWF(myFlashVideo.src, myFlashVideo.elementId, myFlashVideo.width, myFlashVideo.height, myFlashVideo.flashVersion,"expressInstall.swf", myFlashVideo.flashvars, myFlashVideo.params, myFlashVideo.attributes);	
}

function popupFancyBoxImage(flashObjSrc) {
   $j.fancybox({ 'title' : '', 'href' : flashObjSrc });
}
