/* Javascript functions for all general functions 

	Copyright IWA Consult BV, Woerden

*/

/* 	variable global definition area 		*/

var 	HideTimeOut=0;				// variable used for timeout to hide element
var	today			=	new Date();		// set today's date
var	FullYear			=	today.getFullYear();	// set fullyear
var	Year				=	today.getFullYear();	// set fullyear
	Year				=	2010;
var	WindowHeight		=	0;				// Actual Window Height
var	WindowWidth		=	0;				// Actual Window Width

var	vScrollTop		=	0;
var	vScrollLeft		=	0;
var 	browser_name		=	navigator.appName;
var 	browser_version	=	parseFloat(navigator.appVersion);
var 	IE_version		=	'';

var isSafari, isOpera, isOpera10, isIE, isNav, isFFold, isFF, isGecko, isKonqueror, isChrome, isOther = false;

if (navigator.userAgent.indexOf("Opera/9.80")!=-1) 
{
	isOpera10 = true;
	CookieWrite('Browser','Opera')	
} 
else if (navigator.userAgent.indexOf("Opera")!=-1) 
{
	isOpera = true;
	CookieWrite('Browser','Opera')	
} 
else if (navigator.userAgent.indexOf("Chrome/2")!=-1) 
{
	isChrome = true;
	CookieWrite('Browser','Chrome')	
} 
else if (navigator.userAgent.indexOf("Safari")!=-1) 
{
	isSafari = true;
	CookieWrite('Browser','Safari')	
} 
else if (navigator.userAgent.indexOf("Firefox/3.5")!=-1) 
{
	isFF = true;
	CookieWrite('Browser','Firefox')	
} 
else if (navigator.userAgent.indexOf("Firefox/3.6")!=-1) 
{
	isFF = true;
	CookieWrite('Browser','Firefox')	
} 
else if (navigator.userAgent.indexOf("Firefox")!=-1) 
{
	isFFold = true;
	CookieWrite('Browser','Firefox')	
} 
else if (navigator.appName == "Microsoft Internet Explorer") 
{
	isIE = true;
	IE_Version=navigator.appVersion.match('MSIE 8.0');
	if (IE_Version==null) 	{
		IE_Version=navigator.appVersion.match('MSIE 7.0');
		if (IE_Version==null) 	{
			IE_Version='MSIE 6.0';
		}
	}
	
	CookieWrite('Browser','Microsoft Internet Explorer')	
} 
else if (navigator.appName == "Netscape") 
{
	isNav = true;
	CookieWrite('Browser','Netscape')	
} 
else 
{
	isOther = true;
	CookieWrite('Browser','Other')	

}

var 	Msg 		= 	new Array(100);
Msg[2]		=	'Emailadres is niet correct';

function CheckEmailAddress(id)
/*
	check for valid  entered emailadres, return true if valid else false
*/
{
	var email=document.getElementById(id);
	var str=email.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	/* if emailaddress is invalid generate a message and return false else return true */

	if (filter.test(str))
	{
		checkemailresult=true;
		email.style.color="green"
		email.style.background="transparent"
		//email.style.background=noc;
		//document.getElementById('idMsg').innerHTML='';
	}
	else
	{
		checkemailresult=false;
		email.style.color="white"
		email.style.background="red"
	}
	
	return checkemailresult;
}

function Close()
{
	window.opener='X'; 
	window.open('','_parent',''); 
	window.close();
}

function CookieDelete(c_name)
{
	var expires_date = new Date();
	document.cookie=c_name + '=""' + '; expires= ' + expires_date.toUTCString();
	return;
}

function CookieRead(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 ;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length
			
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return "";
}

function CookieWrite(c_name,value,ExpiresInMinutes)
/*
	Write cookie that expires in specified ExpiresInMinutes
*/
{
	var today=new Date();
	today.setTime(today.getTime());
	
	e=1000*60*60*24;							// default expiration = 24 hours

	if (ExpiresInMinutes) 
	{
		var e = ExpiresInMinutes*1000*60;				// calculates milliseconds
	}
	
	var expires_date = new Date(today.getTime()+e);		// calculate expiration date
	document.cookie=c_name + '=' + escape(value) + '; expires= ' + expires_date.toUTCString();
	return;
}

function Copyright()
{
	// write Copyright statement  with current year to page
	document.write('<span style="font-size: 8pt;font-weight: bold;text-align: center;color: #800080;">Webdesign & Copyright IWA Consult BV '+FullYear+'</span>');
}

function Goback()
{
	// goback to previous page
	if(document.referrer) {
		window.open(document.referrer,'_self');} 
	else {
		history.go(-1);}
	return false;
}

function infoMsg(str,bgcolor)
{
/*	create div element (if not exists) and position this element at the bottom, and display str
*/
	if (bgcolor==null)
	{
		bgcolor='red';
	}

	var id='idMsg';	// id of element
	clearTimeout(HideTimeOut);

	if (document.getElementById(id) == null)
	{
		// Element Does Not Exists -> Create Element
		var msgdiv = document.createElement('div');
		document.body.appendChild(msgdiv);
		msgdiv.setAttribute('id',id);
	}
	else
	{
		// Element Already Exists -> Use that Element
		msgdiv=document.getElementById(id);
	}
	
	var l=str.length;
	
	WindowSize();
	
	// set style for this element
	
	msgdiv.style.visibility='visible';
	msgdiv.style.fontSize='10pt';
	msgdiv.style.fontWeight='bold';
	msgdiv.style.paddingTop='2px';
	msgdiv.style.textAlign='center';
	msgdiv.style.backgroundColor=bgcolor;
	msgdiv.style.color='yellow';
	msgdiv.style.border='1px #000066 solid';

	// center element on bottom screen
	var h1=20;
	var h2=40;
	var w=l*10;
	if (w>WindowWidth)
	{
		h1=50;
		h2=65;
		w=WindowWidth;
	}
	
	msgdiv.style.position='absolute';
	msgdiv.style.top=((WindowHeight*1-h2)/1)+'px';
	msgdiv.style.width=w+'px';
	msgdiv.style.left=((WindowWidth*1-w)/2)+'px';
	msgdiv.style.height=h1+'px'

	msgdiv.innerHTML=str;

	HideTimeOut=setTimeout('Hide(\''+id+'\')',5000);

	return;

}

function insertToSelectSorted(id, text, value) 
{ 

//	function to Insert <option> element into <select> in sorted order

	alert(id+'-'+value+'-'+text);

	oSelect=document.getElementById(id);		
	var foundIndex = oSelect.options.length; 	
	alert(foundIndex);
	for (i = 0; i < oSelect.options.length; i++) 
	{ 
		// loop thru to find right position
		if (oSelect.options[i].innerText.toLowerCase() > text.toLowerCase()) 
		{ 
			// if found stop looping
			foundIndex = i; 
			break; 
		} 
	} 
	// now define <option> and add it to <select>
	var oOption = new Option(text,value); 
	oSelect.options.add(oOption, foundIndex); 
}

function PrintPage()
/*
	print page
*/
{
	window.print();
}

function trim(str, chars) 
/*
	strip specified characters
*/
{
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
/*
	strip specified characters at the left side
*/
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
/*
	strip specified characters at the right side
*/
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function hex2str(value) 
{
	var rvalue='';
	for (var i=0;i<value.length;i=i+2)
	{
		rvalue += String.fromCharCode(parseInt(value.substr(i,2),16));
	}

	return rvalue;
}

function str2hex(value)
{
/* 	convert ascii string to hex string	a=61,b=62, etc 
	purpose is to escap specialcharacters like $ % # &
*/
	var rvalue='';
	for(i=0;i<value.length;i++)
	{
		var xv=value.charCodeAt(i);
		if (xv<16)
		{ 
			rvalue+='0'+value.charCodeAt(i).toString(16);
		}
		else
		{
			rvalue+=value.charCodeAt(i).toString(16);
		}
	}
	
	return rvalue;
}

function sqlEditUpdate(type,table,field,value,key)
{

	if (type=='checkbox') 
	{
		if (value==true) {xv=1};
		if (value==false) {xv=0};
		value=xv.toString();
	}

	url="_sqlexec.php?table="+table+"&field="+field+"&value="+value+"&key="+key;
	xmlExec('',url)
}

function Show(id)
{
	document.getElementById(id).style.visibility='visible';
}

function WindowSize()
/*
	Get actual Window size, store width and heigth in WindowWidth and WindowHeight defined above
*/
{
	if( typeof( window.innerWidth ) == 'number' ) 
	{	//Non-IE
		WindowWidth = window.innerWidth;
		WindowHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{	//IE 6+ in 'standards compliant mode'
		WindowWidth = document.documentElement.clientWidth;
		WindowHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{	//IE 4 compatible
		WindowWidth = document.body.clientWidth;
		WindowHeight = document.body.clientHeight;
	}
	//window.alert( 'Width = ' + WindowWidth );
	//window.alert( 'Height = ' + WindowHeight );
}

WindowSize()

function divHREF(href,bgcolor)
{
/*	create div element (if not exists) and position this element at the bottom, and display str
*/

	// set default background color
	if (bgcolor==null)
	{
		bgcolor='MintCream';
	}

	var id='iddivHTML';	// id of element

	clearTimeout(HideTimeOut);

	
	// if div element Does Not Exists -> Create Element else use it
	
	if (document.getElementById(id) == null)
	{
		var divHTML = document.createElement('div');
		document.body.appendChild(divHTML);
		divHTML.setAttribute('id',id);
	}
	else
	{
		// Element Already Exists -> Use that Element but clean it first
		divHTML=document.getElementById(id);
		divHTML.innerHTML='';
	}

	//	get window sizes
			
	WindowSize();
	
	// set style for this element
	
	divHTML.style.visibility='visible';
	divHTML.style.fontSize='10pt';
	divHTML.style.fontWeight='bold';
	divHTML.style.paddingTop='2px';
	divHTML.style.textAlign='center';
	divHTML.style.backgroundColor=bgcolor;
	divHTML.style.color='#000066';
	divHTML.style.border='2px #000066 solid';
	
	// size div element on 80% width and 80% height and center it
	
	var pw = Number(80);
	var ph = Number(80);
	
	var w=Number(0);
	var h=Number(0);
	if (WindowHeight<=800)  {ph=80;}
	if (WindowWidth<1025)  {pw=80;}
	
	w=WindowWidth*pw/100;
	h=WindowHeight*ph/100;
	w=w.toFixed(0);
	h=h.toFixed(0);

	divHTML.style.position='absolute';
	divHTML.style.top=((WindowHeight*1-h)/2)+'px';
	divHTML.style.width=w+'px';
	divHTML.style.left=((WindowWidth*1-w)/5*4)+'px';
	divHTML.style.height=h+'px'
		
	//var el='<div style="z-index:900;position:absolute;top:-24px;width:100%;height:20px;text-align:right"><input type="button" style="cursor:pointer;font-size:12pt;font-weight:bold;background-color:red;color:white;border:1px black solid"value="Sluiten (X)" onclick="Hide(\''+id+'\')"/></div>';
	var el='<div style="z-index:900;position:absolute;top:-24px;width:100%;height:20px;text-align:right"><input type="button" style="cursor:pointer;font-size:12pt;font-weight:bold;background-color:red;color:white;border:1px black solid"value="Sluiten (X)" onclick="RemoveElement(\''+id+'\')"/></div>';
	
	w=w-2; 	// set space between div en iframe
	
	divHTML.innerHTML=el+'<iframe name="ifr" id="ifr'+id+'" src="'+href+'" style="z-index:1;width: '+w+'px; height: '+h+'px" marginwidth="5" marginheight="5" frameborder="0">No inline frame support</iframe>';

	return;

}

function RemoveElement(id)
{
	// remove element
	var element = document.getElementById(id);
	element.parentNode.removeChild(element);
}

function WindowScroll()
{

	WindowSize();
	
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		vScrollTop = window.pageYOffset;
		vScrollLeft = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		vScrollTop = document.body.scrollTop;
		vScrollLeft = document.body.scrollLeft;
	} 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		vScrollTop = document.documentElement.scrollTop;
		vScrollLeft = document.documentElement.scrollLeft;
	}
	
	return;
}

window.onscroll = WindowScroll;
