// See http://gr0w.com/articles/code/ajax_vs_rpc_javascript_with_livesearch_example/
var head,scrNum=0;
//var url="http://www.cpkp.co.uk/peg_sites/peg.php"; 	//url="http://localhost/web/cpkp/rpc/rpc.php";

var url = "php/peg.php";
var title = document.title;
function check_params(s,p)
{	var re=/\W+/;
	if (!s || !p)	return false;
	if (s.match(re))return false;
	// if (p.match(re))return false;
	return true;
}

function peg(site_name,peg)
{
	if (check_params(site_name,peg) == false)	{ alert("site id or peg invalid or missing"); return; }
	head=document.getElementsByTagName('head').item(0);
	script=document.createElement('script');
	script.id='scr'+scrNum;
	// Create a path to the server and params peg, site_name and script_id ( required by function removeScript(scriptid) )
	script.src=url + "?" + "peg=" + peg + "&" + "site_name=" + site_name + "&" + "script_id=" + script.id;	
	script.type='text/javascript';
	script.defer=true;
	scrNum++;
	void(head.appendChild(script));
}

function hdpeg() { peg('hd',title); }

function mdspeg(){ peg('mds',title); }
 
// alternative but not used here
function include_rpc(script_filename) { // See http://www.activewidgets.com/javascript.forum.4257.38/javascript-and-php-variables.html
    var html_doc = document.getElementsByTagName('head').item(0); 
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript'); 
    js.setAttribute('type', 'text/javascript'); 
    js.setAttribute('src', script_filename); 
    html_doc.appendChild(js); 
    return false; 
} 

function removeScript(scriptid){	// called dynamically when PHP script "calls" it
	// alert(scriptid);
	var old=document.getElementById(scriptid);
 	if(old){head.removeChild(old);}
}
