// JavaScript Document for all Pages on Calpol

/****************************************************
* Global Vars										*
*													*
*****************************************************/

var root_dir = "/";





/****************************************************
* Call Handlers										*
*													*
*****************************************************/


function addEventFunc(el, evtType, func, capture){
	var ret = 0;
	
	if (el.addEventFuncListener){
		ret = el.addEventFuncListener(evtType, func, capture);
	} else if (el.attachEvent){
		ret = el.attachEvent('on' + evtType, func);
	} else { el['on' + evtType] = func; }
	
	return ret;
}





/****************************************************
* Popup Window										*
*													*
*****************************************************/

function popup(url){
	var popup = window.open (url,"popup","location=0,status=0,toolbar=0,menubar=0,directories=0,scrollbars=1,resizable=0,width=300,height=400");
	popup.moveTo(0,0);	
} 


function closeWindow(){
	window.close();
} 





/****************************************************
* Browser Detection									*
*													*
*****************************************************/


function Detect() {
	
	agent 	= navigator.userAgent.toLowerCase();
						
	// detect OS
	this.isMac		= (agent.indexOf('mac') != -1);
	this.isWin		= (agent.indexOf('win') != -1);
	this.isWin2k	= (this.isWin && (agent.indexOf('nt 5') != -1));
	this.isWinSP2	= (this.isWin && (agent.indexOf('xp') != -1 || agent.indexOf('sv1') != -1));
	this.isOther	= (agent.indexOf('unix') != -1 || agent.indexOf('sunos') != -1 || agent.indexOf('bsd') != -1 || agent.indexOf('x11') != -1 || agent.indexOf('linux') != -1);
	
	// detect browser
	this.isSafari	= (agent.indexOf('safari') != -1);
	this.isSafari2 = (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
	this.isOpera	= (agent.indexOf('opera') != -1);
	this.isNN		= (agent.indexOf('netscape') != -1);
	this.isFF3		= (agent.indexOf('firefox/3') != -1);
	this.isIE		= (agent.indexOf('msie') != -1);
	this.isIE6		= (agent.indexOf('msie 6.') != -1);
	this.isIE5		= (agent.indexOf('msie 5.') != -1);
}

var browser = new Detect();




/****************************************************
* Return Objects By ClassName						*
*													*
*****************************************************/

function getElementsByClassName(strClassName, obj) {
    var ar = arguments[2] || [];
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if (re.test(obj.className)) {
        ar.push(obj);
    }
    for (var i=0; i<obj.childNodes.length; i++){
        getElementsByClassName(strClassName, obj.childNodes[i], ar);
	}
    
    return ar;
}






/****************************************************
* Grab all stylesheets								*
*													*
*****************************************************/

function getAllSheets(){
	if(!window.ScriptEngine && navigator.__ice_version){
		return document.styleSheets; 
	}
	if(document.getElementsByTagName){
		var Lt = document.getElementsByTagName('link');
		var St = document.getElementsByTagName('style');
	} else if(document.styleSheets && document.all){
		var Lt = document.all.tags('LINK'), St = document.all.tags('STYLE');
	} else { return []; }
	
	for(var x=0, os=[]; Lt[x]; x++){
		if(Lt[x].rel){ var rel = Lt[x].rel;
		} else if(Lt[x].getAttribute) { var rel = Lt[x].getAttribute('rel');
		} else { var rel = ''; }
	
		if(typeof(rel) == 'string' && rel.toLowerCase().indexOf('style') + 1){
			os[os.length] = Lt[x];
		}
	}
	
	for(var x=0; St[x]; x++){ os[os.length] = St[x]; } return os;
}


function changeStyle(){
	for(var x=0, ss=getAllSheets(); ss[x]; x++){
		if(ss[x].title){
			ss[x].disabled = true;
		}
		
		for(var y=0; y<arguments.length; y++){
			if(ss[x].title == arguments[y]){
				ss[x].disabled = false;
			}
		}
	}
	
	if(!ss.length) { alert('Your browser cannot change stylesheets'); }
}






/****************************************************
* Handle mousedown on free space, i.e. losing focus	*
* on HTML elements									*
*													*
*****************************************************/

function loseFocus(e){	
	var inputs = document.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].value == "" && inputs[i].getAttribute("busy") == 0){			
			switch(inputs[i].name){
				case "query":
					inputs[i].value = "Search";
					break;    
				case "username":
					inputs[i].value = "Username";
					break;
				case "password":					
					try {
						inputs[i].setAttribute("type", "text");
					} catch(err){
					   var new_input = document.createElement('input');
					   new_input.setAttribute('type','text');
					   new_input.setAttribute('name',inputs[i].getAttribute('name'));
					   new_input.setAttribute('id',inputs[i].getAttribute('id'));
					   new_input.setAttribute('busy',inputs[i].getAttribute('busy'));
					   inputs[i].parentNode.replaceChild(new_input,inputs[i]);
					   addEventFunc(new_input, "mousedown", updateInputs, false);
					   addEventFunc(new_input, "focus", updateInputs, false);
					   addEventFunc(new_input, "blur", inputBlur, false);
					}
					inputs[i].value = "Password";
					break;
				default:
			}
		}
	}
}





/****************************************************
* Handle input Blur									*
*													*
*****************************************************/

function inputBlur(e){	
	if (!e){ e = window.event; }
	var tgt = e.target || e.srcElement;
	
	tgt.setAttribute("busy", 0);
	loseFocus();
}





/****************************************************
* Remove Username text from login forms and change	*
* Password field to be of type password				*
*													*
*****************************************************/

function updateInputs(e){	
	if (!e){ e = window.event; }
	var tgt = e.target || e.srcElement;
	
	if(tgt.nodeName.toLowerCase() == "input"){
		
		tgt.setAttribute("fixed", 1);
		tgt.setAttribute("busy",1);
		
		if(tgt.value == "Username"){
			tgt.value = "";
		} else if(tgt.value == "Search"){
			tgt.value = "";
		} else if(tgt.value == "Password"){
			tgt.value = "";
			try {
				tgt.setAttribute("type", "password");
			} catch(err){
			   var new_input = document.createElement('input');
			   new_input.setAttribute('type','password');
			   new_input.setAttribute('name',tgt.getAttribute('name'));
			   new_input.setAttribute('id',tgt.getAttribute('id'));
			   new_input.setAttribute('busy',tgt.getAttribute('busy'));
			   tgt.parentNode.replaceChild(new_input,tgt);
			   addEventFunc(new_input, "mousedown", updateInputs, false);
			   addEventFunc(new_input, "focus", updateInputs, false);
			   addEventFunc(new_input, "blur", inputBlur, false);
			   new_input.select();
			   new_input.focus();			   
			}
		}
	}
}




/****************************************************
* Build Search Query								*
*													*
*****************************************************/

function setupSearch(){
	if(document.getElementById("site-search")){
		var forms = document.getElementById("site-search").getElementsByTagName("form");
		if(forms.length > 0){
			addEventFunc(forms[0], "submit", buildQuery, false);
		}
	}
}


function buildQuery(e){
	if (!e){ e = window.event; }
	var tgt = e.target || e.srcElement;
	
	var inputs = tgt.getElementsByTagName("input");
	if(inputs.length > 0){
		if(inputs[0].name == "query"){
			tgt.action = "/cgi-bin/search.cgi?q=" + inputs[0].value;
		}
	}
}






/****************************************************
* Event Handlers									*
*													*
*****************************************************/


function addListeners(){
	var as = document.getElementsByTagName("a");
	for(var i=0;i<as.length;i++){
		as[i].hideFocus = true;
	}
	
	var inputs = document.getElementsByTagName("input");
	for(i=0;i<inputs.length;i++){
		inputs[i].setAttribute("busy",0);
		if((inputs[i].value == "Username" || inputs[i].value == "Password" || inputs[i].value == "Search") && inputs[i].getAttribute("fixed") != 1){			
			addEventFunc(inputs[i], "mousedown", updateInputs, false);
			addEventFunc(inputs[i], "focus", updateInputs, false);
			addEventFunc(inputs[i], "blur", inputBlur, false);
		}
	}
	
	if(browser.isIE){ addEventFunc(document.body, "mousedown", loseFocus, false); }
}





/****************************************************
* Get Flash Object									*
*													*
*****************************************************/


function getFlashMovieObject(movieName){
  if (window.document[movieName]){
	  return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1){
    if (document.embeds && document.embeds[movieName]){
      return document.embeds[movieName];
	}
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}


function getFlashMovie(movieName){
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}






/****************************************************
* Write Out JS Browser Stylesheet					*
*													*
*****************************************************/

function writeJsStyles(w){
	var sSheets = document.getElementsByTagName("link");
	var dir = "";

	if(sSheets[0]){
		if(sSheets[0].href.indexOf("css") != -1){
			dir = sSheets[0].href.substring(0,sSheets[0].href.indexOf("css"));
		}
	}

	if(w==1){ document.write('<link href="' + dir + 'css/js-dependant.css" rel="stylesheet" type="text/css" media="screen" />'); }
	if(browser.isSafari){ document.write('<link href="' + dir + 'css/safari.css" rel="stylesheet" type="text/css" media="screen" />'); }
	if(browser.isIE5 || browser.isIE6){ document.write('<link href="' + dir + 'css/ie.css" rel="stylesheet" type="text/css" media="screen" />'); }
}






/****************************************************
* On Page Load Functions							*
*													*
*****************************************************/

function init(){
	addListeners();
	setupSearch();
}




writeJsStyles(1);
addEventFunc(window, "load", init, false);


/****************************************************
* send an email to a friend							*
*													*
*****************************************************/



function mailpage()
{
mail_str = "mailto:?subject=" + urlencode(document.title);
mail_str += "&body=You might be interested in the " + urlencode(document.title);
mail_str += ". You can view it at, " + location.href;
location.href = mail_str;
}

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40').replace(/&/g, '%26');
}


/****************************************************
* display tab function							*
*													*
*****************************************************/


function display(variab,num)
	 	 
	 {
  
	var tabl= new Array("layer1","layer2");
     var Obj;
	 for( var i=0; i< tabl.length; i++)
	 
	 { 
  
	  if(document.getElementById( tabl[i]))
	  {
	  
		 Obj = document.getElementById(tabl[i]); 
		 if( variab==tabl[i])
		 {
		  Obj.style.display = "inline";
		 }
		 else
		 {
		 Obj.style.display = "none";
		
		 }
 		 
	  }
	 }
 			  if (variab=="layer2")
					{
		document.getElementById('img-layer2').src='../images/subpages/graphic_growth/tab_height_active.gif';
		document.getElementById('img-layer1').src='../images/subpages/graphic_growth/tab_weight_inactive.gif';
					}
			  else
			  		{
		document.getElementById('img-layer2').src='../images/subpages/graphic_growth/tab_height_inactive.gif';
		document.getElementById('img-layer1').src='../images/subpages/graphic_growth/tab_weight_active.gif';
				    }	 
     }

