/*
*
* general js for relationssajten.se by Ragnar Seton of dm9.se
*
*/

/* i miss the trim func in js! */
String.prototype.trim=function()
{
	return (this.replace(/\s+/g,'')).replace(/\s+$/g,'');
}
	
/* assume gebi support */
var gebiSupport=true;
/* and test it. */
if(!document.getElementById)
{
	var gebi=(document.layers?document.layers:document.all);
	gebiSupport=false;
}

var ns4=(navigator.appName.indexOf("Netscape")>=0 && !gebiSupport) ? true : false;
var ns6=(navigator.appName.indexOf("Netscape")>=0 && gebiSupport) ? true : false;
var ie4=(document.all && !gebiSupport) ? true : false;
var ie5=(document.all && gebiSupport) ? true : false;

function getElement(strId)
{
	if(gebiSupport) return document.getElementById(strId);
	else return gebi[strId];
}
function insertHTML(strId,html)
{
	var box=getElement(strId);
	if(ns4)
	{
		box.document.open();
		box.document.write(html);
		box.document.close();
	}
	else if(gebiSupport||ie4) box.innerHTML=html;
}

/*
* slightly modyfied version of Scott Andrew's
* cross browser event handler, see
* http://www.scottandrew.com/weblog/articles/cbs-events
* for original version.
*/
function addEvent(obj, evType, fn)
{
	var r=null;
	if(obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if(obj.attachEvent)
	{
		r=obj.attachEvent("on"+evType, fn);
		return r;
	}
	else return false;
}

function showInfo(section,what,id)
{
	window.open("misc.php?section="+section+"&action="+what+"&id="+id,"infoWindow"
		,"height=400,width=300,dependent=1,hotkeys=0,location=0,menubar=0,personalbar=0"
		+",resizable=0,scrollbars=1,status=0,toolbar=0,");
}
function printItem(section,id)
{
	window.open("misc.php?section="+section+"&action=print&id="+id,"printWindow"
		,"height=101,width=300,dependent=1,hotkeys=0,location=0,menubar=0,personalbar=0"
		+",resizable=0,scrollbars=0,status=0,toolbar=0,");
}
function linkItem(section,id)
{
	window.open("misc.php?section="+section+"&action=link&id="+id,"linkWindow"
		,"height=200,width=400,dependent=1,hotkeys=0,location=0,menubar=0,personalbar=0"
		+",resizable=0,scrollbars=0,status=0,toolbar=0,");
}
function sendItem(section,id)
{
	window.open("misc.php?section="+section+"&action=send&id="+id,"sendWindow"
		,"height=380,width=300,dependent=1,hotkeys=0,location=0,menubar=0,personalbar=0"
		+",resizable=0,scrollbars=0,status=0,toolbar=0,");
}
function validateEmail(str)
{
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,16})+$/.test(str) &&
		(str.toLowerCase().indexOf("slaskpost.se")==-1));
}

/*
* since relationstest.php only got one func
* ill throw it in here
*/
function validateTestForm()
{
	var i,j,tmp,cleared="",ret=true,dfe=document.forms['testForm'].elements;
	
	for(i=0;i<dfe.length;i++)
	{
		if(dfe[i].type=="radio" && cleared.indexOf(dfe[i].name)==-1)
		{
			tmp=false;
			for(j=0;j<dfe[dfe[i].name].length;j++)
			{
				if(dfe[dfe[i].name][j].checked)
				{
					tmp=true;
					break;
				}
			}
			if(!tmp) ret=false;
			cleared=dfe[i].name+" ";
		}
	}
	if(!ret) alert("Du måste svara på frågan innan du kan fortsätta.");
	return ret;
}
/*
* same with needhelp.php
*/
function validateHelpForm()
{
	var dfe=document.forms['helpForm'].elements;
	var tel=/^(\+\d{2})?\s*(\(\d\))?\d{1,5}[\-\d\s]*$/;
	
	if(dfe['name'].value.trim().length<3)
	{
		dfe['name'].style.backgroundColor='#ff9090';
		alert("Ditt namn måste vara minst tre tecken långt.");
		return false;
	}
	else dfe['name'].style.backgroundColor='#f0f0f0';
	
	if(!tel.test(dfe['tel'].value) || dfe['tel'].value.trim().length<8)
	{
		dfe['tel'].style.backgroundColor='#ff9090';
		alert("Du måste fylla i ditt telefonnummer korrekt.");
		return false;
	}
	else dfe['tel'].style.backgroundColor='#f0f0f0';
	
	if(!validateEmail(dfe['mail'].value))
	{
		dfe['mail'].style.backgroundColor='#ff9090';
		alert("Du måste fylla i en korrekt mailadress.");
		return false;
	}
	else dfe['mail'].style.backgroundColor='#f0f0f0';
	return true;
}
