//-----------------------------------------------------------------

function toCelsius(temp)
{
    return Math.round((temp - 32) / 1.8);
}

//-----------------------------------------------------------------

function parseTimeString(string)
{
	// parses string of the form nn:nn

	var obj = null;
	try{
		var array = string.match(/\d{1,2}/g);
		obj ={hour:parseInt(array[0], 10), minute:parseInt(array[1],10), str:string};
	}
	catch(ex){
		// ignore
	}

	return obj;
}

//-----------------------------------------------------------------

function trimWhiteSpace(string)
{
	return string.replace(/^\s*/, '').replace(/\s*$/, '');
}

//-----------------------------------------------------------------

function findXMLKey(text, keyName)
{
	var ix1, ix2, offset;
  	var	tag1, tag2;
  	
  	tag1="<"+keyName+">";
  	tag2="</"+keyName+">";
  	
  	len1 = tag1.length;
  	ix1 = text.indexOf(tag1);
  	if (ix1 < 0)
		return null;
 
  	ix2 = text.indexOf(tag2);
  	if (ix2 < 0)
		return null;
  	
  	offset = ix1 + tag1.length;
	return text.substr(offset, ix2-offset);
}

//-----------------------------------------------------------------

function findChild(element, nodeName)
{
	var child;
  
	for (child = element.firstChild; child != null; child = child.nextSibling)
	{
		if (child.nodeName == nodeName)
      		return child;
  	}
  
	return null;
}

//-----------------------------------------------------------------

function ucfirst(string)
{
	var firstChar = string.charAt(0).toUpperCase();
	return firstChar + string.substr(1);
}

//-----------------------------------------------------------------

function ucwords(string) 
{
	return string.replace(
	/^(.)|\s(.)/g, 
		function($1)
    	{ 
			return $1.toUpperCase(); 
		}
	);
}

//-----------------------------------------------------------------

var filesadded="" //list of files already added

function checkloadjscssfile(filename, filetype)
{
	if (filesadded.indexOf("["+filename+"]")==-1)
	{
		loadjscssfile(filename, filetype);
		filesadded+="["+filename+"]"; //List of files added in the form "[filename1],[filename2],etc"
	}
	else
		alert("file already added!");
}

function loadjscssfile(filename, filetype)
{
	if (filetype=="js")
	{ 
		//if filename is a external JavaScript file
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	}
	else if (filetype=="css")
	{ 
		//if filename is an external CSS file
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
}

function createjscssfile(filename, filetype)
{
	if (filetype=="js")
	{ 
		//if filename is a external JavaScript file
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	}
	else if (filetype=="css")
	{ 
		//if filename is an external CSS file
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	
	return fileref;
}

function removejscssfile(filename, filetype)
{
	var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; 	//determine element type to create nodelist from
	var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; 		//determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName(targetelement);
	for (var i=allsuspects.length; i>=0; i--)
	{ 
		//search backwards within nodelist for matching elements to remove
		if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
			allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
	}
}

function replacejscssfile(oldfilename, newfilename, filetype)
{
	var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; 	//determine element type to create nodelist using
	var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; 		//determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName(targetelement);
	for (var i=allsuspects.length; i>=0; i--)
	{ 
		//search backwards within nodelist for matching elements to remove
		if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1)
		{
			var newelement=createjscssfile(newfilename, filetype);
			allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i]);
		}
	}
}

//-----------------------------------------------------------------

function hslToRgb(h, s, l) 
{
   	var n1, n2;
	var r, g, b;

	if (h < -359) 	h = -359;
	if (h < 0) 		h = 359+h;
	if (h > 359)	h = 359;
	
	h = Math.round(h*255/359);
	s = Math.round(s*2.55);
	l = Math.round(l*2.55);
	
	if (h < 0) 		h = 0;
	if (h > 255)	h = 255;

	if (s < 0) 		s = 0;
	if (s > 255)	s = 255;

	if (l < 0) 		l = 0;
	if (l > 255)	l = 255;
	
	//---
	
    if (s == 0)
	{
		r = g = b = l;
	}
    else
    {
	    if (l < 128)	n2 = l * (255 + s);
	    else			n2 = (l + s) * 255 - (l * s);

	    n1 = 510 * l - n2;

	    r = color_value(n1,n2,h+85);
	    g = color_value(n1,n2,h);
	    b = color_value(n1,n2,h-85);
    }
	
	//---
	// return r: 0..255, g: 0..255, b: 0..255
	
	r = Math.round(r);
	g = Math.round(g);
	b = Math.round(b);
	
	if (r < 0) 		r = 0;
	if (r > 255)	r = 255;

	if (g < 0) 		g = 0;
	if (g > 255)	g = 255;

	if (b < 0) 		b = 0;
	if (b > 255)	b = 255;
	
	return [r, g, b];
}

//-----------------------------------------------------------------

function color_value(n1, n2, hue)
{
	var  value;

	if (hue <   0) hue += 255;
	if (hue > 255) hue -= 255;

	if (hue < 43)		value = (85*n1 + 2*(n2-n1)*hue + 127) / 21675;
	else if (hue < 128)	value = n2 / 255;
	else if (hue < 170)	value = (85*n1 + 2*(n2-n1)*(170-hue) + 127) / 21675;
	else				value = n1 / 255;

	return value;
}

//-----------------------------------------------------------------

function rgbToHsl(r, g, b) 
{
    var max, min;
    var h, l, s;
    var diff;
    var sum;

	r = Math.round(r);
	g = Math.round(g);
	b = Math.round(b);

	if (r < 0) 		r = 0;
	if (r > 255)	r = 255;

	if (g < 0) 		g = 0;
	if (g > 255)	g = 255;

	if (b < 0) 		b = 0;
	if (b > 255)	b = 255;
	
	//---
	
	max = Math.max(r, g, b);
	min = Math.min(r, g, b);
    
    diff = max-min;
    sum  = max+min;

    l = sum/2;

    if (diff == 0) 
    {
		s = 0;
		h = 0;
    } 
    else 
    {
		if (l < 128)		s = (diff*255)/(sum);
		else				s = (diff*255)/(510-sum);

		if (r == max)		h = ((g-b)*85+1)/(2*diff);
		else if (g == max)	h = 85 + ((b-r)*85+1)/(2*diff);
	    else				h = 170 + ((r-g)*85+1)/(2*diff);

		if (h<0)
	  		h += 255;
    }
	
	//---
	// return h: 0..359, s: 0..100, l: 0..100

	if (h < 0) 		h = 0;
	if (h > 359)	h = 359;

	if (s < 0) 		s = 0;
	if (s > 255)	s = 255;

	if (l < 0) 		l = 0;
	if (l > 255)	l = 255;
	
	h = Math.round(h/255*359);
	s = Math.round(s/2.55);
	l = Math.round(l/2.55);
	
	return [h, s, l];
}

//-----------------------------------------------------------------

