//color picker**************************************************************************
addary = new Array();           //red
addary[0] = new Array(0,1,0);   //red green
addary[1] = new Array(-1,0,0);  //green
addary[2] = new Array(0,0,1);   //green blue
addary[3] = new Array(0,-1,0);  //blue
addary[4] = new Array(1,0,0);   //red blue
addary[5] = new Array(0,0,-1);  //red
addary[6] = new Array(255,1,1);
clrary = new Array(360);
for(i = 0; i < 6; i++)
for(j = 0; j < 60; j++) {
  clrary[60 * i + j] = new Array(3);
  for(k = 0; k < 3; k++) {
    clrary[60 * i + j][k] = addary[6][k];
    addary[6][k] += (addary[i][k] * 4);
    }
  }
  
hexary = new Array("#000000", "#000000", "#000000");
picary = new Array("picka");
initary = new Array("#000000");

function capture() {
 hoverColor();
 initColor();
 if(document.layers) {
  layobj = document.layers['wheel'];
  layobj.document.captureEvents(Event.MOUSEMOVE);
  layobj.document.onmousemove = mouseMoved;
 }
 else if (document.all) {
  layobj = document.all["wheel"];
  layobj.onmousemove = mouseMoved;
   }
 else if (document.getElementById) {
  window.document.getElementById("wheel").onmousemove = mouseMoved;
 }
}

function mouseMoved(e) {
 if (document.layers) {
  x = 4 * e.layerX;
  y = 4 * e.layerY;
 }
 else if (document.all) {
  x = 4 * event.offsetX;
  y = 4 * event.offsetY;
 }
 else if (document.getElementById) {
  x = 4 * (e.pageX - document.getElementById("wheel").offsetLeft);
  y = 4 * (e.pageY - document.getElementById("wheel").offsetTop);
 }
 sx = x - 512;
 sy = y - 512;
 qx = (sx < 0)?0:1;
 qy = (sy < 0)?0:1;
 q = 2 * qy + qx;
 quad = new Array(-180,360,180,0);
 xa = Math.abs(sx);
 ya = Math.abs(sy);
 d = ya * 45 / xa;
 if(ya > xa) d = 90 - (xa * 45 / ya);
 deg = Math.floor(Math.abs(quad[q] - d));
 n = 0;
 sx = Math.abs(x - 512);
 sy = Math.abs(y - 512);
 r = Math.sqrt((sx * sx) + (sy * sy));
 if(x == 512 & y == 512) {c = "000000";}
 else {
   for(i = 0; i < 3; i++) {
     r2 = clrary[deg][i] * r / 256;
     if(r > 256) r2 += Math.floor(r - 256);
     if(r2 > 255) r2 = 255;
     n = 256 * n + Math.floor(r2);
   }
   c = n.toString(16);
   while(c.length < 6) c = "0" + c;
 }
         hexary[1] = "#" + c.charAt(0) + c.charAt(0) + c.charAt(2) + c.charAt(2) + c.charAt(4) + c.charAt(4);
         hexary[2] = "#" + c;
         hexary[0] = safetyFirst(c);
   hoverColor();
   return false;
}

function hoverColor() {
/* Detta buggar upp EI:
 if (document.layers) {
  document.layers["wheel"].bgColor = hexary[1];
  document.layers["democ"].bgColor = hexary[2];
  document.layers["democ"].document.abcform.abc.value = hexary[2];
 } else if (  document.all) {
  document.all["wheel"].style.backgroundColor = hexary[1];
  document.all["democ"].style.backgroundColor = hexary[2];
  document.all["democ"].document.abcform.abc.value = hexary[2];
 } else if (document.getElementById) {
 */
  document.getElementById("wheel").style.backgroundColor = hexary[1];
  document.getElementById("democ").style.backgroundColor = hexary[2];
  document.getElementById("abc").value = hexary[2];
 //}
 return false;
}

function pickColor() {
  thecontents = "<tt class='wtxt'>" + hexary[2] +"</tt><br><tt class='btxt'>" + hexary[2] +"</tt></div>";

   document.getElementById("picka").innerHTML = thecontents;
   document.getElementById("picka").style.backgroundColor = hexary[2];
   document.getElementById("namecolor").style.color = hexary[2];
   document.getElementById("savepick").style.color = hexary[2];

}

function initColor() {
  thecontents = "<tt class='wtxt'>" + initary[0] +"</tt><br><tt class='btxt'>" + initary[0] +"</tt></div>";

   document.getElementById("picka").innerHTML = thecontents;
   document.getElementById("picka").style.backgroundColor = initary[0];
}

function safetyFirst(c) {
    cary = new Array(c.charAt(0),c.charAt(2),c.charAt(4));
    for (ci = 0; ci < 3; ci++) {
       switch (cary[ci]) {
         case "1":
         cary[ci]="0";
         break;
         case "2":
         cary[ci]="3";
         break;
         case "4":
         cary[ci]="3";
         break;
         case "5":
         cary[ci]="6";
         break;
         case "7":
         cary[ci]="6";
         break;
         case "8":
         cary[ci]="9";
         break;
         case "a":
         cary[ci]="9";
         break;
         case "b":
         cary[ci]="c";
         break;
         case "d":
         cary[ci]="c";
         break;
         case "e":
         cary[ci]="f";
         break;
      }
   }
  safecolor = "#" + cary[0]+cary[0] + cary[1]+cary[1] + cary[2]+cary[2];
  return safecolor;
}

/***********************************/





//*****create dynamic select menus ****/

function insertOptionBefore(num)
{
  var elSel = document.getElementById('selectX');
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = 'Insert' + num;
    elOptNew.value = 'insert' + num;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function removeOptionSelected()
{
  var elSel = document.getElementById('selectX');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function appendOptionLast(num)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = 'Append' + num;
  elOptNew.value = 'append' + num;
  var elSel = document.getElementById('selectX');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}


function appendOption(value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = value;
  elOptNew.value = value;
  var elSel = document.getElementById('selectX');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptionLast()
{
  var elSel = document.getElementById('selectX');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}


//Totally removes the div 'removediv'
function toggleVis(removediv)
{ 
var div = document.getElementById(removediv);
	if(div)
	{
		if(div.style.display=='none')
		{
			div.style.display='inline';
		}
		else
		{
			div.style.visibility='none';
		}
	}
}

function checkAll(field) 
{
	if(field)
	{
		for (i = 0; i < field.length; i++)
			field[i].checked = true ;
	}
} 
 
function uncheckAll(field) 
{
if(field)
	{
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
	}
}

/*
function reset(input)
{ 
var field = document.getElementById(input);

if(field)
{
field.value="";
}
}
*/

function getElementsByClassName(classname, node)  
{
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function writedomains(value){
var elements=getElementsByClassName('showdomain');
var j = elements.length;

for (var i = 0; i < j; i++) {
  elements[i].innerHTML=value;
	}
}


function highlightpattern(field){
var elements=getElementsByClassName(field);
var j = elements.length;

		if(this.value==1)
		{
			color="red";
			this.value=0;
		}
		else
		{
			color="black";
			this.value=1;
		}


for (var i = 0; i < j; i++) {

		elements[i].style.color=color;
	}
}


function setFont(value){
var elements=getElementsByClassName('setfont');
var j = elements.length;

for (var i = 0; i < j; i++) {
  elements[i].style.fontFamily=value;
	}
}


// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}


/**
 * Actual function that does all the work. Returns an array with all the info.
 * This test will fail for the font set as the default serif font.
 * 
 * Usage: d = new Detector();
 *        d.test('font_name');
 */
var Detector = function(){
	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");
	d.appendChild(s);
	d.style.fontFamily = "sans";			//font for the parent element DIV.
	s.style.fontFamily = "sans";			//serif font used as a comparator.
	s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
	s.innerHTML        = "mmmmmmmmmmlil";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
	h.appendChild(d);
	var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
	var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
	h.removeChild(d);
	/* test
	 * params:
	 * font - name of the font you wish to detect
	 * return: 
	 * f[0] - Input font name.
	 * f[1] - Computed width.
	 * f[2] - Computed height.
	 * f[3] - Detected? (true/false).
	 */
	function debug(font) {
		h.appendChild(d);
		var f = [];
		f[0] = s.style.fontFamily = font;	// Name of the font
		f[1] = s.offsetWidth;				// Width
		f[2] = s.offsetHeight;				// Height
		h.removeChild(d);
		font = font.toLowerCase();
		if (font == "serif") {
			f[3] = true;	// to set arial and sans-serif true
		} else {
			f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
		}
		return f;
	}
	function test(font){
		f = debug(font);
		return f[3];
	}
	this.detailedTest = debug;
	this.test = test;	
}



function installedFonts() {

var elSel = document.getElementById('selectX');
  
//tmp solution to just do this once whne mouseover.  
if(elSel.length<=1)
{

d = new Detector();
var allfonts = [
'Arial',
'Courier New',
'Times New Roman',
'Microsoft Sans Serif',
'Segoe UI',
'Tahoma',
'Calibri',
'Cambria',
'Candara',
'Consolas',
'Constantias',
'Corbel',
'Arial Black',
'Franklin Gothic Medium',
'Impact',
'Lucida Console',
'Lucida Sans Console',
'Palatino Linotype',
'Segoe Print',
'Segoe Script',
'Trebuchet MS',
'Verdana',
'Al Bayan',
'American Typewriter',
'Andale Mono',
'Apple Casual',
'Apple Chancery',
'Apple Garamond',
'Apple Gothic',
'Apple LiGothic',
'Apple LiSung',
'Apple Myungjo',
'Apple Symbols',
'AquaKana',
'Arial Hebrew',
'Ayuthaya',
'Baghdad',
'Baskerville',
'Beijing',
'BiauKai',
'Big Caslon',
'Brush Script',
'Chalkboard',
'Charcoal',
'Charcoal CY',
'Chicago',
'Cochin',
'Comic Sans',
'Cooper',
'Copperplate',
'Corsiva Hebrew',
'Courier',
'DecoType Naskh',
'Devanagari',
'Didot',
'Eupheima UCAS',
'Fang Song',
'Futura',
'Gadget',
'Geeza Pro',
'Geezah',
'Geneva',
'Geneva CY',
'Georgia',
'Gill Sans',
'Gujarati',
'Gung Seouche',
'Gurmukhi',
'Hangangche',
'HeadlineA',
'Hei',
'Helvetica',
'Helvetica CY',
'Helvetica Neue',
'Herculanum',
'Hiragino Kaku Gothic Pro',
'Hiragino Kaku Gothic ProN',
'Hiragino Kaku Gothic Std',
'Hiragino Kaku Gothic StdN',
'Hiragino Maru Gothic Pro',
'Hiragino Maru Gothic ProN',
'Hiragino Mincho Pro',
'Hiragino Mincho ProN',
'Hoefler Text',
'Inai Mathi',
'Jung Gothic',
'Kai',
'Keyboard',
'Krungthep',
'KufiStandard GK',
'LastResort',
'LiHei Pro',
'LiSong Pro',
'Lucida Grande',
'Marker Felt',
'Monaco',
'Monaco CY',
'Mshtakan',
'Nadeem',
'New Peninim',
'New York',
'NISC GB18030',
'Optima',
'Osaka',
'Palatino',
'Papyrus',
'PC Myungjo',
'Pilgiche',
'Plantagenet Cherokee',
'Raanana',
'Sand',
'Sathu',
'Seoul',
'Shin Myungjo Neue',
'Silom',
'Skia',
'Song',
'ST FangSong',
'ST Heiti',
'ST Kaiti',
'ST Song',
'Tae Graphic',
'Taipei',
'Techno',
'Textile',
'Thonburi',
'Times',
'Times CY',
'Trebuchet',
'Zapf Chancery',
'Zapf Dingbats',
'Zapfino'];

allfonts.sort();

	for (var i = 0; i < allfonts.length; i++) {
	if(d.test(allfonts[i]))
		{
		appendOption(allfonts[i]);
		}
	}
}
}





//Ajax: creates the XMLHttpRequest object and calls a function to perform when server responds (onreadystatechange)
//Do this for each AJAX task.
function loadXMLDoc(url, method, cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open(method,url,true);
xmlhttp.send();
}


//Ajax:Loads url into div
function loadPage(url, method, div)
{
loadXMLDoc(url, method, function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	$("#"+div).hide();
    document.getElementById(div).innerHTML=xmlhttp.responseText;
	$('html, body').animate({scrollTop:0}, 'slow');
	$("#"+div).fadeIn("slow");
    }
  });
}

//Ajax:Loads url into div without scrolling page to top
function loadPageNoScroll(url, method, div)
{
loadXMLDoc(url, method, function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	$("#"+div).hide();
    document.getElementById(div).innerHTML=xmlhttp.responseText;
	$("#"+div).fadeIn("slow");
    }
  });
}


//Ajax:Loads url into div
function loadPage2(url, method, div)
{

loadXMLDoc('urlfind.php?url='+document.getElementById('url').value, 'get', function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {	
		
		/* Slå på detta för att tvinga anv att länka för att kunna make report.
		var response=xmlhttp.responseText;
		if(response.charAt(13)!='o')  //fullösn, för class="Ok"
		{
			alert('Link not found, verify it above.');
		}
		else
		{
		*/
		document.getElementById('loading').style.visibility='visible'; 
		document.getElementById('verify').style.visibility='hidden'; 
		document.getElementById('report').style.visibility='hidden';
		document.getElementById('searchcontrol').style.display='inline';

		loadXMLDoc(url, method, function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			$("#"+div).hide();
			document.getElementById(div).innerHTML=xmlhttp.responseText;
			$('html, body').animate({scrollTop:0}, 'slow');
			$("#"+div).fadeIn("slow");
			}
		  });
		  
		//}
		
    }
  });
  
}



















//Ajax:Loads url into div and inits layout components
function loadLayout(url, method, div)
{
loadXMLDoc(url, method, function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(div).innerHTML=xmlhttp.responseText;
    installedFonts();
	capture();
	$('html, body').animate({scrollTop:0}, 'slow');
	}
  });
}





//CONTACT form code
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}






function sendemail2() {
	var name = document.emailform.name.value;
	var email = document.emailform.email.value;
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(email) == true && name!="Your name")
	{
	loadPage('email.php?email='+email+'&name='+name, 'get', 'emailform')
	}
	else
	{
	alert('Enter email address and your full name.');
	}
}



function sendemail() {

	var http = createRequestObject();

	var name = document.emailform.name.value;
	var email = document.emailform.email.value;
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(email) == true && name!="Your name")
	{
	http.open('get', 'email.php?email='+email+'&name='+name);
    http.onreadystatechange = handleResponse;
    http.send(null);
	alert('Email sent to '+email+'.');
	}
	else
	{
	alert('Enter email address and your full name.');
	}
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
         
        }
    }
}




function clearText(field){
    if (field.defaultValue == field.value) 
	{
	field.value = '';
	field.className = 'input';
	}
    else if (field.value == '') 
	{
	field.value = field.defaultValue;
	field.className = 'inputDefault';
	}
}

function clearTextFix(field, text){
    if (field.defaultValue == field.value) 
	{
	field.value = '';
	field.className = 'input';
	}
    else if (field.value == '') 
	{
	field.value = text;
	field.className = 'inputDefault';
	}
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}



function submitcheckbox(field, alerttext){

var set=0; //init

if(field.checked == true)
{
set=1;
}


for (i = 0; i < field.length; i++)
{
	if(field[i].checked == true)
	{
	set=1;
	}
}
	
	if(!set)
	{
	alert(alerttext); 
	return false;
	}
}


function submitradio(field, alerttext){

var value=getCheckedValue(field);

	if(value=="")
	{
	alert(alerttext); 
	}
	else
	{
	document.getElementById('step3button').style.visibility='hidden'; 
	document.getElementById('loading').style.visibility='visible';
	loadPage('decomposestep.php?namechoice='+value, 'get', 'loadpage');
	
	}	
}



function createsubmit(alerttext)
{
	if(document.step1.type.value==2 && document.step1.predefined.value=='')
	{
		alert(alerttext);
		return false;
	}
}








