/*
 * 	CMsys - subroutines                                                               
 * 	Copyright (C) 2005 Ton Driessen
 * 
 * 	[File Name	: cmsys.js]
 * 	[Version	: 4.1] 
 * 	[Modified	: 04-09-2007] 
 * 
 * 	File Author:	Ton Driessen
 */

/**
 *	 Main Ajax routine
 *	p_switch	switch on or off
 *	p_confirm	ask for confirmation
 *	p_confstr	confirmation message
 *	ajxproc	ajax module to start, if not specified default "ajax.php" is started
 *	task		task window to start when request fails
 *	content	content (ex. cen-btn-02)
 *	bname	base name
*	id		ajax ID, if none the content will be taken as ID
 */

function cmsys_ajax(p_switch,p_confirm,p_confstr,ajxproc,task,content,bname,id,width,height) {
	var tstckie = getCookie('test');
	if (!checkCookie('ajax') && (tstckie == "ajxon") && (p_switch != 'off')){
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			window.open('phpcmsys/'+task+'.php?content='+content,'editwin','resizable=yes,scrollbars=yes,width='+width+',height='+height+'');
		}
		else {
			var d=new Date();
			var t=d.getTime()-1167609600000;
			var url="phpcmsys/";
			var c=true;
			if (p_confirm == 'yes') {
				if (confirm(p_confstr)) {
					c=true;
				}
				else{
					c=false;
				}
			}
			if (c){
				if (ajxproc!=""){
					url=url+ajxproc+".php";
				}
				else {
					url=url+"ajax.php";
				}
				Put_Cookie('ajax',task+"-"+t);
				if (id!=""){
					Put_Cookie('ajaxid',id);
				}
				Put_Cookie('scrl',document.body.scrollTop);
				document.getElementById('ajx-stat-bar').innerHTML='<font color="#ff0000">active</font>';
				url=url+"?content="+content;
				url=url+"&bname="+bname;
				url=url+"&task="+task;
				url=url+"&id="+id;
				url=url+"&time="+t;
				xmlHttp.onreadystatechange= function() { 
					if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
					  	var tmp=xmlHttp.responseText; 
						var kil=tmp.substr(0,18);
						kil=kil.search(/{killing-ajax.php}/);
						if (kil != -1) {
							
						}
						else {
							var sea=tmp.search(/ajax/);
							var id=tmp.slice(0,sea); 
							var tmp=tmp.substr(sea+4);
							document.getElementById(id).innerHTML=tmp;
//alert ("ajaxid: "+id);
						}
						document.getElementById('ajx-stat-bar').innerHTML="on";
						delCookie('ajax');
						var scrl = getCookie('scrl');
						if (scrl > 0){
							document.body.scrollTop = scrl;
						}
					}
				}
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
				if (ajxproc==""){
					window.open('phpcmsys/'+task+'.php?content='+content,'editwin','resizable=yes,scrollbars=yes,width='+width+',height='+height+'');
				}
			}
		}
	}
	else {
		delCookie('ajax');
		window.open('phpcmsys/'+task+'.php?content='+content+'&bname='+bname,'editwin','resizable=yes,scrollbars=yes,width='+width+',height='+height+'');
//		window.open(''+task+'.php?content='+content,'editwin','resizable=yes,scrollbars=yes,width='+width+',height='+height+'');
	}
}
function cmsys_tools(content) {
	window.open('phpcmsys/tsk.tools.php?content=' + content,'toolswin','width=260,height=220');
}
function cmsys_iconset(content) {
	window.open('phpcmsys/tsk.iconset.php?content=' + content,'editwin','resizable=yes,scrollbars=yes,width=900,height=720');
}
function cmsys_lineset(content) {
	window.open('phpcmsys/lineset.php?content=' + content,'editwin','resizable=yes,scrollbars=yes,width=900,height=720');
}
function cmsys_buttonup(content) {
	window.open('phpcmsys/tsk.btn.up.php?content=' + content,'toolswin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_buttondown(content) {
	window.open('phpcmsys/tsk.btn.down.php?content=' + content,'toolswin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_textup(content) {
	window.open('phpcmsys/tsk.txt.up.php?content=' + content,'toolswin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_textdown(content) {
	window.open('phpcmsys/tsk.txt.down.php?content=' + content,'toolswin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_start(content) {
	window.open('http://www.bonsite.nl/wslog.php?content='+content,'toolswin','width=1,height=1,top=1,left=1');
}
function cmsys_hrefset(content) {
	window.open('phpcmsys/tsk.hrefset.php?content=' + content,'editwin','resizable=yes,scrollbars=yes,width=800,height=680');
}
function cmsys_imgpos(content) {
	window.open('phpcmsys/tsk.img.pos.php?content=' + content,'editwin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_swpflg(content) {
	window.open('phpcmsys/tsk.swpflg.php?content=' + content,'toolswin','resizable=yes,scrollbars=yes,width=1,height=1');
}
function cmsys_open_url(url) {
	window.open(url);
}
function getCookie(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 checkCookie(c_name){
	nam=getCookie(c_name)
	if (nam!=null && nam!=""){
		return true;
	}
	else{
		return false;
	}
}
function delCookie(c_name){
	var ckie=getCookie(c_name);
	Clear_Cookie(c_name,ckie);
}

function Put_Cookie(c_name,c_value) {
	document.cookie = c_name + '=' + escape(c_value) + '; path=/'; 
}
function Put_Cookie_Reload(c_name,c_value) {
	document.cookie = c_name + '=' + escape(c_value) + '; path=/'; 
	location.reload();
}
function Keep_Cookie_Reload(c_name,c_value) {
	document.cookie = c_name + '=' + escape(c_value) + '; expires=Fri, 31 Dec 2099 23:59:59 GMT;' + 'path=/'; 
	location.reload();
}
function Clear_Cookie(c_name,c_value) {
	document.cookie = c_name + '=' + escape(c_value) + '; expires=Fri, 31 Dec 1999 23:59:59 GMT;' + 'path=/'; 
}
function Clear_Cookie_Reload(c_name,c_value) {
	document.cookie = c_name + '=' + escape(c_value) + '; expires=Fri, 31 Dec 1999 23:59:59 GMT;' + 'path=/'; 
	location.reload();
}
function Open_Window(p_confirm,p_confstr,p_file,p_content,p_width,p_height) {
	if (p_confirm == 'yes') {
		if (confirm(p_confstr)) {
			if (p_width == 0) {
				window.open(p_file + '.php?content=' + p_content,'editwin','resizable=yes,scrollbars=yes,status=yes');
			}
			else {
				window.open(p_file + '.php?content=' + p_content,'editwin','resizable=yes,scrollbars=yes,width=' + p_width + ',height=' + p_height);
			}
		}
	}
	else {
		if (p_width == 0) {
			window.open(p_file + '.php?content=' + p_content,'editwin','resizable=yes,scrollbars=yes,status=yes');
		}
		else {
			window.open(p_file + '.php?content=' + p_content,'editwin','resizable=yes,scrollbars=yes,width=' + p_width + ',height=' + p_height);
		}
	}
}
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function ajx_fnc_wdm(bname,cdir) {
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp!=null){
			var url="phpcmsys/ajx.fnc.wdm.php";
			url=url+"?bname="+bname;
			url=url+"&cdir="+cdir;
			Put_Cookie('ajax',"ajx.fnc.wdm");
			document.getElementById('ajx-stat-bar').innerHTML='<font color="#ff0000">active</font>';
			xmlHttp.onreadystatechange= function () { 
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			  		var response=xmlHttp.responseText; 
					document.getElementById('ajx-fnc-wdm').innerHTML=response;
					document.getElementById('ajx-stat-bar').innerHTML="on";
					delCookie('ajax');
				}
			}
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
}
function testajax(){
	if (getCookie('test') != "ajxoff"){
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp!=null){
			Put_Cookie('test','ajxon');
		}
		else {
			Put_Cookie('test','ajxns');
		}
	}
}
function getwndsize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	if (checkCookie('test')){
		Put_Cookie('wndsiz','w'+myWidth+'h'+myHeight);
	}
	else{
		window.open('phpcmsys/tsk.getwndsiz.php?w='+myWidth+'&h='+myHeight,'editwin','resizable=yes,scrollbars=yes,width=1,height=1');
	}
}

function ajx_gallphoto(bname,cdir,album,photo,type) {
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp!=null){
			var url="phpcmsys/ajx.gall.inc.php";
			url=url+"?bname="+bname;
			url=url+"&cdir="+cdir;
			url=url+"&album="+album;
			url=url+"&photo="+photo;
			url=url+"&type="+type;
			Put_Cookie('ajax',"ajx.gall.inc");
			if (document.getElementById('ajx-stat-bar')){
				document.getElementById('ajx-stat-bar').innerHTML='<font color="#ff0000">active</font>';
			}
			xmlHttp.onreadystatechange= function () { 
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			  		var response=xmlHttp.responseText; 
					document.getElementById('gallphoto').innerHTML=response;
					if (document.getElementById('ajx-stat-bar')){
						document.getElementById('ajx-stat-bar').innerHTML="on";
					}
					delCookie('ajax');
				}
			}
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
}

/**
 *	This function kills the the ajax process that is started with function cmsys_ajax and should return to that function
 *	it requests the ajax.kill.php proces that will trigger the ajax.php process so it can finsih
 */

function killajax() {
	if (checkCookie('ajax')){
		xmlHttp=GetXmlHttpObject();
			if (xmlHttp!=null){
				var nam=getCookie('ajax');
				var url="ajax.kill.php";
				url=url+"?ckie="+nam;
				url=url+"&kstr="+"{killing-ajax.php}";
				document.cookie = 'ajax'  + '; expires=Fri, 31 Dec 1999 23:59:59 GMT;' + 'path=/'; 
				xmlHttp.onreadystatechange= function () { 
				}
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
				// alert('Killing started ' + url);
			}
	}
	else {
		// alert('NOT killed');
	}
}

function AddToCookie(c_name,node){
	var ckie=getCookie(c_name);
	if (ckie.search(','+node+',') == -1){
		Put_Cookie(c_name,ckie+','+node+',');
	}
}

function SubFromCookie(c_name,node){
	var ckie=getCookie(c_name);
	if (ckie.search(','+node+',') != -1){
		ckie=ckie.replace(','+node+',',"");
		Put_Cookie(c_name,ckie);
	}
}

function adjustposition() {
	var myWidth = 0, myHeight = 0, bdyWidth=0, borWidth=0;

	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	bdyWidth = document.body.style.width.replace('px','')
	if (bdyWidth.indexOf('%') < 0 ) {
		borWidth = document.body.style.borderLeftWidth.replace('px','');
		bdyWidth = (1*(bdyWidth)) + (1*(borWidth));
		document.body.style.marginLeft = (myWidth-bdyWidth)/2;
		document.body.style.marginRight = document.body.style.marginLeft; 
		//alert(document.body.style.marginRight);
	} else {
		//alert(bdyWidth);
	}	
}


