var site_root =document.location.protocol+'//'+document.domain+'/';
var mod_basket_datafeed = site_root+"admincp/modules/products/basket.php";
var mod_sensors_datafeed = site_root+"admincp/modules/products/blades_plugin.php";
var mod_purchase_datafeed = site_root+"includes/purchase/purchase.php";


function json_decode(sin)
{
	if (sin!='')
	{
		  sout = sin.replace(/\[#BCK#\]\[#SGL#\]/g,"'");
  		  sout = sout.replace(/\[#BCK#\]\[#DBL#\]/g,'"');
		  sout = sout.replace(/\[#BCK#\]/g,"\\");
		  sout = sout.replace(/\[#DBL#\]/g,'"');
		  sout = sout.replace(/\[#FWD#\]/g,"/");
		  sout = sout.replace(/\[#SGL#\]/g,"'");
	}
	else
		sout = "";

//  sout2 =  sout.replace([#SGL#]',"'");
  return sout;
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function rollclass(ref,newclass)
{
	$(ref).className = newclass;
}


function new_verify()
{
	// loads new verify image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("verify").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent browser/isp caching
		document.getElementById("verify").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Please refresh this page");
	}
}



function swapimage(img_name,img_url)
{
	$(img_name).src = img_url;
}


function projects_swap_image(img_id,img_url,img_caption,link_id,link_href)
{
	//clone the link as the image width may reduce
	Position.clone($(link_id),$('projectsImageLoading'));

	new Effect.Appear('projectsImageLoading',{duration:0.2});
	$(img_id).onload = function(){new Effect.Fade('projectsImageLoading'); $('projects_title').innerHTML=img_caption;}

	$(img_id).src=decodeURIComponent(img_url);
	$(img_id).setAttribute('alt',img_caption);
	$(link_id).href=decodeURIComponent(link_href);
	$(link_id).setAttribute('title',img_caption);
}

function products_swap_image(img_id,img_url,img_caption,link_id,link_href)
{
	//clone the link as the image width may reduce
	Position.clone($(link_id),$('productsImageLoading'));

	new Effect.Appear('productsImageLoading',{duration:0.2});
	$(img_id).onload = function(){new Effect.Fade('productsImageLoading'); $('products_title').innerHTML=img_caption;}

	$(img_id).src=decodeURIComponent(img_url);
	$(img_id).setAttribute('alt',img_caption);
	$(link_id).href=decodeURIComponent(link_href);
	$(link_id).setAttribute('title',img_caption);
}




function lowlight(obj)
{
	$(obj).className='img_grey70';
}

function highlight(obj)
{
	$(obj).className='img_nogrey';
}

function setproductImage(img, url,bigurl)
{
//	alert(decodeURIComponent(url));
	$(img).src=decodeURIComponent(url);
	$('projectImageLink').href=decodeURIComponent(bigurl);
}



function setautomenu(txt,id)
{
	$('automenutext').value=txt;
	$('automenuid').value=id;
	Element.show('clearmenu');
}

function clearmenuitem()
{
	$('automenutext').value='';
	$('automenuid').value='-1';
	Element.hide('clearmenu');
}

//called to perform a dynamic DB operation
function core_rpc(rpc,mod,pars,method)
{
	var newpars = 'rpc='+rpc+pars;

	if (typeof(method)!='undefined')
	{
		themethod = method;
		params = newpars;
	}
	else
	{
		params=null;
		themethod = 'post';
	}

		var myAjax = new Ajax.Request(
			mod, 
			{
				method:themethod,
				parameters: params,
				postBody:newpars, 
				onComplete:function(transport){
					json = transport.responseJSON;
					if (json)
					{
						if (json.status=='success')
						{	
							//alert(json.execute);
							if (json.execute)
							{
								if (json.execute!='')
								{
									eval(json_decode(json.execute));
								}
							}
							
							
						}
					}
				}
			});	
}

function showloading10(divvy)
{

	Element.update(divvy,'<center><img style="display:none" alt="loading" src="/images/layout/ajax-loader.gif" /></center>');

}

//called to perform a dynamic LOAD into DIV operation
function core_load_rpc(rpc,feed,pars,intodiv,oncomp,method)
{
	
	var newpars = 'rpc='+rpc+pars;

	if (typeof(method)!='undefined')
	{
		themethod = method;
		params = newpars;
	}
	else
	{
		params=null;
		themethod = 'post';
	}
	//showloading10(intodiv);
	
		var myAjax = new Ajax.Request(
			feed, 
			{
				method:themethod, 
				parameters: params,
				postBody:newpars, 
					onComplete:function(transport){
					json = transport.responseJSON;
					if (json)
					{
						if (json.status=='success')
						{	
							//alert(json_decode(json.content));

							Element.update($(intodiv),json_decode(json.content));
						}

						if ( (json.status=='executeonly') || (json.status=='success') )
						{																					
							if (json.execute)
							{							
								if (json.execute!='')
								{
									eval(json_decode(json.execute));
								}								
							}
							if (typeof(oncomp)!='undefined')
							{
								if (oncomp!='')
									eval(oncomp);
							}
							
						}
						else
						{
						//	alert('An Error Occured');
							//alert(transport.responseText);
						}
					}
					else
					{//alert("ERROR:"+newpars);
					}

				}
			});	
}

function row_highlight(rowid)
{
	$('row_'+rowid).className+='_over';
}

function row_normal(rowid)
{
	$('row_'+rowid).className=$('row_'+rowid).className.replace('_over', '');
}



function highlightprodtext(textlink)
{
	$(textlink).className=$(textlink).className+'_over';
}

function dehighlightprodtext(textlink)
{
	$(textlink).className=$(textlink).className.replace('_over', '');
}

